@syncular/server 0.58.0 → 0.60.0
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 +49 -36
- package/dist/admin.d.ts +32 -20
- package/dist/admin.js +46 -40
- package/dist/blob-handlers.d.ts +25 -31
- package/dist/blob-handlers.js +63 -41
- package/dist/blob-store.d.ts +42 -24
- package/dist/blob-store.js +0 -0
- package/dist/bucket-blob-store.d.ts +12 -8
- package/dist/bucket-blob-store.js +54 -45
- package/dist/bucket-segment-store.js +18 -16
- package/dist/content-encoding.d.ts +5 -2
- package/dist/content-encoding.js +42 -13
- package/dist/context.d.ts +19 -5
- package/dist/context.js +19 -1
- package/dist/d1-storage.d.ts +14 -98
- package/dist/d1-storage.js +249 -1119
- package/dist/errors.js +16 -0
- package/dist/events.d.ts +36 -18
- package/dist/frame-bytes.js +24 -6
- package/dist/handler.js +503 -208
- package/dist/local-authority.d.ts +2 -0
- package/dist/local-authority.js +24 -12
- package/dist/object-bucket.d.ts +2 -0
- package/dist/operations-realtime.js +69 -50
- package/dist/operations.d.ts +5 -2
- package/dist/operations.js +20 -29
- package/dist/pg-executor-pglite.d.ts +5 -6
- package/dist/pg-executor-pglite.js +22 -14
- package/dist/pg-executor.d.ts +4 -1
- package/dist/postgres-storage.d.ts +24 -19
- package/dist/postgres-storage.js +420 -262
- package/dist/pull.d.ts +70 -47
- package/dist/pull.js +270 -301
- package/dist/push.d.ts +12 -3
- package/dist/push.js +361 -173
- package/dist/realtime.d.ts +85 -39
- package/dist/realtime.js +355 -219
- package/dist/relational-rows.d.ts +21 -66
- package/dist/relational-rows.js +28 -109
- package/dist/s3-blob-store.d.ts +11 -12
- package/dist/s3-blob-store.js +20 -13
- package/dist/s3-object-bucket.d.ts +8 -1
- package/dist/s3-object-bucket.js +16 -3
- package/dist/schema.d.ts +14 -0
- package/dist/schema.js +23 -0
- package/dist/scopes.d.ts +6 -2
- package/dist/scopes.js +22 -4
- package/dist/seed.js +10 -16
- package/dist/segment-download.d.ts +4 -1
- package/dist/segment-download.js +22 -6
- package/dist/signed-url.d.ts +2 -2
- package/dist/signed-url.js +9 -2
- package/dist/sigv4.d.ts +8 -2
- package/dist/sigv4.js +8 -4
- package/dist/sqlite-blob-store.d.ts +1 -1
- package/dist/sqlite-blob-store.js +23 -22
- package/dist/sqlite-dialect.d.ts +164 -84
- package/dist/sqlite-dialect.js +1145 -150
- package/dist/sqlite-lease-store.js +13 -18
- package/dist/sqlite-storage.d.ts +9 -62
- package/dist/sqlite-storage.js +303 -1123
- package/dist/storage-query.d.ts +30 -1
- package/dist/storage-query.js +68 -0
- package/dist/storage.d.ts +185 -63
- package/dist/storage.js +15 -0
- package/package.json +2 -2
- package/src/admin.ts +99 -57
- package/src/blob-handlers.ts +91 -72
- package/src/blob-store.ts +0 -0
- package/src/bucket-blob-store.ts +75 -48
- package/src/bucket-segment-store.ts +20 -15
- package/src/content-encoding.ts +48 -16
- package/src/context.ts +33 -6
- package/src/d1-storage.ts +350 -1700
- package/src/errors.ts +16 -0
- package/src/events.ts +37 -18
- package/src/frame-bytes.ts +26 -6
- package/src/handler.ts +654 -261
- package/src/local-authority.ts +27 -14
- package/src/object-bucket.ts +2 -0
- package/src/operations-realtime.ts +85 -61
- package/src/operations.ts +44 -42
- package/src/pg-executor-pglite.ts +27 -23
- package/src/pg-executor.ts +5 -1
- package/src/postgres-storage.ts +557 -354
- package/src/pull.ts +406 -440
- package/src/push.ts +466 -220
- package/src/realtime.ts +447 -290
- package/src/relational-rows.ts +43 -121
- package/src/s3-blob-store.ts +23 -13
- package/src/s3-object-bucket.ts +20 -2
- package/src/schema.ts +31 -0
- package/src/scopes.ts +23 -3
- package/src/seed.ts +13 -20
- package/src/segment-download.ts +33 -7
- package/src/signed-url.ts +10 -4
- package/src/sigv4.ts +17 -4
- package/src/sqlite-blob-store.ts +27 -26
- package/src/sqlite-dialect.ts +1851 -199
- package/src/sqlite-lease-store.ts +12 -23
- package/src/sqlite-storage.ts +390 -1821
- package/src/storage-query.ts +102 -0
- package/src/storage.ts +215 -67
package/dist/push.js
CHANGED
|
@@ -79,6 +79,17 @@ function conflictColumnsBitmap(columnCount, marked) {
|
|
|
79
79
|
}
|
|
80
80
|
return bytes;
|
|
81
81
|
}
|
|
82
|
+
/** §6.6: answer every not-yet-known blobId with one concurrent round of
|
|
83
|
+
* existence lookups and remember the answers for the rest of the commit. */
|
|
84
|
+
async function resolveBlobPresence(blobCtx, blobIds) {
|
|
85
|
+
const store = blobCtx.store;
|
|
86
|
+
if (store === undefined)
|
|
87
|
+
return;
|
|
88
|
+
const unknown = [...new Set(blobIds)].filter((blobId) => !blobCtx.presence.has(blobId));
|
|
89
|
+
await Promise.all(unknown.map(async (blobId) => {
|
|
90
|
+
blobCtx.presence.set(blobId, await store.has(blobCtx.partition, blobId));
|
|
91
|
+
}));
|
|
92
|
+
}
|
|
82
93
|
/**
|
|
83
94
|
* §6.7: run the table's write-validation hook, if configured, on the row
|
|
84
95
|
* that WILL persist (post scope-strip, post CRDT-merge — the values the
|
|
@@ -162,7 +173,9 @@ async function mergeCrdtColumns(table, values, storedValues, present, opIndex, m
|
|
|
162
173
|
}
|
|
163
174
|
return { changed };
|
|
164
175
|
}
|
|
165
|
-
async function applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blobCtx, crdtMergers, validators, partition, actorId
|
|
176
|
+
async function applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blobCtx, crdtMergers, validators, partition, actorId,
|
|
177
|
+
/** §2.2: the partition's delete floor, raised by this commit's deletes. */
|
|
178
|
+
deleteFloor) {
|
|
166
179
|
const table = schema.tables.get(op.table);
|
|
167
180
|
const wireTable = wireSchema.tables.get(op.table);
|
|
168
181
|
if (table === undefined || wireTable === undefined) {
|
|
@@ -171,11 +184,16 @@ async function applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blo
|
|
|
171
184
|
if (!resolved.ok) {
|
|
172
185
|
return errorRecord(opIndex, 'sync.forbidden', 'scope resolution failed (§3.4 step 4)');
|
|
173
186
|
}
|
|
187
|
+
if (op.rowId.includes('\u0000')) {
|
|
188
|
+
// §6.1: no store sees a rowId holding U+0000; PostgreSQL refuses it.
|
|
189
|
+
return errorRecord(opIndex, 'sync.invalid_request', 'rowId holds U+0000');
|
|
190
|
+
}
|
|
174
191
|
const stored = await tx.getRow(op.table, op.rowId);
|
|
175
192
|
if (op.op === 'rescope') {
|
|
176
193
|
// §2.2 explicit scope move. One stored row changes its scope values;
|
|
177
|
-
// apply rewrites it in place,
|
|
178
|
-
// delete under the old scopes plus the upsert under the new ones
|
|
194
|
+
// apply rewrites it in place, advances `server_version` by 1, and emits
|
|
195
|
+
// the delete under the old scopes plus the upsert under the new ones,
|
|
196
|
+
// both at the new version.
|
|
179
197
|
if (stored === undefined) {
|
|
180
198
|
return errorRecord(opIndex, 'sync.row_missing', 'rescope targets an absent row (§2.2)');
|
|
181
199
|
}
|
|
@@ -237,17 +255,31 @@ async function applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blo
|
|
|
237
255
|
if (blobCheck !== undefined)
|
|
238
256
|
return blobCheck;
|
|
239
257
|
const payload = encodeRow(table.columns, values);
|
|
258
|
+
// §2.2: the moved scope columns take the new version as their column
|
|
259
|
+
// version; every other column keeps its own (a row stored before column
|
|
260
|
+
// versions holds every column at its current version).
|
|
261
|
+
const movedVersion = stored.serverVersion + 1;
|
|
262
|
+
const storedVersions = decodeColumnVersions(stored.columnVersions);
|
|
263
|
+
const movedVersions = new Map();
|
|
264
|
+
for (let i = 0; i < table.columns.length; i++) {
|
|
265
|
+
const version = scopeIndexes.has(i) &&
|
|
266
|
+
!sameValue(values[i] ?? null, storedValues[i] ?? null)
|
|
267
|
+
? movedVersion
|
|
268
|
+
: (storedVersions.get(i) ??
|
|
269
|
+
(stored.columnVersions === undefined ? stored.serverVersion : 1));
|
|
270
|
+
if (version > 1)
|
|
271
|
+
movedVersions.set(i, version);
|
|
272
|
+
}
|
|
273
|
+
const columnVersions = encodeColumnVersions(movedVersions);
|
|
240
274
|
// The move clears any tombstone from an earlier incarnation: the row is
|
|
241
275
|
// live at the end of this commit, so the next move must not hit §5.2.
|
|
242
276
|
await tx.clearTombstone(op.table, op.rowId);
|
|
243
277
|
await tx.upsertRow(op.table, {
|
|
244
278
|
rowId: op.rowId,
|
|
245
|
-
serverVersion:
|
|
279
|
+
serverVersion: movedVersion,
|
|
246
280
|
scopes: extracted.scopes,
|
|
247
281
|
payload,
|
|
248
|
-
...(
|
|
249
|
-
? { columnVersions: stored.columnVersions }
|
|
250
|
-
: {}),
|
|
282
|
+
...(columnVersions !== undefined ? { columnVersions } : {}),
|
|
251
283
|
}, { opIndex });
|
|
252
284
|
const storedRow = toValidateRow(table.columns, storedValues);
|
|
253
285
|
return {
|
|
@@ -257,13 +289,14 @@ async function applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blo
|
|
|
257
289
|
table: op.table,
|
|
258
290
|
rowId: op.rowId,
|
|
259
291
|
op: 'delete',
|
|
292
|
+
rowVersion: movedVersion,
|
|
260
293
|
scopes: stored.scopes,
|
|
261
294
|
},
|
|
262
295
|
{
|
|
263
296
|
table: op.table,
|
|
264
297
|
rowId: op.rowId,
|
|
265
298
|
op: 'upsert',
|
|
266
|
-
rowVersion:
|
|
299
|
+
rowVersion: movedVersion,
|
|
267
300
|
scopes: extracted.scopes,
|
|
268
301
|
payload,
|
|
269
302
|
},
|
|
@@ -286,7 +319,7 @@ async function applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blo
|
|
|
286
319
|
row: toValidateRow(table.columns, values),
|
|
287
320
|
stored: storedRow,
|
|
288
321
|
storedServerVersion: stored.serverVersion,
|
|
289
|
-
nextServerVersion:
|
|
322
|
+
nextServerVersion: movedVersion,
|
|
290
323
|
},
|
|
291
324
|
],
|
|
292
325
|
staged: {
|
|
@@ -295,6 +328,7 @@ async function applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blo
|
|
|
295
328
|
rowId: op.rowId,
|
|
296
329
|
opIndex,
|
|
297
330
|
values,
|
|
331
|
+
present: table.columns.map(() => true),
|
|
298
332
|
stored,
|
|
299
333
|
},
|
|
300
334
|
};
|
|
@@ -346,6 +380,9 @@ async function applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blo
|
|
|
346
380
|
table: op.table,
|
|
347
381
|
rowId: op.rowId,
|
|
348
382
|
op: 'delete',
|
|
383
|
+
// §2.2: a delete advances the version by 1; its tombstone records
|
|
384
|
+
// it and the delete floor rises to it.
|
|
385
|
+
rowVersion: stored.serverVersion + 1,
|
|
349
386
|
scopes: stored.scopes,
|
|
350
387
|
},
|
|
351
388
|
],
|
|
@@ -509,41 +546,60 @@ async function applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blo
|
|
|
509
546
|
nextServerVersion: newVersion,
|
|
510
547
|
},
|
|
511
548
|
],
|
|
512
|
-
staged: {
|
|
549
|
+
staged: {
|
|
550
|
+
table,
|
|
551
|
+
op: 'upsert',
|
|
552
|
+
rowId: op.rowId,
|
|
553
|
+
opIndex,
|
|
554
|
+
values,
|
|
555
|
+
present,
|
|
556
|
+
stored,
|
|
557
|
+
},
|
|
513
558
|
};
|
|
514
559
|
}
|
|
515
|
-
// Insert path: no stored row (§5.2).
|
|
560
|
+
// Insert path: no stored row (§5.2). The authorization row is the payload
|
|
561
|
+
// plus the primary key (§3.4 step 2); a missing or empty scope column value
|
|
562
|
+
// ⇒ deny.
|
|
516
563
|
const values = sparse.map((value) => value ?? null);
|
|
564
|
+
const extracted = storedScopesForRow(table, values);
|
|
565
|
+
const insert = 'missing' in extracted
|
|
566
|
+
? errorRecord(opIndex, 'sync.forbidden', 'insert missing scope column value (§3.4)')
|
|
567
|
+
: authorizeWrite(table, extracted.scopes, resolved)
|
|
568
|
+
? extracted
|
|
569
|
+
: errorRecord(opIndex, 'sync.forbidden', 'insert denied by scope authorization (§3.4)');
|
|
517
570
|
if (op.baseVersion !== undefined && op.baseVersion !== 0) {
|
|
518
571
|
// Authorize the payload first so absence is not disclosed to actors
|
|
519
572
|
// without the scope; then §6.2: baseVersion ≠ 0, row absent.
|
|
520
|
-
|
|
521
|
-
if ('missing' in extractedFirst ||
|
|
522
|
-
!authorizeWrite(table, extractedFirst.scopes, resolved)) {
|
|
573
|
+
if ('kind' in insert) {
|
|
523
574
|
return errorRecord(opIndex, 'sync.forbidden', 'write denied by scope authorization (§3.4)');
|
|
524
575
|
}
|
|
525
576
|
return errorRecord(opIndex, 'sync.row_missing', 'upsert with baseVersion targets an absent row (§6.2)');
|
|
526
577
|
}
|
|
527
|
-
|
|
578
|
+
// §6.2: a payload that presents every scope column is authorized before
|
|
579
|
+
// the tombstone read, so the answer never discloses a deletion to an actor
|
|
580
|
+
// who could not write the row.
|
|
581
|
+
if (op.baseVersion === undefined &&
|
|
582
|
+
'kind' in insert &&
|
|
583
|
+
table.scopePatterns.every((pattern) => present[pattern.columnIndex])) {
|
|
584
|
+
return insert;
|
|
585
|
+
}
|
|
586
|
+
// §6.2: a tombstone whose stored scopes the actor fails reads as absent.
|
|
587
|
+
const tombstone = await tx.getTombstone(op.table, op.rowId);
|
|
588
|
+
const deleted = tombstone?.scopes === undefined ||
|
|
589
|
+
authorizeWrite(table, tombstone.scopes, resolved)
|
|
590
|
+
? tombstone
|
|
591
|
+
: undefined;
|
|
592
|
+
if (op.baseVersion === undefined && deleted !== undefined) {
|
|
528
593
|
// §5.2: a delete within the retention horizon beats an unversioned
|
|
529
594
|
// upsert; an explicit insert (baseVersion 0) recreates instead.
|
|
530
|
-
|
|
531
|
-
if (tombstoneSeq !== undefined) {
|
|
532
|
-
return errorRecord(opIndex, 'sync.row_deleted', 'a deleted row rejects an unversioned upsert within the retention horizon (§5.2)');
|
|
533
|
-
}
|
|
595
|
+
return errorRecord(opIndex, 'sync.row_deleted', 'a deleted row rejects an unversioned upsert within the retention horizon (§5.2)');
|
|
534
596
|
}
|
|
535
597
|
if (presentCount < table.columns.length) {
|
|
536
598
|
// §6.3: an insert requires every column present.
|
|
537
599
|
return errorRecord(opIndex, 'sync.row_missing', 'a partial payload targets an absent row (§6.3)');
|
|
538
600
|
}
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
// §3.4 step 2: a missing or empty scope column value ⇒ deny.
|
|
542
|
-
return errorRecord(opIndex, 'sync.forbidden', `insert missing scope column value for ${JSON.stringify(extracted.missing)} (§3.4)`);
|
|
543
|
-
}
|
|
544
|
-
if (!authorizeWrite(table, extracted.scopes, resolved)) {
|
|
545
|
-
return errorRecord(opIndex, 'sync.forbidden', 'insert denied by scope authorization (§3.4)');
|
|
546
|
-
}
|
|
601
|
+
if ('kind' in insert)
|
|
602
|
+
return insert;
|
|
547
603
|
// §5.10.3: on insert a crdt column merges against the empty document
|
|
548
604
|
// (stored = null) — normalizes the initial state through the merger.
|
|
549
605
|
const insertMerge = await mergeCrdtColumns(table, values, undefined, present, opIndex, crdtMergers);
|
|
@@ -562,16 +618,16 @@ async function applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blo
|
|
|
562
618
|
const insertReject = await runValidator(validators, table, 'upsert', op.rowId, values, undefined, opIndex, tx, partition, actorId);
|
|
563
619
|
if (insertReject !== undefined)
|
|
564
620
|
return insertReject;
|
|
565
|
-
// §
|
|
566
|
-
//
|
|
567
|
-
//
|
|
568
|
-
if (
|
|
621
|
+
// §2.2, §6.2: every insert starts one above the delete floor, over a
|
|
622
|
+
// tombstone or not; without a column-versions blob every column sits at
|
|
623
|
+
// that version.
|
|
624
|
+
if (tombstone !== undefined)
|
|
569
625
|
await tx.clearTombstone(op.table, op.rowId);
|
|
570
|
-
|
|
626
|
+
const serverVersion = deleteFloor + 1;
|
|
571
627
|
const newRow = {
|
|
572
628
|
rowId: op.rowId,
|
|
573
|
-
serverVersion
|
|
574
|
-
scopes:
|
|
629
|
+
serverVersion,
|
|
630
|
+
scopes: insert.scopes,
|
|
575
631
|
payload: insertPayload,
|
|
576
632
|
};
|
|
577
633
|
await tx.upsertRow(op.table, newRow, { opIndex });
|
|
@@ -582,8 +638,8 @@ async function applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blo
|
|
|
582
638
|
table: op.table,
|
|
583
639
|
rowId: op.rowId,
|
|
584
640
|
op: 'upsert',
|
|
585
|
-
rowVersion:
|
|
586
|
-
scopes:
|
|
641
|
+
rowVersion: serverVersion,
|
|
642
|
+
scopes: insert.scopes,
|
|
587
643
|
payload: insertPayload,
|
|
588
644
|
},
|
|
589
645
|
],
|
|
@@ -595,7 +651,7 @@ async function applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blo
|
|
|
595
651
|
rowId: op.rowId,
|
|
596
652
|
row: toValidateRow(table.columns, values),
|
|
597
653
|
stored: undefined,
|
|
598
|
-
nextServerVersion:
|
|
654
|
+
nextServerVersion: serverVersion,
|
|
599
655
|
},
|
|
600
656
|
],
|
|
601
657
|
staged: {
|
|
@@ -604,6 +660,7 @@ async function applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blo
|
|
|
604
660
|
rowId: op.rowId,
|
|
605
661
|
opIndex,
|
|
606
662
|
values,
|
|
663
|
+
present,
|
|
607
664
|
stored: undefined,
|
|
608
665
|
},
|
|
609
666
|
};
|
|
@@ -623,10 +680,11 @@ async function checkAndRecordBlobs(tx, table, rowId, values, opIndex, blobCtx) {
|
|
|
623
680
|
if (blobCtx.store === undefined) {
|
|
624
681
|
return errorRecord(opIndex, 'blob.not_found', 'row references a blob but the server has no blob store (§5.9.6)');
|
|
625
682
|
}
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
683
|
+
// Pushed blobIds were answered before the operation loop; a blobId the
|
|
684
|
+
// row keeps from its stored values is answered here.
|
|
685
|
+
await resolveBlobPresence(blobCtx, blobIds);
|
|
686
|
+
if (blobIds.some((blobId) => blobCtx.presence.get(blobId) !== true)) {
|
|
687
|
+
return errorRecord(opIndex, 'blob.not_found', 'push references a blob that has not been uploaded (§5.9.6)');
|
|
630
688
|
}
|
|
631
689
|
}
|
|
632
690
|
// Update the reference index for this row (empty set clears it, §5.9.4).
|
|
@@ -805,13 +863,22 @@ async function enforceReferences(tx, schema, resolved, staged, clientOpCount, ca
|
|
|
805
863
|
const changes = [];
|
|
806
864
|
const operations = [];
|
|
807
865
|
const originByIndex = [];
|
|
866
|
+
// A reference column counts as present when any staged upsert of the row
|
|
867
|
+
// presents it; its value is the row's final value (§6.11).
|
|
808
868
|
const finalByRow = new Map();
|
|
809
869
|
for (const write of staged) {
|
|
810
870
|
const rowKey = `${write.table.name}\u0000${write.rowId}`;
|
|
811
871
|
const existing = finalByRow.get(rowKey);
|
|
872
|
+
const presentReferences = new Set(existing?.presentReferences);
|
|
873
|
+
for (const reference of write.table.references) {
|
|
874
|
+
if (write.present?.[reference.columnIndex] === true) {
|
|
875
|
+
presentReferences.add(reference.columnIndex);
|
|
876
|
+
}
|
|
877
|
+
}
|
|
812
878
|
finalByRow.set(rowKey, {
|
|
813
879
|
first: existing?.first ?? write,
|
|
814
880
|
last: write,
|
|
881
|
+
presentReferences,
|
|
815
882
|
});
|
|
816
883
|
}
|
|
817
884
|
// One candidate-state read per parent table, not one per reference: a
|
|
@@ -819,10 +886,12 @@ async function enforceReferences(tx, schema, resolved, staged, clientOpCount, ca
|
|
|
819
886
|
// combatants, its chronicle) reads that parent once. Postgres binds the
|
|
820
887
|
// ids as one array per table; SQLite and D1 answer with one lookup per id.
|
|
821
888
|
const parentIds = new Map();
|
|
822
|
-
for (const { last: write } of finalByRow.values()) {
|
|
889
|
+
for (const { last: write, presentReferences } of finalByRow.values()) {
|
|
823
890
|
if (write.op !== 'upsert' || write.values === undefined)
|
|
824
891
|
continue;
|
|
825
892
|
for (const reference of write.table.references) {
|
|
893
|
+
if (!presentReferences.has(reference.columnIndex))
|
|
894
|
+
continue;
|
|
826
895
|
const value = write.values[reference.columnIndex];
|
|
827
896
|
if (value === null || value === undefined)
|
|
828
897
|
continue;
|
|
@@ -840,17 +909,25 @@ async function enforceReferences(tx, schema, resolved, staged, clientOpCount, ca
|
|
|
840
909
|
for (const [parentTable, ids] of parentIds) {
|
|
841
910
|
parents.set(parentTable, await readCandidateRows(tx, parentTable, [...ids]));
|
|
842
911
|
}
|
|
843
|
-
for (const { last: write } of finalByRow.values()) {
|
|
912
|
+
for (const { last: write, presentReferences } of finalByRow.values()) {
|
|
844
913
|
if (write.op !== 'upsert' || write.values === undefined)
|
|
845
914
|
continue;
|
|
846
915
|
for (const reference of write.table.references) {
|
|
916
|
+
if (!presentReferences.has(reference.columnIndex))
|
|
917
|
+
continue;
|
|
847
918
|
const value = write.values[reference.columnIndex];
|
|
848
919
|
if (value === null || value === undefined)
|
|
849
920
|
continue;
|
|
850
921
|
const parentRowId = renderScopeValue(value);
|
|
851
922
|
if (parentRowId === undefined)
|
|
852
923
|
continue;
|
|
853
|
-
|
|
924
|
+
// A parent the actor cannot authorize (§3.4, on its stored scopes) is
|
|
925
|
+
// an absent parent: the rejection below is identical either way.
|
|
926
|
+
const parent = parents.get(reference.parentTable)?.get(parentRowId);
|
|
927
|
+
const parentTable = schema.tables.get(reference.parentTable);
|
|
928
|
+
if (parent !== undefined &&
|
|
929
|
+
parentTable !== undefined &&
|
|
930
|
+
authorizeWrite(parentTable, parent.scopes, resolved)) {
|
|
854
931
|
continue;
|
|
855
932
|
}
|
|
856
933
|
return {
|
|
@@ -897,24 +974,17 @@ async function enforceReferences(tx, schema, resolved, staged, clientOpCount, ca
|
|
|
897
974
|
if (tx.scanRowsByIndex === undefined) {
|
|
898
975
|
throw new Error('storage transaction does not support declared-reference enforcement (scanRowsByIndex)');
|
|
899
976
|
}
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
const page = await tx.scanRowsByIndex({
|
|
977
|
+
if (reference.onDelete === 'RESTRICT') {
|
|
978
|
+
// One remaining child decides RESTRICT.
|
|
979
|
+
const blocking = await tx.scanRowsByIndex({
|
|
904
980
|
table: reference.table,
|
|
905
981
|
index: reference.index,
|
|
906
982
|
values: [parentKeyValue],
|
|
907
|
-
afterRowId,
|
|
908
|
-
limit:
|
|
983
|
+
afterRowId: null,
|
|
984
|
+
limit: 1,
|
|
909
985
|
});
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
break;
|
|
913
|
-
afterRowId = page[page.length - 1]?.rowId ?? null;
|
|
914
|
-
}
|
|
915
|
-
if (children.length === 0)
|
|
916
|
-
continue;
|
|
917
|
-
if (reference.onDelete === 'RESTRICT') {
|
|
986
|
+
if (blocking.length === 0)
|
|
987
|
+
continue;
|
|
918
988
|
return {
|
|
919
989
|
kind: 'terminate',
|
|
920
990
|
record: referenceRejection(item.originOpIndex, 'delete is blocked by a declared reference with ON DELETE RESTRICT (§6.11)', {
|
|
@@ -927,101 +997,120 @@ async function enforceReferences(tx, schema, resolved, staged, clientOpCount, ca
|
|
|
927
997
|
if (child === undefined) {
|
|
928
998
|
throw new Error(`unreachable: reference child table ${reference.table} is not compiled`);
|
|
929
999
|
}
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
1000
|
+
// Each page reads no more than the remaining cascade budget plus one
|
|
1001
|
+
// child, so a cascade past the cap stops after one extra row. A page is
|
|
1002
|
+
// applied before the next is read; keyset pagination by rowId is
|
|
1003
|
+
// unaffected by deleting or nulling rows already read.
|
|
1004
|
+
let afterRowId = null;
|
|
1005
|
+
for (;;) {
|
|
1006
|
+
const limit = Math.min(1_000, cascadeLimit - originByIndex.length + 1);
|
|
1007
|
+
const page = await tx.scanRowsByIndex({
|
|
1008
|
+
table: reference.table,
|
|
1009
|
+
index: reference.index,
|
|
1010
|
+
values: [parentKeyValue],
|
|
1011
|
+
afterRowId,
|
|
1012
|
+
limit,
|
|
1013
|
+
});
|
|
1014
|
+
for (const childRow of page) {
|
|
1015
|
+
if (!authorizeWrite(child, childRow.scopes, resolved)) {
|
|
1016
|
+
return errorRecord(item.originOpIndex, 'sync.forbidden', 'cascade denied by scope authorization (§3.4)');
|
|
1017
|
+
}
|
|
1018
|
+
if (reference.onDelete === 'CASCADE') {
|
|
1019
|
+
if (!schedule(child, childRow.rowId, item.originOpIndex, scheduled, queue)) {
|
|
1020
|
+
continue;
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
else if (scheduled.has(`${child.name}\u0000${childRow.rowId}`)) {
|
|
936
1024
|
continue;
|
|
937
1025
|
}
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
return {
|
|
944
|
-
kind: 'terminate',
|
|
945
|
-
record: referenceRejection(item.originOpIndex, 'declared-reference cascade exceeded the per-commit operation cap (§6.11)', { reason: 'cascade_limit' }),
|
|
946
|
-
};
|
|
947
|
-
}
|
|
948
|
-
const syntheticIndex = clientOpCount + originByIndex.length;
|
|
949
|
-
const storedValues = decodeRow(child.columns, childRow.payload);
|
|
950
|
-
if (reference.onDelete === 'CASCADE') {
|
|
951
|
-
const reject = await runValidator(validators, child, 'delete', childRow.rowId, undefined, storedValues, item.originOpIndex, tx, partition, actorId);
|
|
952
|
-
if (reject !== undefined) {
|
|
953
|
-
return { kind: 'terminate', record: reject.record };
|
|
1026
|
+
if (originByIndex.length >= cascadeLimit) {
|
|
1027
|
+
return {
|
|
1028
|
+
kind: 'terminate',
|
|
1029
|
+
record: referenceRejection(item.originOpIndex, 'declared-reference cascade exceeded the per-commit operation cap (§6.11)', { reason: 'cascade_limit' }),
|
|
1030
|
+
};
|
|
954
1031
|
}
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
1032
|
+
const syntheticIndex = clientOpCount + originByIndex.length;
|
|
1033
|
+
const storedValues = decodeRow(child.columns, childRow.payload);
|
|
1034
|
+
if (reference.onDelete === 'CASCADE') {
|
|
1035
|
+
const reject = await runValidator(validators, child, 'delete', childRow.rowId, undefined, storedValues, item.originOpIndex, tx, partition, actorId);
|
|
1036
|
+
if (reject !== undefined) {
|
|
1037
|
+
return { kind: 'terminate', record: reject.record };
|
|
1038
|
+
}
|
|
1039
|
+
await tx.deleteRow(child.name, childRow.rowId);
|
|
1040
|
+
changes.push({
|
|
1041
|
+
table: child.name,
|
|
1042
|
+
rowId: childRow.rowId,
|
|
1043
|
+
op: 'delete',
|
|
1044
|
+
rowVersion: childRow.serverVersion + 1,
|
|
1045
|
+
scopes: childRow.scopes,
|
|
1046
|
+
});
|
|
1047
|
+
operations.push({
|
|
1048
|
+
opIndex: syntheticIndex,
|
|
1049
|
+
op: 'delete',
|
|
1050
|
+
table: child.name,
|
|
1051
|
+
rowId: childRow.rowId,
|
|
1052
|
+
row: undefined,
|
|
1053
|
+
stored: toValidateRow(child.columns, storedValues),
|
|
1054
|
+
storedServerVersion: childRow.serverVersion,
|
|
1055
|
+
});
|
|
978
1056
|
}
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
1057
|
+
else {
|
|
1058
|
+
const values = [...storedValues];
|
|
1059
|
+
values[reference.columnIndex] = null;
|
|
1060
|
+
const reject = await runValidator(validators, child, 'upsert', childRow.rowId, values, storedValues, item.originOpIndex, tx, partition, actorId);
|
|
1061
|
+
if (reject !== undefined) {
|
|
1062
|
+
return { kind: 'terminate', record: reject.record };
|
|
1063
|
+
}
|
|
1064
|
+
const nextServerVersion = childRow.serverVersion + 1;
|
|
1065
|
+
// §6.2: the SET NULL upsert writes only the reference column, so
|
|
1066
|
+
// only that column's version moves.
|
|
1067
|
+
const storedVersions = decodeColumnVersions(childRow.columnVersions);
|
|
1068
|
+
const nextVersions = new Map();
|
|
1069
|
+
for (let i = 0; i < child.columns.length; i++) {
|
|
1070
|
+
const version = i === reference.columnIndex
|
|
1071
|
+
? nextServerVersion
|
|
1072
|
+
: (storedVersions.get(i) ??
|
|
1073
|
+
(childRow.columnVersions === undefined
|
|
1074
|
+
? childRow.serverVersion
|
|
1075
|
+
: 1));
|
|
1076
|
+
if (version > 1)
|
|
1077
|
+
nextVersions.set(i, version);
|
|
1078
|
+
}
|
|
1079
|
+
const payload = encodeRow(child.columns, values);
|
|
1080
|
+
const cascadeVersions = encodeColumnVersions(nextVersions);
|
|
1081
|
+
await tx.upsertRow(child.name, {
|
|
1082
|
+
rowId: childRow.rowId,
|
|
1083
|
+
serverVersion: nextServerVersion,
|
|
1084
|
+
scopes: childRow.scopes,
|
|
1085
|
+
payload,
|
|
1086
|
+
...(cascadeVersions !== undefined
|
|
1087
|
+
? { columnVersions: cascadeVersions }
|
|
1088
|
+
: {}),
|
|
1089
|
+
}, { opIndex: syntheticIndex });
|
|
1090
|
+
changes.push({
|
|
1091
|
+
table: child.name,
|
|
1092
|
+
rowId: childRow.rowId,
|
|
1093
|
+
op: 'upsert',
|
|
1094
|
+
rowVersion: nextServerVersion,
|
|
1095
|
+
scopes: childRow.scopes,
|
|
1096
|
+
payload,
|
|
1097
|
+
});
|
|
1098
|
+
operations.push({
|
|
1099
|
+
opIndex: syntheticIndex,
|
|
1100
|
+
op: 'upsert',
|
|
1101
|
+
table: child.name,
|
|
1102
|
+
rowId: childRow.rowId,
|
|
1103
|
+
row: toValidateRow(child.columns, values),
|
|
1104
|
+
stored: toValidateRow(child.columns, storedValues),
|
|
1105
|
+
storedServerVersion: childRow.serverVersion,
|
|
1106
|
+
nextServerVersion,
|
|
1107
|
+
});
|
|
993
1108
|
}
|
|
994
|
-
|
|
995
|
-
const cascadeVersions = encodeColumnVersions(nextVersions);
|
|
996
|
-
await tx.upsertRow(child.name, {
|
|
997
|
-
rowId: childRow.rowId,
|
|
998
|
-
serverVersion: nextServerVersion,
|
|
999
|
-
scopes: childRow.scopes,
|
|
1000
|
-
payload,
|
|
1001
|
-
...(cascadeVersions !== undefined
|
|
1002
|
-
? { columnVersions: cascadeVersions }
|
|
1003
|
-
: {}),
|
|
1004
|
-
}, { opIndex: syntheticIndex });
|
|
1005
|
-
changes.push({
|
|
1006
|
-
table: child.name,
|
|
1007
|
-
rowId: childRow.rowId,
|
|
1008
|
-
op: 'upsert',
|
|
1009
|
-
rowVersion: nextServerVersion,
|
|
1010
|
-
scopes: childRow.scopes,
|
|
1011
|
-
payload,
|
|
1012
|
-
});
|
|
1013
|
-
operations.push({
|
|
1014
|
-
opIndex: syntheticIndex,
|
|
1015
|
-
op: 'upsert',
|
|
1016
|
-
table: child.name,
|
|
1017
|
-
rowId: childRow.rowId,
|
|
1018
|
-
row: toValidateRow(child.columns, values),
|
|
1019
|
-
stored: toValidateRow(child.columns, storedValues),
|
|
1020
|
-
storedServerVersion: childRow.serverVersion,
|
|
1021
|
-
nextServerVersion,
|
|
1022
|
-
});
|
|
1109
|
+
originByIndex.push(item.originOpIndex);
|
|
1023
1110
|
}
|
|
1024
|
-
|
|
1111
|
+
if (page.length < limit)
|
|
1112
|
+
break;
|
|
1113
|
+
afterRowId = page[page.length - 1]?.rowId ?? null;
|
|
1025
1114
|
}
|
|
1026
1115
|
}
|
|
1027
1116
|
}
|
|
@@ -1039,10 +1128,11 @@ function resultFrame(clientCommitId, stored, replay) {
|
|
|
1039
1128
|
results: [...stored.results],
|
|
1040
1129
|
};
|
|
1041
1130
|
}
|
|
1042
|
-
function processedPushCommit(clientCommitId, stored, replayed) {
|
|
1131
|
+
function processedPushCommit(clientCommitId, stored, replayed, watermark) {
|
|
1043
1132
|
return {
|
|
1044
1133
|
frame: resultFrame(clientCommitId, stored, replayed),
|
|
1045
1134
|
replayed,
|
|
1135
|
+
...(watermark !== undefined ? { watermark } : {}),
|
|
1046
1136
|
...(stored.recordedAtMs !== undefined
|
|
1047
1137
|
? { recordedAtMs: stored.recordedAtMs }
|
|
1048
1138
|
: {}),
|
|
@@ -1074,6 +1164,23 @@ function idempotencyCacheMissFrame(clientCommitId, error) {
|
|
|
1074
1164
|
],
|
|
1075
1165
|
};
|
|
1076
1166
|
}
|
|
1167
|
+
/** §6.3: the unpersisted refusal of a commit below its outbox's watermark. */
|
|
1168
|
+
function commitExpiredFrame(clientCommitId) {
|
|
1169
|
+
return {
|
|
1170
|
+
type: 'PUSH_RESULT',
|
|
1171
|
+
clientCommitId,
|
|
1172
|
+
status: 'rejected',
|
|
1173
|
+
results: [
|
|
1174
|
+
{
|
|
1175
|
+
opIndex: 0,
|
|
1176
|
+
status: 'error',
|
|
1177
|
+
code: 'sync.commit_expired',
|
|
1178
|
+
message: 'the commit lies below its outbox watermark (§6.3.2)',
|
|
1179
|
+
retryable: false,
|
|
1180
|
+
},
|
|
1181
|
+
],
|
|
1182
|
+
};
|
|
1183
|
+
}
|
|
1077
1184
|
/**
|
|
1078
1185
|
* Process one `PUSH_COMMIT` frame: idempotency replay (§2.3), sequential
|
|
1079
1186
|
* atomic apply (§6.4), realtime notification for applied commits.
|
|
@@ -1086,13 +1193,15 @@ export async function processPushCommit(ctx, schema, resolved, clientId, frame)
|
|
|
1086
1193
|
* rejected replays as `status: rejected`; this companion result preserves the
|
|
1087
1194
|
* cache provenance needed by structured events and server helpers.
|
|
1088
1195
|
*/
|
|
1089
|
-
export async function processPushCommitWithTrace(ctx, schema, resolved, clientId, frame, wireSchema = schema) {
|
|
1196
|
+
export async function processPushCommitWithTrace(ctx, schema, resolved, clientId, frame, wireSchema = schema, outbox) {
|
|
1090
1197
|
// The frame names its rows before the transaction opens, so the admission
|
|
1091
1198
|
// read carries the commit's read-ahead and the apply issues no separate
|
|
1092
1199
|
// prefetch statement.
|
|
1093
1200
|
const targets = new Map();
|
|
1094
1201
|
for (const op of frame.operations) {
|
|
1095
|
-
|
|
1202
|
+
// A rowId holding U+0000 is rejected per operation (§6.1) before any
|
|
1203
|
+
// store reads it.
|
|
1204
|
+
if (!schema.tables.has(op.table) || op.rowId.includes('\u0000'))
|
|
1096
1205
|
continue;
|
|
1097
1206
|
const ids = targets.get(op.table) ?? [];
|
|
1098
1207
|
ids.push(op.rowId);
|
|
@@ -1100,22 +1209,31 @@ export async function processPushCommitWithTrace(ctx, schema, resolved, clientId
|
|
|
1100
1209
|
}
|
|
1101
1210
|
return processPushOperationsWithTrace(ctx, schema, resolved, clientId, frame.clientCommitId, async () => frame.operations, wireSchema, targets.size === 0
|
|
1102
1211
|
? undefined
|
|
1103
|
-
: [...targets].map(([table, rowIds]) => ({ table, rowIds })));
|
|
1212
|
+
: [...targets].map(([table, rowIds]) => ({ table, rowIds })), outbox);
|
|
1104
1213
|
}
|
|
1105
1214
|
/**
|
|
1106
1215
|
* Shared serialized apply path for SSP2 commits and authoritative commands.
|
|
1107
1216
|
* The builder runs after the partition lock and idempotency re-check, so its
|
|
1108
1217
|
* reads and the returned operations share the transaction that is committed.
|
|
1109
1218
|
*/
|
|
1110
|
-
export async function processPushOperationsWithTrace(ctx, schema, resolved, clientId, clientCommitId, buildOperations, wireSchema = schema, prefetchTargets) {
|
|
1219
|
+
export async function processPushOperationsWithTrace(ctx, schema, resolved, clientId, clientCommitId, buildOperations, wireSchema = schema, prefetchTargets, outbox) {
|
|
1111
1220
|
const { partition } = ctx;
|
|
1112
1221
|
const createdAtMs = clockOf(ctx)();
|
|
1113
|
-
const blobCtx = {
|
|
1222
|
+
const blobCtx = {
|
|
1223
|
+
store: ctx.blobs,
|
|
1224
|
+
partition,
|
|
1225
|
+
presence: new Map(),
|
|
1226
|
+
};
|
|
1114
1227
|
const crdtMergers = ctx.crdtMergers;
|
|
1115
1228
|
const validators = ctx.validators;
|
|
1116
1229
|
const commitValidator = ctx.commitValidator;
|
|
1117
1230
|
const reactionPlanner = ctx.reactionPlanner;
|
|
1118
1231
|
const tx = await ctx.storage.begin(partition);
|
|
1232
|
+
// §6.3.2: the settlement's writes commit whatever the commit's outcome.
|
|
1233
|
+
const release = () => outbox?.settlement !== undefined ? tx.commit() : tx.rollback();
|
|
1234
|
+
const sequence = outbox === undefined
|
|
1235
|
+
? undefined
|
|
1236
|
+
: { outboxId: outbox.outboxId, clientSeq: outbox.clientSeq };
|
|
1119
1237
|
try {
|
|
1120
1238
|
if (reactionPlanner !== undefined && tx.enqueueReactions === undefined) {
|
|
1121
1239
|
throw new Error('storage transaction does not support configured durable reactions');
|
|
@@ -1128,14 +1246,14 @@ export async function processPushOperationsWithTrace(ctx, schema, resolved, clie
|
|
|
1128
1246
|
// validator, or merge.
|
|
1129
1247
|
let prepared;
|
|
1130
1248
|
try {
|
|
1131
|
-
prepared = await tx.preparePushApply(clientId, clientCommitId, schema.version, prefetchTargets);
|
|
1249
|
+
prepared = await tx.preparePushApply(clientId, clientCommitId, schema.version, prefetchTargets, outbox?.settlement);
|
|
1132
1250
|
}
|
|
1133
1251
|
catch (error) {
|
|
1134
1252
|
if (error instanceof SyncError &&
|
|
1135
1253
|
error.code === 'sync.idempotency_cache_miss') {
|
|
1136
1254
|
// §6.3: answer the retryable cache-miss for this commit rather than
|
|
1137
1255
|
// re-applying. Not persisted — a retry may find a readable record.
|
|
1138
|
-
await
|
|
1256
|
+
await release();
|
|
1139
1257
|
return {
|
|
1140
1258
|
frame: idempotencyCacheMissFrame(clientCommitId, error),
|
|
1141
1259
|
replayed: false,
|
|
@@ -1148,9 +1266,10 @@ export async function processPushOperationsWithTrace(ctx, schema, resolved, clie
|
|
|
1148
1266
|
// migration can interleave in between.
|
|
1149
1267
|
const refusal = serveGateRefusal(prepared.gate, schema.version, ctx.checkpoints);
|
|
1150
1268
|
if (refusal !== undefined) {
|
|
1151
|
-
await
|
|
1269
|
+
await release();
|
|
1152
1270
|
throw serveNotReadyError(refusal);
|
|
1153
1271
|
}
|
|
1272
|
+
const watermark = prepared.watermark ?? outbox?.watermark;
|
|
1154
1273
|
if (prepared.persisted !== undefined) {
|
|
1155
1274
|
const conflict = prepared.persisted.results.find((result) => result.status === 'conflict');
|
|
1156
1275
|
const authorization = prepared.persisted.conflictAuthorization;
|
|
@@ -1160,7 +1279,7 @@ export async function processPushOperationsWithTrace(ctx, schema, resolved, clie
|
|
|
1160
1279
|
authorization.schemaVersion !== schema.version ||
|
|
1161
1280
|
table === undefined ||
|
|
1162
1281
|
!authorizeWrite(table, authorization.scopes, resolved))) {
|
|
1163
|
-
await
|
|
1282
|
+
await release();
|
|
1164
1283
|
return processedPushCommit(clientCommitId, {
|
|
1165
1284
|
...prepared.persisted,
|
|
1166
1285
|
results: [
|
|
@@ -1172,10 +1291,21 @@ export async function processPushOperationsWithTrace(ctx, schema, resolved, clie
|
|
|
1172
1291
|
retryable: false,
|
|
1173
1292
|
},
|
|
1174
1293
|
],
|
|
1175
|
-
}, true);
|
|
1294
|
+
}, true, watermark);
|
|
1176
1295
|
}
|
|
1177
|
-
await
|
|
1178
|
-
return processedPushCommit(clientCommitId, prepared.persisted, true);
|
|
1296
|
+
await release();
|
|
1297
|
+
return processedPushCommit(clientCommitId, prepared.persisted, true, watermark);
|
|
1298
|
+
}
|
|
1299
|
+
// §6.3.2 step 3: below the watermark without a retained result.
|
|
1300
|
+
if (outbox !== undefined &&
|
|
1301
|
+
watermark !== undefined &&
|
|
1302
|
+
outbox.clientSeq < watermark) {
|
|
1303
|
+
await release();
|
|
1304
|
+
return {
|
|
1305
|
+
frame: commitExpiredFrame(clientCommitId),
|
|
1306
|
+
replayed: false,
|
|
1307
|
+
watermark,
|
|
1308
|
+
};
|
|
1179
1309
|
}
|
|
1180
1310
|
const results = [];
|
|
1181
1311
|
const changes = [];
|
|
@@ -1186,7 +1316,7 @@ export async function processPushOperationsWithTrace(ctx, schema, resolved, clie
|
|
|
1186
1316
|
if (tx.prefetchRows !== undefined) {
|
|
1187
1317
|
const targets = new Map();
|
|
1188
1318
|
for (const op of operations) {
|
|
1189
|
-
if (!schema.tables.has(op.table))
|
|
1319
|
+
if (!schema.tables.has(op.table) || op.rowId.includes('\u0000'))
|
|
1190
1320
|
continue;
|
|
1191
1321
|
const ids = targets.get(op.table) ?? [];
|
|
1192
1322
|
ids.push(op.rowId);
|
|
@@ -1196,15 +1326,58 @@ export async function processPushOperationsWithTrace(ctx, schema, resolved, clie
|
|
|
1196
1326
|
await tx.prefetchRows([...targets].map(([table, rowIds]) => ({ table, rowIds })));
|
|
1197
1327
|
}
|
|
1198
1328
|
}
|
|
1329
|
+
// §6.6: the blobIds the commit pushes are checked once, concurrently,
|
|
1330
|
+
// before the operation loop. A payload that fails decode is skipped here
|
|
1331
|
+
// and rejected by its operation.
|
|
1332
|
+
const pushedBlobIds = [];
|
|
1333
|
+
for (const op of operations) {
|
|
1334
|
+
const table = schema.tables.get(op.table);
|
|
1335
|
+
const wireTable = wireSchema.tables.get(op.table);
|
|
1336
|
+
if (op.payload === undefined ||
|
|
1337
|
+
table === undefined ||
|
|
1338
|
+
wireTable === undefined ||
|
|
1339
|
+
table.blobRefColumnIndices.length === 0) {
|
|
1340
|
+
continue;
|
|
1341
|
+
}
|
|
1342
|
+
let sparse;
|
|
1343
|
+
try {
|
|
1344
|
+
sparse = decodeSparseRow(wireTable.columns, wireTable.primaryKeyIndex, op.payload);
|
|
1345
|
+
}
|
|
1346
|
+
catch {
|
|
1347
|
+
continue;
|
|
1348
|
+
}
|
|
1349
|
+
for (const index of table.blobRefColumnIndices) {
|
|
1350
|
+
const value = sparse[index];
|
|
1351
|
+
if (typeof value === 'string') {
|
|
1352
|
+
pushedBlobIds.push(parseBlobRef(value).blobId);
|
|
1353
|
+
}
|
|
1354
|
+
}
|
|
1355
|
+
}
|
|
1356
|
+
await resolveBlobPresence(blobCtx, pushedBlobIds);
|
|
1357
|
+
// §6.4: the row each touched (table, rowId) held before this commit, so a
|
|
1358
|
+
// conflict reports the row as stored once the commit rolls back.
|
|
1359
|
+
const storedBeforeCommit = new Map();
|
|
1360
|
+
// §2.2: inserts start above the floor, which this commit's deletes raise
|
|
1361
|
+
// as they stage.
|
|
1362
|
+
let deleteFloor = prepared.deleteFloor;
|
|
1199
1363
|
for (let opIndex = 0; opIndex < operations.length; opIndex++) {
|
|
1200
1364
|
const op = operations[opIndex];
|
|
1201
1365
|
if (op === undefined)
|
|
1202
1366
|
continue;
|
|
1203
|
-
const outcome = await applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blobCtx, crdtMergers, validators, partition, ctx.actorId);
|
|
1367
|
+
const outcome = await applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blobCtx, crdtMergers, validators, partition, ctx.actorId, deleteFloor);
|
|
1204
1368
|
if (outcome.kind === 'terminate') {
|
|
1205
1369
|
terminated = outcome.record;
|
|
1206
1370
|
break;
|
|
1207
1371
|
}
|
|
1372
|
+
for (const change of outcome.changes) {
|
|
1373
|
+
if (change.op === 'delete') {
|
|
1374
|
+
deleteFloor = Math.max(deleteFloor, change.rowVersion);
|
|
1375
|
+
}
|
|
1376
|
+
}
|
|
1377
|
+
const rowKey = `${op.table}\u0000${op.rowId}`;
|
|
1378
|
+
if (!storedBeforeCommit.has(rowKey)) {
|
|
1379
|
+
storedBeforeCommit.set(rowKey, outcome.staged.stored);
|
|
1380
|
+
}
|
|
1208
1381
|
results.push({ opIndex, status: 'applied' });
|
|
1209
1382
|
validatedOperations.push(...outcome.operations);
|
|
1210
1383
|
stagedWrites.push(outcome.staged);
|
|
@@ -1257,16 +1430,31 @@ export async function processPushOperationsWithTrace(ctx, schema, resolved, clie
|
|
|
1257
1430
|
const conflictOperation = terminated.status === 'conflict'
|
|
1258
1431
|
? operations[terminated.opIndex]
|
|
1259
1432
|
: undefined;
|
|
1260
|
-
|
|
1433
|
+
// §6.4: a conflict only arises in the operation loop, so a row no
|
|
1434
|
+
// earlier operation touched still reads as stored before the commit.
|
|
1435
|
+
const conflictKey = conflictOperation === undefined
|
|
1436
|
+
? undefined
|
|
1437
|
+
: `${conflictOperation.table}\u0000${conflictOperation.rowId}`;
|
|
1438
|
+
const conflictRow = conflictOperation === undefined || conflictKey === undefined
|
|
1261
1439
|
? undefined
|
|
1262
|
-
:
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1440
|
+
: storedBeforeCommit.has(conflictKey)
|
|
1441
|
+
? storedBeforeCommit.get(conflictKey)
|
|
1442
|
+
: await tx.getRow(conflictOperation.table, conflictOperation.rowId);
|
|
1443
|
+
if (terminated.status === 'conflict') {
|
|
1444
|
+
terminated =
|
|
1445
|
+
conflictRow === undefined
|
|
1446
|
+
? errorRecord(terminated.opIndex, 'sync.row_missing', 'the conflicting row exists only through an earlier operation of the same commit (§6.4)').record
|
|
1447
|
+
: {
|
|
1448
|
+
...terminated,
|
|
1449
|
+
serverVersion: conflictRow.serverVersion,
|
|
1450
|
+
serverRow: conflictRow.payload,
|
|
1451
|
+
};
|
|
1266
1452
|
}
|
|
1267
1453
|
const stored = newStoredPushResult(createdAtMs, {
|
|
1268
1454
|
status: 'rejected',
|
|
1269
|
-
...(
|
|
1455
|
+
...(terminated.status === 'conflict' &&
|
|
1456
|
+
conflictOperation !== undefined &&
|
|
1457
|
+
conflictRow !== undefined
|
|
1270
1458
|
? {
|
|
1271
1459
|
conflictAuthorization: {
|
|
1272
1460
|
table: conflictOperation.table,
|
|
@@ -1280,8 +1468,8 @@ export async function processPushOperationsWithTrace(ctx, schema, resolved, clie
|
|
|
1280
1468
|
// Discard candidates and persist the rejection while retaining the same
|
|
1281
1469
|
// partition lock. There is no unlock gap in which a duplicate can rerun,
|
|
1282
1470
|
// and the finalization returns the canonical outcome it persisted.
|
|
1283
|
-
const canonical = await tx.commitRejectedPushResult(clientId, clientCommitId, stored);
|
|
1284
|
-
return processedPushCommit(clientCommitId, canonical, canonical.cacheIdentity !== stored.cacheIdentity);
|
|
1471
|
+
const canonical = await tx.commitRejectedPushResult(clientId, clientCommitId, stored, sequence);
|
|
1472
|
+
return processedPushCommit(clientCommitId, canonical, canonical.cacheIdentity !== stored.cacheIdentity, watermark);
|
|
1285
1473
|
}
|
|
1286
1474
|
const commitSeq = await tx.appendCommit({
|
|
1287
1475
|
clientId,
|
|
@@ -1308,7 +1496,7 @@ export async function processPushOperationsWithTrace(ctx, schema, resolved, clie
|
|
|
1308
1496
|
commitSeq,
|
|
1309
1497
|
results,
|
|
1310
1498
|
});
|
|
1311
|
-
await tx.putPushResult(clientId, clientCommitId, stored);
|
|
1499
|
+
await tx.putPushResult(clientId, clientCommitId, stored, sequence);
|
|
1312
1500
|
await tx.commit();
|
|
1313
1501
|
if (ctx.events !== undefined) {
|
|
1314
1502
|
const atMs = clockOf(ctx)();
|
|
@@ -1342,7 +1530,7 @@ export async function processPushOperationsWithTrace(ctx, schema, resolved, clie
|
|
|
1342
1530
|
changes,
|
|
1343
1531
|
});
|
|
1344
1532
|
}
|
|
1345
|
-
return processedPushCommit(clientCommitId, stored, false);
|
|
1533
|
+
return processedPushCommit(clientCommitId, stored, false, watermark);
|
|
1346
1534
|
}
|
|
1347
1535
|
catch (error) {
|
|
1348
1536
|
if (error instanceof StorageConstraintError) {
|
|
@@ -1359,7 +1547,7 @@ export async function processPushOperationsWithTrace(ctx, schema, resolved, clie
|
|
|
1359
1547
|
],
|
|
1360
1548
|
});
|
|
1361
1549
|
try {
|
|
1362
|
-
const canonical = await tx.commitRejectedPushResult(clientId, clientCommitId, stored);
|
|
1550
|
+
const canonical = await tx.commitRejectedPushResult(clientId, clientCommitId, stored, sequence);
|
|
1363
1551
|
return processedPushCommit(clientCommitId, canonical, canonical.cacheIdentity !== stored.cacheIdentity);
|
|
1364
1552
|
}
|
|
1365
1553
|
catch (finalizationError) {
|