From 18675f3e45fa0896efd777bf66ad0ec4c8157d36 Mon Sep 17 00:00:00 2001 From: eunhhu Date: Fri, 25 Sep 2026 15:49:48 +0100 Subject: Keep app states in the tree store When the data host keeps states (PureData.state and .save, implemented by pure_tree_data), every state of an app lives in the store: a text state's draft is its stored value, a list mirrors each change into a stored draft (record header, then items), and numbers, cursors and indices are stored together by name. An app starts from what is stored, moves list fields by name when a record changed, resets cursors and indices that no longer fit, and refuses damaged stored values. Each key, text and message event publishes the states it changed together with the message's document in one store commit, or fails and leaves memory and store as they were. Frame changes go with the next event or pure_app_save. pure-run takes --store PATH. Tests run the same random events on an app in memory and on a stored app that restarts every few events and require equal states after every step; kill a process at every commit stage of an event that changes a number, a text and a list; and check frames, message atomicity and migration by restart and by replacement. A stored event costs about 10.6 ms more on the Pi's SD card, mostly two fsyncs. --- prototypes/connections/module-store.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'prototypes/connections/module-store.c') diff --git a/prototypes/connections/module-store.c b/prototypes/connections/module-store.c index 941957b..6335903 100644 --- a/prototypes/connections/module-store.c +++ b/prototypes/connections/module-store.c @@ -38,7 +38,26 @@ static void close_document(void *context, void *document) { pure_draft_close(document); } +static PureResult state(void *context, const char *name, PureDraft **out) { + return pure_state_open(context, name, out); +} + +/* The document and the app's changed states in one tree commit. */ +static PureResult save(void *context, void *document, PureDraft *const *states, size_t count, int64_t *revision) { + (void)context; + PureDraft *drafts[2 * PURE_MODULE_STATES + 2] = {0}; + int64_t revisions[2 * PURE_MODULE_STATES + 2] = {0}; + size_t n = 0; + if (count > 2 * PURE_MODULE_STATES + 1) return PURE_LIMIT; + if (document) drafts[n++] = document; + for (size_t i = 0; i < count; ++i) drafts[n++] = states[i]; + PureResult result = pure_draft_commit_all(drafts, n, revisions); + if (document) *revision = revisions[0]; + return result; +} + 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}; + .read = read, .splice = splice, .commit = commit, .close = close_document, + .state = state, .save = save}; } -- cgit v1.2.3