ProtoCore v0.0.1
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
docstore.h
Go to the documentation of this file.
1// 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 (PC_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 ::PC_DBM_VAL_MAX, ids by ::PC_DBM_KEY_MAX.
14 *
15 * Writes are batched; call ::pc_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"
23
24#if PC_ENABLE_DOCSTORE
25
27#include <stddef.h>
28#include <stdint.h>
29
30/** @brief A document store bound to a mounted ::pc_dbm. */
31struct pc_doc_store
32{
33 pc_dbm *db;
34};
35
36/** @brief Bind @p ds to an open @p db. */
37void pc_docstore_open(pc_doc_store *ds, pc_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 ::pc_dbm_put.
42 */
43bool pc_docstore_put(pc_doc_store *ds, const char *id, uint16_t id_len, const uint8_t *json, uint32_t json_len);
44
45/**
46 * @brief Fetch document @p id's JSON body into @p buf (up to @p cap).
47 * @return the body length, or -1 if absent or larger than @p cap.
48 */
49long pc_docstore_get(pc_doc_store *ds, const char *id, uint16_t id_len, uint8_t *buf, size_t cap);
50
51/** @brief Delete document @p id. @return true if it existed. */
52bool pc_docstore_del(pc_doc_store *ds, const char *id, uint16_t id_len);
53
54/** @brief @return true if document @p id exists. */
55bool pc_docstore_contains(const pc_doc_store *ds, const char *id, uint16_t id_len);
56
57/** @brief @return the number of documents. */
58uint32_t pc_docstore_count(const pc_doc_store *ds);
59
60/** @brief Make all writes durable (checkpoints the WAL). @return false on I/O failure. */
61bool pc_docstore_sync(pc_doc_store *ds);
62
63/**
64 * @brief Per-match callback for the find calls: the matching document's id and JSON body (the body points
65 * into a temporary buffer valid only for this call). Return false to stop the scan early.
66 */
67using pc_doc_match_cb = bool (*)(const char *id, uint16_t id_len, const uint8_t *json, uint32_t json_len, void *ctx);
68
69/**
70 * @brief Find documents whose top-level string field @p field equals @p value. @return the match count.
71 * Field string values longer than ::PC_DOCSTORE_FIELD_MAX will not match.
72 */
73uint32_t pc_docstore_find_str(pc_doc_store *ds, const char *field, const char *value, pc_doc_match_cb cb, void *ctx);
74
75/** @brief Find documents whose top-level integer field @p field equals @p value. @return the match count. */
76uint32_t pc_docstore_find_int(pc_doc_store *ds, const char *field, long value, pc_doc_match_cb cb, void *ctx);
77
78/** @brief Find documents whose top-level boolean field @p field equals @p value. @return the match count. */
79uint32_t pc_docstore_find_bool(pc_doc_store *ds, const char *field, bool value, pc_doc_match_cb cb, void *ctx);
80
81#endif // PC_ENABLE_DOCSTORE
82#endif // PROTOCORE_DOCSTORE_H
Log-structured hash key-value store on the WAL (PC_ENABLE_DBM, requires PC_ENABLE_WAL).
User-facing configuration for ProtoCore.