Repository navigation
Expand file tree
/
Copy pathmod.rs
More file actions
704 lines (671 loc) · 24.2 KB
/
Copy pathmod.rs
File metadata and controls
704 lines (671 loc) · 24.2 KB
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
#![cfg(feature = "kv-rocksdb")]
mod cnf;
use crate::err::Error;
use crate::key::debug::Sprintable;
use crate::kvs::{Check, Key, Val};
use anyhow::{Result, bail, ensure};
use rocksdb::{
BlockBasedOptions, Cache, DBCompactionStyle, DBCompressionType, FlushOptions, LogLevel,
OptimisticTransactionDB, OptimisticTransactionOptions, Options, ReadOptions, WriteOptions,
};
use std::ops::Range;
use std::pin::Pin;
use std::sync::Arc;
use std::thread;
use std::time::Duration;
use super::savepoint::SavePoints;
const TARGET: &str = "surrealdb::core::kvs::rocksdb";
pub struct Datastore {
db: Pin<Arc<OptimisticTransactionDB>>,
}
pub struct Transaction {
/// Is the transaction complete?
done: bool,
/// Is the transaction writeable?
write: bool,
/// Should we check unhandled transactions?
check: Check,
/// The underlying datastore transaction
inner: Option<rocksdb::Transaction<'static, OptimisticTransactionDB>>,
/// The read options containing the Snapshot
ro: ReadOptions,
// The above, supposedly 'static transaction
// actually points here, so we need to ensure
// the memory is kept alive. This pointer must
// be declared last, so that it is dropped last.
_db: Pin<Arc<OptimisticTransactionDB>>,
}
impl Drop for Transaction {
fn drop(&mut self) {
if !self.done && self.write {
match self.check {
Check::None => {
trace!("A transaction was dropped without being committed or cancelled");
}
Check::Warn => {
warn!("A transaction was dropped without being committed or cancelled");
}
Check::Error => {
error!("A transaction was dropped without being committed or cancelled");
}
}
}
}
}
impl Datastore {
/// Open a new database
pub(crate) async fn new(path: &str) -> Result<Datastore> {
// Configure custom options
let mut opts = Options::default();
// Ensure we use fdatasync
opts.set_use_fsync(false);
// Create database if missing
opts.create_if_missing(true);
// Create column families if missing
opts.create_missing_column_families(true);
// Increase the background thread count
info!(target: TARGET, "Background thread count: {}", *cnf::ROCKSDB_THREAD_COUNT);
opts.increase_parallelism(*cnf::ROCKSDB_THREAD_COUNT);
// Specify the max concurrent background jobs
info!(target: TARGET, "Maximum background jobs count: {}", *cnf::ROCKSDB_JOBS_COUNT);
opts.set_max_background_jobs(*cnf::ROCKSDB_JOBS_COUNT);
// Set the maximum number of open files that can be used by the database
info!(target: TARGET, "Maximum number of open files: {}", *cnf::ROCKSDB_MAX_OPEN_FILES);
opts.set_max_open_files(*cnf::ROCKSDB_MAX_OPEN_FILES);
// Set the number of log files to keep
info!(target: TARGET, "Number of log files to keep: {}", *cnf::ROCKSDB_KEEP_LOG_FILE_NUM);
opts.set_keep_log_file_num(*cnf::ROCKSDB_KEEP_LOG_FILE_NUM);
// Set the maximum number of write buffers
info!(target: TARGET, "Maximum write buffers: {}", *cnf::ROCKSDB_MAX_WRITE_BUFFER_NUMBER);
opts.set_max_write_buffer_number(*cnf::ROCKSDB_MAX_WRITE_BUFFER_NUMBER);
// Set the amount of data to build up in memory
info!(target: TARGET, "Write buffer size: {}", *cnf::ROCKSDB_WRITE_BUFFER_SIZE);
opts.set_write_buffer_size(*cnf::ROCKSDB_WRITE_BUFFER_SIZE);
// Set the target file size for compaction
info!(target: TARGET, "Target file size for compaction: {}", *cnf::ROCKSDB_TARGET_FILE_SIZE_BASE);
opts.set_target_file_size_base(*cnf::ROCKSDB_TARGET_FILE_SIZE_BASE);
// Set the levelled target file size multipler
info!(target: TARGET, "Target file size compaction multiplier: {}", *cnf::ROCKSDB_TARGET_FILE_SIZE_MULTIPLIER);
opts.set_target_file_size_multiplier(*cnf::ROCKSDB_TARGET_FILE_SIZE_MULTIPLIER);
// Set minimum number of write buffers to merge
info!(target: TARGET, "Minimum write buffers to merge: {}", *cnf::ROCKSDB_MIN_WRITE_BUFFER_NUMBER_TO_MERGE);
opts.set_min_write_buffer_number_to_merge(*cnf::ROCKSDB_MIN_WRITE_BUFFER_NUMBER_TO_MERGE);
// Delay compaction until the minimum number of files accumulate
info!(target: TARGET, "Number of files to trigger compaction: {}", *cnf::ROCKSDB_FILE_COMPACTION_TRIGGER);
opts.set_level_zero_file_num_compaction_trigger(*cnf::ROCKSDB_FILE_COMPACTION_TRIGGER);
// Set the compaction readahead size
info!(target: TARGET, "Compaction readahead size: {}", *cnf::ROCKSDB_COMPACTION_READAHEAD_SIZE);
opts.set_compaction_readahead_size(*cnf::ROCKSDB_COMPACTION_READAHEAD_SIZE);
// Set the max number of subcompactions
info!(target: TARGET, "Maximum concurrent subcompactions: {}", *cnf::ROCKSDB_MAX_CONCURRENT_SUBCOMPACTIONS);
opts.set_max_subcompactions(*cnf::ROCKSDB_MAX_CONCURRENT_SUBCOMPACTIONS);
// Use separate write thread queues
info!(target: TARGET, "Use separate thread queues: {}", *cnf::ROCKSDB_ENABLE_PIPELINED_WRITES);
opts.set_enable_pipelined_write(*cnf::ROCKSDB_ENABLE_PIPELINED_WRITES);
// Enable separation of keys and values
info!(target: TARGET, "Enable separation of keys and values: {}", *cnf::ROCKSDB_ENABLE_BLOB_FILES);
opts.set_enable_blob_files(*cnf::ROCKSDB_ENABLE_BLOB_FILES);
// Store large values separate from keys
info!(target: TARGET, "Minimum blob value size: {}", *cnf::ROCKSDB_MIN_BLOB_SIZE);
opts.set_min_blob_size(*cnf::ROCKSDB_MIN_BLOB_SIZE);
// Set the write-ahead-log size limit in MB
info!(target: TARGET, "Write-ahead-log file size limit: {}MB", *cnf::ROCKSDB_WAL_SIZE_LIMIT);
opts.set_wal_size_limit_mb(*cnf::ROCKSDB_WAL_SIZE_LIMIT);
// Allow multiple writers to update memtables in parallel
info!(target: TARGET, "Allow concurrent memtable writes: true");
opts.set_allow_concurrent_memtable_write(true);
// Avoid unnecessary blocking io, preferring background threads
info!(target: TARGET, "Avoid unnecessary blocking IO: true");
opts.set_avoid_unnecessary_blocking_io(true);
// Improve concurrency from write batch mutex
info!(target: TARGET, "Allow adaptive write thread yielding: true");
opts.set_enable_write_thread_adaptive_yield(true);
// Log if writes should be synced
info!(target: TARGET, "Wait for disk sync acknowledgement: {}", *cnf::SYNC_DATA);
// Set the block cache size in bytes
info!(target: TARGET, "Block cache size: {}", *cnf::ROCKSDB_BLOCK_CACHE_SIZE);
// Configure the in-memory cache options
let cache = Cache::new_lru_cache(*cnf::ROCKSDB_BLOCK_CACHE_SIZE);
// Configure the block based file options
let mut block_opts = BlockBasedOptions::default();
block_opts.set_pin_l0_filter_and_index_blocks_in_cache(true);
block_opts.set_pin_top_level_index_and_filter(true);
block_opts.set_bloom_filter(10.0, false);
block_opts.set_block_size(*cnf::ROCKSDB_BLOCK_SIZE);
block_opts.set_block_cache(&cache);
// Configure the database with the cache
opts.set_block_based_table_factory(&block_opts);
opts.set_blob_cache(&cache);
opts.set_row_cache(&cache);
// Configure memory-mapped reads
info!(target: TARGET, "Enable memory-mapped reads: {}", *cnf::ROCKSDB_ENABLE_MEMORY_MAPPED_READS);
opts.set_allow_mmap_reads(*cnf::ROCKSDB_ENABLE_MEMORY_MAPPED_READS);
// Configure memory-mapped writes
info!(target: TARGET, "Enable memory-mapped writes: {}", *cnf::ROCKSDB_ENABLE_MEMORY_MAPPED_WRITES);
opts.set_allow_mmap_writes(*cnf::ROCKSDB_ENABLE_MEMORY_MAPPED_WRITES);
// Set the delete compaction factory
info!(target: TARGET, "Setting delete compaction factory: {} / {} ({})",
*cnf::ROCKSDB_DELETION_FACTORY_WINDOW_SIZE,
*cnf::ROCKSDB_DELETION_FACTORY_DELETE_COUNT,
*cnf::ROCKSDB_DELETION_FACTORY_RATIO,
);
opts.add_compact_on_deletion_collector_factory(
*cnf::ROCKSDB_DELETION_FACTORY_WINDOW_SIZE,
*cnf::ROCKSDB_DELETION_FACTORY_DELETE_COUNT,
*cnf::ROCKSDB_DELETION_FACTORY_RATIO,
);
// Set the datastore compaction style
info!(target: TARGET, "Setting compaction style: {}", *cnf::ROCKSDB_COMPACTION_STYLE);
opts.set_compaction_style(
match cnf::ROCKSDB_COMPACTION_STYLE.to_ascii_lowercase().as_str() {
"universal" => DBCompactionStyle::Universal,
_ => DBCompactionStyle::Level,
},
);
// Set specific compression levels
info!(target: TARGET, "Setting compression level");
opts.set_compression_per_level(&[
DBCompressionType::None,
DBCompressionType::None,
DBCompressionType::Snappy,
DBCompressionType::Snappy,
DBCompressionType::Snappy,
]);
// Set specific storage log level
info!(target: TARGET, "Setting storage engine log level: {}", *cnf::ROCKSDB_STORAGE_LOG_LEVEL);
opts.set_log_level(match cnf::ROCKSDB_STORAGE_LOG_LEVEL.to_ascii_lowercase().as_str() {
"debug" => LogLevel::Debug,
"info" => LogLevel::Info,
"warn" => LogLevel::Warn,
"error" => LogLevel::Error,
"fatal" => LogLevel::Fatal,
l => {
bail!(Error::Ds(format!("Invalid storage engine log level specified: {l}")));
}
});
// Configure background WAL flush behaviour
let db = match *cnf::ROCKSDB_BACKGROUND_FLUSH {
// Beckground flush is disabled which
// means that the WAL will be flushed
// whenever a transaction is committed.
false => {
// Dispay the configuration setting
info!(target: TARGET, "Background write-ahead-log flushing: disabled");
// Enable manual WAL flush
opts.set_manual_wal_flush(false);
// Create the optimistic datastore
Arc::pin(OptimisticTransactionDB::open(&opts, path)?)
}
// Background flush is enabled so we
// spawn a background worker thread to
// flush the WAL to disk periodically.
true => {
// Dispay the configuration setting
info!(target: TARGET, "Background write-ahead-log flushing: enabled every {}ms", *cnf::ROCKSDB_BACKGROUND_FLUSH_INTERVAL);
// Enable manual WAL flush
opts.set_manual_wal_flush(true);
// Create the optimistic datastore
let db = Arc::pin(OptimisticTransactionDB::open(&opts, path)?);
// Clone the database reference
let dbc = db.clone();
// Create a new background thread
thread::spawn(move || {
loop {
// Get the specified flush interval
let wait = *cnf::ROCKSDB_BACKGROUND_FLUSH_INTERVAL;
// Wait for the specified interval
thread::sleep(Duration::from_millis(wait));
// Flush the WAL to disk periodically
if let Err(err) = dbc.flush_wal(*cnf::SYNC_DATA) {
error!("Failed to flush WAL: {err}");
}
}
});
// Return the datastore
db
}
};
// Return the datastore
Ok(Datastore {
db,
})
}
/// Shutdown the database
pub(crate) async fn shutdown(&self) -> Result<()> {
// Create new flush options
let mut opts = FlushOptions::default();
// Wait for the sync to finish
opts.set_wait(true);
// Flush the WAL to storage
if let Err(e) = self.db.flush_wal(true) {
error!("An error occured flushing the WAL buffer to disk: {e}");
}
// Flush the memtables to SST
if let Err(e) = self.db.flush_opt(&opts) {
error!("An error occured flushing memtables to SST files: {e}");
}
// All good
Ok(())
}
/// Start a new transaction
pub(crate) async fn transaction(
&self,
write: bool,
_: bool,
) -> Result<Box<dyn crate::kvs::api::Transaction>> {
// Set the transaction options
let mut to = OptimisticTransactionOptions::default();
to.set_snapshot(true);
// Set the write options
let mut wo = WriteOptions::default();
wo.set_sync(*cnf::SYNC_DATA);
// Create a new transaction
let inner = self.db.transaction_opt(&wo, &to);
// The database reference must always outlive
// the transaction. If it doesn't then this
// is undefined behaviour. This unsafe block
// ensures that the transaction reference is
// static, but will cause a crash if the
// datastore is dropped prematurely.
let inner = unsafe {
std::mem::transmute::<
rocksdb::Transaction<'_, OptimisticTransactionDB>,
rocksdb::Transaction<'static, OptimisticTransactionDB>,
>(inner)
};
// Set the read options
let mut ro = ReadOptions::default();
ro.set_snapshot(&inner.snapshot());
ro.set_async_io(true);
ro.fill_cache(true);
// Specify the check level
#[cfg(not(debug_assertions))]
let check = Check::Warn;
#[cfg(debug_assertions)]
let check = Check::Error;
// Create a new transaction
Ok(Box::new(Transaction {
done: false,
write,
check,
inner: Some(inner),
ro,
_db: self.db.clone(),
}))
}
}
#[cfg_attr(target_family = "wasm", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_family = "wasm"), async_trait::async_trait)]
impl super::api::Transaction for Transaction {
fn kind(&self) -> &'static str {
"rocksdb"
}
/// Behaviour if unclosed
fn check_level(&mut self, check: Check) {
self.check = check;
}
/// Check if closed
fn closed(&self) -> bool {
self.done
}
/// Check if writeable
fn writeable(&self) -> bool {
self.write
}
/// Cancel a transaction
#[instrument(level = "trace", target = "surrealdb::core::kvs::api", skip(self))]
async fn cancel(&mut self) -> Result<()> {
// Check to see if transaction is closed
ensure!(!self.done, Error::TxFinished);
// Mark this transaction as done
self.done = true;
// Cancel this transaction
self.inner.as_ref().unwrap().rollback()?;
// Continue
Ok(())
}
/// Commit a transaction
#[instrument(level = "trace", target = "surrealdb::core::kvs::api", skip(self))]
async fn commit(&mut self) -> Result<()> {
// Check to see if transaction is closed
ensure!(!self.done, Error::TxFinished);
// Check to see if transaction is writable
ensure!(self.write, Error::TxReadonly);
// Mark this transaction as done
self.done = true;
// Commit this transaction
self.inner.take().unwrap().commit()?;
// Continue
Ok(())
}
/// Check if a key exists
#[instrument(level = "trace", target = "surrealdb::core::kvs::api", skip(self), fields(key = key.sprint()))]
async fn exists(&mut self, key: Key, version: Option<u64>) -> Result<bool> {
// RocksDB does not support versioned queries.
ensure!(version.is_none(), Error::UnsupportedVersionedQueries);
// Check to see if transaction is closed
ensure!(!self.done, Error::TxFinished);
// Get the key
let res = self.inner.as_ref().unwrap().get_pinned_opt(key, &self.ro)?.is_some();
// Return result
Ok(res)
}
/// Fetch a key from the database
#[instrument(level = "trace", target = "surrealdb::core::kvs::api", skip(self), fields(key = key.sprint()))]
async fn get(&mut self, key: Key, version: Option<u64>) -> Result<Option<Val>> {
// RocksDB does not support versioned queries.
ensure!(version.is_none(), Error::UnsupportedVersionedQueries);
// Check to see if transaction is closed
ensure!(!self.done, Error::TxFinished);
// Get the key
let res = self.inner.as_ref().unwrap().get_opt(key, &self.ro)?;
// Return result
Ok(res)
}
/// Fetch many keys from the datastore.
#[instrument(level = "trace", target = "surrealdb::core::kvs::api", skip(self), fields(keys = keys.sprint()))]
async fn getm(&mut self, keys: Vec<Key>) -> Result<Vec<Option<Val>>> {
// Check to see if transaction is closed
ensure!(!self.closed(), Error::TxFinished);
// Check to see if transaction is closed
ensure!(!self.done, Error::TxFinished);
// Get the keys
let res = self.inner.as_ref().unwrap().multi_get_opt(keys, &self.ro);
// Convert result
let res = res.into_iter().collect::<Result<_, _>>()?;
// Return result
Ok(res)
}
/// Insert or update a key in the database
#[instrument(level = "trace", target = "surrealdb::core::kvs::api", skip(self), fields(key = key.sprint()))]
async fn set(&mut self, key: Key, val: Val, version: Option<u64>) -> Result<()> {
// RocksDB does not support versioned queries.
ensure!(version.is_none(), Error::UnsupportedVersionedQueries);
// Check to see if transaction is closed
ensure!(!self.done, Error::TxFinished);
// Check to see if transaction is writable
ensure!(self.write, Error::TxReadonly);
// Set the key
self.inner.as_ref().unwrap().put(key, val)?;
// Return result
Ok(())
}
/// Insert a key if it doesn't exist in the database
#[instrument(level = "trace", target = "surrealdb::core::kvs::api", skip(self), fields(key = key.sprint()))]
async fn put(&mut self, key: Key, val: Val, version: Option<u64>) -> Result<()> {
// RocksDB does not support versioned queries.
ensure!(version.is_none(), Error::UnsupportedVersionedQueries);
// Check to see if transaction is closed
ensure!(!self.done, Error::TxFinished);
// Check to see if transaction is writable
ensure!(self.write, Error::TxReadonly);
// Set the key if empty
match self.inner.as_ref().unwrap().get_pinned_opt(&key, &self.ro)? {
None => self.inner.as_ref().unwrap().put(key, val)?,
_ => bail!(Error::TxKeyAlreadyExists),
};
// Return result
Ok(())
}
/// Insert a key if the current value matches a condition
#[instrument(level = "trace", target = "surrealdb::core::kvs::api", skip(self), fields(key = key.sprint()))]
async fn putc(&mut self, key: Key, val: Val, chk: Option<Val>) -> Result<()> {
// Check to see if transaction is closed
ensure!(!self.done, Error::TxFinished);
// Check to see if transaction is writable
ensure!(self.write, Error::TxReadonly);
// Set the key if empty
match (self.inner.as_ref().unwrap().get_pinned_opt(&key, &self.ro)?, chk) {
(Some(v), Some(w)) if v.eq(&w) => self.inner.as_ref().unwrap().put(key, val)?,
(None, None) => self.inner.as_ref().unwrap().put(key, val)?,
_ => bail!(Error::TxConditionNotMet),
};
// Return result
Ok(())
}
/// Delete a key
#[instrument(level = "trace", target = "surrealdb::core::kvs::api", skip(self), fields(key = key.sprint()))]
async fn del(&mut self, key: Key) -> Result<()> {
// Check to see if transaction is closed
ensure!(!self.done, Error::TxFinished);
// Check to see if transaction is writable
ensure!(self.write, Error::TxReadonly);
// Remove the key
self.inner.as_ref().unwrap().delete(key)?;
// Return result
Ok(())
}
/// Delete a key if the current value matches a condition
#[instrument(level = "trace", target = "surrealdb::core::kvs::api", skip(self), fields(key = key.sprint()))]
async fn delc(&mut self, key: Key, chk: Option<Val>) -> Result<()> {
// Check to see if transaction is closed
ensure!(!self.done, Error::TxFinished);
// Check to see if transaction is writable
ensure!(self.write, Error::TxReadonly);
// Delete the key if valid
match (self.inner.as_ref().unwrap().get_pinned_opt(&key, &self.ro)?, chk) {
(Some(v), Some(w)) if v.eq(&w) => self.inner.as_ref().unwrap().delete(key)?,
(None, None) => self.inner.as_ref().unwrap().delete(key)?,
_ => bail!(Error::TxConditionNotMet),
};
// Return result
Ok(())
}
/// Retrieve a range of keys from the databases
#[instrument(level = "trace", target = "surrealdb::core::kvs::api", skip(self), fields(rng = rng.sprint()))]
async fn keys(
&mut self,
rng: Range<Key>,
limit: u32,
version: Option<u64>,
) -> Result<Vec<Key>> {
let rng = self.prepare_scan(rng, version).await?;
// Execute on the blocking threadpool
let res = affinitypool::spawn_local(move || {
// Create result set
let mut res = vec![];
// Set the key range
let beg = rng.start.as_slice();
let end = rng.end.as_slice();
// Set the ReadOptions with the snapshot
let mut ro = ReadOptions::default();
ro.set_snapshot(&self.inner.as_ref().unwrap().snapshot());
ro.set_iterate_lower_bound(beg);
ro.set_iterate_upper_bound(end);
ro.set_async_io(true);
ro.fill_cache(true);
// Create the iterator
let mut iter = self.inner.as_ref().unwrap().raw_iterator_opt(ro);
// Seek to the start key
iter.seek(&rng.start);
// Check the scan limit
while res.len() < limit as usize {
// Check the key and value
if let Some(k) = iter.key() {
// Check the range validity
if k >= beg && k < end {
res.push(k.to_vec());
iter.next();
continue;
}
}
// Exit
break;
}
// Drop the iterator
drop(iter);
// Return result
res
})
.await;
// Return result
Ok(res)
}
/// Retrieve a range of keys from the databases
#[instrument(level = "trace", target = "surrealdb::core::kvs::api", skip(self), fields(rng = rng.sprint()))]
async fn keysr(
&mut self,
rng: Range<Key>,
limit: u32,
version: Option<u64>,
) -> Result<Vec<Key>> {
let rng = self.prepare_scan(rng, version).await?;
// Get the transaction
let inner = self.inner.as_ref().unwrap();
// Create result set
let mut res = vec![];
// Set the key range
let beg = rng.start.as_slice();
let end = rng.end.as_slice();
// Set the ReadOptions with the snapshot
let mut ro = ReadOptions::default();
ro.set_snapshot(&inner.snapshot());
ro.set_iterate_lower_bound(beg);
ro.set_iterate_upper_bound(end);
ro.set_async_io(true);
ro.fill_cache(true);
// Create the iterator
let mut iter = inner.raw_iterator_opt(ro);
// Seek to the start key
iter.seek_for_prev(&rng.end);
// Check the scan limit
while res.len() < limit as usize {
// Check the key and value
if let Some(k) = iter.key() {
// Check the range validity
if k >= beg && k < end {
res.push(k.to_vec());
iter.prev();
continue;
}
}
// Exit
break;
}
// Return result
Ok(res)
}
/// Retrieve a range of keys from the databases
#[instrument(level = "trace", target = "surrealdb::core::kvs::api", skip(self), fields(rng = rng.sprint()))]
async fn scan(
&mut self,
rng: Range<Key>,
limit: u32,
version: Option<u64>,
) -> Result<Vec<(Key, Val)>> {
let rng = self.prepare_scan(rng, version).await?;
// Execute on the blocking threadpool
let res = affinitypool::spawn_local(move || {
// Create result set
let mut res = vec![];
// Set the key range
let beg = rng.start.as_slice();
let end = rng.end.as_slice();
// Set the ReadOptions with the snapshot
let mut ro = ReadOptions::default();
ro.set_snapshot(&self.inner.as_ref().unwrap().snapshot());
ro.set_iterate_lower_bound(beg);
ro.set_iterate_upper_bound(end);
ro.set_async_io(true);
ro.fill_cache(true);
// Create the iterator
let mut iter = self.inner.as_ref().unwrap().raw_iterator_opt(ro);
// Seek to the start key
iter.seek(&rng.start);
// Check the scan limit
while res.len() < limit as usize {
// Check the key and value
if let Some((k, v)) = iter.item() {
// Check the range validity
if k >= beg && k < end {
res.push((k.to_vec(), v.to_vec()));
iter.next();
continue;
}
}
// Exit
break;
}
// Drop the iterator
drop(iter);
// Return result
res
})
.await;
// Return result
Ok(res)
}
#[instrument(level = "trace", target = "surrealdb::core::kvs::api", skip(self), fields(rng = rng.sprint()))]
async fn scanr(
&mut self,
rng: Range<Key>,
limit: u32,
version: Option<u64>,
) -> Result<Vec<(Key, Val)>> {
let rng = self.prepare_scan(rng, version).await?;
// Get the transaction
let inner = self.inner.as_ref().unwrap();
// Create result set
let mut res = vec![];
// Set the key range
let beg = rng.start.as_slice();
let end = rng.end.as_slice();
// Set the ReadOptions with the snapshot
let mut ro = ReadOptions::default();
ro.set_snapshot(&inner.snapshot());
ro.set_iterate_lower_bound(beg);
ro.set_iterate_upper_bound(end);
ro.set_async_io(true);
ro.fill_cache(true);
// Create the iterator
let mut iter = inner.raw_iterator_opt(ro);
// Seek to the start key
iter.seek_for_prev(&rng.end);
// Check the scan limit
while res.len() < limit as usize {
// Check the key and value
if let Some((k, v)) = iter.item() {
// Check the range validity
if k >= beg && k < end {
res.push((k.to_vec(), v.to_vec()));
iter.prev();
continue;
}
}
// Exit
break;
}
// Return result
Ok(res)
}
fn get_save_points(&mut self) -> &mut SavePoints {
unimplemented!("Get save points not implemented for for the RocksDB backend");
}
fn new_save_point(&mut self) {
// Get the transaction
let inner = self.inner.as_ref().unwrap();
// Set the save point
inner.set_savepoint();
}
async fn rollback_to_save_point(&mut self) -> Result<()> {
// Get the transaction
let inner = self.inner.as_ref().unwrap();
// Rollback
inner.rollback_to_savepoint()?;
//
Ok(())
}
fn release_last_save_point(&mut self) -> Result<()> {
Ok(())
}
}
impl Transaction {
#[instrument(level = "trace", target = "surrealdb::core::kvs::api", skip(self), fields(rng = rng.sprint()))]
async fn prepare_scan(&mut self, rng: Range<Key>, version: Option<u64>) -> Result<Range<Key>> {
// RocksDB does not support versioned queries.
ensure!(version.is_none(), Error::UnsupportedVersionedQueries);
// Check to see if transaction is closed
ensure!(!self.done, Error::TxFinished);
Ok(rng)
}
}