@yaks/sqlite 0.0.0-stage → 0.2.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +1596 -2
- package/_dist/archetype.d.ts +75 -0
- package/_dist/archetype.d.ts.map +1 -0
- package/_dist/branches.d.ts +23 -0
- package/_dist/branches.d.ts.map +1 -0
- package/_dist/budget.d.ts +52 -0
- package/_dist/budget.d.ts.map +1 -0
- package/_dist/bundle.d.ts +2 -0
- package/_dist/bundle.d.ts.map +1 -0
- package/_dist/cache.d.ts +24 -0
- package/_dist/cache.d.ts.map +1 -0
- package/_dist/catalog.d.ts +9 -0
- package/_dist/catalog.d.ts.map +1 -0
- package/_dist/check.d.ts +4 -0
- package/_dist/check.d.ts.map +1 -0
- package/_dist/db.d.ts +36 -0
- package/_dist/db.d.ts.map +1 -0
- package/_dist/ddl.d.ts +143 -0
- package/_dist/ddl.d.ts.map +1 -0
- package/_dist/doom.d.ts +22 -0
- package/_dist/doom.d.ts.map +1 -0
- package/_dist/follow.d.ts +74 -0
- package/_dist/follow.d.ts.map +1 -0
- package/_dist/history-shapes.d.ts +3 -0
- package/_dist/history-shapes.d.ts.map +1 -0
- package/_dist/history.d.ts +18 -0
- package/_dist/history.d.ts.map +1 -0
- package/_dist/inspect.d.ts +43 -0
- package/_dist/inspect.d.ts.map +1 -0
- package/_dist/jsonb.d.ts +17 -0
- package/_dist/jsonb.d.ts.map +1 -0
- package/_dist/layout.d.ts +7 -0
- package/_dist/layout.d.ts.map +1 -0
- package/_dist/ledger.d.ts +41 -0
- package/_dist/ledger.d.ts.map +1 -0
- package/_dist/meta.d.ts +89 -0
- package/_dist/meta.d.ts.map +1 -0
- package/_dist/migration.d.ts +41 -0
- package/_dist/migration.d.ts.map +1 -0
- package/_dist/mod.d.ts +201 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/mode.d.ts +14 -0
- package/_dist/mode.d.ts.map +1 -0
- package/_dist/overlay.d.ts +30 -0
- package/_dist/overlay.d.ts.map +1 -0
- package/_dist/owe.d.ts +180 -0
- package/_dist/owe.d.ts.map +1 -0
- package/_dist/physical.d.ts +89 -0
- package/_dist/physical.d.ts.map +1 -0
- package/_dist/point.d.ts +10 -0
- package/_dist/point.d.ts.map +1 -0
- package/_dist/raise.d.ts +22 -0
- package/_dist/raise.d.ts.map +1 -0
- package/_dist/read-db.d.ts +9 -0
- package/_dist/read-db.d.ts.map +1 -0
- package/_dist/read-store.d.ts +10 -0
- package/_dist/read-store.d.ts.map +1 -0
- package/_dist/read.d.ts +59 -0
- package/_dist/read.d.ts.map +1 -0
- package/_dist/reader.d.ts +11 -0
- package/_dist/reader.d.ts.map +1 -0
- package/_dist/rules.d.ts +19 -0
- package/_dist/rules.d.ts.map +1 -0
- package/_dist/service.d.ts +12 -0
- package/_dist/service.d.ts.map +1 -0
- package/_dist/shape.d.ts +12 -0
- package/_dist/shape.d.ts.map +1 -0
- package/_dist/statements.d.ts +4 -0
- package/_dist/statements.d.ts.map +1 -0
- package/_dist/survey.d.ts +31 -0
- package/_dist/survey.d.ts.map +1 -0
- package/_dist/tools.d.ts +15 -0
- package/_dist/tools.d.ts.map +1 -0
- package/_dist/trash.d.ts +18 -0
- package/_dist/trash.d.ts.map +1 -0
- package/_dist/unit.d.ts +11 -0
- package/_dist/unit.d.ts.map +1 -0
- package/_dist/vocab.d.ts +6 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/_dist/write.d.ts +169 -0
- package/_dist/write.d.ts.map +1 -0
- package/answer.js +110 -0
- package/answer.js.map +1 -0
- package/answer.ts +122 -0
- package/archetype.js +730 -0
- package/archetype.js.map +1 -0
- package/archetype.ts +790 -0
- package/binary_cases.ts +240 -0
- package/branches.js +195 -0
- package/branches.js.map +1 -0
- package/branches.ts +257 -0
- package/budget.js +89 -0
- package/budget.js.map +1 -0
- package/budget.ts +117 -0
- package/builtin.js +34 -0
- package/builtin.js.map +1 -0
- package/builtin.ts +31 -0
- package/bundle.js +21 -0
- package/bundle.js.map +1 -0
- package/bundle.ts +22 -0
- package/buried.js +95 -0
- package/buried.js.map +1 -0
- package/buried.ts +124 -0
- package/cache.js +153 -0
- package/cache.js.map +1 -0
- package/cache.ts +154 -0
- package/cache_worker.ts +16 -0
- package/catalog.js +173 -0
- package/catalog.js.map +1 -0
- package/catalog.ts +224 -0
- package/check.js +80 -0
- package/check.js.map +1 -0
- package/check.ts +92 -0
- package/checkpoint.js +98 -0
- package/checkpoint.js.map +1 -0
- package/checkpoint.ts +129 -0
- package/checkpoint_worker.ts +37 -0
- package/db.js +170 -0
- package/db.js.map +1 -0
- package/db.ts +173 -0
- package/ddl.js +1441 -0
- package/ddl.js.map +1 -0
- package/ddl.ts +1508 -0
- package/deno.json +1 -0
- package/doom.js +59 -0
- package/doom.js.map +1 -0
- package/doom.ts +75 -0
- package/driver.js +69 -0
- package/driver.js.map +1 -0
- package/driver.ts +101 -0
- package/error.js +30 -0
- package/error.js.map +1 -0
- package/error.ts +46 -0
- package/ffi/LICENSE +202 -0
- package/ffi/blob.js +99 -0
- package/ffi/blob.js.map +1 -0
- package/ffi/blob.ts +156 -0
- package/ffi/constants.js +65 -0
- package/ffi/constants.js.map +1 -0
- package/ffi/constants.ts +67 -0
- package/ffi/database.js +613 -0
- package/ffi/database.js.map +1 -0
- package/ffi/database.ts +931 -0
- package/ffi/ffi.js +535 -0
- package/ffi/ffi.js.map +1 -0
- package/ffi/ffi.ts +604 -0
- package/ffi/mod.js +15 -0
- package/ffi/mod.js.map +1 -0
- package/ffi/mod.ts +28 -0
- package/ffi/statement.js +555 -0
- package/ffi/statement.js.map +1 -0
- package/ffi/statement.ts +788 -0
- package/ffi/util.js +32 -0
- package/ffi/util.js.map +1 -0
- package/ffi/util.ts +48 -0
- package/follow.js +342 -0
- package/follow.js.map +1 -0
- package/follow.ts +401 -0
- package/history-branch.js +166 -0
- package/history-branch.js.map +1 -0
- package/history-branch.ts +140 -0
- package/history-shapes.js +254 -0
- package/history-shapes.js.map +1 -0
- package/history-shapes.ts +268 -0
- package/history.js +620 -0
- package/history.js.map +1 -0
- package/history.ts +674 -0
- package/inspect.js +120 -0
- package/inspect.js.map +1 -0
- package/inspect.ts +164 -0
- package/jsonb.js +73 -0
- package/jsonb.js.map +1 -0
- package/jsonb.ts +108 -0
- package/layout.js +22 -0
- package/layout.js.map +1 -0
- package/layout.ts +26 -0
- package/ledger.js +210 -0
- package/ledger.js.map +1 -0
- package/ledger.ts +210 -0
- package/legacy.js +4 -0
- package/legacy.js.map +1 -0
- package/legacy.ts +3 -0
- package/maintenance.js +148 -0
- package/maintenance.js.map +1 -0
- package/maintenance.ts +154 -0
- package/measure.js +133 -0
- package/measure.js.map +1 -0
- package/measure.ts +167 -0
- package/measure_worker.ts +18 -0
- package/memo.js +1250 -0
- package/memo.js.map +1 -0
- package/memo.ts +1480 -0
- package/meta.js +265 -0
- package/meta.js.map +1 -0
- package/meta.ts +321 -0
- package/migration.js +272 -0
- package/migration.js.map +1 -0
- package/migration.ts +286 -0
- package/mod.js +1103 -0
- package/mod.js.map +1 -0
- package/mod.ts +1762 -0
- package/mode.js +192 -0
- package/mode.js.map +1 -0
- package/mode.ts +207 -0
- package/native.js +544 -0
- package/native.js.map +1 -0
- package/native.ts +544 -0
- package/overlay.js +408 -0
- package/overlay.js.map +1 -0
- package/overlay.ts +485 -0
- package/owe.js +719 -0
- package/owe.js.map +1 -0
- package/owe.ts +873 -0
- package/package.json +99 -4
- package/physical.js +350 -0
- package/physical.js.map +1 -0
- package/physical.ts +434 -0
- package/plan.js +78 -0
- package/plan.js.map +1 -0
- package/plan.ts +104 -0
- package/point.js +81 -0
- package/point.js.map +1 -0
- package/point.ts +52 -0
- package/population.js +639 -0
- package/population.js.map +1 -0
- package/population.ts +630 -0
- package/purge.js +117 -0
- package/purge.js.map +1 -0
- package/purge.ts +127 -0
- package/raise.js +270 -0
- package/raise.js.map +1 -0
- package/raise.ts +316 -0
- package/read-db.js +381 -0
- package/read-db.js.map +1 -0
- package/read-db.ts +282 -0
- package/read-store.js +74 -0
- package/read-store.js.map +1 -0
- package/read-store.ts +86 -0
- package/read.js +1100 -0
- package/read.js.map +1 -0
- package/read.ts +1441 -0
- package/reader.js +141 -0
- package/reader.js.map +1 -0
- package/reader.ts +159 -0
- package/rules.js +88 -0
- package/rules.js.map +1 -0
- package/rules.ts +113 -0
- package/service.js +27 -0
- package/service.js.map +1 -0
- package/service.ts +41 -0
- package/shape.js +76 -0
- package/shape.js.map +1 -0
- package/shape.ts +108 -0
- package/sparse_presence_fixture.ts +45 -0
- package/sqlitepath.js +24 -0
- package/sqlitepath.js.map +1 -0
- package/sqlitepath.ts +29 -0
- package/statements.js +78 -0
- package/statements.js.map +1 -0
- package/statements.ts +96 -0
- package/survey.js +135 -0
- package/survey.js.map +1 -0
- package/survey.ts +196 -0
- package/tools.js +17 -0
- package/tools.js.map +1 -0
- package/tools.ts +50 -0
- package/trash.js +144 -0
- package/trash.js.map +1 -0
- package/trash.ts +187 -0
- package/unit.js +215 -0
- package/unit.js.map +1 -0
- package/unit.ts +211 -0
- package/versions.js +115 -0
- package/versions.js.map +1 -0
- package/versions.ts +110 -0
- package/vocab.js +19 -0
- package/vocab.js.map +1 -0
- package/vocab.json +27 -0
- package/vocab.ts +19 -0
- package/watch.js +33 -0
- package/watch.js.map +1 -0
- package/watch.ts +42 -0
- package/write.js +1164 -0
- package/write.js.map +1 -0
- package/write.ts +1356 -0
- package/written.js +200 -0
- package/written.js.map +1 -0
- package/written.ts +202 -0
package/meta.js
ADDED
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
// The store's own key/value: the few facts a store keeps about itself rather
|
|
2
|
+
// than about the entities in it — the sync epoch a returning client checks its
|
|
3
|
+
// cursor against, a sweep's high-water mark, the marker recording that a
|
|
4
|
+
// one-shot repair has already run, the plugins its data requires.
|
|
5
|
+
//
|
|
6
|
+
// It sits beside the graph on purpose. A row here has no entity id and no
|
|
7
|
+
// component, so nothing that walks the vocabulary can carry it: not a read, not
|
|
8
|
+
// a bundle, not a client cache. An application that needs a durable scalar and
|
|
9
|
+
// does not want clients to read it writes it here instead of inventing a
|
|
10
|
+
// component for it.
|
|
11
|
+
//
|
|
12
|
+
// The table is `server_meta` (ddl.ts {@link META}), created by `schema()` with
|
|
13
|
+
// the rest of the spine, so a store that has installed already has it.
|
|
14
|
+
import { and, call, cell, col, effect, eq, ge, lt, revision, select, table, val } from "@yaks/sql/core";
|
|
15
|
+
import { META } from "./layout.js";
|
|
16
|
+
let put = {
|
|
17
|
+
t: 'insert',
|
|
18
|
+
into: META,
|
|
19
|
+
cols: [
|
|
20
|
+
'k',
|
|
21
|
+
'v'
|
|
22
|
+
],
|
|
23
|
+
upsert: [
|
|
24
|
+
{
|
|
25
|
+
on: [
|
|
26
|
+
col('k')
|
|
27
|
+
],
|
|
28
|
+
set: {
|
|
29
|
+
v: col('v', 'excluded')
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
]
|
|
33
|
+
};
|
|
34
|
+
/** Bind the key/value interface to a driver. A value read is held by the
|
|
35
|
+
* driver's connection until something it cannot vouch for moves the table's
|
|
36
|
+
* data (@yaks/sql `revision`: another statement writing it, a rollback, DDL,
|
|
37
|
+
* another connection's commit); a write through here keeps what is held
|
|
38
|
+
* current instead, so one store's bookkeeping does not send every other key
|
|
39
|
+
* back to SQLite. */ export let meta = (driver)=>({
|
|
40
|
+
get: (k)=>{
|
|
41
|
+
let { values } = holding(driver);
|
|
42
|
+
if (!values.has(k)) values.set(k, read(driver, k));
|
|
43
|
+
return values.get(k);
|
|
44
|
+
},
|
|
45
|
+
set: (k, v)=>{
|
|
46
|
+
let kept = keeping(driver, ()=>effect(driver, {
|
|
47
|
+
...put,
|
|
48
|
+
rows: [
|
|
49
|
+
[
|
|
50
|
+
val(k),
|
|
51
|
+
val(v)
|
|
52
|
+
]
|
|
53
|
+
]
|
|
54
|
+
}));
|
|
55
|
+
if (kept) changed(kept, k, v);
|
|
56
|
+
},
|
|
57
|
+
setAll: (values)=>{
|
|
58
|
+
let entries = Object.entries(values);
|
|
59
|
+
if (!entries.length) return;
|
|
60
|
+
let kept = keeping(driver, ()=>effect(driver, {
|
|
61
|
+
...put,
|
|
62
|
+
q: select({
|
|
63
|
+
cols: [
|
|
64
|
+
cell(0),
|
|
65
|
+
cell(1)
|
|
66
|
+
],
|
|
67
|
+
from: call('json_each', [
|
|
68
|
+
val(JSON.stringify(entries))
|
|
69
|
+
]),
|
|
70
|
+
where: and()
|
|
71
|
+
})
|
|
72
|
+
}));
|
|
73
|
+
if (kept) {
|
|
74
|
+
for (let [k, v] of entries)changed(kept, k, v);
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
del: (k)=>{
|
|
78
|
+
let kept = keeping(driver, ()=>effect(driver, {
|
|
79
|
+
t: 'delete',
|
|
80
|
+
from: META,
|
|
81
|
+
where: eq(col('k'), val(k))
|
|
82
|
+
}));
|
|
83
|
+
if (kept) changed(kept, k);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
/** One key's value as the table holds it now, read past what is held. */ export let read = (driver, k)=>{
|
|
87
|
+
let row = driver.query(select({
|
|
88
|
+
cols: [
|
|
89
|
+
col('v')
|
|
90
|
+
],
|
|
91
|
+
from: table(META),
|
|
92
|
+
where: eq(col('k'), val(k))
|
|
93
|
+
}))[0];
|
|
94
|
+
return row ? String(row.v) : undefined;
|
|
95
|
+
};
|
|
96
|
+
let held = new WeakMap();
|
|
97
|
+
let holding = (driver)=>{
|
|
98
|
+
let at = revision(driver, 'data', META), on = driver.connection ?? driver;
|
|
99
|
+
let kept = held.get(on);
|
|
100
|
+
if (kept?.at !== at) {
|
|
101
|
+
held.set(on, kept = {
|
|
102
|
+
at,
|
|
103
|
+
values: new Map(),
|
|
104
|
+
ranges: new Map()
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
return kept;
|
|
108
|
+
};
|
|
109
|
+
/** Every key starting `prefix`, with its value, in key order: one range read
|
|
110
|
+
* of the table, held as a key's value is. */ export let under = (driver, prefix)=>{
|
|
111
|
+
let { ranges } = holding(driver);
|
|
112
|
+
let rows = ranges.get(prefix);
|
|
113
|
+
if (!rows) {
|
|
114
|
+
let past = prefix.slice(0, -1) + String.fromCharCode(prefix.charCodeAt(prefix.length - 1) + 1);
|
|
115
|
+
rows = driver.query(select({
|
|
116
|
+
cols: [
|
|
117
|
+
col('k'),
|
|
118
|
+
col('v')
|
|
119
|
+
],
|
|
120
|
+
from: table(META),
|
|
121
|
+
where: and(ge(col('k'), val(prefix)), lt(col('k'), val(past))),
|
|
122
|
+
order: [
|
|
123
|
+
col('k')
|
|
124
|
+
]
|
|
125
|
+
})).map((r)=>[
|
|
126
|
+
String(r.k),
|
|
127
|
+
String(r.v)
|
|
128
|
+
]);
|
|
129
|
+
ranges.set(prefix, rows);
|
|
130
|
+
}
|
|
131
|
+
return rows;
|
|
132
|
+
};
|
|
133
|
+
// A write by a connection that alone owns its file moves only the key it
|
|
134
|
+
// names: what was held just before it stays true after. A file's other
|
|
135
|
+
// connections are asked for at every revision outside a transaction, so
|
|
136
|
+
// there a write lets go of what is held instead.
|
|
137
|
+
let keeping = (driver, write)=>{
|
|
138
|
+
let kept = driver.file ? undefined : held.get(driver.connection ?? driver);
|
|
139
|
+
let current = !!kept && kept.at === revision(driver, 'data', META);
|
|
140
|
+
write();
|
|
141
|
+
if (!kept || !current) return;
|
|
142
|
+
kept.at = revision(driver, 'data', META);
|
|
143
|
+
return kept;
|
|
144
|
+
};
|
|
145
|
+
let changed = (kept, key, value)=>{
|
|
146
|
+
kept.values.set(key, value);
|
|
147
|
+
for (let prefix of kept.ranges.keys()){
|
|
148
|
+
if (key.startsWith(prefix)) kept.ranges.delete(prefix);
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
/** The key the epoch is kept under. */ export let EPOCH = 'epoch';
|
|
152
|
+
/** The key the fingerprint of the installed schema is kept under (mod.ts
|
|
153
|
+
* `install`). */ export let SCHEMA = 'schema';
|
|
154
|
+
/** The fingerprint of the schema this file last had installed, or undefined
|
|
155
|
+
* for a file no install has finished in — one that may not have this table
|
|
156
|
+
* yet. */ export let installed = (driver)=>installation(driver)?.schema;
|
|
157
|
+
/** The schema and its analysis receipt share the installation's existing
|
|
158
|
+
* metadata read. File stores keep their plain stamp; full analysis records
|
|
159
|
+
* stored bytes after a successful pass so another incarnation can skip it. */ export let installation = (driver)=>{
|
|
160
|
+
let value = kept(driver, SCHEMA);
|
|
161
|
+
if (value == null) return;
|
|
162
|
+
let [schema, statistics] = value.split('\n');
|
|
163
|
+
return {
|
|
164
|
+
schema,
|
|
165
|
+
statistics: statistics == null ? undefined : JSON.parse(statistics)
|
|
166
|
+
};
|
|
167
|
+
};
|
|
168
|
+
/** Keep an analysis receipt without changing the public schema fingerprint. */ export let analyzedAt = (driver, schema, statistics)=>meta(driver).set(SCHEMA, `${schema}\n${JSON.stringify(statistics)}`);
|
|
169
|
+
/** The prefix of the key a file records each composed schema's print under
|
|
170
|
+
* (./follow.ts): `schema:<print>`, its value when it was recorded. */ export let PRINTED = 'schema:';
|
|
171
|
+
/** Whether this file has recorded the composed schema `print` names. */ export let printed = (driver, print)=>kept(driver, PRINTED + print) != null;
|
|
172
|
+
/** Every composed schema's print this file has recorded: one range read of
|
|
173
|
+
* `server_meta`. */ export let prints = (driver)=>under(driver, PRINTED).map(([k])=>k.slice(PRINTED.length));
|
|
174
|
+
/** The prefix of the key a file records each survey of a store's schema under
|
|
175
|
+
* (./survey.ts): `surveyed:<print>`, the store's print. */ export let SURVEYED = 'surveyed:';
|
|
176
|
+
/** The survey this file recorded of the store schema `print` names, as it
|
|
177
|
+
* was recorded. */ export let surveyedAt = (driver, print)=>kept(driver, SURVEYED + print);
|
|
178
|
+
/** The prefix of the key a store records each plugin its data requires under:
|
|
179
|
+
* `required:<plugin>`, its value when it was recorded. A host that does not
|
|
180
|
+
* compose every one refuses to open the store (@yaks/host `meet`). A plugin
|
|
181
|
+
* removed on purpose drops its row:
|
|
182
|
+
* `delete from server_meta where k = 'required:@yaks/journal'`. */ export let REQUIRED = 'required:';
|
|
183
|
+
/** The plugins this store's data requires: one range read of `server_meta`,
|
|
184
|
+
* and none where the table is not there yet. */ export let requirements = (driver)=>driver.query({
|
|
185
|
+
t: 'pragma',
|
|
186
|
+
name: 'table_info',
|
|
187
|
+
arg: META
|
|
188
|
+
}).length ? under(driver, REQUIRED).map(([k])=>k.slice(REQUIRED.length)) : [];
|
|
189
|
+
/** Record that this store's data requires each of `plugins`, from now on. A
|
|
190
|
+
* plugin it already requires keeps the time it was first recorded. */ export let requires = (driver, plugins)=>{
|
|
191
|
+
if (!plugins.length) return;
|
|
192
|
+
let at = new Date().toISOString();
|
|
193
|
+
effect(driver, {
|
|
194
|
+
t: 'insert',
|
|
195
|
+
or: 'ignore',
|
|
196
|
+
into: META,
|
|
197
|
+
cols: [
|
|
198
|
+
'k',
|
|
199
|
+
'v'
|
|
200
|
+
],
|
|
201
|
+
rows: plugins.map((p)=>[
|
|
202
|
+
val(REQUIRED + p),
|
|
203
|
+
val(at)
|
|
204
|
+
])
|
|
205
|
+
});
|
|
206
|
+
};
|
|
207
|
+
// A key's value, read without assuming the table is there yet. The table is
|
|
208
|
+
// asked after by name (`table_info`, empty for one that is not there) rather
|
|
209
|
+
// than found in the whole catalogue, which a store reads on every wake.
|
|
210
|
+
// The drivers whose file has the table: once it has, it keeps it.
|
|
211
|
+
let standing = new WeakSet();
|
|
212
|
+
let kept = (driver, k)=>{
|
|
213
|
+
if (!standing.has(driver)) {
|
|
214
|
+
if (!driver.query({
|
|
215
|
+
t: 'pragma',
|
|
216
|
+
name: 'table_info',
|
|
217
|
+
arg: META
|
|
218
|
+
}).length) {
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
standing.add(driver);
|
|
222
|
+
}
|
|
223
|
+
return meta(driver).get(k);
|
|
224
|
+
};
|
|
225
|
+
/** The key the component tables the file holds are kept under, as their
|
|
226
|
+
* names: what its install found, and each a write raised since (./raise.ts),
|
|
227
|
+
* so a reader knows which tables a store holds from one row rather than a
|
|
228
|
+
* look at its schema. */ export let TABLES = 'tables';
|
|
229
|
+
/** The component tables the file holds, as its install and its writes
|
|
230
|
+
* recorded them; undefined for a file no install of this code recorded. */ export let tablesAt = (driver)=>{
|
|
231
|
+
let v = kept(driver, TABLES);
|
|
232
|
+
return v == null ? undefined : v ? v.split(' ') : [];
|
|
233
|
+
};
|
|
234
|
+
/** The store's epoch where one was minted, read without minting it: what a
|
|
235
|
+
* read asks, where {@link epoch} writes. */ export let epochAt = (driver)=>kept(driver, EPOCH);
|
|
236
|
+
/**
|
|
237
|
+
* The store's lineage identity: a string minted once and persisted, so it
|
|
238
|
+
* survives a restart, a deploy and a handover — a returning client whose cursor
|
|
239
|
+
* carries this epoch may resume where it left off, and a client holding another
|
|
240
|
+
* store's epoch (or none) must start over, because rows from a different
|
|
241
|
+
* lineage can never be replayed against it.
|
|
242
|
+
*
|
|
243
|
+
* `install()` mints it, and this is idempotent (`insert or ignore`): a store
|
|
244
|
+
* that already has one keeps it and reads it back. It writes, so a read-only
|
|
245
|
+
* path calls `meta(driver).get(EPOCH)` instead and treats an absent one as a
|
|
246
|
+
* store no cursor can be trusted against.
|
|
247
|
+
*/ export let epoch = (driver)=>{
|
|
248
|
+
effect(driver, {
|
|
249
|
+
t: 'insert',
|
|
250
|
+
or: 'ignore',
|
|
251
|
+
into: META,
|
|
252
|
+
cols: [
|
|
253
|
+
'k',
|
|
254
|
+
'v'
|
|
255
|
+
],
|
|
256
|
+
rows: [
|
|
257
|
+
[
|
|
258
|
+
val(EPOCH),
|
|
259
|
+
val(crypto.randomUUID())
|
|
260
|
+
]
|
|
261
|
+
]
|
|
262
|
+
});
|
|
263
|
+
return meta(driver).get(EPOCH);
|
|
264
|
+
};
|
|
265
|
+
//# sourceMappingURL=meta.js.map
|
package/meta.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"meta.js","sources":["./meta.ts"],"names":[],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O,CAAC,QAAgB;EAC1B,IAAI,CAAC,SAAS,GAAG,CAAC,SAAS;IACzB,IAAI,CAAC,OAAO,KAAK,CAAC;MAAE,GAAG;MAAU,MAAM;MAAc,KAAK;IAAK,GAAG,MAAM,EAAE;MACxE;IACF;IACA,SAAS,GAAG,CAAC;EACf;EACA,OAAO,KAAK,QAAQ,GAAG,CAAC;AAC1B;AAEA;;;uBAGuB,GACvB,OAAO,IAAI,SAAS,SAAQ;AAE5B;yEACyE,GACzE,OAAO,IAAI,WAAW,CAAC;EACrB,IAAI,IAAI,KAAK,QAAQ;EACrB,OAAO,KAAK,OAAO,YAAY,IAAI,EAAE,KAAK,CAAC,OAAO,EAAE;AACtD,EAAC;AAED;0CAC0C,GAC1C,OAAO,IAAI,UAAU,CAAC,SAAuC,KAAK,QAAQ,OAAM;AAEhF;;;;;;;;;;;CAWC,GACD,OAAO,IAAI,QAAQ,CAAC;EAClB,OAAO,QAAQ;IACb,GAAG;IACH,IAAI;IACJ,MAAM;IACN,MAAM;MAAC;MAAK;KAAI;IAChB,MAAM;MAAC;QAAC,IAAI;QAAQ,IAAI,OAAO,UAAU;OAAI;KAAC;EAChD;EACA,OAAO,KAAK,QAAQ,GAAG,CAAC;AAC1B,EAAC"}
|
package/meta.ts
ADDED
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
// The store's own key/value: the few facts a store keeps about itself rather
|
|
2
|
+
// than about the entities in it — the sync epoch a returning client checks its
|
|
3
|
+
// cursor against, a sweep's high-water mark, the marker recording that a
|
|
4
|
+
// one-shot repair has already run, the plugins its data requires.
|
|
5
|
+
//
|
|
6
|
+
// It sits beside the graph on purpose. A row here has no entity id and no
|
|
7
|
+
// component, so nothing that walks the vocabulary can carry it: not a read, not
|
|
8
|
+
// a bundle, not a client cache. An application that needs a durable scalar and
|
|
9
|
+
// does not want clients to read it writes it here instead of inventing a
|
|
10
|
+
// component for it.
|
|
11
|
+
//
|
|
12
|
+
// The table is `server_meta` (ddl.ts {@link META}), created by `schema()` with
|
|
13
|
+
// the rest of the spine, so a store that has installed already has it.
|
|
14
|
+
|
|
15
|
+
import {
|
|
16
|
+
and,
|
|
17
|
+
call,
|
|
18
|
+
cell,
|
|
19
|
+
col,
|
|
20
|
+
type Driver,
|
|
21
|
+
effect,
|
|
22
|
+
eq,
|
|
23
|
+
ge,
|
|
24
|
+
type Insert,
|
|
25
|
+
lt,
|
|
26
|
+
revision,
|
|
27
|
+
select,
|
|
28
|
+
table,
|
|
29
|
+
val,
|
|
30
|
+
} from '@yaks/sql/core'
|
|
31
|
+
import { META } from './layout.js'
|
|
32
|
+
|
|
33
|
+
/** Read, write, clear — the whole interface. Values are text; an application
|
|
34
|
+
* that wants a number or a timestamp formats it itself, the way it will parse
|
|
35
|
+
* it back. */
|
|
36
|
+
export type Meta = {
|
|
37
|
+
get: (k: string) => string | undefined
|
|
38
|
+
set: (k: string, v: string) => void
|
|
39
|
+
del: (k: string) => void
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
let put: Insert = {
|
|
43
|
+
t: 'insert',
|
|
44
|
+
into: META,
|
|
45
|
+
cols: ['k', 'v'],
|
|
46
|
+
upsert: [{ on: [col('k')], set: { v: col('v', 'excluded') } }],
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Bind the key/value interface to a driver. A value read is held by the
|
|
50
|
+
* driver's connection until something it cannot vouch for moves the table's
|
|
51
|
+
* data (@yaks/sql `revision`: another statement writing it, a rollback, DDL,
|
|
52
|
+
* another connection's commit); a write through here keeps what is held
|
|
53
|
+
* current instead, so one store's bookkeeping does not send every other key
|
|
54
|
+
* back to SQLite. */
|
|
55
|
+
export let meta = (driver: Driver): Meta & {
|
|
56
|
+
setAll: (values: Record<string, string>) => void
|
|
57
|
+
} => ({
|
|
58
|
+
get: (k) => {
|
|
59
|
+
let { values } = holding(driver)
|
|
60
|
+
if (!values.has(k)) values.set(k, read(driver, k))
|
|
61
|
+
return values.get(k)
|
|
62
|
+
},
|
|
63
|
+
set: (k, v) => {
|
|
64
|
+
let kept = keeping(
|
|
65
|
+
driver,
|
|
66
|
+
() => effect(driver, { ...put, rows: [[val(k), val(v)]] }),
|
|
67
|
+
)
|
|
68
|
+
if (kept) changed(kept, k, v)
|
|
69
|
+
},
|
|
70
|
+
setAll: (values) => {
|
|
71
|
+
let entries = Object.entries(values)
|
|
72
|
+
if (!entries.length) return
|
|
73
|
+
let kept = keeping(driver, () =>
|
|
74
|
+
effect(driver, {
|
|
75
|
+
...put,
|
|
76
|
+
q: select({
|
|
77
|
+
cols: [cell(0), cell(1)],
|
|
78
|
+
from: call('json_each', [val(JSON.stringify(entries))]),
|
|
79
|
+
where: and(),
|
|
80
|
+
}),
|
|
81
|
+
}))
|
|
82
|
+
if (kept) { for (let [k, v] of entries) changed(kept, k, v) }
|
|
83
|
+
},
|
|
84
|
+
del: (k) => {
|
|
85
|
+
let kept = keeping(driver, () =>
|
|
86
|
+
effect(driver, {
|
|
87
|
+
t: 'delete',
|
|
88
|
+
from: META,
|
|
89
|
+
where: eq(col('k'), val(k)),
|
|
90
|
+
}))
|
|
91
|
+
if (kept) changed(kept, k)
|
|
92
|
+
},
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
/** One key's value as the table holds it now, read past what is held. */
|
|
96
|
+
export let read = (driver: Driver, k: string): string | undefined => {
|
|
97
|
+
let row = driver.query(select({
|
|
98
|
+
cols: [col('v')],
|
|
99
|
+
from: table(META),
|
|
100
|
+
where: eq(col('k'), val(k)),
|
|
101
|
+
}))[0]
|
|
102
|
+
return row ? String(row.v) : undefined
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// What each connection holds of the table, and the data revision it holds
|
|
106
|
+
// it at.
|
|
107
|
+
type Held = {
|
|
108
|
+
at: number
|
|
109
|
+
values: Map<string, string | undefined>
|
|
110
|
+
/** each prefix read, its keys and values in key order */
|
|
111
|
+
ranges: Map<string, [string, string][]>
|
|
112
|
+
}
|
|
113
|
+
let held = new WeakMap<object, Held>()
|
|
114
|
+
let holding = (driver: Driver): Held => {
|
|
115
|
+
let at = revision(driver, 'data', META), on = driver.connection ?? driver
|
|
116
|
+
let kept = held.get(on)
|
|
117
|
+
if (kept?.at !== at) {
|
|
118
|
+
held.set(on, kept = { at, values: new Map(), ranges: new Map() })
|
|
119
|
+
}
|
|
120
|
+
return kept
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Every key starting `prefix`, with its value, in key order: one range read
|
|
124
|
+
* of the table, held as a key's value is. */
|
|
125
|
+
export let under = (driver: Driver, prefix: string): [string, string][] => {
|
|
126
|
+
let { ranges } = holding(driver)
|
|
127
|
+
let rows = ranges.get(prefix)
|
|
128
|
+
if (!rows) {
|
|
129
|
+
let past = prefix.slice(0, -1) +
|
|
130
|
+
String.fromCharCode(prefix.charCodeAt(prefix.length - 1) + 1)
|
|
131
|
+
rows = driver.query(select({
|
|
132
|
+
cols: [col('k'), col('v')],
|
|
133
|
+
from: table(META),
|
|
134
|
+
where: and(ge(col('k'), val(prefix)), lt(col('k'), val(past))),
|
|
135
|
+
order: [col('k')],
|
|
136
|
+
})).map((r) => [String(r.k), String(r.v)])
|
|
137
|
+
ranges.set(prefix, rows)
|
|
138
|
+
}
|
|
139
|
+
return rows
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// A write by a connection that alone owns its file moves only the key it
|
|
143
|
+
// names: what was held just before it stays true after. A file's other
|
|
144
|
+
// connections are asked for at every revision outside a transaction, so
|
|
145
|
+
// there a write lets go of what is held instead.
|
|
146
|
+
let keeping = (
|
|
147
|
+
driver: Driver,
|
|
148
|
+
write: () => void,
|
|
149
|
+
): Held | undefined => {
|
|
150
|
+
let kept = driver.file ? undefined : held.get(driver.connection ?? driver)
|
|
151
|
+
let current = !!kept && kept.at === revision(driver, 'data', META)
|
|
152
|
+
write()
|
|
153
|
+
if (!kept || !current) return
|
|
154
|
+
kept.at = revision(driver, 'data', META)
|
|
155
|
+
return kept
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
let changed = (kept: Held, key: string, value?: string) => {
|
|
159
|
+
kept.values.set(key, value)
|
|
160
|
+
for (let prefix of kept.ranges.keys()) {
|
|
161
|
+
if (key.startsWith(prefix)) kept.ranges.delete(prefix)
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** The key the epoch is kept under. */
|
|
166
|
+
export let EPOCH = 'epoch'
|
|
167
|
+
|
|
168
|
+
/** The key the fingerprint of the installed schema is kept under (mod.ts
|
|
169
|
+
* `install`). */
|
|
170
|
+
export let SCHEMA = 'schema'
|
|
171
|
+
|
|
172
|
+
/** What a full-analysis engine's planner statistics stand at (./ddl.ts
|
|
173
|
+
* `analyzed`): the stored bytes growth is measured from, the tables that held
|
|
174
|
+
* at most one row when last looked at (no SQLite statistics row is taken of
|
|
175
|
+
* them, ./ddl.ts `analyzing`), and the tables owed an analysis, in the order
|
|
176
|
+
* they are worked off, and how far the count of the first has got where it is
|
|
177
|
+
* counted (./ddl.ts `counted`). A receipt kept with a `few` list is from
|
|
178
|
+
* before each decision read those off the statistics, and is written anew. */
|
|
179
|
+
export type Statistics = {
|
|
180
|
+
bytes: number
|
|
181
|
+
empty: string[]
|
|
182
|
+
owed?: string[]
|
|
183
|
+
counting?: Counting
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** How far a table's count has got: the rows read, and the last rowid read
|
|
187
|
+
* (none before the first window). */
|
|
188
|
+
export type Counting = { table: string; rows: number; after?: number }
|
|
189
|
+
|
|
190
|
+
/** The fingerprint of the schema this file last had installed, or undefined
|
|
191
|
+
* for a file no install has finished in — one that may not have this table
|
|
192
|
+
* yet. */
|
|
193
|
+
export let installed = (driver: Driver): string | undefined =>
|
|
194
|
+
installation(driver)?.schema
|
|
195
|
+
|
|
196
|
+
/** The schema and its analysis receipt share the installation's existing
|
|
197
|
+
* metadata read. File stores keep their plain stamp; full analysis records
|
|
198
|
+
* stored bytes after a successful pass so another incarnation can skip it. */
|
|
199
|
+
export let installation = (
|
|
200
|
+
driver: Driver,
|
|
201
|
+
): { schema: string; statistics?: Statistics } | undefined => {
|
|
202
|
+
let value = kept(driver, SCHEMA)
|
|
203
|
+
if (value == null) return
|
|
204
|
+
let [schema, statistics] = value.split('\n')
|
|
205
|
+
return {
|
|
206
|
+
schema,
|
|
207
|
+
statistics: statistics == null ? undefined : JSON.parse(statistics),
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** Keep an analysis receipt without changing the public schema fingerprint. */
|
|
212
|
+
export let analyzedAt = (
|
|
213
|
+
driver: Driver,
|
|
214
|
+
schema: string,
|
|
215
|
+
statistics: Statistics,
|
|
216
|
+
): void => meta(driver).set(SCHEMA, `${schema}\n${JSON.stringify(statistics)}`)
|
|
217
|
+
|
|
218
|
+
/** The prefix of the key a file records each composed schema's print under
|
|
219
|
+
* (./follow.ts): `schema:<print>`, its value when it was recorded. */
|
|
220
|
+
export let PRINTED = 'schema:'
|
|
221
|
+
|
|
222
|
+
/** Whether this file has recorded the composed schema `print` names. */
|
|
223
|
+
export let printed = (driver: Driver, print: string): boolean =>
|
|
224
|
+
kept(driver, PRINTED + print) != null
|
|
225
|
+
|
|
226
|
+
/** Every composed schema's print this file has recorded: one range read of
|
|
227
|
+
* `server_meta`. */
|
|
228
|
+
export let prints = (driver: Driver): string[] =>
|
|
229
|
+
under(driver, PRINTED).map(([k]) => k.slice(PRINTED.length))
|
|
230
|
+
|
|
231
|
+
/** The prefix of the key a file records each survey of a store's schema under
|
|
232
|
+
* (./survey.ts): `surveyed:<print>`, the store's print. */
|
|
233
|
+
export let SURVEYED = 'surveyed:'
|
|
234
|
+
|
|
235
|
+
/** The survey this file recorded of the store schema `print` names, as it
|
|
236
|
+
* was recorded. */
|
|
237
|
+
export let surveyedAt = (driver: Driver, print: string): string | undefined =>
|
|
238
|
+
kept(driver, SURVEYED + print)
|
|
239
|
+
|
|
240
|
+
/** The prefix of the key a store records each plugin its data requires under:
|
|
241
|
+
* `required:<plugin>`, its value when it was recorded. A host that does not
|
|
242
|
+
* compose every one refuses to open the store (@yaks/host `meet`). A plugin
|
|
243
|
+
* removed on purpose drops its row:
|
|
244
|
+
* `delete from server_meta where k = 'required:@yaks/journal'`. */
|
|
245
|
+
export let REQUIRED = 'required:'
|
|
246
|
+
|
|
247
|
+
/** The plugins this store's data requires: one range read of `server_meta`,
|
|
248
|
+
* and none where the table is not there yet. */
|
|
249
|
+
export let requirements = (driver: Driver): string[] =>
|
|
250
|
+
driver.query({ t: 'pragma', name: 'table_info', arg: META }).length
|
|
251
|
+
? under(driver, REQUIRED).map(([k]) => k.slice(REQUIRED.length))
|
|
252
|
+
: []
|
|
253
|
+
|
|
254
|
+
/** Record that this store's data requires each of `plugins`, from now on. A
|
|
255
|
+
* plugin it already requires keeps the time it was first recorded. */
|
|
256
|
+
export let requires = (driver: Driver, plugins: readonly string[]): void => {
|
|
257
|
+
if (!plugins.length) return
|
|
258
|
+
let at = new Date().toISOString()
|
|
259
|
+
effect(driver, {
|
|
260
|
+
t: 'insert',
|
|
261
|
+
or: 'ignore',
|
|
262
|
+
into: META,
|
|
263
|
+
cols: ['k', 'v'],
|
|
264
|
+
rows: plugins.map((p) => [val(REQUIRED + p), val(at)]),
|
|
265
|
+
})
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// A key's value, read without assuming the table is there yet. The table is
|
|
269
|
+
// asked after by name (`table_info`, empty for one that is not there) rather
|
|
270
|
+
// than found in the whole catalogue, which a store reads on every wake.
|
|
271
|
+
// The drivers whose file has the table: once it has, it keeps it.
|
|
272
|
+
let standing = new WeakSet<Driver>()
|
|
273
|
+
let kept = (driver: Driver, k: string): string | undefined => {
|
|
274
|
+
if (!standing.has(driver)) {
|
|
275
|
+
if (!driver.query({ t: 'pragma', name: 'table_info', arg: META }).length) {
|
|
276
|
+
return
|
|
277
|
+
}
|
|
278
|
+
standing.add(driver)
|
|
279
|
+
}
|
|
280
|
+
return meta(driver).get(k)
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** The key the component tables the file holds are kept under, as their
|
|
284
|
+
* names: what its install found, and each a write raised since (./raise.ts),
|
|
285
|
+
* so a reader knows which tables a store holds from one row rather than a
|
|
286
|
+
* look at its schema. */
|
|
287
|
+
export let TABLES = 'tables'
|
|
288
|
+
|
|
289
|
+
/** The component tables the file holds, as its install and its writes
|
|
290
|
+
* recorded them; undefined for a file no install of this code recorded. */
|
|
291
|
+
export let tablesAt = (driver: Driver): string[] | undefined => {
|
|
292
|
+
let v = kept(driver, TABLES)
|
|
293
|
+
return v == null ? undefined : v ? v.split(' ') : []
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/** The store's epoch where one was minted, read without minting it: what a
|
|
297
|
+
* read asks, where {@link epoch} writes. */
|
|
298
|
+
export let epochAt = (driver: Driver): string | undefined => kept(driver, EPOCH)
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* The store's lineage identity: a string minted once and persisted, so it
|
|
302
|
+
* survives a restart, a deploy and a handover — a returning client whose cursor
|
|
303
|
+
* carries this epoch may resume where it left off, and a client holding another
|
|
304
|
+
* store's epoch (or none) must start over, because rows from a different
|
|
305
|
+
* lineage can never be replayed against it.
|
|
306
|
+
*
|
|
307
|
+
* `install()` mints it, and this is idempotent (`insert or ignore`): a store
|
|
308
|
+
* that already has one keeps it and reads it back. It writes, so a read-only
|
|
309
|
+
* path calls `meta(driver).get(EPOCH)` instead and treats an absent one as a
|
|
310
|
+
* store no cursor can be trusted against.
|
|
311
|
+
*/
|
|
312
|
+
export let epoch = (driver: Driver): string => {
|
|
313
|
+
effect(driver, {
|
|
314
|
+
t: 'insert',
|
|
315
|
+
or: 'ignore',
|
|
316
|
+
into: META,
|
|
317
|
+
cols: ['k', 'v'],
|
|
318
|
+
rows: [[val(EPOCH), val(crypto.randomUUID())]],
|
|
319
|
+
})
|
|
320
|
+
return meta(driver).get(EPOCH)!
|
|
321
|
+
}
|