Skip to main content

kevy_store/
snapshot.rs

1//! Point-in-time snapshot views — the freeze half of COW serialization.
2//!
3//! [`Store::collect_snapshot`] walks the keyspace once and shallow-clones
4//! every live entry: keys and string values copy their bytes (≤22 B inline
5//! = a 24 B memcpy), collection values bump an `Arc` refcount. The pause is
6//! O(n) at nanoseconds per entry — independent of collection sizes and of
7//! disk speed. The returned [`SnapshotView`] is `Send`: hand it to a
8//! background thread and serialize at leisure while the store keeps
9//! mutating (writes copy-on-write via `Arc::make_mut`, deletions just drop
10//! one strong ref — the view's data stays alive until it is dropped).
11//!
12//! TTLs are resolved to remaining-milliseconds at collect time, so the view
13//! is a consistent instant: an entry that expires *after* the collect still
14//! appears with the remaining TTL it had at that instant.
15
16use crate::value::Value;
17use crate::{SmallBytes, Store, now_ns, remaining_ms};
18
19/// A frozen, `Send` view of one store's live entries at a single instant.
20pub struct SnapshotView {
21    entries: Vec<(SmallBytes, Value, Option<u64>)>,
22}
23
24// Compile-time guarantee that a view can cross to a serializer thread.
25const _: () = {
26    const fn assert_send<T: Send>() {}
27    assert_send::<SnapshotView>();
28};
29
30impl SnapshotView {
31    /// Visit every entry as `(key, &value, ttl_ms)` — the same shape as
32    /// [`Store::snapshot_each`], so serializers take either source.
33    pub fn each<F: FnMut(&[u8], &Value, Option<u64>)>(&self, mut f: F) {
34        for (k, v, ttl) in &self.entries {
35            f(k.as_slice(), v, *ttl);
36        }
37    }
38
39    /// Number of entries frozen in the view.
40    pub fn len(&self) -> usize {
41        self.entries.len()
42    }
43
44    /// Whether the view holds zero entries.
45    pub fn is_empty(&self) -> bool {
46        self.entries.is_empty()
47    }
48}
49
50impl Store {
51    /// Freeze a point-in-time [`SnapshotView`] of every live entry.
52    ///
53    /// O(n) shallow: per entry one key clone + one [`Value`] clone (string
54    /// bytes copied, collections refcount-bumped) + the TTL resolved to
55    /// remaining millis. Expired-but-unreaped entries are skipped, matching
56    /// [`Store::snapshot_each`].
57    pub fn collect_snapshot(&self) -> SnapshotView {
58        let now = now_ns();
59        let mut entries = Vec::with_capacity(self.map.len());
60        for (k, e) in &self.map {
61            if e.is_expired_at(now) {
62                continue;
63            }
64            let ttl = e.expire_at_ns.map(|ns| remaining_ms(ns, now));
65            entries.push((k.clone(), e.value.clone(), ttl));
66        }
67        SnapshotView { entries }
68    }
69}