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
use crate::{
config::{ReadOptions, ScanOptions},
DbIterator,
};
use bytes::Bytes;
use std::ops::RangeBounds;
/// Trait for read-only database operations.
///
/// This trait defines the interface for reading data from SlateDB,
/// and can be implemented by `Db`, `DbReader` and `DbSnapshot`
/// to provide a unified interface for read-only operations.
///
/// The trait is designed to be object-safe, allowing for dynamic dispatch
/// when needed.
#[async_trait::async_trait]
pub trait DbRead {
/// Get a value from the database with default read options.
///
/// The `Bytes` object returned contains a slice of an entire
/// 4 KiB block. The block will be held in memory as long as the
/// caller holds a reference to the `Bytes` object. Consider
/// copying the data if you need to hold it for a long time.
///
/// ## Arguments
/// - `key`: the key to get
///
/// ## Returns
/// - `Result<Option<Bytes>, Error>`:
/// - `Some(Bytes)`: the value if it exists
/// - `None`: if the value does not exist
///
/// ## Errors
/// - `Error`: if there was an error getting the value
async fn get<K: AsRef<[u8]> + Send>(&self, key: K) -> Result<Option<Bytes>, crate::Error> {
self.get_with_options(key, &ReadOptions::default()).await
}
/// Get a value from the database with custom read options.
///
/// The `Bytes` object returned contains a slice of an entire
/// 4 KiB block. The block will be held in memory as long as the
/// caller holds a reference to the `Bytes` object. Consider
/// copying the data if you need to hold it for a long time.
///
/// ## Arguments
/// - `key`: the key to get
/// - `options`: the read options to use
///
/// ## Returns
/// - `Result<Option<Bytes>, Error>`:
/// - `Some(Bytes)`: the value if it exists
/// - `None`: if the value does not exist
///
/// ## Errors
/// - `Error`: if there was an error getting the value
async fn get_with_options<K: AsRef<[u8]> + Send>(
&self,
key: K,
options: &ReadOptions,
) -> Result<Option<Bytes>, crate::Error>;
/// Scan a range of keys using the default scan options.
///
/// returns a `DbIterator`
///
/// ## Arguments
/// - `range`: the range of keys to scan
///
/// ## Errors
/// - `Error`: if there was an error scanning the range of keys
///
/// ## Returns
/// - `Result<DbIterator, Error>`: An iterator with the results of the scan
async fn scan<K, T>(&self, range: T) -> Result<DbIterator, crate::Error>
where
K: AsRef<[u8]> + Send,
T: RangeBounds<K> + Send,
{
self.scan_with_options(range, &ScanOptions::default()).await
}
/// Scan a range of keys with the provided options.
///
/// returns a `DbIterator`
///
/// ## Arguments
/// - `range`: the range of keys to scan
/// - `options`: the scan options to use
///
/// ## Errors
/// - `Error`: if there was an error scanning the range of keys
///
/// ## Returns
/// - `Result<DbIterator, Error>`: An iterator with the results of the scan
async fn scan_with_options<K, T>(
&self,
range: T,
options: &ScanOptions,
) -> Result<DbIterator, crate::Error>
where
K: AsRef<[u8]> + Send,
T: RangeBounds<K> + Send;
}