Skip to main content

StateStore

Trait StateStore 

Source
pub trait StateStore: Send + Sync {
    // Required methods
    fn get(&self, ns: &str, key: &str) -> Result<Option<Value>, String>;
    fn set(&self, ns: &str, key: &str, value: Value) -> Result<(), String>;
    fn delete(&self, ns: &str, key: &str) -> Result<bool, String>;
    fn keys(&self, ns: &str) -> Result<Vec<String>, String>;
    fn has(&self, ns: &str, key: &str) -> Result<bool, String>;
    fn set_nx(&self, ns: &str, key: &str, value: Value) -> Result<bool, String>;
    fn incr(
        &self,
        ns: &str,
        key: &str,
        delta: f64,
        default: f64,
    ) -> Result<f64, String>;
}
Expand description

Backend-agnostic key-value state store.

All operations are namespace-scoped. Implementations must be Send + Sync so they can be shared across Lua VMs (e.g. fork).

Required Methods§

Source

fn get(&self, ns: &str, key: &str) -> Result<Option<Value>, String>

Read a value. Returns None if the key does not exist.

Source

fn set(&self, ns: &str, key: &str, value: Value) -> Result<(), String>

Write a value (upsert).

Source

fn delete(&self, ns: &str, key: &str) -> Result<bool, String>

Remove a key. Returns true if it existed.

Source

fn keys(&self, ns: &str) -> Result<Vec<String>, String>

List all keys in a namespace.

Source

fn has(&self, ns: &str, key: &str) -> Result<bool, String>

Check whether a key exists.

Whether this is cheaper than get + nil check depends on the backend. JsonFileStore still loads the whole namespace; backends like Redis or SQLite can answer with an EXISTS command.

Source

fn set_nx(&self, ns: &str, key: &str, value: Value) -> Result<bool, String>

Set a value only if the key does not already exist. Returns true if the value was written, false if the key was already present.

Note: JsonFileStore performs a non-locking load-check-save cycle. This is safe within a single process but not across concurrent processes. Backends with native CAS (Redis SETNX, SQLite transactions) will provide true atomicity.

Source

fn incr( &self, ns: &str, key: &str, delta: f64, default: f64, ) -> Result<f64, String>

Counter increment (single-process atomic).

Adds delta to the current numeric value at key. If the key is missing, initialises it to default before adding. Returns the new value.

Note: JsonFileStore performs a non-locking read-modify-write. Safe within one process; use a backend with native INCR (Redis) or transactions (SQLite) for multi-process safety.

Uses f64 internally. Integer-valued deltas are exact; fractional deltas may accumulate floating-point rounding errors over many calls.

Errors if the existing value is not a JSON number.

Implementors§