@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/owe.ts
ADDED
|
@@ -0,0 +1,873 @@
|
|
|
1
|
+
// Work a schema change leaves whose cost grows with rows (D-121389): an index
|
|
2
|
+
// over a populated table, a table's refit, filling a text index. The pass that
|
|
3
|
+
// brings a file to its schema (./follow.ts) does what costs the same at any
|
|
4
|
+
// size and writes the rest down here, in `server_meta` as
|
|
5
|
+
// `owed:<kind>:<name>`: a JSON value holding the statements that do it and
|
|
6
|
+
// how far it has got. The statements are data, so a process whose vocabulary
|
|
7
|
+
// moved on can still finish what an older one owed.
|
|
8
|
+
//
|
|
9
|
+
// One process at a time works it off ({@link tend}), one window per unit, each
|
|
10
|
+
// holding the write lock for about a tenth of a second on the box's copy; on
|
|
11
|
+
// the box that is whichever process serves the effects role, under the
|
|
12
|
+
// `@yaks/sqlite/schema` lease (packages/cli/host.ts). Opening never waits for
|
|
13
|
+
// it, and the graph serves the shape it holds meanwhile: a query runs without
|
|
14
|
+
// the index it will have. A window that fails is reported and tried again an
|
|
15
|
+
// hour later, and nothing refuses an open on its account.
|
|
16
|
+
//
|
|
17
|
+
// The same process does a file's upkeep ({@link Daily}): the planner
|
|
18
|
+
// statistics an install or the day owes, a table a window, and
|
|
19
|
+
// whatever a host adds, such as the box's check of its text indexes. It also
|
|
20
|
+
// gives back the pages a bulk delete freed ({@link reclaim}), so the file
|
|
21
|
+
// shrinks without a VACUUM. Opening does none of it.
|
|
22
|
+
//
|
|
23
|
+
// A file in memory belongs to one process, which has nobody to owe: its work
|
|
24
|
+
// is done at once, as it was before any of this.
|
|
25
|
+
|
|
26
|
+
import {
|
|
27
|
+
and,
|
|
28
|
+
as,
|
|
29
|
+
catalogue,
|
|
30
|
+
col,
|
|
31
|
+
count,
|
|
32
|
+
type CreateIndex,
|
|
33
|
+
type CreateTable,
|
|
34
|
+
type CreateTrigger,
|
|
35
|
+
type Driver,
|
|
36
|
+
eq,
|
|
37
|
+
erect,
|
|
38
|
+
type Expr,
|
|
39
|
+
fn,
|
|
40
|
+
from,
|
|
41
|
+
gt,
|
|
42
|
+
type Insert,
|
|
43
|
+
le,
|
|
44
|
+
lit,
|
|
45
|
+
lt,
|
|
46
|
+
op,
|
|
47
|
+
over,
|
|
48
|
+
type SchemaObject,
|
|
49
|
+
select,
|
|
50
|
+
type Stmt,
|
|
51
|
+
stored,
|
|
52
|
+
sub,
|
|
53
|
+
table,
|
|
54
|
+
val,
|
|
55
|
+
} from '@yaks/sql/core'
|
|
56
|
+
import type { Filler } from 'jsr:@yaks/fts@0.2.36'
|
|
57
|
+
import {
|
|
58
|
+
analyzed,
|
|
59
|
+
analyzing,
|
|
60
|
+
carried,
|
|
61
|
+
counted,
|
|
62
|
+
needless,
|
|
63
|
+
type Refitting,
|
|
64
|
+
tallied,
|
|
65
|
+
tally,
|
|
66
|
+
} from './ddl.js'
|
|
67
|
+
import { BUILDING, META } from './layout.js'
|
|
68
|
+
import { analyzedAt, installation, meta, under } from './meta.js'
|
|
69
|
+
import { unit } from './unit.js'
|
|
70
|
+
|
|
71
|
+
/** The prefix of an owed piece of work's `server_meta` key. */
|
|
72
|
+
export let OWED = 'owed:'
|
|
73
|
+
/** The lease the process working owed work off holds. */
|
|
74
|
+
export let SCHEMA_DUTY = '@yaks/sqlite/schema'
|
|
75
|
+
/** A table holding this many rows gets a new index as owed work: on the
|
|
76
|
+
* box's copy an index over 10,000 rows builds in about 10 ms, and over its
|
|
77
|
+
* 4,057,139 `created` rows in 2.6 s. */
|
|
78
|
+
export let BIG = 10_000
|
|
79
|
+
/** The rows one window fills into a text index. Its time goes on the text,
|
|
80
|
+
* not the rows: on the box's copy, whose `entry_fts` bodies run about 5 KB,
|
|
81
|
+
* 1,000 rows took 164 ms a window and up to 515 ms, and its 356,025 rows
|
|
82
|
+
* take 39 s in one `rebuild`. */
|
|
83
|
+
export let FILL = 500
|
|
84
|
+
/** The text one window fills into a text index, past its first row: a
|
|
85
|
+
* substring index (@yaks/fts `contains`) takes about 190 ms a MiB, and 500 of
|
|
86
|
+
* the box's transcript texts held a window for up to 6.8 s. */
|
|
87
|
+
export let FILL_TEXT = 1 << 20
|
|
88
|
+
/** The rows one window copies into a table being rebuilt: 5.6 ms for 20,000
|
|
89
|
+
* of the box's `created`, whose rebuild in one unit holds the lock 10.2 s. */
|
|
90
|
+
export let COPY = 20_000
|
|
91
|
+
/** The rows one window deletes from a rebuilt table's old rows: about 65 ms
|
|
92
|
+
* on the box's copy. */
|
|
93
|
+
export let CLEAR = 10_000
|
|
94
|
+
/** The rows one window counts of a table whose statistics are its row count
|
|
95
|
+
* (./ddl.ts `counted`): one leaf page a row at worst, so 2 MB of the box's
|
|
96
|
+
* 4 KiB pages. On its 10 GB file under a load of 25, a window over a text
|
|
97
|
+
* index's data took 0.1 to 0.9 s, where `ANALYZE` read a whole table in one
|
|
98
|
+
* window and held the write lock 20.4 s (bug bace8585). */
|
|
99
|
+
export let COUNT = 500
|
|
100
|
+
/** The free pages a file keeps for its next writes to reuse; past these,
|
|
101
|
+
* {@link reclaim} gives the rest back. 64 MiB of the box's 4 KiB pages. */
|
|
102
|
+
export let SLACK = 16_384
|
|
103
|
+
/** The pages one window gives back: on a 660 MB file of 4 KiB pages with a
|
|
104
|
+
* third of its rows deleted in runs, 1,000 pages took 28 ms a window, and up
|
|
105
|
+
* to 176 ms with the checkpoint that followed. */
|
|
106
|
+
export let RECLAIM = 1_000
|
|
107
|
+
/** The sizes above as one value, which what chooses by them takes, so a
|
|
108
|
+
* test can shrink them instead of building the rows. */
|
|
109
|
+
export type Sizes = {
|
|
110
|
+
big: number
|
|
111
|
+
fill: number
|
|
112
|
+
fillText: number
|
|
113
|
+
copy: number
|
|
114
|
+
clear: number
|
|
115
|
+
slack: number
|
|
116
|
+
reclaim: number
|
|
117
|
+
count: number
|
|
118
|
+
}
|
|
119
|
+
export let SIZES: Sizes = {
|
|
120
|
+
big: BIG,
|
|
121
|
+
fill: FILL,
|
|
122
|
+
fillText: FILL_TEXT,
|
|
123
|
+
copy: COPY,
|
|
124
|
+
clear: CLEAR,
|
|
125
|
+
slack: SLACK,
|
|
126
|
+
reclaim: RECLAIM,
|
|
127
|
+
count: COUNT,
|
|
128
|
+
}
|
|
129
|
+
/** How long a window that failed waits before it is tried again. */
|
|
130
|
+
export let RETRY = 3_600_000
|
|
131
|
+
/** How often a file's upkeep ({@link Daily}) is done. */
|
|
132
|
+
export let DAY = 86_400_000
|
|
133
|
+
|
|
134
|
+
/** One piece of owed work. `error` and `next` are a failed window's: what it
|
|
135
|
+
* threw, and when it is tried again. */
|
|
136
|
+
export type Owed =
|
|
137
|
+
& { name: string; error?: string; next?: string }
|
|
138
|
+
& (
|
|
139
|
+
/** an index over a table of {@link BIG} rows or more: SQLite builds an
|
|
140
|
+
* index in one statement, so this is the one window that holds the lock
|
|
141
|
+
* for seconds, off the opening path */
|
|
142
|
+
| { kind: 'index'; stmt: CreateIndex }
|
|
143
|
+
/** a text index filled {@link FILL} rows and {@link FILL_TEXT} of text a
|
|
144
|
+
* window from `source`, its
|
|
145
|
+
* triggers keeping the rows `cursor` has passed (@yaks/fts `Filler`) */
|
|
146
|
+
| {
|
|
147
|
+
kind: 'fill'
|
|
148
|
+
source: string
|
|
149
|
+
props: string[]
|
|
150
|
+
fields?: string[]
|
|
151
|
+
triggers: CreateTrigger[]
|
|
152
|
+
cursor: number
|
|
153
|
+
}
|
|
154
|
+
/** a table of {@link BIG} rows or more whose constraints moved, rebuilt
|
|
155
|
+
* beside itself as `table` ({@link building}): {@link COPY} rows a
|
|
156
|
+
* window, triggers keeping the rows `cursor` has passed, then swapped in
|
|
157
|
+
* by name in one unit, which raises `after` (the indexes and triggers
|
|
158
|
+
* that stood on it) on the new one */
|
|
159
|
+
| {
|
|
160
|
+
kind: 'refit'
|
|
161
|
+
table: CreateTable
|
|
162
|
+
cols: string[]
|
|
163
|
+
changed: Refitting['changed']
|
|
164
|
+
after: Stmt[]
|
|
165
|
+
cursor: number
|
|
166
|
+
}
|
|
167
|
+
/** the rows a refit left in its old table, deleted {@link CLEAR} a
|
|
168
|
+
* window, then the table */
|
|
169
|
+
| { kind: 'clear' }
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
let key = (w: Pick<Owed, 'kind' | 'name'>) => `${OWED}${w.kind}:${w.name}`
|
|
173
|
+
|
|
174
|
+
/** Write `work` down, replacing whatever was owed under its name. */
|
|
175
|
+
export let owe = (driver: Driver, work: Owed): void =>
|
|
176
|
+
meta(driver).set(key(work), JSON.stringify(work))
|
|
177
|
+
|
|
178
|
+
// The order owed work is done in: a missing index first, since queries run
|
|
179
|
+
// without it meanwhile, and a rebuilt table's old rows last, since nothing
|
|
180
|
+
// reads them.
|
|
181
|
+
let order: Owed['kind'][] = ['index', 'fill', 'refit', 'clear']
|
|
182
|
+
|
|
183
|
+
/** Everything this file owes, in the order it is worked off, then by name. */
|
|
184
|
+
export let owed = (driver: Driver): Owed[] =>
|
|
185
|
+
catalogue(driver).some((o) => o.name == META)
|
|
186
|
+
? under(driver, OWED).map(([, v]) => JSON.parse(v) as Owed)
|
|
187
|
+
.sort((a, b) => order.indexOf(a.kind) - order.indexOf(b.kind))
|
|
188
|
+
: []
|
|
189
|
+
|
|
190
|
+
/** Whether `table` holds at least `n` rows, counting no further than that. */
|
|
191
|
+
export let holds = (driver: Driver, name: string, n: number): boolean =>
|
|
192
|
+
Number(
|
|
193
|
+
driver.query(select({
|
|
194
|
+
cols: [as(count(), 'n')],
|
|
195
|
+
from: from(
|
|
196
|
+
select({ cols: [lit(1)], from: table(name), limit: lit(n) }),
|
|
197
|
+
'r',
|
|
198
|
+
),
|
|
199
|
+
}))[0]?.n ?? 0,
|
|
200
|
+
) >= n
|
|
201
|
+
|
|
202
|
+
/** Whether the engine refused a unique index because rows already in its
|
|
203
|
+
* table share a key. */
|
|
204
|
+
let collides = (e: unknown): boolean =>
|
|
205
|
+
e instanceof Error && /unique constraint failed/i.test(e.message)
|
|
206
|
+
|
|
207
|
+
/** What is reported of a unique index the rows in its table break: it is not
|
|
208
|
+
* built, and the table serves without it. */
|
|
209
|
+
export let unbuilt = (s: CreateIndex, e: unknown): Error =>
|
|
210
|
+
new Error(
|
|
211
|
+
`${s.name} is not built: rows in ${s.on} share its key and need a ` +
|
|
212
|
+
`migration that prepares them (${e instanceof Error ? e.message : e})`,
|
|
213
|
+
{ cause: e },
|
|
214
|
+
)
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Run `stmts` as `erect` does, owing each index a file lacks over a table of
|
|
218
|
+
* {@link BIG} rows or more instead of building it here. A new unique index the
|
|
219
|
+
* rows already in its table break is left unbuilt and returned, so the file
|
|
220
|
+
* opens with the shape it holds and the caller reports it; a failed statement
|
|
221
|
+
* undoes only itself, so the rest stands.
|
|
222
|
+
*/
|
|
223
|
+
export let build = (
|
|
224
|
+
driver: Driver,
|
|
225
|
+
stmts: readonly Stmt[],
|
|
226
|
+
sizes: Sizes = SIZES,
|
|
227
|
+
deferred = false,
|
|
228
|
+
): Error[] => {
|
|
229
|
+
let names = new Set(catalogue(driver).map((o) => o.name))
|
|
230
|
+
let owing = (!!driver.file || deferred) && names.has(META)
|
|
231
|
+
let now = stmts.filter((s) => {
|
|
232
|
+
if (!owing || s.t != 'create index' || names.has(s.name)) return true
|
|
233
|
+
if (!names.has(s.on) || !holds(driver, s.on, deferred ? 1 : sizes.big)) {
|
|
234
|
+
return true
|
|
235
|
+
}
|
|
236
|
+
owe(driver, { kind: 'index', name: s.name, stmt: s })
|
|
237
|
+
return false
|
|
238
|
+
})
|
|
239
|
+
let unique = (s: Stmt): s is CreateIndex =>
|
|
240
|
+
s.t == 'create index' && !!s.unique && !names.has(s.name)
|
|
241
|
+
erect(driver, now.filter((s) => !unique(s)))
|
|
242
|
+
return now.filter(unique).flatMap((s) => {
|
|
243
|
+
try {
|
|
244
|
+
driver.query(s)
|
|
245
|
+
return []
|
|
246
|
+
} catch (e) {
|
|
247
|
+
if (!collides(e)) throw e
|
|
248
|
+
return [unbuilt(s, e)]
|
|
249
|
+
}
|
|
250
|
+
})
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** What one call to {@link work} did: the work it finished or advanced, or
|
|
254
|
+
* the window that failed. */
|
|
255
|
+
export type Worked =
|
|
256
|
+
| { name: string; done: boolean }
|
|
257
|
+
| { name: string; error: unknown }
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Upkeep whoever tends a file does once a day, after what it owes: planner
|
|
261
|
+
* statistics ({@link statistics}) always, and what a host adds, the way the
|
|
262
|
+
* box checks that its text indexes hold every row (@yaks/fts `heal`). `run`
|
|
263
|
+
* is one window, in one unit. When each is next due is kept in `server_meta`
|
|
264
|
+
* as `daily:<name>`, with its `revision` after a `#`. One whose run changed
|
|
265
|
+
* what it decides raises its revision, so every store runs it at its next
|
|
266
|
+
* wake rather than up to a day after the deploy.
|
|
267
|
+
*/
|
|
268
|
+
export type Daily = {
|
|
269
|
+
name: string
|
|
270
|
+
run: (driver: Driver) => void
|
|
271
|
+
revision?: number
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// A daily's mark: when it is next due, and the revision that set it.
|
|
275
|
+
let marked = (d: Daily, ms: number) =>
|
|
276
|
+
new Date(ms).toISOString() + (d.revision ? `#${d.revision}` : '')
|
|
277
|
+
let due = (d: Daily, at: string | null | undefined, now: number) => {
|
|
278
|
+
if (!at) return true
|
|
279
|
+
let [when, revision] = at.split('#')
|
|
280
|
+
return (revision ?? '') != String(d.revision ?? '') || Date.parse(when) <= now
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
let DAILY = 'daily:'
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Work a host's rows owe rather than its schema, found in them each time it
|
|
287
|
+
* is looked for: the box makes the pieces of its long texts' substring index
|
|
288
|
+
* this way (@yaks/fts `pieces`). `run` is one window, in one unit, and
|
|
289
|
+
* answers whether it found any to do. One that throws is put off an hour, as
|
|
290
|
+
* `chore:<name>` in `server_meta`.
|
|
291
|
+
*/
|
|
292
|
+
export type Chore = { name: string; run: (driver: Driver) => boolean }
|
|
293
|
+
|
|
294
|
+
let CHORE = 'chore:'
|
|
295
|
+
|
|
296
|
+
// One window of the first of `chores` that is not put off and finds work to
|
|
297
|
+
// do; one that throws is put off an hour.
|
|
298
|
+
let chore = (
|
|
299
|
+
driver: Driver,
|
|
300
|
+
chores: Chore[],
|
|
301
|
+
now: number,
|
|
302
|
+
): Worked | undefined => {
|
|
303
|
+
let m = meta(driver)
|
|
304
|
+
for (let c of chores) {
|
|
305
|
+
let name = CHORE + c.name
|
|
306
|
+
let at = m.get(name)
|
|
307
|
+
if (at && Date.parse(at) > now) continue
|
|
308
|
+
try {
|
|
309
|
+
let did = unit(driver, () => {
|
|
310
|
+
if (at) m.del(name)
|
|
311
|
+
return c.run(driver)
|
|
312
|
+
})
|
|
313
|
+
if (did) return { name, done: false }
|
|
314
|
+
} catch (error) {
|
|
315
|
+
unit(driver, () => m.set(name, new Date(now + RETRY).toISOString()))
|
|
316
|
+
return { name, error }
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/** Planner statistics (./ddl.ts `analyzed`): due once a day, and as soon as
|
|
322
|
+
* an index or a rebuilt table is in place, when the tables that lack them, or
|
|
323
|
+
* all that hold rows once the file has grown tenfold, are owed an analysis,
|
|
324
|
+
* which {@link work} does a table a window. */
|
|
325
|
+
export let statistics: Daily = {
|
|
326
|
+
name: 'statistics',
|
|
327
|
+
// 2: tables whose statistics say they hold a few rows (./ddl.ts `small`).
|
|
328
|
+
revision: 2,
|
|
329
|
+
run: (driver) => {
|
|
330
|
+
let receipt = installation(driver)
|
|
331
|
+
let owed = analyzed(driver, receipt?.statistics)
|
|
332
|
+
if (owed && receipt) analyzedAt(driver, receipt.schema, owed)
|
|
333
|
+
},
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/** The `server_meta` key of {@link analyze}: when it is next tried, after a
|
|
337
|
+
* window that failed. */
|
|
338
|
+
export let ANALYZING = 'analyze'
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Take one window of the statistics of the first table the receipt owes them,
|
|
342
|
+
* and answer how many stay owed; undefined when none is. A table that is
|
|
343
|
+
* counted (./ddl.ts `counted`) has `count` rows read in a snapshot, holding
|
|
344
|
+
* no lock, and the receipt written after in a unit of its own; any other is
|
|
345
|
+
* analyzed in its own unit.
|
|
346
|
+
*/
|
|
347
|
+
export let analyze = (
|
|
348
|
+
driver: Driver,
|
|
349
|
+
count: number = COUNT,
|
|
350
|
+
): number | undefined => {
|
|
351
|
+
let window = unit(driver, () => {
|
|
352
|
+
let statistics = installation(driver)?.statistics
|
|
353
|
+
let first = statistics?.owed?.[0]
|
|
354
|
+
if (first == null || !counted(driver, first)) return
|
|
355
|
+
return tally(driver, statistics!, count)
|
|
356
|
+
}, 'read')
|
|
357
|
+
return unit(driver, () => {
|
|
358
|
+
let receipt = installation(driver)
|
|
359
|
+
let statistics = receipt?.statistics
|
|
360
|
+
if (!receipt || !statistics?.owed?.length) return
|
|
361
|
+
let next = !counted(driver, statistics.owed[0])
|
|
362
|
+
? analyzing(driver, statistics, 1)
|
|
363
|
+
: window
|
|
364
|
+
? tallied(driver, statistics, window)
|
|
365
|
+
: statistics
|
|
366
|
+
if (next != statistics) analyzedAt(driver, receipt.schema, next)
|
|
367
|
+
return next.owed?.length ?? 0
|
|
368
|
+
})
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// One window of the planner statistics the receipt owes; one that throws is
|
|
372
|
+
// put off an hour.
|
|
373
|
+
let analysis = (
|
|
374
|
+
driver: Driver,
|
|
375
|
+
now: number,
|
|
376
|
+
sizes: Sizes,
|
|
377
|
+
): Worked | undefined => {
|
|
378
|
+
if (!installation(driver)?.statistics?.owed?.length) return
|
|
379
|
+
let at = meta(driver).get(ANALYZING)
|
|
380
|
+
if (at && Date.parse(at) > now) return
|
|
381
|
+
try {
|
|
382
|
+
let left = analyze(driver, sizes.count)
|
|
383
|
+
return left == null ? undefined : { name: ANALYZING, done: !left }
|
|
384
|
+
} catch (error) {
|
|
385
|
+
unit(
|
|
386
|
+
driver,
|
|
387
|
+
() => meta(driver).set(ANALYZING, new Date(now + RETRY).toISOString()),
|
|
388
|
+
)
|
|
389
|
+
return { name: ANALYZING, error }
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* Do one window, in one unit: of the first owed work that is due, else of
|
|
395
|
+
* the first of `chores` with work to do, else of the first table owed an
|
|
396
|
+
* analysis ({@link analyze}), else of giving free pages back
|
|
397
|
+
* ({@link reclaim}), else of the first of `daily` that is due. Undefined
|
|
398
|
+
* when nothing is. A window that throws leaves its work as it was, marked to
|
|
399
|
+
* be tried again in an hour, and says why.
|
|
400
|
+
*/
|
|
401
|
+
export let work = (
|
|
402
|
+
driver: Driver,
|
|
403
|
+
now: number = Date.now(),
|
|
404
|
+
daily: Daily[] = [],
|
|
405
|
+
sizes: Sizes = SIZES,
|
|
406
|
+
chores: Chore[] = [],
|
|
407
|
+
): Worked | undefined => {
|
|
408
|
+
let due = owed(driver).find((w) => !w.next || Date.parse(w.next) <= now)
|
|
409
|
+
if (!due) {
|
|
410
|
+
return chore(driver, chores, now) ?? analysis(driver, now, sizes) ??
|
|
411
|
+
reclaim(driver, now, sizes) ?? upkeep(driver, daily, now)
|
|
412
|
+
}
|
|
413
|
+
try {
|
|
414
|
+
return unit(
|
|
415
|
+
driver,
|
|
416
|
+
() => ({ name: key(due), done: step(driver, due, sizes) }),
|
|
417
|
+
)
|
|
418
|
+
} catch (error) {
|
|
419
|
+
unit(driver, () =>
|
|
420
|
+
owe(driver, {
|
|
421
|
+
...due,
|
|
422
|
+
error: error instanceof Error ? error.message : String(error),
|
|
423
|
+
next: new Date(now + RETRY).toISOString(),
|
|
424
|
+
}))
|
|
425
|
+
return { name: key(due), error }
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
let pragma = (driver: Driver, name: string) =>
|
|
430
|
+
Number(Object.values(driver.query({ t: 'pragma', name })[0] ?? {})[0] ?? 0)
|
|
431
|
+
|
|
432
|
+
/** The `server_meta` key of {@link reclaim}: when it is next tried, after a
|
|
433
|
+
* window that failed. */
|
|
434
|
+
export let RECLAIMING = 'reclaim'
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Give back the free pages past {@link SLACK}, at most {@link RECLAIM} a
|
|
438
|
+
* window: SQLite moves pages from the end of the file into the free ones and
|
|
439
|
+
* cuts the end off, which the next checkpoint writes to the file. Only a file
|
|
440
|
+
* in incremental mode can (./db.ts `open`); one in another mode, or in
|
|
441
|
+
* memory, has nothing to give. Undefined when nothing is past the slack; one
|
|
442
|
+
* that throws is put off an hour.
|
|
443
|
+
*/
|
|
444
|
+
export let reclaim = (
|
|
445
|
+
driver: Driver,
|
|
446
|
+
now: number = Date.now(),
|
|
447
|
+
sizes: Sizes = SIZES,
|
|
448
|
+
): Worked | undefined => {
|
|
449
|
+
if (!driver.file || pragma(driver, 'auto_vacuum') != 2) return
|
|
450
|
+
let past = pragma(driver, 'freelist_count') - sizes.slack
|
|
451
|
+
if (past <= 0) return
|
|
452
|
+
let m = meta(driver)
|
|
453
|
+
let at = m.get(RECLAIMING)
|
|
454
|
+
if (at && Date.parse(at) > now) return
|
|
455
|
+
let n = Math.min(past, sizes.reclaim)
|
|
456
|
+
try {
|
|
457
|
+
unit(
|
|
458
|
+
driver,
|
|
459
|
+
() => driver.query({ t: 'pragma', name: 'incremental_vacuum', arg: n }),
|
|
460
|
+
)
|
|
461
|
+
return { name: RECLAIMING, done: n == past }
|
|
462
|
+
} catch (error) {
|
|
463
|
+
unit(driver, () => m.set(RECLAIMING, new Date(now + RETRY).toISOString()))
|
|
464
|
+
return { name: RECLAIMING, error }
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/** Do the first of `daily` that is due, and put it off a day; one that throws
|
|
469
|
+
* is put off an hour. Undefined when none is due. */
|
|
470
|
+
export let upkeep = (
|
|
471
|
+
driver: Driver,
|
|
472
|
+
daily: Daily[],
|
|
473
|
+
now: number,
|
|
474
|
+
): Worked | undefined => {
|
|
475
|
+
let m = meta(driver)
|
|
476
|
+
let next = daily.find((d) => due(d, m.get(DAILY + d.name), now))
|
|
477
|
+
if (!next) return
|
|
478
|
+
let name = DAILY + next.name
|
|
479
|
+
let later = (ms: number) => m.set(name, marked(next, now + ms))
|
|
480
|
+
try {
|
|
481
|
+
unit(driver, () => {
|
|
482
|
+
next.run(driver)
|
|
483
|
+
later(DAY)
|
|
484
|
+
})
|
|
485
|
+
return { name, done: true }
|
|
486
|
+
} catch (error) {
|
|
487
|
+
unit(driver, () => later(RETRY))
|
|
488
|
+
return { name, error }
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
// The highest id among the next `n` rows of `source` past `cursor`, or null
|
|
493
|
+
// when there are none: where the window that takes them ends. A window's
|
|
494
|
+
// bounds are bound, never written into its text, so every window of one piece
|
|
495
|
+
// of work runs one prepared statement, where a text of its own would be
|
|
496
|
+
// compiled once and kept, pushing what a store repeats out of the driver's
|
|
497
|
+
// bounded cache.
|
|
498
|
+
let next = (driver: Driver, source: string, cursor: number, n: number) => {
|
|
499
|
+
let [{ hi }] = driver.query(select({
|
|
500
|
+
cols: [as(fn('max', col('entity')), 'hi')],
|
|
501
|
+
from: from(
|
|
502
|
+
select({
|
|
503
|
+
cols: [col('entity')],
|
|
504
|
+
from: table(source),
|
|
505
|
+
where: gt(col('entity'), val(cursor)),
|
|
506
|
+
order: [col('entity')],
|
|
507
|
+
limit: val(n),
|
|
508
|
+
}),
|
|
509
|
+
'w',
|
|
510
|
+
),
|
|
511
|
+
}))
|
|
512
|
+
return hi == null ? null : Number(hi)
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
// The highest id among the next rows of a text index's source past `cursor`,
|
|
516
|
+
// at most `n` of them and as many as the text before each leaves under
|
|
517
|
+
// `text`, or null when there are none.
|
|
518
|
+
let reach = (
|
|
519
|
+
driver: Driver,
|
|
520
|
+
w: { source: string; props: string[]; fields?: string[]; cursor: number },
|
|
521
|
+
n: number,
|
|
522
|
+
text: number,
|
|
523
|
+
) => {
|
|
524
|
+
let size = (w.fields ?? w.props).map((p) =>
|
|
525
|
+
fn('coalesce', fn('length', col(p)), lit(0))
|
|
526
|
+
)
|
|
527
|
+
.reduce((a, b) => op('+', a, b))
|
|
528
|
+
let rows = select({
|
|
529
|
+
cols: [col('entity'), as(size, 'size')],
|
|
530
|
+
from: table(w.source),
|
|
531
|
+
where: gt(col('entity'), val(w.cursor)),
|
|
532
|
+
order: [col('entity')],
|
|
533
|
+
limit: val(n),
|
|
534
|
+
})
|
|
535
|
+
let before = op(
|
|
536
|
+
'-',
|
|
537
|
+
over(fn('sum', col('size')), undefined, [col('entity')]),
|
|
538
|
+
col('size'),
|
|
539
|
+
)
|
|
540
|
+
let [{ hi }] = driver.query(select({
|
|
541
|
+
cols: [as(fn('max', col('entity')), 'hi')],
|
|
542
|
+
from: from(
|
|
543
|
+
select({
|
|
544
|
+
cols: [col('entity'), as(before, 'before')],
|
|
545
|
+
from: from(rows, 'w'),
|
|
546
|
+
}),
|
|
547
|
+
'r',
|
|
548
|
+
),
|
|
549
|
+
where: lt(col('before'), val(text)),
|
|
550
|
+
}))
|
|
551
|
+
return hi == null ? null : Number(hi)
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
// The rows of `source` a window takes: past `cursor`, up to `hi`.
|
|
555
|
+
let window = (cursor: number, hi: number) =>
|
|
556
|
+
and(gt(col('entity'), val(cursor)), le(col('entity'), val(hi)))
|
|
557
|
+
|
|
558
|
+
// What the triggers of work being done behind a cursor read it through: the
|
|
559
|
+
// owed row itself, so there is one copy of how far it has got. A row that is
|
|
560
|
+
// gone reads as every row passed.
|
|
561
|
+
let cursorOf = (w: Pick<Owed, 'kind' | 'name'>): Expr =>
|
|
562
|
+
fn(
|
|
563
|
+
'coalesce',
|
|
564
|
+
sub(select({
|
|
565
|
+
cols: [fn('json_extract', col('v'), lit('$.cursor'))],
|
|
566
|
+
from: table(META),
|
|
567
|
+
where: eq(col('k'), lit(key(w))),
|
|
568
|
+
})),
|
|
569
|
+
lit(Number.MAX_SAFE_INTEGER),
|
|
570
|
+
)
|
|
571
|
+
|
|
572
|
+
/** The name a table is rebuilt under, and the name its old rows wait under
|
|
573
|
+
* to be cleared. Neither is a component table (./physical.ts). */
|
|
574
|
+
export let building = (comp: string): string => BUILDING + comp
|
|
575
|
+
export let leaving = (comp: string): string => `_yaks_old_${comp}`
|
|
576
|
+
|
|
577
|
+
// The triggers keeping a table being rebuilt in step with the rows its cursor
|
|
578
|
+
// has passed: a write behind the cursor is written to it too, and one ahead is
|
|
579
|
+
// read when its window comes.
|
|
580
|
+
let steps = (comp: string, w: Extract<Owed, { kind: 'refit' }>) => {
|
|
581
|
+
let into = w.table.name
|
|
582
|
+
let r = { changed: w.changed } as Refitting
|
|
583
|
+
let passed = (row: string) => le(col('entity', row), cursorOf(w))
|
|
584
|
+
let put: Insert = {
|
|
585
|
+
t: 'insert',
|
|
586
|
+
or: 'replace',
|
|
587
|
+
into,
|
|
588
|
+
cols: w.cols,
|
|
589
|
+
rows: [w.cols.map((c) => carried(r, c, 'new'))],
|
|
590
|
+
}
|
|
591
|
+
let trigger = (
|
|
592
|
+
event: CreateTrigger['event'],
|
|
593
|
+
body: CreateTrigger['body'],
|
|
594
|
+
): CreateTrigger => ({
|
|
595
|
+
t: 'create trigger',
|
|
596
|
+
name: `${into}_${event}`,
|
|
597
|
+
ifNot: true,
|
|
598
|
+
timing: 'after',
|
|
599
|
+
event,
|
|
600
|
+
on: comp,
|
|
601
|
+
when: passed(event == 'delete' ? 'old' : 'new'),
|
|
602
|
+
body,
|
|
603
|
+
})
|
|
604
|
+
return [
|
|
605
|
+
trigger('insert', [put]),
|
|
606
|
+
trigger('update', [put]),
|
|
607
|
+
trigger('delete', [{
|
|
608
|
+
t: 'delete',
|
|
609
|
+
from: into,
|
|
610
|
+
where: eq(col('entity'), col('entity', 'old')),
|
|
611
|
+
}]),
|
|
612
|
+
]
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
// Give up rebuilding a table: the triggers keeping the new one in step, and
|
|
616
|
+
// the new one. A table its vocabulary dropped has taken the triggers already.
|
|
617
|
+
let quit = (driver: Driver, w: Extract<Owed, { kind: 'refit' }>) => {
|
|
618
|
+
for (let t of steps(w.name, w)) {
|
|
619
|
+
driver.query({ t: 'drop', kind: 'trigger', name: t.name, ifExists: true })
|
|
620
|
+
}
|
|
621
|
+
driver.query({ t: 'drop', kind: 'table', name: w.table.name, ifExists: true })
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
// Each kind's window, inside its unit; true when the work is finished, and
|
|
625
|
+
// its row then gone.
|
|
626
|
+
let windows: {
|
|
627
|
+
[K in Owed['kind']]: (
|
|
628
|
+
driver: Driver,
|
|
629
|
+
w: Extract<Owed, { kind: K }>,
|
|
630
|
+
here: Map<string, SchemaObject>,
|
|
631
|
+
sizes: Sizes,
|
|
632
|
+
) => boolean
|
|
633
|
+
} = {
|
|
634
|
+
index: (driver, w, here) => {
|
|
635
|
+
// A table a later vocabulary dropped takes its owed index with it, and
|
|
636
|
+
// one owed before the install stopped building it is forgotten.
|
|
637
|
+
if (here.has(w.stmt.on) && !needless(driver, w.stmt)) {
|
|
638
|
+
driver.query({ ...w.stmt, ifNot: true })
|
|
639
|
+
}
|
|
640
|
+
return true
|
|
641
|
+
},
|
|
642
|
+
fill: (driver, w, here, { fill, fillText }) => {
|
|
643
|
+
// An index a later pass rebuilt whole, or dropped, is no longer this
|
|
644
|
+
// work's.
|
|
645
|
+
let guarded = here.get(w.triggers[0]?.name)?.sql?.includes(key(w))
|
|
646
|
+
if (!here.has(w.name) || !guarded) return true
|
|
647
|
+
let hi = reach(driver, w, fill, fillText)
|
|
648
|
+
// Filled: the triggers keeping it behind the cursor give way to the ones
|
|
649
|
+
// it keeps for good, each under the name it just gave up.
|
|
650
|
+
if (hi == null) {
|
|
651
|
+
for (let t of w.triggers) {
|
|
652
|
+
driver.query({
|
|
653
|
+
t: 'drop',
|
|
654
|
+
kind: 'trigger',
|
|
655
|
+
name: t.name,
|
|
656
|
+
ifExists: true,
|
|
657
|
+
})
|
|
658
|
+
driver.query(t)
|
|
659
|
+
}
|
|
660
|
+
return true
|
|
661
|
+
}
|
|
662
|
+
driver.query({
|
|
663
|
+
t: 'insert',
|
|
664
|
+
into: w.name,
|
|
665
|
+
cols: ['rowid', ...w.props],
|
|
666
|
+
q: select({
|
|
667
|
+
cols: [
|
|
668
|
+
col('entity'),
|
|
669
|
+
...(w.fields ?? w.props).map((p) => fn('coalesce', col(p), lit(''))),
|
|
670
|
+
],
|
|
671
|
+
from: table(w.source),
|
|
672
|
+
where: window(w.cursor, hi),
|
|
673
|
+
}),
|
|
674
|
+
})
|
|
675
|
+
owe(driver, { ...w, cursor: hi })
|
|
676
|
+
return false
|
|
677
|
+
},
|
|
678
|
+
refit: (driver, w, here, sizes) => {
|
|
679
|
+
let comp = w.name, into = w.table.name
|
|
680
|
+
let v = driver.stored?.v
|
|
681
|
+
let old = leaving(v ? stored(v, comp) : comp)
|
|
682
|
+
if (!here.has(comp) || !here.has(into)) return quit(driver, w), true
|
|
683
|
+
let hi = next(driver, comp, w.cursor, sizes.copy)
|
|
684
|
+
if (hi != null) {
|
|
685
|
+
let r = { changed: w.changed } as Refitting
|
|
686
|
+
driver.query({
|
|
687
|
+
t: 'insert',
|
|
688
|
+
into,
|
|
689
|
+
cols: w.cols,
|
|
690
|
+
q: select({
|
|
691
|
+
cols: w.cols.map((c) => carried(r, c, comp)),
|
|
692
|
+
from: table(comp),
|
|
693
|
+
where: window(w.cursor, hi),
|
|
694
|
+
}),
|
|
695
|
+
})
|
|
696
|
+
owe(driver, { ...w, cursor: hi })
|
|
697
|
+
return false
|
|
698
|
+
}
|
|
699
|
+
// Every row is across: the swap, in this one unit.
|
|
700
|
+
if (here.has(old)) throw new Error(`${comp} waits for ${old} to clear`)
|
|
701
|
+
let filling = owed(driver).find((o) =>
|
|
702
|
+
o.kind == 'fill' && o.triggers.some((t) => t.on == comp)
|
|
703
|
+
)
|
|
704
|
+
if (filling) throw new Error(`${comp} waits for ${filling.name} to fill`)
|
|
705
|
+
for (let o of here.values()) {
|
|
706
|
+
if (o.tbl_name != comp || o.sql == null) continue
|
|
707
|
+
if (o.type != 'trigger' && o.type != 'index') continue
|
|
708
|
+
driver.query({ t: 'drop', kind: o.type, name: o.name })
|
|
709
|
+
}
|
|
710
|
+
// Renamed without rewriting the views and triggers that name the table,
|
|
711
|
+
// which name the new one once it holds the name.
|
|
712
|
+
let legacy = (value: string) =>
|
|
713
|
+
driver.query({ t: 'pragma', name: 'legacy_alter_table', value })
|
|
714
|
+
legacy('on')
|
|
715
|
+
try {
|
|
716
|
+
driver.query({ t: 'alter table', table: comp, rename: old })
|
|
717
|
+
driver.query({ t: 'alter table', table: into, rename: comp })
|
|
718
|
+
} finally {
|
|
719
|
+
legacy('off')
|
|
720
|
+
}
|
|
721
|
+
// An index over the rows is built in a window of its own.
|
|
722
|
+
build(driver, w.after, sizes)
|
|
723
|
+
owe(driver, { kind: 'clear', name: old })
|
|
724
|
+
return true
|
|
725
|
+
},
|
|
726
|
+
clear: (driver, w, here, { clear }) => {
|
|
727
|
+
if (!here.has(w.name)) return true
|
|
728
|
+
let hi = next(driver, w.name, Number.MIN_SAFE_INTEGER, clear)
|
|
729
|
+
if (hi == null) {
|
|
730
|
+
driver.query({ t: 'drop', kind: 'table', name: w.name })
|
|
731
|
+
return true
|
|
732
|
+
}
|
|
733
|
+
driver.query({
|
|
734
|
+
t: 'delete',
|
|
735
|
+
from: w.name,
|
|
736
|
+
where: le(col('entity'), val(hi)),
|
|
737
|
+
})
|
|
738
|
+
return false
|
|
739
|
+
},
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
// One window of `w`, inside its unit; true when the work is finished, and its
|
|
743
|
+
// row then gone. An index or a rebuilt table in place makes the statistics
|
|
744
|
+
// due, since the planner has none for it yet.
|
|
745
|
+
let step = (driver: Driver, w: Owed, sizes: Sizes): boolean => {
|
|
746
|
+
let here = new Map(catalogue(driver).map((o) => [o.name, o]))
|
|
747
|
+
let done = (windows[w.kind] as (
|
|
748
|
+
d: Driver,
|
|
749
|
+
w: Owed,
|
|
750
|
+
h: Map<string, SchemaObject>,
|
|
751
|
+
s: Sizes,
|
|
752
|
+
) => boolean)(driver, w, here, sizes)
|
|
753
|
+
if (done) meta(driver).del(key(w))
|
|
754
|
+
if (done && (w.kind == 'index' || w.kind == 'refit')) {
|
|
755
|
+
meta(driver).del(DAILY + statistics.name)
|
|
756
|
+
}
|
|
757
|
+
return done
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
/**
|
|
761
|
+
* The {@link Filler} @yaks/fts `adopt` hands a new or changed text index to:
|
|
762
|
+
* on a file, an index whose content holds more than one window's rows is owed
|
|
763
|
+
* and filled {@link FILL} rows at a time; a smaller one is rebuilt at once.
|
|
764
|
+
*/
|
|
765
|
+
export let filler = (driver: Driver, sizes: Sizes = SIZES): Filler => ({
|
|
766
|
+
pending: (fts) =>
|
|
767
|
+
owed(driver).some((w) => w.kind == 'fill' && w.name == fts)
|
|
768
|
+
? cursorOf({ kind: 'fill', name: fts })
|
|
769
|
+
: undefined,
|
|
770
|
+
owe: ({ fts, ...fill }) => {
|
|
771
|
+
let here = catalogue(driver).some((o) => o.name == META)
|
|
772
|
+
if (!driver.file || !here || !holds(driver, fill.source, sizes.fill + 1)) {
|
|
773
|
+
return
|
|
774
|
+
}
|
|
775
|
+
owe(driver, { kind: 'fill', name: fts, ...fill, cursor: 0 })
|
|
776
|
+
return cursorOf({ kind: 'fill', name: fts })
|
|
777
|
+
},
|
|
778
|
+
})
|
|
779
|
+
|
|
780
|
+
/**
|
|
781
|
+
* Take on rebuilding `comp` as `r` says, on a file where it holds {@link BIG}
|
|
782
|
+
* rows or more: the table it is built as and the triggers keeping it in step,
|
|
783
|
+
* made now, and the copying owed. `after` is what to raise on the new table
|
|
784
|
+
* once it holds the name: the indexes and triggers the composed schema puts
|
|
785
|
+
* on it. A rebuild already under way to the same shape carries on, with this
|
|
786
|
+
* `after`; one to another shape is abandoned first. False where the table is
|
|
787
|
+
* small enough to rebuild at once.
|
|
788
|
+
*/
|
|
789
|
+
export let reshape = (
|
|
790
|
+
driver: Driver,
|
|
791
|
+
comp: string,
|
|
792
|
+
r: Refitting,
|
|
793
|
+
after: Stmt[],
|
|
794
|
+
sizes: Sizes = SIZES,
|
|
795
|
+
): boolean => {
|
|
796
|
+
let names = new Set(catalogue(driver).map((o) => o.name))
|
|
797
|
+
if (!driver.file || !names.has(META) || !holds(driver, comp, sizes.big)) {
|
|
798
|
+
return false
|
|
799
|
+
}
|
|
800
|
+
let table = { ...r.fresh, name: building(comp), ifNot: true }
|
|
801
|
+
let was = owed(driver).find((w) => w.kind == 'refit' && w.name == comp)
|
|
802
|
+
if (
|
|
803
|
+
was?.kind == 'refit' && JSON.stringify(was.table) == JSON.stringify(table)
|
|
804
|
+
) {
|
|
805
|
+
owe(driver, { ...was, after })
|
|
806
|
+
return true
|
|
807
|
+
}
|
|
808
|
+
let w: Owed = {
|
|
809
|
+
kind: 'refit',
|
|
810
|
+
name: comp,
|
|
811
|
+
table,
|
|
812
|
+
cols: r.cols,
|
|
813
|
+
changed: r.changed,
|
|
814
|
+
after,
|
|
815
|
+
cursor: 0,
|
|
816
|
+
}
|
|
817
|
+
quit(driver, w)
|
|
818
|
+
driver.query(table)
|
|
819
|
+
owe(driver, w)
|
|
820
|
+
for (let t of steps(comp, w)) driver.query(t)
|
|
821
|
+
return true
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
/** Give up every table rebuild owed but those of `kept`: a pass that took on
|
|
825
|
+
* no rebuild of a table found it fits its vocabulary again, or cannot be
|
|
826
|
+
* rebuilt (./ddl.ts `fit`). */
|
|
827
|
+
export let forgo = (driver: Driver, kept: Set<string>): void => {
|
|
828
|
+
for (let w of owed(driver)) {
|
|
829
|
+
if (w.kind != 'refit' || kept.has(w.name)) continue
|
|
830
|
+
quit(driver, w)
|
|
831
|
+
meta(driver).del(key(w))
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
let sleep = (ms: number, signal: AbortSignal) =>
|
|
836
|
+
new Promise<void>((done) => {
|
|
837
|
+
if (signal.aborted) return done()
|
|
838
|
+
let at = setTimeout(stop, ms)
|
|
839
|
+
function stop() {
|
|
840
|
+
clearTimeout(at)
|
|
841
|
+
signal.removeEventListener('abort', stop)
|
|
842
|
+
done()
|
|
843
|
+
}
|
|
844
|
+
signal.addEventListener('abort', stop)
|
|
845
|
+
})
|
|
846
|
+
|
|
847
|
+
/**
|
|
848
|
+
* Work off what this file owes, and do its upkeep, until `signal` aborts: a
|
|
849
|
+
* window, a pause as long as the window held the lock so other writers get
|
|
850
|
+
* their turn, and a look every `idle` milliseconds while nothing is due.
|
|
851
|
+
* `daily` is the host's upkeep beside the statistics, and `chores` the work
|
|
852
|
+
* its rows owe. A failed window is `report`ed and waits its hour.
|
|
853
|
+
*/
|
|
854
|
+
export let tend = async (
|
|
855
|
+
driver: Driver,
|
|
856
|
+
opts: {
|
|
857
|
+
signal: AbortSignal
|
|
858
|
+
report?: (error: unknown, work: string) => void
|
|
859
|
+
idle?: number
|
|
860
|
+
daily?: Daily[]
|
|
861
|
+
chores?: Chore[]
|
|
862
|
+
},
|
|
863
|
+
): Promise<void> => {
|
|
864
|
+
let idle = opts.idle ?? 60_000
|
|
865
|
+
let daily = [statistics, ...opts.daily ?? []]
|
|
866
|
+
while (!opts.signal.aborted) {
|
|
867
|
+
let at = performance.now()
|
|
868
|
+
let did = work(driver, Date.now(), daily, SIZES, opts.chores)
|
|
869
|
+
if (did && 'error' in did) opts.report?.(did.error, did.name)
|
|
870
|
+
let held = performance.now() - at
|
|
871
|
+
await sleep(did ? Math.max(10, held) : idle, opts.signal)
|
|
872
|
+
}
|
|
873
|
+
}
|