@g9tm/syncdata 1.2.1
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.md +21 -0
- package/PARITY.md +71 -0
- package/README.md +71 -0
- package/THIRD-PARTY-NOTICES.md +31 -0
- package/dist/abstractions/digest.d.ts +27 -0
- package/dist/abstractions/digest.js +78 -0
- package/dist/abstractions/digest.js.map +1 -0
- package/dist/abstractions/enums.d.ts +42 -0
- package/dist/abstractions/enums.js +60 -0
- package/dist/abstractions/enums.js.map +1 -0
- package/dist/abstractions/errors.d.ts +72 -0
- package/dist/abstractions/errors.js +90 -0
- package/dist/abstractions/errors.js.map +1 -0
- package/dist/abstractions/guid.d.ts +24 -0
- package/dist/abstractions/guid.js +93 -0
- package/dist/abstractions/guid.js.map +1 -0
- package/dist/abstractions/key.d.ts +16 -0
- package/dist/abstractions/key.js +47 -0
- package/dist/abstractions/key.js.map +1 -0
- package/dist/abstractions/long.d.ts +103 -0
- package/dist/abstractions/long.js +351 -0
- package/dist/abstractions/long.js.map +1 -0
- package/dist/abstractions/parameters.d.ts +49 -0
- package/dist/abstractions/parameters.js +153 -0
- package/dist/abstractions/parameters.js.map +1 -0
- package/dist/brotli-js/decode.d.ts +8 -0
- package/dist/brotli-js/decode.js +2004 -0
- package/dist/brotli-js/decode.js.map +1 -0
- package/dist/brotli-js/index.d.ts +5 -0
- package/dist/brotli-js/index.js +19 -0
- package/dist/brotli-js/index.js.map +1 -0
- package/dist/client/engine.d.ts +110 -0
- package/dist/client/engine.js +1158 -0
- package/dist/client/engine.js.map +1 -0
- package/dist/client/model.d.ts +61 -0
- package/dist/client/model.js +154 -0
- package/dist/client/model.js.map +1 -0
- package/dist/client/progress.d.ts +101 -0
- package/dist/client/progress.js +251 -0
- package/dist/client/progress.js.map +1 -0
- package/dist/client/runs.d.ts +219 -0
- package/dist/client/runs.js +239 -0
- package/dist/client/runs.js.map +1 -0
- package/dist/client/storeTypes.d.ts +428 -0
- package/dist/client/storeTypes.js +256 -0
- package/dist/client/storeTypes.js.map +1 -0
- package/dist/client/transport.d.ts +19 -0
- package/dist/client/transport.js +2 -0
- package/dist/client/transport.js.map +1 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +35 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/abort.d.ts +21 -0
- package/dist/internal/abort.js +125 -0
- package/dist/internal/abort.js.map +1 -0
- package/dist/internal/base64.d.ts +8 -0
- package/dist/internal/base64.js +85 -0
- package/dist/internal/base64.js.map +1 -0
- package/dist/internal/dotnetFormat.d.ts +32 -0
- package/dist/internal/dotnetFormat.js +207 -0
- package/dist/internal/dotnetFormat.js.map +1 -0
- package/dist/internal/md5.d.ts +2 -0
- package/dist/internal/md5.js +97 -0
- package/dist/internal/md5.js.map +1 -0
- package/dist/internal/sha256.d.ts +20 -0
- package/dist/internal/sha256.js +153 -0
- package/dist/internal/sha256.js.map +1 -0
- package/dist/internal/utf8.d.ts +8 -0
- package/dist/internal/utf8.js +143 -0
- package/dist/internal/utf8.js.map +1 -0
- package/dist/node/index.d.ts +9 -0
- package/dist/node/index.js +35 -0
- package/dist/node/index.js.map +1 -0
- package/dist/node/sqliteDriver.d.ts +11 -0
- package/dist/node/sqliteDriver.js +252 -0
- package/dist/node/sqliteDriver.js.map +1 -0
- package/dist/protocol/dtos.d.ts +280 -0
- package/dist/protocol/dtos.js +33 -0
- package/dist/protocol/dtos.js.map +1 -0
- package/dist/protocol/protocol.d.ts +36 -0
- package/dist/protocol/protocol.js +63 -0
- package/dist/protocol/protocol.js.map +1 -0
- package/dist/protocol/rowBatch/columnLayout.d.ts +4 -0
- package/dist/protocol/rowBatch/columnLayout.js +309 -0
- package/dist/protocol/rowBatch/columnLayout.js.map +1 -0
- package/dist/protocol/rowBatch/format.d.ts +74 -0
- package/dist/protocol/rowBatch/format.js +163 -0
- package/dist/protocol/rowBatch/format.js.map +1 -0
- package/dist/protocol/rowBatch/reader.d.ts +119 -0
- package/dist/protocol/rowBatch/reader.js +459 -0
- package/dist/protocol/rowBatch/reader.js.map +1 -0
- package/dist/protocol/rowBatch/writer.d.ts +101 -0
- package/dist/protocol/rowBatch/writer.js +412 -0
- package/dist/protocol/rowBatch/writer.js.map +1 -0
- package/dist/protocol/wire.d.ts +32 -0
- package/dist/protocol/wire.js +329 -0
- package/dist/protocol/wire.js.map +1 -0
- package/dist/signalr/index.d.ts +2 -0
- package/dist/signalr/index.js +5 -0
- package/dist/signalr/index.js.map +1 -0
- package/dist/signalr/protocols.d.ts +17 -0
- package/dist/signalr/protocols.js +109 -0
- package/dist/signalr/protocols.js.map +1 -0
- package/dist/signalr/transport.d.ts +67 -0
- package/dist/signalr/transport.js +286 -0
- package/dist/signalr/transport.js.map +1 -0
- package/dist/sqlite/apply.d.ts +91 -0
- package/dist/sqlite/apply.js +657 -0
- package/dist/sqlite/apply.js.map +1 -0
- package/dist/sqlite/commands.d.ts +24 -0
- package/dist/sqlite/commands.js +167 -0
- package/dist/sqlite/commands.js.map +1 -0
- package/dist/sqlite/connection.d.ts +81 -0
- package/dist/sqlite/connection.js +322 -0
- package/dist/sqlite/connection.js.map +1 -0
- package/dist/sqlite/driver.d.ts +63 -0
- package/dist/sqlite/driver.js +9 -0
- package/dist/sqlite/driver.js.map +1 -0
- package/dist/sqlite/index.d.ts +6 -0
- package/dist/sqlite/index.js +7 -0
- package/dist/sqlite/index.js.map +1 -0
- package/dist/sqlite/internals.d.ts +63 -0
- package/dist/sqlite/internals.js +194 -0
- package/dist/sqlite/internals.js.map +1 -0
- package/dist/sqlite/options.d.ts +78 -0
- package/dist/sqlite/options.js +78 -0
- package/dist/sqlite/options.js.map +1 -0
- package/dist/sqlite/push.d.ts +9 -0
- package/dist/sqlite/push.js +272 -0
- package/dist/sqlite/push.js.map +1 -0
- package/dist/sqlite/query.d.ts +38 -0
- package/dist/sqlite/query.js +476 -0
- package/dist/sqlite/query.js.map +1 -0
- package/dist/sqlite/revocation.d.ts +28 -0
- package/dist/sqlite/revocation.js +337 -0
- package/dist/sqlite/revocation.js.map +1 -0
- package/dist/sqlite/sql.d.ts +60 -0
- package/dist/sqlite/sql.js +324 -0
- package/dist/sqlite/sql.js.map +1 -0
- package/dist/sqlite/store.d.ts +116 -0
- package/dist/sqlite/store.js +664 -0
- package/dist/sqlite/store.js.map +1 -0
- package/dist/sqlite/values.d.ts +29 -0
- package/dist/sqlite/values.js +288 -0
- package/dist/sqlite/values.js.map +1 -0
- package/dist/testing/index.d.ts +61 -0
- package/dist/testing/index.js +175 -0
- package/dist/testing/index.js.map +1 -0
- package/package.json +104 -0
|
@@ -0,0 +1,664 @@
|
|
|
1
|
+
// Twin of G9SyncData.Client.Sqlite/G9CSqliteSyncStore.cs (+ .Incarnation.cs, .Recovery.cs): the SQLite implementation of
|
|
2
|
+
// the client store. Every object it creates is the one the .NET store creates (AiGuides/08-Client-Sqlite.md), so the same
|
|
3
|
+
// database file serves the MAUI app and the Lynx app.
|
|
4
|
+
import { G9EMoveOutPolicy, G9ESyncDirection } from '../abstractions/enums.js';
|
|
5
|
+
import { G9SyncErrorCodes, G9SyncException } from '../abstractions/errors.js';
|
|
6
|
+
import { guidToN, newGuid } from '../abstractions/guid.js';
|
|
7
|
+
import { G9Long } from '../abstractions/long.js';
|
|
8
|
+
import { G9SyncParameters } from '../abstractions/parameters.js';
|
|
9
|
+
import { G9ApplyReport, G9ERevocationReason, G9ESubscriptionState, } from '../client/storeTypes.js';
|
|
10
|
+
import * as apply from './apply.js';
|
|
11
|
+
import * as commands from './commands.js';
|
|
12
|
+
import { asNumber, asOptionalString, asString, G9AsyncLock, G9SqliteConnection } from './connection.js';
|
|
13
|
+
import { byTid, LocalTable, logTo, meta, nowMs, persistedJson, readLocalValues, readPersistedDefinitions, readPersistedNegotiated, readSubscriptions, serializeValues, setMeta, } from './internals.js';
|
|
14
|
+
import { resolveStoreOptions } from './options.js';
|
|
15
|
+
import * as push from './push.js';
|
|
16
|
+
import * as query from './query.js';
|
|
17
|
+
import * as revocation from './revocation.js';
|
|
18
|
+
import { allColumnsMask, bit, columnDefinition, createCommandTriggers, createTable, createTriggers, dropTriggers, EPHEMERAL_SLOT, keyExpression, METADATA, modelHash, quote } from './sql.js';
|
|
19
|
+
/**
|
|
20
|
+
* The SQLite client store over a G9SqliteDriver. Library writes are serialised in-process; app writes on other
|
|
21
|
+
* connections are captured by triggers and wait on SQLite's busy timeout.
|
|
22
|
+
*/
|
|
23
|
+
export class G9SqliteSyncStore {
|
|
24
|
+
constructor(options) {
|
|
25
|
+
this.writeLock = new G9AsyncLock();
|
|
26
|
+
this.byLocal = new Map();
|
|
27
|
+
this.byServerId = new Map();
|
|
28
|
+
/** True when this initialisation found a database of another installation and gave it a new identity (ADR-0067). */
|
|
29
|
+
this.replicaIdentityWasReplaced = false;
|
|
30
|
+
this._model = null;
|
|
31
|
+
this._writer = null;
|
|
32
|
+
this._reader = null;
|
|
33
|
+
this._opening = null;
|
|
34
|
+
this.options = resolveStoreOptions(options);
|
|
35
|
+
}
|
|
36
|
+
// ================================================================ internals
|
|
37
|
+
writer() {
|
|
38
|
+
if (!this._writer)
|
|
39
|
+
throw new Error('The SQLite sync store is not open; call initialize first.');
|
|
40
|
+
return this._writer;
|
|
41
|
+
}
|
|
42
|
+
reader() {
|
|
43
|
+
if (!this._reader)
|
|
44
|
+
throw new Error('The SQLite sync store is not open; call initialize first.');
|
|
45
|
+
return this._reader;
|
|
46
|
+
}
|
|
47
|
+
ensureInitialized() {
|
|
48
|
+
if (!this._model)
|
|
49
|
+
throw new Error('The SQLite sync store is not initialised; call initialize first.');
|
|
50
|
+
return this._model;
|
|
51
|
+
}
|
|
52
|
+
log(level, eventId, message, error) {
|
|
53
|
+
logTo(this.options.logger, level, eventId, message, error);
|
|
54
|
+
}
|
|
55
|
+
async _open() {
|
|
56
|
+
if (this._writer && this._reader)
|
|
57
|
+
return;
|
|
58
|
+
if (!this._opening) {
|
|
59
|
+
this._opening = (async () => {
|
|
60
|
+
const driver = this.options.driver;
|
|
61
|
+
const pragmas = { busyTimeoutMs: this.options.busyTimeoutMs, foreignKeys: false };
|
|
62
|
+
const writerSession = await driver.open({ path: this.options.databasePath, create: true, pragmas });
|
|
63
|
+
const writer = new G9SqliteConnection(driver, writerSession);
|
|
64
|
+
await this._configure(writer);
|
|
65
|
+
if (this.options.useWriteAheadLog)
|
|
66
|
+
await writer.query('PRAGMA journal_mode = WAL');
|
|
67
|
+
const readerSession = await driver.open({ path: this.options.databasePath, create: true, pragmas });
|
|
68
|
+
const reader = new G9SqliteConnection(driver, readerSession);
|
|
69
|
+
await this._configure(reader);
|
|
70
|
+
this._writer = writer;
|
|
71
|
+
this._reader = reader;
|
|
72
|
+
})();
|
|
73
|
+
this._opening.catch(() => (this._opening = null));
|
|
74
|
+
}
|
|
75
|
+
await this._opening;
|
|
76
|
+
}
|
|
77
|
+
/** The PRAGMAs the .NET store sets on every connection it opens. */
|
|
78
|
+
async _configure(connection) {
|
|
79
|
+
await connection.exec('PRAGMA busy_timeout = ' + this.options.busyTimeoutMs + '; PRAGMA foreign_keys = OFF; PRAGMA journal_size_limit = ' + this.options.walSizeLimitBytes + ';');
|
|
80
|
+
}
|
|
81
|
+
// ================================================================ initialise
|
|
82
|
+
async initialize(model) {
|
|
83
|
+
if (!model)
|
|
84
|
+
throw new TypeError('A model is required.');
|
|
85
|
+
await this._open();
|
|
86
|
+
await this.writeLock.run(async () => {
|
|
87
|
+
const connection = this.writer();
|
|
88
|
+
const tables = [];
|
|
89
|
+
await connection.transaction('immediate', async () => {
|
|
90
|
+
await connection.exec(METADATA);
|
|
91
|
+
if (asNumber(await connection.scalar("SELECT count(*) FROM pragma_table_info('g9_ctx') WHERE name = 'command_id'")) === 0) {
|
|
92
|
+
// Stores created before commands existed (ADR-0025).
|
|
93
|
+
await connection.exec('ALTER TABLE g9_ctx ADD COLUMN command_id TEXT NULL');
|
|
94
|
+
}
|
|
95
|
+
if (asNumber(await connection.scalar("SELECT count(*) FROM pragma_table_info('g9_stage_runs') WHERE name = 'generation'")) === 0) {
|
|
96
|
+
// Stores created before stages were validated on resume (ADR-0046): a stage in flight is dropped and replanned.
|
|
97
|
+
await connection.exec('ALTER TABLE g9_stage_runs ADD COLUMN generation TEXT NULL');
|
|
98
|
+
await connection.exec('ALTER TABLE g9_stage_runs ADD COLUMN stage_seq INTEGER NOT NULL DEFAULT 0');
|
|
99
|
+
await connection.exec('DELETE FROM g9_stage; DELETE FROM g9_stage_runs;');
|
|
100
|
+
}
|
|
101
|
+
if (asNumber(await connection.scalar("SELECT count(*) FROM pragma_table_info('g9_stage_runs') WHERE name = 'from_cursors_json'")) === 0) {
|
|
102
|
+
await connection.exec('ALTER TABLE g9_stage_runs ADD COLUMN from_cursors_json TEXT NULL');
|
|
103
|
+
}
|
|
104
|
+
if (asNumber(await connection.scalar("SELECT count(*) FROM pragma_table_info('g9_subscriptions') WHERE name = 'move_out_policy'")) === 0) {
|
|
105
|
+
await connection.exec('ALTER TABLE g9_subscriptions ADD COLUMN move_out_policy INTEGER NULL');
|
|
106
|
+
}
|
|
107
|
+
if ((await meta(connection, 'client_id')) === null)
|
|
108
|
+
await setMeta(connection, 'client_id', newGuid());
|
|
109
|
+
if ((await meta(connection, 'next_batch_id')) === null)
|
|
110
|
+
await setMeta(connection, 'next_batch_id', '1');
|
|
111
|
+
await this._verifyIncarnation(connection);
|
|
112
|
+
for (const table of model.tables)
|
|
113
|
+
tables.push(await this._provisionTable(connection, table));
|
|
114
|
+
});
|
|
115
|
+
this._model = model;
|
|
116
|
+
this.byLocal = new Map(tables.map((t) => [t.model.localName.toLowerCase(), t]));
|
|
117
|
+
this.byServerId = new Map(tables.filter((t) => t.negotiated !== null).map((t) => [t.negotiated.tableId, t]));
|
|
118
|
+
this.log('information', 20101, `G9SyncData SQLite store initialised with ${tables.length} tables`);
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
async _provisionTable(connection, table) {
|
|
122
|
+
const hash = modelHash(table);
|
|
123
|
+
let tid = -1;
|
|
124
|
+
let existingHash = null;
|
|
125
|
+
let negotiatedJson = null;
|
|
126
|
+
let syncedJson = null;
|
|
127
|
+
let isNew = false;
|
|
128
|
+
const found = (await connection.query('SELECT tid, model_hash, negotiated_json, synced_columns FROM g9_tables WHERE local_name = $n', { $n: table.localName }))[0];
|
|
129
|
+
if (found) {
|
|
130
|
+
tid = asNumber(found[0]);
|
|
131
|
+
existingHash = asString(found[1]);
|
|
132
|
+
negotiatedJson = asOptionalString(found[2]);
|
|
133
|
+
syncedJson = asOptionalString(found[3]);
|
|
134
|
+
}
|
|
135
|
+
if (tid < 0) {
|
|
136
|
+
await connection.exec("INSERT INTO g9_tables (local_name, server_name, model_hash) VALUES ($n, $s, '')", { $n: table.localName, $s: table.serverName });
|
|
137
|
+
tid = asNumber(await connection.scalar('SELECT last_insert_rowid()'));
|
|
138
|
+
isNew = true;
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
await connection.exec('UPDATE g9_tables SET server_name = $s WHERE tid = $t', { $s: table.serverName, $t: tid });
|
|
142
|
+
}
|
|
143
|
+
const exists = (await connection.scalar("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = $n COLLATE NOCASE", { $n: table.localName })) !== null;
|
|
144
|
+
if (!exists) {
|
|
145
|
+
await connection.exec(createTable(table, this.options));
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
const existing = new Set((await connection.query('SELECT name FROM pragma_table_info($n)', { $n: table.localName })).map((r) => asString(r[0]).toLowerCase()));
|
|
149
|
+
for (const column of table.columns) {
|
|
150
|
+
if (existing.has(column.name.toLowerCase()))
|
|
151
|
+
continue;
|
|
152
|
+
await connection.exec('ALTER TABLE ' + quote(table.localName) + ' ADD COLUMN ' + columnDefinition(column, this.options));
|
|
153
|
+
this.log('information', 20102, `Added column ${table.localName}.${column.name} from the client model`);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
for (const index of table.indexes) {
|
|
157
|
+
await connection.exec(`CREATE ${index.unique ? 'UNIQUE ' : ''}INDEX IF NOT EXISTS ${quote(index.name)} ON ${quote(table.localName)} (${index.columns.map(quote).join(', ')})`);
|
|
158
|
+
}
|
|
159
|
+
const negotiated = readPersistedNegotiated(negotiatedJson);
|
|
160
|
+
const captureWanted = !negotiated || negotiated.direction !== G9ESyncDirection.DownloadOnly;
|
|
161
|
+
const triggerCount = asNumber(await connection.scalar("SELECT count(*) FROM sqlite_master WHERE type = 'trigger' AND name IN ($a, $b, $c)", {
|
|
162
|
+
$a: 'g9_' + table.localName + '_ai',
|
|
163
|
+
$b: 'g9_' + table.localName + '_au',
|
|
164
|
+
$c: 'g9_' + table.localName + '_ad',
|
|
165
|
+
}));
|
|
166
|
+
const commandTriggerCount = asNumber(await connection.scalar("SELECT count(*) FROM sqlite_master WHERE type = 'trigger' AND name IN ($a, $b, $c)", {
|
|
167
|
+
$a: 'g9_' + table.localName + '_ci',
|
|
168
|
+
$b: 'g9_' + table.localName + '_cu',
|
|
169
|
+
$c: 'g9_' + table.localName + '_cd',
|
|
170
|
+
}));
|
|
171
|
+
if (existingHash !== hash || (captureWanted && triggerCount !== 3) || commandTriggerCount !== 3) {
|
|
172
|
+
await connection.exec(dropTriggers(table));
|
|
173
|
+
if (captureWanted)
|
|
174
|
+
await connection.exec(createTriggers(table, tid));
|
|
175
|
+
await connection.exec(createCommandTriggers(table, tid));
|
|
176
|
+
if (existingHash !== null && existingHash.length > 0 && existingHash !== hash) {
|
|
177
|
+
// Column order may have changed: pending rows upload every column rather than trusting old mask bits.
|
|
178
|
+
await connection.exec('UPDATE g9_rows SET dirty_mask = $m WHERE tid = $t AND dirty = 1', { $m: allColumnsMask(table.columns.length), $t: tid });
|
|
179
|
+
}
|
|
180
|
+
await connection.exec('UPDATE g9_tables SET model_hash = $h WHERE tid = $t', { $h: hash, $t: tid });
|
|
181
|
+
}
|
|
182
|
+
if (isNew && exists) {
|
|
183
|
+
const adopted = await connection.exec(`INSERT OR IGNORE INTO g9_rows (tid, pk) SELECT $t, ${keyExpression(table, 'b')} FROM ${quote(table.localName)} AS b`, { $t: tid });
|
|
184
|
+
if (adopted > 0)
|
|
185
|
+
this.log('information', 20103, `Adopted ${adopted} existing rows of ${table.localName} into change capture`);
|
|
186
|
+
}
|
|
187
|
+
const synced = syncedJson === null ? [] : JSON.parse(syncedJson);
|
|
188
|
+
return new LocalTable(table, tid, negotiated, Array.isArray(synced) ? synced.map(String) : []);
|
|
189
|
+
}
|
|
190
|
+
/** ADR-0067: a database restored from a backup or copied from another device gets a new identity. */
|
|
191
|
+
async _verifyIncarnation(connection) {
|
|
192
|
+
const marker = this.options.incarnationMarker;
|
|
193
|
+
if (!marker)
|
|
194
|
+
return;
|
|
195
|
+
const inDatabase = await meta(connection, 'incarnation');
|
|
196
|
+
const inMarker = await marker.read();
|
|
197
|
+
if (inDatabase !== null && inDatabase === inMarker)
|
|
198
|
+
return;
|
|
199
|
+
// The marker is written BEFORE the transaction commits; if that fails nothing is recorded and nothing is reset.
|
|
200
|
+
const incarnation = guidToN(newGuid());
|
|
201
|
+
if (!(await marker.write(incarnation))) {
|
|
202
|
+
this.log('warning', 20106, 'The incarnation marker could not be read or written; restore detection is off for this run');
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
if (inDatabase === null) {
|
|
206
|
+
// A database from before this check existed, or a brand-new one: adopt it.
|
|
207
|
+
await setMeta(connection, 'incarnation', incarnation);
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
const pending = asNumber(await connection.scalar('SELECT count(*) FROM g9_rows WHERE dirty <> 0'));
|
|
211
|
+
await setMeta(connection, 'incarnation', incarnation);
|
|
212
|
+
await setMeta(connection, 'client_id', newGuid());
|
|
213
|
+
await setMeta(connection, 'next_batch_id', '1');
|
|
214
|
+
await connection.exec("DELETE FROM g9_meta WHERE key IN ('inflight_batch_id', 'inflight_max_seq'); UPDATE g9_commands SET batch_id = NULL WHERE state = 0; DELETE FROM g9_stage; DELETE FROM g9_stage_runs;");
|
|
215
|
+
this.replicaIdentityWasReplaced = true;
|
|
216
|
+
this.log('warning', 20105, `This database was restored or copied from another installation: it has a new client id and its push batch counter starts again. ${pending} pending row(s) were kept and upload as usual`);
|
|
217
|
+
}
|
|
218
|
+
async getClientId() {
|
|
219
|
+
await this._open();
|
|
220
|
+
const id = await meta(this.reader(), 'client_id');
|
|
221
|
+
if (id === null)
|
|
222
|
+
throw new Error('The store is not initialised.');
|
|
223
|
+
return id;
|
|
224
|
+
}
|
|
225
|
+
// ================================================================ server state
|
|
226
|
+
async getServerState() {
|
|
227
|
+
await this._open();
|
|
228
|
+
const connection = this.reader();
|
|
229
|
+
const epoch = await meta(connection, 'epoch');
|
|
230
|
+
const lastHello = await meta(connection, 'last_hello_utc');
|
|
231
|
+
const offset = await meta(connection, 'server_offset_ms');
|
|
232
|
+
const definitions = await meta(connection, 'definitions_json');
|
|
233
|
+
const tables = await meta(connection, 'tables_json');
|
|
234
|
+
const parsedTables = tables === null ? [] : JSON.parse(tables);
|
|
235
|
+
return {
|
|
236
|
+
epoch,
|
|
237
|
+
lastHelloUtcMs: lastHello === null ? null : Number(lastHello),
|
|
238
|
+
serverClockOffsetMs: offset === null ? 0 : Number(offset),
|
|
239
|
+
definitions: readPersistedDefinitions(definitions),
|
|
240
|
+
tables: Array.isArray(parsedTables) ? parsedTables.map((t) => readPersistedNegotiated(JSON.stringify(t))) : [],
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
async saveHello(hello) {
|
|
244
|
+
const model = this.ensureInitialized();
|
|
245
|
+
await this.writeLock.run(async () => {
|
|
246
|
+
const connection = this.writer();
|
|
247
|
+
const updates = [];
|
|
248
|
+
await connection.transaction('immediate', async () => {
|
|
249
|
+
await setMeta(connection, 'epoch', hello.epoch);
|
|
250
|
+
await setMeta(connection, 'last_hello_utc', String(nowMs()));
|
|
251
|
+
await setMeta(connection, 'server_offset_ms', String(Math.trunc(hello.serverTimeUtcMs - Date.now())));
|
|
252
|
+
await setMeta(connection, 'definitions_json', persistedJson(hello.definitions));
|
|
253
|
+
await setMeta(connection, 'tables_json', persistedJson(hello.tables));
|
|
254
|
+
// Kept per subscription: a caller who loses access stops being told the definition (ADR-0048).
|
|
255
|
+
for (const definition of hello.definitions) {
|
|
256
|
+
await connection.exec('UPDATE g9_subscriptions SET move_out_policy = $p WHERE name = $n', { $p: definition.moveOutPolicy, $n: definition.name });
|
|
257
|
+
}
|
|
258
|
+
for (const negotiated of hello.tables) {
|
|
259
|
+
const clientTable = model.findByServerName(negotiated.name);
|
|
260
|
+
const local = clientTable ? this.byLocal.get(clientTable.localName.toLowerCase()) : undefined;
|
|
261
|
+
if (!local)
|
|
262
|
+
continue;
|
|
263
|
+
const names = negotiated.columns.map((c) => c.name);
|
|
264
|
+
let synced;
|
|
265
|
+
if (local.negotiated === null) {
|
|
266
|
+
synced = names;
|
|
267
|
+
}
|
|
268
|
+
else {
|
|
269
|
+
synced = local.syncedColumns.slice();
|
|
270
|
+
const added = names.filter((n) => !synced.some((s) => s.toLowerCase() === n.toLowerCase()));
|
|
271
|
+
if (added.length > 0 && (await connection.scalar('SELECT 1 FROM g9_rows WHERE tid = $t AND deleted = 0 LIMIT 1', { $t: local.tid })) === null) {
|
|
272
|
+
for (const a of added)
|
|
273
|
+
synced.push(a);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
await connection.exec('UPDATE g9_tables SET server_table_id = $s, negotiated_json = $j, synced_columns = $c WHERE tid = $t', {
|
|
277
|
+
$s: negotiated.tableId,
|
|
278
|
+
$j: persistedJson(negotiated),
|
|
279
|
+
$c: persistedJson(synced),
|
|
280
|
+
$t: local.tid,
|
|
281
|
+
});
|
|
282
|
+
if (negotiated.direction === G9ESyncDirection.DownloadOnly) {
|
|
283
|
+
await connection.exec(dropTriggers(local.model));
|
|
284
|
+
}
|
|
285
|
+
else if (local.negotiated && local.negotiated.direction === G9ESyncDirection.DownloadOnly) {
|
|
286
|
+
await connection.exec(dropTriggers(local.model) + createTriggers(local.model, local.tid));
|
|
287
|
+
}
|
|
288
|
+
updates.push({ local, negotiated, synced });
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
for (const { local, negotiated, synced } of updates) {
|
|
292
|
+
local.negotiated = negotiated;
|
|
293
|
+
local.syncedColumns = synced;
|
|
294
|
+
}
|
|
295
|
+
const byServerId = new Map();
|
|
296
|
+
this.byLocal.forEach((t) => {
|
|
297
|
+
if (t.negotiated)
|
|
298
|
+
byServerId.set(t.negotiated.tableId, t);
|
|
299
|
+
});
|
|
300
|
+
this.byServerId = byServerId;
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
async getPendingBackfills() {
|
|
304
|
+
this.ensureInitialized();
|
|
305
|
+
const backfills = [];
|
|
306
|
+
this.byLocal.forEach((table) => {
|
|
307
|
+
if (!table.negotiated)
|
|
308
|
+
return;
|
|
309
|
+
const missing = table.negotiated.columns.map((c) => c.name).filter((n) => !table.syncedColumns.some((s) => s.toLowerCase() === n.toLowerCase()));
|
|
310
|
+
if (missing.length > 0)
|
|
311
|
+
backfills.push({ tableId: table.negotiated.tableId, columns: missing });
|
|
312
|
+
});
|
|
313
|
+
return backfills;
|
|
314
|
+
}
|
|
315
|
+
// ================================================================ subscriptions
|
|
316
|
+
async getSubscriptions() {
|
|
317
|
+
await this._open();
|
|
318
|
+
return readSubscriptions(this.reader(), null);
|
|
319
|
+
}
|
|
320
|
+
async ensureSubscription(name, parameters, ephemeral, ttlMs, adoptExistingRows) {
|
|
321
|
+
if (typeof name !== 'string' || name.trim() === '')
|
|
322
|
+
throw new TypeError('A subscription name is required.');
|
|
323
|
+
const key = G9SyncParameters.buildSubscriptionKey(name, parameters);
|
|
324
|
+
await this._open();
|
|
325
|
+
return this.writeLock.run(() => this.writer().transaction('immediate', async () => {
|
|
326
|
+
const connection = this.writer();
|
|
327
|
+
const existing = await readSubscriptions(connection, key);
|
|
328
|
+
if (existing.length > 0)
|
|
329
|
+
return existing[0];
|
|
330
|
+
let slot = EPHEMERAL_SLOT;
|
|
331
|
+
if (!ephemeral) {
|
|
332
|
+
const used = new Set((await connection.query('SELECT slot FROM g9_subscriptions WHERE ephemeral = 0')).map((r) => asNumber(r[0])));
|
|
333
|
+
slot = -1;
|
|
334
|
+
for (let s = 0; s < EPHEMERAL_SLOT; s++) {
|
|
335
|
+
if (!used.has(s)) {
|
|
336
|
+
slot = s;
|
|
337
|
+
break;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
if (slot < 0) {
|
|
341
|
+
throw new G9SyncException(G9SyncErrorCodes.LocalStoreError, 'At most 63 persistent subscriptions per database; declare per-item subscriptions Ephemeral(ttl) on the server.');
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
const now = nowMs();
|
|
345
|
+
await connection.exec('INSERT INTO g9_subscriptions (key, name, params, slot, ephemeral, ttl_seconds, state, created_utc, last_access_utc) VALUES ($k, $n, $p, $s, $e, $t, $st, $c, $c)', {
|
|
346
|
+
$k: key,
|
|
347
|
+
$n: name,
|
|
348
|
+
$p: JSON.stringify(parameters.values),
|
|
349
|
+
$s: slot,
|
|
350
|
+
$e: ephemeral ? 1 : 0,
|
|
351
|
+
$t: ttlMs === null ? null : Math.trunc(ttlMs / 1000),
|
|
352
|
+
$st: adoptExistingRows ? G9ESubscriptionState.NeedsReconcile : G9ESubscriptionState.Active,
|
|
353
|
+
$c: now,
|
|
354
|
+
});
|
|
355
|
+
return (await readSubscriptions(connection, key))[0];
|
|
356
|
+
}));
|
|
357
|
+
}
|
|
358
|
+
removeSubscription(key, policy) {
|
|
359
|
+
return this._removeSubscription(key, policy, G9ERevocationReason.Unsubscribed, false, null);
|
|
360
|
+
}
|
|
361
|
+
async releaseSubscriptionRows(key) {
|
|
362
|
+
// The policy comes from the last definition the server announced for this subscription (ADR-0048).
|
|
363
|
+
const report = new G9ApplyReport();
|
|
364
|
+
await this._removeSubscription(key, null, G9ERevocationReason.AccessRevoked, true, report);
|
|
365
|
+
return report;
|
|
366
|
+
}
|
|
367
|
+
async _removeSubscription(key, policy, reason, keepSubscription, report) {
|
|
368
|
+
this.ensureInitialized();
|
|
369
|
+
await this.writeLock.run(() => this.writer().transaction('immediate', async () => {
|
|
370
|
+
var _a;
|
|
371
|
+
const connection = this.writer();
|
|
372
|
+
const record = (await readSubscriptions(connection, key))[0];
|
|
373
|
+
if (!record)
|
|
374
|
+
return;
|
|
375
|
+
const effective = (_a = policy !== null && policy !== void 0 ? policy : record.moveOutPolicy) !== null && _a !== void 0 ? _a : G9EMoveOutPolicy.Delete;
|
|
376
|
+
await connection.exec('UPDATE g9_ctx SET applying = 1 WHERE id = 1');
|
|
377
|
+
await connection.exec('DROP TABLE IF EXISTS temp.g9_affected; CREATE TEMP TABLE g9_affected (tid INTEGER NOT NULL, pk TEXT NOT NULL, PRIMARY KEY (tid, pk)) WITHOUT ROWID');
|
|
378
|
+
if (record.ephemeral) {
|
|
379
|
+
await connection.exec('INSERT INTO temp.g9_affected (tid, pk) SELECT tid, pk FROM g9_ephemeral_rows WHERE sub_key = $k', { $k: key });
|
|
380
|
+
await connection.exec('DELETE FROM g9_ephemeral_rows WHERE sub_key = $k', { $k: key });
|
|
381
|
+
await connection.exec('UPDATE g9_rows SET subs = subs & $n WHERE (tid, pk) IN (SELECT tid, pk FROM temp.g9_affected) AND NOT EXISTS (SELECT 1 FROM g9_ephemeral_rows e WHERE e.tid = g9_rows.tid AND e.pk = g9_rows.pk)', { $n: bit(EPHEMERAL_SLOT).not() });
|
|
382
|
+
}
|
|
383
|
+
else {
|
|
384
|
+
const b = bit(record.slot);
|
|
385
|
+
await connection.exec('INSERT INTO temp.g9_affected (tid, pk) SELECT tid, pk FROM g9_rows WHERE (subs & $b) <> 0', { $b: b });
|
|
386
|
+
await connection.exec('UPDATE g9_rows SET subs = subs & $n WHERE (subs & $b) <> 0', { $n: b.not(), $b: b });
|
|
387
|
+
}
|
|
388
|
+
if (effective === G9EMoveOutPolicy.Delete) {
|
|
389
|
+
// Rows no other subscription holds go through the revocation policy (ADR-0026), pending changes included.
|
|
390
|
+
const releasedSql = 'SELECT r.tid, r.pk FROM g9_rows r JOIN temp.g9_affected a ON a.tid = r.tid AND a.pk = r.pk WHERE r.subs = 0 AND r.deleted = 0';
|
|
391
|
+
let removed = 0;
|
|
392
|
+
if ((await apply.foreignTriggerNames(this, connection)).length === 0) {
|
|
393
|
+
removed = await revocation.disposeReleasedRows(this, connection, releasedSql, {}, reason, key, report);
|
|
394
|
+
}
|
|
395
|
+
else {
|
|
396
|
+
// A foreign trigger may write what the next row's decision reads: row by row, each decision after the last.
|
|
397
|
+
const released = (await connection.query(releasedSql)).map((r) => ({ tid: asNumber(r[0]), key: asString(r[1]) }));
|
|
398
|
+
for (const row of released) {
|
|
399
|
+
const table = byTid(this, row.tid);
|
|
400
|
+
// The .NET store advances its sequence for every released row of a known table, whether or not it moved.
|
|
401
|
+
if (!table)
|
|
402
|
+
continue;
|
|
403
|
+
if (await revocation.disposeReleasedRow(this, connection, table, row.key, reason, key, report))
|
|
404
|
+
removed++;
|
|
405
|
+
await connection.exec('UPDATE g9_clock SET seq = seq + 1 WHERE id = 1');
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
this.log('information', 20104, `Removed subscription ${key}; deleted ${removed} rows no other subscription held`);
|
|
409
|
+
}
|
|
410
|
+
await connection.exec(keepSubscription
|
|
411
|
+
? 'UPDATE g9_subscriptions SET cursor = NULL, last_success_utc = NULL, last_reconcile_utc = NULL WHERE key = $k'
|
|
412
|
+
: 'DELETE FROM g9_subscriptions WHERE key = $k', { $k: key });
|
|
413
|
+
await connection.exec('DROP TABLE temp.g9_affected; UPDATE g9_ctx SET applying = 0 WHERE id = 1');
|
|
414
|
+
}));
|
|
415
|
+
}
|
|
416
|
+
async updateSubscription(key, update) {
|
|
417
|
+
const sets = [];
|
|
418
|
+
const params = { $k: key };
|
|
419
|
+
if (update.cursor !== undefined && update.cursor !== null) {
|
|
420
|
+
sets.push('cursor = $cursor');
|
|
421
|
+
params.$cursor = update.cursor;
|
|
422
|
+
}
|
|
423
|
+
if (update.clearCursor)
|
|
424
|
+
sets.push('cursor = NULL');
|
|
425
|
+
if (update.definitionHash !== undefined && update.definitionHash !== null) {
|
|
426
|
+
sets.push('definition_hash = $def');
|
|
427
|
+
params.$def = update.definitionHash;
|
|
428
|
+
}
|
|
429
|
+
if (update.authHash !== undefined && update.authHash !== null) {
|
|
430
|
+
sets.push('auth_hash = $auth');
|
|
431
|
+
params.$auth = update.authHash;
|
|
432
|
+
}
|
|
433
|
+
if (update.state !== undefined && update.state !== null) {
|
|
434
|
+
sets.push('state = $state');
|
|
435
|
+
params.$state = update.state;
|
|
436
|
+
}
|
|
437
|
+
if (update.errorCode !== undefined && update.errorCode !== null) {
|
|
438
|
+
sets.push('error_code = $error');
|
|
439
|
+
params.$error = update.errorCode;
|
|
440
|
+
}
|
|
441
|
+
if (update.clearError)
|
|
442
|
+
sets.push('error_code = NULL');
|
|
443
|
+
const now = nowMs();
|
|
444
|
+
if (update.succeeded)
|
|
445
|
+
sets.push('last_success_utc = ' + now);
|
|
446
|
+
if (update.reconciled)
|
|
447
|
+
sets.push('last_reconcile_utc = ' + now);
|
|
448
|
+
if (update.accessed)
|
|
449
|
+
sets.push('last_access_utc = ' + now);
|
|
450
|
+
if (sets.length === 0)
|
|
451
|
+
return;
|
|
452
|
+
await this._open();
|
|
453
|
+
await this.writeLock.run(() => this.writer().exec('UPDATE g9_subscriptions SET ' + sets.join(', ') + ' WHERE key = $k', params));
|
|
454
|
+
}
|
|
455
|
+
async collectExpiredEphemeral(utcNowMs) {
|
|
456
|
+
await this._open();
|
|
457
|
+
const expired = (await this.reader().query('SELECT key FROM g9_subscriptions WHERE ephemeral = 1 AND ttl_seconds IS NOT NULL AND coalesce(last_access_utc, created_utc) + ttl_seconds * 1000 < $now', { $now: utcNowMs })).map((r) => asString(r[0]));
|
|
458
|
+
for (const key of expired)
|
|
459
|
+
await this._removeSubscription(key, G9EMoveOutPolicy.Delete, G9ERevocationReason.Expired, false, null);
|
|
460
|
+
return expired.length;
|
|
461
|
+
}
|
|
462
|
+
// ================================================================ push
|
|
463
|
+
buildPushBatch(tables, maxRows, maxBytes) {
|
|
464
|
+
return push.buildPushBatch(this, tables, maxRows, maxBytes);
|
|
465
|
+
}
|
|
466
|
+
forgetUploadedRows(localTable, keys) {
|
|
467
|
+
return push.forgetUploadedRows(this, localTable, keys);
|
|
468
|
+
}
|
|
469
|
+
applyPushResult(batch, result, signal) {
|
|
470
|
+
return push.applyPushResult(this, batch, result, signal);
|
|
471
|
+
}
|
|
472
|
+
// ================================================================ pull
|
|
473
|
+
async getStagedPull() {
|
|
474
|
+
await this._open();
|
|
475
|
+
return query.getStagedPull(this, null);
|
|
476
|
+
}
|
|
477
|
+
async getStagedPullForGeneration(generation) {
|
|
478
|
+
await this._open();
|
|
479
|
+
return query.getStagedPull(this, generation);
|
|
480
|
+
}
|
|
481
|
+
beginStagedPull(pull) {
|
|
482
|
+
return query.beginStagedPull(this, pull);
|
|
483
|
+
}
|
|
484
|
+
pruneStagedPulls(olderThanUtcMs, maxBytes, keepGeneration) {
|
|
485
|
+
return query.pruneStagedPulls(this, olderThanUtcMs, maxBytes, keepGeneration);
|
|
486
|
+
}
|
|
487
|
+
discardAllStagedPulls() {
|
|
488
|
+
return query.discardAllStagedPulls(this);
|
|
489
|
+
}
|
|
490
|
+
stageChunk(runId, chunk) {
|
|
491
|
+
return query.stageChunk(this, runId, chunk, (connection) => apply.downloadDurability(this, connection));
|
|
492
|
+
}
|
|
493
|
+
getResumePoint(runId) {
|
|
494
|
+
return query.getResumePoint(this, runId);
|
|
495
|
+
}
|
|
496
|
+
applyStagedPull(runId, rowsApplied, slices, signal) {
|
|
497
|
+
return apply.applyStagedPull(this, runId, rowsApplied, slices, query.readStagedRun, signal);
|
|
498
|
+
}
|
|
499
|
+
discardStagedPull(runId) {
|
|
500
|
+
return query.discardStagedPull(this, runId);
|
|
501
|
+
}
|
|
502
|
+
// ================================================================ reconcile
|
|
503
|
+
computeDigest(subscriptionKey, table) {
|
|
504
|
+
return query.computeDigest(this, subscriptionKey, table);
|
|
505
|
+
}
|
|
506
|
+
readLocalEntries(subscriptionKey, table, buckets, serverKeys) {
|
|
507
|
+
return query.readLocalEntries(this, subscriptionKey, table, buckets, serverKeys);
|
|
508
|
+
}
|
|
509
|
+
applyReconcile(subscriptionKey, table, fetchedFrames, addMembership, moveOut, policy, signal) {
|
|
510
|
+
return apply.applyReconcile(this, subscriptionKey, table, fetchedFrames, addMembership, moveOut, policy, signal);
|
|
511
|
+
}
|
|
512
|
+
// ================================================================ queries and maintenance
|
|
513
|
+
getPendingSummary() {
|
|
514
|
+
return query.getPendingSummary(this);
|
|
515
|
+
}
|
|
516
|
+
enqueueCommand(request, applyLocally, signal) {
|
|
517
|
+
return commands.enqueueCommand(this, request, applyLocally, signal);
|
|
518
|
+
}
|
|
519
|
+
/**
|
|
520
|
+
* Records a command on a connection the APP owns (its own driver session, inside its own transaction or not), so the
|
|
521
|
+
* command and the effects the app writes in `applyLocally` commit or roll back together (the .NET overload that takes the
|
|
522
|
+
* app's `sqlite3` handle). The session must be open on this store's database file.
|
|
523
|
+
*/
|
|
524
|
+
enqueueCommandOnSession(session, request, applyLocally) {
|
|
525
|
+
var _a;
|
|
526
|
+
this.ensureInitialized();
|
|
527
|
+
const connection = new G9SqliteConnection(session.driver, session.session);
|
|
528
|
+
connection.tx = (_a = session.tx) !== null && _a !== void 0 ? _a : null;
|
|
529
|
+
return commands.enqueueCommandOnConnection(connection, request, applyLocally);
|
|
530
|
+
}
|
|
531
|
+
getQuarantined() {
|
|
532
|
+
return revocation.getQuarantined(this);
|
|
533
|
+
}
|
|
534
|
+
restoreQuarantined(localTable, key) {
|
|
535
|
+
return revocation.restoreQuarantined(this, localTable, key);
|
|
536
|
+
}
|
|
537
|
+
discardQuarantined(localTable, key) {
|
|
538
|
+
return revocation.discardQuarantined(this, localTable, key);
|
|
539
|
+
}
|
|
540
|
+
getRetained() {
|
|
541
|
+
return revocation.getRetained(this);
|
|
542
|
+
}
|
|
543
|
+
reevaluateRetained() {
|
|
544
|
+
return revocation.reevaluateRetained(this);
|
|
545
|
+
}
|
|
546
|
+
getCommands(state, limit) {
|
|
547
|
+
return commands.getCommands(this, state, limit);
|
|
548
|
+
}
|
|
549
|
+
readChangeFeed(localTable, afterSequence, limit) {
|
|
550
|
+
return query.readChangeFeed(this, localTable, afterSequence, limit);
|
|
551
|
+
}
|
|
552
|
+
getChangeSequence() {
|
|
553
|
+
return query.getChangeSequence(this);
|
|
554
|
+
}
|
|
555
|
+
getChangeFeedFloor() {
|
|
556
|
+
return query.getChangeFeedFloor(this);
|
|
557
|
+
}
|
|
558
|
+
getRejections() {
|
|
559
|
+
return query.getRejections(this);
|
|
560
|
+
}
|
|
561
|
+
retryRejected(localTable, key) {
|
|
562
|
+
return query.retryRejected(this, localTable, key);
|
|
563
|
+
}
|
|
564
|
+
markDirty(localTable, keys) {
|
|
565
|
+
return query.markDirty(this, localTable, keys);
|
|
566
|
+
}
|
|
567
|
+
adoptExistingRows() {
|
|
568
|
+
return query.adoptExistingRows(this);
|
|
569
|
+
}
|
|
570
|
+
saveRunReport(report, keep) {
|
|
571
|
+
return query.saveRunReport(this, report, keep);
|
|
572
|
+
}
|
|
573
|
+
getRecentRunReports(count) {
|
|
574
|
+
return query.getRecentRunReports(this, count);
|
|
575
|
+
}
|
|
576
|
+
getPhaseStatistics() {
|
|
577
|
+
return query.getPhaseStatistics(this);
|
|
578
|
+
}
|
|
579
|
+
recordPhaseStatistics(samples) {
|
|
580
|
+
return query.recordPhaseStatistics(this, samples);
|
|
581
|
+
}
|
|
582
|
+
purgeDeletedFeed(keep) {
|
|
583
|
+
return query.purgeDeletedFeed(this, keep);
|
|
584
|
+
}
|
|
585
|
+
/** The triggers in the database that the library did not create (they keep trigger execution on during applies). */
|
|
586
|
+
async getForeignTriggers() {
|
|
587
|
+
this.ensureInitialized();
|
|
588
|
+
return apply.foreignTriggerNames(this, this.reader());
|
|
589
|
+
}
|
|
590
|
+
// ================================================================ recovery (ADR-0068)
|
|
591
|
+
/** Runs SQLite's quick_check; an empty list means the file is sound. A file too damaged to check is one problem. */
|
|
592
|
+
async checkIntegrity() {
|
|
593
|
+
const problems = [];
|
|
594
|
+
try {
|
|
595
|
+
await this._open();
|
|
596
|
+
for (const r of await this.reader().query('PRAGMA quick_check(50)')) {
|
|
597
|
+
const line = asString(r[0]);
|
|
598
|
+
if (line.toLowerCase() !== 'ok')
|
|
599
|
+
problems.push(line);
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
catch (error) {
|
|
603
|
+
problems.push(error instanceof Error ? error.message : String(error));
|
|
604
|
+
}
|
|
605
|
+
if (problems.length > 0)
|
|
606
|
+
this.log('error', 20107, `The device database failed its integrity check with ${problems.length} problem(s); first: ${problems[0]}`);
|
|
607
|
+
return problems;
|
|
608
|
+
}
|
|
609
|
+
/**
|
|
610
|
+
* Hands every pending local change to `write`, one JSON line per row — `{"table":…,"key":…,"deleted":…,"values":{…}}` —
|
|
611
|
+
* best effort: a row that cannot be read is listed with its key and no values. Nothing in the database changes.
|
|
612
|
+
*/
|
|
613
|
+
async exportPendingChanges(write) {
|
|
614
|
+
this.ensureInitialized();
|
|
615
|
+
const connection = this.reader();
|
|
616
|
+
const pending = (await connection.query('SELECT tid, pk, dirty FROM g9_rows WHERE dirty <> 0 ORDER BY local_seq')).map((r) => ({
|
|
617
|
+
tid: asNumber(r[0]),
|
|
618
|
+
key: asString(r[1]),
|
|
619
|
+
deleted: asNumber(r[2]) === 2,
|
|
620
|
+
}));
|
|
621
|
+
let rows = 0;
|
|
622
|
+
let unreadable = 0;
|
|
623
|
+
for (const row of pending) {
|
|
624
|
+
const table = byTid(this, row.tid);
|
|
625
|
+
if (!table)
|
|
626
|
+
continue;
|
|
627
|
+
let values = null;
|
|
628
|
+
if (!row.deleted) {
|
|
629
|
+
try {
|
|
630
|
+
const read = await readLocalValues(connection, table, row.key);
|
|
631
|
+
if (Object.keys(read).length > 0)
|
|
632
|
+
values = serializeValues(read);
|
|
633
|
+
}
|
|
634
|
+
catch {
|
|
635
|
+
// The page this row lives on is one of the damaged ones. Its key is still worth keeping.
|
|
636
|
+
}
|
|
637
|
+
if (values === null)
|
|
638
|
+
unreadable++;
|
|
639
|
+
}
|
|
640
|
+
const line = '{"table":' + JSON.stringify(table.model.localName) + ',"key":' + JSON.stringify(row.key) + ',"deleted":' + (row.deleted ? 'true' : 'false') + (values === null ? '' : ',"values":' + values) + '}';
|
|
641
|
+
await write(line);
|
|
642
|
+
rows++;
|
|
643
|
+
}
|
|
644
|
+
if (rows > 0)
|
|
645
|
+
this.log('warning', 20108, `Exported ${rows} pending row(s) (${unreadable} without readable values)`);
|
|
646
|
+
return { rows, unreadable };
|
|
647
|
+
}
|
|
648
|
+
async dispose() {
|
|
649
|
+
const writer = this._writer;
|
|
650
|
+
const reader = this._reader;
|
|
651
|
+
this._writer = null;
|
|
652
|
+
this._reader = null;
|
|
653
|
+
this._opening = null;
|
|
654
|
+
if (writer) {
|
|
655
|
+
await writer.rollback();
|
|
656
|
+
await this.options.driver.close(writer.session);
|
|
657
|
+
}
|
|
658
|
+
if (reader)
|
|
659
|
+
await this.options.driver.close(reader.session);
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
/** Long values the store exposes for tests and tools. */
|
|
663
|
+
export { G9Long };
|
|
664
|
+
//# sourceMappingURL=store.js.map
|