@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/archetype.js
ADDED
|
@@ -0,0 +1,730 @@
|
|
|
1
|
+
import { sha256 } from "@yaks/graph/sha256";
|
|
2
|
+
import { TOMBSTONE } from "@yaks/graph/bundle";
|
|
3
|
+
import { descriptorOf } from "@yaks/archetype/sets";
|
|
4
|
+
import { and, as, col, cross, eq, fn, gt, isNull, le, left, lit, oneOf, select, table, val } from "@yaks/sql";
|
|
5
|
+
import { componentTables } from "./physical.js";
|
|
6
|
+
import { descriptor, setsFor, standing } from "./catalog.js";
|
|
7
|
+
import { pointerIndex } from "./ddl.js";
|
|
8
|
+
import { catalogue } from "@yaks/sql";
|
|
9
|
+
import { mintSql } from "./write.js";
|
|
10
|
+
import { unit } from "./unit.js";
|
|
11
|
+
import { revision } from "@yaks/sql";
|
|
12
|
+
import { Unpointed } from "./error.js";
|
|
13
|
+
import { ledger } from "./ledger.js";
|
|
14
|
+
export { ledger } from "./ledger.js";
|
|
15
|
+
export { componentTables } from "./physical.js";
|
|
16
|
+
// A value read the first time it is asked for, and kept.
|
|
17
|
+
let once = (read)=>{
|
|
18
|
+
let held;
|
|
19
|
+
return ()=>(held ??= {
|
|
20
|
+
value: read()
|
|
21
|
+
}).value;
|
|
22
|
+
};
|
|
23
|
+
// The entity an eid names, and the one an id does.
|
|
24
|
+
let byEid = (eid)=>eq(col('eid'), val(eid));
|
|
25
|
+
let byId = (id)=>eq(col('id'), val(id));
|
|
26
|
+
// Whether `t` holds a row for this owner.
|
|
27
|
+
let holds = (run, t, owner)=>run(select({
|
|
28
|
+
cols: [
|
|
29
|
+
col('entity')
|
|
30
|
+
],
|
|
31
|
+
from: table(t),
|
|
32
|
+
where: eq(col('entity'), val(owner))
|
|
33
|
+
})).length > 0;
|
|
34
|
+
// Point entities at an archetype.
|
|
35
|
+
let point = (target, which)=>({
|
|
36
|
+
t: 'update',
|
|
37
|
+
table: 'entity',
|
|
38
|
+
set: {
|
|
39
|
+
archetype: val(target)
|
|
40
|
+
},
|
|
41
|
+
where: which
|
|
42
|
+
});
|
|
43
|
+
// Physical discovery belongs to schema changes, not each gameplay write.
|
|
44
|
+
// revision observes the statements already passing through this connection;
|
|
45
|
+
// it also invalidates on rollback and on another file connection's DDL.
|
|
46
|
+
let facetsHeld = new WeakMap();
|
|
47
|
+
let facets = (driver)=>{
|
|
48
|
+
let version = revision(driver, 'schema');
|
|
49
|
+
let held = facetsHeld.get(driver);
|
|
50
|
+
if (held?.version == version) return held.tables;
|
|
51
|
+
let tables = componentTables(driver);
|
|
52
|
+
facetsHeld.set(driver, {
|
|
53
|
+
version,
|
|
54
|
+
tables
|
|
55
|
+
});
|
|
56
|
+
return tables;
|
|
57
|
+
};
|
|
58
|
+
/** Which of `tables` each owner in `where` has a row in, in table order. */ export let presence = (run, tables, owners, which)=>{
|
|
59
|
+
for (let name of tables){
|
|
60
|
+
let w = which(col('entity', 'c'));
|
|
61
|
+
for (let row of run(select({
|
|
62
|
+
cols: [
|
|
63
|
+
col('entity', 'c')
|
|
64
|
+
],
|
|
65
|
+
from: table(name, 'c'),
|
|
66
|
+
joins: w.joins,
|
|
67
|
+
where: w.where
|
|
68
|
+
}))){
|
|
69
|
+
owners.get(Number(row.entity))?.push(name);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return owners;
|
|
73
|
+
};
|
|
74
|
+
// The descriptors a connection's units found or minted, by eid, as each one's
|
|
75
|
+
// id, while no statement but theirs moved one (@yaks/sql `revision`): a unit
|
|
76
|
+
// pointing entities at an archetype it has met asks the file nothing for it.
|
|
77
|
+
let met = new WeakMap();
|
|
78
|
+
let descriptors = (driver)=>{
|
|
79
|
+
let at = revision(driver, 'descriptors');
|
|
80
|
+
let held = met.get(driver.connection ?? driver);
|
|
81
|
+
if (held?.at != at) {
|
|
82
|
+
met.set(driver.connection ?? driver, held = {
|
|
83
|
+
at,
|
|
84
|
+
ids: new Map()
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
return held;
|
|
88
|
+
};
|
|
89
|
+
// Descriptor minting shared by boot and live reclassification. A descriptor is
|
|
90
|
+
// found by eid first; a bare spine that already has component rows is rejected
|
|
91
|
+
// rather than reused. The descriptor set {archetype} is its own fixed point.
|
|
92
|
+
let minter = (run, driver, cache, ids, counts)=>{
|
|
93
|
+
let meta = cache.intern([
|
|
94
|
+
'archetype'
|
|
95
|
+
]);
|
|
96
|
+
let made = new Set();
|
|
97
|
+
let born = [];
|
|
98
|
+
let considered = new Map();
|
|
99
|
+
let mint = (eid)=>{
|
|
100
|
+
let id = ids.get(eid);
|
|
101
|
+
if (id != null) return id;
|
|
102
|
+
let existing = run(select({
|
|
103
|
+
cols: [
|
|
104
|
+
col('id', 'e'),
|
|
105
|
+
as(col('entity', 'a'), 'descriptor'),
|
|
106
|
+
col('tables', 'p')
|
|
107
|
+
],
|
|
108
|
+
from: table('entity', 'e'),
|
|
109
|
+
joins: [
|
|
110
|
+
left(table('archetype', 'a'), eq(col('entity', 'a'), col('id', 'e'))),
|
|
111
|
+
left(table('archetype', 'p'), eq(col('entity', 'p'), col('archetype', 'e')))
|
|
112
|
+
],
|
|
113
|
+
where: eq(col('eid', 'e'), val(eid))
|
|
114
|
+
}))[0];
|
|
115
|
+
if (existing?.descriptor != null) {
|
|
116
|
+
ids.set(eid, id = Number(existing.id));
|
|
117
|
+
return id;
|
|
118
|
+
}
|
|
119
|
+
if (existing) {
|
|
120
|
+
let pointed = existing.tables == null ? null : descriptor(driver, String(existing.tables)).tables;
|
|
121
|
+
let held = considered.get(Number(existing.id)) ?? ledger(driver).held(eid, pointed);
|
|
122
|
+
if (!held) throw new Unpointed([
|
|
123
|
+
eid
|
|
124
|
+
]);
|
|
125
|
+
if (held.length) throw new Error(`Archetype identity is occupied: ${eid}`);
|
|
126
|
+
}
|
|
127
|
+
let owner = existing ?? run({
|
|
128
|
+
...mintSql(eid),
|
|
129
|
+
returning: [
|
|
130
|
+
col('id')
|
|
131
|
+
]
|
|
132
|
+
})[0];
|
|
133
|
+
id = Number(owner.id);
|
|
134
|
+
let list = JSON.stringify(cache.get(eid).descriptor);
|
|
135
|
+
run({
|
|
136
|
+
t: 'insert',
|
|
137
|
+
into: 'archetype',
|
|
138
|
+
cols: [
|
|
139
|
+
'entity',
|
|
140
|
+
'tables'
|
|
141
|
+
],
|
|
142
|
+
rows: [
|
|
143
|
+
[
|
|
144
|
+
val(id),
|
|
145
|
+
val(list)
|
|
146
|
+
]
|
|
147
|
+
],
|
|
148
|
+
upsert: [
|
|
149
|
+
{
|
|
150
|
+
on: [
|
|
151
|
+
col('entity')
|
|
152
|
+
]
|
|
153
|
+
}
|
|
154
|
+
]
|
|
155
|
+
});
|
|
156
|
+
ids.set(eid, id);
|
|
157
|
+
made.add(id);
|
|
158
|
+
counts.archetypes++;
|
|
159
|
+
// Register first, then recurse: meta points to itself.
|
|
160
|
+
let target = eid == meta.eid ? id : mint(meta.eid);
|
|
161
|
+
run(point(target, byId(id)));
|
|
162
|
+
born.push({
|
|
163
|
+
entity: {
|
|
164
|
+
eid,
|
|
165
|
+
archetype: meta.eid
|
|
166
|
+
},
|
|
167
|
+
archetype: {
|
|
168
|
+
tables: list
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
return id;
|
|
172
|
+
};
|
|
173
|
+
// Resolve all sets before assigning: a bare reference stub can become one
|
|
174
|
+
// of the descriptors, regardless of its position in the snapshot. Hash once
|
|
175
|
+
// per distinct set and bind owner groups in bounded chunks.
|
|
176
|
+
let assign = (owners)=>{
|
|
177
|
+
considered = owners;
|
|
178
|
+
let groups = new Map();
|
|
179
|
+
for (let [owner, names] of owners){
|
|
180
|
+
let key = JSON.stringify(names);
|
|
181
|
+
let group = groups.get(key);
|
|
182
|
+
if (!group) groups.set(key, group = {
|
|
183
|
+
names,
|
|
184
|
+
owners: []
|
|
185
|
+
});
|
|
186
|
+
group.owners.push(owner);
|
|
187
|
+
}
|
|
188
|
+
let targets = [
|
|
189
|
+
...groups.values()
|
|
190
|
+
].map((g)=>[
|
|
191
|
+
g.owners,
|
|
192
|
+
mint(cache.intern(g.names).eid),
|
|
193
|
+
g.names
|
|
194
|
+
]);
|
|
195
|
+
for (let [owners, id] of targets){
|
|
196
|
+
let pending = owners.filter((owner)=>!made.has(owner));
|
|
197
|
+
for(let i = 0; i < pending.length; i += 2048){
|
|
198
|
+
run(point(id, oneOf(col('id'), pending.slice(i, i + 2048))));
|
|
199
|
+
}
|
|
200
|
+
counts.entities += owners.length;
|
|
201
|
+
}
|
|
202
|
+
return targets;
|
|
203
|
+
};
|
|
204
|
+
return {
|
|
205
|
+
mint,
|
|
206
|
+
assign,
|
|
207
|
+
made,
|
|
208
|
+
born
|
|
209
|
+
};
|
|
210
|
+
};
|
|
211
|
+
/** The id of the archetype of `tables`, its descriptor minted where the file
|
|
212
|
+
* holds none: what a branch points an entity it makes at, since on main that
|
|
213
|
+
* entity holds nothing (./branches.ts). */ export let described = (driver, tables)=>unit(driver, ()=>{
|
|
214
|
+
let cache = setsFor(driver);
|
|
215
|
+
let counts = {
|
|
216
|
+
entities: 0,
|
|
217
|
+
archetypes: 0,
|
|
218
|
+
retired: 0
|
|
219
|
+
};
|
|
220
|
+
let { ids } = descriptors(driver);
|
|
221
|
+
let { mint } = minter((s)=>driver.query(s), driver, cache, ids, counts);
|
|
222
|
+
return mint(cache.intern(tables).eid);
|
|
223
|
+
});
|
|
224
|
+
/**
|
|
225
|
+
* Every entity classified again from the tables it holds rows in, not only the
|
|
226
|
+
* unclassified ones: what a pass that wrote component tables directly, outside
|
|
227
|
+
* the graph, leaves to do. Atomic, like {@link backfill}.
|
|
228
|
+
*/ export let reclassifyAll = (driver)=>unit(driver, ()=>{
|
|
229
|
+
driver.query({
|
|
230
|
+
t: 'update',
|
|
231
|
+
table: 'entity',
|
|
232
|
+
set: {
|
|
233
|
+
archetype: lit(null)
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
return backfill(driver);
|
|
237
|
+
});
|
|
238
|
+
/**
|
|
239
|
+
* Idempotent, atomic boot maintenance, after additive DDL has installed the
|
|
240
|
+
* column and archetype vocabulary. Reads presence from the physical file, not
|
|
241
|
+
* vocab. Retires forever a descriptor naming a table that is missing or no
|
|
242
|
+
* longer a component table (a key renamed away from `entity`); reclassifies
|
|
243
|
+
* their owners, plus null assignments, in table-sized scans. No per-owner
|
|
244
|
+
* component census.
|
|
245
|
+
*/ export function backfill(driver, assignments = true) {
|
|
246
|
+
let run = (s)=>driver.query(s);
|
|
247
|
+
let counts = {
|
|
248
|
+
entities: 0,
|
|
249
|
+
archetypes: 0,
|
|
250
|
+
retired: 0
|
|
251
|
+
};
|
|
252
|
+
return unit(driver, ()=>{
|
|
253
|
+
// The catalog's missing-pointer lookup must seek even while a new store
|
|
254
|
+
// is being classified. This index is part of classification's repair.
|
|
255
|
+
if (assignments && !pointable(driver)) driver.query(pointerIndex);
|
|
256
|
+
let cache = setsFor(driver);
|
|
257
|
+
// What the file holds is read once, and only when something is asked of
|
|
258
|
+
// it: a fresh file reads nothing, and a file with descriptors reads which
|
|
259
|
+
// of its tables are component tables, once, to know none names another.
|
|
260
|
+
let tables = once(()=>componentTables(driver));
|
|
261
|
+
let present = once(()=>new Set(tables()));
|
|
262
|
+
let ids = new Map();
|
|
263
|
+
// The descriptors retired on this boot. One retired on an earlier boot
|
|
264
|
+
// had its owners unpointed in that boot's unit, and no write points at a
|
|
265
|
+
// set naming a table that is gone, so its owners are not looked for
|
|
266
|
+
// again: a file that keeps hundreds of them would scan every entity on
|
|
267
|
+
// each boot to find none.
|
|
268
|
+
let stale = [];
|
|
269
|
+
for (let row of run(select({
|
|
270
|
+
cols: [
|
|
271
|
+
col('entity', 'a'),
|
|
272
|
+
col('eid', 'e'),
|
|
273
|
+
col('tables', 'a'),
|
|
274
|
+
as(col('entity', 'r'), 'retired')
|
|
275
|
+
],
|
|
276
|
+
from: table('archetype', 'a'),
|
|
277
|
+
joins: [
|
|
278
|
+
// The catalogue is small; drive its primary-key lookups, never
|
|
279
|
+
// let a no-statistics Store scan every retained entity instead.
|
|
280
|
+
cross(table('entity', 'e')),
|
|
281
|
+
left(table('retired', 'r'), eq(col('entity', 'r'), col('entity', 'a')))
|
|
282
|
+
],
|
|
283
|
+
where: eq(col('id', 'e'), col('entity', 'a'))
|
|
284
|
+
}))){
|
|
285
|
+
let a = cache.decode(row.tables);
|
|
286
|
+
if (a.eid != row.eid) {
|
|
287
|
+
// The original contract used a bare SHA, sharing blobs' address space.
|
|
288
|
+
// Rename only verified legacy descriptors, never arbitrary occupants.
|
|
289
|
+
// Keeping the spine id preserves every integer ref (including owners
|
|
290
|
+
// and retirement); the enclosing savepoint makes this all-or-nothing.
|
|
291
|
+
if (row.eid != sha256(descriptorOf(row.tables).join('|'))) {
|
|
292
|
+
throw new Error(`Invalid archetype identity: ${row.eid}`);
|
|
293
|
+
}
|
|
294
|
+
// A legacy blob could have been added after this descriptor was born.
|
|
295
|
+
// Renaming that shared spine would steal the blob's content address.
|
|
296
|
+
// References to a mixed entity are ambiguous: refuse rather than guess
|
|
297
|
+
// which ones belong to the descriptor. Ordinary descriptors only
|
|
298
|
+
// carry
|
|
299
|
+
// archetype and (optionally) retired.
|
|
300
|
+
if (tables().some((t)=>t != 'archetype' && t != 'retired' && holds(run, t, Number(row.entity)))) {
|
|
301
|
+
throw new Error(`Legacy archetype identity has extra facets: ${row.eid}`);
|
|
302
|
+
}
|
|
303
|
+
let taken = select({
|
|
304
|
+
cols: [
|
|
305
|
+
col('id')
|
|
306
|
+
],
|
|
307
|
+
from: table('entity'),
|
|
308
|
+
where: byEid(a.eid)
|
|
309
|
+
});
|
|
310
|
+
if (run(taken).length) {
|
|
311
|
+
throw new Error(`Archetype identity is occupied: ${a.eid}`);
|
|
312
|
+
}
|
|
313
|
+
run({
|
|
314
|
+
t: 'update',
|
|
315
|
+
table: 'entity',
|
|
316
|
+
set: {
|
|
317
|
+
eid: val(a.eid)
|
|
318
|
+
},
|
|
319
|
+
where: byId(Number(row.entity))
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
let id = Number(row.entity);
|
|
323
|
+
ids.set(a.eid, id);
|
|
324
|
+
if (a.tables.some((t)=>!present().has(t)) && row.retired == null) {
|
|
325
|
+
stale.push(id);
|
|
326
|
+
run({
|
|
327
|
+
t: 'insert',
|
|
328
|
+
into: 'retired',
|
|
329
|
+
cols: [
|
|
330
|
+
'entity'
|
|
331
|
+
],
|
|
332
|
+
rows: [
|
|
333
|
+
[
|
|
334
|
+
val(id)
|
|
335
|
+
]
|
|
336
|
+
]
|
|
337
|
+
});
|
|
338
|
+
run(point(null, byId(id)));
|
|
339
|
+
counts.retired++;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
if (!assignments) {
|
|
343
|
+
let owners = new Map(run(select({
|
|
344
|
+
cols: [
|
|
345
|
+
col('id', 'e')
|
|
346
|
+
],
|
|
347
|
+
from: table('archetype', 'a'),
|
|
348
|
+
joins: [
|
|
349
|
+
cross(table('entity', 'e'))
|
|
350
|
+
],
|
|
351
|
+
where: and(eq(col('id', 'e'), col('entity', 'a')), isNull(col('archetype', 'e')))
|
|
352
|
+
})).map((r)=>[
|
|
353
|
+
Number(r.id),
|
|
354
|
+
[]
|
|
355
|
+
]));
|
|
356
|
+
if (owners.size) {
|
|
357
|
+
presence(run, tables(), owners, (c)=>({
|
|
358
|
+
where: oneOf(c, [
|
|
359
|
+
...owners.keys()
|
|
360
|
+
])
|
|
361
|
+
}));
|
|
362
|
+
minter(run, driver, cache, ids, counts).assign(owners);
|
|
363
|
+
}
|
|
364
|
+
return counts;
|
|
365
|
+
}
|
|
366
|
+
// Spelled out a list at a time, each member a seek on the pointer index.
|
|
367
|
+
for(let i = 0; i < stale.length; i += 512){
|
|
368
|
+
run(point(null, oneOf(col('archetype'), stale.slice(i, i + 512))));
|
|
369
|
+
}
|
|
370
|
+
// Snapshot the incomplete owners before minting descriptors. New descriptors
|
|
371
|
+
// are classified directly below; they never need another whole-file pass.
|
|
372
|
+
let owners = new Map(run(select({
|
|
373
|
+
cols: [
|
|
374
|
+
col('id')
|
|
375
|
+
],
|
|
376
|
+
from: table('entity'),
|
|
377
|
+
where: isNull(col('archetype'))
|
|
378
|
+
})).map((r)=>[
|
|
379
|
+
Number(r.id),
|
|
380
|
+
[]
|
|
381
|
+
]));
|
|
382
|
+
if (owners.size) {
|
|
383
|
+
presence(run, tables(), owners, (c)=>({
|
|
384
|
+
// A backfill reads each component once, looking up its owner. Without
|
|
385
|
+
// statistics a join may walk every missing spine for each empty table.
|
|
386
|
+
joins: [
|
|
387
|
+
cross(table('entity', 'e'))
|
|
388
|
+
],
|
|
389
|
+
where: and(eq(col('id', 'e'), c), isNull(col('archetype', 'e')))
|
|
390
|
+
}));
|
|
391
|
+
}
|
|
392
|
+
minter(run, driver, cache, ids, counts).assign(owners);
|
|
393
|
+
return counts;
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
// The entities `which` names that are not descriptors themselves, with the
|
|
397
|
+
// eid of the archetype each one points at and the tables it names.
|
|
398
|
+
let owned = (which)=>select({
|
|
399
|
+
cols: [
|
|
400
|
+
col('id', 'e'),
|
|
401
|
+
col('eid', 'e'),
|
|
402
|
+
col('archetype', 'e'),
|
|
403
|
+
as(col('eid', 'd'), 'assigned'),
|
|
404
|
+
as(col('tables', 'p'), 'pointed')
|
|
405
|
+
],
|
|
406
|
+
from: table('entity', 'e'),
|
|
407
|
+
joins: [
|
|
408
|
+
left(table('entity', 'd'), eq(col('id', 'd'), col('archetype', 'e'))),
|
|
409
|
+
left(table('archetype', 'p'), eq(col('entity', 'p'), col('id', 'd'))),
|
|
410
|
+
left(table('archetype', 'a'), eq(col('entity', 'a'), col('id', 'e')))
|
|
411
|
+
],
|
|
412
|
+
where: and(isNull(col('entity', 'a')), which)
|
|
413
|
+
});
|
|
414
|
+
// Whether an owner's pointer ({@link owned}) describes `set`: the very
|
|
415
|
+
// descriptor, or one written in another form (labels, before its words had
|
|
416
|
+
// identities) of the same set, which stands until the migration rewrites it.
|
|
417
|
+
let describes = (driver, set, r)=>set.eid == r.assigned || r.pointed != null && descriptor(driver, String(r.pointed)).tables.join('|') == set.tables.join('|');
|
|
418
|
+
/** Whether the file holds the pointer index, through which the owners of a
|
|
419
|
+
* pointer are found without reading the spine. */ export let pointable = (driver)=>catalogue(driver).some((o)=>o.name == pointerIndex.name || o.name == `${pointerIndex.name}_num`);
|
|
420
|
+
/**
|
|
421
|
+
* Up to `limit` owners whose pointer says nothing true of them: none at all,
|
|
422
|
+
* or a descriptor naming a table the file no longer holds. Each is a seek on
|
|
423
|
+
* the pointer index ({@link pointable}), so a classified file answers none
|
|
424
|
+
* without reading its spine, and the descriptors are those the catalog holds
|
|
425
|
+
* (./catalog.ts `standing`), read for a plan once. Classifying one always
|
|
426
|
+
* moves it out, since what an owner holds names only tables the file holds.
|
|
427
|
+
*/ export let strays = (driver, limit)=>{
|
|
428
|
+
let present = new Set(componentTables(driver));
|
|
429
|
+
let gone = [
|
|
430
|
+
...standing(driver)
|
|
431
|
+
].flatMap(([id, set])=>set.tables.some((t)=>!present.has(t)) ? [
|
|
432
|
+
id
|
|
433
|
+
] : []);
|
|
434
|
+
let some = (which, n)=>driver.query({
|
|
435
|
+
...owned(which),
|
|
436
|
+
limit: lit(n)
|
|
437
|
+
});
|
|
438
|
+
let rows = some(isNull(col('archetype', 'e')), limit);
|
|
439
|
+
for(let i = 0; i < gone.length && rows.length < limit; i += 512){
|
|
440
|
+
rows.push(...some(oneOf(col('archetype', 'e'), gone.slice(i, i + 512)), limit - rows.length));
|
|
441
|
+
}
|
|
442
|
+
return rows;
|
|
443
|
+
};
|
|
444
|
+
// Owners are audited a window of ids at a time, so a half-million-entity file
|
|
445
|
+
// costs one window of JS rather than the whole file: each window is a slice of
|
|
446
|
+
// the same scan, through the integer primary key every component table has.
|
|
447
|
+
let WINDOW = 20_000;
|
|
448
|
+
/**
|
|
449
|
+
* The audit half of `reclassify`, and it writes nothing. A row written past
|
|
450
|
+
* the graph must name its owners; a writer that forgets leaves a pointer that
|
|
451
|
+
* no longer describes its entity, and both the read path and the query planner
|
|
452
|
+
* trust that pointer — nothing else notices. So this reads presence for every
|
|
453
|
+
* owner, by the same rule and the same component list classification uses, and
|
|
454
|
+
* reports where the two disagree. Descriptors are their own fixed point and
|
|
455
|
+
* are left out, exactly as `reclassify` leaves them out.
|
|
456
|
+
*/ export function drift(driver, sample = 12) {
|
|
457
|
+
let run = (s)=>driver.query(s);
|
|
458
|
+
let tables = facets(driver);
|
|
459
|
+
let cache = setsFor(driver);
|
|
460
|
+
// Presence set (as the joined table names) → the set it interns to. The
|
|
461
|
+
// whole file holds a few hundred distinct sets, so hashing one per window's
|
|
462
|
+
// worth of owners would be the audit's dominant cost.
|
|
463
|
+
let known = new Map();
|
|
464
|
+
let out = {
|
|
465
|
+
checked: 0,
|
|
466
|
+
drifted: 0,
|
|
467
|
+
sample: []
|
|
468
|
+
};
|
|
469
|
+
let top = Number(run(select({
|
|
470
|
+
cols: [
|
|
471
|
+
as(fn('max', col('id')), 'top')
|
|
472
|
+
],
|
|
473
|
+
from: table('entity')
|
|
474
|
+
}))[0]?.top ?? 0);
|
|
475
|
+
for(let lo = 0; lo < top; lo += WINDOW){
|
|
476
|
+
let hi = lo + WINDOW;
|
|
477
|
+
// A window's pointers and rows are read in one snapshot, so a writer
|
|
478
|
+
// committing between the two is never counted as drift.
|
|
479
|
+
let [rows, owners] = unit(driver, ()=>{
|
|
480
|
+
let rows = run(owned(and(gt(col('id', 'e'), val(lo)), le(col('id', 'e'), val(hi)))));
|
|
481
|
+
return [
|
|
482
|
+
rows,
|
|
483
|
+
rows.length ? presence(run, tables, new Map(rows.map((r)=>[
|
|
484
|
+
Number(r.id),
|
|
485
|
+
[]
|
|
486
|
+
])), (c)=>({
|
|
487
|
+
where: and(gt(c, val(lo)), le(c, val(hi)))
|
|
488
|
+
})) : new Map()
|
|
489
|
+
];
|
|
490
|
+
}, 'read');
|
|
491
|
+
for (let r of rows){
|
|
492
|
+
let names = owners.get(Number(r.id));
|
|
493
|
+
let key = names.join('|');
|
|
494
|
+
let set = known.get(key) ?? cache.intern(names);
|
|
495
|
+
known.set(key, set);
|
|
496
|
+
out.checked++;
|
|
497
|
+
if (describes(driver, set, r)) continue;
|
|
498
|
+
out.drifted++;
|
|
499
|
+
if (out.sample.length < sample) out.sample.push(String(r.eid));
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
return out;
|
|
503
|
+
}
|
|
504
|
+
/**
|
|
505
|
+
* Every owner the audit finds out of step ({@link drift}) classified again
|
|
506
|
+
* from the rows it holds, a few thousand to a unit, so a large file never
|
|
507
|
+
* holds the write lock for the whole pass: the repair for pointers a writer
|
|
508
|
+
* past every door left behind. Returns how many moved.
|
|
509
|
+
*/ export let mend = (driver)=>{
|
|
510
|
+
let { sample } = drift(driver, Infinity);
|
|
511
|
+
let moved = 0;
|
|
512
|
+
for(let i = 0; i < sample.length; i += 5000){
|
|
513
|
+
moved += reclassify(driver, sample.slice(i, i + 5000)).filter((b)=>b.archetype == null).length;
|
|
514
|
+
}
|
|
515
|
+
return moved;
|
|
516
|
+
};
|
|
517
|
+
// Each of these entities' eid with the tables its stored pointer names, or
|
|
518
|
+
// null where it names no descriptor. One statement reads the pointers.
|
|
519
|
+
let pointers = (driver, eids)=>driver.query(select({
|
|
520
|
+
cols: [
|
|
521
|
+
col('eid', 'e'),
|
|
522
|
+
col('tables', 'a')
|
|
523
|
+
],
|
|
524
|
+
from: table('entity', 'e'),
|
|
525
|
+
joins: [
|
|
526
|
+
left(table('archetype', 'a'), eq(col('entity', 'a'), col('archetype', 'e')))
|
|
527
|
+
],
|
|
528
|
+
where: oneOf(col('eid', 'e'), [
|
|
529
|
+
...new Set(eids)
|
|
530
|
+
])
|
|
531
|
+
})).map((row)=>[
|
|
532
|
+
String(row.eid),
|
|
533
|
+
row.tables == null ? null : descriptor(driver, String(row.tables)).tables
|
|
534
|
+
]);
|
|
535
|
+
/**
|
|
536
|
+
* The tables these entities hold rows in, as their removal clears them: each
|
|
537
|
+
* one's stored archetype, as the connection's ledger amends it for what the
|
|
538
|
+
* open transaction wrote since ({@link Ledger} `held`), among the component
|
|
539
|
+
* tables the file holds. One statement reads the pointers, so a removal costs
|
|
540
|
+
* the tables its entities wear rather than every table in the file.
|
|
541
|
+
*
|
|
542
|
+
* The pointer is exact wherever no unit is open: every door classifies what it
|
|
543
|
+
* wrote before its unit closes, and a raw writer calls {@link reclassify}
|
|
544
|
+
* (what {@link drift} audits). An entity with no pointer that the transaction
|
|
545
|
+
* did not mint is a failure: `lost` repairs those, as {@link reclassify} does
|
|
546
|
+
* before their tables are read again; without one, the failure is thrown.
|
|
547
|
+
*/ export function heldBy(driver, eids, book, lost) {
|
|
548
|
+
let held = new Set();
|
|
549
|
+
let missing = [];
|
|
550
|
+
let take = (rows)=>{
|
|
551
|
+
for (let [eid, pointed] of rows){
|
|
552
|
+
let tables = book ? book.held(eid, pointed) : pointed;
|
|
553
|
+
if (tables) {
|
|
554
|
+
for (let t of tables)held.add(t);
|
|
555
|
+
} else missing.push(eid);
|
|
556
|
+
}
|
|
557
|
+
};
|
|
558
|
+
take(pointers(driver, eids));
|
|
559
|
+
if (missing.length) {
|
|
560
|
+
let unknown = missing;
|
|
561
|
+
missing = [];
|
|
562
|
+
if (!lost) throw new Unpointed(unknown);
|
|
563
|
+
lost(unknown);
|
|
564
|
+
take(pointers(driver, unknown));
|
|
565
|
+
}
|
|
566
|
+
return componentTables(driver, [], [
|
|
567
|
+
...held
|
|
568
|
+
]);
|
|
569
|
+
}
|
|
570
|
+
/**
|
|
571
|
+
* Point these entities, just removed, at the tombstone set, minting its
|
|
572
|
+
* descriptor the first time a store needs one. A removal clears every table
|
|
573
|
+
* its entities hold rows in ({@link heldBy}), so what each one holds afterwards
|
|
574
|
+
* is its tombstone and nothing else, known without reading a table. A unit
|
|
575
|
+
* calls this for the dead its ledger still owes a pointer ({@link Ledger}
|
|
576
|
+
* `buried`) as it closes, so the dead leave their archetype whichever door
|
|
577
|
+
* removed them, and a presence lookup passes them by. Answers whether it
|
|
578
|
+
* minted a descriptor.
|
|
579
|
+
*/ export function entomb(driver, eids) {
|
|
580
|
+
if (!eids.length) return false;
|
|
581
|
+
let run = (s)=>driver.query(s);
|
|
582
|
+
return unit(driver, ()=>{
|
|
583
|
+
let cache = setsFor(driver);
|
|
584
|
+
let counts = {
|
|
585
|
+
entities: 0,
|
|
586
|
+
archetypes: 0,
|
|
587
|
+
retired: 0
|
|
588
|
+
};
|
|
589
|
+
let found = descriptors(driver);
|
|
590
|
+
let { mint } = minter(run, driver, cache, found.ids, counts);
|
|
591
|
+
let dead = mint(cache.intern([
|
|
592
|
+
TOMBSTONE
|
|
593
|
+
]).eid);
|
|
594
|
+
found.at = revision(driver, 'descriptors');
|
|
595
|
+
run(point(dead, oneOf(col('eid'), [
|
|
596
|
+
...new Set(eids)
|
|
597
|
+
])));
|
|
598
|
+
return counts.archetypes > 0;
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
/**
|
|
602
|
+
* Classify the named entities from their physical presence, now, inside the
|
|
603
|
+
* caller's transaction. For rows an application wrote past the graph (raw SQL
|
|
604
|
+
* into a component table), and the repair of an entity met with no archetype:
|
|
605
|
+
* no queue, no triggers, the same presence rule as boot. Returns the pointers
|
|
606
|
+
* that moved and any descriptor minted, as bundles an application can
|
|
607
|
+
* broadcast. Descriptors and unknown eids are left alone.
|
|
608
|
+
*/ export function reclassify(driver, eids) {
|
|
609
|
+
return unit(driver, ()=>{
|
|
610
|
+
let { born, put } = sorted(driver, eids);
|
|
611
|
+
return [
|
|
612
|
+
...born,
|
|
613
|
+
...put.flatMap(([owners, id])=>{
|
|
614
|
+
let [row] = driver.query(select({
|
|
615
|
+
cols: [
|
|
616
|
+
col('eid')
|
|
617
|
+
],
|
|
618
|
+
from: table('entity'),
|
|
619
|
+
where: byId(id)
|
|
620
|
+
}));
|
|
621
|
+
return owners.map((eid)=>({
|
|
622
|
+
entity: {
|
|
623
|
+
eid,
|
|
624
|
+
archetype: String(row.eid)
|
|
625
|
+
}
|
|
626
|
+
}));
|
|
627
|
+
})
|
|
628
|
+
];
|
|
629
|
+
});
|
|
630
|
+
}
|
|
631
|
+
/**
|
|
632
|
+
* A unit settling what it owes: each entity pointed at the archetype of the
|
|
633
|
+
* tables it holds, which `known` says from its pointer and the unit's ledger
|
|
634
|
+
* (given the tables the pointer names), so no table is read. Only an entity
|
|
635
|
+
* `known` cannot speak for is read from the rows it holds, as a repair is.
|
|
636
|
+
* `learned` hears the archetype, by its id, each entity is found or put in.
|
|
637
|
+
* It runs inside the unit that owes them, which makes it all or nothing, and
|
|
638
|
+
* answers whether it minted a descriptor.
|
|
639
|
+
*/ export let classify = (driver, eids, known, learned)=>sorted(driver, eids, known, learned).born.length > 0;
|
|
640
|
+
// The named entities put in the archetype of the tables each holds: the
|
|
641
|
+
// descriptors minted for it, and the eids pointed at each archetype's id.
|
|
642
|
+
let sorted = (driver, eids, known, learned)=>{
|
|
643
|
+
let none = {
|
|
644
|
+
born: [],
|
|
645
|
+
put: []
|
|
646
|
+
};
|
|
647
|
+
if (!eids.length) return none;
|
|
648
|
+
let run = (s)=>driver.query(s);
|
|
649
|
+
let cache = setsFor(driver);
|
|
650
|
+
let tables = once(()=>facets(driver));
|
|
651
|
+
let rows = run(owned(oneOf(col('eid', 'e'), [
|
|
652
|
+
...new Set(eids)
|
|
653
|
+
])));
|
|
654
|
+
if (!rows.length) return none;
|
|
655
|
+
let owners = new Map();
|
|
656
|
+
let ids = [];
|
|
657
|
+
for (let r of rows){
|
|
658
|
+
let pointed = r.pointed == null ? null : descriptor(driver, String(r.pointed)).tables;
|
|
659
|
+
let held = known?.(String(r.eid), pointed);
|
|
660
|
+
owners.set(Number(r.id), held ?? []);
|
|
661
|
+
if (!held) ids.push(Number(r.id));
|
|
662
|
+
}
|
|
663
|
+
if (ids.length) {
|
|
664
|
+
presence(run, tables(), owners, (c)=>({
|
|
665
|
+
where: oneOf(c, ids)
|
|
666
|
+
}));
|
|
667
|
+
}
|
|
668
|
+
let moved = new Map();
|
|
669
|
+
for (let r of rows){
|
|
670
|
+
let names = owners.get(Number(r.id));
|
|
671
|
+
if (describes(driver, cache.intern(names), r)) {
|
|
672
|
+
learned?.(String(r.eid), names, Number(r.archetype));
|
|
673
|
+
owners.delete(Number(r.id));
|
|
674
|
+
} else moved.set(Number(r.id), String(r.eid));
|
|
675
|
+
}
|
|
676
|
+
if (!owners.size) return none;
|
|
677
|
+
let counts = {
|
|
678
|
+
entities: 0,
|
|
679
|
+
archetypes: 0,
|
|
680
|
+
retired: 0
|
|
681
|
+
};
|
|
682
|
+
let found = descriptors(driver);
|
|
683
|
+
let { assign, born } = minter(run, driver, cache, found.ids, counts);
|
|
684
|
+
let put = assign(owners).map(([group, id, names])=>{
|
|
685
|
+
let eids = group.map((owner)=>moved.get(owner));
|
|
686
|
+
for (let eid of eids)learned?.(eid, names, id);
|
|
687
|
+
return [
|
|
688
|
+
eids,
|
|
689
|
+
id
|
|
690
|
+
];
|
|
691
|
+
});
|
|
692
|
+
found.at = revision(driver, 'descriptors');
|
|
693
|
+
return {
|
|
694
|
+
born,
|
|
695
|
+
put
|
|
696
|
+
};
|
|
697
|
+
};
|
|
698
|
+
let pending = new WeakMap();
|
|
699
|
+
/** The table sets the open write knows instead of its stored pointers,
|
|
700
|
+
* shared by its reads until either the rows or the ledger change. */ export let pendingShapes = (driver, book)=>{
|
|
701
|
+
let eids = book.owed();
|
|
702
|
+
if (!eids.length) {
|
|
703
|
+
pending.delete(book);
|
|
704
|
+
return new Map();
|
|
705
|
+
}
|
|
706
|
+
let connection = driver.connection ?? driver;
|
|
707
|
+
let data = revision(driver, 'data'), at = book.revision();
|
|
708
|
+
let kept = pending.get(book);
|
|
709
|
+
if (kept?.connection == connection && kept.data == data && kept.revision == at) return kept.shapes;
|
|
710
|
+
let shapes = new Map(driver.query(owned(oneOf(col('eid', 'e'), eids))).map((r)=>{
|
|
711
|
+
let eid = String(r.eid);
|
|
712
|
+
let pointed = r.pointed == null ? null : descriptor(driver, String(r.pointed)).tables;
|
|
713
|
+
let held = book.held(eid, pointed);
|
|
714
|
+
if (!held) throw new Unpointed([
|
|
715
|
+
eid
|
|
716
|
+
]);
|
|
717
|
+
return [
|
|
718
|
+
Number(r.id),
|
|
719
|
+
held
|
|
720
|
+
];
|
|
721
|
+
}));
|
|
722
|
+
pending.set(book, {
|
|
723
|
+
connection,
|
|
724
|
+
data,
|
|
725
|
+
revision: at,
|
|
726
|
+
shapes
|
|
727
|
+
});
|
|
728
|
+
return shapes;
|
|
729
|
+
};
|
|
730
|
+
//# sourceMappingURL=archetype.js.map
|