aboutsummaryrefslogtreecommitdiffstats
path: root/prototypes/connections/module-store.c
diff options
context:
space:
mode:
authoreunhhu <[email protected]>2026-09-25 11:48:51 +0100
committereunhhu <[email protected]>2026-09-25 11:48:51 +0100
commitefba82d75e4357c7e82b027b04a5ec9937f8443b (patch)
tree9c874527466faf836e6fb4a03dd25c9c7160410a /prototypes/connections/module-store.c
parent12410be7ec2a030e8c3adcf318d87d5ac2954e1d (diff)
downloadpure-efba82d75e4357c7e82b027b04a5ec9937f8443b.tar.gz
pure-efba82d75e4357c7e82b027b04a5ec9937f8443b.zip
Add typed messages and keyed documents to Pure modules
Module format v2 adds byte values, message inputs with u32 and bytes-tail fields, one keyed byte collection, replies and require. The verifier checks every operand's type and the context each operation may run in, and allows branches and joins only at an empty stack, so typed values never merge. A message event opens its key's working version through a PureData host, conflicts early on a stale base revision, runs its handlers, and commits only if they succeed; a failed event leaves documents and states unchanged. Replies lend payload or document extents, and a document reply is copied before commit. pure_tree_data implements the host on tree store drafts, and pure-edit-module offers the document CLI contract of pure-edit from a module. test-module adds document semantics on an in-memory host (not found, create, conflict before bounds, limits, short payloads, busy commits, stable replies, require, state rollback), typed verifier rules, and 20,000 damaged message modules run safely under ASan/UBSan.
Diffstat (limited to 'prototypes/connections/module-store.c')
-rw-r--r--prototypes/connections/module-store.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/prototypes/connections/module-store.c b/prototypes/connections/module-store.c
new file mode 100644
index 0000000..941957b
--- /dev/null
+++ b/prototypes/connections/module-store.c
@@ -0,0 +1,44 @@
+#include "module-store.h"
+
+static PureResult open_document(void *context, uint32_t collection, const char *key,
+ void **document, int64_t *revision) {
+ (void)collection; /* v2 modules have one collection: the store's key space */
+ PureDraft *draft;
+ PureResult result = pure_draft_open(context, key, &draft);
+ if (result != PURE_OK) return result;
+ pure_draft_hold(draft, true); /* values read in the event stay readable */
+ *document = draft;
+ *revision = pure_draft_revision(draft);
+ return PURE_OK;
+}
+
+static PureVersion version(void *context, void *document) {
+ (void)context;
+ return pure_draft_version(document);
+}
+
+static PureResult read(void *context, void *document, PureVersion at, size_t offset, size_t length,
+ bool (*visit)(void *, PureSpan), void *visit_context) {
+ (void)context;
+ return pure_draft_read_version(document, at, offset, length, visit, visit_context);
+}
+
+static PureResult splice(void *context, void *document, size_t offset, size_t remove, PureSpan insert) {
+ (void)context;
+ return pure_draft_splice(document, offset, remove, insert);
+}
+
+static PureResult commit(void *context, void *document, int64_t *revision) {
+ (void)context;
+ return pure_draft_commit(document, revision);
+}
+
+static void close_document(void *context, void *document) {
+ (void)context;
+ pure_draft_close(document);
+}
+
+PureData pure_tree_data(const char *path) {
+ return (PureData){.context = (void *)path, .open = open_document, .version = version,
+ .read = read, .splice = splice, .commit = commit, .close = close_document};
+}