@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/db.js
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import * as io from "@yaks/file/io";
|
|
2
|
+
// The embedded driver itself — @db/sqlite's (./ffi), opened against the
|
|
3
|
+
// library ./sqlitepath.ts names. This module is its one door, and it hands out
|
|
4
|
+
// none of it: a caller gets `open(path)`, a {@link Driver} over the database
|
|
5
|
+
// there (./native.ts), and never the driver's own objects, so a different
|
|
6
|
+
// driver is a change to this package alone.
|
|
7
|
+
//
|
|
8
|
+
// ./mod.ts stays free of it on purpose: the adapter there works against any
|
|
9
|
+
// `Driver`, and only an application that wants an in-process database needs
|
|
10
|
+
// this.
|
|
11
|
+
import { Database } from "./legacy.js";
|
|
12
|
+
import { render } from "@yaks/sql";
|
|
13
|
+
import { chunk, configure, driver, walHook } from "./native.js";
|
|
14
|
+
import { checkpoint } from "./checkpoint.js";
|
|
15
|
+
import { loadable, open as reader } from "./read-db.js";
|
|
16
|
+
// The WAL a file's connection keeps, and lets grow before a checkpoint, in
|
|
17
|
+
// bytes: 16384 pages of 4 KiB.
|
|
18
|
+
let WAL = 64 * 1024 * 1024;
|
|
19
|
+
// The most a file's connection keeps of its pages in memory, in bytes.
|
|
20
|
+
let CACHE = 64 * 1024 * 1024;
|
|
21
|
+
/**
|
|
22
|
+
* Open (or create) the database at `path` — a file, whose directory is made
|
|
23
|
+
* when missing, or `:memory:` — as a {@link Driver} to bind `storage()` to.
|
|
24
|
+
* This is the one place a connection's settings are made. `readOnly: true`
|
|
25
|
+
* requires an existing file and changes no journal settings. Extensions can
|
|
26
|
+
* read installed indexes; the read-only handle refuses metadata writes. A
|
|
27
|
+
* library that cannot load them (macOS's own) opens with no `extension`.
|
|
28
|
+
*
|
|
29
|
+
* Every connection runs with foreign keys on. A file is one other processes
|
|
30
|
+
* may have open too, so it also runs in WAL mode, with WAL's crash-safe pairing
|
|
31
|
+
* `synchronous = normal`, a 64 MiB WAL checkpointed on a thread of its own
|
|
32
|
+
* once it fills and cut back as it starts over (`wal` sets the size, for a test; `wal: false` keeps a
|
|
33
|
+
* rollback journal instead, for one file to copy elsewhere), a 64 MiB page cache, no
|
|
34
|
+
* checkpoint on close or commit, and a one-minute busy timeout, and a new file starts in incremental
|
|
35
|
+
* auto-vacuum mode, so the pages a delete frees can be given back (./owe.ts `reclaim`); a database in memory
|
|
36
|
+
* belongs to this process alone and needs none of that. A graph batch may be large, so contention gives
|
|
37
|
+
* the one writer that bounded minute to finish. A writer held longer is a
|
|
38
|
+
* performance failure, not a reason for every other process to wait forever.
|
|
39
|
+
*
|
|
40
|
+
* ```ts
|
|
41
|
+
* import { open } from '@yaks/sqlite/db'
|
|
42
|
+
*
|
|
43
|
+
* let sql = open(':memory:')
|
|
44
|
+
* sql.query({ t: 'create table', name: 't', cols: [{ name: 'x' }] })
|
|
45
|
+
* sql.close()
|
|
46
|
+
* ```
|
|
47
|
+
*/ export let open = (path, opts = {})=>{
|
|
48
|
+
if (opts.readOnly) return reader(path);
|
|
49
|
+
// A file's directory, made when absent. A name with none (`graph.db`, a
|
|
50
|
+
// `file:` URI such as `file:x?mode=memory`) is SQLite's to resolve.
|
|
51
|
+
let dir = path.startsWith('file:') ? 0 : path.lastIndexOf('/');
|
|
52
|
+
if (dir > 0) io.mkdirSync(path.slice(0, dir), {
|
|
53
|
+
recursive: true
|
|
54
|
+
});
|
|
55
|
+
let db = new Database(path);
|
|
56
|
+
// The busy timeout before anything else: the driver reads the schema as it
|
|
57
|
+
// is built, and a file another connection holds locked (its last checkpoint,
|
|
58
|
+
// as it closes, or a large atomic batch) is waited for, never refused while
|
|
59
|
+
// that bounded transaction finishes.
|
|
60
|
+
if (path != ':memory:') {
|
|
61
|
+
db.exec(render({
|
|
62
|
+
t: 'pragma',
|
|
63
|
+
name: 'busy_timeout',
|
|
64
|
+
value: 60_000
|
|
65
|
+
}).sql);
|
|
66
|
+
}
|
|
67
|
+
// A database in memory is lent a spare connection holding its template
|
|
68
|
+
// where one waits (./native.ts `spares`), and gives it back as it closes.
|
|
69
|
+
let d = driver(db, {
|
|
70
|
+
lend: path == ':memory:'
|
|
71
|
+
});
|
|
72
|
+
let set = (name, value)=>d.query({
|
|
73
|
+
t: 'pragma',
|
|
74
|
+
name,
|
|
75
|
+
value
|
|
76
|
+
});
|
|
77
|
+
set('foreign_keys', 'on');
|
|
78
|
+
if (path != ':memory:') {
|
|
79
|
+
// A new file's, before WAL, whose header write fixes the mode: the free
|
|
80
|
+
// pages a delete leaves can then go back (./owe.ts `reclaim`). Only while
|
|
81
|
+
// the file is empty, since on one in this mode the pragma takes the write
|
|
82
|
+
// lock to rewrite the header, and one made without it keeps its mode until
|
|
83
|
+
// a VACUUM.
|
|
84
|
+
let [{ page_count }] = d.query({
|
|
85
|
+
t: 'pragma',
|
|
86
|
+
name: 'page_count'
|
|
87
|
+
});
|
|
88
|
+
if (!page_count) set('auto_vacuum', 'incremental');
|
|
89
|
+
// The pages a connection keeps: at SQLite's 2 MiB, a write to a graph of
|
|
90
|
+
// a few hundred MiB read most of the index pages it touched back from the
|
|
91
|
+
// file, statement by statement.
|
|
92
|
+
set('cache_size', -CACHE / 1024);
|
|
93
|
+
// A checkpoint that grows the file changes its size, and syncing a size
|
|
94
|
+
// waits for ext4's journal to commit, which waits for every other
|
|
95
|
+
// process's ordered writes: beside a copy of the graph held at its disk cap
|
|
96
|
+
// that was 10 s on a server's main thread (T-133091). Growing a 128th of
|
|
97
|
+
// the file at a time, between 1 and 64 MiB, makes that once an hour on the
|
|
98
|
+
// box's graph and costs a small file one MiB.
|
|
99
|
+
let [{ page_size }] = d.query({
|
|
100
|
+
t: 'pragma',
|
|
101
|
+
name: 'page_size'
|
|
102
|
+
});
|
|
103
|
+
let mib = Number(page_count) * Number(page_size) / 128 / 2 ** 20;
|
|
104
|
+
if (d.file) chunk(db, Math.min(64, Math.max(1, Math.floor(mib))) * 2 ** 20);
|
|
105
|
+
// A file kept with a rollback journal is one file, for a copy taken
|
|
106
|
+
// elsewhere. It starts that way rather than leaving WAL once written:
|
|
107
|
+
// SQLite refuses to leave WAL while another connection holds the file,
|
|
108
|
+
// as the checkpoint thread's does, at once and past any busy timeout.
|
|
109
|
+
if (opts.wal === false) set('journal_mode', 'delete');
|
|
110
|
+
else logged(db, d, path, opts.wal ?? WAL, Number(page_size));
|
|
111
|
+
}
|
|
112
|
+
return Object.assign(d, {
|
|
113
|
+
...loadable() && {
|
|
114
|
+
extension: (file)=>{
|
|
115
|
+
let db = d.database();
|
|
116
|
+
db.enableLoadExtension = true;
|
|
117
|
+
try {
|
|
118
|
+
db.loadExtension(file);
|
|
119
|
+
} finally{
|
|
120
|
+
db.enableLoadExtension = false;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
};
|
|
126
|
+
// WAL mode with WAL's crash-safe pairing `synchronous = normal`, a WAL of `wal`
|
|
127
|
+
// bytes, and its checkpoints on ./checkpoint.ts's thread.
|
|
128
|
+
let logged = (db, d, path, wal, pageSize)=>{
|
|
129
|
+
let set = (name, value)=>d.query({
|
|
130
|
+
t: 'pragma',
|
|
131
|
+
name,
|
|
132
|
+
value
|
|
133
|
+
});
|
|
134
|
+
set('journal_mode', 'wal');
|
|
135
|
+
set('synchronous', 'normal');
|
|
136
|
+
// Once a checkpoint has moved all of the WAL, the next commit starts it over
|
|
137
|
+
// at its first frame, and a WAL kept at its size is then written over in
|
|
138
|
+
// place. A frame is a 24-byte header and a page, so every write covers part
|
|
139
|
+
// of a file page, and the kernel reads that page back first wherever memory
|
|
140
|
+
// pressure dropped it: on the committing thread, behind every other
|
|
141
|
+
// process's queue at the disk, where it held a server's main thread 11 s
|
|
142
|
+
// (bug bace8585, T-675). A limit of 0 cuts the WAL back to the commit that
|
|
143
|
+
// starts it over, so every later frame is appended past its end and no
|
|
144
|
+
// commit reads. The checkpoint's mark is the hook's below.
|
|
145
|
+
set('journal_size_limit', 0);
|
|
146
|
+
// The last connection to close leaves the WAL for the next to append to.
|
|
147
|
+
// A checkpoint on close syncs both files, and the next process's first
|
|
148
|
+
// commit then starts the WAL afresh and syncs its header: together a
|
|
149
|
+
// short command's slowest steps. The checkpoint thread still moves the
|
|
150
|
+
// pages into the file.
|
|
151
|
+
configure(db, 'SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE', true);
|
|
152
|
+
if (!d.file) return;
|
|
153
|
+
// A checkpoint syncs the file, which on this box waited seconds behind
|
|
154
|
+
// other processes' writes, and SQLite runs its own inside the COMMIT that
|
|
155
|
+
// crosses its mark, on the committing thread: a server's main thread was
|
|
156
|
+
// held 5.6 s (bug bace8585). The hook takes its place, and only asks
|
|
157
|
+
// ./checkpoint.ts's thread once the WAL holds what it is kept to.
|
|
158
|
+
let checkpoints = checkpoint(path);
|
|
159
|
+
let hook = walHook(db, (pages)=>pages >= wal / pageSize && checkpoints.run());
|
|
160
|
+
let close = d.close;
|
|
161
|
+
d.close = ()=>checkpoints.close(()=>{
|
|
162
|
+
try {
|
|
163
|
+
close();
|
|
164
|
+
} finally{
|
|
165
|
+
hook?.close();
|
|
166
|
+
hook = undefined;
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
};
|
|
170
|
+
//# sourceMappingURL=db.js.map
|
package/db.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"db.js","sources":["./db.ts"],"names":[],"mappings":"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"}
|
package/db.ts
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import * as io from '@yaks/file/io'
|
|
2
|
+
// The embedded driver itself — @db/sqlite's (./ffi), opened against the
|
|
3
|
+
// library ./sqlitepath.ts names. This module is its one door, and it hands out
|
|
4
|
+
// none of it: a caller gets `open(path)`, a {@link Driver} over the database
|
|
5
|
+
// there (./native.ts), and never the driver's own objects, so a different
|
|
6
|
+
// driver is a change to this package alone.
|
|
7
|
+
//
|
|
8
|
+
// ./mod.ts stays free of it on purpose: the adapter there works against any
|
|
9
|
+
// `Driver`, and only an application that wants an in-process database needs
|
|
10
|
+
// this.
|
|
11
|
+
|
|
12
|
+
import { Database } from './legacy.js'
|
|
13
|
+
import { type Driver, render } from '@yaks/sql'
|
|
14
|
+
import { chunk, configure, driver, walHook } from './native.js'
|
|
15
|
+
import { checkpoint } from './checkpoint.js'
|
|
16
|
+
|
|
17
|
+
import { loadable, open as reader } from './read-db.js'
|
|
18
|
+
|
|
19
|
+
/** A database this process opened: its {@link Driver}, and the way to close
|
|
20
|
+
* it. */
|
|
21
|
+
export type Opened = Driver & { close: () => void }
|
|
22
|
+
|
|
23
|
+
// The WAL a file's connection keeps, and lets grow before a checkpoint, in
|
|
24
|
+
// bytes: 16384 pages of 4 KiB.
|
|
25
|
+
let WAL = 64 * 1024 * 1024
|
|
26
|
+
|
|
27
|
+
// The most a file's connection keeps of its pages in memory, in bytes.
|
|
28
|
+
let CACHE = 64 * 1024 * 1024
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Open (or create) the database at `path` — a file, whose directory is made
|
|
32
|
+
* when missing, or `:memory:` — as a {@link Driver} to bind `storage()` to.
|
|
33
|
+
* This is the one place a connection's settings are made. `readOnly: true`
|
|
34
|
+
* requires an existing file and changes no journal settings. Extensions can
|
|
35
|
+
* read installed indexes; the read-only handle refuses metadata writes. A
|
|
36
|
+
* library that cannot load them (macOS's own) opens with no `extension`.
|
|
37
|
+
*
|
|
38
|
+
* Every connection runs with foreign keys on. A file is one other processes
|
|
39
|
+
* may have open too, so it also runs in WAL mode, with WAL's crash-safe pairing
|
|
40
|
+
* `synchronous = normal`, a 64 MiB WAL checkpointed on a thread of its own
|
|
41
|
+
* once it fills and cut back as it starts over (`wal` sets the size, for a test; `wal: false` keeps a
|
|
42
|
+
* rollback journal instead, for one file to copy elsewhere), a 64 MiB page cache, no
|
|
43
|
+
* checkpoint on close or commit, and a one-minute busy timeout, and a new file starts in incremental
|
|
44
|
+
* auto-vacuum mode, so the pages a delete frees can be given back (./owe.ts `reclaim`); a database in memory
|
|
45
|
+
* belongs to this process alone and needs none of that. A graph batch may be large, so contention gives
|
|
46
|
+
* the one writer that bounded minute to finish. A writer held longer is a
|
|
47
|
+
* performance failure, not a reason for every other process to wait forever.
|
|
48
|
+
*
|
|
49
|
+
* ```ts
|
|
50
|
+
* import { open } from '@yaks/sqlite/db'
|
|
51
|
+
*
|
|
52
|
+
* let sql = open(':memory:')
|
|
53
|
+
* sql.query({ t: 'create table', name: 't', cols: [{ name: 'x' }] })
|
|
54
|
+
* sql.close()
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export let open = (
|
|
58
|
+
path: string,
|
|
59
|
+
opts: { readOnly?: boolean; wal?: number | false } = {},
|
|
60
|
+
): Opened => {
|
|
61
|
+
if (opts.readOnly) return reader(path)
|
|
62
|
+
// A file's directory, made when absent. A name with none (`graph.db`, a
|
|
63
|
+
// `file:` URI such as `file:x?mode=memory`) is SQLite's to resolve.
|
|
64
|
+
let dir = path.startsWith('file:') ? 0 : path.lastIndexOf('/')
|
|
65
|
+
if (dir > 0) io.mkdirSync(path.slice(0, dir), { recursive: true })
|
|
66
|
+
let db = new Database(path)
|
|
67
|
+
// The busy timeout before anything else: the driver reads the schema as it
|
|
68
|
+
// is built, and a file another connection holds locked (its last checkpoint,
|
|
69
|
+
// as it closes, or a large atomic batch) is waited for, never refused while
|
|
70
|
+
// that bounded transaction finishes.
|
|
71
|
+
if (path != ':memory:') {
|
|
72
|
+
db.exec(render({ t: 'pragma', name: 'busy_timeout', value: 60_000 }).sql)
|
|
73
|
+
}
|
|
74
|
+
// A database in memory is lent a spare connection holding its template
|
|
75
|
+
// where one waits (./native.ts `spares`), and gives it back as it closes.
|
|
76
|
+
let d = driver(db, { lend: path == ':memory:' })
|
|
77
|
+
let set = (name: string, value: string | number) =>
|
|
78
|
+
d.query({ t: 'pragma', name, value })
|
|
79
|
+
set('foreign_keys', 'on')
|
|
80
|
+
if (path != ':memory:') {
|
|
81
|
+
// A new file's, before WAL, whose header write fixes the mode: the free
|
|
82
|
+
// pages a delete leaves can then go back (./owe.ts `reclaim`). Only while
|
|
83
|
+
// the file is empty, since on one in this mode the pragma takes the write
|
|
84
|
+
// lock to rewrite the header, and one made without it keeps its mode until
|
|
85
|
+
// a VACUUM.
|
|
86
|
+
let [{ page_count }] = d.query({ t: 'pragma', name: 'page_count' })
|
|
87
|
+
if (!page_count) set('auto_vacuum', 'incremental')
|
|
88
|
+
// The pages a connection keeps: at SQLite's 2 MiB, a write to a graph of
|
|
89
|
+
// a few hundred MiB read most of the index pages it touched back from the
|
|
90
|
+
// file, statement by statement.
|
|
91
|
+
set('cache_size', -CACHE / 1024)
|
|
92
|
+
// A checkpoint that grows the file changes its size, and syncing a size
|
|
93
|
+
// waits for ext4's journal to commit, which waits for every other
|
|
94
|
+
// process's ordered writes: beside a copy of the graph held at its disk cap
|
|
95
|
+
// that was 10 s on a server's main thread (T-133091). Growing a 128th of
|
|
96
|
+
// the file at a time, between 1 and 64 MiB, makes that once an hour on the
|
|
97
|
+
// box's graph and costs a small file one MiB.
|
|
98
|
+
let [{ page_size }] = d.query({ t: 'pragma', name: 'page_size' })
|
|
99
|
+
let mib = Number(page_count) * Number(page_size) / 128 / 2 ** 20
|
|
100
|
+
if (d.file) chunk(db, Math.min(64, Math.max(1, Math.floor(mib))) * 2 ** 20)
|
|
101
|
+
// A file kept with a rollback journal is one file, for a copy taken
|
|
102
|
+
// elsewhere. It starts that way rather than leaving WAL once written:
|
|
103
|
+
// SQLite refuses to leave WAL while another connection holds the file,
|
|
104
|
+
// as the checkpoint thread's does, at once and past any busy timeout.
|
|
105
|
+
if (opts.wal === false) set('journal_mode', 'delete')
|
|
106
|
+
else logged(db, d, path, opts.wal ?? WAL, Number(page_size))
|
|
107
|
+
}
|
|
108
|
+
return Object.assign(d, {
|
|
109
|
+
...loadable() && {
|
|
110
|
+
extension: (file: string) => {
|
|
111
|
+
let db = d.database()
|
|
112
|
+
db.enableLoadExtension = true
|
|
113
|
+
try {
|
|
114
|
+
db.loadExtension(file)
|
|
115
|
+
} finally {
|
|
116
|
+
db.enableLoadExtension = false
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
})
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// WAL mode with WAL's crash-safe pairing `synchronous = normal`, a WAL of `wal`
|
|
124
|
+
// bytes, and its checkpoints on ./checkpoint.ts's thread.
|
|
125
|
+
let logged = (
|
|
126
|
+
db: Database,
|
|
127
|
+
d: ReturnType<typeof driver>,
|
|
128
|
+
path: string,
|
|
129
|
+
wal: number,
|
|
130
|
+
pageSize: number,
|
|
131
|
+
) => {
|
|
132
|
+
let set = (name: string, value: string | number) =>
|
|
133
|
+
d.query({ t: 'pragma', name, value })
|
|
134
|
+
set('journal_mode', 'wal')
|
|
135
|
+
set('synchronous', 'normal')
|
|
136
|
+
// Once a checkpoint has moved all of the WAL, the next commit starts it over
|
|
137
|
+
// at its first frame, and a WAL kept at its size is then written over in
|
|
138
|
+
// place. A frame is a 24-byte header and a page, so every write covers part
|
|
139
|
+
// of a file page, and the kernel reads that page back first wherever memory
|
|
140
|
+
// pressure dropped it: on the committing thread, behind every other
|
|
141
|
+
// process's queue at the disk, where it held a server's main thread 11 s
|
|
142
|
+
// (bug bace8585, T-675). A limit of 0 cuts the WAL back to the commit that
|
|
143
|
+
// starts it over, so every later frame is appended past its end and no
|
|
144
|
+
// commit reads. The checkpoint's mark is the hook's below.
|
|
145
|
+
set('journal_size_limit', 0)
|
|
146
|
+
// The last connection to close leaves the WAL for the next to append to.
|
|
147
|
+
// A checkpoint on close syncs both files, and the next process's first
|
|
148
|
+
// commit then starts the WAL afresh and syncs its header: together a
|
|
149
|
+
// short command's slowest steps. The checkpoint thread still moves the
|
|
150
|
+
// pages into the file.
|
|
151
|
+
configure(db, 'SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE', true)
|
|
152
|
+
if (!d.file) return
|
|
153
|
+
// A checkpoint syncs the file, which on this box waited seconds behind
|
|
154
|
+
// other processes' writes, and SQLite runs its own inside the COMMIT that
|
|
155
|
+
// crosses its mark, on the committing thread: a server's main thread was
|
|
156
|
+
// held 5.6 s (bug bace8585). The hook takes its place, and only asks
|
|
157
|
+
// ./checkpoint.ts's thread once the WAL holds what it is kept to.
|
|
158
|
+
let checkpoints = checkpoint(path)
|
|
159
|
+
let hook: { close(): void } | undefined = walHook(
|
|
160
|
+
db,
|
|
161
|
+
(pages) => pages >= wal / pageSize && checkpoints.run(),
|
|
162
|
+
)
|
|
163
|
+
let close = d.close
|
|
164
|
+
d.close = () =>
|
|
165
|
+
checkpoints.close(() => {
|
|
166
|
+
try {
|
|
167
|
+
close()
|
|
168
|
+
} finally {
|
|
169
|
+
hook?.close()
|
|
170
|
+
hook = undefined
|
|
171
|
+
}
|
|
172
|
+
})
|
|
173
|
+
}
|