Skip to main content

kevy_store/
types.rs

1//! Public store types split from `lib.rs` (500-LOC rule):
2//! [`RenameOutcome`] / [`StoreError`] / [`EvictionPolicy`].
3
4/// Outcome of [`Store::rename`] — three-way result so the dispatch
5/// layer can pick the right RESP frame (`+OK` / `-ERR no such key` /
6/// `:0` for `RENAMENX`-with-existing-dst).
7#[derive(Debug, PartialEq, Eq)]
8pub enum RenameOutcome {
9    /// Source removed, destination created (overwriting any prior dst).
10    Renamed,
11    /// Source key doesn't exist.
12    NoSuchSrc,
13    /// `RENAMENX` only — destination already exists, no rename done.
14    DstExists,
15}
16
17/// Operation errors surfaced to the command layer.
18#[derive(Debug, PartialEq, Eq)]
19pub enum StoreError {
20    /// Key holds a different type than the command expects.
21    WrongType,
22    /// Value is not a base-10 integer (INCR family).
23    NotInteger,
24    /// Result would overflow `i64`.
25    Overflow,
26    /// Index outside the collection (LSET).
27    OutOfRange,
28    /// Key does not exist where the command requires one (LSET).
29    NoSuchKey,
30    /// Value is not a valid float (INCRBYFLOAT).
31    NotFloat,
32    /// `maxmemory` would be exceeded and the active eviction policy is
33    /// [`EvictionPolicy::NoEviction`]. Surfaces as Redis's classic OOM error
34    /// at the RESP layer.
35    OutOfMemory,
36}
37
38/// Maxmemory eviction policy. Mirror of `kevy_config::EvictionPolicy` —
39/// duplicated here so `kevy-store` stays a leaf crate (no `kevy-config` dep).
40#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
41pub enum EvictionPolicy {
42    /// Refuse writes once `maxmemory` is hit. Default.
43    #[default]
44    NoEviction,
45    /// Approximated LRU across all keys.
46    AllKeysLru,
47    /// Approximated LFU across all keys.
48    AllKeysLfu,
49    /// Random key across all keys.
50    AllKeysRandom,
51    /// Approximated LRU across keys with a TTL.
52    VolatileLru,
53    /// Approximated LFU across keys with a TTL.
54    VolatileLfu,
55    /// Random key from those with a TTL.
56    VolatileRandom,
57    /// Key with the shortest remaining TTL.
58    VolatileTtl,
59}
60
61impl EvictionPolicy {
62    /// Whether the policy ranks candidates by LRU clock (read-touches matter).
63    #[inline]
64    pub fn uses_lru(self) -> bool {
65        matches!(self, Self::AllKeysLru | Self::VolatileLru)
66    }
67
68    /// Whether the policy ranks candidates by LFU counter (read-touches and
69    /// log-counter increments matter).
70    #[inline]
71    pub fn uses_lfu(self) -> bool {
72        matches!(self, Self::AllKeysLfu | Self::VolatileLfu)
73    }
74
75    /// Whether the policy restricts eviction to keys that carry a TTL.
76    #[inline]
77    pub fn is_volatile(self) -> bool {
78        matches!(
79            self,
80            Self::VolatileLru | Self::VolatileLfu | Self::VolatileRandom | Self::VolatileTtl
81        )
82    }
83}