@syncular/server 0.58.0 → 0.59.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 +31 -22
- package/dist/admin.js +8 -4
- 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 +16 -0
- package/dist/context.js +21 -1
- package/dist/d1-storage.d.ts +14 -98
- package/dist/d1-storage.js +223 -1111
- package/dist/errors.js +8 -0
- package/dist/events.d.ts +1 -1
- package/dist/handler.js +135 -103
- package/dist/local-authority.d.ts +2 -0
- package/dist/local-authority.js +17 -3
- package/dist/object-bucket.d.ts +2 -0
- package/dist/operations-realtime.js +69 -50
- 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 +1 -1
- package/dist/postgres-storage.js +182 -105
- package/dist/pull.d.ts +28 -20
- package/dist/pull.js +85 -87
- package/dist/push.js +272 -147
- package/dist/realtime.d.ts +68 -28
- package/dist/realtime.js +297 -136
- package/dist/relational-rows.d.ts +27 -15
- package/dist/relational-rows.js +38 -22
- 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 +1 -2
- package/dist/segment-download.js +18 -4
- 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 +150 -47
- package/dist/sqlite-dialect.js +987 -88
- package/dist/sqlite-lease-store.js +13 -18
- package/dist/sqlite-storage.d.ts +9 -62
- package/dist/sqlite-storage.js +293 -1117
- package/dist/storage.d.ts +32 -12
- package/package.json +2 -2
- package/src/admin.ts +16 -2
- 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 +32 -1
- package/src/d1-storage.ts +326 -1692
- package/src/errors.ts +8 -0
- package/src/events.ts +1 -1
- package/src/handler.ts +178 -127
- package/src/local-authority.ts +17 -3
- package/src/object-bucket.ts +2 -0
- package/src/operations-realtime.ts +85 -61
- package/src/pg-executor-pglite.ts +27 -23
- package/src/pg-executor.ts +5 -1
- package/src/postgres-storage.ts +231 -138
- package/src/pull.ts +133 -147
- package/src/push.ts +356 -205
- package/src/realtime.ts +380 -179
- package/src/relational-rows.ts +54 -27
- 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 +1 -2
- package/src/segment-download.ts +25 -4
- 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 +1607 -114
- package/src/sqlite-lease-store.ts +12 -23
- package/src/sqlite-storage.ts +360 -1811
- package/src/storage.ts +33 -12
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
|
|
@@ -171,6 +182,10 @@ async function applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blo
|
|
|
171
182
|
if (!resolved.ok) {
|
|
172
183
|
return errorRecord(opIndex, 'sync.forbidden', 'scope resolution failed (§3.4 step 4)');
|
|
173
184
|
}
|
|
185
|
+
if (op.rowId.includes('\u0000')) {
|
|
186
|
+
// §6.1: no store sees a rowId holding U+0000; PostgreSQL refuses it.
|
|
187
|
+
return errorRecord(opIndex, 'sync.invalid_request', 'rowId holds U+0000');
|
|
188
|
+
}
|
|
174
189
|
const stored = await tx.getRow(op.table, op.rowId);
|
|
175
190
|
if (op.op === 'rescope') {
|
|
176
191
|
// §2.2 explicit scope move. One stored row changes its scope values;
|
|
@@ -295,6 +310,7 @@ async function applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blo
|
|
|
295
310
|
rowId: op.rowId,
|
|
296
311
|
opIndex,
|
|
297
312
|
values,
|
|
313
|
+
present: table.columns.map(() => true),
|
|
298
314
|
stored,
|
|
299
315
|
},
|
|
300
316
|
};
|
|
@@ -346,6 +362,7 @@ async function applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blo
|
|
|
346
362
|
table: op.table,
|
|
347
363
|
rowId: op.rowId,
|
|
348
364
|
op: 'delete',
|
|
365
|
+
deletedVersion: stored.serverVersion,
|
|
349
366
|
scopes: stored.scopes,
|
|
350
367
|
},
|
|
351
368
|
],
|
|
@@ -509,41 +526,60 @@ async function applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blo
|
|
|
509
526
|
nextServerVersion: newVersion,
|
|
510
527
|
},
|
|
511
528
|
],
|
|
512
|
-
staged: {
|
|
529
|
+
staged: {
|
|
530
|
+
table,
|
|
531
|
+
op: 'upsert',
|
|
532
|
+
rowId: op.rowId,
|
|
533
|
+
opIndex,
|
|
534
|
+
values,
|
|
535
|
+
present,
|
|
536
|
+
stored,
|
|
537
|
+
},
|
|
513
538
|
};
|
|
514
539
|
}
|
|
515
|
-
// Insert path: no stored row (§5.2).
|
|
540
|
+
// Insert path: no stored row (§5.2). The authorization row is the payload
|
|
541
|
+
// plus the primary key (§3.4 step 2); a missing or empty scope column value
|
|
542
|
+
// ⇒ deny.
|
|
516
543
|
const values = sparse.map((value) => value ?? null);
|
|
544
|
+
const extracted = storedScopesForRow(table, values);
|
|
545
|
+
const insert = 'missing' in extracted
|
|
546
|
+
? errorRecord(opIndex, 'sync.forbidden', 'insert missing scope column value (§3.4)')
|
|
547
|
+
: authorizeWrite(table, extracted.scopes, resolved)
|
|
548
|
+
? extracted
|
|
549
|
+
: errorRecord(opIndex, 'sync.forbidden', 'insert denied by scope authorization (§3.4)');
|
|
517
550
|
if (op.baseVersion !== undefined && op.baseVersion !== 0) {
|
|
518
551
|
// Authorize the payload first so absence is not disclosed to actors
|
|
519
552
|
// without the scope; then §6.2: baseVersion ≠ 0, row absent.
|
|
520
|
-
|
|
521
|
-
if ('missing' in extractedFirst ||
|
|
522
|
-
!authorizeWrite(table, extractedFirst.scopes, resolved)) {
|
|
553
|
+
if ('kind' in insert) {
|
|
523
554
|
return errorRecord(opIndex, 'sync.forbidden', 'write denied by scope authorization (§3.4)');
|
|
524
555
|
}
|
|
525
556
|
return errorRecord(opIndex, 'sync.row_missing', 'upsert with baseVersion targets an absent row (§6.2)');
|
|
526
557
|
}
|
|
527
|
-
|
|
558
|
+
// §6.2: a payload that presents every scope column is authorized before
|
|
559
|
+
// the tombstone read, so the answer never discloses a deletion to an actor
|
|
560
|
+
// who could not write the row.
|
|
561
|
+
if (op.baseVersion === undefined &&
|
|
562
|
+
'kind' in insert &&
|
|
563
|
+
table.scopePatterns.every((pattern) => present[pattern.columnIndex])) {
|
|
564
|
+
return insert;
|
|
565
|
+
}
|
|
566
|
+
// §6.2: a tombstone whose stored scopes the actor fails reads as absent.
|
|
567
|
+
const tombstone = await tx.getTombstone(op.table, op.rowId);
|
|
568
|
+
const deleted = tombstone?.scopes === undefined ||
|
|
569
|
+
authorizeWrite(table, tombstone.scopes, resolved)
|
|
570
|
+
? tombstone
|
|
571
|
+
: undefined;
|
|
572
|
+
if (op.baseVersion === undefined && deleted !== undefined) {
|
|
528
573
|
// §5.2: a delete within the retention horizon beats an unversioned
|
|
529
574
|
// 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
|
-
}
|
|
575
|
+
return errorRecord(opIndex, 'sync.row_deleted', 'a deleted row rejects an unversioned upsert within the retention horizon (§5.2)');
|
|
534
576
|
}
|
|
535
577
|
if (presentCount < table.columns.length) {
|
|
536
578
|
// §6.3: an insert requires every column present.
|
|
537
579
|
return errorRecord(opIndex, 'sync.row_missing', 'a partial payload targets an absent row (§6.3)');
|
|
538
580
|
}
|
|
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
|
-
}
|
|
581
|
+
if ('kind' in insert)
|
|
582
|
+
return insert;
|
|
547
583
|
// §5.10.3: on insert a crdt column merges against the empty document
|
|
548
584
|
// (stored = null) — normalizes the initial state through the merger.
|
|
549
585
|
const insertMerge = await mergeCrdtColumns(table, values, undefined, present, opIndex, crdtMergers);
|
|
@@ -562,16 +598,16 @@ async function applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blo
|
|
|
562
598
|
const insertReject = await runValidator(validators, table, 'upsert', op.rowId, values, undefined, opIndex, tx, partition, actorId);
|
|
563
599
|
if (insertReject !== undefined)
|
|
564
600
|
return insertReject;
|
|
565
|
-
// §
|
|
566
|
-
//
|
|
567
|
-
//
|
|
568
|
-
if (
|
|
601
|
+
// §6.2: an insert recreates over the tombstone and starts above the
|
|
602
|
+
// deleted row's version; without a column-versions blob every column sits
|
|
603
|
+
// at that version.
|
|
604
|
+
if (tombstone !== undefined)
|
|
569
605
|
await tx.clearTombstone(op.table, op.rowId);
|
|
570
|
-
|
|
606
|
+
const serverVersion = (deleted?.serverVersion ?? 0) + 1;
|
|
571
607
|
const newRow = {
|
|
572
608
|
rowId: op.rowId,
|
|
573
|
-
serverVersion
|
|
574
|
-
scopes:
|
|
609
|
+
serverVersion,
|
|
610
|
+
scopes: insert.scopes,
|
|
575
611
|
payload: insertPayload,
|
|
576
612
|
};
|
|
577
613
|
await tx.upsertRow(op.table, newRow, { opIndex });
|
|
@@ -582,8 +618,8 @@ async function applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blo
|
|
|
582
618
|
table: op.table,
|
|
583
619
|
rowId: op.rowId,
|
|
584
620
|
op: 'upsert',
|
|
585
|
-
rowVersion:
|
|
586
|
-
scopes:
|
|
621
|
+
rowVersion: serverVersion,
|
|
622
|
+
scopes: insert.scopes,
|
|
587
623
|
payload: insertPayload,
|
|
588
624
|
},
|
|
589
625
|
],
|
|
@@ -595,7 +631,7 @@ async function applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blo
|
|
|
595
631
|
rowId: op.rowId,
|
|
596
632
|
row: toValidateRow(table.columns, values),
|
|
597
633
|
stored: undefined,
|
|
598
|
-
nextServerVersion:
|
|
634
|
+
nextServerVersion: serverVersion,
|
|
599
635
|
},
|
|
600
636
|
],
|
|
601
637
|
staged: {
|
|
@@ -604,6 +640,7 @@ async function applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blo
|
|
|
604
640
|
rowId: op.rowId,
|
|
605
641
|
opIndex,
|
|
606
642
|
values,
|
|
643
|
+
present,
|
|
607
644
|
stored: undefined,
|
|
608
645
|
},
|
|
609
646
|
};
|
|
@@ -623,10 +660,11 @@ async function checkAndRecordBlobs(tx, table, rowId, values, opIndex, blobCtx) {
|
|
|
623
660
|
if (blobCtx.store === undefined) {
|
|
624
661
|
return errorRecord(opIndex, 'blob.not_found', 'row references a blob but the server has no blob store (§5.9.6)');
|
|
625
662
|
}
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
663
|
+
// Pushed blobIds were answered before the operation loop; a blobId the
|
|
664
|
+
// row keeps from its stored values is answered here.
|
|
665
|
+
await resolveBlobPresence(blobCtx, blobIds);
|
|
666
|
+
if (blobIds.some((blobId) => blobCtx.presence.get(blobId) !== true)) {
|
|
667
|
+
return errorRecord(opIndex, 'blob.not_found', 'push references a blob that has not been uploaded (§5.9.6)');
|
|
630
668
|
}
|
|
631
669
|
}
|
|
632
670
|
// Update the reference index for this row (empty set clears it, §5.9.4).
|
|
@@ -805,13 +843,22 @@ async function enforceReferences(tx, schema, resolved, staged, clientOpCount, ca
|
|
|
805
843
|
const changes = [];
|
|
806
844
|
const operations = [];
|
|
807
845
|
const originByIndex = [];
|
|
846
|
+
// A reference column counts as present when any staged upsert of the row
|
|
847
|
+
// presents it; its value is the row's final value (§6.11).
|
|
808
848
|
const finalByRow = new Map();
|
|
809
849
|
for (const write of staged) {
|
|
810
850
|
const rowKey = `${write.table.name}\u0000${write.rowId}`;
|
|
811
851
|
const existing = finalByRow.get(rowKey);
|
|
852
|
+
const presentReferences = new Set(existing?.presentReferences);
|
|
853
|
+
for (const reference of write.table.references) {
|
|
854
|
+
if (write.present?.[reference.columnIndex] === true) {
|
|
855
|
+
presentReferences.add(reference.columnIndex);
|
|
856
|
+
}
|
|
857
|
+
}
|
|
812
858
|
finalByRow.set(rowKey, {
|
|
813
859
|
first: existing?.first ?? write,
|
|
814
860
|
last: write,
|
|
861
|
+
presentReferences,
|
|
815
862
|
});
|
|
816
863
|
}
|
|
817
864
|
// One candidate-state read per parent table, not one per reference: a
|
|
@@ -819,10 +866,12 @@ async function enforceReferences(tx, schema, resolved, staged, clientOpCount, ca
|
|
|
819
866
|
// combatants, its chronicle) reads that parent once. Postgres binds the
|
|
820
867
|
// ids as one array per table; SQLite and D1 answer with one lookup per id.
|
|
821
868
|
const parentIds = new Map();
|
|
822
|
-
for (const { last: write } of finalByRow.values()) {
|
|
869
|
+
for (const { last: write, presentReferences } of finalByRow.values()) {
|
|
823
870
|
if (write.op !== 'upsert' || write.values === undefined)
|
|
824
871
|
continue;
|
|
825
872
|
for (const reference of write.table.references) {
|
|
873
|
+
if (!presentReferences.has(reference.columnIndex))
|
|
874
|
+
continue;
|
|
826
875
|
const value = write.values[reference.columnIndex];
|
|
827
876
|
if (value === null || value === undefined)
|
|
828
877
|
continue;
|
|
@@ -840,17 +889,25 @@ async function enforceReferences(tx, schema, resolved, staged, clientOpCount, ca
|
|
|
840
889
|
for (const [parentTable, ids] of parentIds) {
|
|
841
890
|
parents.set(parentTable, await readCandidateRows(tx, parentTable, [...ids]));
|
|
842
891
|
}
|
|
843
|
-
for (const { last: write } of finalByRow.values()) {
|
|
892
|
+
for (const { last: write, presentReferences } of finalByRow.values()) {
|
|
844
893
|
if (write.op !== 'upsert' || write.values === undefined)
|
|
845
894
|
continue;
|
|
846
895
|
for (const reference of write.table.references) {
|
|
896
|
+
if (!presentReferences.has(reference.columnIndex))
|
|
897
|
+
continue;
|
|
847
898
|
const value = write.values[reference.columnIndex];
|
|
848
899
|
if (value === null || value === undefined)
|
|
849
900
|
continue;
|
|
850
901
|
const parentRowId = renderScopeValue(value);
|
|
851
902
|
if (parentRowId === undefined)
|
|
852
903
|
continue;
|
|
853
|
-
|
|
904
|
+
// A parent the actor cannot authorize (§3.4, on its stored scopes) is
|
|
905
|
+
// an absent parent: the rejection below is identical either way.
|
|
906
|
+
const parent = parents.get(reference.parentTable)?.get(parentRowId);
|
|
907
|
+
const parentTable = schema.tables.get(reference.parentTable);
|
|
908
|
+
if (parent !== undefined &&
|
|
909
|
+
parentTable !== undefined &&
|
|
910
|
+
authorizeWrite(parentTable, parent.scopes, resolved)) {
|
|
854
911
|
continue;
|
|
855
912
|
}
|
|
856
913
|
return {
|
|
@@ -897,24 +954,17 @@ async function enforceReferences(tx, schema, resolved, staged, clientOpCount, ca
|
|
|
897
954
|
if (tx.scanRowsByIndex === undefined) {
|
|
898
955
|
throw new Error('storage transaction does not support declared-reference enforcement (scanRowsByIndex)');
|
|
899
956
|
}
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
const page = await tx.scanRowsByIndex({
|
|
957
|
+
if (reference.onDelete === 'RESTRICT') {
|
|
958
|
+
// One remaining child decides RESTRICT.
|
|
959
|
+
const blocking = await tx.scanRowsByIndex({
|
|
904
960
|
table: reference.table,
|
|
905
961
|
index: reference.index,
|
|
906
962
|
values: [parentKeyValue],
|
|
907
|
-
afterRowId,
|
|
908
|
-
limit:
|
|
963
|
+
afterRowId: null,
|
|
964
|
+
limit: 1,
|
|
909
965
|
});
|
|
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') {
|
|
966
|
+
if (blocking.length === 0)
|
|
967
|
+
continue;
|
|
918
968
|
return {
|
|
919
969
|
kind: 'terminate',
|
|
920
970
|
record: referenceRejection(item.originOpIndex, 'delete is blocked by a declared reference with ON DELETE RESTRICT (§6.11)', {
|
|
@@ -927,101 +977,120 @@ async function enforceReferences(tx, schema, resolved, staged, clientOpCount, ca
|
|
|
927
977
|
if (child === undefined) {
|
|
928
978
|
throw new Error(`unreachable: reference child table ${reference.table} is not compiled`);
|
|
929
979
|
}
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
980
|
+
// Each page reads no more than the remaining cascade budget plus one
|
|
981
|
+
// child, so a cascade past the cap stops after one extra row. A page is
|
|
982
|
+
// applied before the next is read; keyset pagination by rowId is
|
|
983
|
+
// unaffected by deleting or nulling rows already read.
|
|
984
|
+
let afterRowId = null;
|
|
985
|
+
for (;;) {
|
|
986
|
+
const limit = Math.min(1_000, cascadeLimit - originByIndex.length + 1);
|
|
987
|
+
const page = await tx.scanRowsByIndex({
|
|
988
|
+
table: reference.table,
|
|
989
|
+
index: reference.index,
|
|
990
|
+
values: [parentKeyValue],
|
|
991
|
+
afterRowId,
|
|
992
|
+
limit,
|
|
993
|
+
});
|
|
994
|
+
for (const childRow of page) {
|
|
995
|
+
if (!authorizeWrite(child, childRow.scopes, resolved)) {
|
|
996
|
+
return errorRecord(item.originOpIndex, 'sync.forbidden', 'cascade denied by scope authorization (§3.4)');
|
|
997
|
+
}
|
|
998
|
+
if (reference.onDelete === 'CASCADE') {
|
|
999
|
+
if (!schedule(child, childRow.rowId, item.originOpIndex, scheduled, queue)) {
|
|
1000
|
+
continue;
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
else if (scheduled.has(`${child.name}\u0000${childRow.rowId}`)) {
|
|
936
1004
|
continue;
|
|
937
1005
|
}
|
|
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 };
|
|
1006
|
+
if (originByIndex.length >= cascadeLimit) {
|
|
1007
|
+
return {
|
|
1008
|
+
kind: 'terminate',
|
|
1009
|
+
record: referenceRejection(item.originOpIndex, 'declared-reference cascade exceeded the per-commit operation cap (§6.11)', { reason: 'cascade_limit' }),
|
|
1010
|
+
};
|
|
954
1011
|
}
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
1012
|
+
const syntheticIndex = clientOpCount + originByIndex.length;
|
|
1013
|
+
const storedValues = decodeRow(child.columns, childRow.payload);
|
|
1014
|
+
if (reference.onDelete === 'CASCADE') {
|
|
1015
|
+
const reject = await runValidator(validators, child, 'delete', childRow.rowId, undefined, storedValues, item.originOpIndex, tx, partition, actorId);
|
|
1016
|
+
if (reject !== undefined) {
|
|
1017
|
+
return { kind: 'terminate', record: reject.record };
|
|
1018
|
+
}
|
|
1019
|
+
await tx.deleteRow(child.name, childRow.rowId);
|
|
1020
|
+
changes.push({
|
|
1021
|
+
table: child.name,
|
|
1022
|
+
rowId: childRow.rowId,
|
|
1023
|
+
op: 'delete',
|
|
1024
|
+
deletedVersion: childRow.serverVersion,
|
|
1025
|
+
scopes: childRow.scopes,
|
|
1026
|
+
});
|
|
1027
|
+
operations.push({
|
|
1028
|
+
opIndex: syntheticIndex,
|
|
1029
|
+
op: 'delete',
|
|
1030
|
+
table: child.name,
|
|
1031
|
+
rowId: childRow.rowId,
|
|
1032
|
+
row: undefined,
|
|
1033
|
+
stored: toValidateRow(child.columns, storedValues),
|
|
1034
|
+
storedServerVersion: childRow.serverVersion,
|
|
1035
|
+
});
|
|
978
1036
|
}
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
1037
|
+
else {
|
|
1038
|
+
const values = [...storedValues];
|
|
1039
|
+
values[reference.columnIndex] = null;
|
|
1040
|
+
const reject = await runValidator(validators, child, 'upsert', childRow.rowId, values, storedValues, item.originOpIndex, tx, partition, actorId);
|
|
1041
|
+
if (reject !== undefined) {
|
|
1042
|
+
return { kind: 'terminate', record: reject.record };
|
|
1043
|
+
}
|
|
1044
|
+
const nextServerVersion = childRow.serverVersion + 1;
|
|
1045
|
+
// §6.2: the SET NULL upsert writes only the reference column, so
|
|
1046
|
+
// only that column's version moves.
|
|
1047
|
+
const storedVersions = decodeColumnVersions(childRow.columnVersions);
|
|
1048
|
+
const nextVersions = new Map();
|
|
1049
|
+
for (let i = 0; i < child.columns.length; i++) {
|
|
1050
|
+
const version = i === reference.columnIndex
|
|
1051
|
+
? nextServerVersion
|
|
1052
|
+
: (storedVersions.get(i) ??
|
|
1053
|
+
(childRow.columnVersions === undefined
|
|
1054
|
+
? childRow.serverVersion
|
|
1055
|
+
: 1));
|
|
1056
|
+
if (version > 1)
|
|
1057
|
+
nextVersions.set(i, version);
|
|
1058
|
+
}
|
|
1059
|
+
const payload = encodeRow(child.columns, values);
|
|
1060
|
+
const cascadeVersions = encodeColumnVersions(nextVersions);
|
|
1061
|
+
await tx.upsertRow(child.name, {
|
|
1062
|
+
rowId: childRow.rowId,
|
|
1063
|
+
serverVersion: nextServerVersion,
|
|
1064
|
+
scopes: childRow.scopes,
|
|
1065
|
+
payload,
|
|
1066
|
+
...(cascadeVersions !== undefined
|
|
1067
|
+
? { columnVersions: cascadeVersions }
|
|
1068
|
+
: {}),
|
|
1069
|
+
}, { opIndex: syntheticIndex });
|
|
1070
|
+
changes.push({
|
|
1071
|
+
table: child.name,
|
|
1072
|
+
rowId: childRow.rowId,
|
|
1073
|
+
op: 'upsert',
|
|
1074
|
+
rowVersion: nextServerVersion,
|
|
1075
|
+
scopes: childRow.scopes,
|
|
1076
|
+
payload,
|
|
1077
|
+
});
|
|
1078
|
+
operations.push({
|
|
1079
|
+
opIndex: syntheticIndex,
|
|
1080
|
+
op: 'upsert',
|
|
1081
|
+
table: child.name,
|
|
1082
|
+
rowId: childRow.rowId,
|
|
1083
|
+
row: toValidateRow(child.columns, values),
|
|
1084
|
+
stored: toValidateRow(child.columns, storedValues),
|
|
1085
|
+
storedServerVersion: childRow.serverVersion,
|
|
1086
|
+
nextServerVersion,
|
|
1087
|
+
});
|
|
993
1088
|
}
|
|
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
|
-
});
|
|
1089
|
+
originByIndex.push(item.originOpIndex);
|
|
1023
1090
|
}
|
|
1024
|
-
|
|
1091
|
+
if (page.length < limit)
|
|
1092
|
+
break;
|
|
1093
|
+
afterRowId = page[page.length - 1]?.rowId ?? null;
|
|
1025
1094
|
}
|
|
1026
1095
|
}
|
|
1027
1096
|
}
|
|
@@ -1092,7 +1161,9 @@ export async function processPushCommitWithTrace(ctx, schema, resolved, clientId
|
|
|
1092
1161
|
// prefetch statement.
|
|
1093
1162
|
const targets = new Map();
|
|
1094
1163
|
for (const op of frame.operations) {
|
|
1095
|
-
|
|
1164
|
+
// A rowId holding U+0000 is rejected per operation (§6.1) before any
|
|
1165
|
+
// store reads it.
|
|
1166
|
+
if (!schema.tables.has(op.table) || op.rowId.includes('\u0000'))
|
|
1096
1167
|
continue;
|
|
1097
1168
|
const ids = targets.get(op.table) ?? [];
|
|
1098
1169
|
ids.push(op.rowId);
|
|
@@ -1110,7 +1181,11 @@ export async function processPushCommitWithTrace(ctx, schema, resolved, clientId
|
|
|
1110
1181
|
export async function processPushOperationsWithTrace(ctx, schema, resolved, clientId, clientCommitId, buildOperations, wireSchema = schema, prefetchTargets) {
|
|
1111
1182
|
const { partition } = ctx;
|
|
1112
1183
|
const createdAtMs = clockOf(ctx)();
|
|
1113
|
-
const blobCtx = {
|
|
1184
|
+
const blobCtx = {
|
|
1185
|
+
store: ctx.blobs,
|
|
1186
|
+
partition,
|
|
1187
|
+
presence: new Map(),
|
|
1188
|
+
};
|
|
1114
1189
|
const crdtMergers = ctx.crdtMergers;
|
|
1115
1190
|
const validators = ctx.validators;
|
|
1116
1191
|
const commitValidator = ctx.commitValidator;
|
|
@@ -1186,7 +1261,7 @@ export async function processPushOperationsWithTrace(ctx, schema, resolved, clie
|
|
|
1186
1261
|
if (tx.prefetchRows !== undefined) {
|
|
1187
1262
|
const targets = new Map();
|
|
1188
1263
|
for (const op of operations) {
|
|
1189
|
-
if (!schema.tables.has(op.table))
|
|
1264
|
+
if (!schema.tables.has(op.table) || op.rowId.includes('\u0000'))
|
|
1190
1265
|
continue;
|
|
1191
1266
|
const ids = targets.get(op.table) ?? [];
|
|
1192
1267
|
ids.push(op.rowId);
|
|
@@ -1196,6 +1271,37 @@ export async function processPushOperationsWithTrace(ctx, schema, resolved, clie
|
|
|
1196
1271
|
await tx.prefetchRows([...targets].map(([table, rowIds]) => ({ table, rowIds })));
|
|
1197
1272
|
}
|
|
1198
1273
|
}
|
|
1274
|
+
// §6.6: the blobIds the commit pushes are checked once, concurrently,
|
|
1275
|
+
// before the operation loop. A payload that fails decode is skipped here
|
|
1276
|
+
// and rejected by its operation.
|
|
1277
|
+
const pushedBlobIds = [];
|
|
1278
|
+
for (const op of operations) {
|
|
1279
|
+
const table = schema.tables.get(op.table);
|
|
1280
|
+
const wireTable = wireSchema.tables.get(op.table);
|
|
1281
|
+
if (op.payload === undefined ||
|
|
1282
|
+
table === undefined ||
|
|
1283
|
+
wireTable === undefined ||
|
|
1284
|
+
table.blobRefColumnIndices.length === 0) {
|
|
1285
|
+
continue;
|
|
1286
|
+
}
|
|
1287
|
+
let sparse;
|
|
1288
|
+
try {
|
|
1289
|
+
sparse = decodeSparseRow(wireTable.columns, wireTable.primaryKeyIndex, op.payload);
|
|
1290
|
+
}
|
|
1291
|
+
catch {
|
|
1292
|
+
continue;
|
|
1293
|
+
}
|
|
1294
|
+
for (const index of table.blobRefColumnIndices) {
|
|
1295
|
+
const value = sparse[index];
|
|
1296
|
+
if (typeof value === 'string') {
|
|
1297
|
+
pushedBlobIds.push(parseBlobRef(value).blobId);
|
|
1298
|
+
}
|
|
1299
|
+
}
|
|
1300
|
+
}
|
|
1301
|
+
await resolveBlobPresence(blobCtx, pushedBlobIds);
|
|
1302
|
+
// §6.4: the row each touched (table, rowId) held before this commit, so a
|
|
1303
|
+
// conflict reports the row as stored once the commit rolls back.
|
|
1304
|
+
const storedBeforeCommit = new Map();
|
|
1199
1305
|
for (let opIndex = 0; opIndex < operations.length; opIndex++) {
|
|
1200
1306
|
const op = operations[opIndex];
|
|
1201
1307
|
if (op === undefined)
|
|
@@ -1205,6 +1311,10 @@ export async function processPushOperationsWithTrace(ctx, schema, resolved, clie
|
|
|
1205
1311
|
terminated = outcome.record;
|
|
1206
1312
|
break;
|
|
1207
1313
|
}
|
|
1314
|
+
const rowKey = `${op.table}\u0000${op.rowId}`;
|
|
1315
|
+
if (!storedBeforeCommit.has(rowKey)) {
|
|
1316
|
+
storedBeforeCommit.set(rowKey, outcome.staged.stored);
|
|
1317
|
+
}
|
|
1208
1318
|
results.push({ opIndex, status: 'applied' });
|
|
1209
1319
|
validatedOperations.push(...outcome.operations);
|
|
1210
1320
|
stagedWrites.push(outcome.staged);
|
|
@@ -1257,16 +1367,31 @@ export async function processPushOperationsWithTrace(ctx, schema, resolved, clie
|
|
|
1257
1367
|
const conflictOperation = terminated.status === 'conflict'
|
|
1258
1368
|
? operations[terminated.opIndex]
|
|
1259
1369
|
: undefined;
|
|
1260
|
-
|
|
1370
|
+
// §6.4: a conflict only arises in the operation loop, so a row no
|
|
1371
|
+
// earlier operation touched still reads as stored before the commit.
|
|
1372
|
+
const conflictKey = conflictOperation === undefined
|
|
1261
1373
|
? undefined
|
|
1262
|
-
:
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1374
|
+
: `${conflictOperation.table}\u0000${conflictOperation.rowId}`;
|
|
1375
|
+
const conflictRow = conflictOperation === undefined || conflictKey === undefined
|
|
1376
|
+
? undefined
|
|
1377
|
+
: storedBeforeCommit.has(conflictKey)
|
|
1378
|
+
? storedBeforeCommit.get(conflictKey)
|
|
1379
|
+
: await tx.getRow(conflictOperation.table, conflictOperation.rowId);
|
|
1380
|
+
if (terminated.status === 'conflict') {
|
|
1381
|
+
terminated =
|
|
1382
|
+
conflictRow === undefined
|
|
1383
|
+
? errorRecord(terminated.opIndex, 'sync.row_missing', 'the conflicting row exists only through an earlier operation of the same commit (§6.4)').record
|
|
1384
|
+
: {
|
|
1385
|
+
...terminated,
|
|
1386
|
+
serverVersion: conflictRow.serverVersion,
|
|
1387
|
+
serverRow: conflictRow.payload,
|
|
1388
|
+
};
|
|
1266
1389
|
}
|
|
1267
1390
|
const stored = newStoredPushResult(createdAtMs, {
|
|
1268
1391
|
status: 'rejected',
|
|
1269
|
-
...(
|
|
1392
|
+
...(terminated.status === 'conflict' &&
|
|
1393
|
+
conflictOperation !== undefined &&
|
|
1394
|
+
conflictRow !== undefined
|
|
1270
1395
|
? {
|
|
1271
1396
|
conflictAuthorization: {
|
|
1272
1397
|
table: conflictOperation.table,
|