1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
#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);
}
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,
.state = state, .save = save};
}
|