Skip to content

Commit 3953744

Browse files
committed
Implement MigratableKVStore for InMemoryStore
This was unimplemented for the in-memory kv store. Useful in tests so we can migrate data across all supported store implementations. AI-assisted-by: OpenAI Codex
1 parent 53b18eb commit 3953744

1 file changed

Lines changed: 59 additions & 1 deletion

File tree

‎src/io/in_memory_store.rs‎

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ use std::sync::atomic::{AtomicU64, Ordering};
1111
use std::sync::Mutex;
1212

1313
use lightning::io;
14-
use lightning::util::persist::{KVStore, PageToken, PaginatedKVStore, PaginatedListResponse};
14+
use lightning::util::persist::{
15+
KVStore, MigratableKVStore, PageToken, PaginatedKVStore, PaginatedListResponse,
16+
};
1517

1618
const IN_MEMORY_PAGE_SIZE: usize = 50;
1719

@@ -96,6 +98,27 @@ impl InMemoryStore {
9698
hash_map::Entry::Vacant(_) => Ok(Vec::new()),
9799
}
98100
}
101+
102+
fn list_all_keys_internal(&self) -> io::Result<Vec<(String, String, String)>> {
103+
let persisted_lock = self.persisted_bytes.lock().unwrap();
104+
let mut keys = Vec::new();
105+
106+
for (prefixed_namespace, namespace_entries) in persisted_lock.iter() {
107+
let (primary_namespace, secondary_namespace) =
108+
prefixed_namespace.split_once('/').ok_or_else(|| {
109+
io::Error::new(io::ErrorKind::InvalidData, "Invalid namespace format")
110+
})?;
111+
for key in namespace_entries.keys() {
112+
keys.push((
113+
primary_namespace.to_string(),
114+
secondary_namespace.to_string(),
115+
key.clone(),
116+
));
117+
}
118+
}
119+
120+
Ok(keys)
121+
}
99122
}
100123

101124
impl KVStore for InMemoryStore {
@@ -187,5 +210,40 @@ impl PaginatedKVStore for InMemoryStore {
187210
}
188211
}
189212

213+
impl MigratableKVStore for InMemoryStore {
214+
fn list_all_keys(
215+
&self,
216+
) -> impl Future<Output = Result<Vec<(String, String, String)>, io::Error>> + 'static + Send {
217+
let res = self.list_all_keys_internal();
218+
async move { res }
219+
}
220+
}
221+
190222
unsafe impl Sync for InMemoryStore {}
191223
unsafe impl Send for InMemoryStore {}
224+
225+
#[cfg(test)]
226+
mod tests {
227+
use super::*;
228+
229+
#[tokio::test]
230+
async fn in_memory_store_list_all_keys() {
231+
let store = InMemoryStore::new();
232+
233+
KVStore::write(&store, "ns_a", "sub_a", "key_a", vec![1u8]).await.unwrap();
234+
KVStore::write(&store, "ns_a", "sub_b", "key_b", vec![2u8]).await.unwrap();
235+
KVStore::write(&store, "ns_b", "", "key_c", vec![3u8]).await.unwrap();
236+
237+
let mut keys = MigratableKVStore::list_all_keys(&store).await.unwrap();
238+
keys.sort();
239+
240+
assert_eq!(
241+
keys,
242+
vec![
243+
("ns_a".to_string(), "sub_a".to_string(), "key_a".to_string()),
244+
("ns_a".to_string(), "sub_b".to_string(), "key_b".to_string()),
245+
("ns_b".to_string(), "".to_string(), "key_c".to_string()),
246+
]
247+
);
248+
}
249+
}

0 commit comments

Comments
 (0)