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
use std::{
marker::PhantomData,
sync::{Arc, Mutex},
};
use engula_apis::*;
use crate::{expr::call, Client, Error, Object, Result};
#[derive(Clone)]
pub struct DatabaseTxn {
inner: Arc<DatabaseTxnInner>,
}
struct DatabaseTxnInner {
handle: DatabaseTxnHandle,
requests: Mutex<Vec<CollectionTxnRequest>>,
}
struct DatabaseTxnHandle {
dbname: String,
client: Client,
}
impl DatabaseTxn {
pub(crate) fn new(dbname: String, client: Client) -> Self {
let inner = DatabaseTxnInner {
handle: DatabaseTxnHandle { dbname, client },
requests: Mutex::new(Vec::new()),
};
Self {
inner: Arc::new(inner),
}
}
pub(crate) fn collection<T: Object>(&self, coname: String) -> CollectionTxn<T> {
CollectionTxn::new_with(coname, self.inner.clone())
}
pub async fn commit(self) -> Result<()> {
let inner =
Arc::try_unwrap(self.inner).map_err(|_| Error::aborted("pending transaction"))?;
let handle = inner.handle;
let req = DatabaseTxnRequest {
name: handle.dbname,
requests: inner.requests.into_inner().unwrap(),
};
handle.client.database_txn(req).await?;
Ok(())
}
}
pub struct CollectionTxn<T: Object> {
inner: Arc<CollectionTxnInner>,
subtxn: Option<T::Txn>,
_marker: PhantomData<T>,
}
struct CollectionTxnInner {
coname: String,
handle: Option<DatabaseTxnHandle>,
parent: Option<Arc<DatabaseTxnInner>>,
exprs: Mutex<Vec<Expr>>,
}
struct CollectionTxnHandle {
dbname: String,
coname: String,
client: Client,
}
impl<T: Object> CollectionTxn<T> {
pub(crate) fn new(dbname: String, coname: String, client: Client) -> Self {
let handle = DatabaseTxnHandle { dbname, client };
Self::new_inner(coname, Some(handle), None)
}
fn new_with(coname: String, parent: Arc<DatabaseTxnInner>) -> Self {
Self::new_inner(coname, None, Some(parent))
}
fn new_inner(
coname: String,
handle: Option<DatabaseTxnHandle>,
parent: Option<Arc<DatabaseTxnInner>>,
) -> Self {
let inner = CollectionTxnInner {
coname,
handle,
parent,
exprs: Mutex::new(Vec::new()),
};
Self {
inner: Arc::new(inner),
subtxn: None,
_marker: PhantomData,
}
}
pub fn object(&mut self, id: impl Into<Vec<u8>>) -> &mut T::Txn {
self.subtxn = Some(self.txn(id).into());
self.subtxn.as_mut().unwrap()
}
pub async fn commit(mut self) -> Result<()> {
self.subtxn.take();
let inner =
Arc::try_unwrap(self.inner).map_err(|_| Error::aborted("pending transaction"))?;
let req = CollectionTxnRequest {
name: inner.coname,
exprs: inner.exprs.into_inner().unwrap(),
};
if let Some(handle) = inner.handle {
handle.client.collection_txn(handle.dbname, req).await?;
} else {
let parent = inner.parent.unwrap();
parent.requests.lock().unwrap().push(req);
}
Ok(())
}
}
impl<T: Object> CollectionTxn<T> {
fn txn(&self, id: impl Into<Vec<u8>>) -> Txn {
Txn::new_with(id.into(), self.inner.clone())
}
pub fn set(&mut self, id: impl Into<Vec<u8>>, value: impl Into<T::Value>) {
self.txn(id).store(value.into());
}
pub fn delete(&mut self, id: impl Into<Vec<u8>>) {
self.txn(id).reset();
}
}
pub struct Txn {
handle: Option<CollectionTxnHandle>,
parent: Option<Arc<CollectionTxnInner>>,
expr: Expr,
}
impl Txn {
pub(crate) fn new(id: Vec<u8>, dbname: String, coname: String, client: Client) -> Self {
let handle = CollectionTxnHandle {
dbname,
coname,
client,
};
Self::new_inner(id, Some(handle), None)
}
fn new_with(id: Vec<u8>, parent: Arc<CollectionTxnInner>) -> Self {
Self::new_inner(id, None, Some(parent))
}
fn new_inner(
id: Vec<u8>,
handle: Option<CollectionTxnHandle>,
parent: Option<Arc<CollectionTxnInner>>,
) -> Self {
Self {
handle,
parent,
expr: Expr {
from: Some(expr::From::Id(id)),
..Default::default()
},
}
}
fn add_call(&mut self, call: CallExpr) -> &mut Self {
let expr = Expr {
call: Some(call),
..Default::default()
};
self.expr.subexprs.push(expr);
self
}
fn add_index_call(&mut self, index: impl Into<Value>, call: CallExpr) -> &mut Self {
let expr = Expr {
from: Some(expr::From::Index(index.into().into())),
call: Some(call),
..Default::default()
};
self.expr.subexprs.push(expr);
self
}
pub fn store(&mut self, value: impl Into<Value>) -> &mut Self {
self.add_call(call::store(value))
}
pub fn reset(&mut self) -> &mut Self {
self.add_call(call::reset())
}
pub fn add(&mut self, value: impl Into<Value>) -> &mut Self {
self.add_call(call::add(value))
}
pub fn sub(&mut self, value: impl Into<Value>) -> &mut Self {
self.add_call(call::sub(value))
}
pub(crate) fn append(&mut self, value: impl Into<Value>) -> &mut Self {
self.add_call(call::append(value))
}
pub(crate) fn push_back(&mut self, value: impl Into<Value>) -> &mut Self {
self.add_call(call::push_back(value))
}
pub(crate) fn push_front(&mut self, value: impl Into<Value>) -> &mut Self {
self.add_call(call::push_front(value))
}
pub(crate) fn set(&mut self, index: impl Into<Value>, value: impl Into<Value>) -> &mut Self {
self.add_index_call(index, call::store(value))
}
pub(crate) fn delete(&mut self, index: impl Into<Value>) -> &mut Self {
self.add_index_call(index, call::reset())
}
pub async fn commit(mut self) -> Result<()> {
if let Some(handle) = self.handle.take() {
let expr = std::mem::take(&mut self.expr);
handle
.client
.collection_expr(handle.dbname, handle.coname, expr)
.await?;
}
Ok(())
}
}
impl Drop for Txn {
fn drop(&mut self) {
if let Some(parent) = self.parent.take() {
let expr = std::mem::take(&mut self.expr);
parent.exprs.lock().unwrap().push(expr);
}
}
}