-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
subtxn.rs
269 lines (234 loc) · 8.29 KB
/
subtxn.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Copied from https://github.com/tcdi/pgx/pull/912 while it is WIP
// (rev cb812f4)
use pgx::{pg_sys, spi::SpiClient, PgMemoryContexts};
use std::fmt::Debug;
use std::ops::{Deref, DerefMut};
/// Releases a sub-transaction on Drop
pub trait ReleaseOnDrop {}
/// Sub-transaction's contextual information
#[derive(Clone, Copy)]
pub struct Context {
memory_context: pg_sys::MemoryContext,
// Resource ownership before the transaction
//
// Based on information from src/backend/utils/resowner/README
// as well as practical use of it in src/pl/plpython/plpy_spi.c
resource_owner: pg_sys::ResourceOwner,
}
impl Context {
/// Captures the context
fn capture() -> Self {
// Remember the memory context before starting the sub-transaction
let memory_context = PgMemoryContexts::CurrentMemoryContext.value();
// Remember resource owner before starting the sub-transaction
let resource_owner = unsafe { pg_sys::CurrentResourceOwner };
Self {
memory_context,
resource_owner,
}
}
}
impl From<Context> for CommitOnDrop {
fn from(context: Context) -> Self {
CommitOnDrop(context)
}
}
impl From<Context> for RollbackOnDrop {
fn from(context: Context) -> Self {
RollbackOnDrop(context)
}
}
/// Commits a sub-transaction on Drop
pub struct CommitOnDrop(Context);
impl Drop for CommitOnDrop {
fn drop(&mut self) {
unsafe {
pg_sys::ReleaseCurrentSubTransaction();
pg_sys::CurrentResourceOwner = self.0.resource_owner;
}
PgMemoryContexts::For(self.0.memory_context).set_as_current();
}
}
impl ReleaseOnDrop for CommitOnDrop {}
/// Rolls back a sub-transaction on Drop
pub struct RollbackOnDrop(Context);
impl Drop for RollbackOnDrop {
fn drop(&mut self) {
unsafe {
pg_sys::RollbackAndReleaseCurrentSubTransaction();
pg_sys::CurrentResourceOwner = self.0.resource_owner;
}
PgMemoryContexts::For(self.0.memory_context).set_as_current();
}
}
impl ReleaseOnDrop for RollbackOnDrop {}
impl Into<RollbackOnDrop> for CommitOnDrop {
fn into(self) -> RollbackOnDrop {
let result = RollbackOnDrop(self.0);
// IMPORTANT: avoid running Drop (that would commit)
std::mem::forget(self);
result
}
}
impl Into<CommitOnDrop> for RollbackOnDrop {
fn into(self) -> CommitOnDrop {
let result = CommitOnDrop(self.0);
// IMPORTANT: avoid running Drop (that would roll back)
std::mem::forget(self);
result
}
}
struct NoOpOnDrop;
impl ReleaseOnDrop for NoOpOnDrop {}
/// Sub-transaction
///
/// Can be created by calling `SpiClient::sub_transaction`, `SubTransaction<Parent>::sub_transaction`
/// or any other implementation of `SubTransactionExt` and obtaining it as an argument to the provided closure.
///
/// Unless rolled back or committed explicitly, it'll commit if `Release` generic parameter is `CommitOnDrop`
/// (default) or roll back if it is `RollbackOnDrop`.
#[derive(Debug)]
pub struct SubTransaction<Parent: SubTransactionExt, Release: ReleaseOnDrop = CommitOnDrop> {
// Transaction release mechanism (commit, drop)
release: Release,
// Transaction parent
parent: Parent,
}
impl<Parent: SubTransactionExt, Release: ReleaseOnDrop> SubTransaction<Parent, Release>
where
Release: From<Context>,
{
/// Create a new sub-transaction.
fn new(parent: Parent) -> Self {
let context = Context::capture();
let memory_context = context.memory_context;
let release = context.into();
unsafe {
pg_sys::BeginInternalSubTransaction(std::ptr::null() /* [no] transaction name */);
}
// Switch to the outer memory context so that all allocations remain
// there instead of the sub-transaction's context
PgMemoryContexts::For(memory_context).set_as_current();
Self { release, parent }
}
}
impl<Parent: SubTransactionExt> SubTransaction<Parent, CommitOnDrop> {
/// Commit the transaction, returning its parent
pub fn commit(self) -> Parent {
// `Self::do_nothing_on_drop()` will commit as `Release` is `CommitOnDrop`
self.do_nothing_on_drop().parent
}
}
impl<Parent: SubTransactionExt> SubTransaction<Parent, RollbackOnDrop> {
/// Commit the transaction, returning its parent
pub fn commit(self) -> Parent {
// Make sub-transaction commit on drop and then use `commit`
self.commit_on_drop().commit()
}
}
impl<Parent: SubTransactionExt> SubTransaction<Parent, RollbackOnDrop> {
/// Rollback the transaction, returning its parent
pub fn rollback(self) -> Parent {
// `Self::do_nothing_on_drop()` will roll back as `Release` is `RollbackOnDrop`
self.do_nothing_on_drop().parent
}
}
impl<Parent: SubTransactionExt> SubTransaction<Parent, CommitOnDrop> {
/// Rollback the transaction, returning its parent
pub fn rollback(self) -> Parent {
// Make sub-transaction roll back on drop and then use `rollback`
self.rollback_on_drop().rollback()
}
}
impl<Parent: SubTransactionExt> SubTransaction<Parent, CommitOnDrop> {
/// Make this sub-transaction roll back on drop
pub fn rollback_on_drop(self) -> SubTransaction<Parent, RollbackOnDrop> {
SubTransaction {
parent: self.parent,
release: self.release.into(),
}
}
}
impl<Parent: SubTransactionExt> SubTransaction<Parent, RollbackOnDrop> {
/// Make this sub-transaction commit on drop
pub fn commit_on_drop(self) -> SubTransaction<Parent, CommitOnDrop> {
SubTransaction {
parent: self.parent,
release: self.release.into(),
}
}
}
impl<Parent: SubTransactionExt, Release: ReleaseOnDrop> SubTransaction<Parent, Release> {
/// Make this sub-transaction do nothing on drop
///
/// Releases the sub-transaction based on `Release` generic parameter. Further
/// dropping of the sub-transaction will not do anything.
fn do_nothing_on_drop(self) -> SubTransaction<Parent, NoOpOnDrop> {
SubTransaction {
parent: self.parent,
release: NoOpOnDrop,
}
}
}
// This allows SubTransaction to be de-referenced to SpiClient
impl<'conn, Release: ReleaseOnDrop> Deref for SubTransaction<SpiClient<'conn>, Release> {
type Target = SpiClient<'conn>;
fn deref(&self) -> &Self::Target {
&self.parent
}
}
impl<'conn, Release: ReleaseOnDrop> DerefMut for SubTransaction<SpiClient<'conn>, Release> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.parent
}
}
// This allows a SubTransaction of a SubTransaction to be de-referenced to SpiClient
impl<Parent: SubTransactionExt, Release: ReleaseOnDrop> Deref
for SubTransaction<SubTransaction<Parent>, Release>
{
type Target = Parent;
fn deref(&self) -> &Self::Target {
&self.parent.parent
}
}
impl<Parent: SubTransactionExt, Release: ReleaseOnDrop> DerefMut
for SubTransaction<SubTransaction<Parent>, Release>
{
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.parent.parent
}
}
/// Trait that allows creating a sub-transaction off any type
pub trait SubTransactionExt {
/// Parent's type
///
/// In most common cases, it'll be equal to `Self`. However, in some cases
/// it may be desirable to use a different type to achieve certain goals.
type Parent: SubTransactionExt;
/// Consume `self` and execute a closure with a sub-transaction
///
/// If further use of the given sub-transaction is necessary, it must
/// be returned by the closure alongside with its intended result. Otherwise,
/// the sub-transaction be released when dropped.
fn sub_transaction<F: FnOnce(SubTransaction<Self::Parent>) -> R, R>(self, f: F) -> R
where
Self: Sized;
}
impl<'a> SubTransactionExt for SpiClient<'a> {
type Parent = Self;
fn sub_transaction<F: FnOnce(SubTransaction<Self::Parent>) -> R, R>(self, f: F) -> R
where
Self: Sized,
{
f(SubTransaction::new(self))
}
}
impl<Parent: SubTransactionExt> SubTransactionExt for SubTransaction<Parent> {
type Parent = Self;
fn sub_transaction<F: FnOnce(SubTransaction<Self::Parent>) -> R, R>(self, f: F) -> R
where
Self: Sized,
{
f(SubTransaction::new(self))
}
}