Skip to content

local-store: add contains, len and count metadata queries - #527

Closed
lgahdl wants to merge 1 commit into
developfrom
feat/291-local-store-metadata-queries
Closed

local-store: add contains, len and count metadata queries#527
lgahdl wants to merge 1 commit into
developfrom
feat/291-local-store-metadata-queries

Conversation

@lgahdl

@lgahdl lgahdl commented Jul 21, 2026

Copy link
Copy Markdown
Collaborator

Summary

nexum:host/local-store only exposes get/set/delete/list-keys, so a guest answering "does this key exist," "how big is this value," or "how many keys match this prefix" has to transfer the full payload (or full key list) across the wasm boundary just to discard it.

Adds three metadata queries, all answering without transferring the bytes:

  • contains(key) -> bool
  • len(key) -> option<u64> (byte length, none if absent)
  • count(prefix) -> u64 (key cardinality)

Design

  • wit/nexum-host/local-store.wit: the three new funcs.
  • StateHandle (crates/nexum-runtime/src/host/component/state.rs) and the SDK's LocalStoreHost trait (crates/nexum-sdk/src/host.rs) both gain default bodies over get/list_keys, so existing implementors keep compiling unchanged.
  • The redb backend (local_store_redb.rs) overrides all three with cheap paths: contains/len read the entry in place without copying the value out; count is a bounded B-tree range scan that never materialises key strings.
  • Per the issue's needs-design note: doc comments say len/count "may be a scan" rather than promising O(1), since not every backend can beat the default.
  • Wired through crates/nexum-runtime/src/host/impls/local_store.rs (host trait impl), crates/nexum-sdk/src/wit_bindgen_macro.rs (guest-side macro), and both mock crates (nexum-sdk-test, shepherd-sdk-test) with matching overrides + tests.

Testing

  • New unit tests for the redb backend (contains/len/count, including namespace isolation) and for nexum-sdk-test's MockLocalStore (including fault-injection interaction).
  • A default-bodies test in nexum-sdk/src/host.rs verifying contains/len/count derive correctly from only the four required trait methods.
  • cargo check --workspace, cargo clippy --all-targets -- -D warnings (on the touched crates), and cargo fmt --all -- --check all pass.

Closes #291

get/list-keys force a guest to copy a full payload across the wasm
boundary to answer questions that don't need the payload: existence
checks marshal the whole value, size queries fetch it, prefix counts
materialise the whole key list. Add three metadata queries that answer
without transferring bytes:

- contains(key) -> bool: existence without fetching the value.
- len(key) -> option<u64>: value byte length, none if absent.
- count(prefix) -> u64: key cardinality without materialising the list.

The redb backend answers contains/len from the entry in place and
count from a bounded range scan (no key strings materialised). The
StateHandle trait and the SDK's LocalStoreHost trait ship default
bodies over get/list_keys, so existing backends and mocks keep
compiling; the redb backend and both mock crates additionally override
with the cheap path. Doc comments flag that len/count may still be a
scan on backends that can't do better (per the issue's needs-design
note on naming/complexity), rather than promising O(1).

Closes #291
@lgahdl
lgahdl requested a review from mfw78 as a code owner July 21, 2026 19:31
@lgahdl

lgahdl commented Jul 21, 2026

Copy link
Copy Markdown
Collaborator Author

Duplicated: #457

@lgahdl lgahdl closed this Jul 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

local-store: add contains, len and count metadata queries

1 participant