|
ZephyrDB
Embedded database for Zephyr RTOS: KV, time-series, and document models
|
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... | |
| struct | zdb_doc_metadata_t |
| 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. | |
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.
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.
| enum 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.
| 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.
| doc | Open document handle. |
| ZDB_OK | Success. |
| ZDB_ERR_INVAL | doc is NULL or not open. |
| 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().
| db | Initialized instance with DOC enabled. |
| collection_name | Collection name, at most CONFIG_ZDB_MAX_KEY_LEN bytes. |
| document_id | Document ID, at most CONFIG_ZDB_MAX_KEY_LEN bytes. |
| out_doc | Handle to fill; close with zdb_doc_close(). |
| ZDB_OK | Success. |
| ZDB_ERR_INVAL | NULL argument, over-long name, or name containing path separators. |
| ZDB_ERR_NOMEM | Document slots exhausted. |
| zdb_status_t zdb_doc_delete | ( | zdb_t * | db, |
| const char * | collection_name, | ||
| const char * | document_id ) |
Delete a document from storage.
| db | Initialized instance with DOC enabled. |
| collection_name | Collection name. |
| document_id | Document ID. |
| ZDB_OK | Success. |
| ZDB_ERR_INVAL | NULL argument, over-long name, or path separators. |
| ZDB_ERR_NOT_FOUND | No such document. |
| ZDB_ERR_IO | Filesystem delete failure. |
| 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.
| doc | Open document handle. |
| out_buf | Destination buffer, or NULL to query the size. |
| out_capacity | Capacity of out_buf in bytes. |
| out_len | Set to the serialized size. |
| ZDB_OK | Success, or size reported for a NULL buffer. |
| ZDB_ERR_INVAL | NULL argument or a closed document. |
| ZDB_ERR_NOMEM | out_capacity is smaller than out_len, or the builder ran out of memory. |
| ZDB_ERR_UNSUPPORTED | Built without FlatBuffers support, or a field uses a reserved type. |
| ZDB_ERR_IO | Serialization failure. |
| zdb_status_t zdb_doc_field_get_bool | ( | const zdb_doc_t * | doc, |
| const char * | field_name, | ||
| bool * | out_value ) |
Read a BOOL field.
| doc | Open document handle. |
| field_name | Field name. |
| out_value | Set to the field's value. |
| ZDB_OK | Success. |
| ZDB_ERR_INVAL | NULL argument or the field has a different type. |
| ZDB_ERR_NOT_FOUND | No field with this name. |
| 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.
| doc | Open document handle. |
| field_name | Field name. |
| out_value | Set to the stored bytes; valid until the field is modified or the document is closed. |
| ZDB_OK | Success. |
| ZDB_ERR_INVAL | NULL argument or the field has a different type. |
| ZDB_ERR_NOT_FOUND | No field with this name. |
| zdb_status_t zdb_doc_field_get_f64 | ( | const zdb_doc_t * | doc, |
| const char * | field_name, | ||
| double * | out_value ) |
Read a DOUBLE field.
| doc | Open document handle. |
| field_name | Field name. |
| out_value | Set to the field's value. |
| ZDB_OK | Success. |
| ZDB_ERR_INVAL | NULL argument or the field has a different type. |
| ZDB_ERR_NOT_FOUND | No field with this name. |
| 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.
| doc | Open document handle. |
| field_name | Field name. |
| out_value | Set to the field's value. |
| ZDB_OK | Success. |
| ZDB_ERR_INVAL | NULL argument or the field has a different type. |
| ZDB_ERR_NOT_FOUND | No field with this name. |
| 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.
| doc | Open document handle. |
| field_name | Field name. |
| out_value | Set to the stored string; valid until the field is modified or the document is closed. |
| ZDB_OK | Success. |
| ZDB_ERR_INVAL | NULL argument or the field has a different type. |
| ZDB_ERR_NOT_FOUND | No field with this name. |
| 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.
| doc | Open document handle. |
| field_name | Field name. |
| value | Value to store. |
| ZDB_OK | Success. |
| ZDB_ERR_INVAL | NULL argument. |
| ZDB_ERR_NOMEM | Field capacity exhausted. |
| 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.
| doc | Open document handle. |
| field_name | Field name. |
| value | Bytes to store. |
| len | Number of bytes at value. |
| ZDB_OK | Success. |
| ZDB_ERR_INVAL | NULL argument. |
| ZDB_ERR_NOMEM | Field capacity exhausted. |
| 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.
| doc | Open document handle. |
| field_name | Field name. |
| value | Value to store. |
| ZDB_OK | Success. |
| ZDB_ERR_INVAL | NULL argument. |
| ZDB_ERR_NOMEM | Field capacity exhausted. |
| 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.
| doc | Open document handle. |
| field_name | Field name. |
| value | Value to store. |
| ZDB_OK | Success. |
| ZDB_ERR_INVAL | NULL argument. |
| ZDB_ERR_NOMEM | Field capacity exhausted. |
| 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.
| doc | Open document handle. |
| field_name | Field name. |
| value | NUL-terminated string to store. |
| ZDB_OK | Success. |
| ZDB_ERR_INVAL | NULL argument. |
| ZDB_ERR_NOMEM | Field capacity exhausted. |
| zdb_status_t zdb_doc_metadata_free | ( | zdb_doc_metadata_t * | metadata, |
| size_t | count ) |
Release strings owned by zdb_doc_query() results.
| metadata | Result array previously filled by zdb_doc_query(). |
| count | Number of populated entries. |
| ZDB_OK | Success. |
| ZDB_ERR_INVAL | metadata is NULL with a non-zero count. |
| 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.
| db | Initialized instance with DOC enabled. |
| collection_name | Collection name. |
| document_id | Document ID. |
| out_doc | Handle to fill; close with zdb_doc_close(). |
| ZDB_OK | Success. |
| ZDB_ERR_INVAL | NULL argument, over-long name, or path separators. |
| ZDB_ERR_NOT_FOUND | No such document. |
| ZDB_ERR_CORRUPT | Stored document failed its CRC or is truncated. |
| ZDB_ERR_NOMEM | Stored document has more fields than the handle holds. |
| ZDB_ERR_UNSUPPORTED | Stored document uses an unimplemented field type. |
| ZDB_ERR_IO | Filesystem read failure. |
| 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.
| db | Initialized instance with DOC enabled. |
| query | Query description. |
| out_metadata | Result array, or NULL to only count matches. Populated entries must be released with zdb_doc_metadata_free(). |
| out_count | In: capacity of out_metadata. Out: number of matches reported (total match count when out_metadata is NULL). |
| ZDB_OK | Success (zero matches is still ZDB_OK). |
| ZDB_ERR_INVAL | NULL db, query, or out_count. |
| ZDB_ERR_IO | Filesystem scan failure. |
| 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.
| doc | Open document handle. |
| ZDB_OK | Success. |
| ZDB_ERR_INVAL | doc is NULL or not open. |
| ZDB_ERR_UNSUPPORTED | A field uses an unimplemented type. |
| ZDB_ERR_IO | Filesystem write or rename failure. |