use std::time::Instant;
use crate::value::Value;
use crate::{SmallBytes, Store, unpack_deadline};
pub struct SnapshotView {
entries: Vec<(SmallBytes, Value, Option<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 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 = Instant::now();
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| unpack_deadline(ns).saturating_duration_since(now).as_millis() as u64);
entries.push((k.clone(), e.value.clone(), ttl));
}
SnapshotView { entries }
}
}