ZephyrDB
Embedded database for Zephyr RTOS: KV, time-series, and document models
Loading...
Searching...
No Matches
Key-Value

Namespaced key-value storage on NVS or ZMS (CONFIG_ZDB_KV). More...

Classes

struct  zdb_kv_t
 Open handle to one KV namespace (see zdb_kv_open()). More...
struct  zdb_kv_iter_t
 KV iterator state (see zdb_kv_iter_open()). More...

Functions

zdb_status_t zdb_kv_open (zdb_t *db, const char *namespace_name, zdb_kv_t *kv)
 Open a namespace handle.
zdb_status_t zdb_kv_close (zdb_kv_t *kv)
 Close a namespace handle.
zdb_status_t zdb_kv_set (zdb_kv_t *kv, const char *key, const void *value, size_t value_len)
 Store (create or overwrite) a value under a key.
zdb_status_t zdb_kv_get (zdb_kv_t *kv, const char *key, void *out_value, size_t out_capacity, size_t *out_len)
 Read the value stored under a key.
zdb_status_t zdb_kv_set_str (zdb_kv_t *kv, const char *key, const char *value)
 Store a NUL-terminated string.
zdb_status_t zdb_kv_defaults_apply (zdb_t *db)
 Write any missing entries of the configured defaults table.
zdb_status_t zdb_kv_reset_namespace (zdb_kv_t *kv)
 Delete every key in a namespace, then re-apply its defaults.
zdb_status_t zdb_kv_get_str (zdb_kv_t *kv, const char *key, char *out_str, size_t out_capacity, size_t *out_len)
 Read a value as a NUL-terminated string.
zdb_status_t zdb_kv_delete (zdb_kv_t *kv, const char *key)
 Delete the record stored under a key.
zdb_status_t zdb_kv_iter_open (zdb_kv_t *kv, zdb_kv_iter_t *out_iter)
 Open an iterator over the namespace's keys.
zdb_status_t zdb_kv_iter_next (zdb_kv_iter_t *iter, char *out_key, size_t out_key_capacity, size_t *out_key_len, void *out_value, size_t out_value_capacity, size_t *out_value_len)
 Fetch the next key (and its value) from an iterator.
zdb_status_t zdb_kv_iter_close (zdb_kv_iter_t *iter)
 Close an iterator.

Detailed Description

Namespaced key-value storage on NVS or ZMS (CONFIG_ZDB_KV).

Records are persisted in the v2 on-disk format [0xDB tag][ns_len][key_len][namespace][key][value] under a backend record ID hashed (FNV-1a) from the namespace and key together, so namespaces are isolated in storage and every read verifies the full identity of the stored record. Records written by pre-v2 builds are treated as absent and their slots are reclaimed on the next set.

Function Documentation

◆ zdb_kv_close()

zdb_status_t zdb_kv_close ( zdb_kv_t * kv)

Close a namespace handle.

Parameters
kvOpen handle.
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALkv is NULL or not open.

◆ zdb_kv_defaults_apply()

zdb_status_t zdb_kv_defaults_apply ( zdb_t * db)

Write any missing entries of the configured defaults table.

For each entry in zdb_cfg_t::kv_defaults, writes the default value only when the key is absent. Existing keys are left alone, so values the user or application changed survive.

zdb_init() runs this automatically when a defaults table is configured, which covers both first boot (nothing is stored, so everything is seeded) and a firmware update that adds new entries (only the new keys are written). Call it directly only to re-run the pass without re-initializing.

A key deleted by the application is indistinguishable from one that was never written, so it is re-seeded on the next pass. Use zdb_kv_set() with an explicit value if a key must stay absent.

Every entry is attempted even if one fails; the first failing status is returned.

Parameters
dbInitialized instance with a KV backend configured.
Return values
ZDB_OKAll entries present or written.
ZDB_ERR_INVALdb is NULL, has no KV backend, or the table holds an invalid namespace or key.
ZDB_ERR_NOMEMA default exceeds the KV IO slab block size.
ZDB_ERR_COLLISIONA default's record ID slot holds a different key.
ZDB_ERR_IOBackend read or write failure.

◆ zdb_kv_delete()

zdb_status_t zdb_kv_delete ( zdb_kv_t * kv,
const char * key )

Delete the record stored under a key.

Verifies the stored record's identity first, so a colliding record belonging to a different (namespace, key) is never deleted.

Parameters
kvOpen namespace handle.
keyKey string.
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALNULL argument or over-long key.
ZDB_ERR_NOT_FOUNDNo record for this (namespace, key).
ZDB_ERR_IOBackend delete failure.

◆ zdb_kv_get()

zdb_status_t zdb_kv_get ( zdb_kv_t * kv,
const char * key,
void * out_value,
size_t out_capacity,
size_t * out_len )

Read the value stored under a key.

Never fails on a too-small buffer: copies up to out_capacity bytes and reports the full stored length in out_len — compare the two to detect truncation.

Parameters
kvOpen namespace handle.
keyKey string.
out_valueDestination buffer (may be NULL if out_capacity is 0).
out_capacityDestination capacity in bytes.
out_lenSet to the full stored value length.
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALNULL argument or over-long key.
ZDB_ERR_NOT_FOUNDNo record for this (namespace, key), including slots holding a different key or pre-v2 data.
ZDB_ERR_IOBackend read failure.

◆ zdb_kv_get_str()

zdb_status_t zdb_kv_get_str ( zdb_kv_t * kv,
const char * key,
char * out_str,
size_t out_capacity,
size_t * out_len )

Read a value as a NUL-terminated string.

Never fails on a too-small buffer and always terminates out_str, copying at most out_capacity - 1 bytes. Detect truncation with *out_len + 1 > out_capacity.

Values written by zdb_kv_set_str() round-trip exactly. A value written through zdb_kv_set() without a terminator is also returned as a terminated string.

Parameters
kvOpen namespace handle.
keyKey string.
out_strDestination buffer; must be at least 1 byte.
out_capacityDestination capacity in bytes, including the terminator.
out_lenSet to the string length excluding the terminator. If the value did not fit, set to the stored byte count, which bounds the buffer size needed.
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALNULL argument, zero capacity, or over-long key.
ZDB_ERR_NOT_FOUNDNo record for this (namespace, key).
ZDB_ERR_IOBackend read failure.

◆ zdb_kv_iter_close()

zdb_status_t zdb_kv_iter_close ( zdb_kv_iter_t * iter)

Close an iterator.

Parameters
iterOpen iterator.
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALiter is NULL or not open.

◆ zdb_kv_iter_next()

zdb_status_t zdb_kv_iter_next ( zdb_kv_iter_t * iter,
char * out_key,
size_t out_key_capacity,
size_t * out_key_len,
void * out_value,
size_t out_value_capacity,
size_t * out_value_len )

Fetch the next key (and its value) from an iterator.

Both copies clamp to their capacity while reporting full lengths, following the zdb_kv_get() truncation contract.

Parameters
iterOpen iterator.
out_keyBuffer for the NUL-terminated key.
out_key_capacityCapacity of out_key in bytes.
out_key_lenSet to the full key length.
out_valueBuffer for the value (may be NULL if capacity 0).
out_value_capacityCapacity of out_value in bytes.
out_value_lenSet to the full stored value length.
Return values
ZDB_OKA record was produced.
ZDB_ERR_NOT_FOUNDIteration is exhausted.
ZDB_ERR_INVALNULL argument or iterator not open.

◆ zdb_kv_iter_open()

zdb_status_t zdb_kv_iter_open ( zdb_kv_t * kv,
zdb_kv_iter_t * out_iter )

Open an iterator over the namespace's keys.

Enumerates every key the index holds, including keys written before the current boot when CONFIG_ZDB_KV_PERSIST_INDEX is enabled (the default). With it disabled the iterator covers only keys set or deleted during this session.

The index holds at most CONFIG_ZDB_KV_INDEX_MAX_ENTRIES keys. Keys stored beyond that bound remain readable through zdb_kv_get() but are not returned by the iterator.

Parameters
kvOpen namespace handle.
out_iterIterator to initialize.
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALNULL argument.

◆ zdb_kv_open()

zdb_status_t zdb_kv_open ( zdb_t * db,
const char * namespace_name,
zdb_kv_t * kv )

Open a namespace handle.

Parameters
kvHandle to fill.
dbInitialized instance with a KV backend configured.
namespace_nameNamespace string, at most CONFIG_ZDB_MAX_KEY_LEN bytes; kept by reference and must outlive kv.
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALNULL argument or over-long namespace.

◆ zdb_kv_reset_namespace()

zdb_status_t zdb_kv_reset_namespace ( zdb_kv_t * kv)

Delete every key in a namespace, then re-apply its defaults.

The factory-reset primitive: deletes each key the index holds for this namespace, then runs the defaults pass for it, leaving the namespace exactly as a first boot would. Other namespaces are untouched. Each deletion emits ZDB_EVENT_KV_DELETE.

Deletion covers the keys the iterator can see, so the same bound applies: keys stored beyond CONFIG_ZDB_KV_INDEX_MAX_ENTRIES are not enumerable and therefore not deleted. With CONFIG_ZDB_KV_PERSIST_INDEX disabled, only keys touched in this session are removed.

Every key is attempted even if one fails; the first failing status is returned, and the defaults pass still runs.

Parameters
kvOpen namespace handle.
Return values
ZDB_OKNamespace cleared and defaults applied.
ZDB_ERR_INVALNULL argument or no KV backend configured.
ZDB_ERR_NOMEMThe key index could not be allocated.
ZDB_ERR_COLLISIONA default's record ID slot holds a different key.
ZDB_ERR_IOBackend delete or write failure.

◆ zdb_kv_set()

zdb_status_t zdb_kv_set ( zdb_kv_t * kv,
const char * key,
const void * value,
size_t value_len )

Store (create or overwrite) a value under a key.

The maximum value size is CONFIG_ZDB_KV_IO_SLAB_BLOCK_SIZE - 3 - strlen(namespace) - strlen(key).

Parameters
kvOpen namespace handle.
keyKey string, at most CONFIG_ZDB_MAX_KEY_LEN bytes.
valueValue bytes; may be NULL when value_len is 0.
value_lenValue length in bytes; 0 stores an empty value.
Return values
ZDB_OKSuccess (including a byte-identical rewrite).
ZDB_ERR_INVALNULL argument or over-long key.
ZDB_ERR_NOMEMRecord exceeds the KV I/O slab block size.
ZDB_ERR_COLLISIONThe key's backend record ID is occupied by a different (namespace, key); the stored record is untouched — choose a different key name.
ZDB_ERR_IOBackend write failure.

◆ zdb_kv_set_str()

zdb_status_t zdb_kv_set_str ( zdb_kv_t * kv,
const char * key,
const char * value )

Store a NUL-terminated string.

Convenience wrapper over zdb_kv_set() that stores strlen(value) + 1 bytes, so the terminator is part of the stored value. An empty string is stored as a single NUL byte.

Parameters
kvOpen namespace handle.
keyKey string.
valueNUL-terminated value to store.
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALNULL argument or over-long key.
ZDB_ERR_NOMEMRecord exceeds the KV IO slab block size, or no slab block was free.
ZDB_ERR_COLLISIONThe record ID slot holds a different key.
ZDB_ERR_IOBackend write failure.