@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/catalog.ts
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { Unpointed } from './error.js'
|
|
2
|
+
// Immutable table sets and a connection-revision snapshot: planning reads no
|
|
3
|
+
// persistent rows until a catalog mutation or rollback invalidates that snapshot.
|
|
4
|
+
import {
|
|
5
|
+
type Archetype,
|
|
6
|
+
Archetypes,
|
|
7
|
+
type Naming,
|
|
8
|
+
namingOf,
|
|
9
|
+
satisfies,
|
|
10
|
+
} from '@yaks/archetype/sets'
|
|
11
|
+
import {
|
|
12
|
+
and,
|
|
13
|
+
type ArchetypeSet,
|
|
14
|
+
archetypeSet,
|
|
15
|
+
col,
|
|
16
|
+
type Driver,
|
|
17
|
+
isNull,
|
|
18
|
+
not,
|
|
19
|
+
oneOf,
|
|
20
|
+
select,
|
|
21
|
+
table,
|
|
22
|
+
} from '@yaks/sql/core'
|
|
23
|
+
import { revision } from '@yaks/sql/core'
|
|
24
|
+
import { tableVersion } from './versions.js'
|
|
25
|
+
|
|
26
|
+
let caches = new WeakMap<Driver, {
|
|
27
|
+
naming: Naming
|
|
28
|
+
sets: Archetypes
|
|
29
|
+
text: Map<string, Archetype>
|
|
30
|
+
// The last snapshot and the catalog version it was read at (below).
|
|
31
|
+
descriptorVersion?: number
|
|
32
|
+
tableVersion?: string
|
|
33
|
+
schemaVersion?: number
|
|
34
|
+
descriptors?: ArchetypeSet
|
|
35
|
+
ids?: Map<string, number>
|
|
36
|
+
version?: number
|
|
37
|
+
set?: ArchetypeSet
|
|
38
|
+
}>()
|
|
39
|
+
let cacheFor = (driver: Driver) => {
|
|
40
|
+
let cache = caches.get(driver), naming = namingFor(driver)
|
|
41
|
+
if (cache?.naming != naming) {
|
|
42
|
+
cache = { naming, sets: new Archetypes(naming), text: new Map() }
|
|
43
|
+
caches.set(driver, cache)
|
|
44
|
+
}
|
|
45
|
+
return cache
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** How this connection's descriptors name components: by the identities of
|
|
49
|
+
* the vocabulary its statements are written with (@yaks/archetype
|
|
50
|
+
* `namingOf`). */
|
|
51
|
+
export let namingFor = (driver: Driver): Naming => namingOf(driver.stored?.v)
|
|
52
|
+
|
|
53
|
+
/** A cache of sets named as this connection's descriptors name them. */
|
|
54
|
+
export let setsFor = (driver: Driver): Archetypes =>
|
|
55
|
+
new Archetypes(namingFor(driver))
|
|
56
|
+
|
|
57
|
+
/** The set of the components `tables` names, as this connection's
|
|
58
|
+
* descriptors describe it: its eid and descriptor. */
|
|
59
|
+
export let setOf = (driver: Driver, tables: Iterable<string>): Archetype =>
|
|
60
|
+
cacheFor(driver).sets.intern(tables)
|
|
61
|
+
|
|
62
|
+
/** Decode a descriptor once, independent of its transaction-local row id:
|
|
63
|
+
* the set it describes, under the names this connection's statements write. */
|
|
64
|
+
export function descriptor(driver: Driver, text: string): Archetype {
|
|
65
|
+
let cache = cacheFor(driver)
|
|
66
|
+
let a = cache.text.get(text)
|
|
67
|
+
if (!a) {
|
|
68
|
+
a = cache.sets.decode(text)
|
|
69
|
+
cache.text.set(text, a)
|
|
70
|
+
}
|
|
71
|
+
return a
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function snapshot(
|
|
75
|
+
driver: Driver,
|
|
76
|
+
pending?: ReadonlyMap<number, readonly string[]>,
|
|
77
|
+
): ArchetypeSet {
|
|
78
|
+
let cache = cacheFor(driver)
|
|
79
|
+
let v = revision(driver, 'catalog')
|
|
80
|
+
if (cache.version == v && cache.set) return cache.set
|
|
81
|
+
// An incomplete catalog is corruption, never permission for the planner
|
|
82
|
+
// to discover presence in component tables. The writer repairs it.
|
|
83
|
+
let missing = driver.query(select({
|
|
84
|
+
cols: [col('eid')],
|
|
85
|
+
from: table('entity'),
|
|
86
|
+
where: and(
|
|
87
|
+
isNull(col('archetype')),
|
|
88
|
+
...pending?.size ? [not(oneOf(col('id'), [...pending.keys()]))] : [],
|
|
89
|
+
),
|
|
90
|
+
}))
|
|
91
|
+
if (missing.length) throw new Unpointed(missing.map((r) => String(r.eid)))
|
|
92
|
+
cache.version = v
|
|
93
|
+
return cache.set = fresh(driver).descriptors!
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// The descriptors this connection holds, read again only where one may have
|
|
97
|
+
// moved since (@yaks/sql `revision`).
|
|
98
|
+
let fresh = (driver: Driver) => {
|
|
99
|
+
let cache = cacheFor(driver)
|
|
100
|
+
let version = revision(driver, 'descriptors')
|
|
101
|
+
if (cache.descriptorVersion != version || !cache.descriptors) {
|
|
102
|
+
// SQLite's data_version hears every peer commit. The table's own clock
|
|
103
|
+
// distinguishes a changed descriptor from an unrelated graph write,
|
|
104
|
+
// without rereading the entire catalog. Schema changes still invalidate
|
|
105
|
+
// it, even when a table or its triggers were replaced outside the graph.
|
|
106
|
+
// A store owning its file keeps no clock: every statement that could
|
|
107
|
+
// move a descriptor passes through it, and only those move the revision.
|
|
108
|
+
let clock = driver.file ? tableVersion(driver, 'archetype') : undefined
|
|
109
|
+
let schema = revision(driver, 'schema')
|
|
110
|
+
if (
|
|
111
|
+
!cache.descriptors || clock === undefined ||
|
|
112
|
+
cache.tableVersion != clock || cache.schemaVersion != schema
|
|
113
|
+
) {
|
|
114
|
+
let ids = new Map<string, number>()
|
|
115
|
+
let all = select({
|
|
116
|
+
cols: [col('entity'), col('tables')],
|
|
117
|
+
from: table('archetype'),
|
|
118
|
+
})
|
|
119
|
+
for (let row of driver.query(all)) {
|
|
120
|
+
let a = descriptor(driver, String(row.tables))
|
|
121
|
+
ids.set(a.eid, Number(row.entity))
|
|
122
|
+
}
|
|
123
|
+
cache.descriptors = archetypeSet(cache.sets, ids)
|
|
124
|
+
cache.ids = ids
|
|
125
|
+
}
|
|
126
|
+
cache.tableVersion = clock
|
|
127
|
+
cache.schemaVersion = schema
|
|
128
|
+
cache.descriptorVersion = version
|
|
129
|
+
}
|
|
130
|
+
return cache
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Every descriptor the file holds, as the set it describes by its entity
|
|
134
|
+
* id: the very read the catalog keeps (`catalog`), so a look at them all
|
|
135
|
+
* after an install costs the planner nothing, and none again while no
|
|
136
|
+
* statement moves one. Unlike a plan's catalog it asks nothing of owners. */
|
|
137
|
+
export let standing = (driver: Driver): Map<number, Archetype> => {
|
|
138
|
+
let { ids, sets } = fresh(driver)
|
|
139
|
+
return new Map([...ids!].map(([eid, id]) => [id, sets.get(eid)!]))
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Descriptors this connection wrote, by entity id and table set, where they
|
|
144
|
+
* are all that moved one since revision `from` (@yaks/sql `revision`): a
|
|
145
|
+
* catalog current then takes them in and stays current, so the next plan
|
|
146
|
+
* reads none of the descriptors it held. Any other catalog is read again.
|
|
147
|
+
*/
|
|
148
|
+
export function described(
|
|
149
|
+
driver: Driver,
|
|
150
|
+
from: number,
|
|
151
|
+
rows: readonly { id: number; tables: string }[],
|
|
152
|
+
): void {
|
|
153
|
+
let cache = cacheFor(driver)
|
|
154
|
+
if (!cache.ids || cache.descriptorVersion != from) return
|
|
155
|
+
let was = cache.descriptors
|
|
156
|
+
for (let { id, tables } of rows) {
|
|
157
|
+
cache.ids.set(descriptor(driver, tables).eid, id)
|
|
158
|
+
}
|
|
159
|
+
cache.descriptors = archetypeSet(cache.sets, cache.ids)
|
|
160
|
+
cache.descriptorVersion = revision(driver, 'descriptors')
|
|
161
|
+
if (driver.file) cache.tableVersion = tableVersion(driver, 'archetype')
|
|
162
|
+
if (cache.set == was) cache.set = cache.descriptors
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* A lazy, single-plan catalog snapshot. Value-only queries do not need it;
|
|
167
|
+
* the first component-presence or kind predicate loads it and all others share
|
|
168
|
+
* that snapshot.
|
|
169
|
+
* Create one per compile, not once per connection. Compile and execute inside
|
|
170
|
+
* the same transaction, so another writer cannot change the catalog between.
|
|
171
|
+
*/
|
|
172
|
+
export function catalog(
|
|
173
|
+
driver: Driver,
|
|
174
|
+
pending?:
|
|
175
|
+
| ReadonlyMap<number, readonly string[]>
|
|
176
|
+
| (() => ReadonlyMap<number, readonly string[]>),
|
|
177
|
+
): ArchetypeSet {
|
|
178
|
+
let loaded = false
|
|
179
|
+
let current: ArchetypeSet | undefined
|
|
180
|
+
let moved: ReadonlyMap<number, readonly string[]> | undefined
|
|
181
|
+
let shapes = () =>
|
|
182
|
+
moved ??= typeof pending == 'function' ? pending() : pending
|
|
183
|
+
let cache = cacheFor(driver)
|
|
184
|
+
let choose = (predicate: Parameters<ArchetypeSet>[0]) => {
|
|
185
|
+
if (!loaded) {
|
|
186
|
+
current = snapshot(driver, shapes())
|
|
187
|
+
loaded = true
|
|
188
|
+
}
|
|
189
|
+
return current?.(predicate)
|
|
190
|
+
}
|
|
191
|
+
if (pending) {
|
|
192
|
+
Object.defineProperty(choose, 'pending', {
|
|
193
|
+
get: () => {
|
|
194
|
+
let pending = shapes()
|
|
195
|
+
return pending?.size
|
|
196
|
+
? {
|
|
197
|
+
ids: [...pending.keys()],
|
|
198
|
+
matching: (predicate: Parameters<ArchetypeSet>[0]) =>
|
|
199
|
+
[...pending].filter(([, tables]) =>
|
|
200
|
+
satisfies(predicate, { tables })
|
|
201
|
+
).map(([id]) => id),
|
|
202
|
+
}
|
|
203
|
+
: undefined
|
|
204
|
+
},
|
|
205
|
+
})
|
|
206
|
+
}
|
|
207
|
+
return Object.defineProperty(choose, 'held', {
|
|
208
|
+
get: () => {
|
|
209
|
+
if (shapes()?.size) return true
|
|
210
|
+
if (!cache.descriptors) return false
|
|
211
|
+
let version = revision(driver, 'descriptors')
|
|
212
|
+
if (cache.descriptorVersion == version) return true
|
|
213
|
+
if (
|
|
214
|
+
cache.tableVersion === undefined ||
|
|
215
|
+
cache.schemaVersion != revision(driver, 'schema') ||
|
|
216
|
+
cache.tableVersion != tableVersion(driver, 'archetype')
|
|
217
|
+
) return false
|
|
218
|
+
// Only descriptor freshness is proven. snapshot() still checks for
|
|
219
|
+
// corrupt owners before handing this set to a plan.
|
|
220
|
+
cache.descriptorVersion = version
|
|
221
|
+
return true
|
|
222
|
+
},
|
|
223
|
+
}) as ArchetypeSet
|
|
224
|
+
}
|
package/check.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// SQLite diagnostics bound to the adapter's driver. ./tools.ts exposes
|
|
2
|
+
// these findings through the check tools without receiving the driver.
|
|
3
|
+
//
|
|
4
|
+
// These are the checks that cannot be run as graph queries. An orphaned
|
|
5
|
+
// component row and a reference to an entity that is gone are invisible to
|
|
6
|
+
// every query by construction — a read starts from the spine, so a row with no
|
|
7
|
+
// spine is not there to be found, which is exactly why the fleet's copies of
|
|
8
|
+
// them went unnoticed for months. They are questions about the file, so they
|
|
9
|
+
// belong to the package that owns the file, and they are run against the
|
|
10
|
+
// driver bound by `checks(driver)`, not the portable host.
|
|
11
|
+
//
|
|
12
|
+
// What the schema already prevents is most of it: every component table keys
|
|
13
|
+
// to `entity(id)` and every reference carries a foreign key, so neither state
|
|
14
|
+
// can be written while foreign keys are on (./ddl.ts). That makes this check
|
|
15
|
+
// cheap and precise instead of a scan: ask SQLite whether enforcement is on,
|
|
16
|
+
// and ask it which rows violate a key. A file that was written by a connection
|
|
17
|
+
// with `foreign_keys` off is how the impossible gets in, and that is the one
|
|
18
|
+
// thing worth reporting. A `keep` reference is deliberately key-free — it
|
|
19
|
+
// outlives the row it names, which is what "kept as history" means — so it is
|
|
20
|
+
// never a finding here.
|
|
21
|
+
//
|
|
22
|
+
// The archetype pointer is the same kind of question. @yaks/archetype
|
|
23
|
+
// maintains it inside the transaction, and ./archetype.ts `drift` reads the
|
|
24
|
+
// physical presence it should describe; a raw writer that inserts rows without
|
|
25
|
+
// naming the owners it touched leaves a pointer both the read path and the
|
|
26
|
+
// query planner trust. @yaks/archetype cannot run this check — it knows
|
|
27
|
+
// component sets and moves, not tables — so it lives here, where the file is.
|
|
28
|
+
import { tally } from "@yaks/fp";
|
|
29
|
+
import { drift } from "./archetype.js";
|
|
30
|
+
let broken = tally((r)=>`${String(r.table)} → ${String(r.parent)}`);
|
|
31
|
+
/** The adapter's diagnostics, bound to its own connection. */ export let checks = (driver)=>({
|
|
32
|
+
storage: ()=>{
|
|
33
|
+
let found = [];
|
|
34
|
+
// Enforcement first: every finding below is only as reliable as this
|
|
35
|
+
// pragma, and a file written with it off is how a broken key gets in at
|
|
36
|
+
// all.
|
|
37
|
+
let on = driver.query({
|
|
38
|
+
t: 'pragma',
|
|
39
|
+
name: 'foreign_keys'
|
|
40
|
+
})[0];
|
|
41
|
+
if (!Number(Object.values(on ?? {})[0] ?? 0)) {
|
|
42
|
+
found.push({
|
|
43
|
+
level: 'warn',
|
|
44
|
+
text: 'this connection has `foreign_keys` off — the schema stops ' + 'orphaned rows and dangling references only while it is on, and a ' + 'writer that opened the file this way could have left either'
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
for (let [said, n] of Object.entries(broken(driver.query({
|
|
48
|
+
t: 'pragma',
|
|
49
|
+
name: 'foreign_key_check'
|
|
50
|
+
})))){
|
|
51
|
+
found.push({
|
|
52
|
+
level: 'fail',
|
|
53
|
+
text: `${said}: ${n} row(s) point at an entity that is not there — ` + `a component row with no spine, or a reference to a deleted entity`
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
let verdict = driver.query({
|
|
57
|
+
t: 'pragma',
|
|
58
|
+
name: 'integrity_check'
|
|
59
|
+
})[0];
|
|
60
|
+
let said = String(Object.values(verdict ?? {})[0] ?? 'unknown');
|
|
61
|
+
if (said != 'ok') {
|
|
62
|
+
found.push({
|
|
63
|
+
level: 'fail',
|
|
64
|
+
text: `SQLite's own integrity check answers ${said}`
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
return found;
|
|
68
|
+
},
|
|
69
|
+
archetypes: (sample)=>{
|
|
70
|
+
let d = drift(driver, sample);
|
|
71
|
+
let rest = d.drifted - d.sample.length;
|
|
72
|
+
return d.drifted ? [
|
|
73
|
+
{
|
|
74
|
+
level: 'fail',
|
|
75
|
+
text: `${d.drifted} of ${d.checked} pointer(s) disagree with the ` + `tables their owner has rows in: ${d.sample.join(', ')}` + `${rest > 0 ? `, and ${rest} more` : ''}. Something wrote rows ` + `past the graph without naming the owners it touched, or this ` + `file was never classified at all (./archetype.ts backfill)`
|
|
76
|
+
}
|
|
77
|
+
] : [];
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
//# sourceMappingURL=check.js.map
|
package/check.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"check.js","sources":["./check.ts"],"names":[],"mappings":"AAAA,uEAAuE;AACvE,uEAAuE;AACvE,EAAE;AACF,wEAAwE;AACxE,2EAA2E;AAC3E,+EAA+E;AAC/E,6EAA6E;AAC7E,6EAA6E;AAC7E,yEAAyE;AACzE,2DAA2D;AAC3D,EAAE;AACF,6EAA6E;AAC7E,8EAA8E;AAC9E,6EAA6E;AAC7E,6EAA6E;AAC7E,+EAA+E;AAC/E,6EAA6E;AAC7E,0EAA0E;AAC1E,8EAA8E;AAC9E,wBAAwB;AACxB,EAAE;AACF,sEAAsE;AACtE,4EAA4E;AAC5E,+EAA+E;AAC/E,2EAA2E;AAC3E,wEAAwE;AACxE,8EAA8E;AAE9E,SAAS,KAAK,wBAA6B;AAE3C,SAAS,KAAK,yBAAwB;AAOtC,IAAI,SAAS,MAAM,CAAC,IAAiB,GAAG,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,GAAG;AAE/E,4DAA4D,GAC5D,OAAO,IAAI,SAAS,CAAC,SAAmB,CAAC;IACvC,SAAS;MACP,IAAI,QAAmB,EAAE;MACzB,qEAAqE;MACrE,wEAAwE;MACxE,OAAO;MACP,IAAI,KAAK,OAAO,KAAK,CAAC;QAAE,GAAG;QAAU,MAAM;MAAe,EAAE,CAAC,EAAE;MAC/D,IAAI,CAAC,OAAO,OAAO,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,IAAI,IAAI;QAC5C,MAAM,IAAI,CAAC;UACT,OAAO;UACP,MAAM,+DACJ,sEACA;QACJ;MACF;MACA,KACE,IAAI,CAAC,MAAM,EAAE,IAAI,OAAO,OAAO,CAAC,OAC9B,OAAO,KAAK,CAAC;QAAE,GAAG;QAAU,MAAM;MAAoB,KAExD;QACA,MAAM,IAAI,CAAC;UACT,OAAO;UACP,MAAM,GAAG,KAAK,EAAE,EAAE,EAAE,+CAA+C,CAAC,GAClE,CAAC,iEAAiE,CAAC;QACvE;MACF;MACA,IAAI,UAAU,OAAO,KAAK,CAAC;QAAE,GAAG;QAAU,MAAM;MAAkB,EAAE,CAAC,EAAE;MACvE,IAAI,OAAO,OAAO,OAAO,MAAM,CAAC,WAAW,CAAC,EAAE,CAAC,EAAE,IAAI;MACrD,IAAI,QAAQ,MAAM;QAChB,MAAM,IAAI,CAAC;UACT,OAAO;UACP,MAAM,CAAC,qCAAqC,EAAE,MAAM;QACtD;MACF;MACA,OAAO;IACT;IAEA,YAAY,CAAC;MACX,IAAI,IAAI,MAAM,QAAQ;MACtB,IAAI,OAAO,EAAE,OAAO,GAAG,EAAE,MAAM,CAAC,MAAM;MACtC,OAAO,EAAE,OAAO,GACZ;QAAC;UACD,OAAO;UACP,MAAM,GAAG,EAAE,OAAO,CAAC,IAAI,EAAE,EAAE,OAAO,CAAC,8BAA8B,CAAC,GAChE,CAAC,gCAAgC,EAAE,EAAE,MAAM,CAAC,IAAI,CAAC,OAAO,GACxD,GAAG,OAAO,IAAI,CAAC,MAAM,EAAE,KAAK,KAAK,CAAC,GAAG,GAAG,uBAAuB,CAAC,GAChE,CAAC,6DAA6D,CAAC,GAC/D,CAAC,0DAA0D,CAAC;QAChE;OAAE,GACA,EAAE;IACR;EACF,CAAC,EAAC"}
|
package/check.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
// SQLite diagnostics bound to the adapter's driver. ./tools.ts exposes
|
|
2
|
+
// these findings through the check tools without receiving the driver.
|
|
3
|
+
//
|
|
4
|
+
// These are the checks that cannot be run as graph queries. An orphaned
|
|
5
|
+
// component row and a reference to an entity that is gone are invisible to
|
|
6
|
+
// every query by construction — a read starts from the spine, so a row with no
|
|
7
|
+
// spine is not there to be found, which is exactly why the fleet's copies of
|
|
8
|
+
// them went unnoticed for months. They are questions about the file, so they
|
|
9
|
+
// belong to the package that owns the file, and they are run against the
|
|
10
|
+
// driver bound by `checks(driver)`, not the portable host.
|
|
11
|
+
//
|
|
12
|
+
// What the schema already prevents is most of it: every component table keys
|
|
13
|
+
// to `entity(id)` and every reference carries a foreign key, so neither state
|
|
14
|
+
// can be written while foreign keys are on (./ddl.ts). That makes this check
|
|
15
|
+
// cheap and precise instead of a scan: ask SQLite whether enforcement is on,
|
|
16
|
+
// and ask it which rows violate a key. A file that was written by a connection
|
|
17
|
+
// with `foreign_keys` off is how the impossible gets in, and that is the one
|
|
18
|
+
// thing worth reporting. A `keep` reference is deliberately key-free — it
|
|
19
|
+
// outlives the row it names, which is what "kept as history" means — so it is
|
|
20
|
+
// never a finding here.
|
|
21
|
+
//
|
|
22
|
+
// The archetype pointer is the same kind of question. @yaks/archetype
|
|
23
|
+
// maintains it inside the transaction, and ./archetype.ts `drift` reads the
|
|
24
|
+
// physical presence it should describe; a raw writer that inserts rows without
|
|
25
|
+
// naming the owners it touched leaves a pointer both the read path and the
|
|
26
|
+
// query planner trust. @yaks/archetype cannot run this check — it knows
|
|
27
|
+
// component sets and moves, not tables — so it lives here, where the file is.
|
|
28
|
+
|
|
29
|
+
import { tally } from '@yaks/fp'
|
|
30
|
+
import type { Finding } from 'jsr:@yaks/tools@0.2.36'
|
|
31
|
+
import { drift } from './archetype.js'
|
|
32
|
+
import type { Driver } from 'jsr:@yaks/sql@0.2.36'
|
|
33
|
+
|
|
34
|
+
// `pragma foreign_key_check` returns one row per violation: the table holding
|
|
35
|
+
// it, its rowid, and which of that table's foreign keys it broke.
|
|
36
|
+
type Violation = { table?: unknown; parent?: unknown; rowid?: unknown }
|
|
37
|
+
|
|
38
|
+
let broken = tally((r: Violation) => `${String(r.table)} → ${String(r.parent)}`)
|
|
39
|
+
|
|
40
|
+
/** The adapter's diagnostics, bound to its own connection. */
|
|
41
|
+
export let checks = (driver: Driver) => ({
|
|
42
|
+
storage: (): Finding[] => {
|
|
43
|
+
let found: Finding[] = []
|
|
44
|
+
// Enforcement first: every finding below is only as reliable as this
|
|
45
|
+
// pragma, and a file written with it off is how a broken key gets in at
|
|
46
|
+
// all.
|
|
47
|
+
let on = driver.query({ t: 'pragma', name: 'foreign_keys' })[0]
|
|
48
|
+
if (!Number(Object.values(on ?? {})[0] ?? 0)) {
|
|
49
|
+
found.push({
|
|
50
|
+
level: 'warn',
|
|
51
|
+
text: 'this connection has `foreign_keys` off — the schema stops ' +
|
|
52
|
+
'orphaned rows and dangling references only while it is on, and a ' +
|
|
53
|
+
'writer that opened the file this way could have left either',
|
|
54
|
+
})
|
|
55
|
+
}
|
|
56
|
+
for (
|
|
57
|
+
let [said, n] of Object.entries(broken(
|
|
58
|
+
driver.query({ t: 'pragma', name: 'foreign_key_check' }),
|
|
59
|
+
))
|
|
60
|
+
) {
|
|
61
|
+
found.push({
|
|
62
|
+
level: 'fail',
|
|
63
|
+
text: `${said}: ${n} row(s) point at an entity that is not there — ` +
|
|
64
|
+
`a component row with no spine, or a reference to a deleted entity`,
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
let verdict = driver.query({ t: 'pragma', name: 'integrity_check' })[0]
|
|
68
|
+
let said = String(Object.values(verdict ?? {})[0] ?? 'unknown')
|
|
69
|
+
if (said != 'ok') {
|
|
70
|
+
found.push({
|
|
71
|
+
level: 'fail',
|
|
72
|
+
text: `SQLite's own integrity check answers ${said}`,
|
|
73
|
+
})
|
|
74
|
+
}
|
|
75
|
+
return found
|
|
76
|
+
},
|
|
77
|
+
|
|
78
|
+
archetypes: (sample: number): Finding[] => {
|
|
79
|
+
let d = drift(driver, sample)
|
|
80
|
+
let rest = d.drifted - d.sample.length
|
|
81
|
+
return d.drifted
|
|
82
|
+
? [{
|
|
83
|
+
level: 'fail',
|
|
84
|
+
text: `${d.drifted} of ${d.checked} pointer(s) disagree with the ` +
|
|
85
|
+
`tables their owner has rows in: ${d.sample.join(', ')}` +
|
|
86
|
+
`${rest > 0 ? `, and ${rest} more` : ''}. Something wrote rows ` +
|
|
87
|
+
`past the graph without naming the owners it touched, or this ` +
|
|
88
|
+
`file was never classified at all (./archetype.ts backfill)`,
|
|
89
|
+
}]
|
|
90
|
+
: []
|
|
91
|
+
},
|
|
92
|
+
})
|
package/checkpoint.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// A file's checkpoints run on a thread of their own: the sync they wait for
|
|
2
|
+
// holds no commit open. Each database owns a shared lifetime, so closing can
|
|
3
|
+
// cancel queued work without waiting for the thread to receive its message.
|
|
4
|
+
import { Worker } from 'node:worker_threads';
|
|
5
|
+
let IDLE = 0, QUEUED = 1, RUNNING = 2, CLOSED = 4;
|
|
6
|
+
let worker;
|
|
7
|
+
let serial = 0;
|
|
8
|
+
/** Retain requests until their thread answers or exits. A failed thread leaves
|
|
9
|
+
* live owners able to request work again; closed owners stay closed. */ export let pending = (send)=>{
|
|
10
|
+
let requests = new Map();
|
|
11
|
+
return {
|
|
12
|
+
send: (request)=>{
|
|
13
|
+
requests.set(request.id, request.state);
|
|
14
|
+
try {
|
|
15
|
+
send(request);
|
|
16
|
+
} catch (error) {
|
|
17
|
+
requests.delete(request.id);
|
|
18
|
+
throw error;
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
done: (id)=>requests.delete(id),
|
|
22
|
+
failed: ()=>{
|
|
23
|
+
for (let state of requests.values()){
|
|
24
|
+
Atomics.and(state, 0, CLOSED);
|
|
25
|
+
Atomics.notify(state, 0);
|
|
26
|
+
}
|
|
27
|
+
requests.clear();
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
let start = ()=>{
|
|
32
|
+
let w = new Worker(new URL('./checkpoint_worker.ts', import.meta.url));
|
|
33
|
+
w.unref();
|
|
34
|
+
w.on('message', ({ id, path, error })=>{
|
|
35
|
+
requests.done(id);
|
|
36
|
+
if (error) reportError(new Error(`checkpoint of ${path} failed: ${error}`));
|
|
37
|
+
});
|
|
38
|
+
let failed = false;
|
|
39
|
+
w.on('error', (error)=>{
|
|
40
|
+
failed = true;
|
|
41
|
+
reportError(error);
|
|
42
|
+
});
|
|
43
|
+
w.on('exit', (code)=>{
|
|
44
|
+
worker = undefined;
|
|
45
|
+
requests.failed();
|
|
46
|
+
if (!failed) reportError(new Error(`checkpoint thread exited (${code})`));
|
|
47
|
+
});
|
|
48
|
+
return w;
|
|
49
|
+
};
|
|
50
|
+
let requests = pending((request)=>(worker ??= start()).postMessage(request));
|
|
51
|
+
/** Bind checkpoint requests to one database's lifetime. Commits only enqueue;
|
|
52
|
+
* idle or queued work closes immediately. Only work already holding a file
|
|
53
|
+
* must finish before close returns, independently of main-thread messages. */ export let checkpoint = (path, send = requests.send, wait = Atomics.wait)=>{
|
|
54
|
+
let state = new Int32Array(new SharedArrayBuffer(4));
|
|
55
|
+
return {
|
|
56
|
+
run: ()=>{
|
|
57
|
+
if (Atomics.compareExchange(state, 0, IDLE, QUEUED) != IDLE) return;
|
|
58
|
+
try {
|
|
59
|
+
send({
|
|
60
|
+
id: ++serial,
|
|
61
|
+
path,
|
|
62
|
+
state
|
|
63
|
+
});
|
|
64
|
+
} catch (error) {
|
|
65
|
+
Atomics.compareExchange(state, 0, QUEUED, IDLE);
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
close: (release)=>{
|
|
70
|
+
Atomics.or(state, 0, CLOSED);
|
|
71
|
+
try {
|
|
72
|
+
release();
|
|
73
|
+
} finally{
|
|
74
|
+
while(Atomics.load(state, 0) == (CLOSED | RUNNING)){
|
|
75
|
+
wait(state, 0, CLOSED | RUNNING);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
/** Take a queued request before opening its file, and retain ownership through
|
|
82
|
+
* the file's close, including failures. The thread supplies the file boundary;
|
|
83
|
+
* in-process callers can exercise the same lifetime without starting it. */ export let perform = (request, open)=>{
|
|
84
|
+
let { path, state } = request;
|
|
85
|
+
if (Atomics.compareExchange(state, 0, QUEUED, RUNNING) != QUEUED) return;
|
|
86
|
+
try {
|
|
87
|
+
let file = open(path);
|
|
88
|
+
try {
|
|
89
|
+
file.checkpoint();
|
|
90
|
+
} finally{
|
|
91
|
+
file.close();
|
|
92
|
+
}
|
|
93
|
+
} finally{
|
|
94
|
+
Atomics.and(state, 0, CLOSED);
|
|
95
|
+
Atomics.notify(state, 0);
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
//# sourceMappingURL=checkpoint.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"checkpoint.js","sources":["./checkpoint.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,6EAA6E;AAC7E,4EAA4E;AAE5E,SAAS,MAAM,QAAQ,sBAAqB;AAiB5C,IAAI,OAAO,GAAG,SAAS,GAAG,UAAU,GAAG,SAAS;AAChD,IAAI;AACJ,IAAI,SAAS;AASb;sEACsE,GACtE,OAAO,IAAI,UAAU,CAAC;EACpB,IAAI,WAAW,IAAI;EACnB,OAAO;IACL,MAAM,CAAC;MACL,SAAS,GAAG,CAAC,QAAQ,EAAE,EAAE,QAAQ,KAAK;MACtC,IAAI;QACF,KAAK;MACP,EAAE,OAAO,OAAO;QACd,SAAS,MAAM,CAAC,QAAQ,EAAE;QAC1B,MAAM;MACR;IACF;IACA,MAAM,CAAC,KAAO,SAAS,MAAM,CAAC;IAC9B,QAAQ;MACN,KAAK,IAAI,SAAS,SAAS,MAAM,GAAI;QACnC,QAAQ,GAAG,CAAC,OAAO,GAAG;QACtB,QAAQ,MAAM,CAAC,OAAO;MACxB;MACA,SAAS,KAAK;IAChB;EACF;AACF,EAAC;AAED,IAAI,QAAQ;EACV,IAAI,IAAI,IAAI,OAAO,IAAI,IAAI,0BAA0B,YAAY,GAAG;EACpE,EAAE,KAAK;EACP,EAAE,EAAE,CAAC,WAAW,CAAC,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAQ;IACxC,SAAS,IAAI,CAAC;IACd,IAAI,OAAO,YAAY,IAAI,MAAM,CAAC,cAAc,EAAE,KAAK,SAAS,EAAE,OAAO;EAC3E;EACA,IAAI,SAAS;EACb,EAAE,EAAE,CAAC,SAAS,CAAC;IACb,SAAS;IACT,YAAY;EACd;EACA,EAAE,EAAE,CAAC,QAAQ,CAAC;IACZ,SAAS;IACT,SAAS,MAAM;IACf,IAAI,CAAC,QAAQ,YAAY,IAAI,MAAM,CAAC,0BAA0B,EAAE,KAAK,CAAC,CAAC;EACzE;EACA,OAAO;AACT;AAEA,IAAI,WAAW,QAAQ,CAAC,UAAY,CAAC,WAAW,OAAO,EAAE,WAAW,CAAC;AAErE;;4EAE4E,GAC5E,OAAO,IAAI,aAAa,CACtB,MACA,OAAmC,SAAS,IAAI,EAChD,OAAa,QAAQ,IAAI;EAEzB,IAAI,QAAQ,IAAI,WAAW,IAAI,kBAAkB;EACjD,OAAO;IACL,KAAK;MACH,IAAI,QAAQ,eAAe,CAAC,OAAO,GAAG,MAAM,WAAW,MAAM;MAC7D,IAAI;QACF,KAAK;UAAE,IAAI,EAAE;UAAQ;UAAM;QAAM;MACnC,EAAE,OAAO,OAAO;QACd,QAAQ,eAAe,CAAC,OAAO,GAAG,QAAQ;QAC1C,MAAM;MACR;IACF;IACA,OAAO,CAAC;MACN,QAAQ,EAAE,CAAC,OAAO,GAAG;MACrB,IAAI;QACF;MACF,SAAU;QACR,MAAO,QAAQ,IAAI,CAAC,OAAO,MAAM,CAAC,SAAS,OAAO,EAAG;UACnD,KAAK,OAAO,GAAG,SAAS;QAC1B;MACF;IACF;EACF;AACF,EAAC;AAED;;0EAE0E,GAC1E,OAAO,IAAI,UAAU,CAAC,SAAkB;EACtC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG;EACtB,IAAI,QAAQ,eAAe,CAAC,OAAO,GAAG,QAAQ,YAAY,QAAQ;EAClE,IAAI;IACF,IAAI,OAAO,KAAK;IAChB,IAAI;MACF,KAAK,UAAU;IACjB,SAAU;MACR,KAAK,KAAK;IACZ;EACF,SAAU;IACR,QAAQ,GAAG,CAAC,OAAO,GAAG;IACtB,QAAQ,MAAM,CAAC,OAAO;EACxB;AACF,EAAC"}
|
package/checkpoint.ts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// A file's checkpoints run on a thread of their own: the sync they wait for
|
|
2
|
+
// holds no commit open. Each database owns a shared lifetime, so closing can
|
|
3
|
+
// cancel queued work without waiting for the thread to receive its message.
|
|
4
|
+
|
|
5
|
+
import { Worker } from 'node:worker_threads'
|
|
6
|
+
|
|
7
|
+
/** A checkpoint request belongs to one open database, even when another
|
|
8
|
+
* database is opened later at the same path. */
|
|
9
|
+
export type Request = { id: number; path: string; state: Int32Array }
|
|
10
|
+
|
|
11
|
+
/** What the thread answers for a file it checkpointed. */
|
|
12
|
+
export type Done = { id: number; path: string; error?: string }
|
|
13
|
+
|
|
14
|
+
/** Checkpoint work owned by a database. Closing releases the owner's handle
|
|
15
|
+
* before waiting for an already running checkpoint to release its own. */
|
|
16
|
+
export type Checkpoint = { run(): void; close(release: () => void): void }
|
|
17
|
+
|
|
18
|
+
type Wait = (state: Int32Array, index: number, value: number) => unknown
|
|
19
|
+
|
|
20
|
+
type File = { checkpoint(): void; close(): void }
|
|
21
|
+
|
|
22
|
+
let IDLE = 0, QUEUED = 1, RUNNING = 2, CLOSED = 4
|
|
23
|
+
let worker: Worker | undefined
|
|
24
|
+
let serial = 0
|
|
25
|
+
|
|
26
|
+
/** Outstanding work for one thread, identified by owner rather than path. */
|
|
27
|
+
export type Pending = {
|
|
28
|
+
send(request: Request): void
|
|
29
|
+
done(id: number): void
|
|
30
|
+
failed(): void
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Retain requests until their thread answers or exits. A failed thread leaves
|
|
34
|
+
* live owners able to request work again; closed owners stay closed. */
|
|
35
|
+
export let pending = (send: (request: Request) => void): Pending => {
|
|
36
|
+
let requests = new Map<number, Int32Array>()
|
|
37
|
+
return {
|
|
38
|
+
send: (request) => {
|
|
39
|
+
requests.set(request.id, request.state)
|
|
40
|
+
try {
|
|
41
|
+
send(request)
|
|
42
|
+
} catch (error) {
|
|
43
|
+
requests.delete(request.id)
|
|
44
|
+
throw error
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
done: (id) => requests.delete(id),
|
|
48
|
+
failed: () => {
|
|
49
|
+
for (let state of requests.values()) {
|
|
50
|
+
Atomics.and(state, 0, CLOSED)
|
|
51
|
+
Atomics.notify(state, 0)
|
|
52
|
+
}
|
|
53
|
+
requests.clear()
|
|
54
|
+
},
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
let start = () => {
|
|
59
|
+
let w = new Worker(new URL('./checkpoint_worker.ts', import.meta.url))
|
|
60
|
+
w.unref()
|
|
61
|
+
w.on('message', ({ id, path, error }: Done) => {
|
|
62
|
+
requests.done(id)
|
|
63
|
+
if (error) reportError(new Error(`checkpoint of ${path} failed: ${error}`))
|
|
64
|
+
})
|
|
65
|
+
let failed = false
|
|
66
|
+
w.on('error', (error) => {
|
|
67
|
+
failed = true
|
|
68
|
+
reportError(error)
|
|
69
|
+
})
|
|
70
|
+
w.on('exit', (code) => {
|
|
71
|
+
worker = undefined
|
|
72
|
+
requests.failed()
|
|
73
|
+
if (!failed) reportError(new Error(`checkpoint thread exited (${code})`))
|
|
74
|
+
})
|
|
75
|
+
return w
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
let requests = pending((request) => (worker ??= start()).postMessage(request))
|
|
79
|
+
|
|
80
|
+
/** Bind checkpoint requests to one database's lifetime. Commits only enqueue;
|
|
81
|
+
* idle or queued work closes immediately. Only work already holding a file
|
|
82
|
+
* must finish before close returns, independently of main-thread messages. */
|
|
83
|
+
export let checkpoint = (
|
|
84
|
+
path: string,
|
|
85
|
+
send: (request: Request) => void = requests.send,
|
|
86
|
+
wait: Wait = Atomics.wait,
|
|
87
|
+
): Checkpoint => {
|
|
88
|
+
let state = new Int32Array(new SharedArrayBuffer(4))
|
|
89
|
+
return {
|
|
90
|
+
run: () => {
|
|
91
|
+
if (Atomics.compareExchange(state, 0, IDLE, QUEUED) != IDLE) return
|
|
92
|
+
try {
|
|
93
|
+
send({ id: ++serial, path, state })
|
|
94
|
+
} catch (error) {
|
|
95
|
+
Atomics.compareExchange(state, 0, QUEUED, IDLE)
|
|
96
|
+
throw error
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
close: (release) => {
|
|
100
|
+
Atomics.or(state, 0, CLOSED)
|
|
101
|
+
try {
|
|
102
|
+
release()
|
|
103
|
+
} finally {
|
|
104
|
+
while (Atomics.load(state, 0) == (CLOSED | RUNNING)) {
|
|
105
|
+
wait(state, 0, CLOSED | RUNNING)
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Take a queued request before opening its file, and retain ownership through
|
|
113
|
+
* the file's close, including failures. The thread supplies the file boundary;
|
|
114
|
+
* in-process callers can exercise the same lifetime without starting it. */
|
|
115
|
+
export let perform = (request: Request, open: (path: string) => File): void => {
|
|
116
|
+
let { path, state } = request
|
|
117
|
+
if (Atomics.compareExchange(state, 0, QUEUED, RUNNING) != QUEUED) return
|
|
118
|
+
try {
|
|
119
|
+
let file = open(path)
|
|
120
|
+
try {
|
|
121
|
+
file.checkpoint()
|
|
122
|
+
} finally {
|
|
123
|
+
file.close()
|
|
124
|
+
}
|
|
125
|
+
} finally {
|
|
126
|
+
Atomics.and(state, 0, CLOSED)
|
|
127
|
+
Atomics.notify(state, 0)
|
|
128
|
+
}
|
|
129
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// One connection per running checkpoint, released before its owner can finish
|
|
2
|
+
// closing. Opening never creates a file or installs another checkpoint hook.
|
|
3
|
+
|
|
4
|
+
import { parentPort } from 'node:worker_threads'
|
|
5
|
+
import { Database } from './legacy.ts'
|
|
6
|
+
import { configure } from './native.ts'
|
|
7
|
+
import { render } from 'jsr:@yaks/sql@0.2.36'
|
|
8
|
+
import { type Done, perform, type Request } from './checkpoint.ts'
|
|
9
|
+
|
|
10
|
+
let open = (path: string) => {
|
|
11
|
+
let db = new Database(path, { create: false })
|
|
12
|
+
try {
|
|
13
|
+
db.exec(render({ t: 'pragma', name: 'busy_timeout', value: 60_000 }).sql)
|
|
14
|
+
configure(db, 'SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE', true)
|
|
15
|
+
db.exec(render({ t: 'pragma', name: 'synchronous', value: 'normal' }).sql)
|
|
16
|
+
return {
|
|
17
|
+
checkpoint: () =>
|
|
18
|
+
db.exec(
|
|
19
|
+
render({ t: 'pragma', name: 'wal_checkpoint', value: 'passive' }).sql,
|
|
20
|
+
),
|
|
21
|
+
close: () => db.close(),
|
|
22
|
+
}
|
|
23
|
+
} catch (error) {
|
|
24
|
+
db.close()
|
|
25
|
+
throw error
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
parentPort!.on('message', (request: Request) => {
|
|
30
|
+
let done: Done = { id: request.id, path: request.path }
|
|
31
|
+
try {
|
|
32
|
+
perform(request, open)
|
|
33
|
+
} catch (e) {
|
|
34
|
+
done.error = e instanceof Error ? e.stack ?? e.message : String(e)
|
|
35
|
+
}
|
|
36
|
+
parentPort!.postMessage(done)
|
|
37
|
+
})
|