use crate::value::Value;
use crate::{SmallBytes, Store, now_ns, remaining_ms};
pub struct SnapshotView {
entries: Vec<(SmallBytes, Value, Option<u64>)>,
hfttl: Vec<(SmallBytes, SmallBytes, u64)>,
}
const _: () = {
const fn assert_send<T: Send>() {}
assert_send::<SnapshotView>();
};
impl SnapshotView {
pub fn each<F: FnMut(&[u8], &Value, Option<u64>)>(&self, mut f: F) {
for (k, v, ttl) in &self.entries {
f(k.as_slice(), v, *ttl);
}
}
pub fn each_hash_ttl<F: FnMut(&[u8], &[u8], u64)>(&self, mut f: F) {
for (k, field, d) in &self.hfttl {
f(k.as_slice(), field.as_slice(), *d);
}
}
pub fn len(&self) -> usize {
self.entries.len()
}
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
}
}
impl Store {
pub fn collect_snapshot(&self) -> SnapshotView {
let now = now_ns();
let mut entries = Vec::with_capacity(self.map.len());
for (k, e) in &self.map {
if e.is_expired_at(now) {
continue;
}
let ttl = e.expire_at_ns.map(|ns| remaining_ms(ns, now));
entries.push((k.clone(), e.value.clone(), ttl));
}
let mut hfttl = Vec::new();
self.hash_ttl_each(|k, f, d| {
hfttl.push((
crate::SmallBytes::from_slice(k),
crate::SmallBytes::from_slice(f),
d,
));
});
SnapshotView { entries, hfttl }
}
}