@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/budget.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// What one read may materialize. A read that answers more rows or bytes than a
|
|
2
|
+
// process can hold is cut off where its rows are decoded and refused to its
|
|
3
|
+
// caller, so the process stays up for every other read. A store's reads
|
|
4
|
+
// (./mod.ts) each run `metered`, and both engines (./native.ts, ./read-db.ts)
|
|
5
|
+
// `meter` each row they decode against the read running. Reads over SQLite are
|
|
6
|
+
// synchronous, so one thread runs one at a time, and the running one is the one
|
|
7
|
+
// a row belongs to.
|
|
8
|
+
import { Refused } from "@yaks/graph/errors";
|
|
9
|
+
import { mask, stringify } from "@yaks/query";
|
|
10
|
+
/** A read's limits: a million rows or 256 MiB. Measured on the box's graph:
|
|
11
|
+
* its pages and tools spend at most 35,000 rows and 5 MiB a read,
|
|
12
|
+
* a million narrow rows meter about 120 MiB, and a read cut at 256 MiB peaks
|
|
13
|
+
* near 480 MiB of a V8 heap's 4 GB while it decodes. */ export let LIMITS = {
|
|
14
|
+
rows: 1_000_000,
|
|
15
|
+
bytes: 256 * 1024 * 1024
|
|
16
|
+
};
|
|
17
|
+
/** What a row costs beside its text and blobs: the object and its keys. */ export let ROW = 64;
|
|
18
|
+
/** What each value costs beside its own bytes. */ export let VALUE = 16;
|
|
19
|
+
// The read running on this thread, and what it has spent.
|
|
20
|
+
let running;
|
|
21
|
+
/** Run `read`, refusing it once the rows it decodes pass `limits`, the
|
|
22
|
+
* refusal naming the read as `what` says, so its caller and any report of it
|
|
23
|
+
* can tell which read it was. A read inside another counts against the outer
|
|
24
|
+
* one.
|
|
25
|
+
*
|
|
26
|
+
* ```ts
|
|
27
|
+
* import { meter, metered } from './budget.ts'
|
|
28
|
+
* import { equal, throws } from '@yaks/testing'
|
|
29
|
+
*
|
|
30
|
+
* let scan = (n: number) => () => {
|
|
31
|
+
* for (let i = 0; i < n; i++) meter({ x: 'x' })
|
|
32
|
+
* return n
|
|
33
|
+
* }
|
|
34
|
+
* let limits = { rows: 2, bytes: 1_000 }
|
|
35
|
+
* equal(metered(scan(2), { limits }), 2)
|
|
36
|
+
* let refused = await throws(() => metered(scan(3), { limits }))
|
|
37
|
+
* equal((refused as Error).name, 'Refused')
|
|
38
|
+
* let what = () => '.task'
|
|
39
|
+
* await throws(() => metered(scan(3), { what, limits }), /^\.task answers/)
|
|
40
|
+
* let narrow = { rows: 9, bytes: 100 }
|
|
41
|
+
* await throws(() => metered(scan(2), { limits: narrow }), /MiB/)
|
|
42
|
+
* meter({ x: 'outside any read, nothing is counted' })
|
|
43
|
+
* ```
|
|
44
|
+
*/ export let metered = (read, { what, limits = LIMITS } = {})=>{
|
|
45
|
+
if (running) return read();
|
|
46
|
+
running = {
|
|
47
|
+
rows: 0,
|
|
48
|
+
bytes: 0,
|
|
49
|
+
limits,
|
|
50
|
+
what
|
|
51
|
+
};
|
|
52
|
+
try {
|
|
53
|
+
return read();
|
|
54
|
+
} finally{
|
|
55
|
+
running = undefined;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
/** A query read, named as a trace keeps it: every value masked, bounded, and
|
|
59
|
+
* said to be masked where it was, so a search for one word reads as a query
|
|
60
|
+
* whose word is hidden, not as a message with a blank left in it.
|
|
61
|
+
*
|
|
62
|
+
* ```ts
|
|
63
|
+
* import { named } from './budget.ts'
|
|
64
|
+
* import { equal } from '@yaks/testing'
|
|
65
|
+
*
|
|
66
|
+
* equal(named('mossvale')(), 'the query `?` (its values masked)')
|
|
67
|
+
* equal(named('.task')(), 'the query `.task`')
|
|
68
|
+
* ```
|
|
69
|
+
*/ export let named = (query)=>()=>{
|
|
70
|
+
let line = typeof query == 'string' ? query : stringify(query);
|
|
71
|
+
let masked = mask(line);
|
|
72
|
+
return `the query \`${masked.slice(0, 512)}\`` + (masked == line ? '' : ' (its values masked)');
|
|
73
|
+
};
|
|
74
|
+
/** A get, named by how many entities it asked for. */ export let gotten = (eids)=>()=>`a get of ${eids.length} entities`;
|
|
75
|
+
/** Count one decoded row against the read running, if one is; throws a
|
|
76
|
+
* refusal once the read passes its limits. */ export let meter = (row)=>{
|
|
77
|
+
if (!running) return;
|
|
78
|
+
let bytes = ROW;
|
|
79
|
+
for(let key in row){
|
|
80
|
+
let value = row[key];
|
|
81
|
+
bytes += VALUE + (typeof value == 'string' ? value.length : value instanceof Uint8Array ? value.byteLength : 0);
|
|
82
|
+
}
|
|
83
|
+
let { limits, what } = running;
|
|
84
|
+
running.rows++;
|
|
85
|
+
running.bytes += bytes;
|
|
86
|
+
if (running.rows <= limits.rows && running.bytes <= limits.bytes) return;
|
|
87
|
+
throw new Refused(`${what?.() ?? 'this read'} answers more than ` + `${limits.rows.toLocaleString('en')} rows or ` + `${limits.bytes / 1024 / 1024} MiB, more than one read may hold: ` + 'narrow it, or add a .limit');
|
|
88
|
+
};
|
|
89
|
+
//# sourceMappingURL=budget.js.map
|
package/budget.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"budget.js","sources":["./budget.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,4EAA4E;AAC5E,wEAAwE;AACxE,8EAA8E;AAC9E,+EAA+E;AAC/E,gFAAgF;AAChF,oBAAoB;AAEpB,SAAS,OAAO,kCAAuC;AAEvD,SAAS,IAAI,EAAE,SAAS,2BAAgC;AAMxD;;;sDAGsD,GACtD,OAAO,IAAI,SAAiB;EAAE,MAAM;EAAW,OAAO,MAAM,OAAO;AAAK,EAAC;AAEzE,yEAAyE,GACzE,OAAO,IAAI,MAAM,GAAE;AACnB,gDAAgD,GAChD,OAAO,IAAI,QAAQ,GAAE;AAErB,0DAA0D;AAC1D,IAAI;AAIJ;;;;;;;;;;;;;;;;;;;;;;;CAuBC,GACD,OAAO,IAAI,UAAU,CACnB,MACA,EAAE,IAAI,EAAE,SAAS,MAAM,EAA4C,GAAG,CAAC,CAAC;EAExE,IAAI,SAAS,OAAO;EACpB,UAAU;IAAE,MAAM;IAAG,OAAO;IAAG;IAAQ;EAAK;EAC5C,IAAI;IACF,OAAO;EACT,SAAU;IACR,UAAU;EACZ;AACF,EAAC;AAED;;;;;;;;;;;CAWC,GACD,OAAO,IAAI,QAAQ,CAAC,QAAiB;IACnC,IAAI,OAAO,OAAO,SAAS,WAAW,QAAQ,UAAU;IACxD,IAAI,SAAS,KAAK;IAClB,OAAO,CAAC,YAAY,EAAE,OAAO,KAAK,CAAC,GAAG,KAAK,EAAE,CAAC,GAC5C,CAAC,UAAU,OAAO,KAAK,sBAAsB;EACjD,EAAC;AAED,oDAAoD,GACpD,OAAO,IAAI,SAAS,CAAC,OAAoB,IACvC,CAAC,SAAS,EAAE,KAAK,MAAM,CAAC,SAAS,CAAC,CAAA;AAEpC;4CAC4C,GAC5C,OAAO,IAAI,QAAQ,CAAC;EAClB,IAAI,CAAC,SAAS;EACd,IAAI,QAAQ;EACZ,IAAK,IAAI,OAAO,IAAK;IACnB,IAAI,QAAQ,GAAG,CAAC,IAAI;IACpB,SAAS,QACP,CAAC,OAAO,SAAS,WACb,MAAM,MAAM,GACZ,iBAAiB,aACjB,MAAM,UAAU,GAChB,CAAC;EACT;EACA,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG;EACvB,QAAQ,IAAI;EACZ,QAAQ,KAAK,IAAI;EACjB,IAAI,QAAQ,IAAI,IAAI,OAAO,IAAI,IAAI,QAAQ,KAAK,IAAI,OAAO,KAAK,EAAE;EAClE,MAAM,IAAI,QACR,GAAG,YAAY,YAAY,mBAAmB,CAAC,GAC7C,GAAG,OAAO,IAAI,CAAC,cAAc,CAAC,MAAM,SAAS,CAAC,GAC9C,GAAG,OAAO,KAAK,GAAG,OAAO,KAAK,mCAAmC,CAAC,GAClE;AAEN,EAAC"}
|
package/budget.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// What one read may materialize. A read that answers more rows or bytes than a
|
|
2
|
+
// process can hold is cut off where its rows are decoded and refused to its
|
|
3
|
+
// caller, so the process stays up for every other read. A store's reads
|
|
4
|
+
// (./mod.ts) each run `metered`, and both engines (./native.ts, ./read-db.ts)
|
|
5
|
+
// `meter` each row they decode against the read running. Reads over SQLite are
|
|
6
|
+
// synchronous, so one thread runs one at a time, and the running one is the one
|
|
7
|
+
// a row belongs to.
|
|
8
|
+
|
|
9
|
+
import { Refused } from '@yaks/graph/errors'
|
|
10
|
+
import type { Row } from 'jsr:@yaks/sql@0.2.36'
|
|
11
|
+
import { mask, stringify } from '@yaks/query'
|
|
12
|
+
import type { Query } from './read.ts'
|
|
13
|
+
|
|
14
|
+
/** The most one read may answer: rows decoded, and their size. */
|
|
15
|
+
export type Limits = { rows: number; bytes: number }
|
|
16
|
+
|
|
17
|
+
/** A read's limits: a million rows or 256 MiB. Measured on the box's graph:
|
|
18
|
+
* its pages and tools spend at most 35,000 rows and 5 MiB a read,
|
|
19
|
+
* a million narrow rows meter about 120 MiB, and a read cut at 256 MiB peaks
|
|
20
|
+
* near 480 MiB of a V8 heap's 4 GB while it decodes. */
|
|
21
|
+
export let LIMITS: Limits = { rows: 1_000_000, bytes: 256 * 1024 * 1024 }
|
|
22
|
+
|
|
23
|
+
/** What a row costs beside its text and blobs: the object and its keys. */
|
|
24
|
+
export let ROW = 64
|
|
25
|
+
/** What each value costs beside its own bytes. */
|
|
26
|
+
export let VALUE = 16
|
|
27
|
+
|
|
28
|
+
// The read running on this thread, and what it has spent.
|
|
29
|
+
let running:
|
|
30
|
+
| { rows: number; bytes: number; limits: Limits; what?: () => string }
|
|
31
|
+
| undefined
|
|
32
|
+
|
|
33
|
+
/** Run `read`, refusing it once the rows it decodes pass `limits`, the
|
|
34
|
+
* refusal naming the read as `what` says, so its caller and any report of it
|
|
35
|
+
* can tell which read it was. A read inside another counts against the outer
|
|
36
|
+
* one.
|
|
37
|
+
*
|
|
38
|
+
* ```ts
|
|
39
|
+
* import { meter, metered } from './budget.ts'
|
|
40
|
+
* import { equal, throws } from '@yaks/testing'
|
|
41
|
+
*
|
|
42
|
+
* let scan = (n: number) => () => {
|
|
43
|
+
* for (let i = 0; i < n; i++) meter({ x: 'x' })
|
|
44
|
+
* return n
|
|
45
|
+
* }
|
|
46
|
+
* let limits = { rows: 2, bytes: 1_000 }
|
|
47
|
+
* equal(metered(scan(2), { limits }), 2)
|
|
48
|
+
* let refused = await throws(() => metered(scan(3), { limits }))
|
|
49
|
+
* equal((refused as Error).name, 'Refused')
|
|
50
|
+
* let what = () => '.task'
|
|
51
|
+
* await throws(() => metered(scan(3), { what, limits }), /^\.task answers/)
|
|
52
|
+
* let narrow = { rows: 9, bytes: 100 }
|
|
53
|
+
* await throws(() => metered(scan(2), { limits: narrow }), /MiB/)
|
|
54
|
+
* meter({ x: 'outside any read, nothing is counted' })
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export let metered = <R>(
|
|
58
|
+
read: () => R,
|
|
59
|
+
{ what, limits = LIMITS }: { what?: () => string; limits?: Limits } = {},
|
|
60
|
+
): R => {
|
|
61
|
+
if (running) return read()
|
|
62
|
+
running = { rows: 0, bytes: 0, limits, what }
|
|
63
|
+
try {
|
|
64
|
+
return read()
|
|
65
|
+
} finally {
|
|
66
|
+
running = undefined
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** A query read, named as a trace keeps it: every value masked, bounded, and
|
|
71
|
+
* said to be masked where it was, so a search for one word reads as a query
|
|
72
|
+
* whose word is hidden, not as a message with a blank left in it.
|
|
73
|
+
*
|
|
74
|
+
* ```ts
|
|
75
|
+
* import { named } from './budget.ts'
|
|
76
|
+
* import { equal } from '@yaks/testing'
|
|
77
|
+
*
|
|
78
|
+
* equal(named('mossvale')(), 'the query `?` (its values masked)')
|
|
79
|
+
* equal(named('.task')(), 'the query `.task`')
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export let named = (query: Query) => (): string => {
|
|
83
|
+
let line = typeof query == 'string' ? query : stringify(query)
|
|
84
|
+
let masked = mask(line)
|
|
85
|
+
return `the query \`${masked.slice(0, 512)}\`` +
|
|
86
|
+
(masked == line ? '' : ' (its values masked)')
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** A get, named by how many entities it asked for. */
|
|
90
|
+
export let gotten = (eids: unknown[]) => (): string =>
|
|
91
|
+
`a get of ${eids.length} entities`
|
|
92
|
+
|
|
93
|
+
/** Count one decoded row against the read running, if one is; throws a
|
|
94
|
+
* refusal once the read passes its limits. */
|
|
95
|
+
export let meter = (row: Row): void => {
|
|
96
|
+
if (!running) return
|
|
97
|
+
let bytes = ROW
|
|
98
|
+
for (let key in row) {
|
|
99
|
+
let value = row[key]
|
|
100
|
+
bytes += VALUE +
|
|
101
|
+
(typeof value == 'string'
|
|
102
|
+
? value.length
|
|
103
|
+
: value instanceof Uint8Array
|
|
104
|
+
? value.byteLength
|
|
105
|
+
: 0)
|
|
106
|
+
}
|
|
107
|
+
let { limits, what } = running
|
|
108
|
+
running.rows++
|
|
109
|
+
running.bytes += bytes
|
|
110
|
+
if (running.rows <= limits.rows && running.bytes <= limits.bytes) return
|
|
111
|
+
throw new Refused(
|
|
112
|
+
`${what?.() ?? 'this read'} answers more than ` +
|
|
113
|
+
`${limits.rows.toLocaleString('en')} rows or ` +
|
|
114
|
+
`${limits.bytes / 1024 / 1024} MiB, more than one read may hold: ` +
|
|
115
|
+
'narrow it, or add a .limit',
|
|
116
|
+
)
|
|
117
|
+
}
|
package/builtin.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// The Driver over the SQLite built into Deno (node:sqlite), for databases
|
|
2
|
+
// Deno itself keeps open in this process, such as its module caches. POSIX
|
|
3
|
+
// locks belong to the process, so a second SQLite closing the same file drops
|
|
4
|
+
// the locks Deno's copy holds, and the two can corrupt it. Deno links one
|
|
5
|
+
// SQLite, and node:sqlite is that one; a file Deno holds is opened only here.
|
|
6
|
+
import { DatabaseSync } from 'node:sqlite';
|
|
7
|
+
import * as io from "@yaks/file/io";
|
|
8
|
+
import { driver } from "./driver.js";
|
|
9
|
+
/** Open an existing database through Deno's own SQLite; a missing one is
|
|
10
|
+
* refused, never created. */ export let builtin = (path)=>{
|
|
11
|
+
let url = io.pathToFileURL(path);
|
|
12
|
+
url.searchParams.set('mode', 'rw');
|
|
13
|
+
let db = new DatabaseSync(url);
|
|
14
|
+
let changes = 0;
|
|
15
|
+
let ran = (sql, params)=>{
|
|
16
|
+
let statement = db.prepare(sql);
|
|
17
|
+
let bound = params.map((p)=>typeof p == 'boolean' ? Number(p) : p);
|
|
18
|
+
if (statement.columns().length) {
|
|
19
|
+
return statement.all(...bound).map((row)=>({
|
|
20
|
+
...row
|
|
21
|
+
}));
|
|
22
|
+
}
|
|
23
|
+
changes = Number(statement.run(...bound).changes);
|
|
24
|
+
return [];
|
|
25
|
+
};
|
|
26
|
+
return {
|
|
27
|
+
...driver(ran, {
|
|
28
|
+
file: true,
|
|
29
|
+
changes: ()=>changes
|
|
30
|
+
}),
|
|
31
|
+
close: ()=>db.close()
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
//# sourceMappingURL=builtin.js.map
|
package/builtin.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builtin.js","sources":["./builtin.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,2EAA2E;AAC3E,8EAA8E;AAC9E,0EAA0E;AAC1E,8EAA8E;AAC9E,SAAS,YAAY,QAAQ,cAAa;AAC1C,YAAY,6BAAkC;AAE9C,SAAS,MAAM,sBAAqB;AAEpC;2BAC2B,GAC3B,OAAO,IAAI,UAAU,CAAC;EACpB,IAAI,MAAM,GAAG,aAAa,CAAC;EAC3B,IAAI,YAAY,CAAC,GAAG,CAAC,QAAQ;EAC7B,IAAI,KAAK,IAAI,aAAa;EAC1B,IAAI,UAAU;EACd,IAAI,MAAM,CAAC,KAAa;IACtB,IAAI,YAAY,GAAG,OAAO,CAAC;IAC3B,IAAI,QAAQ,OAAO,GAAG,CAAC,CAAC,IAAM,OAAO,KAAK,YAAY,OAAO,KAAK;IAClE,IAAI,UAAU,OAAO,GAAG,MAAM,EAAE;MAC9B,OAAO,UAAU,GAAG,IAAI,OAAO,GAAG,CAAC,CAAC,MAAQ,CAAC;UAAE,GAAG,GAAG;QAAC,CAAC;IACzD;IACA,UAAU,OAAO,UAAU,GAAG,IAAI,OAAO,OAAO;IAChD,OAAO,EAAE;EACX;EACA,OAAO;IACL,GAAG,OAAO,KAAK;MAAE,MAAM;MAAM,SAAS,IAAM;IAAQ,EAAE;IACtD,OAAO,IAAM,GAAG,KAAK;EACvB;AACF,EAAC"}
|
package/builtin.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// The Driver over the SQLite built into Deno (node:sqlite), for databases
|
|
2
|
+
// Deno itself keeps open in this process, such as its module caches. POSIX
|
|
3
|
+
// locks belong to the process, so a second SQLite closing the same file drops
|
|
4
|
+
// the locks Deno's copy holds, and the two can corrupt it. Deno links one
|
|
5
|
+
// SQLite, and node:sqlite is that one; a file Deno holds is opened only here.
|
|
6
|
+
import { DatabaseSync } from 'node:sqlite'
|
|
7
|
+
import * as io from '@yaks/file/io'
|
|
8
|
+
import type { Driver, Param, Row } from 'jsr:@yaks/sql@0.2.36'
|
|
9
|
+
import { driver } from './driver.js'
|
|
10
|
+
|
|
11
|
+
/** Open an existing database through Deno's own SQLite; a missing one is
|
|
12
|
+
* refused, never created. */
|
|
13
|
+
export let builtin = (path: string): Driver & { close: () => void } => {
|
|
14
|
+
let url = io.pathToFileURL(path)
|
|
15
|
+
url.searchParams.set('mode', 'rw')
|
|
16
|
+
let db = new DatabaseSync(url)
|
|
17
|
+
let changes = 0
|
|
18
|
+
let ran = (sql: string, params: Param[]): Row[] => {
|
|
19
|
+
let statement = db.prepare(sql)
|
|
20
|
+
let bound = params.map((p) => typeof p == 'boolean' ? Number(p) : p)
|
|
21
|
+
if (statement.columns().length) {
|
|
22
|
+
return statement.all(...bound).map((row) => ({ ...row }))
|
|
23
|
+
}
|
|
24
|
+
changes = Number(statement.run(...bound).changes)
|
|
25
|
+
return []
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
...driver(ran, { file: true, changes: () => changes }),
|
|
29
|
+
close: () => db.close(),
|
|
30
|
+
}
|
|
31
|
+
}
|
package/bundle.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// The bundle: how the adapter returns an entity to a caller, and takes one
|
|
2
|
+
// back. The store keeps a component per table, but a caller thinks in whole
|
|
3
|
+
// entities, so a bundle gathers every component an entity has into one
|
|
4
|
+
// object.
|
|
5
|
+
//
|
|
6
|
+
// { entity: { eid: 'cake-01' }, doc: { title: 'Lemon cake' }, recipe: { serves: 8 } }
|
|
7
|
+
//
|
|
8
|
+
// The shape itself is @yaks/graph's — the graph core owns the bundle format
|
|
9
|
+
// every yaks package shares, so this Layer-0 storage adapter depends on it
|
|
10
|
+
// rather than keeping a second definition that could drift. The identity is the
|
|
11
|
+
// `entity` component (`entity.eid` is the id, never a bare root `eid`); every
|
|
12
|
+
// other key is a component name mapping to its properties, or `null` to drop
|
|
13
|
+
// it. A bundle is also the write unit: `$delete: true` deletes the whole entity
|
|
14
|
+
// (tombstoned, death cascades), and `$was` carries per-property preconditions —
|
|
15
|
+
// fields this adapter carries but does not enforce (that check is a phase of
|
|
16
|
+
// @yaks/graph's apply(), not of a storage adapter).
|
|
17
|
+
//
|
|
18
|
+
// On the way out a bundle holds only the components the entity actually has,
|
|
19
|
+
// each a plain object of property values — references already resolved back to
|
|
20
|
+
// eids.
|
|
21
|
+
//# sourceMappingURL=bundle.js.map
|
package/bundle.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bundle.js","sources":["./bundle.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,4EAA4E;AAC5E,uEAAuE;AACvE,UAAU;AACV,EAAE;AACF,wFAAwF;AACxF,EAAE;AACF,4EAA4E;AAC5E,2EAA2E;AAC3E,gFAAgF;AAChF,8EAA8E;AAC9E,6EAA6E;AAC7E,gFAAgF;AAChF,gFAAgF;AAChF,6EAA6E;AAC7E,oDAAoD;AACpD,EAAE;AACF,6EAA6E;AAC7E,+EAA+E;AAC/E,QAAQ"}
|
package/bundle.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// The bundle: how the adapter returns an entity to a caller, and takes one
|
|
2
|
+
// back. The store keeps a component per table, but a caller thinks in whole
|
|
3
|
+
// entities, so a bundle gathers every component an entity has into one
|
|
4
|
+
// object.
|
|
5
|
+
//
|
|
6
|
+
// { entity: { eid: 'cake-01' }, doc: { title: 'Lemon cake' }, recipe: { serves: 8 } }
|
|
7
|
+
//
|
|
8
|
+
// The shape itself is @yaks/graph's — the graph core owns the bundle format
|
|
9
|
+
// every yaks package shares, so this Layer-0 storage adapter depends on it
|
|
10
|
+
// rather than keeping a second definition that could drift. The identity is the
|
|
11
|
+
// `entity` component (`entity.eid` is the id, never a bare root `eid`); every
|
|
12
|
+
// other key is a component name mapping to its properties, or `null` to drop
|
|
13
|
+
// it. A bundle is also the write unit: `$delete: true` deletes the whole entity
|
|
14
|
+
// (tombstoned, death cascades), and `$was` carries per-property preconditions —
|
|
15
|
+
// fields this adapter carries but does not enforce (that check is a phase of
|
|
16
|
+
// @yaks/graph's apply(), not of a storage adapter).
|
|
17
|
+
//
|
|
18
|
+
// On the way out a bundle holds only the components the entity actually has,
|
|
19
|
+
// each a plain object of property values — references already resolved back to
|
|
20
|
+
// eids.
|
|
21
|
+
|
|
22
|
+
export type { Bundle, Comp, Entity, Was } from 'jsr:@yaks/graph@0.2.36'
|
package/buried.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// The graves a retention sweep may prune (@yaks/effects `expire`): tombstones
|
|
2
|
+
// laid before a moment that nothing in the file refers to any more. Nothing is
|
|
3
|
+
// every column keyed to an identity, whichever package made its table (a
|
|
4
|
+
// component's owner and references, the journal's, an embedding's), and every
|
|
5
|
+
// `keep` reference, which outlives its target's tombstone with no key
|
|
6
|
+
// (./ddl.ts). A grave something still names stays: it is that record's
|
|
7
|
+
// target. The walk is in identity order, a page of graves at a time, each probe
|
|
8
|
+
// one index seek. A page is the graves it looks at, not the ones it finds: a
|
|
9
|
+
// long run of named or young graves costs as many pages, each one statement
|
|
10
|
+
// of bounded work, rather than one statement that probes them all.
|
|
11
|
+
import { as, col, eq, exists, field, gt, iff, join, lit, lt, not, or, revision, select, table, val } from "@yaks/sql";
|
|
12
|
+
import { TOMBSTONE } from "@yaks/graph/bundle";
|
|
13
|
+
import { foreign } from "./physical.js";
|
|
14
|
+
/** Every column in the file that names an identity: each foreign key to
|
|
15
|
+
* `entity`, wherever it is declared, and each stored `keep` reference. The
|
|
16
|
+
* tombstone's own key is not a referrer, and a {@link foreign} table is never
|
|
17
|
+
* asked. */ export let referrers = (driver, vocab)=>{
|
|
18
|
+
let names = driver.query({
|
|
19
|
+
t: 'pragma',
|
|
20
|
+
name: 'table_list'
|
|
21
|
+
}).filter((r)=>r.schema == 'main' && r.type == 'table').map((r)=>String(r.name)).filter((name)=>name != TOMBSTONE && !foreign(name));
|
|
22
|
+
let keyed = names.flatMap((name)=>driver.query({
|
|
23
|
+
t: 'pragma',
|
|
24
|
+
name: 'foreign_key_list',
|
|
25
|
+
arg: name
|
|
26
|
+
}).filter((r)=>r.table == 'entity').map((r)=>[
|
|
27
|
+
name,
|
|
28
|
+
String(r.from)
|
|
29
|
+
]));
|
|
30
|
+
let kept = vocab.refProps().filter(([c, p])=>{
|
|
31
|
+
let prop = vocab.prop(c, p);
|
|
32
|
+
return !prop?.computed && !prop?.fk && names.includes(c);
|
|
33
|
+
}).map(([c, p])=>[
|
|
34
|
+
c,
|
|
35
|
+
field(p)
|
|
36
|
+
]);
|
|
37
|
+
return [
|
|
38
|
+
...keyed,
|
|
39
|
+
...kept
|
|
40
|
+
];
|
|
41
|
+
};
|
|
42
|
+
/** The statement selecting a page of graves past the identity `after`: each
|
|
43
|
+
* grave's eid, its integer id, the walk's cursor, and whether it is `free`,
|
|
44
|
+
* laid before `before` and named by none of `refs`. Age is judged in the
|
|
45
|
+
* answer, so a page of young graves is bounded too. */ export let buriedSql = (refs, before, page)=>{
|
|
46
|
+
let grave = col('entity', 't');
|
|
47
|
+
let named = ([name, column])=>exists(select({
|
|
48
|
+
cols: [
|
|
49
|
+
lit(1)
|
|
50
|
+
],
|
|
51
|
+
from: table(name, '__r'),
|
|
52
|
+
where: eq(col(column, '__r'), grave)
|
|
53
|
+
}));
|
|
54
|
+
return select({
|
|
55
|
+
cols: [
|
|
56
|
+
col('eid', 'e'),
|
|
57
|
+
col('id', 'e'),
|
|
58
|
+
as(iff(lt(col('deleted_at', 't'), val(before)), not(or(...refs.map(named))), lit(false)), 'free')
|
|
59
|
+
],
|
|
60
|
+
from: table(TOMBSTONE, 't'),
|
|
61
|
+
joins: [
|
|
62
|
+
join(table('entity', 'e'), eq(col('id', 'e'), grave))
|
|
63
|
+
],
|
|
64
|
+
where: gt(grave, val(page.after ?? 0)),
|
|
65
|
+
order: [
|
|
66
|
+
grave
|
|
67
|
+
],
|
|
68
|
+
limit: val(page.limit)
|
|
69
|
+
});
|
|
70
|
+
};
|
|
71
|
+
/** A store's `buried`, walking graves by integer id: a pruned grave's id
|
|
72
|
+
* still marks where the walk resumes. The referrers are read once while the
|
|
73
|
+
* schema stands. */ export let burying = (driver, vocab)=>{
|
|
74
|
+
let held;
|
|
75
|
+
return (before, page)=>{
|
|
76
|
+
let at = revision(driver, 'schema');
|
|
77
|
+
if (held?.at != at) held = {
|
|
78
|
+
at,
|
|
79
|
+
refs: referrers(driver, vocab)
|
|
80
|
+
};
|
|
81
|
+
let found = driver.query(buriedSql(page.referenced ? [] : held.refs, before, {
|
|
82
|
+
after: page.after == null ? undefined : Number(page.after),
|
|
83
|
+
limit: page.limit
|
|
84
|
+
}));
|
|
85
|
+
let eids = found.filter((r)=>r.free).map((r)=>String(r.eid));
|
|
86
|
+
let last = found.at(-1);
|
|
87
|
+
return found.length < page.limit || !last ? {
|
|
88
|
+
eids
|
|
89
|
+
} : {
|
|
90
|
+
eids,
|
|
91
|
+
next: String(last.id)
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
};
|
|
95
|
+
//# sourceMappingURL=buried.js.map
|
package/buried.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"buried.js","sources":["./buried.ts"],"names":[],"mappings":"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"}
|
package/buried.ts
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// The graves a retention sweep may prune (@yaks/effects `expire`): tombstones
|
|
2
|
+
// laid before a moment that nothing in the file refers to any more. Nothing is
|
|
3
|
+
// every column keyed to an identity, whichever package made its table (a
|
|
4
|
+
// component's owner and references, the journal's, an embedding's), and every
|
|
5
|
+
// `keep` reference, which outlives its target's tombstone with no key
|
|
6
|
+
// (./ddl.ts). A grave something still names stays: it is that record's
|
|
7
|
+
// target. The walk is in identity order, a page of graves at a time, each probe
|
|
8
|
+
// one index seek. A page is the graves it looks at, not the ones it finds: a
|
|
9
|
+
// long run of named or young graves costs as many pages, each one statement
|
|
10
|
+
// of bounded work, rather than one statement that probes them all.
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
as,
|
|
14
|
+
col,
|
|
15
|
+
type Driver,
|
|
16
|
+
eq,
|
|
17
|
+
exists,
|
|
18
|
+
type Expr,
|
|
19
|
+
field,
|
|
20
|
+
gt,
|
|
21
|
+
iff,
|
|
22
|
+
join,
|
|
23
|
+
lit,
|
|
24
|
+
lt,
|
|
25
|
+
not,
|
|
26
|
+
or,
|
|
27
|
+
revision,
|
|
28
|
+
type Select,
|
|
29
|
+
select,
|
|
30
|
+
table,
|
|
31
|
+
val,
|
|
32
|
+
} from '@yaks/sql'
|
|
33
|
+
import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
|
|
34
|
+
import type { Graves } from 'jsr:@yaks/graph@0.2.36'
|
|
35
|
+
import { TOMBSTONE } from '@yaks/graph/bundle'
|
|
36
|
+
import { foreign } from './physical.js'
|
|
37
|
+
|
|
38
|
+
/** A table and one of its columns that names an identity. */
|
|
39
|
+
export type Referrer = [table: string, column: string]
|
|
40
|
+
|
|
41
|
+
/** Every column in the file that names an identity: each foreign key to
|
|
42
|
+
* `entity`, wherever it is declared, and each stored `keep` reference. The
|
|
43
|
+
* tombstone's own key is not a referrer, and a {@link foreign} table is never
|
|
44
|
+
* asked. */
|
|
45
|
+
export let referrers = (driver: Driver, vocab: Vocab): Referrer[] => {
|
|
46
|
+
let names = driver.query({ t: 'pragma', name: 'table_list' })
|
|
47
|
+
.filter((r) => r.schema == 'main' && r.type == 'table')
|
|
48
|
+
.map((r) => String(r.name))
|
|
49
|
+
.filter((name) => name != TOMBSTONE && !foreign(name))
|
|
50
|
+
let keyed = names.flatMap((name) =>
|
|
51
|
+
driver.query({ t: 'pragma', name: 'foreign_key_list', arg: name })
|
|
52
|
+
.filter((r) => r.table == 'entity')
|
|
53
|
+
.map((r): Referrer => [name, String(r.from)])
|
|
54
|
+
)
|
|
55
|
+
let kept = vocab.refProps()
|
|
56
|
+
.filter(([c, p]) => {
|
|
57
|
+
let prop = vocab.prop(c, p)
|
|
58
|
+
return !prop?.computed && !prop?.fk && names.includes(c)
|
|
59
|
+
})
|
|
60
|
+
.map(([c, p]): Referrer => [c, field(p)])
|
|
61
|
+
return [...keyed, ...kept]
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** The statement selecting a page of graves past the identity `after`: each
|
|
65
|
+
* grave's eid, its integer id, the walk's cursor, and whether it is `free`,
|
|
66
|
+
* laid before `before` and named by none of `refs`. Age is judged in the
|
|
67
|
+
* answer, so a page of young graves is bounded too. */
|
|
68
|
+
export let buriedSql = (
|
|
69
|
+
refs: Referrer[],
|
|
70
|
+
before: string,
|
|
71
|
+
page: { after?: number; limit: number },
|
|
72
|
+
): Select => {
|
|
73
|
+
let grave = col('entity', 't')
|
|
74
|
+
let named = ([name, column]: Referrer): Expr =>
|
|
75
|
+
exists(select({
|
|
76
|
+
cols: [lit(1)],
|
|
77
|
+
from: table(name, '__r'),
|
|
78
|
+
where: eq(col(column, '__r'), grave),
|
|
79
|
+
}))
|
|
80
|
+
return select({
|
|
81
|
+
cols: [
|
|
82
|
+
col('eid', 'e'),
|
|
83
|
+
col('id', 'e'),
|
|
84
|
+
as(
|
|
85
|
+
iff(
|
|
86
|
+
lt(col('deleted_at', 't'), val(before)),
|
|
87
|
+
not(or(...refs.map(named))),
|
|
88
|
+
lit(false),
|
|
89
|
+
),
|
|
90
|
+
'free',
|
|
91
|
+
),
|
|
92
|
+
],
|
|
93
|
+
from: table(TOMBSTONE, 't'),
|
|
94
|
+
joins: [join(table('entity', 'e'), eq(col('id', 'e'), grave))],
|
|
95
|
+
where: gt(grave, val(page.after ?? 0)),
|
|
96
|
+
order: [grave],
|
|
97
|
+
limit: val(page.limit),
|
|
98
|
+
})
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** A store's `buried`, walking graves by integer id: a pruned grave's id
|
|
102
|
+
* still marks where the walk resumes. The referrers are read once while the
|
|
103
|
+
* schema stands. */
|
|
104
|
+
export let burying = (driver: Driver, vocab: Vocab) => {
|
|
105
|
+
let held: { at: number; refs: Referrer[] } | undefined
|
|
106
|
+
return (
|
|
107
|
+
before: string,
|
|
108
|
+
page: { after?: string; limit: number; referenced?: boolean },
|
|
109
|
+
): Graves => {
|
|
110
|
+
let at = revision(driver, 'schema')
|
|
111
|
+
if (held?.at != at) held = { at, refs: referrers(driver, vocab) }
|
|
112
|
+
let found = driver.query(
|
|
113
|
+
buriedSql(page.referenced ? [] : held.refs, before, {
|
|
114
|
+
after: page.after == null ? undefined : Number(page.after),
|
|
115
|
+
limit: page.limit,
|
|
116
|
+
}),
|
|
117
|
+
)
|
|
118
|
+
let eids = found.filter((r) => r.free).map((r) => String(r.eid))
|
|
119
|
+
let last = found.at(-1)
|
|
120
|
+
return found.length < page.limit || !last
|
|
121
|
+
? { eids }
|
|
122
|
+
: { eids, next: String(last.id) }
|
|
123
|
+
}
|
|
124
|
+
}
|