|
| 1 | +import type * as types from '@powersync/service-core'; |
| 2 | + |
| 3 | +export type BucketData = Record<string, types.OplogEntry[]>; |
| 4 | + |
| 5 | +export function normalizeData( |
| 6 | + checkpoint: types.StreamingSyncCheckpoint, |
| 7 | + chunks: types.StreamingSyncData[], |
| 8 | + options: { raw: boolean } |
| 9 | +) { |
| 10 | + const lastOpId = BigInt(checkpoint.checkpoint.last_op_id); |
| 11 | + let buckets: BucketData = {}; |
| 12 | + for (let { |
| 13 | + data: { bucket, data } |
| 14 | + } of chunks) { |
| 15 | + buckets[bucket] ??= []; |
| 16 | + for (let entry of data) { |
| 17 | + if (BigInt(entry.op_id) > lastOpId) { |
| 18 | + continue; |
| 19 | + } |
| 20 | + buckets[bucket].push(entry); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + if (options.raw) { |
| 25 | + return buckets; |
| 26 | + } |
| 27 | + |
| 28 | + return Object.fromEntries( |
| 29 | + Object.entries(buckets).map(([bucket, entries]) => { |
| 30 | + return [bucket, reduceBucket(entries)]; |
| 31 | + }) |
| 32 | + ); |
| 33 | +} |
| 34 | + |
| 35 | +export function isStreamingSyncData(line: types.StreamingSyncLine): line is types.StreamingSyncData { |
| 36 | + return (line as types.StreamingSyncData).data != null; |
| 37 | +} |
| 38 | + |
| 39 | +export function isCheckpointComplete(line: types.StreamingSyncLine): line is types.StreamingSyncCheckpointComplete { |
| 40 | + return (line as types.StreamingSyncCheckpointComplete).checkpoint_complete != null; |
| 41 | +} |
| 42 | +export function isCheckpoint(line: types.StreamingSyncLine): line is types.StreamingSyncCheckpoint { |
| 43 | + return (line as types.StreamingSyncCheckpoint).checkpoint != null; |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Reduce a bucket to the final state as stored on the client. |
| 48 | + * |
| 49 | + * This keeps the final state for each row as a PUT operation. |
| 50 | + * |
| 51 | + * All other operations are replaced with a single CLEAR operation, |
| 52 | + * summing their checksums, and using a 0 as an op_id. |
| 53 | + * |
| 54 | + * This is the function $r(B)$, as described in /docs/bucket-properties.md. |
| 55 | + */ |
| 56 | +export function reduceBucket(operations: types.OplogEntry[]) { |
| 57 | + let rowState = new Map<string, types.OplogEntry>(); |
| 58 | + let otherChecksum = 0; |
| 59 | + |
| 60 | + for (let op of operations) { |
| 61 | + const key = rowKey(op); |
| 62 | + if (op.op == 'PUT') { |
| 63 | + const existing = rowState.get(key); |
| 64 | + if (existing) { |
| 65 | + otherChecksum = addChecksums(otherChecksum, existing.checksum as number); |
| 66 | + } |
| 67 | + rowState.set(key, op); |
| 68 | + } else if (op.op == 'REMOVE') { |
| 69 | + const existing = rowState.get(key); |
| 70 | + if (existing) { |
| 71 | + otherChecksum = addChecksums(otherChecksum, existing.checksum as number); |
| 72 | + } |
| 73 | + rowState.delete(key); |
| 74 | + otherChecksum = addChecksums(otherChecksum, op.checksum as number); |
| 75 | + } else if (op.op == 'CLEAR') { |
| 76 | + rowState.clear(); |
| 77 | + otherChecksum = op.checksum as number; |
| 78 | + } else if (op.op == 'MOVE') { |
| 79 | + otherChecksum = addChecksums(otherChecksum, op.checksum as number); |
| 80 | + } else { |
| 81 | + throw new Error(`Unknown operation ${op.op}`); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + const puts = [...rowState.values()].sort((a, b) => { |
| 86 | + return Number(BigInt(a.op_id) - BigInt(b.op_id)); |
| 87 | + }); |
| 88 | + |
| 89 | + let finalState: types.OplogEntry[] = [ |
| 90 | + // Special operation to indiciate the checksum remainder |
| 91 | + { op_id: '0', op: 'CLEAR', checksum: otherChecksum }, |
| 92 | + ...puts |
| 93 | + ]; |
| 94 | + |
| 95 | + return finalState; |
| 96 | +} |
| 97 | + |
| 98 | +function rowKey(entry: types.OplogEntry) { |
| 99 | + return `${entry.object_type}/${entry.object_id}/${entry.subkey}`; |
| 100 | +} |
| 101 | + |
| 102 | +export function addChecksums(a: number, b: number) { |
| 103 | + return (a + b) & 0xffffffff; |
| 104 | +} |
0 commit comments