|
ZephyrDB
Embedded database for Zephyr RTOS: KV, time-series, and document models
|
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. | |
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.
| zdb_status_t zdb_kv_close | ( | zdb_kv_t * | kv | ) |
Close a namespace handle.
| kv | Open handle. |
| ZDB_OK | Success. |
| ZDB_ERR_INVAL | kv is NULL or not open. |
| 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.
| db | Initialized instance with a KV backend configured. |
| ZDB_OK | All entries present or written. |
| ZDB_ERR_INVAL | db is NULL, has no KV backend, or the table holds an invalid namespace or key. |
| ZDB_ERR_NOMEM | A default exceeds the KV IO slab block size. |
| ZDB_ERR_COLLISION | A default's record ID slot holds a different key. |
| ZDB_ERR_IO | Backend read or write failure. |
| 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.
| kv | Open namespace handle. |
| key | Key string. |
| ZDB_OK | Success. |
| ZDB_ERR_INVAL | NULL argument or over-long key. |
| ZDB_ERR_NOT_FOUND | No record for this (namespace, key). |
| ZDB_ERR_IO | Backend delete failure. |
| 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.
| kv | Open namespace handle. |
| key | Key string. |
| out_value | Destination buffer (may be NULL if out_capacity is 0). |
| out_capacity | Destination capacity in bytes. |
| out_len | Set to the full stored value length. |
| ZDB_OK | Success. |
| ZDB_ERR_INVAL | NULL argument or over-long key. |
| ZDB_ERR_NOT_FOUND | No record for this (namespace, key), including slots holding a different key or pre-v2 data. |
| ZDB_ERR_IO | Backend read failure. |
| 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.
| kv | Open namespace handle. |
| key | Key string. |
| out_str | Destination buffer; must be at least 1 byte. |
| out_capacity | Destination capacity in bytes, including the terminator. |
| out_len | Set 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. |
| ZDB_OK | Success. |
| ZDB_ERR_INVAL | NULL argument, zero capacity, or over-long key. |
| ZDB_ERR_NOT_FOUND | No record for this (namespace, key). |
| ZDB_ERR_IO | Backend read failure. |
| zdb_status_t zdb_kv_iter_close | ( | zdb_kv_iter_t * | iter | ) |
Close an iterator.
| iter | Open iterator. |
| ZDB_OK | Success. |
| ZDB_ERR_INVAL | iter is NULL or not open. |
| 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.
| iter | Open iterator. |
| out_key | Buffer for the NUL-terminated key. |
| out_key_capacity | Capacity of out_key in bytes. |
| out_key_len | Set to the full key length. |
| out_value | Buffer for the value (may be NULL if capacity 0). |
| out_value_capacity | Capacity of out_value in bytes. |
| out_value_len | Set to the full stored value length. |
| ZDB_OK | A record was produced. |
| ZDB_ERR_NOT_FOUND | Iteration is exhausted. |
| ZDB_ERR_INVAL | NULL argument or iterator not 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.
| kv | Open namespace handle. |
| out_iter | Iterator to initialize. |
| ZDB_OK | Success. |
| ZDB_ERR_INVAL | NULL argument. |
| zdb_status_t zdb_kv_open | ( | zdb_t * | db, |
| const char * | namespace_name, | ||
| zdb_kv_t * | kv ) |
Open a namespace handle.
| kv | Handle to fill. |
| db | Initialized instance with a KV backend configured. |
| namespace_name | Namespace string, at most CONFIG_ZDB_MAX_KEY_LEN bytes; kept by reference and must outlive kv. |
| ZDB_OK | Success. |
| ZDB_ERR_INVAL | NULL argument or over-long 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.
| kv | Open namespace handle. |
| ZDB_OK | Namespace cleared and defaults applied. |
| ZDB_ERR_INVAL | NULL argument or no KV backend configured. |
| ZDB_ERR_NOMEM | The key index could not be allocated. |
| ZDB_ERR_COLLISION | A default's record ID slot holds a different key. |
| ZDB_ERR_IO | Backend delete or write failure. |
| 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).
| kv | Open namespace handle. |
| key | Key string, at most CONFIG_ZDB_MAX_KEY_LEN bytes. |
| value | Value bytes; may be NULL when value_len is 0. |
| value_len | Value length in bytes; 0 stores an empty value. |
| ZDB_OK | Success (including a byte-identical rewrite). |
| ZDB_ERR_INVAL | NULL argument or over-long key. |
| ZDB_ERR_NOMEM | Record exceeds the KV I/O slab block size. |
| ZDB_ERR_COLLISION | The key's backend record ID is occupied by a different (namespace, key); the stored record is untouched — choose a different key name. |
| ZDB_ERR_IO | Backend write failure. |
| 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.
| kv | Open namespace handle. |
| key | Key string. |
| value | NUL-terminated value to store. |
| ZDB_OK | Success. |
| ZDB_ERR_INVAL | NULL argument or over-long key. |
| ZDB_ERR_NOMEM | Record exceeds the KV IO slab block size, or no slab block was free. |
| ZDB_ERR_COLLISION | The record ID slot holds a different key. |
| ZDB_ERR_IO | Backend write failure. |