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
use crate::storage_engine::EntryMetadata;
use std::io::Result;
pub trait DataStoreReader {
type EntryHandleType;
/// Checks whether a key currently exists in the store.
///
/// This is a **constant‑time** lookup against the in‑memory
/// [`crate::storage_engine::KeyIndexer`] map.
/// A key is considered to *exist* only if it is present **and not marked
/// as deleted**.
///
/// # Parameters
/// - `key`: The **binary key** to check.
///
/// # Returns
/// - `Ok(true)`: Key exists and is active.
/// - `Ok(false)`: Key is absent or has been deleted.
/// - `Err(std::io::Error)`: On I/O failure.
fn exists(&self, key: &[u8]) -> Result<bool>;
/// Retrieves the most recent value associated with a given key.
///
/// This method **efficiently looks up a key** using a fast in-memory index,
/// and returns the latest corresponding value if found.
///
/// # Parameters:
/// - `key`: The **binary key** whose latest value is to be retrieved.
///
/// # Returns:
/// - `Ok(Some(EntryHandle))`: Handle to the entry if found.
/// - `Ok(None)`: If the key does not exist or is deleted.
/// - `Err(std::io::Error)`: On I/O failure.
///
/// # Notes:
/// - The returned `EntryHandle` provides zero-copy access to the stored data.
fn read(&self, key: &[u8]) -> Result<Option<Self::EntryHandleType>>;
/// Retrieves the last entry written to the file.
///
/// # Returns:
/// - `Ok(Some(EntryHandle))`: Handle to the last entry, if any.
/// - `Ok(None)`: If the file is empty.
/// - `Err(std::io::Error)`: On I/O failure.
fn read_last_entry(&self) -> Result<Option<Self::EntryHandleType>>;
/// Reads many keys in one shot.
///
/// This is the **vectorized** counterpart to [`crate::DataStore::read`].
/// It takes a slice of raw-byte keys and returns a `Vec` whose *i-th* element
/// is the result of looking up the *i-th* key.
///
/// * **Zero-copy** – each `Some(EntryHandle)` points directly into the
/// shared `Arc<Mmap>`; no payload is copied.
/// * **Constant-time per key** – the in-memory [`crate::storage_engine::KeyIndexer`] map is used
/// for each lookup, so the complexity is *O(n)* where *n* is
/// `keys.len()`.
/// * **Thread-safe** – a read lock on the index is taken once for the whole
/// batch, so concurrent writers are still blocked only for the same short
/// critical section that a single `read` would need.
///
/// # Returns:
/// - `Ok(results)`: `Vec<Option<EntryHandle>>` in key order.
/// - `Err(std::io::Error)`: On I/O failure.
fn batch_read(&self, keys: &[&[u8]]) -> Result<Vec<Option<Self::EntryHandleType>>>;
/// Retrieves metadata for a given key.
///
/// This method looks up a key in the storage and returns its associated metadata.
///
/// # Parameters:
/// - `key`: The **binary key** whose metadata is to be retrieved.
///
/// # Returns:
/// - `Ok(Some(metadata))`: Metadata if the key exists.
/// - `Ok(None)`: If the key is absent.
/// - `Err(std::io::Error)`: On I/O failure.
fn read_metadata(&self, key: &[u8]) -> Result<Option<EntryMetadata>>;
/// Counts **active** (non-deleted) key-value pairs in the storage.
///
/// # Returns:
/// - `Ok(active_count)`: Total active entries.
/// - `Err(std::io::Error)`: On I/O failure.
fn len(&self) -> Result<usize>;
/// Determines if the store is empty or has no active keys.
///
/// # Returns:
/// - `Ok(bool)`: Whether or not the store has any active keys.
/// - `Err(std::io::Error)`: On I/O failure.
fn is_empty(&self) -> Result<bool>;
/// Returns the current file size on disk (including those of deleted entries).
///
/// # Returns:
/// - `Ok(bytes)`: File size in bytes.
/// - `Err(std::io::Error)`: On I/O failure.
fn file_size(&self) -> Result<u64>;
}
#[async_trait::async_trait]
pub trait AsyncDataStoreReader {
type EntryHandleType;
/// Checks whether a key currently exists in the store.
///
/// This is a **constant‑time** lookup against the in‑memory
/// [`crate::storage_engine::KeyIndexer`] map.
/// A key is considered to *exist* only if it is present **and not marked
/// as deleted**.
///
/// # Parameters
/// - `key`: The **binary key** to check.
///
/// # Returns
/// - `Ok(true)`: Key exists and is active.
/// - `Ok(false)`: Key is absent or has been deleted.
/// - `Err(std::io::Error)`: On I/O failure.
async fn exists(&self, key: &[u8]) -> Result<bool>;
/// Retrieves the most recent value associated with a given key.
///
/// This method **efficiently looks up a key** using a fast in-memory index,
/// and returns the latest corresponding value if found.
///
/// # Parameters:
/// - `key`: The **binary key** whose latest value is to be retrieved.
///
/// # Returns:
/// - `Ok(Some(EntryHandle))`: Handle to the entry if found.
/// - `Ok(None)`: If the key does not exist or is deleted.
/// - `Err(std::io::Error)`: On I/O failure.
///
/// # Notes:
/// - The returned `EntryHandle` provides zero-copy access to the stored data.
async fn read(&self, key: &[u8]) -> Result<Option<Self::EntryHandleType>>;
/// Retrieves the last entry written to the file.
///
/// # Returns:
/// - `Ok(Some(EntryHandle))`: Handle to the last entry, if any.
/// - `Ok(None)`: If the file is empty.
/// - `Err(std::io::Error)`: On I/O failure.
async fn read_last_entry(&self) -> Result<Option<Self::EntryHandleType>>;
/// Reads many keys in one shot.
///
/// This is the **vectorized** counterpart to [`crate::DataStore::read`].
/// It takes a slice of raw-byte keys and returns a `Vec` whose *i-th* element
/// is the result of looking up the *i-th* key.
///
/// * **Zero-copy** – each `Some(EntryHandle)` points directly into the
/// shared `Arc<Mmap>`; no payload is copied.
/// * **Constant-time per key** – the in-memory [`crate::storage_engine::KeyIndexer`] map is used
/// for each lookup, so the complexity is *O(n)* where *n* is
/// `keys.len()`.
/// * **Thread-safe** – a read lock on the index is taken once for the whole
/// batch, so concurrent writers are still blocked only for the same short
/// critical section that a single `read` would need.
///
/// # Returns:
/// - `Ok(results)`: `Vec<Option<EntryHandle>>` in key order.
/// - `Err(std::io::Error)`: On I/O failure.
async fn batch_read(&self, keys: &[&[u8]]) -> Result<Vec<Option<Self::EntryHandleType>>>;
/// Retrieves metadata for a given key.
///
/// This method looks up a key in the storage and returns its associated metadata.
///
/// # Parameters:
/// - `key`: The **binary key** whose metadata is to be retrieved.
///
/// # Returns:
/// - `Ok(Some(metadata))`: Metadata if the key exists.
/// - `Ok(None)`: If the key is absent.
/// - `Err(std::io::Error)`: On I/O failure.
async fn read_metadata(&self, key: &[u8]) -> Result<Option<EntryMetadata>>;
/// Counts **active** (non-deleted) key-value pairs in the storage.
///
/// # Returns:
/// - `Ok(active_count)`: Total active entries.
/// - `Err(std::io::Error)`: On I/O failure.
async fn len(&self) -> Result<usize>;
/// Determines if the store is empty or has no active keys.
///
/// # Returns:
/// - `Ok(bool)`: Whether or not the store has any active keys.
/// - `Err(std::io::Error)`: On I/O failure.
async fn is_empty(&self) -> Result<bool>;
/// Returns the current file size on disk (including those of deleted entries).
///
/// # Returns:
/// - `Ok(bytes)`: File size in bytes.
/// - `Err(std::io::Error)`: On I/O failure.
async fn file_size(&self) -> Result<u64>;
}