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

Instance lifecycle, status codes, health, telemetry, and the cursor framework. More...

Classes

struct  zdb_bytes_t
 Immutable byte span (pointer + length). More...
struct  zdb_ts_stats_t
 Time-series durability counters (see zdb_ts_stats_get()). More...
struct  zdb_ts_stats_export_t
 Compact, packed telemetry export of zdb_ts_stats_t for transport or logging. More...
struct  zdb_kv_default_t
 One entry in a key-value defaults table. More...
struct  zdb_cfg_t
 Instance configuration passed to zdb_init(). More...
struct  zdb_t
 Database instance. More...
struct  zdb_cursor
 Cursor for cooperative scans (currently opened via zdb_ts_cursor_open()). More...

Macros

#define ZDB_DEFINE_STATIC(db_name, cfg_name)
 Declare a static zdb_t wired to Kconfig-sized slabs.

Typedefs

typedef bool(* zdb_predicate_fn) (zdb_model_t model, const zdb_bytes_t *record, void *user_ctx)
 Record filter callback for cursor scans.
typedef struct zdb_cursor zdb_cursor_t
 Cursor for cooperative scans (currently opened via zdb_ts_cursor_open()).

Enumerations

enum  zdb_status_t {
  ZDB_OK = 0 , ZDB_ERR_INVAL , ZDB_ERR_NOMEM , ZDB_ERR_NOT_FOUND ,
  ZDB_ERR_IO , ZDB_ERR_BUSY , ZDB_ERR_TIMEOUT , ZDB_ERR_UNSUPPORTED ,
  ZDB_ERR_CORRUPT , ZDB_ERR_INTERNAL , ZDB_ERR_COLLISION
}
 Status codes returned by every ZephyrDB API. More...
enum  zdb_model_t { ZDB_MODEL_CORE = 0 , ZDB_MODEL_KV , ZDB_MODEL_TS }
 Data-model selector used by cursors and predicates. More...
enum  zdb_backend_t {
  ZDB_BACKEND_NONE = 0 , ZDB_BACKEND_NVS , ZDB_BACKEND_ZMS , ZDB_BACKEND_LFS ,
  ZDB_BACKEND_FCB
}
 Storage backend identifiers. More...
enum  zdb_health_t { ZDB_HEALTH_OK = 0 , ZDB_HEALTH_DEGRADED , ZDB_HEALTH_READONLY , ZDB_HEALTH_FAULT }
 Instance health as reported by zdb_health(). More...

Functions

zdb_status_t zdb_init (zdb_t *db, const zdb_cfg_t *cfg)
 Initialize a database instance.
zdb_status_t zdb_deinit (zdb_t *db)
 Tear down an instance and release all module state.
zdb_health_t zdb_health (const zdb_t *db)
 Report the instance's current health.
const char * zdb_status_str (zdb_status_t status)
 Return a short printable name for a status code.
const char * zdb_health_str (zdb_health_t health)
 Return a short printable name for a health value.
void zdb_ts_stats_get (const zdb_t *db, zdb_ts_stats_t *out_stats)
 Copy the live time-series durability counters.
void zdb_ts_stats_reset (zdb_t *db)
 Reset the time-series durability counters to zero.
zdb_status_t zdb_ts_stats_export (const zdb_t *db, zdb_ts_stats_export_t *out_export)
 Fill a packed, CRC-protected telemetry export of the TS stats.
zdb_status_t zdb_ts_stats_export_validate (const zdb_ts_stats_export_t *export_data)
 Validate a received telemetry export record.
zdb_status_t zdb_cursor_reset (zdb_cursor_t *cursor)
 Rewind a cursor to the start of its scan.
zdb_status_t zdb_cursor_close (zdb_cursor_t *cursor)
 Close a cursor and release its slab allocation.
void zdb_shell_register (zdb_t *db)
 Bind an instance to the zdb shell command tree (CONFIG_ZDB_SHELL).

Detailed Description

Instance lifecycle, status codes, health, telemetry, and the cursor framework.

Macro Definition Documentation

◆ ZDB_DEFINE_STATIC

#define ZDB_DEFINE_STATIC ( db_name,
cfg_name )
Value:
ZDB_DEFINE_CORE_SLAB(_zdb_core_##db_name); \
ZDB_DEFINE_CURSOR_SLAB(_zdb_cursor_##db_name); \
_ZDB_DEFINE_KV_IO_SLAB(_zdb_kv_io_##db_name) \
_ZDB_DEFINE_TS_INGEST_SLAB(_zdb_ts_ingest_##db_name) \
static zdb_t db_name = { \
.core_slab = &_zdb_core_##db_name, \
.cursor_slab = &_zdb_cursor_##db_name, \
_ZDB_KV_IO_SLAB_INIT(_zdb_kv_io_##db_name) \
_ZDB_TS_INGEST_SLAB_INIT(_zdb_ts_ingest_##db_name) \
}
#define ZDB_DEFINE_CORE_SLAB(name)
Declare the core-context slab, sized by CONFIG_ZDB_CORE_SLAB_BLOCK_SIZE / _COUNT.
Definition zephyrdb.h:77
Database instance.
Definition zephyrdb.h:444
struct k_mem_slab * cursor_slab
Definition zephyrdb.h:447

Declare a static zdb_t wired to Kconfig-sized slabs.

Declares the core and cursor slabs plus the KV I/O and TS ingest slabs for whichever of those modules are enabled, and a static zdb_t db_name pointing at them. Call zdb_init(&db_name, &cfg) before first use.

Parameters
db_nameName of the zdb_t variable to declare.
cfg_nameConfiguration name; currently unused by the macro — pass the same zdb_cfg_t to zdb_init().

Typedef Documentation

◆ zdb_cursor_t

typedef struct zdb_cursor zdb_cursor_t

Cursor for cooperative scans (currently opened via zdb_ts_cursor_open()).

Treat the members as internal; drive the cursor with zdb_cursor_next(), zdb_cursor_reset(), and zdb_cursor_close().

◆ zdb_predicate_fn

typedef bool(* zdb_predicate_fn) (zdb_model_t model, const zdb_bytes_t *record, void *user_ctx)

Record filter callback for cursor scans.

Parameters
modelData model the record belongs to.
recordRaw record bytes (valid only during the call).
user_ctxOpaque pointer supplied at cursor open.
Returns
true to yield the record from zdb_cursor_next(), false to skip it.

Enumeration Type Documentation

◆ zdb_backend_t

Storage backend identifiers.

Enumerator
ZDB_BACKEND_NONE 

No backend bound.

ZDB_BACKEND_NVS 

Zephyr NVS (KV).

ZDB_BACKEND_ZMS 

Zephyr ZMS (KV).

ZDB_BACKEND_LFS 

LittleFS file backend (TS, DOC).

ZDB_BACKEND_FCB 

Flash Circular Buffer (TS).

◆ zdb_health_t

Instance health as reported by zdb_health().

Health is derived from time-series integrity checks: a stream whose records fail framing or CRC validation, or which cannot be recovered, moves the instance to ZDB_HEALTH_DEGRADED. A build without CONFIG_ZDB_TS therefore stays ZDB_HEALTH_OK for its lifetime.

Health only ever worsens. Nothing restores an instance to ZDB_HEALTH_OK, because the faults it records are evidence that data was already lost or rewritten; re-run zdb_init() for a fresh verdict. Each change is reported once as a ZDB_CORE_EVENT_HEALTH event.

Enumerator
ZDB_HEALTH_OK 

Fully operational.

ZDB_HEALTH_DEGRADED 

Operational with recovered/ignored faults.

ZDB_HEALTH_READONLY 

Writes disabled.

ZDB_HEALTH_FAULT 

Not operational.

◆ zdb_model_t

Data-model selector used by cursors and predicates.

Enumerator
ZDB_MODEL_CORE 

Core/meta records.

ZDB_MODEL_KV 

Key-value records.

ZDB_MODEL_TS 

Time-series samples.

◆ zdb_status_t

Status codes returned by every ZephyrDB API.

Enumerator
ZDB_OK 

Success.

ZDB_ERR_INVAL 

Invalid argument (NULL pointer, bad length, ...).

ZDB_ERR_NOMEM 

Out of slab memory, or payload exceeds a slab block.

ZDB_ERR_NOT_FOUND 

Requested record/key/document does not exist.

ZDB_ERR_IO 

Backend storage read/write failure.

ZDB_ERR_BUSY 

Resource is held (e.g. another TS stream is active).

ZDB_ERR_TIMEOUT 

Timed out waiting (e.g. zdb_ts_flush_sync()).

ZDB_ERR_UNSUPPORTED 

Operation not supported by the active backend/build.

ZDB_ERR_CORRUPT 

Stored data failed integrity checks (CRC, framing).

ZDB_ERR_INTERNAL 

Unexpected internal state.

ZDB_ERR_COLLISION 

KV record-ID hash collision with a different key (see zdb_kv_set()).

Function Documentation

◆ zdb_cursor_close()

zdb_status_t zdb_cursor_close ( zdb_cursor_t * cursor)

Close a cursor and release its slab allocation.

Parameters
cursorOpen cursor; safe to call once per open.
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALcursor is NULL or not open.

◆ zdb_cursor_reset()

zdb_status_t zdb_cursor_reset ( zdb_cursor_t * cursor)

Rewind a cursor to the start of its scan.

Parameters
cursorOpen cursor.
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALcursor is NULL or not open.

◆ zdb_deinit()

zdb_status_t zdb_deinit ( zdb_t * db)

Tear down an instance and release all module state.

Closes any active TS stream and frees internal contexts back to the slabs. The instance may be re-initialized with zdb_init() afterwards.

Parameters
dbInitialized instance.
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALdb is NULL or not initialized.

◆ zdb_health()

zdb_health_t zdb_health ( const zdb_t * db)

Report the instance's current health.

Parameters
dbInitialized instance.
Returns
Current zdb_health_t; ZDB_HEALTH_FAULT if db is NULL or uninitialized.

◆ zdb_health_str()

const char * zdb_health_str ( zdb_health_t health)

Return a short printable name for a health value.

Parameters
healthAny zdb_health_t value.
Returns
Static string such as "OK" or "DEGRADED"; never NULL.

◆ zdb_init()

zdb_status_t zdb_init ( zdb_t * db,
const zdb_cfg_t * cfg )

Initialize a database instance.

Binds cfg (kept by reference — it must outlive the instance), validates the slab wiring for the enabled modules, and readies the instance for module opens.

Parameters
dbInstance declared via ZDB_DEFINE_STATIC() or manually wired.
cfgConfiguration; see zdb_cfg_t for backend requirements.
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALdb or cfg is NULL, or a required slab is missing.

◆ zdb_shell_register()

void zdb_shell_register ( zdb_t * db)

Bind an instance to the zdb shell command tree (CONFIG_ZDB_SHELL).

Registers db as the target of every zdb command. Call it after zdb_init(); until then the commands report that no instance is registered. One instance at a time; a later call replaces the binding, and passing NULL unbinds.

See docs/shell.md for the command reference.

Parameters
dbInitialized instance, or NULL to unbind.

◆ zdb_status_str()

const char * zdb_status_str ( zdb_status_t status)

Return a short printable name for a status code.

Parameters
statusAny zdb_status_t value.
Returns
Static string such as "OK" or "COLLISION"; never NULL.

◆ zdb_ts_stats_export()

zdb_status_t zdb_ts_stats_export ( const zdb_t * db,
zdb_ts_stats_export_t * out_export )

Fill a packed, CRC-protected telemetry export of the TS stats.

The CRC covers every byte of the record except the crc field itself, so any tampering or transport corruption is detected by zdb_ts_stats_export_validate().

Parameters
dbInitialized instance.
out_exportDestination record.
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALdb or out_export is NULL.

◆ zdb_ts_stats_export_validate()

zdb_status_t zdb_ts_stats_export_validate ( const zdb_ts_stats_export_t * export_data)

Validate a received telemetry export record.

Parameters
export_dataRecord to check.
Return values
ZDB_OKCRC and version are valid.
ZDB_ERR_INVALexport_data is NULL.
ZDB_ERR_CORRUPTCRC mismatch.
ZDB_ERR_UNSUPPORTEDUnknown format version.

◆ zdb_ts_stats_get()

void zdb_ts_stats_get ( const zdb_t * db,
zdb_ts_stats_t * out_stats )

Copy the live time-series durability counters.

Parameters
dbInitialized instance.
out_statsDestination; zeroed when db is NULL/uninitialized.

◆ zdb_ts_stats_reset()

void zdb_ts_stats_reset ( zdb_t * db)

Reset the time-series durability counters to zero.

Parameters
dbInitialized instance (NULL is ignored).