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

Typed-field documents in collections on a mounted filesystem (CONFIG_ZDB_DOC, requires CONFIG_FILE_SYSTEM). More...

Classes

union  zdb_doc_field_value_t
 Field value storage; the active member follows zdb_doc_field_type_t. More...
struct  zdb_doc_field_t
 One named, typed document field. More...
struct  zdb_doc
 In-memory document handle. More...
struct  zdb_doc_query_filter_t
 One equality filter for zdb_doc_query(); filters AND-combine. More...
struct  zdb_doc_query_t
 Query description for zdb_doc_query(). More...
 Per-document metadata produced by zdb_doc_query(). More...
struct  zdb_doc_manifest_entry_t
 One (collection, document) pair in a collection manifest. More...

Typedefs

typedef struct zdb_doc zdb_doc_t
 In-memory document handle.

Enumerations

enum  zdb_doc_field_type_t {
  ZDB_DOC_FIELD_NULL = 0 , ZDB_DOC_FIELD_INT64 , ZDB_DOC_FIELD_DOUBLE , ZDB_DOC_FIELD_STRING ,
  ZDB_DOC_FIELD_BOOL , ZDB_DOC_FIELD_BYTES , ZDB_DOC_FIELD_OBJECT , ZDB_DOC_FIELD_ARRAY
}
 Document field types. More...

Functions

zdb_status_t zdb_doc_create (zdb_t *db, const char *collection_name, const char *document_id, zdb_doc_t *out_doc)
 Create a new, empty document.
zdb_status_t zdb_doc_open (zdb_t *db, const char *collection_name, const char *document_id, zdb_doc_t *out_doc)
 Load an existing document from storage.
zdb_status_t zdb_doc_save (zdb_doc_t *doc)
 Persist a document's current fields.
zdb_status_t zdb_doc_delete (zdb_t *db, const char *collection_name, const char *document_id)
 Delete a document from storage.
zdb_status_t zdb_doc_close (zdb_doc_t *doc)
 Close a document handle and release its in-memory fields.
zdb_status_t zdb_doc_field_set_i64 (zdb_doc_t *doc, const char *field_name, int64_t value)
 Set (create or replace) an INT64 field.
zdb_status_t zdb_doc_field_set_f64 (zdb_doc_t *doc, const char *field_name, double value)
 Set (create or replace) a DOUBLE field.
zdb_status_t zdb_doc_field_set_string (zdb_doc_t *doc, const char *field_name, const char *value)
 Set (create or replace) a STRING field.
zdb_status_t zdb_doc_field_set_bool (zdb_doc_t *doc, const char *field_name, bool value)
 Set (create or replace) a BOOL field.
zdb_status_t zdb_doc_field_set_bytes (zdb_doc_t *doc, const char *field_name, const void *value, size_t len)
 Set (create or replace) a BYTES field.
zdb_status_t zdb_doc_field_get_i64 (const zdb_doc_t *doc, const char *field_name, int64_t *out_value)
 Read an INT64 field.
zdb_status_t zdb_doc_field_get_f64 (const zdb_doc_t *doc, const char *field_name, double *out_value)
 Read a DOUBLE field.
zdb_status_t zdb_doc_field_get_string (const zdb_doc_t *doc, const char *field_name, const char **out_value)
 Read a STRING field.
zdb_status_t zdb_doc_field_get_bool (const zdb_doc_t *doc, const char *field_name, bool *out_value)
 Read a BOOL field.
zdb_status_t zdb_doc_field_get_bytes (const zdb_doc_t *doc, const char *field_name, zdb_bytes_t *out_value)
 Read a BYTES field.
zdb_status_t zdb_doc_query (zdb_t *db, const zdb_doc_query_t *query, zdb_doc_metadata_t *out_metadata, size_t *out_count)
 Query document metadata across all collections.
zdb_status_t zdb_doc_metadata_free (zdb_doc_metadata_t *metadata, size_t count)
 Release strings owned by zdb_doc_query() results.
zdb_status_t zdb_doc_export_flatbuffer (zdb_doc_t *doc, uint8_t *out_buf, size_t out_capacity, size_t *out_len)
 Serialize a document as a FlatBuffer for transport.

Detailed Description

Typed-field documents in collections on a mounted filesystem (CONFIG_ZDB_DOC, requires CONFIG_FILE_SYSTEM).

Documents are persisted one file per document under the configured mount point. Implemented field types are INT64, DOUBLE, STRING, BOOL, and BYTES; NULL, OBJECT, and ARRAY appear in the enum but are not implemented and return ZDB_ERR_UNSUPPORTED.

Typedef Documentation

◆ zdb_doc_t

typedef struct zdb_doc zdb_doc_t

In-memory document handle.

Obtained from zdb_doc_create()/zdb_doc_open(); treat the members as read-only and mutate fields through the zdb_doc_field_set_* / zdb_doc_field_get_* accessors.

Enumeration Type Documentation

◆ zdb_doc_field_type_t

Document field types.

Documents carry scalar, string, and raw-byte fields. Values marked reserved are held for future use; storing one reports ZDB_ERR_UNSUPPORTED. To keep structured data in a field today, serialize it into ZDB_DOC_FIELD_BYTES.

Enumerator
ZDB_DOC_FIELD_NULL 

Reserved.

ZDB_DOC_FIELD_INT64 

int64_t value.

ZDB_DOC_FIELD_DOUBLE 

double value.

ZDB_DOC_FIELD_STRING 

NUL-terminated string value.

ZDB_DOC_FIELD_BOOL 

bool value.

ZDB_DOC_FIELD_BYTES 

Raw byte span value.

ZDB_DOC_FIELD_OBJECT 

Reserved.

ZDB_DOC_FIELD_ARRAY 

Reserved.

Function Documentation

◆ zdb_doc_close()

zdb_status_t zdb_doc_close ( zdb_doc_t * doc)

Close a document handle and release its in-memory fields.

Unsaved field changes are discarded.

Parameters
docOpen document handle.
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALdoc is NULL or not open.

◆ zdb_doc_create()

zdb_status_t zdb_doc_create ( zdb_t * db,
const char * collection_name,
const char * document_id,
zdb_doc_t * out_doc )

Create a new, empty document.

The document exists on storage only after zdb_doc_save().

Parameters
dbInitialized instance with DOC enabled.
collection_nameCollection name, at most CONFIG_ZDB_MAX_KEY_LEN bytes.
document_idDocument ID, at most CONFIG_ZDB_MAX_KEY_LEN bytes.
out_docHandle to fill; close with zdb_doc_close().
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALNULL argument, over-long name, or name containing path separators.
ZDB_ERR_NOMEMDocument slots exhausted.

◆ zdb_doc_delete()

zdb_status_t zdb_doc_delete ( zdb_t * db,
const char * collection_name,
const char * document_id )

Delete a document from storage.

Parameters
dbInitialized instance with DOC enabled.
collection_nameCollection name.
document_idDocument ID.
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALNULL argument, over-long name, or path separators.
ZDB_ERR_NOT_FOUNDNo such document.
ZDB_ERR_IOFilesystem delete failure.

◆ zdb_doc_export_flatbuffer()

zdb_status_t zdb_doc_export_flatbuffer ( zdb_doc_t * doc,
uint8_t * out_buf,
size_t out_capacity,
size_t * out_len )

Serialize a document as a FlatBuffer for transport.

Documents are stored in ZephyrDB's own binary format; this produces a FlatBuffer copy to send off-device, leaving stored data untouched. The buffer carries the collection name, document ID, timestamps, and every field with its name, type, and value. The layout is documented as a schema in the implementation, so a host can generate a reader for it.

Passing NULL for out_buf reports the size the buffer needs in out_len without producing one.

Requires a build with CONFIG_ZDB_FLATBUFFERS and CONFIG_FLATCC, which bring in the flatcc runtime.

Parameters
docOpen document handle.
out_bufDestination buffer, or NULL to query the size.
out_capacityCapacity of out_buf in bytes.
out_lenSet to the serialized size.
Return values
ZDB_OKSuccess, or size reported for a NULL buffer.
ZDB_ERR_INVALNULL argument or a closed document.
ZDB_ERR_NOMEMout_capacity is smaller than out_len, or the builder ran out of memory.
ZDB_ERR_UNSUPPORTEDBuilt without FlatBuffers support, or a field uses a reserved type.
ZDB_ERR_IOSerialization failure.

◆ zdb_doc_field_get_bool()

zdb_status_t zdb_doc_field_get_bool ( const zdb_doc_t * doc,
const char * field_name,
bool * out_value )

Read a BOOL field.

Parameters
docOpen document handle.
field_nameField name.
out_valueSet to the field's value.
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALNULL argument or the field has a different type.
ZDB_ERR_NOT_FOUNDNo field with this name.

◆ zdb_doc_field_get_bytes()

zdb_status_t zdb_doc_field_get_bytes ( const zdb_doc_t * doc,
const char * field_name,
zdb_bytes_t * out_value )

Read a BYTES field.

Parameters
docOpen document handle.
field_nameField name.
out_valueSet to the stored bytes; valid until the field is modified or the document is closed.
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALNULL argument or the field has a different type.
ZDB_ERR_NOT_FOUNDNo field with this name.

◆ zdb_doc_field_get_f64()

zdb_status_t zdb_doc_field_get_f64 ( const zdb_doc_t * doc,
const char * field_name,
double * out_value )

Read a DOUBLE field.

Parameters
docOpen document handle.
field_nameField name.
out_valueSet to the field's value.
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALNULL argument or the field has a different type.
ZDB_ERR_NOT_FOUNDNo field with this name.

◆ zdb_doc_field_get_i64()

zdb_status_t zdb_doc_field_get_i64 ( const zdb_doc_t * doc,
const char * field_name,
int64_t * out_value )

Read an INT64 field.

Parameters
docOpen document handle.
field_nameField name.
out_valueSet to the field's value.
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALNULL argument or the field has a different type.
ZDB_ERR_NOT_FOUNDNo field with this name.

◆ zdb_doc_field_get_string()

zdb_status_t zdb_doc_field_get_string ( const zdb_doc_t * doc,
const char * field_name,
const char ** out_value )

Read a STRING field.

Parameters
docOpen document handle.
field_nameField name.
out_valueSet to the stored string; valid until the field is modified or the document is closed.
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALNULL argument or the field has a different type.
ZDB_ERR_NOT_FOUNDNo field with this name.

◆ zdb_doc_field_set_bool()

zdb_status_t zdb_doc_field_set_bool ( zdb_doc_t * doc,
const char * field_name,
bool value )

Set (create or replace) a BOOL field.

Parameters
docOpen document handle.
field_nameField name.
valueValue to store.
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALNULL argument.
ZDB_ERR_NOMEMField capacity exhausted.

◆ zdb_doc_field_set_bytes()

zdb_status_t zdb_doc_field_set_bytes ( zdb_doc_t * doc,
const char * field_name,
const void * value,
size_t len )

Set (create or replace) a BYTES field.

The bytes are copied into the document's field storage.

Parameters
docOpen document handle.
field_nameField name.
valueBytes to store.
lenNumber of bytes at value.
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALNULL argument.
ZDB_ERR_NOMEMField capacity exhausted.

◆ zdb_doc_field_set_f64()

zdb_status_t zdb_doc_field_set_f64 ( zdb_doc_t * doc,
const char * field_name,
double value )

Set (create or replace) a DOUBLE field.

Parameters
docOpen document handle.
field_nameField name.
valueValue to store.
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALNULL argument.
ZDB_ERR_NOMEMField capacity exhausted.

◆ zdb_doc_field_set_i64()

zdb_status_t zdb_doc_field_set_i64 ( zdb_doc_t * doc,
const char * field_name,
int64_t value )

Set (create or replace) an INT64 field.

Parameters
docOpen document handle.
field_nameField name.
valueValue to store.
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALNULL argument.
ZDB_ERR_NOMEMField capacity exhausted.

◆ zdb_doc_field_set_string()

zdb_status_t zdb_doc_field_set_string ( zdb_doc_t * doc,
const char * field_name,
const char * value )

Set (create or replace) a STRING field.

The string is copied into the document's field storage.

Parameters
docOpen document handle.
field_nameField name.
valueNUL-terminated string to store.
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALNULL argument.
ZDB_ERR_NOMEMField capacity exhausted.

◆ zdb_doc_metadata_free()

zdb_status_t zdb_doc_metadata_free ( zdb_doc_metadata_t * metadata,
size_t count )

Release strings owned by zdb_doc_query() results.

Parameters
metadataResult array previously filled by zdb_doc_query().
countNumber of populated entries.
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALmetadata is NULL with a non-zero count.

◆ zdb_doc_open()

zdb_status_t zdb_doc_open ( zdb_t * db,
const char * collection_name,
const char * document_id,
zdb_doc_t * out_doc )

Load an existing document from storage.

The stored CRC covers the header and every field payload, so a flipped payload byte or a truncated file is reported rather than returned as field values. Documents written by older versions (header-only CRC) are still accepted.

If the document file is missing but zdb_doc_save() left a complete staging file behind, that file is promoted and opened.

Parameters
dbInitialized instance with DOC enabled.
collection_nameCollection name.
document_idDocument ID.
out_docHandle to fill; close with zdb_doc_close().
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALNULL argument, over-long name, or path separators.
ZDB_ERR_NOT_FOUNDNo such document.
ZDB_ERR_CORRUPTStored document failed its CRC or is truncated.
ZDB_ERR_NOMEMStored document has more fields than the handle holds.
ZDB_ERR_UNSUPPORTEDStored document uses an unimplemented field type.
ZDB_ERR_IOFilesystem read failure.

◆ zdb_doc_query()

zdb_status_t zdb_doc_query ( zdb_t * db,
const zdb_doc_query_t * query,
zdb_doc_metadata_t * out_metadata,
size_t * out_count )

Query document metadata across all collections.

Filters are AND-combined equality matches; the time window (zdb_doc_query_t::from_ms / to_ms) filters on updated_ms, which is milliseconds since boot, not wall-clock time. There is no collection filter — the scan covers every collection.

Parameters
dbInitialized instance with DOC enabled.
queryQuery description.
out_metadataResult array, or NULL to only count matches. Populated entries must be released with zdb_doc_metadata_free().
out_countIn: capacity of out_metadata. Out: number of matches reported (total match count when out_metadata is NULL).
Return values
ZDB_OKSuccess (zero matches is still ZDB_OK).
ZDB_ERR_INVALNULL db, query, or out_count.
ZDB_ERR_IOFilesystem scan failure.

◆ zdb_doc_save()

zdb_status_t zdb_doc_save ( zdb_doc_t * doc)

Persist a document's current fields.

Atomic: the document is serialized into a staging file and renamed over the stored one, so an interrupted save leaves the previously stored document intact. The CRC written covers the header and every field payload. Updates zdb_doc_t::updated_ms.

Parameters
docOpen document handle.
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALdoc is NULL or not open.
ZDB_ERR_UNSUPPORTEDA field uses an unimplemented type.
ZDB_ERR_IOFilesystem write or rename failure.