@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/read.js
ADDED
|
@@ -0,0 +1,1100 @@
|
|
|
1
|
+
import { logicalVocab } from "@yaks/sql/core";
|
|
2
|
+
import { readingVocab } from "./shape.js";
|
|
3
|
+
import { Unpointed } from "./error.js";
|
|
4
|
+
import { cardinality, census, classified, installed, POPULATION, populationCount, populations } from "./population.js";
|
|
5
|
+
// Reads: a query in, whole entities out. This is the SELECT half of the
|
|
6
|
+
// adapter. The heavy lifting — turning a query into SQL — belongs to the
|
|
7
|
+
// sibling packages: @yaks/query parses the query text into an AST, @yaks/vocab
|
|
8
|
+
// describes the vocabulary, and @yaks/sql compiles the two into a statement
|
|
9
|
+
// that selects the matching entities' ids. This file runs that statement, then
|
|
10
|
+
// gathers each matched entity's components into a bundle.
|
|
11
|
+
//
|
|
12
|
+
// `rows()` is the raw form: it returns the compiled statement's rows verbatim,
|
|
13
|
+
// which is what aggregate and projection queries (a count, a tally, a field
|
|
14
|
+
// list) want. `read()` is the whole-entity form built on it: it takes the ids
|
|
15
|
+
// `rows()` returns and reads back every component each entity has.
|
|
16
|
+
// `screened()` is the statement alone, for a search beside the graph to run.
|
|
17
|
+
import { unraised } from "./raise.js";
|
|
18
|
+
import { installed as received } from "./meta.js";
|
|
19
|
+
import { field } from "@yaks/sql/query";
|
|
20
|
+
import { map, parse } from "@yaks/query/core";
|
|
21
|
+
import { absentDerived, among, and, as, col, compile, cross, derivedOf, each, eidAt, eidOf, eq, exists, fn, from, idOf, join, left, lit, oneOf, op, pinned, render, revision, screen, select, setOf, sourceScreen, storedOnly, sub, table, tagged, tagIn, val, visibleBacked, visibleDerived } from "@yaks/sql/query";
|
|
22
|
+
import { sha256 } from "@yaks/graph/sha256";
|
|
23
|
+
import { tombstoned } from "@yaks/graph/bundle";
|
|
24
|
+
import { catalog, described as learn, descriptor } from "./catalog.js";
|
|
25
|
+
import { unit } from "./unit.js";
|
|
26
|
+
import { decoded, jsonOut, projected } from "./jsonb.js";
|
|
27
|
+
import { tables } from "./layout.js";
|
|
28
|
+
export let ast = (q)=>typeof q == 'string' ? parse(q) : q;
|
|
29
|
+
// The raw compiled rows for a query — a membership query returns `{ eid }` per
|
|
30
|
+
// match, an aggregate returns its value/count shape, and a `.fields`
|
|
31
|
+
// projection's columns come back as a component read would return them. The
|
|
32
|
+
// values are bound as parameters.
|
|
33
|
+
export let rows = (driver, vocab, query, opts = {})=>{
|
|
34
|
+
let ask = ()=>{
|
|
35
|
+
let parsed = ast(query);
|
|
36
|
+
// A store's counted populations are of the living.
|
|
37
|
+
let shape = opts.graves || opts.archetypes?.pending || !vocab.comp('archetype') ? undefined : cardinality(parsed, vocab, opts);
|
|
38
|
+
if (shape && (!opts.archetypes || opts.archetypes({}) != undefined) && classified(driver)) {
|
|
39
|
+
if (shape.tally || shape.archetype || opts.visibility?.entity) {
|
|
40
|
+
let buckets = populations(driver)?.filter((b)=>{
|
|
41
|
+
let row = {
|
|
42
|
+
entity: {
|
|
43
|
+
eid: b.value,
|
|
44
|
+
archetype: b.value
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
return shape.visible(b.tables) && (!shape.archetype || shape.archetype(row));
|
|
48
|
+
});
|
|
49
|
+
if (buckets) {
|
|
50
|
+
return shape.tally ? buckets.map(({ value, n })=>({
|
|
51
|
+
value,
|
|
52
|
+
n
|
|
53
|
+
})) : [
|
|
54
|
+
{
|
|
55
|
+
value: '',
|
|
56
|
+
n: buckets.reduce((n, b)=>n + (!shape.comp || shape.comp == 'entity' || b.tables.includes(shape.comp) ? b.n : 0), 0)
|
|
57
|
+
}
|
|
58
|
+
];
|
|
59
|
+
}
|
|
60
|
+
} else {
|
|
61
|
+
let n = populationCount(driver, shape.comp, shape.presence);
|
|
62
|
+
if (n != undefined) return [
|
|
63
|
+
{
|
|
64
|
+
value: '',
|
|
65
|
+
n
|
|
66
|
+
}
|
|
67
|
+
];
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
let population = !opts.graves && !opts.archetypes?.pending && parsed.clauses.length == 1 && parsed.clauses[0].kind == 'tally' && parsed.clauses[0].path.join('.') == 'entity.archetype' && installed(driver) && classified(driver) ? POPULATION : undefined;
|
|
71
|
+
let compiled = compile(parsed, readingVocab(driver, vocab), catalogued(driver, vocab, {
|
|
72
|
+
...opts,
|
|
73
|
+
population,
|
|
74
|
+
census: census(driver)
|
|
75
|
+
}));
|
|
76
|
+
// An answer proved without the engine (@yaks/sql `compile`) stands on a
|
|
77
|
+
// file that was installed: one that never was is refused by the engine,
|
|
78
|
+
// as a read of tables it does not hold.
|
|
79
|
+
let proved = compiled.answer && (!driver.file || received(driver) != null) ? compiled.answer : undefined;
|
|
80
|
+
return (proved ?? driver.query(compiled)).map((r)=>projected(vocab, r, parsed));
|
|
81
|
+
};
|
|
82
|
+
// The catalog and entity statement must see the same commit. Otherwise a
|
|
83
|
+
// concurrent writer could introduce a new matching set between the two. A
|
|
84
|
+
// read unit is one snapshot without the write lock, so a long query never
|
|
85
|
+
// makes a writer on another connection wait (./unit.ts).
|
|
86
|
+
return unit(driver, ask, 'read');
|
|
87
|
+
};
|
|
88
|
+
// A compile's options over this file: the caller's, and the file's archetype
|
|
89
|
+
// catalog where the vocabulary keeps one, so every presence and kind test is
|
|
90
|
+
// a lookup on the archetype index.
|
|
91
|
+
let catalogued = (driver, vocab, opts)=>({
|
|
92
|
+
...opts,
|
|
93
|
+
archetypes: opts.archetypes ?? catalog(driver),
|
|
94
|
+
// Where the vocabulary declares none, the descriptors every file keeps
|
|
95
|
+
// (./ddl.ts) are the store's bookkeeping, and no entity of its graph.
|
|
96
|
+
...vocab.comp('archetype') ? {} : {
|
|
97
|
+
bookkeeping: KEPT
|
|
98
|
+
},
|
|
99
|
+
stable: opts.stable ?? driver.stable
|
|
100
|
+
});
|
|
101
|
+
let KEPT = [
|
|
102
|
+
'archetype'
|
|
103
|
+
];
|
|
104
|
+
/**
|
|
105
|
+
* The entities a query admits, as a statement selecting their integer ids as
|
|
106
|
+
* `id` (@yaks/sql `screen`), compiled as this file's reads are: what a search
|
|
107
|
+
* run beside the graph is narrowed by. It names the archetypes that match now,
|
|
108
|
+
* so run it before anything else writes.
|
|
109
|
+
*/ export let screened = (driver, vocab, query, opts = {})=>screen(ast(query), readingVocab(driver, vocab), catalogued(driver, vocab, opts));
|
|
110
|
+
// The properties a gather reads: the stored ones, plus any computed property
|
|
111
|
+
// the caller registered an expression for. A computed property has no row to
|
|
112
|
+
// read, so without a registration there is nothing to select — but with one it
|
|
113
|
+
// is a column like any other, and leaving it out of the bundle while the filter
|
|
114
|
+
// resolves it would make `.task.status=open` select rows whose `status` the
|
|
115
|
+
// result does not carry.
|
|
116
|
+
let read1 = (v, comp, derived)=>v.props(comp).map((p)=>v.prop(comp, p)).filter((c)=>(!c.computed || derived[`${comp}.${c.prop}`]) && derived[`${comp}.${c.prop}`]?.whole !== false);
|
|
117
|
+
let projection = (v, comp, derived, backed, tags, named = false)=>{
|
|
118
|
+
let own = (prop)=>col(prop, comp);
|
|
119
|
+
let sel = [];
|
|
120
|
+
let joins = [];
|
|
121
|
+
let refs = [];
|
|
122
|
+
let reading = new Set();
|
|
123
|
+
let deps = new Set();
|
|
124
|
+
for (let c of read1(v, comp, derived)){
|
|
125
|
+
let over = derived[`${comp}.${c.prop}`];
|
|
126
|
+
let tag = c.ref && backed[c.ref] && tags[c.ref] ? c.ref : undefined;
|
|
127
|
+
if (over) {
|
|
128
|
+
for (let d of over.deps ?? [])deps.add(d);
|
|
129
|
+
let value = !c.computed && storedOnly(over, v) ? over.text(own(field(c.prop))) : over.expr(own('entity'));
|
|
130
|
+
sel.push(as(value, c.prop));
|
|
131
|
+
} else if (tag) {
|
|
132
|
+
reading.add(tag);
|
|
133
|
+
sel.push(as(eidAt(tagIn(tag), own(field(c.prop))), c.prop));
|
|
134
|
+
} else if (c.category == 'ref' && named) {
|
|
135
|
+
refs.push(c.prop);
|
|
136
|
+
sel.push(as(own(field(c.prop)), c.prop));
|
|
137
|
+
} else if (c.category == 'ref') {
|
|
138
|
+
let a = `r_${c.prop.replaceAll(/[^A-Za-z0-9]/g, '_')}`;
|
|
139
|
+
joins.push(left(table('entity', a), eq(col('id', a), own(field(c.prop)))));
|
|
140
|
+
sel.push(as(col('eid', a), c.prop));
|
|
141
|
+
} else if (c.scalar == 'jsonb') {
|
|
142
|
+
sel.push(as(jsonOut(own(field(c.prop))), c.prop));
|
|
143
|
+
} else {
|
|
144
|
+
sel.push(as(own(field(c.prop)), c.prop));
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
for (let d of deps){
|
|
148
|
+
if (d == comp) continue;
|
|
149
|
+
joins.push(left(table(d), eq(col('entity', d), own('entity'))));
|
|
150
|
+
}
|
|
151
|
+
return {
|
|
152
|
+
sel,
|
|
153
|
+
joins,
|
|
154
|
+
...refs.length ? {
|
|
155
|
+
refs
|
|
156
|
+
} : {},
|
|
157
|
+
...reading.size ? {
|
|
158
|
+
tagged: [
|
|
159
|
+
...reading
|
|
160
|
+
]
|
|
161
|
+
} : {}
|
|
162
|
+
};
|
|
163
|
+
};
|
|
164
|
+
// The `with` a statement reading `p` binds its tags in.
|
|
165
|
+
let tagging = (p, tags = {})=>p.tagged ? tagged(tags, p.tagged) : [];
|
|
166
|
+
// What `key` holds in `m`, made on first ask.
|
|
167
|
+
let at = (m, key, make)=>{
|
|
168
|
+
let v = m.get(key);
|
|
169
|
+
if (v === undefined) m.set(key, v = make());
|
|
170
|
+
return v;
|
|
171
|
+
};
|
|
172
|
+
// A read's trees, kept per vocabulary and registries while they live. A store
|
|
173
|
+
// asks the same few reads thousands of times a session, and @yaks/sql renders
|
|
174
|
+
// a tree it has seen once, so only the part holding the ids is written again.
|
|
175
|
+
// The registries are built once per store and never changed.
|
|
176
|
+
let NONE = {};
|
|
177
|
+
let kept = new WeakMap();
|
|
178
|
+
// A status a vocabulary's ladder computes is read like any registered one
|
|
179
|
+
// (@yaks/sql `derivedOf`).
|
|
180
|
+
// The tags are each store's own, so only which components carry one keys it.
|
|
181
|
+
let project = (v, comp, derived = NONE, backed = NONE, tags = NONE, named = false)=>{
|
|
182
|
+
let all = derivedOf(v, derived);
|
|
183
|
+
return at(at(at(at(kept, v, ()=>new WeakMap()), all, ()=>new WeakMap()), backed, ()=>new Map()), `${named} ${comp} ${Object.keys(tags)}`, ()=>projection(v, comp, all, backed, tags, named));
|
|
184
|
+
};
|
|
185
|
+
// The eids of the entities these ids are, in one statement: what a set read
|
|
186
|
+
// asks once for every reference its rows hold. An id no entity has stays
|
|
187
|
+
// unnamed, as a reference whose target is gone reads.
|
|
188
|
+
let naming = (driver, ids)=>{
|
|
189
|
+
let asked = [
|
|
190
|
+
...new Set([
|
|
191
|
+
...ids
|
|
192
|
+
].flatMap((id)=>id == null ? [] : [
|
|
193
|
+
Number(id)
|
|
194
|
+
]))
|
|
195
|
+
];
|
|
196
|
+
if (!asked.length) return new Map();
|
|
197
|
+
return new Map(driver.query(select({
|
|
198
|
+
cols: [
|
|
199
|
+
col('id', 'e'),
|
|
200
|
+
col('eid', 'e')
|
|
201
|
+
],
|
|
202
|
+
...owning(asked, table('entity', 'e'), col('id', 'e'))
|
|
203
|
+
})).map((r)=>[
|
|
204
|
+
Number(r.id),
|
|
205
|
+
String(r.eid)
|
|
206
|
+
]));
|
|
207
|
+
};
|
|
208
|
+
// A row's references, named.
|
|
209
|
+
let named = (value, refs, names)=>{
|
|
210
|
+
if (!refs) return value;
|
|
211
|
+
let out = {
|
|
212
|
+
...value
|
|
213
|
+
};
|
|
214
|
+
for (let prop of refs){
|
|
215
|
+
out[prop] = out[prop] == null ? null : names.get(Number(out[prop])) ?? null;
|
|
216
|
+
}
|
|
217
|
+
return out;
|
|
218
|
+
};
|
|
219
|
+
/**
|
|
220
|
+
* The rows of `comp` that table `name` holds where `where` matches, each read
|
|
221
|
+
* as a whole read reads its component (references as eids, JSON parsed,
|
|
222
|
+
* booleans as booleans), by its owner's id. The table stands where the
|
|
223
|
+
* component's would, aliased by its name: its table of versions
|
|
224
|
+
* (./history.ts) does, so any version reads as the live row it was.
|
|
225
|
+
*/ export let valuesOf = (driver, vocab, comp, name, where, opts = {})=>{
|
|
226
|
+
let p = project(vocab, comp, opts.derived, opts.backed, opts.tags, true);
|
|
227
|
+
let rows = driver.query(select({
|
|
228
|
+
with: tagging(p, opts.tags),
|
|
229
|
+
cols: [
|
|
230
|
+
as(col('entity', comp), '@id'),
|
|
231
|
+
...p.sel
|
|
232
|
+
],
|
|
233
|
+
from: table(name, comp),
|
|
234
|
+
joins: p.joins,
|
|
235
|
+
where
|
|
236
|
+
}));
|
|
237
|
+
let names = naming(driver, rows.flatMap((r)=>p.refs?.map((x)=>r[x]) ?? []));
|
|
238
|
+
return rows.map(({ '@id': id, ...value })=>[
|
|
239
|
+
Number(id),
|
|
240
|
+
decoded(vocab, comp, named(value, p.refs, names))
|
|
241
|
+
]);
|
|
242
|
+
};
|
|
243
|
+
/** Whole components keyed by identity, for reads that select one version. */ export let rowsOf = (...args)=>new Map(valuesOf(...args));
|
|
244
|
+
// One component's read, whatever names its owners: `lead` is what is selected
|
|
245
|
+
// before the component's own columns, `owner` the condition on `o`, the
|
|
246
|
+
// owner's spine row.
|
|
247
|
+
let selectComp = (v, comp, derived, lead, owner)=>{
|
|
248
|
+
let { sel, joins } = project(v, comp, derived);
|
|
249
|
+
return select({
|
|
250
|
+
cols: [
|
|
251
|
+
...lead,
|
|
252
|
+
...sel.length ? sel : [
|
|
253
|
+
as(lit(1), 'present')
|
|
254
|
+
]
|
|
255
|
+
],
|
|
256
|
+
from: table(comp),
|
|
257
|
+
joins: [
|
|
258
|
+
join(table('entity', 'o'), eq(col('id', 'o'), col('entity', comp))),
|
|
259
|
+
...joins
|
|
260
|
+
],
|
|
261
|
+
where: owner
|
|
262
|
+
});
|
|
263
|
+
};
|
|
264
|
+
/**
|
|
265
|
+
* The SELECT that reads one component of one entity, named by its eid.
|
|
266
|
+
* Exported because gathering a bundle is every SQLite-shaped adapter's job,
|
|
267
|
+
* and they must all read a property the same way: @yaks/d1 sends these
|
|
268
|
+
* statements as one batch instead of one at a time, and nothing else differs.
|
|
269
|
+
*/ // Remote adapters already explicitly plan their component projections. Keep
|
|
270
|
+
// their established gather contract; SQLite's own get/read uses the opted-out
|
|
271
|
+
// projection above. An adapter can opt in through the property registry.
|
|
272
|
+
let complete = new WeakMap();
|
|
273
|
+
let completeDerived = (derived)=>at(complete, derived, ()=>({
|
|
274
|
+
...derived,
|
|
275
|
+
...Object.fromEntries(Object.entries(derived).filter(([, d])=>d.whole === false).map(([p, d])=>[
|
|
276
|
+
p,
|
|
277
|
+
{
|
|
278
|
+
...d,
|
|
279
|
+
whole: true
|
|
280
|
+
}
|
|
281
|
+
]))
|
|
282
|
+
}));
|
|
283
|
+
export let compSql = (v, comp, eid, derived = {})=>selectComp(v, comp, completeDerived(derived), [], eq(col('eid', 'o'), val(eid)));
|
|
284
|
+
/**
|
|
285
|
+
* The column a set-shaped read keys its rows by. Not a component prop — a prop
|
|
286
|
+
* is an identifier, so nothing in a vocabulary can collide with it.
|
|
287
|
+
*/ export let OWNER = '@eid';
|
|
288
|
+
/**
|
|
289
|
+
* The same read widened from one entity to a set: every entity `owners` names,
|
|
290
|
+
* each row carrying its owner's eid under {@link OWNER}. `owners` is a query
|
|
291
|
+
* selecting one `eid` column.
|
|
292
|
+
*
|
|
293
|
+
* This is what makes a whole read one round trip over a remote database
|
|
294
|
+
* (@yaks/d1 `wholeSql`): the hits are named by the query that found them
|
|
295
|
+
* instead of by eids a first trip had to go and fetch.
|
|
296
|
+
*/ export let setSql = (v, comp, owners, derived = {})=>selectComp(v, comp, completeDerived(derived), [
|
|
297
|
+
as(col('eid', 'o'), OWNER)
|
|
298
|
+
], among(col('eid', 'o'), owners));
|
|
299
|
+
/**
|
|
300
|
+
* An entity's tombstone, or null: the grave of the entity whose id `id` reads.
|
|
301
|
+
* A subquery on the tombstone's key rather than a join, so it is answered by
|
|
302
|
+
* that key whatever the planner's statistics remember: told a store's graves
|
|
303
|
+
* were few, SQLite would rather scan every one of them for each entity it reads
|
|
304
|
+
* than seek the one it needs.
|
|
305
|
+
*/ export let grave = (id)=>sub(select({
|
|
306
|
+
cols: [
|
|
307
|
+
col('entity', 't')
|
|
308
|
+
],
|
|
309
|
+
from: table('tombstone', 't'),
|
|
310
|
+
where: eq(col('entity', 't'), id)
|
|
311
|
+
}));
|
|
312
|
+
/**
|
|
313
|
+
* The spine rows of the entities `which` names, as `e`: each one's id, eid,
|
|
314
|
+
* number and grave, and its archetype's eid and table set where the
|
|
315
|
+
* vocabulary declares archetypes. What every whole read starts from. Left
|
|
316
|
+
* `kind`-only, it carries the archetype as the id the entity points at
|
|
317
|
+
* (`@kind`), which every file this package installs keeps, for a reader that
|
|
318
|
+
* knows the descriptors already (`kinded`).
|
|
319
|
+
*/ /** A spine row's number and the letter of its series, as an `entity` row
|
|
320
|
+
* carries them: nothing where it has no number, and no prefix on a row
|
|
321
|
+
* numbered before series had letters. */ export let numberOf = (row)=>row.num == null ? {} : {
|
|
322
|
+
num: Number(row.num),
|
|
323
|
+
...row.prefix == null ? {} : {
|
|
324
|
+
prefix: String(row.prefix)
|
|
325
|
+
}
|
|
326
|
+
};
|
|
327
|
+
export let spine = (vocab, which, kind = false)=>{
|
|
328
|
+
let typed = !!vocab.comp('archetype');
|
|
329
|
+
return select({
|
|
330
|
+
cols: [
|
|
331
|
+
col('id', 'e'),
|
|
332
|
+
col('eid', 'e'),
|
|
333
|
+
col('num', 'e'),
|
|
334
|
+
col('prefix', 'e'),
|
|
335
|
+
as(grave(col('id', 'e')), 'dead'),
|
|
336
|
+
...kind ? [
|
|
337
|
+
as(col('archetype', 'e'), '@kind')
|
|
338
|
+
] : !typed ? [] : [
|
|
339
|
+
as(col('eid', 'a'), 'archetype'),
|
|
340
|
+
as(col('tables', 'shape'), '@tables')
|
|
341
|
+
]
|
|
342
|
+
],
|
|
343
|
+
from: table('entity', 'e'),
|
|
344
|
+
joins: [
|
|
345
|
+
...typed && !kind ? [
|
|
346
|
+
left(table('entity', 'a'), eq(col('id', 'a'), col('archetype', 'e'))),
|
|
347
|
+
left(table('archetype', 'shape'), eq(col('entity', 'shape'), col('archetype', 'e')))
|
|
348
|
+
] : []
|
|
349
|
+
],
|
|
350
|
+
where: which
|
|
351
|
+
});
|
|
352
|
+
};
|
|
353
|
+
// The rows of `src` whose `key` is one of `ids`, driven from the requested
|
|
354
|
+
// set. SQLite estimates json_each at a fixed cardinality, so an IN subquery
|
|
355
|
+
// can choose a table scan when statistics remember a small component (even
|
|
356
|
+
// after its history grows); CROSS JOIN keeps the owners outside the indexed
|
|
357
|
+
// spine/component probes. One owner is an equality on the key instead, which
|
|
358
|
+
// reads no list.
|
|
359
|
+
let owning = (ids, src, key)=>ids.length == 1 ? {
|
|
360
|
+
from: src,
|
|
361
|
+
joins: [],
|
|
362
|
+
where: eq(key, val(ids[0]))
|
|
363
|
+
} : {
|
|
364
|
+
from: from(each(ids), '@owners'),
|
|
365
|
+
joins: [
|
|
366
|
+
cross(src)
|
|
367
|
+
],
|
|
368
|
+
where: eq(key, col('value', '@owners'))
|
|
369
|
+
};
|
|
370
|
+
let namedSpine = (vocab, eids)=>{
|
|
371
|
+
let { from, joins, where } = owning(eids, table('entity', 'e'), col('eid', 'e'));
|
|
372
|
+
let base = spine(vocab, where, true);
|
|
373
|
+
return select({
|
|
374
|
+
...base,
|
|
375
|
+
from,
|
|
376
|
+
joins: [
|
|
377
|
+
...joins,
|
|
378
|
+
...base.joins
|
|
379
|
+
]
|
|
380
|
+
});
|
|
381
|
+
};
|
|
382
|
+
// An archetype's eid and tables never change once its row stands, and a store
|
|
383
|
+
// holds a few dozen, so a read keeps them per connection rather than joining
|
|
384
|
+
// both into every entity it reads. A new descriptor, a deleted entity or a
|
|
385
|
+
// rollback starts the keeping over (@yaks/sql `revision`).
|
|
386
|
+
let kinds = new WeakMap();
|
|
387
|
+
/**
|
|
388
|
+
* Descriptors a patch wrote since revision `from`, as its entity's id, eid and
|
|
389
|
+
* table set, where nothing else moved one: what this connection keeps of the
|
|
390
|
+
* catalog (./catalog.ts) and of the archetypes its reads name takes them in
|
|
391
|
+
* rather than reading every descriptor again.
|
|
392
|
+
*/ export let described = (driver, from, rows)=>{
|
|
393
|
+
let cache = kinds.get(driver);
|
|
394
|
+
if (cache?.at == from) {
|
|
395
|
+
for (let r of rows)cache.held.set(r.id, {
|
|
396
|
+
...r
|
|
397
|
+
});
|
|
398
|
+
cache.at = revision(driver, 'descriptors');
|
|
399
|
+
}
|
|
400
|
+
learn(driver, from, rows);
|
|
401
|
+
};
|
|
402
|
+
let kinded = (driver, rows)=>{
|
|
403
|
+
let at = revision(driver, 'descriptors');
|
|
404
|
+
let cache = kinds.get(driver);
|
|
405
|
+
if (cache?.at != at) kinds.set(driver, cache = {
|
|
406
|
+
at,
|
|
407
|
+
held: new Map()
|
|
408
|
+
});
|
|
409
|
+
let { held } = cache;
|
|
410
|
+
let missing = [
|
|
411
|
+
...new Set(rows.flatMap((r)=>r['@kind'] == null || held.has(Number(r['@kind'])) ? [] : [
|
|
412
|
+
Number(r['@kind'])
|
|
413
|
+
]))
|
|
414
|
+
];
|
|
415
|
+
if (missing.length) {
|
|
416
|
+
let { from, joins, where } = owning(missing, table('entity', 'e'), col('id', 'e'));
|
|
417
|
+
let found = driver.query(select({
|
|
418
|
+
cols: [
|
|
419
|
+
col('id', 'e'),
|
|
420
|
+
col('eid', 'e'),
|
|
421
|
+
col('tables', 'a')
|
|
422
|
+
],
|
|
423
|
+
from,
|
|
424
|
+
joins: [
|
|
425
|
+
...joins,
|
|
426
|
+
left(table('archetype', 'a'), eq(col('entity', 'a'), col('id', 'e')))
|
|
427
|
+
],
|
|
428
|
+
where
|
|
429
|
+
}));
|
|
430
|
+
for (let row of found)held.set(Number(row.id), row);
|
|
431
|
+
}
|
|
432
|
+
for (let row of rows){
|
|
433
|
+
let kind = row['@kind'] == null ? undefined : held.get(Number(row['@kind']));
|
|
434
|
+
row.archetype = kind?.eid ?? null;
|
|
435
|
+
row['@tables'] = kind?.tables ?? null;
|
|
436
|
+
}
|
|
437
|
+
return rows;
|
|
438
|
+
};
|
|
439
|
+
// A previous table set is only a guess about which read to prepare. The read
|
|
440
|
+
// always fetches the current spine, descriptor and component values together;
|
|
441
|
+
// a descriptor that names another table sends it through the ordinary gather.
|
|
442
|
+
let shapes = new WeakMap();
|
|
443
|
+
let stored = new WeakMap();
|
|
444
|
+
let readable = (vocab)=>at(stored, vocab, ()=>new Set(tables(vocab)));
|
|
445
|
+
// A joined read rendered for one entity and for many, each with the place of
|
|
446
|
+
// the bind that names them, which holds `ANY` until a read names its own.
|
|
447
|
+
let ANY = '\0';
|
|
448
|
+
let joined = new WeakMap();
|
|
449
|
+
let bounded = (cache, key, value, limit)=>{
|
|
450
|
+
if (cache.size >= limit && !cache.has(key)) {
|
|
451
|
+
cache.delete(cache.keys().next().value);
|
|
452
|
+
}
|
|
453
|
+
cache.set(key, value);
|
|
454
|
+
return value;
|
|
455
|
+
};
|
|
456
|
+
// Each facet's references and derived dependencies have their own scope.
|
|
457
|
+
// Inline joins retain indexed owner probes; a LEFT JOIN over a subquery with
|
|
458
|
+
// its own joins would make SQLite materialize that facet's entire table.
|
|
459
|
+
let scoped = (p, name, prefix)=>{
|
|
460
|
+
let aliases = new Map([
|
|
461
|
+
[
|
|
462
|
+
name,
|
|
463
|
+
prefix
|
|
464
|
+
]
|
|
465
|
+
]);
|
|
466
|
+
for (let j of p.joins){
|
|
467
|
+
if (j.src.t != 'table') return null;
|
|
468
|
+
let alias = j.src.as ?? j.src.name;
|
|
469
|
+
aliases.set(alias, `${prefix}:${alias}`);
|
|
470
|
+
}
|
|
471
|
+
let raw = false;
|
|
472
|
+
let rename = (node)=>{
|
|
473
|
+
if (!node || typeof node != 'object') return node;
|
|
474
|
+
if (Array.isArray(node)) return node.map(rename);
|
|
475
|
+
let value = node;
|
|
476
|
+
// Bound/literal values are data, not SQL trees. A lowered fragment cannot
|
|
477
|
+
// have its identifiers renamed, so it retains the ordinary gather.
|
|
478
|
+
if (value.t == 'val' || value.t == 'lit') return node;
|
|
479
|
+
if (value.t == 'raw') {
|
|
480
|
+
// A compiler-proven closed relation has no outer aliases to rename.
|
|
481
|
+
// Other fragments might refer to this row, even inside FROM subqueries.
|
|
482
|
+
if (!value.closed) raw = true;
|
|
483
|
+
return node;
|
|
484
|
+
}
|
|
485
|
+
if (value.t == 'col') {
|
|
486
|
+
return {
|
|
487
|
+
...value,
|
|
488
|
+
of: aliases.get(String(value.of)) ?? value.of
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
if (value.t == 'table') {
|
|
492
|
+
return {
|
|
493
|
+
...value,
|
|
494
|
+
as: aliases.get(String(value.as ?? value.name)) ?? value.as
|
|
495
|
+
};
|
|
496
|
+
}
|
|
497
|
+
return Object.fromEntries(Object.entries(value).map(([k, v])=>[
|
|
498
|
+
k,
|
|
499
|
+
rename(v)
|
|
500
|
+
]));
|
|
501
|
+
};
|
|
502
|
+
let out = rename(p);
|
|
503
|
+
return raw ? null : out;
|
|
504
|
+
};
|
|
505
|
+
let joinedPlan = (vocab, names, opts)=>{
|
|
506
|
+
let cache = at(at(at(joined, vocab, ()=>new WeakMap()), opts.derived ?? NONE, ()=>new WeakMap()), opts.backed ?? NONE, ()=>new Map());
|
|
507
|
+
let key = JSON.stringify([
|
|
508
|
+
names,
|
|
509
|
+
Object.keys(opts.tags ?? {})
|
|
510
|
+
]);
|
|
511
|
+
if (cache.has(key)) return cache.get(key);
|
|
512
|
+
let base = namedSpine(vocab, [
|
|
513
|
+
ANY,
|
|
514
|
+
ANY
|
|
515
|
+
]);
|
|
516
|
+
let columns = [
|
|
517
|
+
...base.cols
|
|
518
|
+
];
|
|
519
|
+
let props = [];
|
|
520
|
+
let reading = new Set();
|
|
521
|
+
let width = columns.length;
|
|
522
|
+
for (let [i, name] of names.entries()){
|
|
523
|
+
let alias = `@g${i}`;
|
|
524
|
+
let p = scoped(project(vocab, name, opts.derived, opts.backed, opts.tags), name, alias);
|
|
525
|
+
// A row is packed as JSON, which holds no bytes: a component with a
|
|
526
|
+
// binary property keeps the set-shaped gather.
|
|
527
|
+
let bytes = vocab.props(name).some((prop)=>vocab.prop(name, prop)?.scalar == 'binary');
|
|
528
|
+
if (!p || bytes) return bounded(cache, key, null, 128);
|
|
529
|
+
p.tagged?.forEach((c)=>reading.add(c));
|
|
530
|
+
let fields = p.sel.map((e)=>{
|
|
531
|
+
if (e.t != 'as' && e.t != 'col') {
|
|
532
|
+
throw Error('a projected property needs a name');
|
|
533
|
+
}
|
|
534
|
+
return e.name;
|
|
535
|
+
});
|
|
536
|
+
props.push(fields);
|
|
537
|
+
width += fields.length + 1;
|
|
538
|
+
// The outer read and every nested projection fit workerd's column and
|
|
539
|
+
// SQLite's joined-table limits. Wide entities keep the set-shaped gather.
|
|
540
|
+
if (width > 100 || p.joins.length + 1 > 60) {
|
|
541
|
+
return bounded(cache, key, null, 128);
|
|
542
|
+
}
|
|
543
|
+
// Read every value in one primary-key lookup. A scalar lookup pins the
|
|
544
|
+
// plan even for one-row statistics; packing the row avoids billing that
|
|
545
|
+
// same lookup again for each property. Concatenation drops JSON's subtype
|
|
546
|
+
// so every adapter returns the same text for this internal projection.
|
|
547
|
+
columns.push(as(sub(select({
|
|
548
|
+
cols: [
|
|
549
|
+
op('||', fn('json_array', col('entity', alias), ...p.sel.map((e)=>e.t == 'as' ? e.e : e)), lit(''))
|
|
550
|
+
],
|
|
551
|
+
from: table(name, alias),
|
|
552
|
+
joins: p.joins,
|
|
553
|
+
where: eq(col('entity', alias), col('id', 'e'))
|
|
554
|
+
})), `@${i}`));
|
|
555
|
+
}
|
|
556
|
+
// Bound first, each store's own tags are the statement's first parameters.
|
|
557
|
+
let tags = [
|
|
558
|
+
...reading
|
|
559
|
+
];
|
|
560
|
+
let blank = Object.fromEntries(tags.map((c)=>[
|
|
561
|
+
c,
|
|
562
|
+
''
|
|
563
|
+
]));
|
|
564
|
+
let named = (eids)=>{
|
|
565
|
+
let spine = namedSpine(vocab, eids);
|
|
566
|
+
let statement = render(select({
|
|
567
|
+
...spine,
|
|
568
|
+
with: tagged(blank, tags),
|
|
569
|
+
cols: columns
|
|
570
|
+
}), undefined, undefined, vocab);
|
|
571
|
+
let bind = eids.length == 1 ? eids[0] : JSON.stringify(eids);
|
|
572
|
+
return {
|
|
573
|
+
statement,
|
|
574
|
+
at: statement.params.indexOf(bind)
|
|
575
|
+
};
|
|
576
|
+
};
|
|
577
|
+
let one = named([
|
|
578
|
+
ANY
|
|
579
|
+
]), many = named([
|
|
580
|
+
ANY,
|
|
581
|
+
ANY
|
|
582
|
+
]);
|
|
583
|
+
if ([
|
|
584
|
+
one,
|
|
585
|
+
many
|
|
586
|
+
].some(({ statement })=>statement.sql.length > 90_000 || statement.params.length > 100)) return bounded(cache, key, null, 128);
|
|
587
|
+
return bounded(cache, key, {
|
|
588
|
+
one,
|
|
589
|
+
many,
|
|
590
|
+
names,
|
|
591
|
+
props,
|
|
592
|
+
tagged: tags
|
|
593
|
+
}, 128);
|
|
594
|
+
};
|
|
595
|
+
let joinedGet = (driver, vocab, eids, opts, names, wanted, unsettled)=>{
|
|
596
|
+
let plan = joinedPlan(readingVocab(driver, vocab), names, opts);
|
|
597
|
+
if (!plan) return;
|
|
598
|
+
let { statement, at } = eids.length == 1 ? plan.one : plan.many;
|
|
599
|
+
let params = [
|
|
600
|
+
...statement.params
|
|
601
|
+
];
|
|
602
|
+
plan.tagged.forEach((c, i)=>params[i] = opts.tags[c]);
|
|
603
|
+
params[at] = eids.length == 1 ? eids[0] : JSON.stringify(eids);
|
|
604
|
+
let rows;
|
|
605
|
+
try {
|
|
606
|
+
// A read of named components asks the tables it names: only a store
|
|
607
|
+
// that shows archetypes needs the descriptors for it.
|
|
608
|
+
rows = driver.query({
|
|
609
|
+
...statement,
|
|
610
|
+
params
|
|
611
|
+
});
|
|
612
|
+
if (!wanted || vocab.comp('archetype')) rows = kinded(driver, rows);
|
|
613
|
+
} catch (err) {
|
|
614
|
+
// A raw schema edit can remove a table from the previous shape. Read the
|
|
615
|
+
// current descriptor before deciding which remaining tables to gather.
|
|
616
|
+
if (err instanceof Error && /no such (table|column):/i.test(err.message)) {
|
|
617
|
+
return;
|
|
618
|
+
}
|
|
619
|
+
throw err;
|
|
620
|
+
}
|
|
621
|
+
let found = new Map();
|
|
622
|
+
let numbered = !!vocab.prop('entity', 'num');
|
|
623
|
+
let typed = !!vocab.comp('archetype');
|
|
624
|
+
let covered = new Set(names);
|
|
625
|
+
for (let row of rows){
|
|
626
|
+
let entity = {
|
|
627
|
+
eid: String(row.eid),
|
|
628
|
+
...numbered ? numberOf(row) : {},
|
|
629
|
+
...!typed || row.archetype == null ? {} : {
|
|
630
|
+
archetype: String(row.archetype)
|
|
631
|
+
}
|
|
632
|
+
};
|
|
633
|
+
let bundle = row.dead == null ? {
|
|
634
|
+
entity
|
|
635
|
+
} : tombstoned(entity);
|
|
636
|
+
found.set(entity.eid, bundle);
|
|
637
|
+
if (row.dead != null) continue;
|
|
638
|
+
let current = row['@tables'] == null || unsettled?.wrote(entity.eid) ? undefined : descriptor(driver, String(row['@tables'])).tables;
|
|
639
|
+
if (wanted == null && (!current || current.some((name)=>readable(vocab).has(name) && !covered.has(name)))) return {
|
|
640
|
+
spine: rows
|
|
641
|
+
};
|
|
642
|
+
for (let [i, name] of names.entries()){
|
|
643
|
+
if (row[`@${i}`] == null || current && !current.includes(name)) continue;
|
|
644
|
+
let values = JSON.parse(String(row[`@${i}`]));
|
|
645
|
+
bundle[name] = decoded(vocab, name, Object.fromEntries(plan.props[i].map((prop, j)=>[
|
|
646
|
+
prop,
|
|
647
|
+
values[j + 1]
|
|
648
|
+
])));
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
backedGet(driver, vocab, eids.filter((eid)=>!found.has(eid)), opts, wanted).forEach((b)=>found.set(b.entity.eid, b));
|
|
652
|
+
return eids.flatMap((eid)=>found.get(eid) ?? []);
|
|
653
|
+
};
|
|
654
|
+
export { Unpointed } from "./error.js";
|
|
655
|
+
/** The eids, of `eids`, whose entities a scope's `screen` admits. Each
|
|
656
|
+
* requested row is tested against the screen by its id, so the statement reads
|
|
657
|
+
* the rows asked about and never lists everything the scope sees: `id in
|
|
658
|
+
* (screen)` builds that list on every call, at the cost of a scan. */ export let admitted = (eids, screen)=>select({
|
|
659
|
+
cols: [
|
|
660
|
+
col('eid', '__requested')
|
|
661
|
+
],
|
|
662
|
+
from: table('entity', '__requested'),
|
|
663
|
+
where: and(oneOf(col('eid', '__requested'), eids), screen ? exists(select({
|
|
664
|
+
cols: [
|
|
665
|
+
lit(1)
|
|
666
|
+
],
|
|
667
|
+
from: from(screen, '__allowed'),
|
|
668
|
+
where: eq(col('id', '__allowed'), col('id', '__requested'))
|
|
669
|
+
})) : lit(true))
|
|
670
|
+
});
|
|
671
|
+
// `answered`: the eids are what a query under `opts.visibility` answered, in
|
|
672
|
+
// this same snapshot, so the scope has admitted each one already. Their
|
|
673
|
+
// aspects and the entities they reference are still put to it.
|
|
674
|
+
export let get = (driver, vocab, eids, opts = {}, comps, unsettled, cache, answered = false)=>{
|
|
675
|
+
if (!eids.length) return [];
|
|
676
|
+
// The tables this read may touch: every component's, or the named ones',
|
|
677
|
+
// less any the file does not hold yet, which no entity can wear: those a
|
|
678
|
+
// reader was told of, else the ones no write has raised (./raise.ts). A
|
|
679
|
+
// caller's own read overrides read those as worn by nobody too.
|
|
680
|
+
let absent = opts.absent ?? unraised(driver, vocab);
|
|
681
|
+
if (absent.length) {
|
|
682
|
+
opts = {
|
|
683
|
+
...opts,
|
|
684
|
+
derived: absentDerived(vocab, derivedOf(vocab, opts.derived), absent)
|
|
685
|
+
};
|
|
686
|
+
}
|
|
687
|
+
if (opts.visibility) {
|
|
688
|
+
let { visibility, ...plain } = opts;
|
|
689
|
+
// A backed entity has no entity row: it is seen when its backing, as the
|
|
690
|
+
// scope sees it, holds its row, whatever components are asked about.
|
|
691
|
+
let tags = Object.values(plain.tags ?? {});
|
|
692
|
+
let tagged = (eid)=>tags.some((tag)=>idOf(tag, eid) != null);
|
|
693
|
+
let seen = (ids)=>backedGet(driver, vocab, ids, {
|
|
694
|
+
...plain,
|
|
695
|
+
backed: visibleBacked(vocab, opts)
|
|
696
|
+
}, []).map((b)=>b.entity.eid);
|
|
697
|
+
// A getter's eids are physical identities. Query addressing may interpret
|
|
698
|
+
// a human id such as history-1 as number 1, which cannot decide ownership.
|
|
699
|
+
// A deleted entity the scope sees (@yaks/sql `ghostly`) is admitted: it
|
|
700
|
+
// is gotten as its tombstone, as without a scope, and a reference names
|
|
701
|
+
// it, as a query's reference operand does (./mod.ts `resolved`).
|
|
702
|
+
let permitted = (all, components = [])=>{
|
|
703
|
+
let backed = tags.length ? all.filter(tagged) : [];
|
|
704
|
+
let ids = backed.length ? all.filter((eid)=>!tagged(eid)) : all;
|
|
705
|
+
let out = new Set(seen(backed));
|
|
706
|
+
if (!ids.length) return out;
|
|
707
|
+
if (!visibility.entity && !components.some((name)=>visibility.components?.[name])) {
|
|
708
|
+
for (let eid of ids)out.add(eid);
|
|
709
|
+
return out;
|
|
710
|
+
}
|
|
711
|
+
let run = ()=>driver.query(admitted(ids, sourceScreen(readingVocab(driver, vocab), {
|
|
712
|
+
...opts,
|
|
713
|
+
graves: true
|
|
714
|
+
}, components)));
|
|
715
|
+
for (let r of cache ? cache.permit(ids, components, run, true) : run()){
|
|
716
|
+
out.add(String(r.eid));
|
|
717
|
+
}
|
|
718
|
+
return out;
|
|
719
|
+
};
|
|
720
|
+
// The payload cache answers first, so only what is there is put to the
|
|
721
|
+
// scope: an id no entity has (most an address asks about, @yaks/alias)
|
|
722
|
+
// costs no screen.
|
|
723
|
+
let held = cache?.fetch(eids, comps);
|
|
724
|
+
let allowed = answered ? new Set(eids) : permitted(held ? held.map((b)=>b.entity.eid) : eids);
|
|
725
|
+
let ids = eids.filter((eid)=>allowed.has(eid));
|
|
726
|
+
let scoped = ()=>({
|
|
727
|
+
...plain,
|
|
728
|
+
derived: visibleDerived(vocab, opts)
|
|
729
|
+
});
|
|
730
|
+
let found = held ? held.filter((b)=>allowed.has(b.entity.eid)) : get(driver, vocab, ids, scoped(), comps, unsettled);
|
|
731
|
+
if (cache) {
|
|
732
|
+
// The payload cache holds authoritative rows. Every derived component
|
|
733
|
+
// is read again through scoped sources before the payload is exposed.
|
|
734
|
+
let names = new Set(Object.entries(derivedOf(vocab, opts.derived)).filter(([, d])=>!storedOnly(d, vocab)).map(([p])=>p.split('.')[0]));
|
|
735
|
+
let dynamic = [
|
|
736
|
+
...names
|
|
737
|
+
].filter((name)=>found.some((b)=>b[name]));
|
|
738
|
+
if (dynamic.length) {
|
|
739
|
+
let current = new Map(get(driver, vocab, ids, scoped(), dynamic, unsettled).map((b)=>[
|
|
740
|
+
b.entity.eid,
|
|
741
|
+
b
|
|
742
|
+
]));
|
|
743
|
+
found = found.map((b)=>{
|
|
744
|
+
let out = {
|
|
745
|
+
...b
|
|
746
|
+
};
|
|
747
|
+
for (let name of dynamic)delete out[name];
|
|
748
|
+
return {
|
|
749
|
+
...out,
|
|
750
|
+
...current.get(b.entity.eid)
|
|
751
|
+
};
|
|
752
|
+
});
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
let described = found.some((b)=>b.entity.archetype);
|
|
756
|
+
// A component the file holds no table for is worn by nobody, so it hides
|
|
757
|
+
// nothing and no statement names its table.
|
|
758
|
+
let aspects = Object.keys(visibility.components ?? {}).filter((name)=>!setOf(absent).has(name)).filter((name)=>described || found.some((b)=>b[name])).map((name)=>[
|
|
759
|
+
name,
|
|
760
|
+
permitted(eids, [
|
|
761
|
+
name
|
|
762
|
+
])
|
|
763
|
+
]);
|
|
764
|
+
let hidden = new Set();
|
|
765
|
+
if (described) {
|
|
766
|
+
for (let [name, owners] of aspects){
|
|
767
|
+
let wearing = comps && !comps.includes(name) ? driver.query(select({
|
|
768
|
+
cols: [
|
|
769
|
+
col('eid', '__requested')
|
|
770
|
+
],
|
|
771
|
+
from: table('entity', '__requested'),
|
|
772
|
+
joins: [
|
|
773
|
+
join(table(name, '__aspect'), eq(col('entity', '__aspect'), col('id', '__requested')))
|
|
774
|
+
],
|
|
775
|
+
where: oneOf(col('eid', '__requested'), eids)
|
|
776
|
+
})).map((r)=>String(r.eid)) : found.filter((b)=>!!b[name]).map((b)=>b.entity.eid);
|
|
777
|
+
for (let eid of wearing){
|
|
778
|
+
if (!owners.has(eid)) hidden.add(eid);
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
let targets = new Set();
|
|
783
|
+
for (let b of found){
|
|
784
|
+
for (let [name, value] of Object.entries(b)){
|
|
785
|
+
if (name == 'entity' || !value || typeof value != 'object') continue;
|
|
786
|
+
for (let prop of vocab.props(name)){
|
|
787
|
+
let v = value[prop];
|
|
788
|
+
if (vocab.prop(name, prop)?.category == 'ref' && typeof v == 'string') targets.add(v);
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
let references = visibility.entity && targets.size ? permitted([
|
|
793
|
+
...targets
|
|
794
|
+
]) : targets;
|
|
795
|
+
return found.map((b)=>{
|
|
796
|
+
let out = {
|
|
797
|
+
...b,
|
|
798
|
+
entity: {
|
|
799
|
+
...b.entity
|
|
800
|
+
}
|
|
801
|
+
};
|
|
802
|
+
if (hidden.has(b.entity.eid)) delete out.entity.archetype;
|
|
803
|
+
for (let [name, owners] of aspects){
|
|
804
|
+
if (!owners.has(b.entity.eid)) delete out[name];
|
|
805
|
+
}
|
|
806
|
+
for (let [name, value] of Object.entries(out)){
|
|
807
|
+
if (name == 'entity' || !value || typeof value != 'object') continue;
|
|
808
|
+
let component = value;
|
|
809
|
+
for (let prop of vocab.props(name)){
|
|
810
|
+
if (vocab.prop(name, prop)?.category == 'ref' && typeof component[prop] == 'string' && !references.has(component[prop])) {
|
|
811
|
+
component = {
|
|
812
|
+
...component,
|
|
813
|
+
[prop]: null
|
|
814
|
+
};
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
out[name] = component;
|
|
818
|
+
}
|
|
819
|
+
return out;
|
|
820
|
+
});
|
|
821
|
+
}
|
|
822
|
+
let pending = eids.filter((eid)=>unsettled?.wrote(eid));
|
|
823
|
+
let names = (comps ? [
|
|
824
|
+
...new Set(comps)
|
|
825
|
+
].filter((c)=>readable(vocab).has(c)) : tables(vocab)).filter((c)=>!setOf(absent).has(c));
|
|
826
|
+
let remembered = at(at(shapes, driver, ()=>new WeakMap()), logicalVocab(vocab), ()=>new Map());
|
|
827
|
+
let initial;
|
|
828
|
+
// Small, repeated identity reads benefit from one statement. Large gathers
|
|
829
|
+
// still read each worn table once for all its owners, rather than joining
|
|
830
|
+
// the union of every different entity's facets.
|
|
831
|
+
if (eids.length && eids.length <= 32 && !comps && !pending.length && eids.every((eid)=>remembered.has(eid))) {
|
|
832
|
+
let descriptors = eids.filter((eid)=>{
|
|
833
|
+
let names = remembered.get(eid);
|
|
834
|
+
return names.length == 1 && names[0] == 'archetype';
|
|
835
|
+
});
|
|
836
|
+
if (descriptors.length && descriptors.length < eids.length) {
|
|
837
|
+
let owners = eids.filter((eid)=>!descriptors.includes(eid));
|
|
838
|
+
let a = joinedGet(driver, vocab, descriptors, opts, [
|
|
839
|
+
'archetype'
|
|
840
|
+
], undefined, unsettled);
|
|
841
|
+
let names = [
|
|
842
|
+
...new Set(owners.flatMap((eid)=>[
|
|
843
|
+
...remembered.get(eid)
|
|
844
|
+
]))
|
|
845
|
+
].sort();
|
|
846
|
+
let b = joinedGet(driver, vocab, owners, opts, names, undefined, unsettled);
|
|
847
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
848
|
+
let at = new Map([
|
|
849
|
+
...a,
|
|
850
|
+
...b
|
|
851
|
+
].map((row)=>[
|
|
852
|
+
row.entity.eid,
|
|
853
|
+
row
|
|
854
|
+
]));
|
|
855
|
+
return eids.flatMap((eid)=>at.get(eid) ?? []);
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
if (eids.length && eids.length <= 32) {
|
|
860
|
+
let guess = comps ? names : eids.every((eid)=>remembered.has(eid)) ? [
|
|
861
|
+
...new Set(eids.flatMap((eid)=>[
|
|
862
|
+
...remembered.get(eid)
|
|
863
|
+
]))
|
|
864
|
+
].sort() : undefined;
|
|
865
|
+
if (guess) {
|
|
866
|
+
let rows = joinedGet(driver, vocab, eids, opts, guess, comps, unsettled);
|
|
867
|
+
if (Array.isArray(rows)) return rows;
|
|
868
|
+
initial = rows?.spine;
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
let asked = new Set(names);
|
|
872
|
+
let found = new Map();
|
|
873
|
+
// Whether a number is this store's to show. The spine table holds the column
|
|
874
|
+
// in every layout, but the number is @yaks/id's word and a store that never
|
|
875
|
+
// loaded it has none to speak of — including one that was numbered before the
|
|
876
|
+
// plugin was opt in, whose old rows still carry the value. Read off the
|
|
877
|
+
// vocabulary, as the archetype is: every file keeps the pointer, and a store
|
|
878
|
+
// shows it where its vocabulary declares archetypes (@yaks/archetype).
|
|
879
|
+
let numbered = !!vocab.prop('entity', 'num');
|
|
880
|
+
let typed = !!vocab.comp('archetype');
|
|
881
|
+
// Bound parameter count and SQL-cache size; gather a component per set,
|
|
882
|
+
// never every component per entity (a 1,000-entry transcript is otherwise
|
|
883
|
+
// tens of thousands of queries). A set of owners is a collection (@yaks/sql
|
|
884
|
+
// `each`), written the way the host reads one cheapest.
|
|
885
|
+
// Materialize the selected ids once: re-running a window subquery
|
|
886
|
+
// for each component could select different owners under a concurrent writer.
|
|
887
|
+
// Keep caller order and duplicate semantics.
|
|
888
|
+
// Entities nothing says the tables of, across every part of the read.
|
|
889
|
+
let lost = [];
|
|
890
|
+
for(let i = 0; i < eids.length; i += 4096){
|
|
891
|
+
let ids = eids.slice(i, i + 4096);
|
|
892
|
+
let byId = new Map();
|
|
893
|
+
let groups = new Map();
|
|
894
|
+
let grouped = (key, tables, id)=>{
|
|
895
|
+
let group = groups.get(key);
|
|
896
|
+
if (!group) groups.set(key, group = {
|
|
897
|
+
tables,
|
|
898
|
+
ids: []
|
|
899
|
+
});
|
|
900
|
+
group.ids.push(id);
|
|
901
|
+
};
|
|
902
|
+
// The entities whose pointer does not say what they hold.
|
|
903
|
+
let open = [];
|
|
904
|
+
let spines = initial ?? driver.query(namedSpine(vocab, ids));
|
|
905
|
+
// A projection onto one private facet already knows its table. Wider
|
|
906
|
+
// projections use the stored shapes even when readers cannot see them.
|
|
907
|
+
if (!initial && (typed || !comps || names.length > 1)) {
|
|
908
|
+
kinded(driver, spines);
|
|
909
|
+
}
|
|
910
|
+
for (let row of spines){
|
|
911
|
+
let eid = String(row.eid);
|
|
912
|
+
let entity = {
|
|
913
|
+
eid,
|
|
914
|
+
...numbered ? numberOf(row) : {},
|
|
915
|
+
...!typed || row.archetype == null ? {} : {
|
|
916
|
+
archetype: String(row.archetype)
|
|
917
|
+
}
|
|
918
|
+
};
|
|
919
|
+
let bundle = row.dead == null ? {
|
|
920
|
+
entity
|
|
921
|
+
} : tombstoned(entity);
|
|
922
|
+
found.set(eid, bundle);
|
|
923
|
+
byId.set(Number(row.id), bundle);
|
|
924
|
+
if (row.dead != null) {
|
|
925
|
+
bounded(remembered, eid, [], 2048);
|
|
926
|
+
continue;
|
|
927
|
+
}
|
|
928
|
+
// What an entity holds is what its archetype names. No table is asked.
|
|
929
|
+
let pointed = row['@tables'] == null ? null : descriptor(driver, String(row['@tables'])).tables;
|
|
930
|
+
if (!pointed || unsettled?.wrote(eid)) {
|
|
931
|
+
open.push({
|
|
932
|
+
eid,
|
|
933
|
+
id: Number(row.id),
|
|
934
|
+
pointed
|
|
935
|
+
});
|
|
936
|
+
continue;
|
|
937
|
+
}
|
|
938
|
+
bounded(remembered, eid, pointed.filter((c)=>names.includes(c)), 2048);
|
|
939
|
+
// Grouped by the set's names, as an entity nothing points for is: a
|
|
940
|
+
// set described in two forms (identities, and labels from before) is
|
|
941
|
+
// read once.
|
|
942
|
+
grouped(JSON.stringify(pointed), pointed, Number(row.id));
|
|
943
|
+
}
|
|
944
|
+
if (open.length) {
|
|
945
|
+
// The one that knows better says (`unsettled`). A read of named
|
|
946
|
+
// components asks the tables it names, which is its own question; a
|
|
947
|
+
// whole read of an entity nothing speaks for has no table to ask.
|
|
948
|
+
let held = unsettled?.held(open, names);
|
|
949
|
+
for (let { eid, id } of open){
|
|
950
|
+
let tables = held?.get(id) ?? (comps ? names : undefined);
|
|
951
|
+
if (tables) grouped(JSON.stringify(tables), tables, id);
|
|
952
|
+
else lost.push(eid);
|
|
953
|
+
}
|
|
954
|
+
if (lost.length) continue;
|
|
955
|
+
}
|
|
956
|
+
let compOwners = new Map();
|
|
957
|
+
// Group once per table set, then read each present table once across all
|
|
958
|
+
// its owner groups.
|
|
959
|
+
for (let { tables, ids } of groups.values()){
|
|
960
|
+
for (let comp of tables){
|
|
961
|
+
if (!asked.has(comp)) continue;
|
|
962
|
+
let held = compOwners.get(comp);
|
|
963
|
+
if (held) held.push(...ids);
|
|
964
|
+
else compOwners.set(comp, [
|
|
965
|
+
...ids
|
|
966
|
+
]);
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
// The spine pass already resolved every owner's storage id. Do not join
|
|
970
|
+
// it again for each component just to recover the eid we already hold;
|
|
971
|
+
// the references every component's rows hold are named once, together.
|
|
972
|
+
let read = [];
|
|
973
|
+
for (let [comp, ids] of compOwners){
|
|
974
|
+
let p = project(vocab, comp, opts.derived, opts.backed, opts.tags, true);
|
|
975
|
+
let { sel, joins, refs } = p;
|
|
976
|
+
let owners = owning(ids, table(comp), col('entity', comp));
|
|
977
|
+
for (let row of driver.query(select({
|
|
978
|
+
with: tagging(p, opts.tags),
|
|
979
|
+
cols: [
|
|
980
|
+
as(col('entity', comp), '@id'),
|
|
981
|
+
...sel
|
|
982
|
+
],
|
|
983
|
+
...owners,
|
|
984
|
+
joins: [
|
|
985
|
+
...owners.joins,
|
|
986
|
+
...joins
|
|
987
|
+
]
|
|
988
|
+
}))){
|
|
989
|
+
let { '@id': owner, ...value } = row;
|
|
990
|
+
let b = byId.get(Number(owner));
|
|
991
|
+
if ('tombstone' in b) continue;
|
|
992
|
+
read.push([
|
|
993
|
+
b,
|
|
994
|
+
comp,
|
|
995
|
+
value,
|
|
996
|
+
refs
|
|
997
|
+
]);
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
let refNames = naming(driver, read.flatMap(([, , value, refs])=>refs?.map((p)=>value[p]) ?? []));
|
|
1001
|
+
for (let [b, comp, value, refs] of read){
|
|
1002
|
+
b[comp] = decoded(vocab, comp, named(value, refs, refNames));
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
if (lost.length) throw new Unpointed(lost);
|
|
1006
|
+
backedGet(driver, vocab, eids.filter((e)=>!found.has(e)), opts, comps).forEach((b)=>found.set(b.entity.eid, b));
|
|
1007
|
+
for (let eid of eids){
|
|
1008
|
+
if (!found.has(eid)) bounded(remembered, eid, [], 2048);
|
|
1009
|
+
}
|
|
1010
|
+
return eids.flatMap((eid)=>found.has(eid) ? [
|
|
1011
|
+
found.get(eid)
|
|
1012
|
+
] : []);
|
|
1013
|
+
};
|
|
1014
|
+
// The entities a computed component's rows hold, among eids the entity table
|
|
1015
|
+
// does not: each backing's own, read back out of the eid and gathered from its
|
|
1016
|
+
// rows (@yaks/sql `Backing`). An eid no backing's tag names is left out, as an
|
|
1017
|
+
// unknown one is; one whose row is gone is absent too.
|
|
1018
|
+
let backedGet = (driver, vocab, eids, opts, comps)=>Object.entries(opts.backed ?? {}).flatMap(([comp, b])=>{
|
|
1019
|
+
let tag = opts.tags?.[comp];
|
|
1020
|
+
let ids = tag ? eids.map((e)=>idOf(tag, e)).filter((id)=>id != null) : [];
|
|
1021
|
+
if (!tag || !ids.length) return [];
|
|
1022
|
+
let asked = !comps || comps.includes(comp);
|
|
1023
|
+
let p = asked ? project(vocab, comp, opts.derived, opts.backed, opts.tags) : {
|
|
1024
|
+
sel: [],
|
|
1025
|
+
joins: []
|
|
1026
|
+
};
|
|
1027
|
+
return driver.query(select({
|
|
1028
|
+
with: tagging(p, opts.tags),
|
|
1029
|
+
cols: [
|
|
1030
|
+
as(col('entity', comp), '@id'),
|
|
1031
|
+
...p.sel
|
|
1032
|
+
],
|
|
1033
|
+
from: from(b.rows, comp),
|
|
1034
|
+
joins: p.joins,
|
|
1035
|
+
where: oneOf(col('entity', comp), ids)
|
|
1036
|
+
})).map(({ '@id': id, ...value })=>({
|
|
1037
|
+
entity: {
|
|
1038
|
+
eid: eidOf(tag, Number(id))
|
|
1039
|
+
},
|
|
1040
|
+
...asked ? {
|
|
1041
|
+
[comp]: decoded(vocab, comp, value)
|
|
1042
|
+
} : {}
|
|
1043
|
+
}));
|
|
1044
|
+
});
|
|
1045
|
+
/** The tag a computed component's eids end with in a store: the store's epoch
|
|
1046
|
+
* and the component, hashed (@yaks/sql `eidOf`) — so a record's eid names it
|
|
1047
|
+
* in this store and no other. */ export let tagOf = (epoch, comp)=>sha256(`${epoch}|${comp}`).slice(0, 32);
|
|
1048
|
+
// The matched entities, with only `comps` when named. A membership query
|
|
1049
|
+
// returns entities; an aggregate query wants `rows()` instead.
|
|
1050
|
+
// An explicit property query opts into a derived value omitted by a whole
|
|
1051
|
+
// gather. Registries keep their identity so the projection caches still hit.
|
|
1052
|
+
let explicit = new WeakMap();
|
|
1053
|
+
let requestedDerived = (query, derived)=>{
|
|
1054
|
+
let wanted = new Set();
|
|
1055
|
+
let path = (segments)=>{
|
|
1056
|
+
let key = segments.join('.');
|
|
1057
|
+
if (derived[key]?.whole === false) wanted.add(key);
|
|
1058
|
+
};
|
|
1059
|
+
map(query, (clause)=>{
|
|
1060
|
+
if ('path' in clause) path(clause.path);
|
|
1061
|
+
if (clause.kind == 'order') path(clause.value.replace(/^-/, '').split('.'));
|
|
1062
|
+
if (clause.kind == 'fields') clause.fields.forEach((f)=>path(f.path));
|
|
1063
|
+
return clause;
|
|
1064
|
+
});
|
|
1065
|
+
if (!wanted.size) return derived;
|
|
1066
|
+
let key = [
|
|
1067
|
+
...wanted
|
|
1068
|
+
].sort().join(',');
|
|
1069
|
+
let cache = at(explicit, derived, ()=>new Map());
|
|
1070
|
+
return at(cache, key, ()=>({
|
|
1071
|
+
...derived,
|
|
1072
|
+
...Object.fromEntries([
|
|
1073
|
+
...wanted
|
|
1074
|
+
].map((p)=>[
|
|
1075
|
+
p,
|
|
1076
|
+
{
|
|
1077
|
+
...derived[p],
|
|
1078
|
+
whole: true
|
|
1079
|
+
}
|
|
1080
|
+
]))
|
|
1081
|
+
}));
|
|
1082
|
+
};
|
|
1083
|
+
export let read = (driver, vocab, query, opts = {}, comps, ask = (q)=>rows(driver, vocab, q, opts), fetch, unsettled)=>{
|
|
1084
|
+
let parsed = ast(query);
|
|
1085
|
+
let eids = ask(parsed).filter((r)=>r.eid != null).map((r)=>String(r.eid));
|
|
1086
|
+
if (!eids.length) return [];
|
|
1087
|
+
let derived = opts.derived ?? NONE;
|
|
1088
|
+
// What the filter pinned it computed once already: the rows are filled in
|
|
1089
|
+
// with that value (@yaks/sql `pinned`).
|
|
1090
|
+
let asked = pinned(parsed, vocab, requestedDerived(parsed, derived));
|
|
1091
|
+
// A query asking for no derived value a whole read leaves out, and pinning
|
|
1092
|
+
// none, reads its entities as the store's own get does, through `fetch`
|
|
1093
|
+
// where one is given. Either way, the entities are the query's own answer,
|
|
1094
|
+
// which its scope admitted.
|
|
1095
|
+
return fetch && asked == derived ? fetch(eids, comps, true) : get(driver, vocab, eids, {
|
|
1096
|
+
...opts,
|
|
1097
|
+
derived: asked
|
|
1098
|
+
}, comps, unsettled, undefined, true);
|
|
1099
|
+
};
|
|
1100
|
+
//# sourceMappingURL=read.js.map
|