Skip to main content

Module kv

Module kv 

Source
Expand description

std.kv — SQLite-backed key-value store for Lua scripts.

Storage lives in a SQLite database supplied by the host (one shared connection), in a dedicated __kv table:

CREATE TABLE __kv (
    ns    TEXT NOT NULL,
    key   TEXT NOT NULL,
    value TEXT NOT NULL,   -- JSON-serialized Lua value
    PRIMARY KEY (ns, key)
) WITHOUT ROWID;

Trade-offs vs. a JSON-file-per-namespace implementation:

  • Per-key updates (no whole-namespace rewrite on every set).
  • Durability + atomicity delegated to SQLite’s WAL journal.
  • Cross-process writes arbitrated by busy_timeout.

§Wiring contract

Symmetric with crate::sql. The host opens a rusqlite::Connection (typically a database file dedicated to KV scratch state, kept separate from the std.sql user database so backup / WAL / page-cache lifecycles do not collide) and passes it as Arc<Mutex<_>> plus its InterruptHandle. Cancellation and per-query timeout are inherited from the crate::sql::SqlConfig in lua.app_data; the rusqlite those types come from is crate::rusqlite.

Functions§

init_schema
Create the __kv table if it is not there yet.
register
Register std.kv with default SqlConfig (only used if std.sql was not registered first; otherwise the existing config is preserved).
register_with
Register std.kv with caller-provided SqlConfig.