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
use crate::{
bytes_range::BytesRange,
config::{ReadOptions, ScanOptions},
types::KeyValue,
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>;
/// Get a key-value pair from the database with default read options.
///
/// Returns the key along with its value and metadata (sequence number,
/// creation timestamp, expiration timestamp). Unlike [`get`](Self::get),
/// which returns only the value bytes, this method returns a [`KeyValue`]
/// that includes row metadata.
///
/// ## Arguments
/// - `key`: the key to look up
///
/// ## Returns
/// - `Ok(Some(KeyValue))`: if the key exists and is not deleted/expired
/// - `Ok(None)`: if the key does not exist or is deleted/expired
///
/// ## Errors
/// - `Error`: if there was an error reading from the database
async fn get_key_value<K: AsRef<[u8]> + Send>(
&self,
key: K,
) -> Result<Option<KeyValue>, crate::Error> {
self.get_key_value_with_options(key, &ReadOptions::default())
.await
}
/// Get a key-value pair from the database with custom read options.
///
/// Returns the key along with its value and metadata (sequence number,
/// creation timestamp, expiration timestamp). Unlike
/// [`get_with_options`](Self::get_with_options), which returns only the
/// value bytes, this method returns a [`KeyValue`] that includes row
/// metadata.
///
/// ## Arguments
/// - `key`: the key to look up
/// - `options`: the read options to use
///
/// ## Returns
/// - `Ok(Some(KeyValue))`: if the key exists and is not deleted/expired
/// - `Ok(None)`: if the key does not exist or is deleted/expired
///
/// ## Errors
/// - `Error`: if there was an error reading from the database
async fn get_key_value_with_options<K: AsRef<[u8]> + Send>(
&self,
key: K,
options: &ReadOptions,
) -> Result<Option<KeyValue>, 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;
/// Scan all keys that share the provided prefix using the default scan options.
///
/// ## Arguments
/// - `prefix`: the key prefix to scan
///
/// ## Returns
/// - `Result<DbIterator, Error>`: An iterator with the results of the scan
async fn scan_prefix<P>(&self, prefix: P) -> Result<DbIterator, crate::Error>
where
P: AsRef<[u8]> + Send,
{
self.scan_prefix_with_options(prefix, &ScanOptions::default())
.await
}
/// Scan all keys that share the provided prefix with custom options.
///
/// ## Arguments
/// - `prefix`: the key prefix to scan
/// - `options`: the scan options to use
///
/// ## Returns
/// - `Result<DbIterator, Error>`: An iterator with the results of the scan
async fn scan_prefix_with_options<P>(
&self,
prefix: P,
options: &ScanOptions,
) -> Result<DbIterator, crate::Error>
where
P: AsRef<[u8]> + Send,
{
let range = BytesRange::from_prefix(prefix.as_ref());
self.scan_with_options(range, options).await
}
}