@delali/sirannon-db 0.3.5-next.69 → 0.3.5-next.72
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/README.md +2 -0
- package/dist/backup/index.d.ts +5 -5
- package/dist/backup/index.mjs +4 -3
- package/dist/backup-scheduler/index.d.ts +2 -2
- package/dist/backup-scheduler/index.mjs +5 -4
- package/dist/{change-tracker-DqCdeEyx.d.ts → change-tracker-Dpo99EI-.d.ts} +24 -7
- package/dist/{chunk-ADNWEHGZ.mjs → chunk-3VTT3VU6.mjs} +15 -7
- package/dist/chunk-75IBRE73.mjs +55 -0
- package/dist/{chunk-KJLNYOOL.mjs → chunk-AHCMO4YL.mjs} +19 -10
- package/dist/chunk-AP4367HK.mjs +1080 -0
- package/dist/chunk-BGJ5JVYL.mjs +62 -0
- package/dist/{chunk-NVSQ2T5Q.mjs → chunk-BTUD6XQO.mjs} +16 -5
- package/dist/{chunk-R7GBXNTO.mjs → chunk-BUWJH3D3.mjs} +2 -55
- package/dist/chunk-D3YF2G7X.mjs +147 -0
- package/dist/chunk-DBN57KVR.mjs +171 -0
- package/dist/chunk-DDIHBUEN.mjs +240 -0
- package/dist/{chunk-FJPR2CJX.mjs → chunk-DKML5425.mjs} +2 -2
- package/dist/chunk-E2GRX4ZD.mjs +30 -0
- package/dist/{chunk-NZGSDALX.mjs → chunk-G4AG3ESO.mjs} +217 -76
- package/dist/{chunk-QBBLSQHQ.mjs → chunk-HO2XTXVX.mjs} +1 -8
- package/dist/chunk-ISDT4RBD.mjs +149 -0
- package/dist/{chunk-IEKIYO4V.mjs → chunk-JAQ2I4OT.mjs} +5 -5
- package/dist/{chunk-2O3PP4CF.mjs → chunk-JFVEGN7J.mjs} +44 -18
- package/dist/{chunk-2ES3DT4A.mjs → chunk-JZL64CZZ.mjs} +66 -6
- package/dist/chunk-K43WJORM.mjs +23 -0
- package/dist/{chunk-55YRLSM2.mjs → chunk-KOTBP4AW.mjs} +27 -4
- package/dist/{chunk-3BZLZ47R.mjs → chunk-L6EX6K27.mjs} +2 -2
- package/dist/chunk-PJRCMG7L.mjs +232 -0
- package/dist/{chunk-HR7K3GZW.mjs → chunk-RLBKZZ24.mjs} +5 -1
- package/dist/{chunk-ESTZZPT5.mjs → chunk-RPWBYASV.mjs} +391 -33
- package/dist/chunk-SB3MIFLG.mjs +1374 -0
- package/dist/chunk-SEL4A4WT.mjs +156 -0
- package/dist/{chunk-6AOJCNRU.mjs → chunk-UHUDPQFI.mjs} +3 -3
- package/dist/{chunk-4KASJKKK.mjs → chunk-UIDQGPJY.mjs} +147 -6
- package/dist/{chunk-VO3ZWA2G.mjs → chunk-WXPAWGTE.mjs} +255 -46
- package/dist/chunk-ZFFUVYYE.mjs +439 -0
- package/dist/client/index.d.ts +26 -18
- package/dist/client/index.mjs +105 -17
- package/dist/client/topology.d.ts +12 -10
- package/dist/client/topology.mjs +17 -7
- package/dist/{client-base-BxTocU1x.d.ts → client-base-Doaqwrx3.d.ts} +36 -9
- package/dist/{client-streams-YKANXMWO.mjs → client-streams-MKPE4MXW.mjs} +6 -6
- package/dist/codegen/cli.mjs +3 -2
- package/dist/codegen/index.mjs +3 -2
- package/dist/core/index.d.ts +59 -16
- package/dist/core/index.mjs +1169 -438
- package/dist/core/writer-worker.mjs +57 -9
- package/dist/{database-backup-DcLhxoa1.d.ts → database-backup-BwTNiBm2.d.ts} +1 -1
- package/dist/{database-CGDsXW1K.d.ts → database-fz-8CJBP.d.ts} +365 -90
- package/dist/driver/better-sqlite3.d.ts +20 -2
- package/dist/driver/better-sqlite3.mjs +212 -36
- package/dist/driver/bun.d.ts +1 -1
- package/dist/driver/bun.mjs +21 -33
- package/dist/driver/expo.d.ts +1 -1
- package/dist/driver/expo.mjs +35 -5
- package/dist/driver/node.d.ts +1 -1
- package/dist/driver/node.mjs +90 -23
- package/dist/driver/wa-sqlite.d.ts +1 -1
- package/dist/driver/wa-sqlite.mjs +215 -13
- package/dist/file-migrations/index.d.ts +1 -1
- package/dist/file-migrations/index.mjs +13 -8
- package/dist/{forward-rpc-VRWFZXHN.mjs → forward-rpc-S7NMLBX7.mjs} +1 -1
- package/dist/{primary-wins-CHNOYuN7.d.ts → primary-wins-BnNAEjwB.d.ts} +7 -6
- package/dist/{protocol-DFpNNsIT.d.ts → protocol-DZSYSv0S.d.ts} +1 -1
- package/dist/replication/coordinator/etcd.d.ts +22 -5
- package/dist/replication/coordinator/etcd.mjs +349 -271
- package/dist/replication/index.d.ts +269 -41
- package/dist/replication/index.mjs +2197 -1155
- package/dist/{restore-options-CH8mE-Tb.d.ts → restore-options-CcMukOLO.d.ts} +1 -1
- package/dist/server/index.d.ts +55 -19
- package/dist/server/index.mjs +869 -310
- package/dist/{server-options-aYtCkefX.d.ts → server-options-B9CsAZp5.d.ts} +9 -7
- package/dist/{server-streams-24XL4T43.mjs → server-streams-SBYCHWWM.mjs} +36 -59
- package/dist/{sirannon-J3EgPVD7.d.ts → sirannon-D0CjDULa.d.ts} +2 -2
- package/dist/transport/grpc.d.ts +26 -7
- package/dist/transport/grpc.mjs +45 -23
- package/dist/transport/memory.d.ts +5 -5
- package/dist/transport/memory.mjs +9 -3
- package/dist/{types-DqODKeOq.d.ts → types-BG31TtrQ.d.ts} +17 -0
- package/dist/{types-BrAiDGOb.d.ts → types-BlZ9GG1a.d.ts} +33 -8
- package/dist/{types-Dc8oWlxj.d.ts → types-DTmi7pXp.d.ts} +198 -15
- package/dist/{types-guEKSzMr.d.ts → types-MczwKtul.d.ts} +214 -135
- package/package.json +18 -7
- package/dist/chunk-52XW3Z76.mjs +0 -256
- package/dist/chunk-A3PHCO6W.mjs +0 -645
- package/dist/chunk-ER7ODTDA.mjs +0 -23
- package/dist/chunk-GVYLZVUZ.mjs +0 -58
- package/dist/chunk-MVSES6MS.mjs +0 -22
- package/dist/chunk-ZN4HI5FK.mjs +0 -907
- package/dist/chunk-ZYWI45EN.mjs +0 -299
- package/dist/errors-CJUkQ-KL.d.ts +0 -199
|
@@ -0,0 +1,1080 @@
|
|
|
1
|
+
import { record, string, unknown, object, optional, nullable, picklist, safeParse, summarize } from './chunk-ZFFUVYYE.mjs';
|
|
2
|
+
import { withoutRowCounts, runWriterTransaction, queryMetered, execute, executeBatch } from './chunk-DDIHBUEN.mjs';
|
|
3
|
+
import { HLC, ensureMetaTable, selectMetaValue, IDENTIFIER_RE, attributeChangeSeqs, validateDdlSafety, insertDdlChange, extractDroppedTable, selectMaxChangeSeq, updateChangeStampsAfterSeqSql, selectOutboundHistoryChangesSql, selectOutboundChangesSql, selectNodeChangesAfterSeqSql, prepareUpsertNewerColumnVersion, canonicaliseForChecksum, upsertMetaValue, selectMaxColumnVersionHlcForRow, selectMaxRowChangeHlc, validateIdentifier, prepareDeleteRowColumnVersions, prepareUpsertColumnVersion, selectTableExists, selectAppliedSourceSeqsInRange, selectStagedPushTail, selectStagedPushSeqsInRange, deleteStagedPushChanges, prepareInsertStagedPushChange, selectStagedPushChangesAfter, insertAppliedChangesFromStagedPush, prepareInsertAppliedChange } from './chunk-RPWBYASV.mjs';
|
|
4
|
+
import { BatchValidationError, ConflictError, ReplicationError } from './chunk-BLMRNSUG.mjs';
|
|
5
|
+
import { CHANGES_TABLE, isReservedIdentifier, decodeTaggedValues, encodeTaggedValues } from './chunk-JFVEGN7J.mjs';
|
|
6
|
+
import { sha256Hex } from './chunk-GVCNMPOS.mjs';
|
|
7
|
+
import { lateRowCountsOf } from './chunk-K43WJORM.mjs';
|
|
8
|
+
import { rowCountsOf, totalRowCounts, addRowCounts } from './chunk-E2GRX4ZD.mjs';
|
|
9
|
+
import { invokeCallerCallback } from './chunk-HO2XTXVX.mjs';
|
|
10
|
+
import { SirannonError } from './chunk-KINDFBQP.mjs';
|
|
11
|
+
|
|
12
|
+
// src/core/metrics/outcomes.ts
|
|
13
|
+
var CODES_OF_AN_UNKNOWN_OUTCOME = /* @__PURE__ */ new Set([
|
|
14
|
+
"WRITER_WORKER_TIMEOUT",
|
|
15
|
+
"WRITER_WORKER_EXIT",
|
|
16
|
+
"WRITER_WORKER_ERROR",
|
|
17
|
+
"WRITER_WORKER_FATAL"
|
|
18
|
+
]);
|
|
19
|
+
var ROW_COUNTS_UNKNOWN = Object.freeze({});
|
|
20
|
+
function leavesOutcomeUnknown(error) {
|
|
21
|
+
const code = typeof error === "object" && error !== null ? error.code : void 0;
|
|
22
|
+
return typeof code === "string" && CODES_OF_AN_UNKNOWN_OUTCOME.has(code);
|
|
23
|
+
}
|
|
24
|
+
function failedStatementOutcome(error) {
|
|
25
|
+
return rowCountsOf(error) ?? (leavesOutcomeUnknown(error) ? ROW_COUNTS_UNKNOWN : {});
|
|
26
|
+
}
|
|
27
|
+
function readOutcome(result) {
|
|
28
|
+
return { rowsReturned: result.value.length, ...rowCountsOf(result) };
|
|
29
|
+
}
|
|
30
|
+
function readOneOutcome(result) {
|
|
31
|
+
return { rowsReturned: result.value === void 0 ? 0 : 1, ...rowCountsOf(result) };
|
|
32
|
+
}
|
|
33
|
+
function writeOutcome(result) {
|
|
34
|
+
return { changes: result.changes, ...rowCountsOf(result) };
|
|
35
|
+
}
|
|
36
|
+
function batchWriteOutcome(results) {
|
|
37
|
+
let changes = 0;
|
|
38
|
+
for (const result of results) changes += result.changes;
|
|
39
|
+
return { changes, ...totalRowCounts(results) };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// src/core/transaction.ts
|
|
43
|
+
function transactionClosed() {
|
|
44
|
+
return new SirannonError(
|
|
45
|
+
"This transaction has ended, so it executes no further statements; execute them in a new transaction",
|
|
46
|
+
"TRANSACTION_CLOSED"
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
var Transaction = class _Transaction {
|
|
50
|
+
constructor(conn) {
|
|
51
|
+
this.conn = conn;
|
|
52
|
+
}
|
|
53
|
+
_lastInsertRowId = 0;
|
|
54
|
+
meter = null;
|
|
55
|
+
ended = false;
|
|
56
|
+
/**
|
|
57
|
+
* Executes a read inside this transaction.
|
|
58
|
+
*
|
|
59
|
+
* @param sql - The statement to execute.
|
|
60
|
+
* @param params - The values to bind to the statement, named or positional.
|
|
61
|
+
* @returns The rows that the statement returns.
|
|
62
|
+
* @throws A `SirannonError` with code `TRANSACTION_CLOSED` once the transaction has ended.
|
|
63
|
+
*/
|
|
64
|
+
async query(sql, params) {
|
|
65
|
+
this.assertOpen();
|
|
66
|
+
const read = () => queryMetered(this.conn, sql, params);
|
|
67
|
+
const metered = this.meter ? await this.meter.track(sql, read, readOutcome, failedStatementOutcome, params) : await read();
|
|
68
|
+
return metered.value;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Executes one write inside this transaction.
|
|
72
|
+
*
|
|
73
|
+
* @param sql - The statement to execute.
|
|
74
|
+
* @param params - The values to bind to the statement, named or positional.
|
|
75
|
+
* @returns The number of rows that the statement changed, and the row id of the last inserted row.
|
|
76
|
+
* @throws A `SirannonError` with code `TRANSACTION_CLOSED` once the transaction has ended.
|
|
77
|
+
*/
|
|
78
|
+
async execute(sql, params) {
|
|
79
|
+
this.assertOpen();
|
|
80
|
+
const write = () => execute(this.conn, sql, params);
|
|
81
|
+
const result = this.meter ? await this.meter.track(sql, write, writeOutcome, failedStatementOutcome, params) : await write();
|
|
82
|
+
this._lastInsertRowId = result.lastInsertRowId;
|
|
83
|
+
return withoutRowCounts(result);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Executes one statement once for each parameter set, inside this transaction.
|
|
87
|
+
*
|
|
88
|
+
* @param sql - The statement to execute for each parameter set.
|
|
89
|
+
* @param paramsBatch - One parameter set for each execution.
|
|
90
|
+
* @returns One result per parameter set, in order.
|
|
91
|
+
* @throws A `SirannonError` with code `TRANSACTION_CLOSED` once the transaction has ended.
|
|
92
|
+
*/
|
|
93
|
+
async executeBatch(sql, paramsBatch) {
|
|
94
|
+
this.assertOpen();
|
|
95
|
+
const write = () => executeBatch(this.conn, sql, paramsBatch);
|
|
96
|
+
const results = this.meter ? await this.meter.track(sql, write, batchWriteOutcome, failedStatementOutcome) : await write();
|
|
97
|
+
if (results.length > 0) {
|
|
98
|
+
this._lastInsertRowId = results[results.length - 1].lastInsertRowId;
|
|
99
|
+
}
|
|
100
|
+
return results.map(withoutRowCounts);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* The row id that SQLite assigned to the last row that this transaction inserted.
|
|
104
|
+
*/
|
|
105
|
+
get lastInsertRowId() {
|
|
106
|
+
return this._lastInsertRowId;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Reports every statement that this transaction executes to `meter`, and returns this transaction.
|
|
110
|
+
*
|
|
111
|
+
* @param meter - The receiver of each statement, or `null` to report nothing.
|
|
112
|
+
* @returns This transaction.
|
|
113
|
+
* @internal
|
|
114
|
+
*/
|
|
115
|
+
withMeter(meter) {
|
|
116
|
+
this.meter = meter;
|
|
117
|
+
return this;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Ends this transaction, so that a handle that the caller's function leaks refuses every later call with `TRANSACTION_CLOSED`.
|
|
121
|
+
*
|
|
122
|
+
* @internal
|
|
123
|
+
*/
|
|
124
|
+
end() {
|
|
125
|
+
this.ended = true;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Refuses the call once this transaction has ended, so that a subclass can check the transaction before it
|
|
129
|
+
* executes any statement of its own.
|
|
130
|
+
*
|
|
131
|
+
* @throws A `SirannonError` with code `TRANSACTION_CLOSED` once the transaction has ended.
|
|
132
|
+
* @internal
|
|
133
|
+
*/
|
|
134
|
+
assertOpen() {
|
|
135
|
+
if (this.ended) throw transactionClosed();
|
|
136
|
+
}
|
|
137
|
+
/** @internal */
|
|
138
|
+
static async run(conn, fn, meter = null, runInTransaction = runWriterTransaction) {
|
|
139
|
+
return runInTransaction(conn, async (txConn) => {
|
|
140
|
+
const tx = new _Transaction(txConn).withMeter(meter);
|
|
141
|
+
try {
|
|
142
|
+
return await fn(tx);
|
|
143
|
+
} finally {
|
|
144
|
+
tx.end();
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
// src/core/metrics/collector.ts
|
|
151
|
+
function settleLateOutcome(known, counts) {
|
|
152
|
+
if (counts === null) return known;
|
|
153
|
+
let total = rowCountsOf(known);
|
|
154
|
+
for (const statement of counts) total = addRowCounts(total, statement);
|
|
155
|
+
const { rowsRead: _read, rowsWritten: _written, ...rest } = known;
|
|
156
|
+
return total === void 0 ? rest : { ...rest, ...total };
|
|
157
|
+
}
|
|
158
|
+
var MetricsCollector = class {
|
|
159
|
+
config;
|
|
160
|
+
constructor(config) {
|
|
161
|
+
this.config = config ?? {};
|
|
162
|
+
}
|
|
163
|
+
get tracksQueries() {
|
|
164
|
+
return this.config.onQueryComplete !== void 0;
|
|
165
|
+
}
|
|
166
|
+
async trackQuery(fn, context, measure, measureFailure, measureLate = settleLateOutcome) {
|
|
167
|
+
if (!this.config.onQueryComplete) {
|
|
168
|
+
return fn();
|
|
169
|
+
}
|
|
170
|
+
const start = performance.now();
|
|
171
|
+
let failed = false;
|
|
172
|
+
let outcome = {};
|
|
173
|
+
let late;
|
|
174
|
+
try {
|
|
175
|
+
const result = await fn();
|
|
176
|
+
if (measure) outcome = measure(result);
|
|
177
|
+
return result;
|
|
178
|
+
} catch (err) {
|
|
179
|
+
failed = true;
|
|
180
|
+
if (measureFailure) outcome = measureFailure(err);
|
|
181
|
+
late = lateRowCountsOf(err);
|
|
182
|
+
throw err;
|
|
183
|
+
} finally {
|
|
184
|
+
if (late === void 0) {
|
|
185
|
+
this.reportQuery(context, outcome, performance.now() - start, failed);
|
|
186
|
+
} else {
|
|
187
|
+
const known = outcome;
|
|
188
|
+
void late.then((counts) => this.reportQuery(context, measureLate(known, counts), performance.now() - start, true));
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
reportQuery(context, outcome, durationMs, failed) {
|
|
193
|
+
invokeCallerCallback(() => this.config.onQueryComplete?.({ ...context, ...outcome, durationMs, error: failed }));
|
|
194
|
+
}
|
|
195
|
+
trackConnection(metrics) {
|
|
196
|
+
invokeCallerCallback(
|
|
197
|
+
() => metrics.event === "open" ? this.config.onConnectionOpen?.(metrics) : this.config.onConnectionClose?.(metrics)
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
trackCDCEvent(metrics) {
|
|
201
|
+
invokeCallerCallback(() => this.config.onCDCEvent?.(metrics));
|
|
202
|
+
}
|
|
203
|
+
observeDispatch(databaseId) {
|
|
204
|
+
if (!this.config.onCDCEvent) return void 0;
|
|
205
|
+
return (event, subscriberCount) => {
|
|
206
|
+
this.trackCDCEvent({ databaseId, table: event.table, operation: event.type, subscriberCount });
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
get active() {
|
|
210
|
+
return !!(this.config.onQueryComplete || this.config.onConnectionOpen || this.config.onConnectionClose || this.config.onCDCEvent);
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
// src/core/sync/conflict/lww.ts
|
|
215
|
+
var LWWResolver = class {
|
|
216
|
+
/**
|
|
217
|
+
* Returns the incoming row when its stamp is higher, and the local row otherwise.
|
|
218
|
+
*
|
|
219
|
+
* @param ctx - The local and incoming versions of one row.
|
|
220
|
+
* @returns Which version to write.
|
|
221
|
+
*/
|
|
222
|
+
resolve(ctx) {
|
|
223
|
+
if (ctx.localHlc === null) {
|
|
224
|
+
return { action: "accept_remote" };
|
|
225
|
+
}
|
|
226
|
+
const cmp = HLC.compare(ctx.remoteHlc, ctx.localHlc);
|
|
227
|
+
if (cmp > 0) {
|
|
228
|
+
return { action: "accept_remote" };
|
|
229
|
+
}
|
|
230
|
+
if (cmp < 0) {
|
|
231
|
+
return { action: "keep_local" };
|
|
232
|
+
}
|
|
233
|
+
if (ctx.remoteChange.operation === "delete") {
|
|
234
|
+
return { action: "accept_remote" };
|
|
235
|
+
}
|
|
236
|
+
if (ctx.remoteChange.nodeId > (ctx.localChange?.nodeId ?? "")) {
|
|
237
|
+
return { action: "accept_remote" };
|
|
238
|
+
}
|
|
239
|
+
return { action: "keep_local" };
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
// src/core/sync/batch-plan.ts
|
|
244
|
+
var NO_SEQS = /* @__PURE__ */ new Set();
|
|
245
|
+
async function assertTablesExist(conn, changes) {
|
|
246
|
+
if (changes.some((change) => change.operation === "ddl")) return;
|
|
247
|
+
const tables = new Set(changes.map((change) => change.table));
|
|
248
|
+
for (const table of tables) {
|
|
249
|
+
if (!await selectTableExists(conn, table)) {
|
|
250
|
+
throw new BatchValidationError(`Table '${table}' does not exist on the receiving database`);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
function groupEntries(entries, openTxId, stagedTxId) {
|
|
255
|
+
const byTx = /* @__PURE__ */ new Map();
|
|
256
|
+
for (const entry of entries) {
|
|
257
|
+
const group = byTx.get(entry.change.txId);
|
|
258
|
+
if (group) {
|
|
259
|
+
group.push(entry);
|
|
260
|
+
} else {
|
|
261
|
+
byTx.set(entry.change.txId, [entry]);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return [...byTx].map(([txId, txEntries]) => ({
|
|
265
|
+
kind: txId === openTxId ? "stage" : txId === stagedTxId ? "complete" : "apply",
|
|
266
|
+
txId,
|
|
267
|
+
entries: txEntries
|
|
268
|
+
}));
|
|
269
|
+
}
|
|
270
|
+
async function planBatch(conn, batch, seqs, lastApplied) {
|
|
271
|
+
const source = batch.sourceNodeId;
|
|
272
|
+
const recorded = batch.fromSeq <= lastApplied ? await selectAppliedSourceSeqsInRange(conn, source, batch.fromSeq, lastApplied) : NO_SEQS;
|
|
273
|
+
let tail = batch.changeSeqs === void 0 ? null : await selectStagedPushTail(conn, source);
|
|
274
|
+
const staged = tail !== null && batch.fromSeq <= tail.maxSeq ? await selectStagedPushSeqsInRange(conn, source, batch.fromSeq, tail.maxSeq) : NO_SEQS;
|
|
275
|
+
const fresh = [];
|
|
276
|
+
for (const [index, change] of batch.changes.entries()) {
|
|
277
|
+
const changeSeqs = seqs[index];
|
|
278
|
+
if (changeSeqs.some((seq) => recorded.has(seq.toString()) || staged.has(seq.toString()))) continue;
|
|
279
|
+
fresh.push({ change, seqs: changeSeqs });
|
|
280
|
+
}
|
|
281
|
+
if (tail !== null && fresh.length > 0 && fresh[0].change.txId !== tail.txId) {
|
|
282
|
+
await deleteStagedPushChanges(conn, source);
|
|
283
|
+
tail = null;
|
|
284
|
+
}
|
|
285
|
+
const openTxId = batch.txContinues === true ? batch.changes[batch.changes.length - 1].txId : null;
|
|
286
|
+
if (openTxId !== null && batch.changes.some((change) => change.operation === "ddl")) {
|
|
287
|
+
throw new BatchValidationError("A batch whose transaction continues cannot carry a DDL change");
|
|
288
|
+
}
|
|
289
|
+
return {
|
|
290
|
+
skipped: batch.changes.length - fresh.length,
|
|
291
|
+
groups: groupEntries(fresh, openTxId, tail?.txId ?? null)
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// src/core/sync/checksum.ts
|
|
296
|
+
function computeChecksum(changes) {
|
|
297
|
+
return sha256Hex(canonicaliseForChecksum(changes));
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// src/core/sync/conflict/resolve-conflict.ts
|
|
301
|
+
async function resolveConflict(resolver, ctx) {
|
|
302
|
+
try {
|
|
303
|
+
const changeResolver = typeof resolver === "function" ? resolver(ctx.table) : resolver;
|
|
304
|
+
return await changeResolver.resolve(ctx);
|
|
305
|
+
} catch (err) {
|
|
306
|
+
if (err instanceof ConflictError) throw err;
|
|
307
|
+
const reason = err instanceof Error ? err.message : String(err);
|
|
308
|
+
throw new ConflictError(
|
|
309
|
+
`Conflict resolution failed for table '${ctx.table}' row '${ctx.rowId}': ${reason}`,
|
|
310
|
+
ctx.table,
|
|
311
|
+
ctx.rowId,
|
|
312
|
+
err
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// src/core/sync/hlc-store.ts
|
|
318
|
+
var HLC_CLOCK_META_KEY = "hlc_clock";
|
|
319
|
+
function isWellFormedHlc(candidate) {
|
|
320
|
+
if (candidate.length === 0) return false;
|
|
321
|
+
let decoded;
|
|
322
|
+
try {
|
|
323
|
+
decoded = HLC.decode(candidate);
|
|
324
|
+
} catch {
|
|
325
|
+
return false;
|
|
326
|
+
}
|
|
327
|
+
if (!Number.isInteger(decoded.wallMs) || decoded.wallMs < 0) return false;
|
|
328
|
+
if (!Number.isInteger(decoded.logical) || decoded.logical < 0) return false;
|
|
329
|
+
if (decoded.nodeId.length === 0) return false;
|
|
330
|
+
return true;
|
|
331
|
+
}
|
|
332
|
+
async function persistHlcClock(conn, encoded) {
|
|
333
|
+
await ensureMetaTable(conn);
|
|
334
|
+
await upsertMetaValue(conn, HLC_CLOCK_META_KEY, encoded);
|
|
335
|
+
}
|
|
336
|
+
async function loadPersistedHlc(conn) {
|
|
337
|
+
await ensureMetaTable(conn);
|
|
338
|
+
const value = await selectMetaValue(conn, HLC_CLOCK_META_KEY);
|
|
339
|
+
if (value === null || !isWellFormedHlc(value)) return null;
|
|
340
|
+
return value;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// src/core/sync/origins.ts
|
|
344
|
+
var REMOTE_ORIGIN_NODE_ID = "remote";
|
|
345
|
+
|
|
346
|
+
// src/core/sync/push-staging.ts
|
|
347
|
+
var STAGED_PUSH_READ_ROWS = 500;
|
|
348
|
+
var ROW_VALUES_SCHEMA = record(string(), unknown());
|
|
349
|
+
var STORED_CHANGE_SCHEMA = object({
|
|
350
|
+
table: string(),
|
|
351
|
+
operation: picklist(["insert", "update", "delete", "ddl"]),
|
|
352
|
+
rowId: string(),
|
|
353
|
+
primaryKey: ROW_VALUES_SCHEMA,
|
|
354
|
+
hlc: string(),
|
|
355
|
+
txId: string(),
|
|
356
|
+
nodeId: string(),
|
|
357
|
+
newData: nullable(ROW_VALUES_SCHEMA),
|
|
358
|
+
oldData: nullable(ROW_VALUES_SCHEMA),
|
|
359
|
+
ddlStatement: optional(string())
|
|
360
|
+
});
|
|
361
|
+
function encodeChange(change) {
|
|
362
|
+
return JSON.stringify({
|
|
363
|
+
...change,
|
|
364
|
+
primaryKey: encodeTaggedValues(change.primaryKey),
|
|
365
|
+
newData: change.newData === null ? null : encodeTaggedValues(change.newData),
|
|
366
|
+
oldData: change.oldData === null ? null : encodeTaggedValues(change.oldData)
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
function storedChangeError(sourceNodeId, detail) {
|
|
370
|
+
return new ReplicationError(
|
|
371
|
+
`A change that this server holds from device '${sourceNodeId}' is unreadable: ${detail}`,
|
|
372
|
+
"REPLICATION_ERROR",
|
|
373
|
+
{ sourceNodeId }
|
|
374
|
+
);
|
|
375
|
+
}
|
|
376
|
+
function decodeRowValues(sourceNodeId, values) {
|
|
377
|
+
const decoded = safeParse(ROW_VALUES_SCHEMA, decodeTaggedValues(values));
|
|
378
|
+
if (!decoded.success) throw storedChangeError(sourceNodeId, summarize(decoded.issues));
|
|
379
|
+
return decoded.output;
|
|
380
|
+
}
|
|
381
|
+
function decodeChange(sourceNodeId, stored) {
|
|
382
|
+
let json;
|
|
383
|
+
try {
|
|
384
|
+
json = JSON.parse(stored);
|
|
385
|
+
} catch (err) {
|
|
386
|
+
throw storedChangeError(sourceNodeId, err instanceof Error ? err.message : String(err));
|
|
387
|
+
}
|
|
388
|
+
const parsed = safeParse(STORED_CHANGE_SCHEMA, json);
|
|
389
|
+
if (!parsed.success) throw storedChangeError(sourceNodeId, summarize(parsed.issues));
|
|
390
|
+
const raw = parsed.output;
|
|
391
|
+
return {
|
|
392
|
+
...raw,
|
|
393
|
+
primaryKey: decodeRowValues(sourceNodeId, raw.primaryKey),
|
|
394
|
+
newData: raw.newData === null ? null : decodeRowValues(sourceNodeId, raw.newData),
|
|
395
|
+
oldData: raw.oldData === null ? null : decodeRowValues(sourceNodeId, raw.oldData)
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
async function stagePushedChanges(conn, sourceNodeId, entries) {
|
|
399
|
+
await conn.transaction(async (tx) => {
|
|
400
|
+
const stmt = await prepareInsertStagedPushChange(tx);
|
|
401
|
+
for (const entry of entries) {
|
|
402
|
+
await stmt.run(sourceNodeId, entry.seqs[0].toString(), entry.change.txId, encodeChange(entry.change));
|
|
403
|
+
}
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
async function recordAppliedSeqs(tx, sourceNodeId, seqs) {
|
|
407
|
+
if (seqs.length === 0) return;
|
|
408
|
+
const stmt = await prepareInsertAppliedChange(tx);
|
|
409
|
+
const appliedAt = Date.now() / 1e3;
|
|
410
|
+
for (const seq of seqs) {
|
|
411
|
+
await stmt.run(sourceNodeId, seq.toString(), appliedAt);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
async function completeStagedPush(tx, sourceNodeId, tailSeqs) {
|
|
415
|
+
await insertAppliedChangesFromStagedPush(tx, sourceNodeId, Date.now() / 1e3);
|
|
416
|
+
await recordAppliedSeqs(tx, sourceNodeId, tailSeqs);
|
|
417
|
+
await deleteStagedPushChanges(tx, sourceNodeId);
|
|
418
|
+
}
|
|
419
|
+
var StagedPushSource = class {
|
|
420
|
+
constructor(conn, sourceNodeId, tail) {
|
|
421
|
+
this.conn = conn;
|
|
422
|
+
this.sourceNodeId = sourceNodeId;
|
|
423
|
+
this.tail = tail;
|
|
424
|
+
}
|
|
425
|
+
stagedRead = false;
|
|
426
|
+
tailRead = false;
|
|
427
|
+
/**
|
|
428
|
+
* Returns the next page of changes after a sequence.
|
|
429
|
+
*
|
|
430
|
+
* @param afterSeq - The sequence of the last change that the applier has received.
|
|
431
|
+
* @returns The next changes, each with its sequence, or an empty array once the applier has received them all.
|
|
432
|
+
* @throws {@link ReplicationError} when a stored change is not the JSON of a change.
|
|
433
|
+
*/
|
|
434
|
+
async nextBatch(afterSeq) {
|
|
435
|
+
if (!this.stagedRead) {
|
|
436
|
+
const rows = await selectStagedPushChangesAfter(this.conn, this.sourceNodeId, afterSeq, STAGED_PUSH_READ_ROWS);
|
|
437
|
+
if (rows.length > 0) {
|
|
438
|
+
return rows.map((row) => ({
|
|
439
|
+
seq: BigInt(String(row.source_seq)),
|
|
440
|
+
change: decodeChange(this.sourceNodeId, row.change)
|
|
441
|
+
}));
|
|
442
|
+
}
|
|
443
|
+
this.stagedRead = true;
|
|
444
|
+
}
|
|
445
|
+
if (this.tailRead) return [];
|
|
446
|
+
this.tailRead = true;
|
|
447
|
+
return this.tail.map((entry) => ({ seq: entry.seqs[0], change: entry.change }));
|
|
448
|
+
}
|
|
449
|
+
};
|
|
450
|
+
|
|
451
|
+
// src/core/sync/row-lookup.ts
|
|
452
|
+
async function findRowByPk(tx, table, pkColumns, sourceData) {
|
|
453
|
+
const conditions = [];
|
|
454
|
+
const values = [];
|
|
455
|
+
for (const col of pkColumns) {
|
|
456
|
+
if (!validateIdentifier(col)) return void 0;
|
|
457
|
+
if (!(col in sourceData)) return void 0;
|
|
458
|
+
conditions.push(`"${col}" = ?`);
|
|
459
|
+
values.push(sourceData[col]);
|
|
460
|
+
}
|
|
461
|
+
if (conditions.length === 0) return void 0;
|
|
462
|
+
const stmt = await tx.prepare(`SELECT * FROM "${table}" WHERE ${conditions.join(" AND ")} LIMIT 1`);
|
|
463
|
+
return stmt.get(...values);
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
// src/core/sync/row-writer.ts
|
|
467
|
+
var ROW_TOMBSTONE_COLUMN = "";
|
|
468
|
+
var RowWriter = class {
|
|
469
|
+
constructor(pkResolver, changesTable) {
|
|
470
|
+
this.pkResolver = pkResolver;
|
|
471
|
+
this.changesTable = changesTable;
|
|
472
|
+
}
|
|
473
|
+
async findExistingRow(tx, change) {
|
|
474
|
+
if (!IDENTIFIER_RE.test(change.table)) return void 0;
|
|
475
|
+
const pkColumns = await this.pkResolver.forTable(change.table);
|
|
476
|
+
const result = await findRowByPk(tx, change.table, pkColumns, change.newData ?? change.oldData ?? {});
|
|
477
|
+
if (result) return result;
|
|
478
|
+
if (change.operation === "update" && change.oldData) {
|
|
479
|
+
return findRowByPk(tx, change.table, pkColumns, change.oldData);
|
|
480
|
+
}
|
|
481
|
+
return void 0;
|
|
482
|
+
}
|
|
483
|
+
async getLocalHlcForRow(tx, table, rowId) {
|
|
484
|
+
const versioned = await selectMaxColumnVersionHlcForRow(tx, table, rowId);
|
|
485
|
+
const logged = await selectMaxRowChangeHlc(tx, this.changesTable, table, rowId);
|
|
486
|
+
if (versioned === null) return logged;
|
|
487
|
+
if (logged === null) return versioned;
|
|
488
|
+
return HLC.compare(logged, versioned) > 0 ? logged : versioned;
|
|
489
|
+
}
|
|
490
|
+
getAbsentRowHlc(tx, table, rowId) {
|
|
491
|
+
return selectMaxColumnVersionHlcForRow(tx, table, rowId);
|
|
492
|
+
}
|
|
493
|
+
async recordTombstone(tx, change) {
|
|
494
|
+
const stmt = await prepareUpsertNewerColumnVersion(tx);
|
|
495
|
+
await stmt.run(change.table, change.rowId, ROW_TOMBSTONE_COLUMN, change.hlc, change.nodeId);
|
|
496
|
+
}
|
|
497
|
+
async insertRow(tx, change) {
|
|
498
|
+
if (!change.newData) return;
|
|
499
|
+
const columns = Object.keys(change.newData).filter(validateIdentifier);
|
|
500
|
+
if (columns.length === 0) return;
|
|
501
|
+
const placeholders = columns.map(() => "?").join(", ");
|
|
502
|
+
const colNames = columns.map((c) => `"${c}"`).join(", ");
|
|
503
|
+
const values = columns.map((c) => change.newData?.[c]);
|
|
504
|
+
const stmt = await tx.prepare(`INSERT INTO "${change.table}" (${colNames}) VALUES (${placeholders})`);
|
|
505
|
+
await stmt.run(...values);
|
|
506
|
+
}
|
|
507
|
+
async applyRemoteChange(tx, change) {
|
|
508
|
+
if (change.operation === "delete") {
|
|
509
|
+
await this.deleteRow(tx, change);
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
if (!change.newData) return;
|
|
513
|
+
const pkColumns = await this.pkResolver.forTable(change.table);
|
|
514
|
+
const sourceData = change.newData;
|
|
515
|
+
const wherePkSource = change.oldData ?? sourceData;
|
|
516
|
+
const setClauses = [];
|
|
517
|
+
const setValues = [];
|
|
518
|
+
const whereConditions = [];
|
|
519
|
+
const whereValues = [];
|
|
520
|
+
const pkSet = new Set(pkColumns);
|
|
521
|
+
for (const [col, val] of Object.entries(sourceData)) {
|
|
522
|
+
if (!validateIdentifier(col)) continue;
|
|
523
|
+
if (pkSet.has(col)) continue;
|
|
524
|
+
setClauses.push(`"${col}" = ?`);
|
|
525
|
+
setValues.push(val);
|
|
526
|
+
}
|
|
527
|
+
for (const col of pkColumns) {
|
|
528
|
+
if (!validateIdentifier(col)) continue;
|
|
529
|
+
whereConditions.push(`"${col}" = ?`);
|
|
530
|
+
whereValues.push(wherePkSource[col]);
|
|
531
|
+
}
|
|
532
|
+
if (setClauses.length === 0 || whereConditions.length === 0) return;
|
|
533
|
+
const stmt = await tx.prepare(
|
|
534
|
+
`UPDATE "${change.table}" SET ${setClauses.join(", ")} WHERE ${whereConditions.join(" AND ")}`
|
|
535
|
+
);
|
|
536
|
+
await stmt.run(...setValues, ...whereValues);
|
|
537
|
+
}
|
|
538
|
+
async applyAfterSameTransaction(tx, change, existingRow) {
|
|
539
|
+
if (change.operation === "delete" || !change.newData) {
|
|
540
|
+
await this.applyRemoteChange(tx, change);
|
|
541
|
+
await this.recordColumnVersions(tx, change, null);
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
const before = change.oldData;
|
|
545
|
+
const changed = {};
|
|
546
|
+
for (const [column, value] of Object.entries(change.newData)) {
|
|
547
|
+
if (before === null || canonicaliseForChecksum(value) !== canonicaliseForChecksum(before[column])) {
|
|
548
|
+
changed[column] = value;
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
await this.applyRemoteChange(tx, { ...change, newData: { ...existingRow, ...changed } });
|
|
552
|
+
await this.recordColumnVersions(tx, change, changed);
|
|
553
|
+
}
|
|
554
|
+
async applyMergedData(tx, change, mergedData) {
|
|
555
|
+
const pkColumns = await this.pkResolver.forTable(change.table);
|
|
556
|
+
const sourceData = change.newData ?? change.oldData ?? {};
|
|
557
|
+
const setClauses = [];
|
|
558
|
+
const setValues = [];
|
|
559
|
+
const whereConditions = [];
|
|
560
|
+
const whereValues = [];
|
|
561
|
+
for (const [col, val] of Object.entries(mergedData)) {
|
|
562
|
+
if (!validateIdentifier(col)) continue;
|
|
563
|
+
if (!pkColumns.includes(col)) {
|
|
564
|
+
setClauses.push(`"${col}" = ?`);
|
|
565
|
+
setValues.push(val);
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
for (const col of pkColumns) {
|
|
569
|
+
if (!validateIdentifier(col)) continue;
|
|
570
|
+
whereConditions.push(`"${col}" = ?`);
|
|
571
|
+
whereValues.push(sourceData[col]);
|
|
572
|
+
}
|
|
573
|
+
if (setClauses.length === 0 || whereConditions.length === 0) return;
|
|
574
|
+
const stmt = await tx.prepare(
|
|
575
|
+
`UPDATE "${change.table}" SET ${setClauses.join(", ")} WHERE ${whereConditions.join(" AND ")}`
|
|
576
|
+
);
|
|
577
|
+
await stmt.run(...setValues, ...whereValues);
|
|
578
|
+
}
|
|
579
|
+
async deleteRow(tx, change) {
|
|
580
|
+
const pkColumns = await this.pkResolver.forTable(change.table);
|
|
581
|
+
const sourceData = change.oldData ?? change.newData ?? {};
|
|
582
|
+
const conditions = [];
|
|
583
|
+
const values = [];
|
|
584
|
+
for (const col of pkColumns) {
|
|
585
|
+
if (!validateIdentifier(col)) continue;
|
|
586
|
+
conditions.push(`"${col}" = ?`);
|
|
587
|
+
values.push(sourceData[col]);
|
|
588
|
+
}
|
|
589
|
+
if (conditions.length === 0) return;
|
|
590
|
+
const stmt = await tx.prepare(`DELETE FROM "${change.table}" WHERE ${conditions.join(" AND ")}`);
|
|
591
|
+
await stmt.run(...values);
|
|
592
|
+
}
|
|
593
|
+
async recordColumnVersions(tx, change, data) {
|
|
594
|
+
if (change.operation === "delete") {
|
|
595
|
+
const delStmt = await prepareDeleteRowColumnVersions(tx);
|
|
596
|
+
await delStmt.run(change.table, change.rowId);
|
|
597
|
+
await this.recordTombstone(tx, change);
|
|
598
|
+
return;
|
|
599
|
+
}
|
|
600
|
+
if (!data) return;
|
|
601
|
+
const upsertStmt = await prepareUpsertColumnVersion(tx);
|
|
602
|
+
for (const col of Object.keys(data)) {
|
|
603
|
+
if (!validateIdentifier(col)) continue;
|
|
604
|
+
await upsertStmt.run(change.table, change.rowId, col, change.hlc, change.nodeId);
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
};
|
|
608
|
+
|
|
609
|
+
// src/core/sync/batch-applier.ts
|
|
610
|
+
var BatchApplier = class {
|
|
611
|
+
constructor(conn, localNodeId, hlc, pkResolver, getLastAppliedSeq, options = {}) {
|
|
612
|
+
this.conn = conn;
|
|
613
|
+
this.localNodeId = localNodeId;
|
|
614
|
+
this.hlc = hlc;
|
|
615
|
+
this.getLastAppliedSeq = getLastAppliedSeq;
|
|
616
|
+
this.options = options;
|
|
617
|
+
this.changesTable = options.changesTable ?? CHANGES_TABLE;
|
|
618
|
+
this.rows = new RowWriter(pkResolver, this.changesTable);
|
|
619
|
+
}
|
|
620
|
+
rows;
|
|
621
|
+
changesTable;
|
|
622
|
+
async applyBatch(batch, resolver) {
|
|
623
|
+
const expectedChecksum = computeChecksum(batch.changes);
|
|
624
|
+
if (batch.checksum !== expectedChecksum) {
|
|
625
|
+
throw new BatchValidationError(`Checksum mismatch: expected ${expectedChecksum}, got ${batch.checksum}`);
|
|
626
|
+
}
|
|
627
|
+
for (const change of batch.changes) {
|
|
628
|
+
if (change.operation !== "ddl") {
|
|
629
|
+
if (!IDENTIFIER_RE.test(change.table) || isReservedIdentifier(change.table)) {
|
|
630
|
+
throw new BatchValidationError(`Invalid table name: ${change.table}`);
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
const seqs = attributeChangeSeqs(batch);
|
|
635
|
+
await assertTablesExist(this.conn, batch.changes);
|
|
636
|
+
const lastApplied = await this.getLastAppliedSeq(batch.sourceNodeId);
|
|
637
|
+
if (batch.toSeq <= lastApplied) {
|
|
638
|
+
return { applied: 0, skipped: batch.changes.length, conflicts: 0, droppedTables: [] };
|
|
639
|
+
}
|
|
640
|
+
const plan = await planBatch(this.conn, batch, seqs, lastApplied);
|
|
641
|
+
const total = { applied: 0, skipped: plan.skipped, conflicts: 0, droppedTables: [] };
|
|
642
|
+
for (const group of plan.groups) {
|
|
643
|
+
const result = await this.applyPlannedGroup(batch.sourceNodeId, group, resolver);
|
|
644
|
+
total.applied += result.applied;
|
|
645
|
+
total.skipped += result.skipped;
|
|
646
|
+
total.conflicts += result.conflicts;
|
|
647
|
+
total.droppedTables.push(...result.droppedTables);
|
|
648
|
+
}
|
|
649
|
+
await this.mergeHlc(batch.hlcRange.max);
|
|
650
|
+
return total;
|
|
651
|
+
}
|
|
652
|
+
async applyPlannedGroup(sourceNodeId, group, resolver) {
|
|
653
|
+
if (group.kind === "stage") {
|
|
654
|
+
await stagePushedChanges(this.conn, sourceNodeId, group.entries);
|
|
655
|
+
return { applied: 0, skipped: 0, conflicts: 0, droppedTables: [] };
|
|
656
|
+
}
|
|
657
|
+
const seqs = group.entries.flatMap((entry) => entry.seqs);
|
|
658
|
+
if (group.kind === "complete") {
|
|
659
|
+
return this.applyStagedGroup({
|
|
660
|
+
source: new StagedPushSource(this.conn, sourceNodeId, group.entries),
|
|
661
|
+
resolver,
|
|
662
|
+
withinTx: (tx) => completeStagedPush(tx, sourceNodeId, seqs)
|
|
663
|
+
});
|
|
664
|
+
}
|
|
665
|
+
return this.applyGroup({
|
|
666
|
+
sourceNodeId,
|
|
667
|
+
txId: group.txId,
|
|
668
|
+
changes: group.entries.map((entry) => entry.change),
|
|
669
|
+
resolver,
|
|
670
|
+
withinTx: (tx) => recordAppliedSeqs(tx, sourceNodeId, seqs)
|
|
671
|
+
});
|
|
672
|
+
}
|
|
673
|
+
async applyGroup(options) {
|
|
674
|
+
const { sourceNodeId, txId, changes, resolver, withinTx } = options;
|
|
675
|
+
const droppedTables = [];
|
|
676
|
+
const result = await this.conn.transaction(async (tx) => {
|
|
677
|
+
const seqBefore = await this.selectMaxChangeSeq(tx);
|
|
678
|
+
if (this.options.beforeApply) {
|
|
679
|
+
await this.options.beforeApply(tx);
|
|
680
|
+
}
|
|
681
|
+
let applied = 0;
|
|
682
|
+
let skipped = 0;
|
|
683
|
+
let conflicts = 0;
|
|
684
|
+
const ddlChanges = changes.filter((c) => c.operation === "ddl");
|
|
685
|
+
const dataChanges = changes.filter((c) => c.operation !== "ddl");
|
|
686
|
+
for (const ddl of ddlChanges) {
|
|
687
|
+
const ddlSql = ddl.ddlStatement;
|
|
688
|
+
if (!ddlSql || !validateDdlSafety(ddlSql)) {
|
|
689
|
+
throw new BatchValidationError(`Unsafe or missing DDL statement: ${ddlSql ?? "none"}`);
|
|
690
|
+
}
|
|
691
|
+
await tx.exec(ddlSql);
|
|
692
|
+
if (this.options.appliedSchemaChanges === "logged") {
|
|
693
|
+
await insertDdlChange(tx, this.changesTable, {
|
|
694
|
+
ddlStatement: ddlSql,
|
|
695
|
+
nodeId: sourceNodeId,
|
|
696
|
+
txId: ddl.txId,
|
|
697
|
+
hlc: ddl.hlc
|
|
698
|
+
});
|
|
699
|
+
}
|
|
700
|
+
const droppedTable = extractDroppedTable(ddlSql);
|
|
701
|
+
if (droppedTable !== null) {
|
|
702
|
+
droppedTables.push(droppedTable);
|
|
703
|
+
}
|
|
704
|
+
if (this.options.tracker) {
|
|
705
|
+
await this.options.tracker.refreshAllTriggersUsingConnection(tx);
|
|
706
|
+
}
|
|
707
|
+
applied += 1;
|
|
708
|
+
}
|
|
709
|
+
const tally = { applied, skipped, conflicts, writtenRows: /* @__PURE__ */ new Set() };
|
|
710
|
+
for (const change of dataChanges) {
|
|
711
|
+
await this.applyDataChange(tx, change, resolver, tally);
|
|
712
|
+
}
|
|
713
|
+
applied = tally.applied;
|
|
714
|
+
skipped = tally.skipped;
|
|
715
|
+
conflicts = tally.conflicts;
|
|
716
|
+
await this.stampAppliedEcho(tx, seqBefore, sourceNodeId, txId, changes);
|
|
717
|
+
if (withinTx) {
|
|
718
|
+
await withinTx(tx);
|
|
719
|
+
}
|
|
720
|
+
return { applied, skipped, conflicts };
|
|
721
|
+
});
|
|
722
|
+
return { ...result, droppedTables };
|
|
723
|
+
}
|
|
724
|
+
/**
|
|
725
|
+
* Applies one staged transaction, reading its changes from `source` one batch
|
|
726
|
+
* at a time so that memory use stays bounded for a transaction of any size.
|
|
727
|
+
* The method writes every batch, the echo stamps, and the `withinTx` work
|
|
728
|
+
* inside one local transaction, so a failure at any step rolls all of it back.
|
|
729
|
+
*/
|
|
730
|
+
async applyStagedGroup(options) {
|
|
731
|
+
const { source, resolver, withinTx } = options;
|
|
732
|
+
return this.conn.transaction(async (tx) => {
|
|
733
|
+
const seqBefore = await this.selectMaxChangeSeq(tx);
|
|
734
|
+
if (this.options.beforeApply) {
|
|
735
|
+
await this.options.beforeApply(tx);
|
|
736
|
+
}
|
|
737
|
+
const tally = { applied: 0, skipped: 0, conflicts: 0, writtenRows: /* @__PURE__ */ new Set() };
|
|
738
|
+
let cursor = 0n;
|
|
739
|
+
let lastSeq = null;
|
|
740
|
+
let txId = "";
|
|
741
|
+
let sawFirst = false;
|
|
742
|
+
let sourceNodeId = REMOTE_ORIGIN_NODE_ID;
|
|
743
|
+
let maxHlc = "";
|
|
744
|
+
while (true) {
|
|
745
|
+
const batch = await source.nextBatch(cursor);
|
|
746
|
+
if (batch.length === 0) break;
|
|
747
|
+
for (const entry of batch) {
|
|
748
|
+
const change = entry.change;
|
|
749
|
+
if (change.operation === "ddl") {
|
|
750
|
+
throw new BatchValidationError("A staged device transaction cannot carry a DDL change");
|
|
751
|
+
}
|
|
752
|
+
if (!IDENTIFIER_RE.test(change.table) || isReservedIdentifier(change.table)) {
|
|
753
|
+
throw new BatchValidationError(`Invalid table name: ${change.table}`);
|
|
754
|
+
}
|
|
755
|
+
if (!sawFirst) {
|
|
756
|
+
txId = change.txId;
|
|
757
|
+
sawFirst = true;
|
|
758
|
+
} else if (change.txId !== txId) {
|
|
759
|
+
throw new BatchValidationError(`Staged rows span more than one transaction: '${txId}' and '${change.txId}'`);
|
|
760
|
+
}
|
|
761
|
+
if (sourceNodeId === REMOTE_ORIGIN_NODE_ID && change.nodeId !== "") {
|
|
762
|
+
sourceNodeId = change.nodeId;
|
|
763
|
+
}
|
|
764
|
+
if (change.hlc > maxHlc) {
|
|
765
|
+
maxHlc = change.hlc;
|
|
766
|
+
}
|
|
767
|
+
await this.applyDataChange(tx, change, resolver, tally);
|
|
768
|
+
cursor = entry.seq;
|
|
769
|
+
lastSeq = entry.seq;
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
await this.stampAppliedEchoStamps(tx, seqBefore, sourceNodeId, txId, maxHlc);
|
|
773
|
+
if (withinTx) {
|
|
774
|
+
await withinTx(tx);
|
|
775
|
+
}
|
|
776
|
+
const { applied, skipped, conflicts } = tally;
|
|
777
|
+
return { applied, skipped, conflicts, droppedTables: [], maxHlc, lastSeq };
|
|
778
|
+
});
|
|
779
|
+
}
|
|
780
|
+
async applyDataChange(tx, change, resolver, tally) {
|
|
781
|
+
if (isReservedIdentifier(change.table)) {
|
|
782
|
+
throw new BatchValidationError(`Invalid table name: ${change.table}`);
|
|
783
|
+
}
|
|
784
|
+
const rowKey = `${change.table}\0${change.rowId}`;
|
|
785
|
+
const existingRow = await this.rows.findExistingRow(tx, change);
|
|
786
|
+
if (existingRow === void 0) {
|
|
787
|
+
if (await this.applyToAbsentRow(tx, change, tally)) tally.writtenRows.add(rowKey);
|
|
788
|
+
return;
|
|
789
|
+
}
|
|
790
|
+
if (tally.writtenRows.has(rowKey)) {
|
|
791
|
+
await this.rows.applyAfterSameTransaction(tx, change, existingRow);
|
|
792
|
+
tally.applied += 1;
|
|
793
|
+
return;
|
|
794
|
+
}
|
|
795
|
+
tally.conflicts += 1;
|
|
796
|
+
const localHlc = await this.rows.getLocalHlcForRow(tx, change.table, change.rowId);
|
|
797
|
+
const localChange = {
|
|
798
|
+
table: change.table,
|
|
799
|
+
operation: "update",
|
|
800
|
+
rowId: change.rowId,
|
|
801
|
+
primaryKey: change.primaryKey,
|
|
802
|
+
hlc: localHlc ?? "",
|
|
803
|
+
txId: "",
|
|
804
|
+
nodeId: this.localNodeId,
|
|
805
|
+
newData: existingRow,
|
|
806
|
+
oldData: null
|
|
807
|
+
};
|
|
808
|
+
const resolution = await resolveConflict(resolver, {
|
|
809
|
+
table: change.table,
|
|
810
|
+
rowId: change.rowId,
|
|
811
|
+
localChange,
|
|
812
|
+
remoteChange: change,
|
|
813
|
+
localHlc,
|
|
814
|
+
remoteHlc: change.hlc
|
|
815
|
+
});
|
|
816
|
+
if (resolution.action === "accept_remote") {
|
|
817
|
+
await this.rows.applyRemoteChange(tx, change);
|
|
818
|
+
await this.rows.recordColumnVersions(tx, change, change.newData);
|
|
819
|
+
} else if (resolution.action === "merge" && resolution.mergedData) {
|
|
820
|
+
await this.rows.applyMergedData(tx, change, resolution.mergedData);
|
|
821
|
+
await this.rows.recordColumnVersions(tx, change, resolution.mergedData);
|
|
822
|
+
} else {
|
|
823
|
+
tally.skipped += 1;
|
|
824
|
+
return;
|
|
825
|
+
}
|
|
826
|
+
tally.applied += 1;
|
|
827
|
+
tally.writtenRows.add(rowKey);
|
|
828
|
+
}
|
|
829
|
+
async applyToAbsentRow(tx, change, tally) {
|
|
830
|
+
if (change.operation === "delete") {
|
|
831
|
+
await this.rows.recordTombstone(tx, change);
|
|
832
|
+
tally.applied += 1;
|
|
833
|
+
return true;
|
|
834
|
+
}
|
|
835
|
+
const deletedAt = await this.rows.getAbsentRowHlc(tx, change.table, change.rowId);
|
|
836
|
+
const wins = deletedAt === null ? change.operation === "insert" : HLC.compare(change.hlc, deletedAt) >= 0;
|
|
837
|
+
if (!wins || !change.newData) {
|
|
838
|
+
tally.skipped += 1;
|
|
839
|
+
return false;
|
|
840
|
+
}
|
|
841
|
+
await this.rows.insertRow(tx, change);
|
|
842
|
+
await this.rows.recordColumnVersions(tx, change, change.newData);
|
|
843
|
+
tally.applied += 1;
|
|
844
|
+
return true;
|
|
845
|
+
}
|
|
846
|
+
async mergeHlc(remoteHlc) {
|
|
847
|
+
try {
|
|
848
|
+
const merged = this.hlc.receive(remoteHlc);
|
|
849
|
+
await persistHlcClock(this.conn, merged);
|
|
850
|
+
} catch {
|
|
851
|
+
return;
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
async selectMaxChangeSeq(tx) {
|
|
855
|
+
return (await selectMaxChangeSeq(tx, this.changesTable)).toString();
|
|
856
|
+
}
|
|
857
|
+
async stampAppliedEcho(tx, seqBefore, sourceNodeId, txId, txChanges) {
|
|
858
|
+
let maxHlc = "";
|
|
859
|
+
for (const change of txChanges) {
|
|
860
|
+
if (change.hlc > maxHlc) {
|
|
861
|
+
maxHlc = change.hlc;
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
await this.stampAppliedEchoStamps(tx, seqBefore, sourceNodeId, txId, maxHlc);
|
|
865
|
+
}
|
|
866
|
+
async stampAppliedEchoStamps(tx, seqBefore, sourceNodeId, txId, maxHlc) {
|
|
867
|
+
const stmt = await tx.prepare(updateChangeStampsAfterSeqSql(this.changesTable));
|
|
868
|
+
await stmt.run(sourceNodeId, txId, maxHlc, seqBefore);
|
|
869
|
+
}
|
|
870
|
+
};
|
|
871
|
+
|
|
872
|
+
// src/core/sync/batch-reader.ts
|
|
873
|
+
var BatchReader = class {
|
|
874
|
+
constructor(conn, localNodeId, changesTable, pkResolver, excludeDdl = false, describeTransactions = false) {
|
|
875
|
+
this.conn = conn;
|
|
876
|
+
this.localNodeId = localNodeId;
|
|
877
|
+
this.changesTable = changesTable;
|
|
878
|
+
this.pkResolver = pkResolver;
|
|
879
|
+
this.excludeDdl = excludeDdl;
|
|
880
|
+
this.describeTransactions = describeTransactions;
|
|
881
|
+
}
|
|
882
|
+
async readBatch(afterSeq, batchSize, origins) {
|
|
883
|
+
const limit = this.describeTransactions ? batchSize + 1 : batchSize;
|
|
884
|
+
const rows = origins ? await (await this.conn.prepare(selectOutboundHistoryChangesSql(this.changesTable, origins.length))).all(
|
|
885
|
+
afterSeq.toString(),
|
|
886
|
+
...origins,
|
|
887
|
+
limit
|
|
888
|
+
) : await (await this.conn.prepare(selectOutboundChangesSql(this.changesTable, this.excludeDdl))).all(
|
|
889
|
+
afterSeq.toString(),
|
|
890
|
+
this.localNodeId,
|
|
891
|
+
limit
|
|
892
|
+
);
|
|
893
|
+
const next = rows.length > batchSize ? rows.pop() : void 0;
|
|
894
|
+
if (rows.length === 0) {
|
|
895
|
+
return null;
|
|
896
|
+
}
|
|
897
|
+
const changes = [];
|
|
898
|
+
let minHlc = rows[0].hlc;
|
|
899
|
+
let maxHlc = rows[0].hlc;
|
|
900
|
+
for (const row of rows) {
|
|
901
|
+
const operation = row.operation.toLowerCase();
|
|
902
|
+
const isDdl = operation === "ddl";
|
|
903
|
+
const rawNewData = row.new_data ? decodeTaggedValues(JSON.parse(row.new_data)) : null;
|
|
904
|
+
const rawOldData = row.old_data ? decodeTaggedValues(JSON.parse(row.old_data)) : null;
|
|
905
|
+
let ddlStatement;
|
|
906
|
+
let newData = rawNewData;
|
|
907
|
+
let oldData = rawOldData;
|
|
908
|
+
if (isDdl) {
|
|
909
|
+
const candidate = rawNewData?.ddlStatement;
|
|
910
|
+
if (typeof candidate === "string") {
|
|
911
|
+
ddlStatement = candidate;
|
|
912
|
+
}
|
|
913
|
+
newData = null;
|
|
914
|
+
oldData = null;
|
|
915
|
+
}
|
|
916
|
+
const pkColumns = isDdl ? [] : await this.pkResolver.forTable(row.table_name);
|
|
917
|
+
const primaryKey = {};
|
|
918
|
+
if (!isDdl) {
|
|
919
|
+
const sourceData = rawNewData ?? rawOldData ?? {};
|
|
920
|
+
for (const col of pkColumns) {
|
|
921
|
+
if (col in sourceData) {
|
|
922
|
+
primaryKey[col] = sourceData[col];
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
const change = {
|
|
927
|
+
table: row.table_name,
|
|
928
|
+
operation,
|
|
929
|
+
rowId: String(row.row_id),
|
|
930
|
+
primaryKey,
|
|
931
|
+
hlc: row.hlc,
|
|
932
|
+
txId: row.tx_id,
|
|
933
|
+
nodeId: row.node_id,
|
|
934
|
+
newData,
|
|
935
|
+
oldData
|
|
936
|
+
};
|
|
937
|
+
if (ddlStatement !== void 0) {
|
|
938
|
+
change.ddlStatement = ddlStatement;
|
|
939
|
+
}
|
|
940
|
+
changes.push(change);
|
|
941
|
+
if (HLC.compare(row.hlc, minHlc) < 0) {
|
|
942
|
+
minHlc = row.hlc;
|
|
943
|
+
}
|
|
944
|
+
if (HLC.compare(row.hlc, maxHlc) > 0) {
|
|
945
|
+
maxHlc = row.hlc;
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
const fromSeq = BigInt(rows[0].seq);
|
|
949
|
+
const toSeq = BigInt(rows[rows.length - 1].seq);
|
|
950
|
+
const checksum = computeChecksum(changes);
|
|
951
|
+
const batch = {
|
|
952
|
+
sourceNodeId: this.localNodeId,
|
|
953
|
+
batchId: `${this.localNodeId}-${fromSeq}-${toSeq}`,
|
|
954
|
+
fromSeq,
|
|
955
|
+
toSeq,
|
|
956
|
+
hlcRange: { min: minHlc, max: maxHlc },
|
|
957
|
+
changes,
|
|
958
|
+
checksum
|
|
959
|
+
};
|
|
960
|
+
if (this.describeTransactions) {
|
|
961
|
+
batch.changeSeqs = rows.map((row) => BigInt(row.seq));
|
|
962
|
+
if (next !== void 0 && next.tx_id !== "" && next.tx_id === rows[rows.length - 1].tx_id) {
|
|
963
|
+
batch.txContinues = true;
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
return batch;
|
|
967
|
+
}
|
|
968
|
+
};
|
|
969
|
+
|
|
970
|
+
// src/core/sync/stamp-ops.ts
|
|
971
|
+
async function recordLocalColumnVersions(tx, changesTable, localNodeId, afterSeq) {
|
|
972
|
+
const selectStmt = await tx.prepare(selectNodeChangesAfterSeqSql(changesTable));
|
|
973
|
+
const rows = await selectStmt.all(afterSeq.toString(), localNodeId);
|
|
974
|
+
for (const row of rows) {
|
|
975
|
+
if (row.operation === "DELETE") {
|
|
976
|
+
const tombstoneStmt = await prepareUpsertNewerColumnVersion(tx);
|
|
977
|
+
await tombstoneStmt.run(row.table_name, String(row.row_id), ROW_TOMBSTONE_COLUMN, row.hlc, row.node_id);
|
|
978
|
+
continue;
|
|
979
|
+
}
|
|
980
|
+
const oldData = row.old_data ? decodeTaggedValues(JSON.parse(row.old_data)) : {};
|
|
981
|
+
const newData = row.new_data ? decodeTaggedValues(JSON.parse(row.new_data)) : {};
|
|
982
|
+
const changedCols = [];
|
|
983
|
+
if (row.operation === "INSERT") {
|
|
984
|
+
for (const key of Object.keys(newData)) {
|
|
985
|
+
changedCols.push(key);
|
|
986
|
+
}
|
|
987
|
+
} else {
|
|
988
|
+
for (const key of Object.keys(newData)) {
|
|
989
|
+
if (canonicaliseForChecksum(newData[key]) !== canonicaliseForChecksum(oldData[key])) {
|
|
990
|
+
changedCols.push(key);
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
const upsertStmt = await prepareUpsertNewerColumnVersion(tx);
|
|
995
|
+
for (const col of changedCols) {
|
|
996
|
+
await upsertStmt.run(row.table_name, String(row.row_id), col, row.hlc, row.node_id);
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
var StampOps = class {
|
|
1001
|
+
constructor(localNodeId, hlc, changesTable) {
|
|
1002
|
+
this.localNodeId = localNodeId;
|
|
1003
|
+
this.hlc = hlc;
|
|
1004
|
+
this.changesTable = changesTable;
|
|
1005
|
+
}
|
|
1006
|
+
async stampChanges(tx, afterSeq, txId) {
|
|
1007
|
+
const hlcValue = this.hlc.now();
|
|
1008
|
+
const stmt = await tx.prepare(updateChangeStampsAfterSeqSql(this.changesTable));
|
|
1009
|
+
await stmt.run(this.localNodeId, txId, hlcValue, afterSeq.toString());
|
|
1010
|
+
await upsertMetaValue(tx, HLC_CLOCK_META_KEY, hlcValue);
|
|
1011
|
+
}
|
|
1012
|
+
updateColumnVersions(tx, afterSeq) {
|
|
1013
|
+
return recordLocalColumnVersions(tx, this.changesTable, this.localNodeId, afterSeq);
|
|
1014
|
+
}
|
|
1015
|
+
};
|
|
1016
|
+
|
|
1017
|
+
// src/core/metrics/statement-recorder.ts
|
|
1018
|
+
function rowCountsOnly(outcome) {
|
|
1019
|
+
if (outcome === ROW_COUNTS_UNKNOWN) return outcome;
|
|
1020
|
+
return outcome.rowsRead === void 0 ? {} : { rowsRead: outcome.rowsRead, rowsWritten: outcome.rowsWritten };
|
|
1021
|
+
}
|
|
1022
|
+
var StatementRecorder = class {
|
|
1023
|
+
/** The outcome of each statement that the transaction executed, in order. */
|
|
1024
|
+
outcomes = [];
|
|
1025
|
+
/**
|
|
1026
|
+
* Executes one statement through `op` and records the outcome that `measure` or `measureFailure` returns.
|
|
1027
|
+
*
|
|
1028
|
+
* @param _sql - The statement, which the recorder keeps no copy of.
|
|
1029
|
+
* @param op - The call that executes the statement.
|
|
1030
|
+
* @param measure - Returns the outcome of a statement that succeeds.
|
|
1031
|
+
* @param measureFailure - Returns the outcome of a statement that throws.
|
|
1032
|
+
* @returns The value that `op` resolves to.
|
|
1033
|
+
*/
|
|
1034
|
+
async track(_sql, op, measure, measureFailure) {
|
|
1035
|
+
try {
|
|
1036
|
+
const result = await op();
|
|
1037
|
+
this.outcomes.push(measure(result));
|
|
1038
|
+
return result;
|
|
1039
|
+
} catch (err) {
|
|
1040
|
+
this.outcomes.push(measureFailure(err));
|
|
1041
|
+
throw err;
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
/**
|
|
1045
|
+
* Returns the outcomes to report for each statement, keeping only the row counts when the transaction fails.
|
|
1046
|
+
*
|
|
1047
|
+
* @returns The outcomes for a transaction that commits and for one that fails.
|
|
1048
|
+
*/
|
|
1049
|
+
transactionOutcomes() {
|
|
1050
|
+
return {
|
|
1051
|
+
succeeded: () => this.outcomes,
|
|
1052
|
+
failed: () => this.outcomes.map(rowCountsOnly),
|
|
1053
|
+
failedLate: (counts) => this.failedLate(counts)
|
|
1054
|
+
};
|
|
1055
|
+
}
|
|
1056
|
+
failedLate(counts) {
|
|
1057
|
+
const outcomes = this.outcomes.map(rowCountsOnly);
|
|
1058
|
+
const last = this.outcomes.length - 1;
|
|
1059
|
+
if (last >= 0) outcomes[last] = rowCountsOnly(settleLateOutcome(this.outcomes[last], counts));
|
|
1060
|
+
return outcomes;
|
|
1061
|
+
}
|
|
1062
|
+
/**
|
|
1063
|
+
* Returns the summed row counts of every recorded statement, for a statement that executed once for each parameter set and failed.
|
|
1064
|
+
*
|
|
1065
|
+
* @param error - The error that the transaction raised, whose own counts Sirannon reports when the recorder holds no statement.
|
|
1066
|
+
* @param measureFailure - Returns the outcome of an error that no recorded statement raised.
|
|
1067
|
+
* @returns The total counts, or the unknown outcome when any statement's outcome is unknown.
|
|
1068
|
+
*/
|
|
1069
|
+
failedTotal(error, measureFailure) {
|
|
1070
|
+
if (this.outcomes.length === 0) return measureFailure(error);
|
|
1071
|
+
let total;
|
|
1072
|
+
for (const outcome of this.outcomes) {
|
|
1073
|
+
if (outcome === ROW_COUNTS_UNKNOWN) return ROW_COUNTS_UNKNOWN;
|
|
1074
|
+
total = addRowCounts(total, rowCountsOf(outcome));
|
|
1075
|
+
}
|
|
1076
|
+
return total ?? {};
|
|
1077
|
+
}
|
|
1078
|
+
};
|
|
1079
|
+
|
|
1080
|
+
export { BatchApplier, BatchReader, HLC_CLOCK_META_KEY, LWWResolver, MetricsCollector, REMOTE_ORIGIN_NODE_ID, ROW_COUNTS_UNKNOWN, StampOps, StatementRecorder, Transaction, batchWriteOutcome, failedStatementOutcome, isWellFormedHlc, leavesOutcomeUnknown, loadPersistedHlc, readOneOutcome, readOutcome, recordLocalColumnVersions, settleLateOutcome, writeOutcome };
|