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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
use crate::{
bucket::DEFAULT_BUCKET_NAME,
db::Db,
error::Result,
iterator::Iter,
options::WriteOptions,
types::{CommitInfo, KeyRange, ReadVersion, Sequence, Value},
write_batch::WriteBatch,
};
/// Options used by optimistic transactions.
///
/// The options are copied into the transaction when it is created. Changing a
/// separate `TransactionOptions` value later does not affect an existing
/// transaction.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct TransactionOptions {
/// Write options used when the transaction commits.
pub write_options: WriteOptions,
}
/// Optimistic transaction over one read snapshot and a staged write batch.
///
/// Methods without a bucket suffix read or write the built-in default bucket.
/// Methods ending in `_bucket` operate on optional named buckets.
///
/// Reads are performed at the transaction's `read_sequence` and recorded in a
/// read set. Writes are staged in memory through a [`WriteBatch`]. Commit checks
/// whether any later committed point write, point delete, or range delete
/// conflicts with the recorded reads; if so, commit returns
/// [`crate::Error::Conflict`] and none of the staged writes are accepted.
///
/// # Examples
///
/// ```rust
/// use trine_kv::{Db, TransactionOptions};
///
/// # fn main() -> trine_kv::Result<()> {
/// let db = Db::open_sync(trine_kv::DbOptions::memory())?;
/// db.put_sync(b"counter", b"0")?;
///
/// let mut tx = db.transaction(TransactionOptions::default());
/// let current = tx.get_sync(b"counter")?;
/// assert_eq!(current, Some(b"0".to_vec()));
///
/// tx.put(b"counter", b"1");
/// let commit = tx.commit_sync()?;
/// assert!(commit.read_version().as_u64() > 0);
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct Transaction {
db: Db,
read_sequence: Sequence,
options: TransactionOptions,
writes: WriteBatch,
point_reads: Vec<ReadKey>,
range_reads: Vec<ReadRange>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ReadKey {
pub(crate) bucket: String,
pub(crate) key: Vec<u8>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ReadRange {
pub(crate) bucket: String,
pub(crate) range: KeyRange,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(crate) struct TransactionReadSet {
pub(crate) point_reads: Vec<ReadKey>,
pub(crate) range_reads: Vec<ReadRange>,
}
impl Transaction {
#[must_use]
pub(crate) fn new(db: Db, read_sequence: Sequence, options: TransactionOptions) -> Self {
Self {
db,
read_sequence,
options,
writes: WriteBatch::new(),
point_reads: Vec::new(),
range_reads: Vec::new(),
}
}
/// Returns the public read version used by this transaction's read
/// snapshot.
///
/// All transaction reads use this read boundary, even if newer writes
/// commit before the transaction commits.
#[must_use]
pub const fn read_version(&self) -> ReadVersion {
ReadVersion::from_sequence(self.read_sequence)
}
/// Returns this transaction's options.
#[must_use]
pub const fn options(&self) -> TransactionOptions {
self.options
}
/// Reads a default-bucket key and tracks it for commit conflict checks.
///
/// # Parameters
///
/// - `key`: user key bytes in the built-in default bucket.
///
/// The exact key is added to the read set after the read succeeds. Commit
/// fails if a later committed write or delete touches the key, or if a later
/// range delete covers it.
pub fn get_sync(&mut self, key: &[u8]) -> Result<Option<Value>> {
self.get_bucket_sync(DEFAULT_BUCKET_NAME, key)
}
/// Reads a named-bucket key and tracks it for commit conflict checks.
///
/// # Parameters
///
/// - `bucket`: target named bucket.
/// - `key`: user key bytes.
pub fn get_bucket_sync(
&mut self,
bucket: impl Into<String>,
key: &[u8],
) -> Result<Option<Value>> {
let bucket = bucket.into();
let value = self.db.get_at_sequence(&bucket, key, self.read_sequence)?;
// Record the exact user key read at the transaction's read sequence.
// Commit validation rejects the transaction if a later committed point
// write, point delete, or covering range delete touched it.
self.point_reads.push(ReadKey {
bucket,
key: key.to_vec(),
});
Ok(value)
}
/// Reads a default-bucket range and tracks it for commit conflict checks.
///
/// The range cursor is fully consumed before the range is accepted into the
/// read set. That means table or blob read errors are returned immediately
/// instead of being deferred until commit.
///
/// Commit fails if a later committed point mutation falls inside the range
/// or if a later range delete overlaps it.
pub fn read_range_sync(&mut self, range: KeyRange) -> Result<()> {
self.read_range_bucket_sync(DEFAULT_BUCKET_NAME, range)
}
/// Reads a named-bucket range and tracks it for commit conflict checks.
pub fn read_range_bucket_sync(
&mut self,
bucket: impl Into<String>,
range: KeyRange,
) -> Result<()> {
self.db.ensure_open()?;
let bucket = bucket.into();
let iter = self.db.range_at_sequence(
&bucket,
&range,
self.read_sequence,
crate::Direction::Forward,
)?;
// The transaction API records a range that was actually read at the
// transaction sequence. Consume the cursor here so table/blob read
// errors are returned before the read set is accepted.
for item in iter {
item?;
}
// Range reads conflict with any later committed point mutation inside
// the range, plus any later range tombstone that overlaps it.
self.range_reads.push(ReadRange { bucket, range });
Ok(())
}
/// Reads the default-bucket range and returns its cursor, tracking the
/// range for commit conflict checks.
pub fn range_sync(&mut self, range: KeyRange) -> Result<Iter> {
self.range_bucket_sync(DEFAULT_BUCKET_NAME, range)
}
/// Reads a named-bucket range and returns its cursor, tracking the range for
/// commit conflict checks.
///
/// Unlike [`read_range_bucket_sync`](Self::read_range_bucket_sync), the data
/// cursor is returned to the caller rather than consumed: this is the read
/// path for transactions that need the range's values (e.g. a scan), not
/// just conflict tracking. The range is recorded in the read set at the
/// transaction's read sequence; iteration errors surface as the caller
/// drives the returned cursor. Commit fails if a later committed point
/// mutation falls inside the range or a later range delete overlaps it.
pub fn range_bucket_sync(
&mut self,
bucket: impl Into<String>,
range: KeyRange,
) -> Result<Iter> {
self.db.ensure_open()?;
let bucket = bucket.into();
let iter = self.db.range_at_sequence(
&bucket,
&range,
self.read_sequence,
crate::Direction::Forward,
)?;
self.range_reads.push(ReadRange { bucket, range });
Ok(iter)
}
/// Stages one key/value write for the default bucket.
///
/// Staging only mutates the in-memory transaction batch. The write is not
/// visible and does not reserve a commit sequence until commit succeeds.
pub fn put(&mut self, key: impl Into<Vec<u8>>, value: impl Into<Value>) {
self.writes.put(key, value);
}
/// Stages one key/value write for a named bucket.
pub fn put_bucket(
&mut self,
bucket: impl Into<String>,
key: impl Into<Vec<u8>>,
value: impl Into<Value>,
) -> Result<()> {
self.writes.put_bucket(bucket, key, value)
}
/// Stages a point delete for the default bucket.
pub fn delete(&mut self, key: impl Into<Vec<u8>>) {
self.writes.delete(key);
}
/// Stages a point delete for a named bucket.
pub fn delete_bucket(
&mut self,
bucket: impl Into<String>,
key: impl Into<Vec<u8>>,
) -> Result<()> {
self.writes.delete_bucket(bucket, key)
}
/// Stages a range delete for the default bucket.
pub fn delete_range(&mut self, range: KeyRange) {
self.writes.delete_range(range);
}
/// Stages a range delete for a named bucket.
pub fn delete_range_bucket(
&mut self,
bucket: impl Into<String>,
range: KeyRange,
) -> Result<()> {
self.writes.delete_range_bucket(bucket, range)
}
/// Commits the staged writes synchronously after conflict checks.
///
/// Commit consumes the transaction. If conflict validation succeeds, Trine
/// commits all staged writes as one atomic batch using
/// `self.options().write_options`. If validation fails, the staged writes
/// are not accepted.
///
/// # Returns
///
/// Returns [`CommitInfo`] with the assigned commit sequence.
///
/// # Errors
///
/// Returns [`crate::Error::Conflict`] if the read set was invalidated, or
/// the same write errors as [`crate::Db::write_sync`] for storage,
/// durability, or closed/read-only handle failures.
pub fn commit_sync(self) -> Result<CommitInfo> {
let read_set = TransactionReadSet {
point_reads: self.point_reads,
range_reads: self.range_reads,
};
self.db.commit_transaction(
self.read_sequence,
read_set,
self.writes,
self.options.write_options,
)
}
}
/// Primary async transaction read/commit API. Staged write builders stay
/// synchronous because they only mutate the in-memory transaction batch.
#[allow(clippy::unused_async)]
impl Transaction {
/// Reads a default-bucket key and tracks it for commit conflict checks.
pub async fn get(&mut self, key: &[u8]) -> Result<Option<Value>> {
self.get_bucket(DEFAULT_BUCKET_NAME, key).await
}
/// Reads a named-bucket key and tracks it for commit conflict checks.
pub async fn get_bucket(
&mut self,
bucket: impl Into<String>,
key: &[u8],
) -> Result<Option<Value>> {
let bucket = bucket.into();
let value = self
.db
.get_at_sequence_async(&bucket, key, self.read_sequence)
.await?;
self.point_reads.push(ReadKey {
bucket,
key: key.to_vec(),
});
Ok(value)
}
/// Reads a default-bucket range and tracks it for commit conflict checks.
pub async fn read_range(&mut self, range: KeyRange) -> Result<()> {
self.read_range_bucket(DEFAULT_BUCKET_NAME, range).await
}
/// Reads a named-bucket range and tracks it for commit conflict checks.
pub async fn read_range_bucket(
&mut self,
bucket: impl Into<String>,
range: KeyRange,
) -> Result<()> {
self.db.ensure_open()?;
let bucket = bucket.into();
let mut iter = self
.db
.range_at_sequence_async(
&bucket,
&range,
self.read_sequence,
crate::Direction::Forward,
)
.await?;
while iter.next().await?.is_some() {}
self.range_reads.push(ReadRange { bucket, range });
Ok(())
}
/// Reads the default-bucket range and returns its cursor, tracking the
/// range for commit conflict checks.
pub async fn range(&mut self, range: KeyRange) -> Result<Iter> {
self.range_bucket(DEFAULT_BUCKET_NAME, range).await
}
/// Reads a named-bucket range and returns its cursor, tracking the range for
/// commit conflict checks. The async counterpart of
/// [`range_bucket_sync`](Self::range_bucket_sync).
pub async fn range_bucket(
&mut self,
bucket: impl Into<String>,
range: KeyRange,
) -> Result<Iter> {
self.db.ensure_open()?;
let bucket = bucket.into();
let iter = self
.db
.range_at_sequence_async(
&bucket,
&range,
self.read_sequence,
crate::Direction::Forward,
)
.await?;
self.range_reads.push(ReadRange { bucket, range });
Ok(iter)
}
/// Commits the staged writes asynchronously after conflict checks.
pub async fn commit(self) -> Result<CommitInfo> {
let read_set = TransactionReadSet {
point_reads: self.point_reads,
range_reads: self.range_reads,
};
self.db
.commit_transaction_async(
self.read_sequence,
read_set,
self.writes,
self.options.write_options,
)
.await
}
}