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 serialises this operation per namespace with an in-process Mutex, making it safe across concurrent tokio tasks within the same process. Cross-process atomicity still requires a backend with native CAS (Redis SETNX, SQLite transactions).

Source

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

Counter increment, serialised per namespace within the same process.

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

JsonFileStore acquires a per-namespace Mutex for the full read-modify-write cycle, preventing lost updates across concurrent tokio tasks. For multi-process safety use a backend with native INCR (Redis) or transactions (SQLite).

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.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§