ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
docstore.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file docstore.h
6 * @brief Local JSON document store on the WAL (PROTOCORE_ENABLE_DOCSTORE, requires DBM + WAL).
7 *
8 * A small NoSQL document store: documents are JSON objects addressed by an id, kept durably on the
9 * write-ahead log. It is a thin layer over dbm (dbm.h) - the id is the key, the JSON body is the value -
10 * so it inherits dbm's zero-heap index, WAL persistence, and crash recovery for free. What it adds on top
11 * is the document capability: **field queries** - scan the live documents and match those whose top-level
12 * JSON field equals a value (like a small `find({field: value})`), using the zero-heap JSON reader
13 * (json.h). Values are bounded by ::PROTOCORE_DBM_VAL_MAX, ids by ::PROTOCORE_DBM_KEY_MAX.
14 *
15 * Writes are batched; call ::protocore_docstore_sync to checkpoint the WAL and make them durable. Drive it from
16 * one context (a worker / loop), not concurrently, and do not put/delete from inside a find callback.
17 */
18
19#ifndef PROTOCORE_DOCSTORE_H
20#define PROTOCORE_DOCSTORE_H
21
22#include "protocore_config.h" // the entry point: protocore_types.h for the widths
23
24#if PROTOCORE_ENABLE_DOCSTORE
25
27
29
30/** @brief A document store bound to a mounted ::protocore_dbm. */
31typedef struct
32{
33 struct protocore_dbm *db;
34} protocore_doc_store;
35
36/** @brief Bind @p ds to an open @p db. */
37void protocore_docstore_open(protocore_doc_store *ds, struct protocore_dbm *db);
38
39/**
40 * @brief Insert or replace the document @p id with JSON body @p json. Not synced (batched).
41 * @return false on the same bounds/full conditions as ::protocore_dbm_put.
42 */
43proto_bool protocore_docstore_put(protocore_doc_store *ds, const char *id, uint16_t id_len, const uint8_t *json,
44 uint32_t json_len);
45
46/**
47 * @brief Fetch document @p id's JSON body into @p buf (up to @p cap).
48 * @return the body length, or -1 if absent or larger than @p cap.
49 */
50long protocore_docstore_get(protocore_doc_store *ds, const char *id, uint16_t id_len, uint8_t *buf, size_t cap);
51
52/** @brief Delete document @p id. @return true if it existed. */
53proto_bool protocore_docstore_del(protocore_doc_store *ds, const char *id, uint16_t id_len);
54
55/** @brief @return true if document @p id exists. */
56proto_bool protocore_docstore_contains(const protocore_doc_store *ds, const char *id, uint16_t id_len);
57
58/** @brief @return the number of documents. */
59uint32_t protocore_docstore_count(const protocore_doc_store *ds);
60
61/** @brief Make all writes durable (checkpoints the WAL). @return false on I/O failure. */
62proto_bool protocore_docstore_sync(protocore_doc_store *ds);
63
64/**
65 * @brief Per-match callback for the find calls: the matching document's id and JSON body (the body points
66 * into a temporary buffer valid only for this call). Return false to stop the scan early.
67 */
68typedef proto_bool (*protocore_doc_match_cb)(const char *id, uint16_t id_len, const uint8_t *json, uint32_t json_len,
69 void *ctx);
70
71/**
72 * @brief Find documents whose top-level string field @p field equals @p value. @return the match count.
73 * Field string values longer than ::PROTOCORE_DOCSTORE_FIELD_MAX will not match.
74 */
75uint32_t protocore_docstore_find_str(protocore_doc_store *ds, const char *field, const char *value,
76 protocore_doc_match_cb cb, void *ctx);
77
78/** @brief Find documents whose top-level integer field @p field equals @p value. @return the match count. */
79uint32_t protocore_docstore_find_int(protocore_doc_store *ds, const char *field, long value, protocore_doc_match_cb cb,
80 void *ctx);
81
82/** @brief Find documents whose top-level boolean field @p field equals @p value. @return the match count. */
83uint32_t protocore_docstore_find_bool(protocore_doc_store *ds, const char *field, proto_bool value,
84 protocore_doc_match_cb cb, void *ctx);
85
87
88#endif // PROTOCORE_ENABLE_DOCSTORE
89
90#endif // PROTOCORE_DOCSTORE_H
Log-structured hash key-value store on the WAL (PROTOCORE_ENABLE_DBM, requires PROTOCORE_ENABLE_WAL).
#define PROTOCORE_BEGIN_DECLS
Give a header's declarations C linkage, so their symbol names carry no parameter types.
Definition types.h:96
_Bool proto_bool
The truth value.
Definition types.h:64
#define PROTOCORE_END_DECLS
Definition types.h:97