@powersync/service-core 0.13.0 → 0.15.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.
Files changed (181) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/dist/entry/commands/compact-action.js +14 -14
  3. package/dist/entry/commands/compact-action.js.map +1 -1
  4. package/dist/entry/commands/migrate-action.js +15 -4
  5. package/dist/entry/commands/migrate-action.js.map +1 -1
  6. package/dist/index.d.ts +1 -3
  7. package/dist/index.js +1 -3
  8. package/dist/index.js.map +1 -1
  9. package/dist/migrations/PowerSyncMigrationManager.d.ts +17 -0
  10. package/dist/migrations/PowerSyncMigrationManager.js +21 -0
  11. package/dist/migrations/PowerSyncMigrationManager.js.map +1 -0
  12. package/dist/migrations/ensure-automatic-migrations.d.ts +4 -0
  13. package/dist/migrations/ensure-automatic-migrations.js +14 -0
  14. package/dist/migrations/ensure-automatic-migrations.js.map +1 -0
  15. package/dist/migrations/migrations-index.d.ts +2 -3
  16. package/dist/migrations/migrations-index.js +2 -3
  17. package/dist/migrations/migrations-index.js.map +1 -1
  18. package/dist/routes/configure-fastify.d.ts +12 -12
  19. package/dist/routes/endpoints/admin.d.ts +24 -24
  20. package/dist/storage/BucketStorage.d.ts +51 -3
  21. package/dist/storage/BucketStorage.js +26 -0
  22. package/dist/storage/BucketStorage.js.map +1 -1
  23. package/dist/storage/bson.d.ts +24 -0
  24. package/dist/storage/bson.js +73 -0
  25. package/dist/storage/bson.js.map +1 -0
  26. package/dist/storage/storage-index.d.ts +3 -14
  27. package/dist/storage/storage-index.js +3 -14
  28. package/dist/storage/storage-index.js.map +1 -1
  29. package/dist/sync/sync.js +3 -1
  30. package/dist/sync/sync.js.map +1 -1
  31. package/dist/system/ServiceContext.d.ts +3 -0
  32. package/dist/system/ServiceContext.js +11 -3
  33. package/dist/system/ServiceContext.js.map +1 -1
  34. package/dist/util/config/types.d.ts +2 -2
  35. package/dist/util/utils.d.ts +17 -1
  36. package/dist/util/utils.js +49 -1
  37. package/dist/util/utils.js.map +1 -1
  38. package/package.json +7 -8
  39. package/src/entry/commands/compact-action.ts +19 -14
  40. package/src/entry/commands/migrate-action.ts +17 -4
  41. package/src/index.ts +1 -4
  42. package/src/migrations/PowerSyncMigrationManager.ts +42 -0
  43. package/src/migrations/ensure-automatic-migrations.ts +15 -0
  44. package/src/migrations/migrations-index.ts +2 -3
  45. package/src/storage/BucketStorage.ts +59 -3
  46. package/src/storage/bson.ts +78 -0
  47. package/src/storage/storage-index.ts +3 -15
  48. package/src/sync/sync.ts +3 -1
  49. package/src/system/ServiceContext.ts +17 -4
  50. package/src/util/config/types.ts +2 -2
  51. package/src/util/utils.ts +47 -1
  52. package/test/src/env.ts +0 -1
  53. package/tsconfig.tsbuildinfo +1 -1
  54. package/dist/db/db-index.d.ts +0 -1
  55. package/dist/db/db-index.js +0 -2
  56. package/dist/db/db-index.js.map +0 -1
  57. package/dist/db/mongo.d.ts +0 -35
  58. package/dist/db/mongo.js +0 -73
  59. package/dist/db/mongo.js.map +0 -1
  60. package/dist/locks/LockManager.d.ts +0 -10
  61. package/dist/locks/LockManager.js +0 -7
  62. package/dist/locks/LockManager.js.map +0 -1
  63. package/dist/locks/MongoLocks.d.ts +0 -36
  64. package/dist/locks/MongoLocks.js +0 -81
  65. package/dist/locks/MongoLocks.js.map +0 -1
  66. package/dist/locks/locks-index.d.ts +0 -2
  67. package/dist/locks/locks-index.js +0 -3
  68. package/dist/locks/locks-index.js.map +0 -1
  69. package/dist/migrations/db/migrations/1684951997326-init.d.ts +0 -3
  70. package/dist/migrations/db/migrations/1684951997326-init.js +0 -33
  71. package/dist/migrations/db/migrations/1684951997326-init.js.map +0 -1
  72. package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.d.ts +0 -2
  73. package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js +0 -5
  74. package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js.map +0 -1
  75. package/dist/migrations/db/migrations/1702295701188-sync-rule-state.d.ts +0 -3
  76. package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js +0 -56
  77. package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js.map +0 -1
  78. package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.d.ts +0 -3
  79. package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.js +0 -29
  80. package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.js.map +0 -1
  81. package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.d.ts +0 -3
  82. package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.js +0 -31
  83. package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.js.map +0 -1
  84. package/dist/migrations/definitions.d.ts +0 -18
  85. package/dist/migrations/definitions.js +0 -6
  86. package/dist/migrations/definitions.js.map +0 -1
  87. package/dist/migrations/executor.d.ts +0 -16
  88. package/dist/migrations/executor.js +0 -64
  89. package/dist/migrations/executor.js.map +0 -1
  90. package/dist/migrations/migrations.d.ts +0 -18
  91. package/dist/migrations/migrations.js +0 -110
  92. package/dist/migrations/migrations.js.map +0 -1
  93. package/dist/migrations/store/migration-store.d.ts +0 -11
  94. package/dist/migrations/store/migration-store.js +0 -46
  95. package/dist/migrations/store/migration-store.js.map +0 -1
  96. package/dist/storage/MongoBucketStorage.d.ts +0 -48
  97. package/dist/storage/MongoBucketStorage.js +0 -427
  98. package/dist/storage/MongoBucketStorage.js.map +0 -1
  99. package/dist/storage/mongo/MongoBucketBatch.d.ts +0 -74
  100. package/dist/storage/mongo/MongoBucketBatch.js +0 -683
  101. package/dist/storage/mongo/MongoBucketBatch.js.map +0 -1
  102. package/dist/storage/mongo/MongoCompactor.d.ts +0 -40
  103. package/dist/storage/mongo/MongoCompactor.js +0 -310
  104. package/dist/storage/mongo/MongoCompactor.js.map +0 -1
  105. package/dist/storage/mongo/MongoIdSequence.d.ts +0 -12
  106. package/dist/storage/mongo/MongoIdSequence.js +0 -21
  107. package/dist/storage/mongo/MongoIdSequence.js.map +0 -1
  108. package/dist/storage/mongo/MongoPersistedSyncRules.d.ts +0 -9
  109. package/dist/storage/mongo/MongoPersistedSyncRules.js +0 -9
  110. package/dist/storage/mongo/MongoPersistedSyncRules.js.map +0 -1
  111. package/dist/storage/mongo/MongoPersistedSyncRulesContent.d.ts +0 -20
  112. package/dist/storage/mongo/MongoPersistedSyncRulesContent.js +0 -26
  113. package/dist/storage/mongo/MongoPersistedSyncRulesContent.js.map +0 -1
  114. package/dist/storage/mongo/MongoStorageProvider.d.ts +0 -5
  115. package/dist/storage/mongo/MongoStorageProvider.js +0 -26
  116. package/dist/storage/mongo/MongoStorageProvider.js.map +0 -1
  117. package/dist/storage/mongo/MongoSyncBucketStorage.d.ts +0 -38
  118. package/dist/storage/mongo/MongoSyncBucketStorage.js +0 -534
  119. package/dist/storage/mongo/MongoSyncBucketStorage.js.map +0 -1
  120. package/dist/storage/mongo/MongoSyncRulesLock.d.ts +0 -16
  121. package/dist/storage/mongo/MongoSyncRulesLock.js +0 -65
  122. package/dist/storage/mongo/MongoSyncRulesLock.js.map +0 -1
  123. package/dist/storage/mongo/MongoWriteCheckpointAPI.d.ts +0 -20
  124. package/dist/storage/mongo/MongoWriteCheckpointAPI.js +0 -104
  125. package/dist/storage/mongo/MongoWriteCheckpointAPI.js.map +0 -1
  126. package/dist/storage/mongo/OperationBatch.d.ts +0 -35
  127. package/dist/storage/mongo/OperationBatch.js +0 -119
  128. package/dist/storage/mongo/OperationBatch.js.map +0 -1
  129. package/dist/storage/mongo/PersistedBatch.d.ts +0 -46
  130. package/dist/storage/mongo/PersistedBatch.js +0 -223
  131. package/dist/storage/mongo/PersistedBatch.js.map +0 -1
  132. package/dist/storage/mongo/config.d.ts +0 -19
  133. package/dist/storage/mongo/config.js +0 -26
  134. package/dist/storage/mongo/config.js.map +0 -1
  135. package/dist/storage/mongo/db.d.ts +0 -36
  136. package/dist/storage/mongo/db.js +0 -47
  137. package/dist/storage/mongo/db.js.map +0 -1
  138. package/dist/storage/mongo/models.d.ts +0 -163
  139. package/dist/storage/mongo/models.js +0 -27
  140. package/dist/storage/mongo/models.js.map +0 -1
  141. package/dist/storage/mongo/util.d.ts +0 -54
  142. package/dist/storage/mongo/util.js +0 -190
  143. package/dist/storage/mongo/util.js.map +0 -1
  144. package/src/db/db-index.ts +0 -1
  145. package/src/db/mongo.ts +0 -81
  146. package/src/locks/LockManager.ts +0 -16
  147. package/src/locks/MongoLocks.ts +0 -142
  148. package/src/locks/locks-index.ts +0 -2
  149. package/src/migrations/db/migrations/1684951997326-init.ts +0 -38
  150. package/src/migrations/db/migrations/1688556755264-initial-sync-rules.ts +0 -5
  151. package/src/migrations/db/migrations/1702295701188-sync-rule-state.ts +0 -102
  152. package/src/migrations/db/migrations/1711543888062-write-checkpoint-index.ts +0 -34
  153. package/src/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.ts +0 -37
  154. package/src/migrations/definitions.ts +0 -21
  155. package/src/migrations/executor.ts +0 -87
  156. package/src/migrations/migrations.ts +0 -142
  157. package/src/migrations/store/migration-store.ts +0 -63
  158. package/src/storage/MongoBucketStorage.ts +0 -541
  159. package/src/storage/mongo/MongoBucketBatch.ts +0 -900
  160. package/src/storage/mongo/MongoCompactor.ts +0 -393
  161. package/src/storage/mongo/MongoIdSequence.ts +0 -24
  162. package/src/storage/mongo/MongoPersistedSyncRules.ts +0 -16
  163. package/src/storage/mongo/MongoPersistedSyncRulesContent.ts +0 -50
  164. package/src/storage/mongo/MongoStorageProvider.ts +0 -31
  165. package/src/storage/mongo/MongoSyncBucketStorage.ts +0 -640
  166. package/src/storage/mongo/MongoSyncRulesLock.ts +0 -85
  167. package/src/storage/mongo/MongoWriteCheckpointAPI.ts +0 -154
  168. package/src/storage/mongo/OperationBatch.ts +0 -131
  169. package/src/storage/mongo/PersistedBatch.ts +0 -285
  170. package/src/storage/mongo/config.ts +0 -40
  171. package/src/storage/mongo/db.ts +0 -88
  172. package/src/storage/mongo/models.ts +0 -187
  173. package/src/storage/mongo/util.ts +0 -203
  174. package/test/src/__snapshots__/sync.test.ts.snap +0 -332
  175. package/test/src/bucket_validation.test.ts +0 -143
  176. package/test/src/bucket_validation.ts +0 -60
  177. package/test/src/compacting.test.ts +0 -295
  178. package/test/src/data_storage.test.ts +0 -1569
  179. package/test/src/stream_utils.ts +0 -42
  180. package/test/src/sync.test.ts +0 -511
  181. package/test/src/util.ts +0 -150
@@ -1,332 +0,0 @@
1
- // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2
-
3
- exports[`sync - mongodb > compacting data - invalidate checkpoint 1`] = `
4
- [
5
- {
6
- "checkpoint": {
7
- "buckets": [
8
- {
9
- "bucket": "mybucket[]",
10
- "checksum": -93886621,
11
- "count": 2,
12
- },
13
- ],
14
- "last_op_id": "2",
15
- "write_checkpoint": undefined,
16
- },
17
- },
18
- ]
19
- `;
20
-
21
- exports[`sync - mongodb > compacting data - invalidate checkpoint 2`] = `
22
- [
23
- {
24
- "data": {
25
- "after": "0",
26
- "bucket": "mybucket[]",
27
- "data": [
28
- {
29
- "checksum": -93886621n,
30
- "op": "CLEAR",
31
- "op_id": "2",
32
- },
33
- ],
34
- "has_more": false,
35
- "next_after": "2",
36
- },
37
- },
38
- {
39
- "checkpoint_diff": {
40
- "last_op_id": "4",
41
- "removed_buckets": [],
42
- "updated_buckets": [
43
- {
44
- "bucket": "mybucket[]",
45
- "checksum": 499012468,
46
- "count": 4,
47
- },
48
- ],
49
- "write_checkpoint": undefined,
50
- },
51
- },
52
- {
53
- "data": {
54
- "after": "2",
55
- "bucket": "mybucket[]",
56
- "data": [
57
- {
58
- "checksum": 1859363232n,
59
- "data": "{"id":"t1","description":"Test 1b"}",
60
- "object_id": "t1",
61
- "object_type": "test",
62
- "op": "PUT",
63
- "op_id": "3",
64
- "subkey": "e5aa2ddc-1328-58fa-a000-0b5ed31eaf1a",
65
- },
66
- {
67
- "checksum": 3028503153n,
68
- "data": "{"id":"t2","description":"Test 2b"}",
69
- "object_id": "t2",
70
- "object_type": "test",
71
- "op": "PUT",
72
- "op_id": "4",
73
- "subkey": "13423353-9f27-59b4-baf0-64a5e09f1769",
74
- },
75
- ],
76
- "has_more": false,
77
- "next_after": "4",
78
- },
79
- },
80
- {
81
- "checkpoint_complete": {
82
- "last_op_id": "4",
83
- },
84
- },
85
- ]
86
- `;
87
-
88
- exports[`sync - mongodb > expired token 1`] = `
89
- [
90
- {
91
- "token_expires_in": 0,
92
- },
93
- ]
94
- `;
95
-
96
- exports[`sync - mongodb > expiring token 1`] = `
97
- [
98
- {
99
- "checkpoint": {
100
- "buckets": [
101
- {
102
- "bucket": "mybucket[]",
103
- "checksum": 0,
104
- "count": 0,
105
- },
106
- ],
107
- "last_op_id": "0",
108
- "write_checkpoint": undefined,
109
- },
110
- },
111
- {
112
- "checkpoint_complete": {
113
- "last_op_id": "0",
114
- },
115
- },
116
- ]
117
- `;
118
-
119
- exports[`sync - mongodb > expiring token 2`] = `
120
- [
121
- {
122
- "token_expires_in": 0,
123
- },
124
- ]
125
- `;
126
-
127
- exports[`sync - mongodb > sync global data 1`] = `
128
- [
129
- {
130
- "checkpoint": {
131
- "buckets": [
132
- {
133
- "bucket": "mybucket[]",
134
- "checksum": -93886621,
135
- "count": 2,
136
- },
137
- ],
138
- "last_op_id": "2",
139
- "write_checkpoint": undefined,
140
- },
141
- },
142
- {
143
- "data": {
144
- "after": "0",
145
- "bucket": "mybucket[]",
146
- "data": [
147
- {
148
- "checksum": 920318466n,
149
- "data": "{"id":"t1","description":"Test 1"}",
150
- "object_id": "t1",
151
- "object_type": "test",
152
- "op": "PUT",
153
- "op_id": "1",
154
- "subkey": "e5aa2ddc-1328-58fa-a000-0b5ed31eaf1a",
155
- },
156
- {
157
- "checksum": 3280762209n,
158
- "data": "{"id":"t2","description":"Test 2"}",
159
- "object_id": "t2",
160
- "object_type": "test",
161
- "op": "PUT",
162
- "op_id": "2",
163
- "subkey": "13423353-9f27-59b4-baf0-64a5e09f1769",
164
- },
165
- ],
166
- "has_more": false,
167
- "next_after": "2",
168
- },
169
- },
170
- {
171
- "checkpoint_complete": {
172
- "last_op_id": "2",
173
- },
174
- },
175
- ]
176
- `;
177
-
178
- exports[`sync - mongodb > sync legacy non-raw data 1`] = `
179
- [
180
- {
181
- "checkpoint": {
182
- "buckets": [
183
- {
184
- "bucket": "mybucket[]",
185
- "checksum": -852817836,
186
- "count": 1,
187
- },
188
- ],
189
- "last_op_id": "1",
190
- "write_checkpoint": undefined,
191
- },
192
- },
193
- {
194
- "data": {
195
- "after": "0",
196
- "bucket": "mybucket[]",
197
- "data": [
198
- {
199
- "checksum": 3442149460n,
200
- "data": {
201
- "description": "Test
202
- "string"",
203
- "id": "t1",
204
- "large_num": 12345678901234567890n,
205
- },
206
- "object_id": "t1",
207
- "object_type": "test",
208
- "op": "PUT",
209
- "op_id": "1",
210
- "subkey": "e5aa2ddc-1328-58fa-a000-0b5ed31eaf1a",
211
- },
212
- ],
213
- "has_more": false,
214
- "next_after": "1",
215
- },
216
- },
217
- {
218
- "checkpoint_complete": {
219
- "last_op_id": "1",
220
- },
221
- },
222
- ]
223
- `;
224
-
225
- exports[`sync - mongodb > sync updates to global data 1`] = `
226
- [
227
- {
228
- "checkpoint": {
229
- "buckets": [
230
- {
231
- "bucket": "mybucket[]",
232
- "checksum": 0,
233
- "count": 0,
234
- },
235
- ],
236
- "last_op_id": "0",
237
- "write_checkpoint": undefined,
238
- },
239
- },
240
- {
241
- "checkpoint_complete": {
242
- "last_op_id": "0",
243
- },
244
- },
245
- ]
246
- `;
247
-
248
- exports[`sync - mongodb > sync updates to global data 2`] = `
249
- [
250
- {
251
- "checkpoint_diff": {
252
- "last_op_id": "1",
253
- "removed_buckets": [],
254
- "updated_buckets": [
255
- {
256
- "bucket": "mybucket[]",
257
- "checksum": 920318466,
258
- "count": 1,
259
- },
260
- ],
261
- "write_checkpoint": undefined,
262
- },
263
- },
264
- {
265
- "data": {
266
- "after": "0",
267
- "bucket": "mybucket[]",
268
- "data": [
269
- {
270
- "checksum": 920318466n,
271
- "data": "{"id":"t1","description":"Test 1"}",
272
- "object_id": "t1",
273
- "object_type": "test",
274
- "op": "PUT",
275
- "op_id": "1",
276
- "subkey": "e5aa2ddc-1328-58fa-a000-0b5ed31eaf1a",
277
- },
278
- ],
279
- "has_more": false,
280
- "next_after": "1",
281
- },
282
- },
283
- {
284
- "checkpoint_complete": {
285
- "last_op_id": "1",
286
- },
287
- },
288
- ]
289
- `;
290
-
291
- exports[`sync - mongodb > sync updates to global data 3`] = `
292
- [
293
- {
294
- "checkpoint_diff": {
295
- "last_op_id": "2",
296
- "removed_buckets": [],
297
- "updated_buckets": [
298
- {
299
- "bucket": "mybucket[]",
300
- "checksum": -93886621,
301
- "count": 2,
302
- },
303
- ],
304
- "write_checkpoint": undefined,
305
- },
306
- },
307
- {
308
- "data": {
309
- "after": "1",
310
- "bucket": "mybucket[]",
311
- "data": [
312
- {
313
- "checksum": 3280762209n,
314
- "data": "{"id":"t2","description":"Test 2"}",
315
- "object_id": "t2",
316
- "object_type": "test",
317
- "op": "PUT",
318
- "op_id": "2",
319
- "subkey": "13423353-9f27-59b4-baf0-64a5e09f1769",
320
- },
321
- ],
322
- "has_more": false,
323
- "next_after": "2",
324
- },
325
- },
326
- {
327
- "checkpoint_complete": {
328
- "last_op_id": "2",
329
- },
330
- },
331
- ]
332
- `;
@@ -1,143 +0,0 @@
1
- import { OplogEntry } from '@/util/protocol-types.js';
2
- import { describe, expect, test } from 'vitest';
3
- import { validateBucket } from './bucket_validation.js';
4
- import { reduceBucket } from '@/index.js';
5
-
6
- // This tests the reduceBucket function.
7
- // While this function is not used directly in the service implementation,
8
- // it is an important part of validating consistency in other tests.
9
- describe('bucket validation', () => {
10
- const ops1: OplogEntry[] = [
11
- {
12
- op_id: '1',
13
- op: 'PUT',
14
- object_type: 'test',
15
- object_id: 't1',
16
- checksum: 2634521662,
17
- subkey: '6544e3899293153fa7b38331/117ab485-4b42-58a2-ab32-0053a22c3423',
18
- data: '{"id":"t1"}'
19
- },
20
- {
21
- op_id: '2',
22
- op: 'PUT',
23
- object_type: 'test',
24
- object_id: 't2',
25
- checksum: 4243212114,
26
- subkey: '6544e3899293153fa7b38331/ec27c691-b47a-5d92-927a-9944feb89eee',
27
- data: '{"id":"t2"}'
28
- },
29
- {
30
- op_id: '3',
31
- op: 'REMOVE',
32
- object_type: 'test',
33
- object_id: 't1',
34
- checksum: 4228978084,
35
- subkey: '6544e3899293153fa7b38331/117ab485-4b42-58a2-ab32-0053a22c3423',
36
- data: null
37
- },
38
- {
39
- op_id: '4',
40
- op: 'PUT',
41
- object_type: 'test',
42
- object_id: 't2',
43
- checksum: 4243212114,
44
- subkey: '6544e3899293153fa7b38331/ec27c691-b47a-5d92-927a-9944feb89eee',
45
- data: '{"id":"t2"}'
46
- }
47
- ];
48
-
49
- test('reduce 1', () => {
50
- expect(reduceBucket(ops1)).toEqual([
51
- {
52
- checksum: -1778190028,
53
- op: 'CLEAR',
54
- op_id: '0'
55
- },
56
- {
57
- checksum: 4243212114,
58
- data: '{"id":"t2"}',
59
- object_id: 't2',
60
- object_type: 'test',
61
- op: 'PUT',
62
- op_id: '4',
63
- subkey: '6544e3899293153fa7b38331/ec27c691-b47a-5d92-927a-9944feb89eee'
64
- }
65
- ]);
66
-
67
- expect(reduceBucket(reduceBucket(ops1))).toEqual([
68
- {
69
- checksum: -1778190028,
70
- op: 'CLEAR',
71
- op_id: '0'
72
- },
73
- {
74
- checksum: 4243212114,
75
- data: '{"id":"t2"}',
76
- object_id: 't2',
77
- object_type: 'test',
78
- op: 'PUT',
79
- op_id: '4',
80
- subkey: '6544e3899293153fa7b38331/ec27c691-b47a-5d92-927a-9944feb89eee'
81
- }
82
- ]);
83
-
84
- validateBucket(ops1);
85
- });
86
-
87
- test('reduce 2', () => {
88
- const bucket: OplogEntry[] = [
89
- ...ops1,
90
-
91
- {
92
- checksum: 93784613,
93
- op: 'CLEAR',
94
- op_id: '5'
95
- },
96
- {
97
- checksum: 5133378,
98
- data: '{"id":"t3"}',
99
- object_id: 't3',
100
- object_type: 'test',
101
- op: 'PUT',
102
- op_id: '11',
103
- subkey: '6544e3899293153fa7b38333/ec27c691-b47a-5d92-927a-9944feb89eee'
104
- }
105
- ];
106
-
107
- expect(reduceBucket(bucket)).toEqual([
108
- {
109
- checksum: 93784613,
110
- op: 'CLEAR',
111
- op_id: '0'
112
- },
113
- {
114
- checksum: 5133378,
115
- data: '{"id":"t3"}',
116
- object_id: 't3',
117
- object_type: 'test',
118
- op: 'PUT',
119
- op_id: '11',
120
- subkey: '6544e3899293153fa7b38333/ec27c691-b47a-5d92-927a-9944feb89eee'
121
- }
122
- ]);
123
-
124
- expect(reduceBucket(reduceBucket(bucket))).toEqual([
125
- {
126
- checksum: 93784613,
127
- op: 'CLEAR',
128
- op_id: '0'
129
- },
130
- {
131
- checksum: 5133378,
132
- data: '{"id":"t3"}',
133
- object_id: 't3',
134
- object_type: 'test',
135
- op: 'PUT',
136
- op_id: '11',
137
- subkey: '6544e3899293153fa7b38333/ec27c691-b47a-5d92-927a-9944feb89eee'
138
- }
139
- ]);
140
-
141
- validateBucket(bucket);
142
- });
143
- });
@@ -1,60 +0,0 @@
1
- import { OplogEntry } from '@/util/protocol-types.js';
2
- import { reduceBucket } from '@/util/utils.js';
3
- import { expect } from 'vitest';
4
-
5
- /**
6
- * Validate this property, as described in /docs/bucket-properties.md:
7
- *
8
- * $r(B_{[..id_n]}) = r(r(B_{[..id_i]}) \cup B_{[id_{i+1}..id_n]}) \;\forall\; i \in [1..n]$
9
- *
10
- * We test that a client syncing the entire bucket in one go (left side of the equation),
11
- * ends up with the same result as another client syncing up to operation id_i, then sync
12
- * the rest.
13
- */
14
- export function validateBucket(bucket: OplogEntry[]) {
15
- const r1 = reduceBucket(bucket);
16
- for (let i = 0; i <= bucket.length; i++) {
17
- const r2 = reduceBucket(bucket.slice(0, i + 1));
18
- const b3 = bucket.slice(i + 1);
19
- const r3 = r2.concat(b3);
20
- const r4 = reduceBucket(r3);
21
- expect(r4).toEqual(r1);
22
- }
23
-
24
- // This is the same check, just implemented differently
25
- validateCompactedBucket(bucket, bucket);
26
- }
27
-
28
- /**
29
- * Validate these properties for a bucket $B$ and its compacted version $B'$,:
30
- * as described in /docs/bucket-properties.md:
31
- *
32
- * 1. $r(B) = r(B')$
33
- * 2. $r(B_{[..c]}) = r(r(B_{[..c_i]}) \cup B'_{[c_i+1..c]}) \;\forall\; c_i \in B$
34
- *
35
- * The first one is that the result of syncing the original bucket is the same as
36
- * syncing the compacted bucket.
37
- *
38
- * The second property is that result of syncing the entire original bucket, is the same
39
- * as syncing any partial version of that (up to op $c_i$), and then continue syncing
40
- * using the compacted bucket.
41
- */
42
- export function validateCompactedBucket(bucket: OplogEntry[], compacted: OplogEntry[]) {
43
- // r(B_{[..c]})
44
- const r1 = reduceBucket(bucket);
45
- // r(B) = r(B')
46
- expect(reduceBucket(compacted)).toEqual(r1);
47
-
48
- for (let i = 0; i < bucket.length; i++) {
49
- // r(B_{[..c_i]})
50
- const r2 = reduceBucket(bucket.slice(0, i + 1));
51
- const c_i = BigInt(bucket[i].op_id);
52
- // B'_{[c_i+1..c]}
53
- const b3 = compacted.filter((op) => BigInt(op.op_id) > c_i);
54
- // r(B_{[..c_i]}) \cup B'_{[c_i+1..c]}
55
- const r3 = r2.concat(b3);
56
- // r(r(B_{[..c_i]}) \cup B'_{[c_i+1..c]})
57
- const r4 = reduceBucket(r3);
58
- expect(r4).toEqual(r1);
59
- }
60
- }