@powersync/service-module-mongodb-storage 0.0.0-dev-20260602073133 → 0.0.0-dev-20260909133214
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/CHANGELOG.md +177 -5
- package/dist/migrations/db/migrations/1782950400000-checkpoint-requested-at-index.d.ts +3 -0
- package/dist/migrations/db/migrations/1782950400000-checkpoint-requested-at-index.js +41 -0
- package/dist/migrations/db/migrations/1782950400000-checkpoint-requested-at-index.js.map +1 -0
- package/dist/storage/MongoBucketStorage.d.ts +71 -18
- package/dist/storage/MongoBucketStorage.js +516 -202
- package/dist/storage/MongoBucketStorage.js.map +1 -1
- package/dist/storage/implementation/MongoBucketBatch.d.ts +71 -29
- package/dist/storage/implementation/MongoBucketBatch.js +140 -66
- package/dist/storage/implementation/MongoBucketBatch.js.map +1 -1
- package/dist/storage/implementation/MongoChecksums.d.ts +41 -13
- package/dist/storage/implementation/MongoChecksums.js +36 -121
- package/dist/storage/implementation/MongoChecksums.js.map +1 -1
- package/dist/storage/implementation/MongoCompactor.d.ts +28 -90
- package/dist/storage/implementation/MongoCompactor.js +87 -481
- package/dist/storage/implementation/MongoCompactor.js.map +1 -1
- package/dist/storage/implementation/MongoHydrationState.d.ts +11 -0
- package/dist/storage/implementation/MongoHydrationState.js +49 -0
- package/dist/storage/implementation/MongoHydrationState.js.map +1 -0
- package/dist/storage/implementation/MongoParameterCompactor.d.ts +82 -8
- package/dist/storage/implementation/MongoParameterCompactor.js +312 -76
- package/dist/storage/implementation/MongoParameterCompactor.js.map +1 -1
- package/dist/storage/implementation/MongoParsedSyncConfigSet.d.ts +21 -0
- package/dist/storage/implementation/MongoParsedSyncConfigSet.js +93 -0
- package/dist/storage/implementation/MongoParsedSyncConfigSet.js.map +1 -0
- package/dist/storage/implementation/MongoPersistedReplicationStream.d.ts +36 -0
- package/dist/storage/implementation/MongoPersistedReplicationStream.js +90 -0
- package/dist/storage/implementation/MongoPersistedReplicationStream.js.map +1 -0
- package/dist/storage/implementation/MongoPersistedSyncConfigContent.d.ts +25 -0
- package/dist/storage/implementation/MongoPersistedSyncConfigContent.js +125 -0
- package/dist/storage/implementation/MongoPersistedSyncConfigContent.js.map +1 -0
- package/dist/storage/implementation/MongoStorageProvider.js +41 -4
- package/dist/storage/implementation/MongoStorageProvider.js.map +1 -1
- package/dist/storage/implementation/MongoSyncBucketStorage.d.ts +81 -37
- package/dist/storage/implementation/MongoSyncBucketStorage.js +159 -86
- package/dist/storage/implementation/MongoSyncBucketStorage.js.map +1 -1
- package/dist/storage/implementation/MongoSyncRulesLock.d.ts +1 -1
- package/dist/storage/implementation/MongoSyncRulesLock.js +23 -11
- package/dist/storage/implementation/MongoSyncRulesLock.js.map +1 -1
- package/dist/storage/implementation/MongoWriteCheckpointAPI.d.ts +11 -8
- package/dist/storage/implementation/MongoWriteCheckpointAPI.js +159 -33
- package/dist/storage/implementation/MongoWriteCheckpointAPI.js.map +1 -1
- package/dist/storage/implementation/SyncRuleStateUpdate.d.ts +11 -5
- package/dist/storage/implementation/SyncRuleStateUpdate.js +14 -6
- package/dist/storage/implementation/SyncRuleStateUpdate.js.map +1 -1
- package/dist/storage/implementation/common/MongoSyncBucketStorageCheckpoint.d.ts +28 -0
- package/dist/storage/implementation/common/MongoSyncBucketStorageCheckpoint.js +2 -0
- package/dist/storage/implementation/common/MongoSyncBucketStorageCheckpoint.js.map +1 -0
- package/dist/storage/implementation/common/PersistedBatch.d.ts +34 -2
- package/dist/storage/implementation/common/PersistedBatch.js +63 -10
- package/dist/storage/implementation/common/PersistedBatch.js.map +1 -1
- package/dist/storage/implementation/common/VersionedPowerSyncMongoBase.js +1 -1
- package/dist/storage/implementation/common/VersionedPowerSyncMongoBase.js.map +1 -1
- package/dist/storage/implementation/createMongoSyncBucketStorage.d.ts +2 -2
- package/dist/storage/implementation/createMongoSyncBucketStorage.js +4 -4
- package/dist/storage/implementation/createMongoSyncBucketStorage.js.map +1 -1
- package/dist/storage/implementation/db.d.ts +4 -1
- package/dist/storage/implementation/db.js +31 -16
- package/dist/storage/implementation/db.js.map +1 -1
- package/dist/storage/implementation/models.d.ts +55 -13
- package/dist/storage/implementation/models.js.map +1 -1
- package/dist/storage/implementation/v1/MongoBucketBatchV1.d.ts +15 -1
- package/dist/storage/implementation/v1/MongoBucketBatchV1.js +135 -107
- package/dist/storage/implementation/v1/MongoBucketBatchV1.js.map +1 -1
- package/dist/storage/implementation/v1/MongoChecksumsV1.d.ts +4 -4
- package/dist/storage/implementation/v1/MongoChecksumsV1.js +119 -7
- package/dist/storage/implementation/v1/MongoChecksumsV1.js.map +1 -1
- package/dist/storage/implementation/v1/MongoCompactorV1.d.ts +47 -7
- package/dist/storage/implementation/v1/MongoCompactorV1.js +478 -8
- package/dist/storage/implementation/v1/MongoCompactorV1.js.map +1 -1
- package/dist/storage/implementation/v1/MongoParameterCompactorV1.d.ts +16 -2
- package/dist/storage/implementation/v1/MongoParameterCompactorV1.js +20 -9
- package/dist/storage/implementation/v1/MongoParameterCompactorV1.js.map +1 -1
- package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.d.ts +33 -27
- package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.js +146 -73
- package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.js.map +1 -1
- package/dist/storage/implementation/v1/SingleBucketStoreV1.d.ts +9 -10
- package/dist/storage/implementation/v1/SingleBucketStoreV1.js +2 -8
- package/dist/storage/implementation/v1/SingleBucketStoreV1.js.map +1 -1
- package/dist/storage/implementation/v1/VersionedPowerSyncMongoV1.d.ts +2 -1
- package/dist/storage/implementation/v1/VersionedPowerSyncMongoV1.js +3 -0
- package/dist/storage/implementation/v1/VersionedPowerSyncMongoV1.js.map +1 -1
- package/dist/storage/implementation/v1/models.d.ts +10 -1
- package/dist/storage/implementation/v1/models.js +1 -0
- package/dist/storage/implementation/v1/models.js.map +1 -1
- package/dist/storage/implementation/v3/BucketDataContextV3.d.ts +13 -0
- package/dist/storage/implementation/v3/BucketDataContextV3.js +23 -0
- package/dist/storage/implementation/v3/BucketDataContextV3.js.map +1 -0
- package/dist/storage/implementation/v3/CompactionLease.d.ts +50 -0
- package/dist/storage/implementation/v3/CompactionLease.js +131 -0
- package/dist/storage/implementation/v3/CompactionLease.js.map +1 -0
- package/dist/storage/implementation/v3/MongoBucketBatchV3.d.ts +16 -5
- package/dist/storage/implementation/v3/MongoBucketBatchV3.js +393 -262
- package/dist/storage/implementation/v3/MongoBucketBatchV3.js.map +1 -1
- package/dist/storage/implementation/v3/MongoChecksumsV3.d.ts +30 -9
- package/dist/storage/implementation/v3/MongoChecksumsV3.js +161 -20
- package/dist/storage/implementation/v3/MongoChecksumsV3.js.map +1 -1
- package/dist/storage/implementation/v3/MongoCompactorV3.d.ts +108 -18
- package/dist/storage/implementation/v3/MongoCompactorV3.js +1115 -47
- package/dist/storage/implementation/v3/MongoCompactorV3.js.map +1 -1
- package/dist/storage/implementation/v3/MongoParameterCompactorV3.d.ts +12 -3
- package/dist/storage/implementation/v3/MongoParameterCompactorV3.js +13 -8
- package/dist/storage/implementation/v3/MongoParameterCompactorV3.js.map +1 -1
- package/dist/storage/implementation/v3/MongoStoppedSyncConfigCleanup.d.ts +54 -0
- package/dist/storage/implementation/v3/MongoStoppedSyncConfigCleanup.js +319 -0
- package/dist/storage/implementation/v3/MongoStoppedSyncConfigCleanup.js.map +1 -0
- package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.d.ts +61 -36
- package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.js +353 -161
- package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.js.map +1 -1
- package/dist/storage/implementation/v3/MongoWriteCheckpointAPIV3.d.ts +26 -0
- package/dist/storage/implementation/v3/MongoWriteCheckpointAPIV3.js +82 -0
- package/dist/storage/implementation/v3/MongoWriteCheckpointAPIV3.js.map +1 -0
- package/dist/storage/implementation/v3/PersistedBatchV3.d.ts +8 -3
- package/dist/storage/implementation/v3/PersistedBatchV3.js +131 -29
- package/dist/storage/implementation/v3/PersistedBatchV3.js.map +1 -1
- package/dist/storage/implementation/v3/SourceRecordStoreV3.d.ts +3 -3
- package/dist/storage/implementation/v3/SourceRecordStoreV3.js +13 -11
- package/dist/storage/implementation/v3/SourceRecordStoreV3.js.map +1 -1
- package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.d.ts +26 -14
- package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.js +73 -29
- package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.js.map +1 -1
- package/dist/storage/implementation/v3/bucket-format.d.ts +13 -0
- package/dist/storage/implementation/v3/bucket-format.js +79 -0
- package/dist/storage/implementation/v3/bucket-format.js.map +1 -0
- package/dist/storage/implementation/v3/chunking.d.ts +10 -0
- package/dist/storage/implementation/v3/chunking.js +30 -0
- package/dist/storage/implementation/v3/chunking.js.map +1 -0
- package/dist/storage/implementation/v3/compact-utils.d.ts +107 -0
- package/dist/storage/implementation/v3/compact-utils.js +216 -0
- package/dist/storage/implementation/v3/compact-utils.js.map +1 -0
- package/dist/storage/implementation/v3/compaction-constants.d.ts +5 -0
- package/dist/storage/implementation/v3/compaction-constants.js +6 -0
- package/dist/storage/implementation/v3/compaction-constants.js.map +1 -0
- package/dist/storage/implementation/v3/models.d.ts +187 -23
- package/dist/storage/implementation/v3/models.js +11 -28
- package/dist/storage/implementation/v3/models.js.map +1 -1
- package/dist/storage/implementation/v3/object-storage/BucketDataObjectStorage.d.ts +20 -0
- package/dist/storage/implementation/v3/object-storage/BucketDataObjectStorage.js +48 -0
- package/dist/storage/implementation/v3/object-storage/BucketDataObjectStorage.js.map +1 -0
- package/dist/storage/implementation/v3/object-storage/ObjectStorage.d.ts +33 -0
- package/dist/storage/implementation/v3/object-storage/ObjectStorage.js +12 -0
- package/dist/storage/implementation/v3/object-storage/ObjectStorage.js.map +1 -0
- package/dist/storage/implementation/v3/object-storage/ObjectStorageLifecycle.d.ts +68 -0
- package/dist/storage/implementation/v3/object-storage/ObjectStorageLifecycle.js +154 -0
- package/dist/storage/implementation/v3/object-storage/ObjectStorageLifecycle.js.map +1 -0
- package/dist/storage/implementation/v3/object-storage/ObjectStorageUsage.d.ts +37 -0
- package/dist/storage/implementation/v3/object-storage/ObjectStorageUsage.js +167 -0
- package/dist/storage/implementation/v3/object-storage/ObjectStorageUsage.js.map +1 -0
- package/dist/storage/implementation/v3/object-storage/S3ObjectStorage.d.ts +67 -0
- package/dist/storage/implementation/v3/object-storage/S3ObjectStorage.js +439 -0
- package/dist/storage/implementation/v3/object-storage/S3ObjectStorage.js.map +1 -0
- package/dist/storage/implementation/v3/source-table-utils.d.ts +63 -0
- package/dist/storage/implementation/v3/source-table-utils.js +206 -0
- package/dist/storage/implementation/v3/source-table-utils.js.map +1 -0
- package/dist/storage/storage-index.d.ts +5 -4
- package/dist/storage/storage-index.js +5 -4
- package/dist/storage/storage-index.js.map +1 -1
- package/dist/types/types.d.ts +71 -3
- package/dist/types/types.js +58 -2
- package/dist/types/types.js.map +1 -1
- package/dist/utils/test-utils.d.ts +1 -2
- package/dist/utils/test-utils.js +7 -1
- package/dist/utils/test-utils.js.map +1 -1
- package/dist/utils/util.d.ts +9 -2
- package/dist/utils/util.js +131 -5
- package/dist/utils/util.js.map +1 -1
- package/package.json +13 -19
- package/src/migrations/db/migrations/1782950400000-checkpoint-requested-at-index.ts +55 -0
- package/src/storage/MongoBucketStorage.ts +728 -242
- package/src/storage/implementation/MongoBucketBatch.ts +196 -91
- package/src/storage/implementation/MongoChecksums.ts +93 -153
- package/src/storage/implementation/MongoCompactor.ts +106 -631
- package/src/storage/implementation/MongoHydrationState.ts +61 -0
- package/src/storage/implementation/MongoParameterCompactor.ts +392 -87
- package/src/storage/implementation/MongoParsedSyncConfigSet.ts +122 -0
- package/src/storage/implementation/MongoPersistedReplicationStream.ts +118 -0
- package/src/storage/implementation/MongoPersistedSyncConfigContent.ts +156 -0
- package/src/storage/implementation/MongoStorageProvider.ts +48 -4
- package/src/storage/implementation/MongoSyncBucketStorage.ts +226 -118
- package/src/storage/implementation/MongoSyncRulesLock.ts +28 -12
- package/src/storage/implementation/MongoWriteCheckpointAPI.ts +194 -52
- package/src/storage/implementation/SyncRuleStateUpdate.ts +13 -6
- package/src/storage/implementation/common/MongoSyncBucketStorageCheckpoint.ts +31 -0
- package/src/storage/implementation/common/PersistedBatch.ts +83 -15
- package/src/storage/implementation/common/VersionedPowerSyncMongoBase.ts +1 -1
- package/src/storage/implementation/createMongoSyncBucketStorage.ts +21 -7
- package/src/storage/implementation/db.ts +40 -22
- package/src/storage/implementation/models.ts +58 -14
- package/src/storage/implementation/v1/MongoBucketBatchV1.ts +174 -125
- package/src/storage/implementation/v1/MongoChecksumsV1.ts +154 -12
- package/src/storage/implementation/v1/MongoCompactorV1.ts +594 -25
- package/src/storage/implementation/v1/MongoParameterCompactorV1.ts +25 -9
- package/src/storage/implementation/v1/MongoSyncBucketStorageV1.ts +226 -129
- package/src/storage/implementation/v1/SingleBucketStoreV1.ts +18 -27
- package/src/storage/implementation/v1/VersionedPowerSyncMongoV1.ts +11 -1
- package/src/storage/implementation/v1/models.ts +12 -1
- package/src/storage/implementation/v3/BucketDataContextV3.ts +32 -0
- package/src/storage/implementation/v3/CompactionLease.ts +152 -0
- package/src/storage/implementation/v3/MongoBucketBatchV3.ts +502 -324
- package/src/storage/implementation/v3/MongoChecksumsV3.ts +250 -32
- package/src/storage/implementation/v3/MongoCompactorV3.ts +1439 -70
- package/src/storage/implementation/v3/MongoParameterCompactorV3.ts +19 -9
- package/src/storage/implementation/v3/MongoStoppedSyncConfigCleanup.ts +473 -0
- package/src/storage/implementation/v3/MongoSyncBucketStorageV3.ts +453 -211
- package/src/storage/implementation/v3/MongoWriteCheckpointAPIV3.ts +111 -0
- package/src/storage/implementation/v3/PersistedBatchV3.ts +169 -45
- package/src/storage/implementation/v3/SourceRecordStoreV3.ts +12 -12
- package/src/storage/implementation/v3/VersionedPowerSyncMongoV3.ts +99 -38
- package/src/storage/implementation/v3/bucket-format.ts +99 -0
- package/src/storage/implementation/v3/chunking.ts +40 -0
- package/src/storage/implementation/v3/compact-utils.ts +320 -0
- package/src/storage/implementation/v3/compaction-constants.ts +5 -0
- package/src/storage/implementation/v3/models.ts +228 -58
- package/src/storage/implementation/v3/object-storage/BucketDataObjectStorage.ts +62 -0
- package/src/storage/implementation/v3/object-storage/ObjectStorage.ts +44 -0
- package/src/storage/implementation/v3/object-storage/ObjectStorageLifecycle.ts +182 -0
- package/src/storage/implementation/v3/object-storage/ObjectStorageUsage.ts +230 -0
- package/src/storage/implementation/v3/object-storage/S3ObjectStorage.ts +573 -0
- package/src/storage/implementation/v3/source-table-utils.ts +398 -0
- package/src/storage/storage-index.ts +14 -4
- package/src/types/types.ts +76 -2
- package/src/utils/test-utils.ts +8 -3
- package/src/utils/util.ts +188 -7
- package/test/src/__snapshots__/storage.test.ts.snap +4 -54
- package/test/src/__snapshots__/storage_sync.test.ts.snap +652 -645
- package/test/src/cleanup-stopped-sync-configs.test.ts +698 -0
- package/test/src/compact-utils.test.ts +226 -0
- package/test/src/helpers/MemoryObjectStorage.ts +61 -0
- package/test/src/helpers/s3TestFactory.ts +69 -0
- package/test/src/object_storage_lifecycle.test.ts +59 -0
- package/test/src/object_storage_usage.test.ts +255 -0
- package/test/src/parameter_compacting_v1.test.ts +120 -0
- package/test/src/parameter_compacting_v3.test.ts +192 -0
- package/test/src/parameter_compaction_fence.test.ts +276 -0
- package/test/src/setup.ts +6 -1
- package/test/src/storage.test.ts +478 -13
- package/test/src/storage_compacting.test.ts +2516 -107
- package/test/src/storage_object_storage_inline_threshold.test.ts +116 -0
- package/test/src/storage_s3_checksums.test.ts +190 -0
- package/test/src/storage_s3_compaction_lifecycle.test.ts +543 -0
- package/test/src/storage_s3_reading.test.ts +768 -0
- package/test/src/storage_s3_writing.test.ts +136 -0
- package/test/src/storage_sync.test.ts +1507 -76
- package/test/src/storeCurrentData.test.ts +227 -0
- package/test/src/util.ts +3 -2
- package/tsconfig.json +1 -4
- package/tsconfig.tsbuildinfo +1 -1
- package/dist/storage/implementation/BucketDefinitionMapping.d.ts +0 -15
- package/dist/storage/implementation/BucketDefinitionMapping.js +0 -58
- package/dist/storage/implementation/BucketDefinitionMapping.js.map +0 -1
- package/dist/storage/implementation/MongoPersistedSyncRules.d.ts +0 -14
- package/dist/storage/implementation/MongoPersistedSyncRules.js +0 -67
- package/dist/storage/implementation/MongoPersistedSyncRules.js.map +0 -1
- package/dist/storage/implementation/MongoPersistedSyncRulesContent.d.ts +0 -29
- package/dist/storage/implementation/MongoPersistedSyncRulesContent.js +0 -76
- package/dist/storage/implementation/MongoPersistedSyncRulesContent.js.map +0 -1
- package/dist/storage/implementation/common/MongoSyncBucketStorageContext.d.ts +0 -13
- package/dist/storage/implementation/common/MongoSyncBucketStorageContext.js +0 -2
- package/dist/storage/implementation/common/MongoSyncBucketStorageContext.js.map +0 -1
- package/dist/storage/implementation/common/SingleBucketStore.d.ts +0 -54
- package/dist/storage/implementation/common/SingleBucketStore.js +0 -3
- package/dist/storage/implementation/common/SingleBucketStore.js.map +0 -1
- package/dist/storage/implementation/v3/MongoParameterLookupV3.d.ts +0 -4
- package/dist/storage/implementation/v3/MongoParameterLookupV3.js +0 -9
- package/dist/storage/implementation/v3/MongoParameterLookupV3.js.map +0 -1
- package/dist/storage/implementation/v3/SingleBucketStoreV3.d.ts +0 -18
- package/dist/storage/implementation/v3/SingleBucketStoreV3.js +0 -48
- package/dist/storage/implementation/v3/SingleBucketStoreV3.js.map +0 -1
- package/src/storage/implementation/BucketDefinitionMapping.ts +0 -75
- package/src/storage/implementation/MongoPersistedSyncRules.ts +0 -82
- package/src/storage/implementation/MongoPersistedSyncRulesContent.ts +0 -101
- package/src/storage/implementation/common/MongoSyncBucketStorageContext.ts +0 -15
- package/src/storage/implementation/common/SingleBucketStore.ts +0 -63
- package/src/storage/implementation/v3/MongoParameterLookupV3.ts +0 -11
- package/src/storage/implementation/v3/SingleBucketStoreV3.ts +0 -68
|
@@ -1,41 +1,64 @@
|
|
|
1
|
+
import * as lib_mongo from '@powersync/lib-service-mongodb';
|
|
2
|
+
import { ServiceAssertionError } from '@powersync/lib-services-framework';
|
|
1
3
|
import {
|
|
2
4
|
bson,
|
|
3
5
|
BucketChecksum,
|
|
6
|
+
CheckpointChecksumInvalidatedError,
|
|
4
7
|
FetchPartialBucketChecksum,
|
|
5
8
|
InternalOpId,
|
|
9
|
+
isPartialChecksum,
|
|
6
10
|
PartialChecksumMap,
|
|
7
|
-
PartialOrFullChecksum
|
|
11
|
+
PartialOrFullChecksum,
|
|
12
|
+
SingleSyncConfigBucketDefinitionMapping
|
|
8
13
|
} from '@powersync/service-core';
|
|
9
|
-
import { BucketDefinitionMapping } from '../BucketDefinitionMapping.js';
|
|
10
14
|
import {
|
|
15
|
+
checksumFromAggregate,
|
|
11
16
|
emptyChecksumForRequest,
|
|
12
|
-
|
|
17
|
+
FetchPartialBucketChecksumByBucket,
|
|
18
|
+
FetchPartialBucketChecksumByDefinition,
|
|
13
19
|
MongoChecksumOptions,
|
|
14
|
-
MongoChecksums
|
|
20
|
+
MongoChecksums,
|
|
21
|
+
MongoChecksumSessionContext
|
|
15
22
|
} from '../MongoChecksums.js';
|
|
16
23
|
import { VersionedPowerSyncMongoV3 } from './VersionedPowerSyncMongoV3.js';
|
|
24
|
+
import { BucketDataDocumentV3 } from './models.js';
|
|
17
25
|
|
|
18
|
-
|
|
26
|
+
/**
|
|
27
|
+
* Checksum operations addressed by persisted definition id.
|
|
28
|
+
*
|
|
29
|
+
* Safe on any storage instance, including multi-config replication-side instances - no
|
|
30
|
+
* source resolution is involved. This is the only checksum surface the compactor may use.
|
|
31
|
+
*/
|
|
32
|
+
export interface DefinitionChecksumOperations {
|
|
33
|
+
computePartialChecksumsDirectByDefinition(
|
|
34
|
+
batch: FetchPartialBucketChecksumByDefinition[]
|
|
35
|
+
): Promise<PartialChecksumMap>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface MongoChecksumOptionsV3 extends MongoChecksumOptions {
|
|
39
|
+
/**
|
|
40
|
+
* The persisted mapping of the single sync config that reads are served from.
|
|
41
|
+
*
|
|
42
|
+
* A thunk rather than a plain value: checksums are constructed eagerly with the storage
|
|
43
|
+
* instance, but the single-config requirement may only be asserted when a read happens.
|
|
44
|
+
*/
|
|
45
|
+
syncConfigMapping: () => SingleSyncConfigBucketDefinitionMapping;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export class MongoChecksumsV3 extends MongoChecksums implements DefinitionChecksumOperations {
|
|
19
49
|
declare protected readonly db: VersionedPowerSyncMongoV3;
|
|
20
|
-
private readonly
|
|
50
|
+
private readonly syncConfigMapping: () => SingleSyncConfigBucketDefinitionMapping;
|
|
21
51
|
|
|
22
|
-
constructor(db: VersionedPowerSyncMongoV3, group_id: number, options:
|
|
52
|
+
constructor(db: VersionedPowerSyncMongoV3, group_id: number, options: MongoChecksumOptionsV3) {
|
|
23
53
|
super(db, group_id, options);
|
|
24
|
-
this.
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
private normalizeBatch(batch: FetchPartialBucketChecksum[]): FetchPartialBucketChecksumV3[] {
|
|
28
|
-
return batch.map((request) => ({
|
|
29
|
-
bucket: request.bucket,
|
|
30
|
-
definitionId: this.mapping.bucketSourceId(request.source),
|
|
31
|
-
start: request.start,
|
|
32
|
-
end: request.end
|
|
33
|
-
}));
|
|
54
|
+
this.syncConfigMapping = options.syncConfigMapping;
|
|
34
55
|
}
|
|
35
56
|
|
|
36
|
-
async computePartialChecksumsDirectByDefinition(
|
|
57
|
+
async computePartialChecksumsDirectByDefinition(
|
|
58
|
+
batch: FetchPartialBucketChecksumByDefinition[]
|
|
59
|
+
): Promise<PartialChecksumMap> {
|
|
37
60
|
const results = new Map<string, PartialOrFullChecksum>();
|
|
38
|
-
const requestsByDefinition = new Map<string,
|
|
61
|
+
const requestsByDefinition = new Map<string, FetchPartialBucketChecksumByDefinition[]>();
|
|
39
62
|
|
|
40
63
|
for (const request of batch) {
|
|
41
64
|
const existing = requestsByDefinition.get(request.definitionId) ?? [];
|
|
@@ -44,10 +67,10 @@ export class MongoChecksumsV3 extends MongoChecksums {
|
|
|
44
67
|
}
|
|
45
68
|
|
|
46
69
|
for (const [definitionId, requests] of requestsByDefinition.entries()) {
|
|
47
|
-
const groupResults = await this.
|
|
70
|
+
const groupResults = await this.computeChecksumsByDefinition(
|
|
48
71
|
requests,
|
|
49
|
-
this.db.
|
|
50
|
-
|
|
72
|
+
this.db.bucketData(this.group_id, definitionId),
|
|
73
|
+
{ readOptions: { readConcern: 'snapshot' } }
|
|
51
74
|
);
|
|
52
75
|
for (const checksum of groupResults.values()) {
|
|
53
76
|
results.set(checksum.bucket, checksum);
|
|
@@ -60,9 +83,17 @@ export class MongoChecksumsV3 extends MongoChecksums {
|
|
|
60
83
|
}
|
|
61
84
|
|
|
62
85
|
protected async fetchPreStates(
|
|
63
|
-
batch: FetchPartialBucketChecksum[]
|
|
86
|
+
batch: FetchPartialBucketChecksum[],
|
|
87
|
+
context: MongoChecksumSessionContext
|
|
64
88
|
): Promise<Map<string, { opId: InternalOpId; checksum: BucketChecksum }>> {
|
|
65
|
-
const
|
|
89
|
+
const normalizedBatch = batch.map((request) => ({
|
|
90
|
+
bucket: request.bucket,
|
|
91
|
+
definitionId: this.syncConfigMapping().bucketSourceId(request.source),
|
|
92
|
+
start: request.start,
|
|
93
|
+
end: request.end
|
|
94
|
+
}));
|
|
95
|
+
|
|
96
|
+
const preFilters = normalizedBatch
|
|
66
97
|
.filter((request) => request.start == null)
|
|
67
98
|
.map((request) => ({
|
|
68
99
|
_id: {
|
|
@@ -78,10 +109,15 @@ export class MongoChecksumsV3 extends MongoChecksums {
|
|
|
78
109
|
}
|
|
79
110
|
|
|
80
111
|
const states = await this.db
|
|
81
|
-
.
|
|
82
|
-
.find(
|
|
83
|
-
|
|
84
|
-
|
|
112
|
+
.bucketState(this.group_id)
|
|
113
|
+
.find(
|
|
114
|
+
{
|
|
115
|
+
$or: preFilters
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
...context.readOptions
|
|
119
|
+
}
|
|
120
|
+
)
|
|
85
121
|
.toArray();
|
|
86
122
|
|
|
87
123
|
for (const state of states) {
|
|
@@ -99,12 +135,191 @@ export class MongoChecksumsV3 extends MongoChecksums {
|
|
|
99
135
|
return preStates;
|
|
100
136
|
}
|
|
101
137
|
|
|
102
|
-
protected async computePartialChecksumsInternal(
|
|
103
|
-
|
|
138
|
+
protected async computePartialChecksumsInternal(
|
|
139
|
+
batch: FetchPartialBucketChecksum[],
|
|
140
|
+
context: MongoChecksumSessionContext
|
|
141
|
+
): Promise<PartialChecksumMap> {
|
|
142
|
+
const normalized = batch.map((request) => ({
|
|
143
|
+
bucket: request.bucket,
|
|
144
|
+
definitionId: this.syncConfigMapping().bucketSourceId(request.source),
|
|
145
|
+
start: request.start,
|
|
146
|
+
end: request.end
|
|
147
|
+
}));
|
|
148
|
+
|
|
149
|
+
const results = new Map<string, PartialOrFullChecksum>();
|
|
150
|
+
const requestsByDefinition = new Map<string, FetchPartialBucketChecksumByDefinition[]>();
|
|
151
|
+
|
|
152
|
+
for (const request of normalized) {
|
|
153
|
+
const existing = requestsByDefinition.get(request.definitionId) ?? [];
|
|
154
|
+
existing.push(request);
|
|
155
|
+
requestsByDefinition.set(request.definitionId, existing);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
for (const [definitionId, requests] of requestsByDefinition.entries()) {
|
|
159
|
+
const groupResults = await this.computeChecksumsByDefinition(
|
|
160
|
+
requests,
|
|
161
|
+
this.db.bucketData(this.group_id, definitionId),
|
|
162
|
+
context
|
|
163
|
+
);
|
|
164
|
+
for (const checksum of groupResults.values()) {
|
|
165
|
+
results.set(checksum.bucket, checksum);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return new Map<string, PartialOrFullChecksum>(
|
|
170
|
+
normalized.map((request) => [request.bucket, results.get(request.bucket) ?? emptyChecksumForRequest(request)])
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
private async computeChecksumsByDefinition(
|
|
175
|
+
batch: FetchPartialBucketChecksumByBucket[],
|
|
176
|
+
collection: lib_mongo.mongo.Collection<BucketDataDocumentV3>,
|
|
177
|
+
context: MongoChecksumSessionContext
|
|
178
|
+
): Promise<PartialChecksumMap> {
|
|
179
|
+
const requests = new Map<string, FetchPartialBucketChecksumByBucket>();
|
|
180
|
+
for (let request of batch) {
|
|
181
|
+
requests.set(request.bucket, request);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const aggregate = await this.aggregatePartialChecksums(collection, requests, context);
|
|
185
|
+
const { checksums, startStraddledBuckets } = this.normalizePartialChecksumResults(batch, aggregate);
|
|
186
|
+
|
|
187
|
+
if (startStraddledBuckets.size == 0) {
|
|
188
|
+
return checksums;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// A cached base ends inside a compaction-produced document. Its checksum cannot
|
|
192
|
+
// be safely combined with the document-level checksum, so recalculate this
|
|
193
|
+
// bucket from the beginning. A full result replaces the cached base upstream.
|
|
194
|
+
const fullRequests = batch
|
|
195
|
+
.filter((request) => startStraddledBuckets.has(request.bucket))
|
|
196
|
+
.map((request) => ({ ...request, start: undefined }));
|
|
197
|
+
const fullRequestMap = new Map(fullRequests.map((request) => [request.bucket, request]));
|
|
198
|
+
const fullAggregate = await this.aggregatePartialChecksums(collection, fullRequestMap, context);
|
|
199
|
+
const fullResults = this.normalizePartialChecksumResults(fullRequests, fullAggregate);
|
|
200
|
+
|
|
201
|
+
if (fullResults.startStraddledBuckets.size > 0) {
|
|
202
|
+
throw new ServiceAssertionError(
|
|
203
|
+
`Unexpected start-boundary straddle while recalculating bucket(s) ${[...fullResults.startStraddledBuckets].join(', ')}`
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
for (const [bucket, checksum] of fullResults.checksums) {
|
|
207
|
+
checksums.set(bucket, checksum);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return checksums;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
private async aggregatePartialChecksums(
|
|
214
|
+
collection: lib_mongo.mongo.Collection<BucketDataDocumentV3>,
|
|
215
|
+
requests: Map<string, FetchPartialBucketChecksumByBucket>,
|
|
216
|
+
context: MongoChecksumSessionContext
|
|
217
|
+
): Promise<bson.Document[]> {
|
|
218
|
+
return collection
|
|
219
|
+
.aggregate(this.buildPartialChecksumPipeline(requests), {
|
|
220
|
+
...context.readOptions,
|
|
221
|
+
maxTimeMS: lib_mongo.MONGO_CHECKSUM_TIMEOUT_MS
|
|
222
|
+
})
|
|
223
|
+
.toArray()
|
|
224
|
+
.catch((e) => {
|
|
225
|
+
throw lib_mongo.mapQueryError(e, 'while reading checksums');
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
private buildPartialChecksumPipeline(requests: Map<string, FetchPartialBucketChecksumByBucket>): bson.Document[] {
|
|
230
|
+
const filters = Array.from(requests.values(), createBucketFilter);
|
|
231
|
+
|
|
232
|
+
return [
|
|
233
|
+
// $match with $or filters
|
|
234
|
+
{
|
|
235
|
+
$match: {
|
|
236
|
+
$or: filters
|
|
237
|
+
}
|
|
238
|
+
},
|
|
239
|
+
// Only document-level metadata is used below. Bucket-data operations may
|
|
240
|
+
// be offloaded, and checksum queries must never inspect the ops array.
|
|
241
|
+
{
|
|
242
|
+
$project: {
|
|
243
|
+
_id: 1,
|
|
244
|
+
min_op: 1,
|
|
245
|
+
checksum: 1,
|
|
246
|
+
count: 1,
|
|
247
|
+
has_clear_op: 1
|
|
248
|
+
}
|
|
249
|
+
},
|
|
250
|
+
// Aggregate document metadata per bucket. A CLEAR makes the result a full
|
|
251
|
+
// checksum that replaces any cached prefix; storage guarantees that
|
|
252
|
+
// documents preceding the CLEAR have already been removed.
|
|
253
|
+
{
|
|
254
|
+
$group: {
|
|
255
|
+
_id: '$_id.b',
|
|
256
|
+
checksum_total: { $sum: '$checksum' },
|
|
257
|
+
count: { $sum: '$count' },
|
|
258
|
+
has_clear_op: {
|
|
259
|
+
$max: { $cond: ['$has_clear_op', 1, 0] }
|
|
260
|
+
},
|
|
261
|
+
min_op: { $min: '$min_op' },
|
|
262
|
+
max_op: { $max: '$_id.o' }
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
];
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
private normalizePartialChecksumResults(
|
|
269
|
+
batch: FetchPartialBucketChecksumByBucket[],
|
|
270
|
+
aggregate: bson.Document[]
|
|
271
|
+
): { checksums: PartialChecksumMap; startStraddledBuckets: Set<string> } {
|
|
272
|
+
const requests = new Map(batch.map((request) => [request.bucket, request]));
|
|
273
|
+
const startStraddledBuckets = new Set<string>();
|
|
274
|
+
const partialChecksums = new Map<string, PartialOrFullChecksum>();
|
|
275
|
+
|
|
276
|
+
for (const doc of aggregate) {
|
|
277
|
+
const bucket = doc._id as string;
|
|
278
|
+
const request = requests.get(bucket)!;
|
|
279
|
+
|
|
280
|
+
// A maximum beyond end means a matched document straddles the checkpoint.
|
|
281
|
+
// Its document-level checksum includes later operations, so this checkpoint
|
|
282
|
+
// cannot be calculated from document metadata.
|
|
283
|
+
if (doc.max_op > request.end) {
|
|
284
|
+
throw new CheckpointChecksumInvalidatedError(request.end, bucket);
|
|
285
|
+
}
|
|
286
|
+
// The _id filter excludes documents ending at or before start. A minimum
|
|
287
|
+
// at or below start therefore means a matched document straddles it.
|
|
288
|
+
if (request.start != null && doc.min_op <= request.start) {
|
|
289
|
+
startStraddledBuckets.add(bucket);
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
partialChecksums.set(bucket, checksumFromAggregate(doc));
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const checksums = new Map<string, PartialOrFullChecksum>(
|
|
297
|
+
batch.map((request) => {
|
|
298
|
+
const bucket = request.bucket;
|
|
299
|
+
let partialChecksum = partialChecksums.get(bucket);
|
|
300
|
+
if (partialChecksum == null) {
|
|
301
|
+
partialChecksum = {
|
|
302
|
+
bucket,
|
|
303
|
+
partialCount: 0,
|
|
304
|
+
partialChecksum: 0
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
if (request.start == null && isPartialChecksum(partialChecksum)) {
|
|
308
|
+
partialChecksum = {
|
|
309
|
+
bucket,
|
|
310
|
+
count: partialChecksum.partialCount,
|
|
311
|
+
checksum: partialChecksum.partialChecksum
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
return [bucket, partialChecksum];
|
|
316
|
+
})
|
|
317
|
+
);
|
|
318
|
+
return { checksums, startStraddledBuckets };
|
|
104
319
|
}
|
|
105
320
|
}
|
|
106
321
|
|
|
107
|
-
function
|
|
322
|
+
function createBucketFilter(request: Pick<FetchPartialBucketChecksumByBucket, 'bucket' | 'start' | 'end'>) {
|
|
108
323
|
return {
|
|
109
324
|
_id: {
|
|
110
325
|
$gt: {
|
|
@@ -113,8 +328,11 @@ function createV3BucketFilter(request: Pick<FetchPartialBucketChecksumV3, 'bucke
|
|
|
113
328
|
},
|
|
114
329
|
$lte: {
|
|
115
330
|
b: request.bucket,
|
|
116
|
-
o:
|
|
331
|
+
o: new bson.MaxKey()
|
|
117
332
|
}
|
|
333
|
+
},
|
|
334
|
+
min_op: {
|
|
335
|
+
$lte: request.end
|
|
118
336
|
}
|
|
119
337
|
};
|
|
120
338
|
}
|