@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/reader.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// The SELECT half of an installed store. It shares the compiler, component
|
|
2
|
+
// gathers and snapshot rules with storage(), without importing its writers,
|
|
3
|
+
// mutation pipeline, migrations or diagnostics.
|
|
4
|
+
import { absentDerived, derivedOf, revision, storeAs, withoutColumns } from "@yaks/sql";
|
|
5
|
+
import { EPOCH, meta } from "./meta.js";
|
|
6
|
+
import { get, read, rows, tagOf } from "./read.js";
|
|
7
|
+
import { unit } from "./unit.js";
|
|
8
|
+
import { gotten, metered, named } from "./budget.js";
|
|
9
|
+
import { glance, lacked, raisedOver, surveyed } from "./survey.js";
|
|
10
|
+
import { raisedOnly, unraised } from "./raise.js";
|
|
11
|
+
import { bindStorage, identified } from "./shape.js";
|
|
12
|
+
/** Read against the physical schema of this snapshot, not the one that was
|
|
13
|
+
* standing when the command started. Never repair an older vocabulary's
|
|
14
|
+
* dropped columns or indices: another process may have retired them. */ export let options = (driver, vocab, base)=>{
|
|
15
|
+
let tagged;
|
|
16
|
+
let schema;
|
|
17
|
+
let found;
|
|
18
|
+
let layout = vocab;
|
|
19
|
+
// The options over what a look found, and the components no write has
|
|
20
|
+
// raised a table for yet.
|
|
21
|
+
let bound = (seen, unwritten)=>{
|
|
22
|
+
let lacks = lacked(vocab, seen);
|
|
23
|
+
// The file's are owed beside those the base never reads (a mode's tables
|
|
24
|
+
// are relations no index stands on), never in place of them.
|
|
25
|
+
let missingIndexes = seen.unindexed.length && base.missingIndexes?.length ? [
|
|
26
|
+
...new Set([
|
|
27
|
+
...base.missingIndexes,
|
|
28
|
+
...seen.unindexed
|
|
29
|
+
])
|
|
30
|
+
] : seen.unindexed;
|
|
31
|
+
// The registry stays the one every store of the vocabulary shares, so
|
|
32
|
+
// what is compiled through it is too; only the tags are this store's.
|
|
33
|
+
let e = base.backed && meta(driver).get(EPOCH);
|
|
34
|
+
let tags = e ? Object.fromEntries(Object.keys(base.backed).map((c)=>[
|
|
35
|
+
c,
|
|
36
|
+
tagOf(e, c)
|
|
37
|
+
])) : base.tags;
|
|
38
|
+
return absented(vocab, {
|
|
39
|
+
...base,
|
|
40
|
+
...tags ? {
|
|
41
|
+
tags
|
|
42
|
+
} : {},
|
|
43
|
+
derived: seen.lacking.length ? {
|
|
44
|
+
...base.derived,
|
|
45
|
+
...lacks
|
|
46
|
+
} : base.derived,
|
|
47
|
+
...missingIndexes.length ? {
|
|
48
|
+
missingIndexes
|
|
49
|
+
} : {}
|
|
50
|
+
}, [
|
|
51
|
+
...seen.absent,
|
|
52
|
+
...unwritten
|
|
53
|
+
]);
|
|
54
|
+
};
|
|
55
|
+
return ()=>{
|
|
56
|
+
let current = revision(driver, 'schema');
|
|
57
|
+
if (tagged && schema == current) {
|
|
58
|
+
if (driver.stored?.v != layout) storeAs(driver, layout);
|
|
59
|
+
return tagged;
|
|
60
|
+
}
|
|
61
|
+
// The schema is taken as seen only once its look is done: a look that
|
|
62
|
+
// failed (the storage refused a read) is looked again by the next.
|
|
63
|
+
let seen = (options)=>{
|
|
64
|
+
schema = current;
|
|
65
|
+
return tagged = options;
|
|
66
|
+
};
|
|
67
|
+
// A component nothing has written yet has no table (./raise.ts), which
|
|
68
|
+
// the file says without a look at its whole schema.
|
|
69
|
+
let unwritten = unraised(driver, vocab);
|
|
70
|
+
// Where only first writes moved the schema since the last look, what it
|
|
71
|
+
// found stands, but for the tables they made (./survey.ts `raisedOver`).
|
|
72
|
+
if (found && schema !== undefined && raisedOnly(driver, schema, current)) {
|
|
73
|
+
found = raisedOver(driver, found, found.absent);
|
|
74
|
+
return seen(bound(found, unwritten));
|
|
75
|
+
}
|
|
76
|
+
// A file may be changed by another process, and a deferred store owes
|
|
77
|
+
// the indexes its populated tables lack (./owe.ts `build`): neither holds
|
|
78
|
+
// all its installation declares.
|
|
79
|
+
let inspect = !!driver.file || !!base.deferred || schema !== undefined;
|
|
80
|
+
if (!inspect && !base.backed) {
|
|
81
|
+
return seen(unwritten.length ? absented(vocab, base, unwritten) : base);
|
|
82
|
+
}
|
|
83
|
+
// What a writer found when it last looked, where the schema has not
|
|
84
|
+
// moved since (./survey.ts), or else a look at the file now.
|
|
85
|
+
found = inspect ? surveyed(driver, vocab, base.derived) ?? glance(driver, vocab, base.derived) : {
|
|
86
|
+
lacking: [],
|
|
87
|
+
absent: [],
|
|
88
|
+
unindexed: []
|
|
89
|
+
};
|
|
90
|
+
if (identified(vocab) && found.lacking.length) {
|
|
91
|
+
layout = withoutColumns(vocab, found.lacking);
|
|
92
|
+
} else layout = vocab;
|
|
93
|
+
if (driver.stored?.v != layout) storeAs(driver, layout);
|
|
94
|
+
return seen(bound(found, unwritten));
|
|
95
|
+
};
|
|
96
|
+
};
|
|
97
|
+
// `o` reading each of `absent` as a component no entity wears: through the
|
|
98
|
+
// dialect, and through every derived expression that names its table
|
|
99
|
+
// (@yaks/sql `absentDerived`).
|
|
100
|
+
let absented = (vocab, o, absent)=>{
|
|
101
|
+
let all = [
|
|
102
|
+
...new Set([
|
|
103
|
+
...o.absent ?? [],
|
|
104
|
+
...absent
|
|
105
|
+
])
|
|
106
|
+
];
|
|
107
|
+
if (!all.length) return o;
|
|
108
|
+
// The vocabulary's own (a status ladder's rungs) among them.
|
|
109
|
+
return {
|
|
110
|
+
...o,
|
|
111
|
+
absent: all,
|
|
112
|
+
derived: absentDerived(vocab, derivedOf(vocab, o.derived), all)
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
/** Read an already-installed database. No schema is created or repaired, and
|
|
116
|
+
* no mutation methods are exposed. Each operation sees one committed snapshot. */ export let reader = (driver, vocab, base = {})=>{
|
|
117
|
+
vocab = bindStorage(driver, vocab);
|
|
118
|
+
let opts = options(driver, vocab, base);
|
|
119
|
+
// Each read is refused past what one read may hold (./budget.ts).
|
|
120
|
+
return {
|
|
121
|
+
read: (q, o, comps)=>metered(()=>unit(driver, ()=>read(driver, vocab, q, {
|
|
122
|
+
...opts(),
|
|
123
|
+
...o
|
|
124
|
+
}, comps), 'read'), {
|
|
125
|
+
what: named(q)
|
|
126
|
+
}),
|
|
127
|
+
rows: (q, o)=>metered(()=>rows(driver, vocab, q, {
|
|
128
|
+
...opts(),
|
|
129
|
+
...o
|
|
130
|
+
}), {
|
|
131
|
+
what: named(q)
|
|
132
|
+
}),
|
|
133
|
+
get: (ids, comps, o)=>ids.length ? metered(()=>unit(driver, ()=>get(driver, vocab, ids, {
|
|
134
|
+
...opts(),
|
|
135
|
+
...o
|
|
136
|
+
}, comps), 'read'), {
|
|
137
|
+
what: gotten(ids)
|
|
138
|
+
}) : []
|
|
139
|
+
};
|
|
140
|
+
};
|
|
141
|
+
//# sourceMappingURL=reader.js.map
|
package/reader.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reader.js","sources":["./reader.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,4EAA4E;AAC5E,gDAAgD;AAChD,SACE,aAAa,EAEb,SAAS,EAET,QAAQ,EACR,OAAO,EACP,cAAc,yBACa;AAG7B,SAAS,KAAK,EAAE,IAAI,oBAAmB;AACvC,SAAS,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,oBAAmB;AAClD,SAAS,IAAI,oBAAmB;AAChC,SAAS,MAAM,EAAE,OAAO,EAAE,KAAK,sBAAqB;AACpD,SAAS,MAAM,EAAE,MAAM,EAAE,UAAU,EAAe,QAAQ,sBAAqB;AAC/E,SAAS,UAAU,EAAE,QAAQ,qBAAoB;AACjD,SAAS,WAAW,EAAE,UAAU,qBAAoB;AAEpD;;sEAEsE,GACtE,OAAO,IAAI,UAAU,CACnB,QACA,OACA;EAEA,IAAI;EACJ,IAAI;EACJ,IAAI;EACJ,IAAI,SAAS;EACb,sEAAsE;EACtE,0BAA0B;EAC1B,IAAI,QAAQ,CAAC,MAAgC;IAC3C,IAAI,QAAQ,OAAO,OAAO;IAC1B,yEAAyE;IACzE,6DAA6D;IAC7D,IAAI,iBAAiB,KAAK,SAAS,CAAC,MAAM,IAAI,KAAK,cAAc,EAAE,SAC/D;SAAI,IAAI,IAAI;WAAI,KAAK,cAAc;WAAK,KAAK,SAAS;OAAC;KAAE,GACzD,KAAK,SAAS;IAClB,sEAAsE;IACtE,sEAAsE;IACtE,IAAI,IAAI,KAAK,MAAM,IAAI,KAAK,QAAQ,GAAG,CAAC;IACxC,IAAI,OAAO,IACP,OAAO,WAAW,CAClB,OAAO,IAAI,CAAC,KAAK,MAAM,EAAG,GAAG,CAAC,CAA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,EAAE;EACV;AACF,EAAC"}
|
package/reader.ts
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
// The SELECT half of an installed store. It shares the compiler, component
|
|
2
|
+
// gathers and snapshot rules with storage(), without importing its writers,
|
|
3
|
+
// mutation pipeline, migrations or diagnostics.
|
|
4
|
+
import {
|
|
5
|
+
absentDerived,
|
|
6
|
+
type BindOpts,
|
|
7
|
+
derivedOf,
|
|
8
|
+
type Driver,
|
|
9
|
+
revision,
|
|
10
|
+
storeAs,
|
|
11
|
+
withoutColumns,
|
|
12
|
+
} from '@yaks/sql'
|
|
13
|
+
import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
|
|
14
|
+
import type { Store } from './mod.ts'
|
|
15
|
+
import { EPOCH, meta } from './meta.js'
|
|
16
|
+
import { get, read, rows, tagOf } from './read.js'
|
|
17
|
+
import { unit } from './unit.js'
|
|
18
|
+
import { gotten, metered, named } from './budget.js'
|
|
19
|
+
import { glance, lacked, raisedOver, type Survey, surveyed } from './survey.js'
|
|
20
|
+
import { raisedOnly, unraised } from './raise.js'
|
|
21
|
+
import { bindStorage, identified } from './shape.js'
|
|
22
|
+
|
|
23
|
+
/** Read against the physical schema of this snapshot, not the one that was
|
|
24
|
+
* standing when the command started. Never repair an older vocabulary's
|
|
25
|
+
* dropped columns or indices: another process may have retired them. */
|
|
26
|
+
export let options = <T extends BindOpts & { deferred?: boolean }>(
|
|
27
|
+
driver: Driver,
|
|
28
|
+
vocab: Vocab,
|
|
29
|
+
base: T,
|
|
30
|
+
): () => T => {
|
|
31
|
+
let tagged: T | undefined
|
|
32
|
+
let schema: number | undefined
|
|
33
|
+
let found: Omit<Survey, 'unlogged'> | undefined
|
|
34
|
+
let layout = vocab
|
|
35
|
+
// The options over what a look found, and the components no write has
|
|
36
|
+
// raised a table for yet.
|
|
37
|
+
let bound = (seen: Omit<Survey, 'unlogged'>, unwritten: string[]): T => {
|
|
38
|
+
let lacks = lacked(vocab, seen)
|
|
39
|
+
// The file's are owed beside those the base never reads (a mode's tables
|
|
40
|
+
// are relations no index stands on), never in place of them.
|
|
41
|
+
let missingIndexes = seen.unindexed.length && base.missingIndexes?.length
|
|
42
|
+
? [...new Set([...base.missingIndexes, ...seen.unindexed])]
|
|
43
|
+
: seen.unindexed
|
|
44
|
+
// The registry stays the one every store of the vocabulary shares, so
|
|
45
|
+
// what is compiled through it is too; only the tags are this store's.
|
|
46
|
+
let e = base.backed && meta(driver).get(EPOCH)
|
|
47
|
+
let tags = e
|
|
48
|
+
? Object.fromEntries(
|
|
49
|
+
Object.keys(base.backed!).map((c) => [c, tagOf(e, c)]),
|
|
50
|
+
)
|
|
51
|
+
: base.tags
|
|
52
|
+
return absented(vocab, {
|
|
53
|
+
...base,
|
|
54
|
+
...tags ? { tags } : {},
|
|
55
|
+
derived: seen.lacking.length
|
|
56
|
+
? { ...base.derived, ...lacks }
|
|
57
|
+
: base.derived,
|
|
58
|
+
...missingIndexes.length ? { missingIndexes } : {},
|
|
59
|
+
}, [...seen.absent, ...unwritten])
|
|
60
|
+
}
|
|
61
|
+
return () => {
|
|
62
|
+
let current = revision(driver, 'schema')
|
|
63
|
+
if (tagged && schema == current) {
|
|
64
|
+
if (driver.stored?.v != layout) storeAs(driver, layout)
|
|
65
|
+
return tagged
|
|
66
|
+
}
|
|
67
|
+
// The schema is taken as seen only once its look is done: a look that
|
|
68
|
+
// failed (the storage refused a read) is looked again by the next.
|
|
69
|
+
let seen = (options: T) => {
|
|
70
|
+
schema = current
|
|
71
|
+
return tagged = options
|
|
72
|
+
}
|
|
73
|
+
// A component nothing has written yet has no table (./raise.ts), which
|
|
74
|
+
// the file says without a look at its whole schema.
|
|
75
|
+
let unwritten = unraised(driver, vocab)
|
|
76
|
+
// Where only first writes moved the schema since the last look, what it
|
|
77
|
+
// found stands, but for the tables they made (./survey.ts `raisedOver`).
|
|
78
|
+
if (found && schema !== undefined && raisedOnly(driver, schema, current)) {
|
|
79
|
+
found = raisedOver(driver, found, found.absent)
|
|
80
|
+
return seen(bound(found, unwritten))
|
|
81
|
+
}
|
|
82
|
+
// A file may be changed by another process, and a deferred store owes
|
|
83
|
+
// the indexes its populated tables lack (./owe.ts `build`): neither holds
|
|
84
|
+
// all its installation declares.
|
|
85
|
+
let inspect = !!driver.file || !!base.deferred || schema !== undefined
|
|
86
|
+
if (!inspect && !base.backed) {
|
|
87
|
+
return seen(unwritten.length ? absented(vocab, base, unwritten) : base)
|
|
88
|
+
}
|
|
89
|
+
// What a writer found when it last looked, where the schema has not
|
|
90
|
+
// moved since (./survey.ts), or else a look at the file now.
|
|
91
|
+
found = inspect
|
|
92
|
+
? surveyed(driver, vocab, base.derived) ??
|
|
93
|
+
glance(driver, vocab, base.derived)
|
|
94
|
+
: { lacking: [], absent: [], unindexed: [] }
|
|
95
|
+
if (identified(vocab) && found.lacking.length) {
|
|
96
|
+
layout = withoutColumns(vocab, found.lacking)
|
|
97
|
+
} else layout = vocab
|
|
98
|
+
if (driver.stored?.v != layout) storeAs(driver, layout)
|
|
99
|
+
return seen(bound(found, unwritten))
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// `o` reading each of `absent` as a component no entity wears: through the
|
|
104
|
+
// dialect, and through every derived expression that names its table
|
|
105
|
+
// (@yaks/sql `absentDerived`).
|
|
106
|
+
let absented = <T extends BindOpts>(
|
|
107
|
+
vocab: Vocab,
|
|
108
|
+
o: T,
|
|
109
|
+
absent: string[],
|
|
110
|
+
): T => {
|
|
111
|
+
let all = [...new Set([...o.absent ?? [], ...absent])]
|
|
112
|
+
if (!all.length) return o
|
|
113
|
+
// The vocabulary's own (a status ladder's rungs) among them.
|
|
114
|
+
return {
|
|
115
|
+
...o,
|
|
116
|
+
absent: all,
|
|
117
|
+
derived: absentDerived(vocab, derivedOf(vocab, o.derived), all),
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Read an already-installed database. No schema is created or repaired, and
|
|
122
|
+
* no mutation methods are exposed. Each operation sees one committed snapshot. */
|
|
123
|
+
export let reader = (
|
|
124
|
+
driver: Driver,
|
|
125
|
+
vocab: Vocab,
|
|
126
|
+
base: BindOpts = {},
|
|
127
|
+
): Pick<Store, 'read' | 'rows' | 'get'> => {
|
|
128
|
+
vocab = bindStorage(driver, vocab)
|
|
129
|
+
let opts = options(driver, vocab, base)
|
|
130
|
+
// Each read is refused past what one read may hold (./budget.ts).
|
|
131
|
+
return {
|
|
132
|
+
read: (q, o, comps) =>
|
|
133
|
+
metered(
|
|
134
|
+
() =>
|
|
135
|
+
unit(
|
|
136
|
+
driver,
|
|
137
|
+
() => read(driver, vocab, q, { ...opts(), ...o }, comps),
|
|
138
|
+
'read',
|
|
139
|
+
),
|
|
140
|
+
{ what: named(q) },
|
|
141
|
+
),
|
|
142
|
+
rows: (q, o) =>
|
|
143
|
+
metered(() => rows(driver, vocab, q, { ...opts(), ...o }), {
|
|
144
|
+
what: named(q),
|
|
145
|
+
}),
|
|
146
|
+
get: (ids, comps, o) =>
|
|
147
|
+
ids.length
|
|
148
|
+
? metered(
|
|
149
|
+
() =>
|
|
150
|
+
unit(
|
|
151
|
+
driver,
|
|
152
|
+
() => get(driver, vocab, ids, { ...opts(), ...o }, comps),
|
|
153
|
+
'read',
|
|
154
|
+
),
|
|
155
|
+
{ what: gotten(ids) },
|
|
156
|
+
)
|
|
157
|
+
: [],
|
|
158
|
+
}
|
|
159
|
+
}
|
package/rules.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// Declared rules, evaluated. Each flat match or collection level compiles to
|
|
2
|
+
// one statement (@yaks/sql `rule`); this reads its rows back as bindings.
|
|
3
|
+
//
|
|
4
|
+
// Run it against a batch overlay (./overlay.ts) and the same statement reads
|
|
5
|
+
// the graph with the batch in it. That is the whole of "rules run before
|
|
6
|
+
// persistence": no flag, no second code path, a different set of tables
|
|
7
|
+
// underneath — the overlay is a `with` prefix of CTEs, and each covered
|
|
8
|
+
// component's name points at its CTE, so what changes is a name.
|
|
9
|
+
import { collected } from "@yaks/graph/declared";
|
|
10
|
+
import { rule } from "@yaks/sql";
|
|
11
|
+
import { overlay } from "./overlay.js";
|
|
12
|
+
import { readingVocab } from "./shape.js";
|
|
13
|
+
/**
|
|
14
|
+
* Run a compiled match and read its rows back as bindings. The statement is
|
|
15
|
+
* the one above; what makes it report on a batch is the overlay under it
|
|
16
|
+
* (./overlay.ts), not anything here.
|
|
17
|
+
*/ export let matched = (driver, m, vocab, opts = {}, on = {}, over = [])=>// The outer match is anchored to a changed batch; a collection reads every
|
|
18
|
+
// member belonging to that outer binding, including members the batch did
|
|
19
|
+
// not touch.
|
|
20
|
+
collected(m, (plan, anchored)=>{
|
|
21
|
+
let statement = rule({
|
|
22
|
+
patterns: plan.patterns
|
|
23
|
+
}, readingVocab(driver, vocab), opts, anchored ? on : {
|
|
24
|
+
...on,
|
|
25
|
+
touched: undefined
|
|
26
|
+
});
|
|
27
|
+
// The statement is not `distinct` (@yaks/sql match.ts), so a binding it
|
|
28
|
+
// finds twice is kept once here.
|
|
29
|
+
let seen = new Set();
|
|
30
|
+
return driver.query({
|
|
31
|
+
...statement,
|
|
32
|
+
with: [
|
|
33
|
+
...over,
|
|
34
|
+
...statement.with ?? []
|
|
35
|
+
]
|
|
36
|
+
}).flatMap((row)=>{
|
|
37
|
+
let key = JSON.stringify(row);
|
|
38
|
+
if (seen.has(key)) return [];
|
|
39
|
+
seen.add(key);
|
|
40
|
+
return [
|
|
41
|
+
{
|
|
42
|
+
entities: plan.patterns.map((p, i)=>p.makes ? null : String(row[`e${i}`])),
|
|
43
|
+
vars: Object.fromEntries(plan.vars.map((name)=>[
|
|
44
|
+
name,
|
|
45
|
+
row[`v_${name}`]
|
|
46
|
+
]))
|
|
47
|
+
}
|
|
48
|
+
];
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
/**
|
|
52
|
+
* How declared rules are evaluated (@yaks/graph `Tx.bindings`): run every match
|
|
53
|
+
* against this graph with `batch` folded in.
|
|
54
|
+
*
|
|
55
|
+
* One overlay for the whole set — `covers` is what the rules read — and then
|
|
56
|
+
* one statement per flat match or collection level under it. The overlay is a
|
|
57
|
+
* `with` prefix, so it costs
|
|
58
|
+
* nothing to build and nothing to tear down: every statement here simply
|
|
59
|
+
* carries it.
|
|
60
|
+
*/ export let bindings = (driver, vocab, matches, bundles, covers, opts = {})=>{
|
|
61
|
+
if (!matches.length) return [];
|
|
62
|
+
let over = overlay(driver, vocab, bundles, covers);
|
|
63
|
+
// A component the batch writes reads through its overlay, which already
|
|
64
|
+
// answers for a table no write has raised; only the rest read as absent.
|
|
65
|
+
if (opts.absent) {
|
|
66
|
+
opts = {
|
|
67
|
+
...opts,
|
|
68
|
+
absent: opts.absent.filter((c)=>!over.covers.includes(c))
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
// What the batch is about, as the ids the overlay speaks in: the anchor
|
|
72
|
+
// every match is narrowed to, so a rule asks about this batch rather than
|
|
73
|
+
// about the file. With no batch there is nothing to be about and nothing to
|
|
74
|
+
// anchor to: the caller is asking the match outright, which is what a
|
|
75
|
+
// template invocation is.
|
|
76
|
+
let touched = bundles.length ? [
|
|
77
|
+
...new Set(bundles.map((b)=>over.ids.get(b.entity.eid)))
|
|
78
|
+
].filter((id)=>id !== undefined) : undefined;
|
|
79
|
+
let on = {
|
|
80
|
+
at: over.at,
|
|
81
|
+
owned: over.owned,
|
|
82
|
+
referring: over.referring,
|
|
83
|
+
gone: over.gone,
|
|
84
|
+
touched
|
|
85
|
+
};
|
|
86
|
+
return matches.map((m)=>matched(driver, m, vocab, opts, on, over.with));
|
|
87
|
+
};
|
|
88
|
+
//# sourceMappingURL=rules.js.map
|
package/rules.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rules.js","sources":["./rules.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,0EAA0E;AAC1E,EAAE;AACF,6EAA6E;AAC7E,yEAAyE;AACzE,wEAAwE;AACxE,wEAAw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AAQ,OAAO,SAAS;EAC3C,wEAAwE;EACxE,yEAAyE;EACzE,IAAI,KAAK,MAAM,EAAE;IACf,OAAO;MACL,GAAG,IAAI;MACP,QAAQ,KAAK,MAAM,CAAC,MAAM,CAAC,CAAC,IAAM,CAAC,KAAK,MAAM,CAAC,QAAQ,CAAC;IAC1D;EACF;EACA,wEAAwE;EACxE,0EAA0E;EAC1E,4EAA4E;EAC5E,sEAAsE;EACtE,0BAA0B;EAC1B,IAAI,UAAU,QAAQ,MAAM,GACxB;OAAI,IAAI,IAAI,QAAQ,GAAG,CAAC,CAAC,IAAM,KAAK,GAAG,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC,GAAG;GAAK,CAC5D,MAAM,CAAC,CAAC,KAAO,OAAO,aACvB;EACJ,IAAI,KAAK;IACP,IAAI,KAAK,EAAE;IACX,OAAO,KAAK,KAAK;IACjB,WAAW,KAAK,SAAS;IACzB,MAAM,KAAK,IAAI;IACf;EACF;EACA,OAAO,QAAQ,GAAG,CAAC,CAAC,IAAM,QAAQ,QAAQ,GAAG,OAAO,MAAM,IAAI,KAAK,IAAI;AACzE,EAAC"}
|
package/rules.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// Declared rules, evaluated. Each flat match or collection level compiles to
|
|
2
|
+
// one statement (@yaks/sql `rule`); this reads its rows back as bindings.
|
|
3
|
+
//
|
|
4
|
+
// Run it against a batch overlay (./overlay.ts) and the same statement reads
|
|
5
|
+
// the graph with the batch in it. That is the whole of "rules run before
|
|
6
|
+
// persistence": no flag, no second code path, a different set of tables
|
|
7
|
+
// underneath — the overlay is a `with` prefix of CTEs, and each covered
|
|
8
|
+
// component's name points at its CTE, so what changes is a name.
|
|
9
|
+
|
|
10
|
+
import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
|
|
11
|
+
import type { Binding, Bundle, Match } from 'jsr:@yaks/graph@0.2.36'
|
|
12
|
+
import { collected } from '@yaks/graph/declared'
|
|
13
|
+
import {
|
|
14
|
+
type BindOpts,
|
|
15
|
+
type Cte,
|
|
16
|
+
type Driver,
|
|
17
|
+
type On,
|
|
18
|
+
type Row,
|
|
19
|
+
rule,
|
|
20
|
+
} from '@yaks/sql'
|
|
21
|
+
import { overlay } from './overlay.js'
|
|
22
|
+
import { readingVocab } from './shape.js'
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Run a compiled match and read its rows back as bindings. The statement is
|
|
26
|
+
* the one above; what makes it report on a batch is the overlay under it
|
|
27
|
+
* (./overlay.ts), not anything here.
|
|
28
|
+
*/
|
|
29
|
+
export let matched = (
|
|
30
|
+
driver: Driver,
|
|
31
|
+
m: Match,
|
|
32
|
+
vocab: Vocab,
|
|
33
|
+
opts: BindOpts = {},
|
|
34
|
+
on: On = {},
|
|
35
|
+
over: Cte[] = [],
|
|
36
|
+
): Binding[] =>
|
|
37
|
+
// The outer match is anchored to a changed batch; a collection reads every
|
|
38
|
+
// member belonging to that outer binding, including members the batch did
|
|
39
|
+
// not touch.
|
|
40
|
+
collected(m, (plan, anchored) => {
|
|
41
|
+
let statement = rule(
|
|
42
|
+
{ patterns: plan.patterns },
|
|
43
|
+
readingVocab(driver, vocab),
|
|
44
|
+
opts,
|
|
45
|
+
anchored ? on : { ...on, touched: undefined },
|
|
46
|
+
)
|
|
47
|
+
// The statement is not `distinct` (@yaks/sql match.ts), so a binding it
|
|
48
|
+
// finds twice is kept once here.
|
|
49
|
+
let seen = new Set<string>()
|
|
50
|
+
return driver.query({
|
|
51
|
+
...statement,
|
|
52
|
+
with: [...over, ...statement.with ?? []],
|
|
53
|
+
}).flatMap((row: Row) => {
|
|
54
|
+
let key = JSON.stringify(row)
|
|
55
|
+
if (seen.has(key)) return []
|
|
56
|
+
seen.add(key)
|
|
57
|
+
return [{
|
|
58
|
+
entities: plan.patterns.map((p, i) =>
|
|
59
|
+
p.makes ? null : String(row[`e${i}`])
|
|
60
|
+
),
|
|
61
|
+
vars: Object.fromEntries(
|
|
62
|
+
plan.vars.map((name) => [name, row[`v_${name}`]]),
|
|
63
|
+
),
|
|
64
|
+
}]
|
|
65
|
+
})
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* How declared rules are evaluated (@yaks/graph `Tx.bindings`): run every match
|
|
70
|
+
* against this graph with `batch` folded in.
|
|
71
|
+
*
|
|
72
|
+
* One overlay for the whole set — `covers` is what the rules read — and then
|
|
73
|
+
* one statement per flat match or collection level under it. The overlay is a
|
|
74
|
+
* `with` prefix, so it costs
|
|
75
|
+
* nothing to build and nothing to tear down: every statement here simply
|
|
76
|
+
* carries it.
|
|
77
|
+
*/
|
|
78
|
+
export let bindings = (
|
|
79
|
+
driver: Driver,
|
|
80
|
+
vocab: Vocab,
|
|
81
|
+
matches: Match[],
|
|
82
|
+
bundles: Bundle[],
|
|
83
|
+
covers: string[],
|
|
84
|
+
opts: BindOpts = {},
|
|
85
|
+
): Binding[][] => {
|
|
86
|
+
if (!matches.length) return []
|
|
87
|
+
let over = overlay(driver, vocab, bundles, covers)
|
|
88
|
+
// A component the batch writes reads through its overlay, which already
|
|
89
|
+
// answers for a table no write has raised; only the rest read as absent.
|
|
90
|
+
if (opts.absent) {
|
|
91
|
+
opts = {
|
|
92
|
+
...opts,
|
|
93
|
+
absent: opts.absent.filter((c) => !over.covers.includes(c)),
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
// What the batch is about, as the ids the overlay speaks in: the anchor
|
|
97
|
+
// every match is narrowed to, so a rule asks about this batch rather than
|
|
98
|
+
// about the file. With no batch there is nothing to be about and nothing to
|
|
99
|
+
// anchor to: the caller is asking the match outright, which is what a
|
|
100
|
+
// template invocation is.
|
|
101
|
+
let touched = bundles.length
|
|
102
|
+
? [...new Set(bundles.map((b) => over.ids.get(b.entity.eid)!))]
|
|
103
|
+
.filter((id) => id !== undefined)
|
|
104
|
+
: undefined
|
|
105
|
+
let on = {
|
|
106
|
+
at: over.at,
|
|
107
|
+
owned: over.owned,
|
|
108
|
+
referring: over.referring,
|
|
109
|
+
gone: over.gone,
|
|
110
|
+
touched,
|
|
111
|
+
}
|
|
112
|
+
return matches.map((m) => matched(driver, m, vocab, opts, on, over.with))
|
|
113
|
+
}
|
package/service.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// Rebuildable database caches are compacted on their existing inode, so live
|
|
2
|
+
// processes cannot keep a removed cache growing outside its budget. Each trim
|
|
3
|
+
// runs on a thread of its own, so a stalled disk never holds the host's.
|
|
4
|
+
import { sleep } from "@yaks/fp";
|
|
5
|
+
import { threaded } from "./cache.js";
|
|
6
|
+
/** Maintain configured caches until the host stops. Each failure reaches the
|
|
7
|
+
* host's telemetry and leaves the next pass able to try again. */ export let service = async (host, options = {}, signal = AbortSignal.abort(), maintain = threaded)=>{
|
|
8
|
+
for(;;){
|
|
9
|
+
for (let [path, cache] of Object.entries(options.caches ?? {})){
|
|
10
|
+
try {
|
|
11
|
+
await maintain(path, cache);
|
|
12
|
+
} catch (error) {
|
|
13
|
+
;
|
|
14
|
+
(host.report ?? console.error)(error, {
|
|
15
|
+
tags: {
|
|
16
|
+
step: 'cache trim',
|
|
17
|
+
path
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
if (signal.aborted) return;
|
|
23
|
+
await sleep(options.every ?? 60 * 60 * 1000, signal);
|
|
24
|
+
if (signal.aborted) return;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
//# sourceMappingURL=service.js.map
|
package/service.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service.js","sources":["./service.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,8EAA8E;AAC9E,yEAAyE;AACzE,SAAS,KAAK,wBAA6B;AAC3C,SAAqB,QAAQ,qBAA0B;AASvD;gEACgE,GAChE,OAAO,IAAI,UAAU,OACnB,MAMA,UAAmB,CAAC,CAAC,EACrB,SAAsB,YAAY,KAAK,EAAE,EACzC,WAAwB,QAAQ;EAEhC,OAAS;IACP,KAAK,IAAI,CAAC,MAAM,MAAM,IAAI,OAAO,OAAO,CAAC,QAAQ,MAAM,IAAI,CAAC,GAAI;MAC9D,IAAI;QACF,MAAM,SAAS,MAAM;MACvB,EAAE,OAAO,OAAO;;QACb,CAAC,KAAK,MAAM,IAAI,QAAQ,KAAK,EAAE,OAAO;UACrC,MAAM;YAAE,MAAM;YAAc;UAAK;QACnC;MACF;IACF;IACA,IAAI,OAAO,OAAO,EAAE;IACpB,MAAM,MAAM,QAAQ,KAAK,IAAI,KAAK,KAAK,MAAM;IAC7C,IAAI,OAAO,OAAO,EAAE;EACtB;AACF,EAAC"}
|
package/service.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// Rebuildable database caches are compacted on their existing inode, so live
|
|
2
|
+
// processes cannot keep a removed cache growing outside its budget. Each trim
|
|
3
|
+
// runs on a thread of its own, so a stalled disk never holds the host's.
|
|
4
|
+
import { sleep } from '@yaks/fp'
|
|
5
|
+
import { type Cache, threaded, trim } from './cache.js'
|
|
6
|
+
|
|
7
|
+
export type Options = {
|
|
8
|
+
/** Database paths, each with its byte budget and table-to-file-URL-column map. */
|
|
9
|
+
caches?: Record<string, Cache>
|
|
10
|
+
/** Milliseconds between passes; defaults to one hour. */
|
|
11
|
+
every?: number
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Maintain configured caches until the host stops. Each failure reaches the
|
|
15
|
+
* host's telemetry and leaves the next pass able to try again. */
|
|
16
|
+
export let service = async (
|
|
17
|
+
host: {
|
|
18
|
+
report?: (
|
|
19
|
+
error: unknown,
|
|
20
|
+
context?: { tags?: Record<string, string> },
|
|
21
|
+
) => unknown
|
|
22
|
+
},
|
|
23
|
+
options: Options = {},
|
|
24
|
+
signal: AbortSignal = AbortSignal.abort(),
|
|
25
|
+
maintain: typeof trim = threaded,
|
|
26
|
+
): Promise<void> => {
|
|
27
|
+
for (;;) {
|
|
28
|
+
for (let [path, cache] of Object.entries(options.caches ?? {})) {
|
|
29
|
+
try {
|
|
30
|
+
await maintain(path, cache)
|
|
31
|
+
} catch (error) {
|
|
32
|
+
;(host.report ?? console.error)(error, {
|
|
33
|
+
tags: { step: 'cache trim', path },
|
|
34
|
+
})
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (signal.aborted) return
|
|
38
|
+
await sleep(options.every ?? 60 * 60 * 1000, signal)
|
|
39
|
+
if (signal.aborted) return
|
|
40
|
+
}
|
|
41
|
+
}
|
package/shape.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// A file's layout of its words. A component's table, its columns, indexes and
|
|
2
|
+
// views are named by the word's identity (@yaks/vocab `Vocab.stored`), so a
|
|
3
|
+
// rename moves nothing in storage. A file laid out before identities names
|
|
4
|
+
// them by label, and is read that way until it migrates.
|
|
5
|
+
import { labelStored } from "@yaks/vocab";
|
|
6
|
+
import { columnAsWritten, fullLayout, queryPhysical, relating, storeAs, stored } from "@yaks/sql/core";
|
|
7
|
+
import { storedTables } from "./physical.js";
|
|
8
|
+
import { BESIDE, BRANCH, FROM, TO } from "./history-shapes.js";
|
|
9
|
+
import { BUILDING } from "./layout.js";
|
|
10
|
+
/** The vocabulary as a database keeps its words. An expand release keeps
|
|
11
|
+
* an existing label layout until the migration moves it in one transaction. */ export let layoutOf = (v, tables)=>tables.some((name)=>name != 'entity' && !!v.comp(name) && v.stored(name) != name) ? labelStored(v) : v;
|
|
12
|
+
/** The columns a word's table holds in this file, by label, or none where
|
|
13
|
+
* the file never raised it. `v` declares the word, though the connection may
|
|
14
|
+
* be bound to words that do not (a retired word a move still reads): its
|
|
15
|
+
* table is found by the word's identity, or by its label in a file laid out
|
|
16
|
+
* before identities. */ export let wordColumns = (driver, v, comp)=>{
|
|
17
|
+
let layout = layoutOf(v, storedTables(driver));
|
|
18
|
+
return queryPhysical(driver, {
|
|
19
|
+
t: 'pragma',
|
|
20
|
+
name: 'table_info',
|
|
21
|
+
arg: stored(layout, comp)
|
|
22
|
+
}).map((c)=>columnAsWritten(layout, comp, String(c.name)));
|
|
23
|
+
};
|
|
24
|
+
/** Bind the vocabulary for all statements on this connection. */ export let bindStorage = (driver, v)=>{
|
|
25
|
+
let layout = v;
|
|
26
|
+
if (identified(v)) {
|
|
27
|
+
let current = driver.stored?.v;
|
|
28
|
+
let primary = historyLayout(v);
|
|
29
|
+
let labels = labelStored(v);
|
|
30
|
+
let known = current && fullLayout(current);
|
|
31
|
+
layout = known == primary || known == labels ? known : historyLayout(layoutOf(v, storedTables(driver)));
|
|
32
|
+
}
|
|
33
|
+
storeAs(driver, layout);
|
|
34
|
+
return layout;
|
|
35
|
+
};
|
|
36
|
+
/** Whether a vocabulary can distinguish the two layouts. */ export let identified = (v)=>{
|
|
37
|
+
let held = identities.get(v);
|
|
38
|
+
if (held == null) {
|
|
39
|
+
identities.set(v, held = v.all.some((c)=>v.stored(c) != c));
|
|
40
|
+
}
|
|
41
|
+
return held;
|
|
42
|
+
};
|
|
43
|
+
let identities = new WeakMap();
|
|
44
|
+
let histories = new WeakMap();
|
|
45
|
+
let historyLayout = (v)=>{
|
|
46
|
+
if (!identified(v)) return v;
|
|
47
|
+
let held = histories.get(v);
|
|
48
|
+
if (!held) {
|
|
49
|
+
held = relating(v, v.all.flatMap((comp)=>v.comp(comp)?.computed || comp == 'entity' || comp == 'tombstone' ? [] : [
|
|
50
|
+
{
|
|
51
|
+
name: comp + BESIDE,
|
|
52
|
+
comp,
|
|
53
|
+
suffix: BESIDE,
|
|
54
|
+
columns: [
|
|
55
|
+
BRANCH,
|
|
56
|
+
FROM,
|
|
57
|
+
TO
|
|
58
|
+
]
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
name: BUILDING + comp,
|
|
62
|
+
comp,
|
|
63
|
+
prefix: BUILDING
|
|
64
|
+
}
|
|
65
|
+
]));
|
|
66
|
+
histories.set(v, held);
|
|
67
|
+
histories.set(held, held);
|
|
68
|
+
}
|
|
69
|
+
return held;
|
|
70
|
+
};
|
|
71
|
+
/** Compiled reads on a mode name the relations the mode supplies. */ export let readingVocab = (driver, layout)=>{
|
|
72
|
+
if (driver.views) return labelStored(layout);
|
|
73
|
+
let known = driver.stored?.v;
|
|
74
|
+
return known && fullLayout(known) == layout ? known : layout;
|
|
75
|
+
};
|
|
76
|
+
//# sourceMappingURL=shape.js.map
|
package/shape.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shape.js","sources":["./shape.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,4EAA4E;AAC5E,2EAA2E;AAC3E,yDAAyD;AAEzD,SAAS,WAAW,2BAA4C;AAChE,SACE,eAAe,EAEf,UAAU,EACV,aAAa,EACb,QAAQ,EACR,OAAO,EACP,MAAM,8BAC0B;AAClC,SAAS,YAAY,wBAAuB;AAC5C,SAAS,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE,8BAA6B;AAC9D,SAAS,QAAQ,sBAAqB;AAEtC;6EAC6E,GAC7E,OAAO,IAAI,WAAW,CAAC,GAAU,SAC/B,OAAO,IAAI,CAAC,CAAC,OACT,QAAQ,YAAY,CAAC,CAAC,EAAE,IAAI,CAAC,SAAS,EAAE,MAAM,CAAC,SAAS,QAExD,YAAY,KACZ,EAAC;AAEP;;;;sBAIsB,GACtB,OAAO,IAAI,cAAc,CACvB,QACA,GACA;EAEA,IAAI,SAAS,SAAS,GAAG,aAAa;EACtC,OAAO,cAAc,QAAQ;IAC3B,GAAG;IACH,MAAM;IACN,KAAK,OAAO,QAAQ;EACtB,GAAG,GAAG,CAAC,CAAC,IAAM,gBAAgB,QAAQ,MAAM,OAAO,EAAE,IAAI;AAC3D,EAAC;AAED,+DAA+D,GAC/D,OAAO,IAAI,cAAc,CAAC,QAAgB;EACxC,IAAI,SAAS;EACb,IAAI,WAAW,IAAI;IACjB,IAAI,UAAU,OAAO,MAAM,EAAE;IAC7B,IAAI,UAAU,cAAc;IAC5B,IAAI,SAAS,YAAY;IACz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