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

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).

Detailed Description

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).

Enumeration Type Documentation

◆ zdb_ts_agg_t

Aggregation functions for zdb_ts_query_aggregate().

Enumerator
ZDB_TS_AGG_MIN 

Minimum sample value.

ZDB_TS_AGG_MAX 

Maximum sample value.

ZDB_TS_AGG_AVG 

Arithmetic mean of sample values.

ZDB_TS_AGG_SUM 

Sum of sample values.

ZDB_TS_AGG_COUNT 

Number of samples in the window.

Function Documentation

◆ zdb_cursor_next()

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.

Parameters
cursorOpen cursor.
out_recordSet to the next record's bytes; the data stays valid until the next cursor call.
Return values
ZDB_OKA record was produced.
ZDB_ERR_NOT_FOUNDScan is exhausted.
ZDB_ERR_INVALNULL argument or cursor not open.
ZDB_ERR_IOBackend read failure.

◆ zdb_ts_append_batch_i64()

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.

Parameters
tsOpen stream handle.
samplesSample array.
sample_countNumber of samples in samples.
Return values
ZDB_OKAll samples appended.
ZDB_ERR_INVALNULL argument or zero count.
ZDB_ERR_NOMEMRan out of ingest space part-way.
ZDB_ERR_IOBackend write failure (write-through backends).

◆ zdb_ts_append_i64()

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.

Parameters
tsOpen stream handle.
sampleSample to append.
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALNULL argument.
ZDB_ERR_NOMEMIngest buffer full and flush could not make room.
ZDB_ERR_IOBackend write failure (write-through backends).

◆ zdb_ts_close()

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.

Parameters
tsOpen stream handle.
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALts is NULL or not open.
ZDB_ERR_IOBuffered samples could not be written.

◆ zdb_ts_cursor_next_sample()

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().

Parameters
cursorOpen time-series cursor.
out_sampleSet to the next sample.
Return values
ZDB_OKA sample was produced.
ZDB_ERR_NOT_FOUNDScan is exhausted.
ZDB_ERR_INVALNULL argument or cursor not open.
ZDB_ERR_UNSUPPORTEDCursor does not belong to the TS model.
ZDB_ERR_IOBackend read failure.

◆ zdb_ts_cursor_open()

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.

Parameters
tsOpen stream handle.
windowInclusive timestamp window.
predicateOptional filter; NULL yields every sample.
predicate_ctxOpaque pointer passed to predicate.
out_cursorCursor to initialize (close with zdb_cursor_close()).
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALNULL argument.
ZDB_ERR_NOMEMCursor slab exhausted.

◆ zdb_ts_cursor_open_desc()

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.

Parameters
tsOpen stream handle.
windowInclusive timestamp window (ZDB_TS_WINDOW_ALL for everything).
predicateOptional filter, or NULL.
predicate_ctxContext passed to predicate.
out_cursorCursor to initialize (close with zdb_cursor_close()).
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALNULL argument.
ZDB_ERR_NOMEMNo cursor slab block was free.
ZDB_ERR_UNSUPPORTEDBackend cannot walk backwards (FCB).
ZDB_ERR_IOBackend read failure.

◆ zdb_ts_flush_async()

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).

Parameters
tsOpen stream handle.
Return values
ZDB_OKFlush queued (or nothing to flush).
ZDB_ERR_INVALts is NULL or not open.

◆ zdb_ts_flush_sync()

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).

Parameters
tsOpen stream handle.
timeoutHow long to wait; K_NO_WAIT returns immediately.
Return values
ZDB_OKFlush completed.
ZDB_ERR_INVALts is NULL or not open.
ZDB_ERR_BUSYK_NO_WAIT and the flush lock is held.
ZDB_ERR_TIMEOUTTimed out waiting for the flush.
ZDB_ERR_IOBackend write failure.

◆ zdb_ts_open()

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.

Parameters
tsHandle to fill.
dbInitialized instance.
stream_nameStream name, at most CONFIG_ZDB_TS_STREAM_NAME_MAX_LEN bytes; kept by reference and must outlive ts.
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALNULL argument or over-long stream name.
ZDB_ERR_NOMEMNo core slab block was free.
ZDB_ERR_BUSYEvery stream slot is taken by another stream.

◆ zdb_ts_query_aggregate()

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.

Parameters
tsOpen stream handle.
windowInclusive timestamp window (ZDB_TS_WINDOW_ALL for everything).
aggAggregation function.
out_resultResult; points == 0 means no samples matched.
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALNULL argument or unknown agg.
ZDB_ERR_NOT_FOUNDNo samples matched (value-bearing aggregates only).
ZDB_ERR_UNSUPPORTEDBackend cannot aggregate (FCB).
ZDB_ERR_IOBackend read failure.

◆ zdb_ts_recover_stream()

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).

Parameters
tsOpen stream handle.
out_truncated_bytesSet to the number of bytes truncated (may be NULL).
Return values
ZDB_OKRecovery completed (even if bytes were dropped).
ZDB_ERR_INVALts is NULL or not open.
ZDB_ERR_IOBackend read/write failure during recovery.

◆ zdb_ts_sample_i64_export_flatbuffer()

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).

Parameters
sampleSample to serialize.
out_bufDestination buffer.
out_capacityCapacity of out_buf in bytes.
out_lenSet to the serialized size.
Return values
ZDB_OKSuccess.
ZDB_ERR_INVALNULL argument.
ZDB_ERR_NOMEMout_buf is too small (required size in out_len).
ZDB_ERR_UNSUPPORTEDBuilt without FlatCC support.

◆ zdb_ts_watermark_clear()

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.

Parameters
tsOpen stream handle.
Return values
ZDB_OKWatermark cleared or already absent.
ZDB_ERR_INVALNULL argument.
ZDB_ERR_UNSUPPORTEDBackend keeps no sidecar state (FCB).
ZDB_ERR_IOFilesystem delete failure.

◆ zdb_ts_watermark_get()

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.

Parameters
tsOpen stream handle.
out_consumed_ts_msSet to the stored timestamp.
Return values
ZDB_OKWatermark read.
ZDB_ERR_INVALNULL argument.
ZDB_ERR_NOT_FOUNDNo usable watermark stored.
ZDB_ERR_UNSUPPORTEDBackend keeps no sidecar state (FCB).
ZDB_ERR_IOFilesystem read failure.

◆ zdb_ts_watermark_set()

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:

uint64_t mark;
if (zdb_ts_watermark_get(&ts, &mark) == ZDB_OK) {
window.from_ts_ms = mark + 1U;
}
@ ZDB_OK
Definition zephyrdb.h:124
#define ZDB_TS_WINDOW_ALL
A zdb_ts_window_t covering all possible timestamps.
Definition zephyrdb.h:966
zdb_status_t zdb_ts_watermark_get(zdb_ts_t *ts, uint64_t *out_consumed_ts_ms)
Read a stream's consumed watermark.
Inclusive timestamp window for queries and cursors.
Definition zephyrdb.h:960
uint64_t from_ts_ms
Definition zephyrdb.h:961

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.

Parameters
tsOpen stream handle.
consumed_ts_msTimestamp processed up to, inclusive.
Return values
ZDB_OKWatermark stored.
ZDB_ERR_INVALNULL argument.
ZDB_ERR_NOMEMNo core slab block was free.
ZDB_ERR_UNSUPPORTEDBackend keeps no sidecar state (FCB).
ZDB_ERR_IOFilesystem write failure.