|
ZephyrDB
Embedded database for Zephyr RTOS: KV, time-series, and document models
|
Append-oriented int64 sample streams on LittleFS or FCB (CONFIG_ZDB_TS).
More...
Classes | |
| struct | zdb_ts_t |
| Open handle to one time-series stream (see zdb_ts_open()). More... | |
| struct | zdb_ts_sample_i64_t |
| One int64 sample: timestamp + value. More... | |
| struct | zdb_ts_window_t |
| Inclusive timestamp window for queries and cursors. More... | |
| struct | zdb_ts_agg_result_t |
| Result of zdb_ts_query_aggregate(). More... | |
Macros | |
| #define | ZDB_TS_WINDOW_ALL ((zdb_ts_window_t){.from_ts_ms = 0, .to_ts_ms = UINT64_MAX}) |
| A zdb_ts_window_t covering all possible timestamps. | |
Enumerations | |
| enum | zdb_ts_agg_t { ZDB_TS_AGG_MIN = 0 , ZDB_TS_AGG_MAX , ZDB_TS_AGG_AVG , ZDB_TS_AGG_SUM , ZDB_TS_AGG_COUNT } |
| Aggregation functions for zdb_ts_query_aggregate(). More... | |
Functions | |
| zdb_status_t | zdb_ts_open (zdb_t *db, const char *stream_name, zdb_ts_t *ts) |
| Open a stream for appending and reading. | |
| zdb_status_t | zdb_ts_close (zdb_ts_t *ts) |
| Close a stream handle and release its slot. | |
| zdb_status_t | zdb_ts_append_i64 (zdb_ts_t *ts, const zdb_ts_sample_i64_t *sample) |
| Append one sample to the stream's ingest buffer. | |
| zdb_status_t | zdb_ts_append_batch_i64 (zdb_ts_t *ts, const zdb_ts_sample_i64_t *samples, size_t sample_count) |
| Append a batch of samples. | |
| zdb_status_t | zdb_ts_flush_async (zdb_ts_t *ts) |
| Queue an asynchronous flush of the ingest buffer. | |
| zdb_status_t | zdb_ts_flush_sync (zdb_ts_t *ts, k_timeout_t timeout) |
| Flush the ingest buffer and wait for completion. | |
| zdb_status_t | zdb_ts_query_aggregate (zdb_ts_t *ts, zdb_ts_window_t window, zdb_ts_agg_t agg, zdb_ts_agg_result_t *out_result) |
| Compute an aggregate over samples in a time window. | |
| zdb_status_t | zdb_ts_recover_stream (zdb_ts_t *ts, size_t *out_truncated_bytes) |
| Scan the stream's storage and truncate a corrupt tail. | |
| zdb_status_t | zdb_ts_cursor_open (zdb_ts_t *ts, zdb_ts_window_t window, zdb_predicate_fn predicate, void *predicate_ctx, zdb_cursor_t *out_cursor) |
| Open a cursor over the stream's samples. | |
| zdb_status_t | zdb_ts_cursor_open_desc (zdb_ts_t *ts, zdb_ts_window_t window, zdb_predicate_fn predicate, void *predicate_ctx, zdb_cursor_t *out_cursor) |
| Open a cursor that walks the stream newest-first. | |
| zdb_status_t | zdb_ts_watermark_set (zdb_ts_t *ts, uint64_t consumed_ts_ms) |
| Record how far a consumer has processed a stream. | |
| zdb_status_t | zdb_ts_watermark_get (zdb_ts_t *ts, uint64_t *out_consumed_ts_ms) |
| Read a stream's consumed watermark. | |
| zdb_status_t | zdb_ts_watermark_clear (zdb_ts_t *ts) |
| Forget a stream's consumed watermark. | |
| zdb_status_t | zdb_cursor_next (zdb_cursor_t *cursor, zdb_bytes_t *out_record) |
| Fetch the next record from a cursor. | |
| zdb_status_t | zdb_ts_cursor_next_sample (zdb_cursor_t *cursor, zdb_ts_sample_i64_t *out_sample) |
| Fetch the next sample from a time-series cursor. | |
| zdb_status_t | zdb_ts_sample_i64_export_flatbuffer (const zdb_ts_sample_i64_t *sample, uint8_t *out_buf, size_t out_capacity, size_t *out_len) |
Serialize one sample as a FlatBuffer (CONFIG_ZDB_FLATBUFFERS + CONFIG_FLATCC). | |
Append-oriented int64 sample streams on LittleFS or FCB (CONFIG_ZDB_TS).
One instance handles one active stream at a time: opening a second, different stream returns ZDB_ERR_BUSY until zdb_deinit(); re-opening the active stream is allowed. Appends land in a RAM ingest buffer and reach storage on flush (with the FCB backend appends are written through synchronously and flushes are no-ops).
| enum zdb_ts_agg_t |
Aggregation functions for zdb_ts_query_aggregate().
| zdb_status_t zdb_cursor_next | ( | zdb_cursor_t * | cursor, |
| zdb_bytes_t * | out_record ) |
Fetch the next record from a cursor.
Part of the core cursor framework; declared with the TS module because TS is currently the only producer of cursors.
| cursor | Open cursor. |
| out_record | Set to the next record's bytes; the data stays valid until the next cursor call. |
| ZDB_OK | A record was produced. |
| ZDB_ERR_NOT_FOUND | Scan is exhausted. |
| ZDB_ERR_INVAL | NULL argument or cursor not open. |
| ZDB_ERR_IO | Backend read failure. |
| zdb_status_t zdb_ts_append_batch_i64 | ( | zdb_ts_t * | ts, |
| const zdb_ts_sample_i64_t * | samples, | ||
| size_t | sample_count ) |
Append a batch of samples.
| ts | Open stream handle. |
| samples | Sample array. |
| sample_count | Number of samples in samples. |
| ZDB_OK | All samples appended. |
| ZDB_ERR_INVAL | NULL argument or zero count. |
| ZDB_ERR_NOMEM | Ran out of ingest space part-way. |
| ZDB_ERR_IO | Backend write failure (write-through backends). |
| zdb_status_t zdb_ts_append_i64 | ( | zdb_ts_t * | ts, |
| const zdb_ts_sample_i64_t * | sample ) |
Append one sample to the stream's ingest buffer.
| ts | Open stream handle. |
| sample | Sample to append. |
| ZDB_OK | Success. |
| ZDB_ERR_INVAL | NULL argument. |
| ZDB_ERR_NOMEM | Ingest buffer full and flush could not make room. |
| ZDB_ERR_IO | Backend write failure (write-through backends). |
| zdb_status_t zdb_ts_close | ( | zdb_ts_t * | ts | ) |
Close a stream handle and release its slot.
Samples still buffered for the stream are written out first, so closing never discards data. Once the last handle on a stream is closed, its slot and ingest buffer become available to another stream.
| ts | Open stream handle. |
| ZDB_OK | Success. |
| ZDB_ERR_INVAL | ts is NULL or not open. |
| ZDB_ERR_IO | Buffered samples could not be written. |
| zdb_status_t zdb_ts_cursor_next_sample | ( | zdb_cursor_t * | cursor, |
| zdb_ts_sample_i64_t * | out_sample ) |
Fetch the next sample from a time-series cursor.
Advances the cursor and reports the decoded sample. On-disk record layout depends on the build (CONFIG_ZDB_TS_DELTA_ENCODING) and part of the timestamp lives in the segment header, so the bytes handed back by zdb_cursor_next() cannot be decoded portably by a caller. Use this instead when walking a stream opened with zdb_ts_cursor_open() or zdb_ts_cursor_open_desc().
| cursor | Open time-series cursor. |
| out_sample | Set to the next sample. |
| ZDB_OK | A sample was produced. |
| ZDB_ERR_NOT_FOUND | Scan is exhausted. |
| ZDB_ERR_INVAL | NULL argument or cursor not open. |
| ZDB_ERR_UNSUPPORTED | Cursor does not belong to the TS model. |
| ZDB_ERR_IO | Backend read failure. |
| zdb_status_t zdb_ts_cursor_open | ( | zdb_ts_t * | ts, |
| zdb_ts_window_t | window, | ||
| zdb_predicate_fn | predicate, | ||
| void * | predicate_ctx, | ||
| zdb_cursor_t * | out_cursor ) |
Open a cursor over the stream's samples.
The cursor iterates flushed records from storage plus samples still in the RAM ingest buffer, filtered by window and the optional predicate.
| ts | Open stream handle. |
| window | Inclusive timestamp window. |
| predicate | Optional filter; NULL yields every sample. |
| predicate_ctx | Opaque pointer passed to predicate. |
| out_cursor | Cursor to initialize (close with zdb_cursor_close()). |
| ZDB_OK | Success. |
| ZDB_ERR_INVAL | NULL argument. |
| ZDB_ERR_NOMEM | Cursor slab exhausted. |
| zdb_status_t zdb_ts_cursor_open_desc | ( | zdb_ts_t * | ts, |
| zdb_ts_window_t | window, | ||
| zdb_predicate_fn | predicate, | ||
| void * | predicate_ctx, | ||
| zdb_cursor_t * | out_cursor ) |
Open a cursor that walks the stream newest-first.
The reverse of zdb_ts_cursor_open(): unflushed samples come first, newest to oldest, then stored records from the end of the stream backwards. Window and predicate filtering are unchanged.
Order follows storage, which matches timestamp order for the usual case of samples appended as they are produced. A stream appended out of order is returned in reverse append order, not sorted.
Records appended after the cursor is opened are not visible to it.
| ts | Open stream handle. |
| window | Inclusive timestamp window (ZDB_TS_WINDOW_ALL for everything). |
| predicate | Optional filter, or NULL. |
| predicate_ctx | Context passed to predicate. |
| out_cursor | Cursor to initialize (close with zdb_cursor_close()). |
| ZDB_OK | Success. |
| ZDB_ERR_INVAL | NULL argument. |
| ZDB_ERR_NOMEM | No cursor slab block was free. |
| ZDB_ERR_UNSUPPORTED | Backend cannot walk backwards (FCB). |
| ZDB_ERR_IO | Backend read failure. |
| zdb_status_t zdb_ts_flush_async | ( | zdb_ts_t * | ts | ) |
Queue an asynchronous flush of the ingest buffer.
The flush runs on the configured work queue (zdb_cfg_t::work_q or the system work queue). A no-op returning ZDB_OK on write-through backends (FCB).
| ts | Open stream handle. |
| ZDB_OK | Flush queued (or nothing to flush). |
| ZDB_ERR_INVAL | ts is NULL or not open. |
| zdb_status_t zdb_ts_flush_sync | ( | zdb_ts_t * | ts, |
| k_timeout_t | timeout ) |
Flush the ingest buffer and wait for completion.
A no-op returning ZDB_OK on write-through backends (FCB).
| ts | Open stream handle. |
| timeout | How long to wait; K_NO_WAIT returns immediately. |
| ZDB_OK | Flush completed. |
| ZDB_ERR_INVAL | ts is NULL or not open. |
| ZDB_ERR_BUSY | K_NO_WAIT and the flush lock is held. |
| ZDB_ERR_TIMEOUT | Timed out waiting for the flush. |
| ZDB_ERR_IO | Backend write failure. |
| zdb_status_t zdb_ts_open | ( | zdb_t * | db, |
| const char * | stream_name, | ||
| zdb_ts_t * | ts ) |
Open a stream for appending and reading.
An instance holds up to CONFIG_ZDB_TS_MAX_STREAMS streams open at once, each with its own ingest buffer, so appends to different streams stay independent. Opening one more than that reports ZDB_ERR_BUSY; zdb_ts_close() frees a slot.
Opening a stream that is already open shares its slot, so both handles see the same buffered samples. The slot is released once every handle is closed.
| ts | Handle to fill. |
| db | Initialized instance. |
| stream_name | Stream name, at most CONFIG_ZDB_TS_STREAM_NAME_MAX_LEN bytes; kept by reference and must outlive ts. |
| ZDB_OK | Success. |
| ZDB_ERR_INVAL | NULL argument or over-long stream name. |
| ZDB_ERR_NOMEM | No core slab block was free. |
| ZDB_ERR_BUSY | Every stream slot is taken by another stream. |
| zdb_status_t zdb_ts_query_aggregate | ( | zdb_ts_t * | ts, |
| zdb_ts_window_t | window, | ||
| zdb_ts_agg_t | agg, | ||
| zdb_ts_agg_result_t * | out_result ) |
Compute an aggregate over samples in a time window.
Covers flushed records plus samples still in the ingest buffer.
ZDB_TS_AGG_MIN, ZDB_TS_AGG_MAX, ZDB_TS_AGG_AVG and ZDB_TS_AGG_SUM scan at most CONFIG_ZDB_TS_MAX_AGG_POINTS samples; if more matched, the result is computed from that prefix and zdb_ts_agg_result_t::truncated is set. They report ZDB_ERR_NOT_FOUND when nothing matched, since they have no value to return.
ZDB_TS_AGG_COUNT is neither capped nor truncated, and reports an empty window as ZDB_OK with points == 0. Counting every sample in the stream (an unbounded window) reads no payloads.
| ts | Open stream handle. |
| window | Inclusive timestamp window (ZDB_TS_WINDOW_ALL for everything). |
| agg | Aggregation function. |
| out_result | Result; points == 0 means no samples matched. |
| ZDB_OK | Success. |
| ZDB_ERR_INVAL | NULL argument or unknown agg. |
| ZDB_ERR_NOT_FOUND | No samples matched (value-bearing aggregates only). |
| ZDB_ERR_UNSUPPORTED | Backend cannot aggregate (FCB). |
| ZDB_ERR_IO | Backend read failure. |
| zdb_status_t zdb_ts_recover_stream | ( | zdb_ts_t * | ts, |
| size_t * | out_truncated_bytes ) |
Scan the stream's storage and truncate a corrupt tail.
Walks the on-disk records, drops anything failing CRC/framing checks, and updates the instance's zdb_ts_stats_t counters. A no-op reporting zero truncated bytes on write-through backends (FCB).
| ts | Open stream handle. |
| out_truncated_bytes | Set to the number of bytes truncated (may be NULL). |
| ZDB_OK | Recovery completed (even if bytes were dropped). |
| ZDB_ERR_INVAL | ts is NULL or not open. |
| ZDB_ERR_IO | Backend read/write failure during recovery. |
| zdb_status_t zdb_ts_sample_i64_export_flatbuffer | ( | const zdb_ts_sample_i64_t * | sample, |
| uint8_t * | out_buf, | ||
| size_t | out_capacity, | ||
| size_t * | out_len ) |
Serialize one sample as a FlatBuffer (CONFIG_ZDB_FLATBUFFERS + CONFIG_FLATCC).
| sample | Sample to serialize. |
| out_buf | Destination buffer. |
| out_capacity | Capacity of out_buf in bytes. |
| out_len | Set to the serialized size. |
| ZDB_OK | Success. |
| ZDB_ERR_INVAL | NULL argument. |
| ZDB_ERR_NOMEM | out_buf is too small (required size in out_len). |
| ZDB_ERR_UNSUPPORTED | Built without FlatCC support. |
| zdb_status_t zdb_ts_watermark_clear | ( | zdb_ts_t * | ts | ) |
Forget a stream's consumed watermark.
The next zdb_ts_watermark_get() reports ZDB_ERR_NOT_FOUND, so a consumer starts from the beginning again. Succeeds when no watermark was stored.
| ts | Open stream handle. |
| ZDB_OK | Watermark cleared or already absent. |
| ZDB_ERR_INVAL | NULL argument. |
| ZDB_ERR_UNSUPPORTED | Backend keeps no sidecar state (FCB). |
| ZDB_ERR_IO | Filesystem delete failure. |
| zdb_status_t zdb_ts_watermark_get | ( | zdb_ts_t * | ts, |
| uint64_t * | out_consumed_ts_ms ) |
Read a stream's consumed watermark.
Reports ZDB_ERR_NOT_FOUND when no watermark has been stored, and also when the stored one fails its integrity check — a consumer then re-processes from the beginning rather than skipping samples it never handled.
| ts | Open stream handle. |
| out_consumed_ts_ms | Set to the stored timestamp. |
| ZDB_OK | Watermark read. |
| ZDB_ERR_INVAL | NULL argument. |
| ZDB_ERR_NOT_FOUND | No usable watermark stored. |
| ZDB_ERR_UNSUPPORTED | Backend keeps no sidecar state (FCB). |
| ZDB_ERR_IO | Filesystem read failure. |
| zdb_status_t zdb_ts_watermark_set | ( | zdb_ts_t * | ts, |
| uint64_t | consumed_ts_ms ) |
Record how far a consumer has processed a stream.
Stores a per-stream timestamp that survives restarts, so an uploader or forwarder can resume where it stopped instead of replaying the stream. Typical use is to acknowledge a batch after it is safely delivered, then open the next cursor at watermark + 1:
The value is stored, not interpreted: it may move backwards to force re-processing, and it is independent of which samples the stream still holds.
| ts | Open stream handle. |
| consumed_ts_ms | Timestamp processed up to, inclusive. |
| ZDB_OK | Watermark stored. |
| ZDB_ERR_INVAL | NULL argument. |
| ZDB_ERR_NOMEM | No core slab block was free. |
| ZDB_ERR_UNSUPPORTED | Backend keeps no sidecar state (FCB). |
| ZDB_ERR_IO | Filesystem write failure. |