kevy_store/lib.rs
1//! kevy-store — the keyspace.
2//!
3//! A single-threaded, multi-type keyspace with lazy expiration. Each Redis data
4//! type is backed by a modern `std` structure — behaviour-compatible, but **not**
5//! Redis's legacy encodings:
6//!
7//! | Type | Backing structure |
8//! |------|-------------------|
9//! | String | `Vec<u8>` |
10//! | Hash / Set | `HashMap` / `HashSet` (hashbrown Swiss table) |
11//! | List | `VecDeque` (ring buffer, O(1) ends) |
12//! | Sorted set | `HashMap` + `BTreeSet<(score, member)>` (a B-tree, not a skiplist) |
13//!
14//! Wrong-type access returns [`StoreError::WrongType`]. The API is `&mut self`
15//! and lock-free, so a thread-per-core runtime ([kevy-rt]) can own one shard per
16//! core with no locking. Part of the [kevy] key–value server.
17//!
18//! `maxmemory` enforcement + 8 eviction policies live in [`evict`]; toggle via
19//! [`Store::set_max_memory`]. With `maxmemory == 0` (the default) the hot-path
20//! cost collapses to a single predicted-not-taken branch, matching the
21//! "unlimited" mode in Redis byte-for-byte.
22//!
23//! [kevy]: https://crates.io/crates/kevy
24//! [kevy-rt]: https://crates.io/crates/kevy-rt
25//!
26//! # Example
27//!
28//! ```
29//! use kevy_store::Store;
30//!
31//! use std::borrow::Cow;
32//! let mut s = Store::new();
33//! s.set(b"greeting", b"hello".to_vec(), None, false, false);
34//! assert_eq!(s.get(b"greeting").unwrap(), Some(Cow::Borrowed(&b"hello"[..])));
35//!
36//! s.hset(b"user:1", &[(b"name".to_vec(), b"alice".to_vec())]).unwrap();
37//! assert_eq!(s.hget(b"user:1", b"name").unwrap(), Some(&b"alice"[..]));
38//!
39//! // A string command on a hash key is a type error, as in Redis.
40//! assert_eq!(s.get(b"user:1"), Err(kevy_store::StoreError::WrongType));
41//! ```
42#![forbid(unsafe_code)]
43
44mod accounting;
45mod clock;
46mod entry;
47pub mod evict;
48pub mod expire;
49pub use expire::ExpireStats;
50pub(crate) use entry::Entry;
51mod hash;
52mod keyspace;
53mod list;
54mod list_ops;
55mod set;
56mod small_set;
57pub use small_set::{SmallSetData, SmallSetIter};
58mod small_hash;
59pub use small_hash::{SmallHashData, SmallHashIter};
60mod small_list;
61pub use small_list::{SmallListData, SmallListIter};
62mod small_zset;
63pub use small_zset::{SmallZSetData, SmallZSetIter};
64mod snapshot;
65pub use snapshot::SnapshotView;
66mod stream;
67mod string;
68mod util;
69mod value;
70mod zset;
71pub use stream::{
72 AutoclaimResult, ConsumerGroup, ConsumerState, EntryBatch, GroupCreateMode,
73 LoadedGroup, LoadedPelEntry, LoadedStreamEntry, PelEntry, PendingExtended,
74 PendingExtendedRow, PendingSummary, ReadGroupId, StreamData, StreamId, StreamIdError,
75 XAddIdSpec, XClaimOpts, now_unix_ms, parse_explicit_id, parse_range_end,
76 parse_range_start, parse_xadd_id,
77};
78pub use string::GetReply;
79pub use util::glob_match;
80pub use value::*;
81
82pub(crate) use clock::{deadline_at, now_ns, pack_deadline, remaining_ms};
83use kevy_map::KevyMap;
84
85/// Feed kevy's monotonic clock on `wasm32-unknown-unknown`, which has no
86/// `Instant`. The embedding host advances time (ns since an arbitrary fixed
87/// epoch, e.g. `Date.now() * 1e6`) before TTL-sensitive ops and once per
88/// reaper tick. No-op concept on native targets, where the OS clock is the
89/// source — hence wasm-only.
90#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
91pub use clock::set_clock_ns;
92/// Feed kevy's wall clock (Unix-epoch millis, e.g. `Date.now()`) on
93/// `wasm32-unknown-unknown`, where `SystemTime::now()` traps. Used by `XADD`
94/// auto-IDs and `EXPIREAT`/`PEXPIREAT`.
95#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
96pub use clock::set_wall_clock_ms;
97
98
99/// Outcome of [`Store::rename`] — three-way result so the dispatch
100/// layer can pick the right RESP frame (`+OK` / `-ERR no such key` /
101/// `:0` for `RENAMENX`-with-existing-dst).
102#[derive(Debug, PartialEq, Eq)]
103pub enum RenameOutcome {
104 /// Source removed, destination created (overwriting any prior dst).
105 Renamed,
106 /// Source key doesn't exist.
107 NoSuchSrc,
108 /// `RENAMENX` only — destination already exists, no rename done.
109 DstExists,
110}
111
112/// Operation errors surfaced to the command layer.
113#[derive(Debug, PartialEq, Eq)]
114pub enum StoreError {
115 /// Key holds a different type than the command expects.
116 WrongType,
117 /// Value is not a base-10 integer (INCR family).
118 NotInteger,
119 /// Result would overflow `i64`.
120 Overflow,
121 /// Index outside the collection (LSET).
122 OutOfRange,
123 /// Key does not exist where the command requires one (LSET).
124 NoSuchKey,
125 /// Value is not a valid float (INCRBYFLOAT).
126 NotFloat,
127 /// `maxmemory` would be exceeded and the active eviction policy is
128 /// [`EvictionPolicy::NoEviction`]. Surfaces as Redis's classic OOM error
129 /// at the RESP layer.
130 OutOfMemory,
131}
132
133/// Maxmemory eviction policy. Mirror of `kevy_config::EvictionPolicy` —
134/// duplicated here so `kevy-store` stays a leaf crate (no `kevy-config` dep).
135#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
136pub enum EvictionPolicy {
137 /// Refuse writes once `maxmemory` is hit. Default.
138 #[default]
139 NoEviction,
140 /// Approximated LRU across all keys.
141 AllKeysLru,
142 /// Approximated LFU across all keys.
143 AllKeysLfu,
144 /// Random key across all keys.
145 AllKeysRandom,
146 /// Approximated LRU across keys with a TTL.
147 VolatileLru,
148 /// Approximated LFU across keys with a TTL.
149 VolatileLfu,
150 /// Random key from those with a TTL.
151 VolatileRandom,
152 /// Key with the shortest remaining TTL.
153 VolatileTtl,
154}
155
156impl EvictionPolicy {
157 /// Whether the policy ranks candidates by LRU clock (read-touches matter).
158 #[inline]
159 pub fn uses_lru(self) -> bool {
160 matches!(self, Self::AllKeysLru | Self::VolatileLru)
161 }
162
163 /// Whether the policy ranks candidates by LFU counter (read-touches and
164 /// log-counter increments matter).
165 #[inline]
166 pub fn uses_lfu(self) -> bool {
167 matches!(self, Self::AllKeysLfu | Self::VolatileLfu)
168 }
169
170 /// Whether the policy restricts eviction to keys that carry a TTL.
171 #[inline]
172 pub fn is_volatile(self) -> bool {
173 matches!(
174 self,
175 Self::VolatileLru | Self::VolatileLfu | Self::VolatileRandom | Self::VolatileTtl
176 )
177 }
178}
179
180/// A single-database keyspace.
181///
182/// The keyspace map is a [`KevyMap`] — a pure-Rust open-addressing Swiss
183/// table tuned for kevy's per-shard, single-trust-domain keyspace. The
184/// hasher is [`kevy_hash::KevyHash`] (one-call inlinable; no DoS hardening
185/// since the shard is single-threaded with no cross-trust keys). Owning the
186/// table also exposes bucket addresses for software prefetch on the batch
187/// driver.
188#[derive(Default)]
189pub struct Store {
190 pub(crate) map: KevyMap<SmallBytes, Entry>,
191 /// Coarse cached monotonic clock (ns since [`epoch`]), refreshed by the
192 /// reactor loop / reaper tick via [`Self::refresh_clock`]. Lazy expiry on
193 /// the read path (`live_entry`) compares deadlines against this instead of
194 /// calling `Instant::now()` per access — the Redis cached-`mstime` model.
195 /// `0` (the `Default`) reads as "epoch" → keys look live until the first
196 /// refresh, the safe direction (expires at most one refresh-interval late,
197 /// never early — writes stamp deadlines from a *fresh* clock).
198 pub(crate) cached_ns: u64,
199 /// Whether lazy expiry trusts `Self::cached_ns` (set by a reactor/reaper
200 /// that calls [`Self::refresh_clock`]) instead of reading a fresh clock per
201 /// access. Enabled by the server reactor and the embedded background
202 /// reaper; left `false` (the `Default`) for manual-reaper / bare-`Store`
203 /// use, where nothing refreshes the cache so each access reads fresh —
204 /// preserving "lazy expiry works without an explicit tick".
205 pub(crate) cached_clock: bool,
206 /// Live byte estimate (dynamic per-entry weights + [`ENTRY_OVERHEAD`] per
207 /// key). Compared against [`Self::maxmemory`] to drive eviction.
208 pub(crate) used_memory: u64,
209 /// Soft byte ceiling. `0` = unlimited; the entire accounting + eviction
210 /// machinery short-circuits to a single not-taken branch in that case.
211 pub(crate) maxmemory: u64,
212 /// Active eviction policy. Only consulted when `used_memory > maxmemory`.
213 pub(crate) eviction_policy: EvictionPolicy,
214 /// Total keys evicted by [`Self::try_evict_after_write`] — surfaced via
215 /// `INFO memory` / `MEMORY STATS`.
216 pub(crate) evictions_total: u64,
217 /// Monotonic access counter; the upper 32 bits are unused, the lower 32
218 /// stamp `Entry::lru_clock` on each access while eviction is enabled.
219 pub(crate) clock_counter: u64,
220 /// `used_memory` peak across the shard's lifetime; surfaced as
221 /// `used_memory_peak` in `INFO memory`.
222 pub(crate) used_memory_peak: u64,
223 /// Keys expired since startup (lazy reap path AND
224 /// [`Self::tick_expire`]). Surfaced via `INFO keyspace` / `MEMORY STATS`
225 /// once those fields land.
226 pub(crate) expired_keys_total: u64,
227 /// Count of live keys carrying a TTL — the size of Redis's "expire set"
228 /// (`INFO keyspace`'s `expires=`). Maintained in O(1) at every TTL
229 /// transition (`insert_entry` / `remove_entry` deltas + the in-place
230 /// EXPIRE / PERSIST / SET sites) so the gauge never pays an O(n) keyspace
231 /// scan; [`Self::ttl_pending_count`] is the O(n) ground truth used to
232 /// assert this counter never drifts.
233 pub(crate) expires: u64,
234 /// `WATCH` version counters — present only for keys that have been
235 /// `WATCH`-ed at least once. [`Self::record_watch`] inserts the entry
236 /// (version 0 = "never written since first watch"); every subsequent
237 /// write on this shard calls [`Self::bump_if_watched`] which increments
238 /// only if the key is present in the map. Keys never `WATCH`-ed pay
239 /// one empty-map hashmap lookup per write (~10 ns).
240 ///
241 /// The map grows monotonically — entries are never evicted, even
242 /// when no conn is currently watching the key. For high-key-churn
243 /// workloads this can become a memory item; v1.x acceptable since
244 /// the entry is `Vec<u8>` + `u64` (~ 30 B + key length) and only
245 /// touched on writes / WATCH calls.
246 pub(crate) watch_versions: std::collections::HashMap<Vec<u8>, u64>,
247 /// Optional handle to the runtime's bio thread (v1.25 A.3). Set by
248 /// `kevy-rt::Runtime::run` via [`Self::set_bio_drop_sender`] before
249 /// the shard reactor loop starts. `None` = inline drop (bare-Store
250 /// embedders, snapshots-loader programs, the test harness — anything
251 /// without a kevy-rt runtime around it). Reads on the hot path are
252 /// one `Option::as_ref` branch; the steady-state inline-drop path
253 /// pays nothing beyond that branch.
254 pub(crate) bio_drop_sender: Option<value::BioDropSender>,
255 /// v1.25 A.2 batch-send buffer. Heavy `Value`s displaced by SET
256 /// overwrites accumulate here instead of paying one mpsc send per
257 /// drop; flushed in one `mpsc::Sender::send` at the end of every
258 /// reactor iteration (via [`Self::flush_pending_drops`], invoked
259 /// from `kevy-rt`'s epoll + io_uring reactor loops before the AOF
260 /// fsync window). Amortising the channel cost over N drops lets
261 /// the heap-heavy threshold sit at 1 KB — small enough that the
262 /// Axis I 256 B – 16 KB SET tail benefits, big enough that
263 /// sub-µs small-class drops still go inline (the push + flush
264 /// branch would cost more than the inline free).
265 ///
266 /// **Latency window**: drops sit in this buffer ≤ one reactor
267 /// iteration (10s of µs at busy-poll, ≤ park-timeout at idle —
268 /// 50 ms by default). On a reactor with no traffic the buffer
269 /// stays small (no new SETs to displace anything); on a reactor
270 /// with sustained writes the per-iter flush fires fast enough
271 /// that worst-case stall is bounded by `MAX_PENDING_DROPS`.
272 ///
273 /// **Bounded growth**: at `MAX_PENDING_DROPS` items the
274 /// `maybe_offload_drop` path force-flushes — protects against
275 /// pathological "thousand SETs in one iter never flush" cases
276 /// (would otherwise hold thousands of Box<Value>s in RAM until
277 /// the iter ends).
278 pub(crate) pending_drops: Vec<Box<Value>>,
279}
280
281/// Maximum [`Store::pending_drops`] depth before forcing a flush
282/// inside `maybe_offload_drop` (rather than waiting for the reactor's
283/// per-iter `flush_pending_drops`). Caps memory held in the batch
284/// buffer at ≤ 64 × sizeof(Box<Value>) (≤ 512 B of pointers + whatever
285/// the boxed payloads weigh — which we WANT to ship anyway, since
286/// holding the bio-bound batch defeats the point of off-reactor frees).
287/// 64 picked as: amortises mpsc send cost (~few hundred ns) across
288/// enough drops that per-drop overhead is ≤ 10 ns, while staying small
289/// enough that worst-case bunch-up latency at the bio thread is bounded.
290pub(crate) const MAX_PENDING_DROPS: usize = 64;
291
292impl Store {
293 pub fn new() -> Self {
294 Store::default()
295 }
296
297 /// Refresh the coarse cached clock (`Self::cached_ns`) from a single
298 /// `Instant::now()`. Call once per reactor-loop batch / reaper tick; the
299 /// per-access read path then skips its own clock read. Lazy expiry is
300 /// coarse to this cadence (a key expires ≤ one refresh-interval late,
301 /// never early — writes stamp deadlines from a fresh clock).
302 #[inline]
303 pub fn refresh_clock(&mut self) {
304 self.cached_ns = now_ns();
305 }
306
307 /// Enable/disable trusting the cached clock for lazy expiry (see
308 /// `Self::cached_ns`). Call with `true` only when something refreshes the
309 /// clock regularly (the server reactor per batch, the embedded background
310 /// reaper per tick); leave `false` for manual-reaper mode. Seeds the cache
311 /// when enabling so the first access is accurate.
312 #[inline]
313 pub fn set_cached_clock(&mut self, on: bool) {
314 self.cached_clock = on;
315 if on {
316 self.refresh_clock();
317 }
318 }
319
320 /// Install (or clear, with `maxmemory == 0`) the eviction limit and
321 /// policy. Cheap; safe to call repeatedly (e.g. on `CONFIG SET`).
322 #[inline]
323 pub fn set_max_memory(&mut self, maxmemory: u64, policy: EvictionPolicy) {
324 self.maxmemory = maxmemory;
325 self.eviction_policy = policy;
326 }
327
328 /// Install the runtime's bio-drop channel (v1.25 A.3 + A.2). Called
329 /// once from `kevy-rt::Runtime::run` per shard before the reactor
330 /// loop starts. After install, [`Self::maybe_offload_drop`] (invoked
331 /// from the SET overwrite fast path) accumulates oversize `Value`s
332 /// into a per-shard batch; the reactor calls
333 /// [`Self::flush_pending_drops`] at the end of every iter to ship
334 /// the batch in one mpsc send. Bounded the Axis I 10 KB SET p999/max
335 /// blow-up that synchronous `Box::<[u8]>::drop` of a jemalloc
336 /// large-class slot caused (see `kevy_rt::bio`).
337 #[inline]
338 pub fn set_bio_drop_sender(&mut self, sender: value::BioDropSender) {
339 self.bio_drop_sender = Some(sender);
340 }
341
342 /// Accumulate `old` into the per-shard bio-drop batch buffer
343 /// ([`Store::pending_drops`]) if it's heap-heavy AND a bio channel
344 /// is installed. Otherwise drop inline. The hot path is one branch
345 /// on `bio_drop_sender.is_none()` followed by the variant-cheap
346 /// [`Value::is_heap_heavy`] check; for the `Value::Str(SmallBytes)`
347 /// steady state of typical bench shapes the inline-drop path is
348 /// preserved unchanged.
349 ///
350 /// **v1.25 A.2 batch model**: per-send mpsc cost (atomic +
351 /// cross-thread cacheline) is amortised across the batch by
352 /// [`Self::flush_pending_drops`], which the reactor calls once per
353 /// iter. Force-flushes here when the buffer hits
354 /// [`MAX_PENDING_DROPS`] to bound RAM in-flight.
355 #[inline]
356 pub(crate) fn maybe_offload_drop(&mut self, old: Value) {
357 if self.bio_drop_sender.is_none() {
358 // No channel (bare Store / embedded reaper / tests): the
359 // Value falls out of scope and drops inline. Same
360 // behaviour as v1.24.
361 drop(old);
362 return;
363 }
364 if !old.is_heap_heavy() {
365 // Under-threshold: jemalloc small-class free is sub-µs.
366 // The Vec::push + force-flush branch costs more than the
367 // inline free for this size — leave it inline.
368 drop(old);
369 return;
370 }
371 self.pending_drops.push(Box::new(old));
372 if self.pending_drops.len() >= MAX_PENDING_DROPS {
373 self.flush_pending_drops();
374 }
375 }
376
377 /// Ship the per-shard bio-drop batch buffer to the bio thread in
378 /// one mpsc send. Called from `kevy-rt`'s reactor loop at the end
379 /// of every iteration (both the epoll `Shard::run` and the io_uring
380 /// `Shard::run_uring` paths, just before the AOF fsync window so a
381 /// pending fsync stall doesn't pin a batch-ful of heavy values in
382 /// per-shard memory).
383 ///
384 /// Empty-buffer fast path: zero work, predictable not-taken
385 /// branch. Reactor calls this unconditionally per iter; the steady-
386 /// state cost for a no-SET-overwrite iter is one length check.
387 ///
388 /// `SendError` here means the bio thread has exited (shutdown
389 /// territory — `Runtime::run` has dropped its sender AFTER the
390 /// shard threads joined). Drop the batch inline; the `SendError`
391 /// payload carries the `Vec` back so its `Box<Value>`s run their
392 /// Drop here, preserving correctness.
393 #[inline]
394 pub fn flush_pending_drops(&mut self) {
395 if self.pending_drops.is_empty() {
396 return;
397 }
398 let tx = match self.bio_drop_sender.as_ref() {
399 Some(tx) => tx,
400 // Shouldn't happen — caller (`maybe_offload_drop`) only
401 // pushes when the sender exists. Defensive: if a future
402 // refactor invokes `flush_pending_drops` from somewhere
403 // unconditional, drop the batch inline.
404 None => {
405 self.pending_drops.clear();
406 return;
407 }
408 };
409 let batch = std::mem::take(&mut self.pending_drops);
410 if let Err(_send_err) = tx.send(batch) {
411 // Bio thread is gone (shutdown). The SendError carries
412 // the Vec, which drops here — every Box<Value> runs its
413 // Drop inline. Benign one-time stall during tear-down.
414 }
415 }
416
417 /// Live byte estimate (see field doc).
418 #[inline]
419 pub fn used_memory(&self) -> u64 {
420 self.used_memory
421 }
422
423 /// `used_memory` high-water mark since startup.
424 #[inline]
425 pub fn used_memory_peak(&self) -> u64 {
426 self.used_memory_peak
427 }
428
429 /// Configured `maxmemory` (0 = unlimited).
430 #[inline]
431 pub fn maxmemory(&self) -> u64 {
432 self.maxmemory
433 }
434
435 /// Configured eviction policy.
436 #[inline]
437 pub fn eviction_policy(&self) -> EvictionPolicy {
438 self.eviction_policy
439 }
440
441 /// Total keys evicted since startup.
442 #[inline]
443 pub fn evictions_total(&self) -> u64 {
444 self.evictions_total
445 }
446
447 /// Live keys carrying a TTL (`INFO keyspace`'s `expires=`). O(1) — reads
448 /// the maintained counter, not an O(n) scan (cf. [`Self::ttl_pending_count`]).
449 #[inline]
450 pub fn expires_count(&self) -> usize {
451 self.expires as usize
452 }
453
454 /// Apply a signed delta to the [`Self::expires`] counter, clamped at 0.
455 /// Centralises the saturating arithmetic for every TTL-transition site.
456 #[inline]
457 pub(crate) fn adjust_expires(&mut self, delta: i64) {
458 if delta != 0 {
459 self.expires = (self.expires as i64 + delta).max(0) as u64;
460 }
461 }
462
463 /// `WATCH` — record this key in the version tracker and return its
464 /// current version. Subsequent writes on this shard bump the version
465 /// via [`Self::bump_if_watched`]. Caller (the conn's origin shard)
466 /// stores the returned version; `EXEC` later asks every owning shard
467 /// "is the version still N?" via [`Self::key_version`].
468 ///
469 /// Keys that have never been written stay at version 0 — the first
470 /// write after a `WATCH` bumps to 1, which is what makes the "dirty"
471 /// comparison work (stored 0 ≠ current 1 ⇒ abort EXEC).
472 pub fn record_watch(&mut self, key: &[u8]) -> u64 {
473 *self
474 .watch_versions
475 .entry(key.to_vec())
476 .or_insert(0)
477 }
478
479 /// Read-only version lookup used by `EXEC`'s pre-execution check.
480 /// Returns `0` for keys never `WATCH`-ed (matches the initial value
481 /// `record_watch` would have inserted, so a `WATCH` → no-write →
482 /// `EXEC` sequence sees the stored 0 == current 0 and proceeds).
483 #[inline]
484 pub fn key_version(&self, key: &[u8]) -> u64 {
485 self.watch_versions.get(key).copied().unwrap_or(0)
486 }
487
488 /// Bump the version of `key` if (and only if) it has been `WATCH`-ed at
489 /// least once. Write-side call after every mutation. The empty check
490 /// runs BEFORE the key is hashed — the common nothing-watched case
491 /// pays one branch, not a guaranteed-miss probe.
492 #[inline]
493 pub fn bump_if_watched(&mut self, key: &[u8]) {
494 if self.watch_versions.is_empty() {
495 return;
496 }
497 if let Some(v) = self.watch_versions.get_mut(key) {
498 *v = v.wrapping_add(1);
499 }
500 }
501
502 /// Invalidate every watched key in one shot. Called from `FLUSHDB`
503 /// / `FLUSHALL` execution paths — every WATCH against this shard
504 /// must invalidate so a pending `EXEC` aborts.
505 pub fn bump_all_watched(&mut self) {
506 for v in self.watch_versions.values_mut() {
507 *v = v.wrapping_add(1);
508 }
509 }
510
511 /// Cached weight of `key` (dynamic part + [`ENTRY_OVERHEAD`]). Returns
512 /// `None` when the key is absent or expired (no implicit reap).
513 pub fn estimate_key_bytes(&self, key: &[u8]) -> Option<u64> {
514 self.map.get(key).map(|e| e.weight() + ENTRY_OVERHEAD)
515 }
516
517 /// O(1) precondition check the dispatch layer calls before every write
518 /// command. Returns `Err(OutOfMemory)` only when `maxmemory > 0`, the
519 /// budget is already over, AND the policy is `NoEviction` (Redis
520 /// behaviour). All other policies let the write proceed and recover via
521 /// [`Self::try_evict_after_write`].
522 #[inline]
523 pub fn precheck_for_write(&self) -> Result<(), StoreError> {
524 if self.maxmemory == 0 || self.used_memory <= self.maxmemory {
525 return Ok(());
526 }
527 if self.eviction_policy == EvictionPolicy::NoEviction {
528 return Err(StoreError::OutOfMemory);
529 }
530 Ok(())
531 }
532
533 /// Run after every write command. No-op when disabled or under budget;
534 /// otherwise samples per [`Self::eviction_policy`] and removes keys until
535 /// back under `maxmemory` or no eligible candidate remains. Returns the
536 /// number of keys evicted (0 on the common fast path).
537 #[inline]
538 pub fn try_evict_after_write(&mut self) -> usize {
539 if self.maxmemory == 0 || self.used_memory <= self.maxmemory {
540 return 0;
541 }
542 evict::evict_until_under_limit(self)
543 }
544
545}
546
547/// Apply a signed delta to a `u64` (saturating both directions). Used by
548/// `Store::account_delta` / `reweigh_entry` so the in-place mutators don't
549/// have to repeat the same overflow-guarded match.
550#[inline]
551pub(crate) fn apply_delta(v: &mut u64, delta: i64) {
552 if delta >= 0 {
553 *v = v.saturating_add(delta as u64);
554 } else {
555 *v = v.saturating_sub((-delta) as u64);
556 }
557}
558
559/// Heap bytes a `SmallBytes`-encoded key would own (`&[u8]` mirror of
560/// `SmallBytes::heap_bytes`; 22-byte inline boundary per `kevy-bytes`).
561#[inline]
562pub(crate) fn key_heap_bytes_for(key: &[u8]) -> u64 {
563 if key.len() <= 22 { 0 } else { key.len() as u64 }
564}
565
566#[cfg(test)]
567mod tests;
568#[cfg(test)]
569mod tests_memory;
570#[cfg(test)]
571mod tests_snapshot;