DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
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 (DETWS_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 ::DETWS_DBM_VAL_MAX, ids by ::DETWS_DBM_KEY_MAX.
14 *
15 * Writes are batched; call ::detws_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 DETERMINISTICESPASYNCWEBSERVER_DOCSTORE_H
20#define DETERMINISTICESPASYNCWEBSERVER_DOCSTORE_H
21
22#include "ServerConfig.h"
23
24#if DETWS_ENABLE_DOCSTORE
25
26#include "services/dbm/dbm.h"
27#include <stddef.h>
28#include <stdint.h>
29
30/** @brief A document store bound to a mounted ::DetwsDbm. */
31struct DetwsDocStore
32{
33 DetwsDbm *db;
34};
35
36/** @brief Bind @p ds to an open @p db. */
37void detws_docstore_open(DetwsDocStore *ds, DetwsDbm *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 ::detws_dbm_put.
42 */
43bool detws_docstore_put(DetwsDocStore *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 detws_docstore_get(DetwsDocStore *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 detws_docstore_del(DetwsDocStore *ds, const char *id, uint16_t id_len);
53
54/** @brief @return true if document @p id exists. */
55bool detws_docstore_contains(DetwsDocStore *ds, const char *id, uint16_t id_len);
56
57/** @brief @return the number of documents. */
58uint32_t detws_docstore_count(DetwsDocStore *ds);
59
60/** @brief Make all writes durable (checkpoints the WAL). @return false on I/O failure. */
61bool detws_docstore_sync(DetwsDocStore *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 DetwsDocMatchCb = 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 ::DETWS_DOCSTORE_FIELD_MAX will not match.
72 */
73uint32_t detws_docstore_find_str(DetwsDocStore *ds, const char *field, const char *value, DetwsDocMatchCb cb,
74 void *ctx);
75
76/** @brief Find documents whose top-level integer field @p field equals @p value. @return the match count. */
77uint32_t detws_docstore_find_int(DetwsDocStore *ds, const char *field, long value, DetwsDocMatchCb cb, void *ctx);
78
79/** @brief Find documents whose top-level boolean field @p field equals @p value. @return the match count. */
80uint32_t detws_docstore_find_bool(DetwsDocStore *ds, const char *field, bool value, DetwsDocMatchCb cb, void *ctx);
81
82#endif // DETWS_ENABLE_DOCSTORE
83#endif // DETERMINISTICESPASYNCWEBSERVER_DOCSTORE_H
User-facing configuration for DeterministicESPAsyncWebServer.
Log-structured hash key-value store on the WAL (DETWS_ENABLE_DBM, requires DETWS_ENABLE_WAL).