Skip to main content

kevy_embedded/
ops.rs

1//! Data-type methods on [`Store`] — string, hash, list, set, sorted set,
2//! plus the pub/sub `publish` / `subscribe` / `psubscribe` entry points.
3//!
4//! All of these are thin facades over `kevy_store::Store` (the keyspace)
5//! and `pubsub::PubsubBus` (the in-process bus); they hold the embedded
6//! mutex for the duration of the underlying call, then drop it. AOF
7//! logging + post-write eviction sweep run via `commit_write` from
8//! `store.rs`. Behaviour and ABI are unchanged from the v1.1.0 single-file
9//! layout — this module only exists to keep `store.rs` under the 500-LOC
10//! cap.
11
12use std::io;
13use std::time::Duration;
14
15use kevy_store::StoreError;
16
17use crate::pubsub::Subscription;
18#[cfg(not(target_arch = "wasm32"))]
19use crate::replica_glue::ensure_writable;
20use crate::store::{Store, commit_write, store_err};
21
22// wasm has no replica runner, so the read-only guard is unconditionally a
23// no-op. Mirrors `replica_glue::ensure_writable`'s signature so the rest of
24// this file is target-agnostic.
25#[cfg(target_arch = "wasm32")]
26fn ensure_writable(_s: &Store) -> io::Result<()> { Ok(()) }
27
28impl Store {
29    // ---- string ops -----------------------------------------------------
30
31    /// `SET key value` (no TTL, no NX/XX). Returns `true` always under the
32    /// embedded API (Redis semantics: SET overwrites; NX/XX vetoes would
33    /// return `false` but we don't expose those here — use [`Store::with`]
34    /// for the full surface).
35    pub fn set(&self, key: &[u8], value: &[u8]) -> io::Result<bool> {
36        ensure_writable(self)?;
37        let mut g = self.wshard(key);
38        let ok = g.store.set(key, value.to_vec(), None, false, false);
39        commit_write(&mut g, &[b"SET", key, value])?;
40        Ok(ok)
41    }
42
43    /// `SET key value PX ms` — overwrites + sets TTL. The AOF records an
44    /// **absolute** `PEXPIREAT` deadline (not the relative `ttl`) so the key
45    /// expires at the same wall-clock instant after a restart — a relative
46    /// `PEXPIRE` would be re-anchored to replay-time, resetting the TTL to a
47    /// fresh full duration on every restart (INC-2026-06-09).
48    pub fn set_with_ttl(&self, key: &[u8], value: &[u8], ttl: Duration) -> io::Result<bool> {
49        ensure_writable(self)?;
50        let mut g = self.wshard(key);
51        let ok = g.store.set(key, value.to_vec(), Some(ttl), false, false);
52        let ms = ttl.as_millis().min(u128::from(u64::MAX)) as u64;
53        let deadline = kevy_store::now_unix_ms().saturating_add(ms);
54        commit_write(&mut g, &[b"SET", key, value])?;
55        commit_write(&mut g, &[b"PEXPIREAT", key, deadline.to_string().as_bytes()])?;
56        Ok(ok)
57    }
58
59    /// `GET key` — `Some(bytes)` on hit, `None` on miss or expired.
60    ///
61    /// With eviction off (`maxmemory == 0`, the default) this takes the **read**
62    /// lock and a non-mutating store lookup, so concurrent readers scale across
63    /// cores — the path a read-heavy embed cache lives on. With eviction on it
64    /// falls back to the exclusive lock + mutating get so each access still
65    /// stamps the LRU clock.
66    pub fn get(&self, key: &[u8]) -> io::Result<Option<Vec<u8>>> {
67        if self.config().maxmemory == 0 {
68            let g = self.rshard(key);
69            return Ok(g.store.get_shared(key).map_err(store_err)?.map(|c| c.into_owned()));
70        }
71        let mut g = self.wshard(key);
72        Ok(g.store.get(key).map_err(store_err)?.map(|c| c.into_owned()))
73    }
74
75    /// `DEL key1 [key2 ...]`. Returns the count of keys actually removed.
76    /// Keys fan out to their owning shards.
77    pub fn del(&self, keys: &[&[u8]]) -> io::Result<usize> {
78        ensure_writable(self)?;
79        let mut total = 0;
80        for k in keys {
81            let owned = vec![k.to_vec()];
82            let mut g = self.wshard(k);
83            let n = g.store.del(&owned);
84            if n > 0 {
85                total += n;
86                commit_write(&mut g, &[b"DEL", k])?;
87            }
88        }
89        Ok(total)
90    }
91
92    /// `EXISTS key1 [key2 ...]`. Count of existing keys (duplicates counted
93    /// multiple times, matching Redis).
94    pub fn exists(&self, keys: &[&[u8]]) -> io::Result<usize> {
95        let mut total = 0;
96        for k in keys {
97            total += self.wshard(k).store.exists(&[k.to_vec()]);
98        }
99        Ok(total)
100    }
101
102    /// `INCR key`. Returns the post-increment value.
103    pub fn incr(&self, key: &[u8]) -> io::Result<i64> {
104        self.incr_by(key, 1)
105    }
106
107    /// `INCRBY key delta`. Negative `delta` does DECR-style work.
108    pub fn incr_by(&self, key: &[u8], delta: i64) -> io::Result<i64> {
109        ensure_writable(self)?;
110        let mut g = self.wshard(key);
111        let n = g.store.incr_by(key, delta).map_err(store_err)?;
112        commit_write(&mut g, &[b"INCRBY", key, delta.to_string().as_bytes()])?;
113        Ok(n)
114    }
115
116    /// `EXPIRE key seconds`. Returns `true` if a key was touched. The AOF
117    /// records an absolute `PEXPIREAT` deadline (see [`Self::set_with_ttl`])
118    /// so the TTL survives a restart unchanged.
119    pub fn expire(&self, key: &[u8], ttl: Duration) -> io::Result<bool> {
120        ensure_writable(self)?;
121        let mut g = self.wshard(key);
122        let touched = g.store.expire(key, ttl);
123        if touched {
124            let ms = ttl.as_millis().min(u128::from(u64::MAX)) as u64;
125            let deadline = kevy_store::now_unix_ms().saturating_add(ms);
126            commit_write(&mut g, &[b"PEXPIREAT", key, deadline.to_string().as_bytes()])?;
127        }
128        Ok(touched)
129    }
130
131    /// `PERSIST key`. Returns `true` if a TTL was actually cleared.
132    pub fn persist(&self, key: &[u8]) -> io::Result<bool> {
133        ensure_writable(self)?;
134        let mut g = self.wshard(key);
135        let touched = g.store.persist(key);
136        if touched {
137            commit_write(&mut g, &[b"PERSIST", key])?;
138        }
139        Ok(touched)
140    }
141
142    /// Remaining TTL in ms (or Redis-style `-1`/`-2` for no-TTL/no-key).
143    pub fn ttl_ms(&self, key: &[u8]) -> i64 {
144        self.wshard(key).store.pttl(key)
145    }
146
147    /// `TYPE key` — `"string"`, `"hash"`, `"list"`, `"set"`, `"zset"`, or `"none"`.
148    pub fn type_of(&self, key: &[u8]) -> &'static str {
149        self.wshard(key).store.type_of(key)
150    }
151
152    /// `DBSIZE` — total live keys across all shards.
153    pub fn dbsize(&self) -> usize {
154        self.sum_shards(|i| i.store.dbsize())
155    }
156
157    /// `FLUSHALL` — empty every shard (each logs `FLUSHALL` so a replay reaches
158    /// the same empty state).
159    ///
160    /// Named `flushall` — **not** `flush` — to avoid colliding with
161    /// `Write::flush`'s "sync buffered writes to disk" meaning. This call
162    /// WIPES the store; durability needs no explicit call (each write appends
163    /// to the AOF, the shard's `BufWriter` lands per [`AppendFsync`] cadence
164    /// and on drop).
165    ///
166    /// [`AppendFsync`]: crate::AppendFsync
167    pub fn flushall(&self) -> io::Result<()> {
168        ensure_writable(self)?;
169        self.try_for_each_shard(|inner| {
170            inner.store.flushall();
171            commit_write(inner, &[b"FLUSHALL"])
172        })
173    }
174
175    /// Deprecated alias for [`Self::flushall`]. The old name read like
176    /// `Write::flush` (sync-to-disk) but actually WIPES the store — a
177    /// data-loss footgun.
178    #[deprecated(
179        since = "1.2.0",
180        note = "renamed to `flushall`: `flush` collides with Write::flush (sync-to-disk); this WIPES the store"
181    )]
182    pub fn flush(&self) -> io::Result<()> {
183        self.flushall()
184    }
185
186    /// `MEMORY USAGE` for one key — `Some(bytes)` or `None` if absent.
187    pub fn key_bytes(&self, key: &[u8]) -> Option<u64> {
188        self.wshard(key).store.estimate_key_bytes(key)
189    }
190
191    /// Live `used_memory` estimate (summed across shards).
192    pub fn used_memory(&self) -> u64 {
193        self.sum_shards_u64(|i| i.store.used_memory())
194    }
195
196    /// `INFO`-style counter: total keys evicted by `maxmemory` (all shards).
197    pub fn evictions_total(&self) -> u64 {
198        self.sum_shards_u64(|i| i.store.evictions_total())
199    }
200
201    /// `INFO`-style counter: total keys expired (lazy + active reaper, all shards).
202    pub fn expired_keys_total(&self) -> u64 {
203        self.sum_shards_u64(|i| i.store.expired_keys_total())
204    }
205
206    // ---- hash ops -------------------------------------------------------
207
208    /// `HSET key field value [field value ...]`. Returns count newly added.
209    pub fn hset(&self, key: &[u8], pairs: &[(&[u8], &[u8])]) -> io::Result<usize> {
210        ensure_writable(self)?;
211        let mut g = self.wshard(key);
212        let owned: Vec<(Vec<u8>, Vec<u8>)> =
213            pairs.iter().map(|(f, v)| (f.to_vec(), v.to_vec())).collect();
214        let added = g.store.hset(key, &owned).map_err(store_err)?;
215        let mut parts: Vec<&[u8]> = Vec::with_capacity(2 + pairs.len() * 2);
216        parts.push(b"HSET");
217        parts.push(key);
218        for (f, v) in pairs {
219            parts.push(f);
220            parts.push(v);
221        }
222        commit_write(&mut g, &parts)?;
223        Ok(added)
224    }
225
226    /// `HGET key field`. `None` if absent.
227    pub fn hget(&self, key: &[u8], field: &[u8]) -> io::Result<Option<Vec<u8>>> {
228        let mut g = self.wshard(key);
229        Ok(g.store
230            .hget(key, field)
231            .map_err(store_err)?
232            .map(<[u8]>::to_vec))
233    }
234
235    /// `HDEL key field [field ...]`. Returns count actually removed.
236    pub fn hdel(&self, key: &[u8], fields: &[&[u8]]) -> io::Result<usize> {
237        ensure_writable(self)?;
238        let mut g = self.wshard(key);
239        let owned: Vec<Vec<u8>> = fields.iter().map(|f| f.to_vec()).collect();
240        let removed = g.store.hdel(key, &owned).map_err(store_err)?;
241        if removed > 0 {
242            let mut parts: Vec<&[u8]> = Vec::with_capacity(2 + fields.len());
243            parts.push(b"HDEL");
244            parts.push(key);
245            for f in fields {
246                parts.push(f);
247            }
248            commit_write(&mut g, &parts)?;
249        }
250        Ok(removed)
251    }
252
253    // ---- list ops -------------------------------------------------------
254
255    /// `LPUSH key value [value ...]`. Returns the new list length.
256    pub fn lpush(&self, key: &[u8], values: &[&[u8]]) -> io::Result<usize> {
257        push_helper(self, key, values, b"LPUSH", kevy_store::Store::lpush)
258    }
259
260    /// `RPUSH key value [value ...]`. Returns the new list length.
261    pub fn rpush(&self, key: &[u8], values: &[&[u8]]) -> io::Result<usize> {
262        push_helper(self, key, values, b"RPUSH", kevy_store::Store::rpush)
263    }
264
265    /// `LPOP key count`. Returns popped values from the head.
266    pub fn lpop(&self, key: &[u8], count: usize) -> io::Result<Vec<Vec<u8>>> {
267        pop_helper(self, key, count, false)
268    }
269
270    /// `RPOP key count`. Symmetric to `LPOP` from the tail.
271    pub fn rpop(&self, key: &[u8], count: usize) -> io::Result<Vec<Vec<u8>>> {
272        pop_helper(self, key, count, true)
273    }
274
275    /// `LLEN key`. Length of the list at `key`; 0 if absent.
276    pub fn llen(&self, key: &[u8]) -> io::Result<usize> {
277        self.wshard(key).store.llen(key).map_err(store_err)
278    }
279
280    // ---- set ops --------------------------------------------------------
281
282    /// `SADD key member [member ...]`. Returns count newly added.
283    pub fn sadd(&self, key: &[u8], members: &[&[u8]]) -> io::Result<usize> {
284        push_helper(self, key, members, b"SADD", kevy_store::Store::sadd)
285    }
286
287    /// `SREM key member [member ...]`. Returns count actually removed.
288    pub fn srem(&self, key: &[u8], members: &[&[u8]]) -> io::Result<usize> {
289        ensure_writable(self)?;
290        let mut g = self.wshard(key);
291        let owned: Vec<Vec<u8>> = members.iter().map(|m| m.to_vec()).collect();
292        let removed = g.store.srem(key, &owned).map_err(store_err)?;
293        if removed > 0 {
294            let mut parts: Vec<&[u8]> = Vec::with_capacity(2 + members.len());
295            parts.push(b"SREM");
296            parts.push(key);
297            for m in members {
298                parts.push(m);
299            }
300            commit_write(&mut g, &parts)?;
301        }
302        Ok(removed)
303    }
304
305    /// `SMEMBERS key`. Order implementation-defined; empty if absent.
306    pub fn smembers(&self, key: &[u8]) -> io::Result<Vec<Vec<u8>>> {
307        self.wshard(key).store.smembers(key).map_err(store_err)
308    }
309
310    /// `SCARD key`. Member count; 0 if absent.
311    pub fn scard(&self, key: &[u8]) -> io::Result<usize> {
312        self.wshard(key).store.scard(key).map_err(store_err)
313    }
314
315    // ---- zset ops -------------------------------------------------------
316
317    /// `ZADD key score member [score member ...]`. Returns count newly added.
318    pub fn zadd(&self, key: &[u8], pairs: &[(f64, &[u8])]) -> io::Result<usize> {
319        ensure_writable(self)?;
320        let mut g = self.wshard(key);
321        let owned: Vec<(f64, Vec<u8>)> =
322            pairs.iter().map(|(s, m)| (*s, m.to_vec())).collect();
323        let added = g.store.zadd(key, &owned).map_err(store_err)?;
324        let mut score_strs: Vec<Vec<u8>> = Vec::with_capacity(pairs.len());
325        for (s, _) in pairs {
326            score_strs.push(format!("{s}").into_bytes());
327        }
328        let mut parts: Vec<&[u8]> = Vec::with_capacity(2 + pairs.len() * 2);
329        parts.push(b"ZADD");
330        parts.push(key);
331        for (i, (_, m)) in pairs.iter().enumerate() {
332            parts.push(&score_strs[i]);
333            parts.push(m);
334        }
335        commit_write(&mut g, &parts)?;
336        Ok(added)
337    }
338
339    /// `ZREM key member [member ...]`. Returns count actually removed.
340    pub fn zrem(&self, key: &[u8], members: &[&[u8]]) -> io::Result<usize> {
341        ensure_writable(self)?;
342        let mut g = self.wshard(key);
343        let owned: Vec<Vec<u8>> = members.iter().map(|m| m.to_vec()).collect();
344        let removed = g.store.zrem(key, &owned).map_err(store_err)?;
345        if removed > 0 {
346            let mut parts: Vec<&[u8]> = Vec::with_capacity(2 + members.len());
347            parts.push(b"ZREM");
348            parts.push(key);
349            for m in members {
350                parts.push(m);
351            }
352            commit_write(&mut g, &parts)?;
353        }
354        Ok(removed)
355    }
356
357    /// `ZSCORE key member`. `Some(score)` if present.
358    pub fn zscore(&self, key: &[u8], member: &[u8]) -> io::Result<Option<f64>> {
359        self.wshard(key).store.zscore(key, member).map_err(store_err)
360    }
361
362    /// `ZCARD key`. Member count; 0 if absent.
363    pub fn zcard(&self, key: &[u8]) -> io::Result<usize> {
364        self.wshard(key).store.zcard(key).map_err(store_err)
365    }
366
367    // ---- pub/sub --------------------------------------------------------
368
369    /// `PUBLISH channel payload`. Delivers `payload` to every subscriber on
370    /// `channel` (direct + pattern matches) inside this process. Returns
371    /// the count of receivers the message reached.
372    pub fn publish(&self, channel: &[u8], payload: &[u8]) -> usize {
373        // Clone matching senders under the lock, then release before
374        // send() so a slow receiver can't stall unrelated traffic.
375        let plans = {
376            // Pub/sub is process-wide; the bus lives on shard 0.
377            let g = self.lock();
378            g.bus.collect_delivery(channel, payload)
379        };
380        let mut count = 0;
381        for (frame, sender) in plans {
382            if sender.send(frame).is_ok() {
383                count += 1;
384            }
385        }
386        count
387    }
388
389    /// Open a [`Subscription`] subscribed to `channels`. Drop the handle
390    /// to unsubscribe from everything atomically. Pass `&[]` to start
391    /// with no subscriptions and add some later via
392    /// [`Subscription::subscribe`] / [`Subscription::psubscribe`].
393    pub fn subscribe(&self, channels: &[&[u8]]) -> Subscription {
394        let mut sub = Subscription::new(self.inner_handle(), self.guard_handle());
395        if !channels.is_empty() {
396            sub.subscribe(channels);
397        }
398        sub
399    }
400
401    /// Convenience: open a [`Subscription`] starting on pattern subscriptions.
402    pub fn psubscribe(&self, patterns: &[&[u8]]) -> Subscription {
403        let mut sub = Subscription::new(self.inner_handle(), self.guard_handle());
404        if !patterns.is_empty() {
405            sub.psubscribe(patterns);
406        }
407        sub
408    }
409}
410
411// ─────────────────────────────────────────────────────────────────────────
412// Shared list/set push + list pop helpers. `&Store` so we can lock + AOF-log.
413// ─────────────────────────────────────────────────────────────────────────
414
415fn push_helper<F>(
416    s: &Store,
417    key: &[u8],
418    values: &[&[u8]],
419    verb: &'static [u8],
420    op: F,
421) -> io::Result<usize>
422where
423    F: FnOnce(&mut kevy_store::Store, &[u8], &[Vec<u8>]) -> Result<usize, StoreError>,
424{
425    ensure_writable(s)?;
426    let mut g = s.wshard(key);
427    let owned: Vec<Vec<u8>> = values.iter().map(|v| v.to_vec()).collect();
428    let n = op(&mut g.store, key, &owned).map_err(store_err)?;
429    let mut parts: Vec<&[u8]> = Vec::with_capacity(2 + values.len());
430    parts.push(verb);
431    parts.push(key);
432    for v in values {
433        parts.push(v);
434    }
435    commit_write(&mut g, &parts)?;
436    Ok(n)
437}
438
439fn pop_helper(s: &Store, key: &[u8], count: usize, from_tail: bool) -> io::Result<Vec<Vec<u8>>> {
440    ensure_writable(s)?;
441    let mut g = s.wshard(key);
442    let popped = if from_tail {
443        g.store.rpop(key, count).map_err(store_err)?
444    } else {
445        g.store.lpop(key, count).map_err(store_err)?
446    };
447    if !popped.is_empty() {
448        let verb: &[u8] = if from_tail { b"RPOP" } else { b"LPOP" };
449        let count_str = popped.len().to_string();
450        let parts: [&[u8]; 3] = [verb, key, count_str.as_bytes()];
451        commit_write(&mut g, &parts)?;
452    }
453    Ok(popped)
454}