@powersync/service-core 0.10.1 → 0.12.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.
@@ -1,63 +1,7 @@
1
1
  import { OplogEntry } from '@/util/protocol-types.js';
2
- import { addChecksums } from '@/util/utils.js';
2
+ import { reduceBucket } from '@/util/utils.js';
3
3
  import { expect } from 'vitest';
4
4
 
5
- /**
6
- * Reduce a bucket to the final state as stored on the client.
7
- *
8
- * This keeps the final state for each row as a PUT operation.
9
- *
10
- * All other operations are replaced with a single CLEAR operation,
11
- * summing their checksums, and using a 0 as an op_id.
12
- *
13
- * This is the function $r(B)$, as described in /docs/bucket-properties.md.
14
- */
15
- export function reduceBucket(operations: OplogEntry[]) {
16
- let rowState = new Map<string, OplogEntry>();
17
- let otherChecksum = 0;
18
-
19
- for (let op of operations) {
20
- const key = rowKey(op);
21
- if (op.op == 'PUT') {
22
- const existing = rowState.get(key);
23
- if (existing) {
24
- otherChecksum = addChecksums(otherChecksum, existing.checksum as number);
25
- }
26
- rowState.set(key, op);
27
- } else if (op.op == 'REMOVE') {
28
- const existing = rowState.get(key);
29
- if (existing) {
30
- otherChecksum = addChecksums(otherChecksum, existing.checksum as number);
31
- }
32
- rowState.delete(key);
33
- otherChecksum = addChecksums(otherChecksum, op.checksum as number);
34
- } else if (op.op == 'CLEAR') {
35
- rowState.clear();
36
- otherChecksum = op.checksum as number;
37
- } else if (op.op == 'MOVE') {
38
- otherChecksum = addChecksums(otherChecksum, op.checksum as number);
39
- } else {
40
- throw new Error(`Unknown operation ${op.op}`);
41
- }
42
- }
43
-
44
- const puts = [...rowState.values()].sort((a, b) => {
45
- return Number(BigInt(a.op_id) - BigInt(b.op_id));
46
- });
47
-
48
- let finalState: OplogEntry[] = [
49
- // Special operation to indiciate the checksum remainder
50
- { op_id: '0', op: 'CLEAR', checksum: otherChecksum },
51
- ...puts
52
- ];
53
-
54
- return finalState;
55
- }
56
-
57
- function rowKey(entry: OplogEntry) {
58
- return `${entry.object_type}/${entry.object_id}/${entry.subkey}`;
59
- }
60
-
61
5
  /**
62
6
  * Validate this property, as described in /docs/bucket-properties.md:
63
7
  *
package/test/src/util.ts CHANGED
@@ -30,6 +30,8 @@ export interface StorageOptions {
30
30
  * Setting this to true will drop the collections completely.
31
31
  */
32
32
  dropAll?: boolean;
33
+
34
+ doNotClear?: boolean;
33
35
  }
34
36
  export type StorageFactory = (options?: StorageOptions) => Promise<BucketStorageFactory>;
35
37
 
@@ -37,7 +39,7 @@ export const MONGO_STORAGE_FACTORY: StorageFactory = async (options?: StorageOpt
37
39
  const db = await connectMongo();
38
40
  if (options?.dropAll) {
39
41
  await db.drop();
40
- } else {
42
+ } else if (!options?.doNotClear) {
41
43
  await db.clear();
42
44
  }
43
45
  return new MongoBucketStorage(db, { slot_name_prefix: 'test_' });