@powersync/service-module-mongodb-storage 0.15.3 → 0.16.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/CHANGELOG.md +54 -0
- package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js +1 -1
- package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js.map +1 -1
- package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js +3 -3
- package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js.map +1 -1
- package/dist/migrations/db/migrations/1770213298299-storage-version.js.map +1 -1
- package/dist/storage/MongoBucketStorage.d.ts +5 -3
- package/dist/storage/MongoBucketStorage.js +50 -36
- package/dist/storage/MongoBucketStorage.js.map +1 -1
- package/dist/storage/MongoReportStorage.js.map +1 -1
- package/dist/storage/implementation/BucketDefinitionMapping.d.ts +17 -0
- package/dist/storage/implementation/BucketDefinitionMapping.js +58 -0
- package/dist/storage/implementation/BucketDefinitionMapping.js.map +1 -0
- package/dist/storage/implementation/MongoBucketBatch.d.ts +16 -14
- package/dist/storage/implementation/MongoBucketBatch.js +80 -115
- package/dist/storage/implementation/MongoBucketBatch.js.map +1 -1
- package/dist/storage/implementation/MongoBucketBatchShared.d.ts +5 -0
- package/dist/storage/implementation/MongoBucketBatchShared.js +8 -0
- package/dist/storage/implementation/MongoBucketBatchShared.js.map +1 -0
- package/dist/storage/implementation/MongoChecksums.d.ts +28 -17
- package/dist/storage/implementation/MongoChecksums.js +13 -72
- package/dist/storage/implementation/MongoChecksums.js.map +1 -1
- package/dist/storage/implementation/MongoCompactor.d.ts +98 -58
- package/dist/storage/implementation/MongoCompactor.js +229 -296
- package/dist/storage/implementation/MongoCompactor.js.map +1 -1
- package/dist/storage/implementation/MongoParameterCompactor.d.ts +11 -6
- package/dist/storage/implementation/MongoParameterCompactor.js +11 -8
- package/dist/storage/implementation/MongoParameterCompactor.js.map +1 -1
- package/dist/storage/implementation/MongoPersistedSyncRules.d.ts +14 -0
- package/dist/storage/implementation/MongoPersistedSyncRules.js +64 -0
- package/dist/storage/implementation/MongoPersistedSyncRules.js.map +1 -0
- package/dist/storage/implementation/MongoPersistedSyncRulesContent.d.ts +3 -0
- package/dist/storage/implementation/MongoPersistedSyncRulesContent.js +9 -0
- package/dist/storage/implementation/MongoPersistedSyncRulesContent.js.map +1 -1
- package/dist/storage/implementation/MongoStorageProvider.js +1 -1
- package/dist/storage/implementation/MongoStorageProvider.js.map +1 -1
- package/dist/storage/implementation/MongoSyncBucketStorage.d.ts +49 -30
- package/dist/storage/implementation/MongoSyncBucketStorage.js +96 -388
- package/dist/storage/implementation/MongoSyncBucketStorage.js.map +1 -1
- package/dist/storage/implementation/MongoSyncRulesLock.d.ts +5 -3
- package/dist/storage/implementation/MongoSyncRulesLock.js +12 -10
- package/dist/storage/implementation/MongoSyncRulesLock.js.map +1 -1
- package/dist/storage/implementation/MongoWriteCheckpointAPI.js +1 -1
- package/dist/storage/implementation/MongoWriteCheckpointAPI.js.map +1 -1
- package/dist/storage/implementation/OperationBatch.js +1 -1
- package/dist/storage/implementation/common/BucketDataDoc.d.ts +35 -0
- package/dist/storage/implementation/common/BucketDataDoc.js +2 -0
- package/dist/storage/implementation/common/BucketDataDoc.js.map +1 -0
- package/dist/storage/implementation/common/MongoSyncBucketStorageContext.d.ts +13 -0
- package/dist/storage/implementation/common/MongoSyncBucketStorageContext.js +2 -0
- package/dist/storage/implementation/common/MongoSyncBucketStorageContext.js.map +1 -0
- package/dist/storage/implementation/common/PersistedBatch.d.ts +108 -0
- package/dist/storage/implementation/common/PersistedBatch.js +237 -0
- package/dist/storage/implementation/common/PersistedBatch.js.map +1 -0
- package/dist/storage/implementation/common/SingleBucketStore.d.ts +54 -0
- package/dist/storage/implementation/common/SingleBucketStore.js +3 -0
- package/dist/storage/implementation/common/SingleBucketStore.js.map +1 -0
- package/dist/storage/implementation/common/SourceRecordStore.d.ts +36 -0
- package/dist/storage/implementation/common/SourceRecordStore.js +2 -0
- package/dist/storage/implementation/common/SourceRecordStore.js.map +1 -0
- package/dist/storage/implementation/common/VersionedPowerSyncMongoBase.d.ts +27 -0
- package/dist/storage/implementation/common/VersionedPowerSyncMongoBase.js +57 -0
- package/dist/storage/implementation/common/VersionedPowerSyncMongoBase.js.map +1 -0
- package/dist/storage/implementation/createMongoSyncBucketStorage.d.ts +7 -0
- package/dist/storage/implementation/createMongoSyncBucketStorage.js +9 -0
- package/dist/storage/implementation/createMongoSyncBucketStorage.js.map +1 -0
- package/dist/storage/implementation/db.d.ts +34 -34
- package/dist/storage/implementation/db.js +78 -98
- package/dist/storage/implementation/db.js.map +1 -1
- package/dist/storage/implementation/models.d.ts +63 -34
- package/dist/storage/implementation/models.js +21 -2
- package/dist/storage/implementation/models.js.map +1 -1
- package/dist/storage/implementation/v1/MongoBucketBatchV1.d.ts +13 -0
- package/dist/storage/implementation/v1/MongoBucketBatchV1.js +22 -0
- package/dist/storage/implementation/v1/MongoBucketBatchV1.js.map +1 -0
- package/dist/storage/implementation/v1/MongoChecksumsV1.d.ts +12 -0
- package/dist/storage/implementation/v1/MongoChecksumsV1.js +56 -0
- package/dist/storage/implementation/v1/MongoChecksumsV1.js.map +1 -0
- package/dist/storage/implementation/v1/MongoCompactorV1.d.ts +23 -0
- package/dist/storage/implementation/v1/MongoCompactorV1.js +52 -0
- package/dist/storage/implementation/v1/MongoCompactorV1.js.map +1 -0
- package/dist/storage/implementation/v1/MongoParameterCompactorV1.d.ts +9 -0
- package/dist/storage/implementation/v1/MongoParameterCompactorV1.js +20 -0
- package/dist/storage/implementation/v1/MongoParameterCompactorV1.js.map +1 -0
- package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.d.ts +41 -0
- package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.js +283 -0
- package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.js.map +1 -0
- package/dist/storage/implementation/v1/PersistedBatchV1.d.ts +26 -0
- package/dist/storage/implementation/v1/PersistedBatchV1.js +183 -0
- package/dist/storage/implementation/v1/PersistedBatchV1.js.map +1 -0
- package/dist/storage/implementation/v1/SingleBucketStoreV1.d.ts +18 -0
- package/dist/storage/implementation/v1/SingleBucketStoreV1.js +57 -0
- package/dist/storage/implementation/v1/SingleBucketStoreV1.js.map +1 -0
- package/dist/storage/implementation/v1/SourceRecordStoreV1.d.ts +19 -0
- package/dist/storage/implementation/v1/SourceRecordStoreV1.js +105 -0
- package/dist/storage/implementation/v1/SourceRecordStoreV1.js.map +1 -0
- package/dist/storage/implementation/v1/VersionedPowerSyncMongoV1.d.ts +12 -0
- package/dist/storage/implementation/v1/VersionedPowerSyncMongoV1.js +20 -0
- package/dist/storage/implementation/v1/VersionedPowerSyncMongoV1.js.map +1 -0
- package/dist/storage/implementation/v1/models.d.ts +34 -0
- package/dist/storage/implementation/v1/models.js +37 -0
- package/dist/storage/implementation/v1/models.js.map +1 -0
- package/dist/storage/implementation/v3/MongoBucketBatchV3.d.ts +13 -0
- package/dist/storage/implementation/v3/MongoBucketBatchV3.js +34 -0
- package/dist/storage/implementation/v3/MongoBucketBatchV3.js.map +1 -0
- package/dist/storage/implementation/v3/MongoChecksumsV3.d.ts +15 -0
- package/dist/storage/implementation/v3/MongoChecksumsV3.js +84 -0
- package/dist/storage/implementation/v3/MongoChecksumsV3.js.map +1 -0
- package/dist/storage/implementation/v3/MongoCompactorV3.d.ts +23 -0
- package/dist/storage/implementation/v3/MongoCompactorV3.js +68 -0
- package/dist/storage/implementation/v3/MongoCompactorV3.js.map +1 -0
- package/dist/storage/implementation/v3/MongoParameterCompactorV3.d.ts +9 -0
- package/dist/storage/implementation/v3/MongoParameterCompactorV3.js +18 -0
- package/dist/storage/implementation/v3/MongoParameterCompactorV3.js.map +1 -0
- package/dist/storage/implementation/v3/MongoParameterLookupV3.d.ts +5 -0
- package/dist/storage/implementation/v3/MongoParameterLookupV3.js +9 -0
- package/dist/storage/implementation/v3/MongoParameterLookupV3.js.map +1 -0
- package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.d.ts +41 -0
- package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.js +407 -0
- package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.js.map +1 -0
- package/dist/storage/implementation/v3/PersistedBatchV3.d.ts +29 -0
- package/dist/storage/implementation/v3/PersistedBatchV3.js +259 -0
- package/dist/storage/implementation/v3/PersistedBatchV3.js.map +1 -0
- package/dist/storage/implementation/v3/SingleBucketStoreV3.d.ts +18 -0
- package/dist/storage/implementation/v3/SingleBucketStoreV3.js +48 -0
- package/dist/storage/implementation/v3/SingleBucketStoreV3.js.map +1 -0
- package/dist/storage/implementation/v3/SourceRecordStoreV3.d.ts +22 -0
- package/dist/storage/implementation/v3/SourceRecordStoreV3.js +164 -0
- package/dist/storage/implementation/v3/SourceRecordStoreV3.js.map +1 -0
- package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.d.ts +21 -0
- package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.js +71 -0
- package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.js.map +1 -0
- package/dist/storage/implementation/v3/models.d.ts +43 -0
- package/dist/storage/implementation/v3/models.js +34 -0
- package/dist/storage/implementation/v3/models.js.map +1 -0
- package/dist/storage/storage-index.d.ts +8 -5
- package/dist/storage/storage-index.js +8 -5
- package/dist/storage/storage-index.js.map +1 -1
- package/dist/utils/util.d.ts +11 -4
- package/dist/utils/util.js +25 -4
- package/dist/utils/util.js.map +1 -1
- package/package.json +9 -9
- package/src/migrations/db/migrations/1688556755264-initial-sync-rules.ts +1 -1
- package/src/migrations/db/migrations/1702295701188-sync-rule-state.ts +7 -7
- package/src/migrations/db/migrations/1770213298299-storage-version.ts +1 -1
- package/src/storage/MongoBucketStorage.ts +97 -62
- package/src/storage/MongoReportStorage.ts +2 -2
- package/src/storage/implementation/BucketDefinitionMapping.ts +72 -0
- package/src/storage/implementation/MongoBucketBatch.ts +110 -144
- package/src/storage/implementation/MongoBucketBatchShared.ts +11 -0
- package/src/storage/implementation/MongoChecksums.ts +53 -76
- package/src/storage/implementation/MongoCompactor.ts +374 -404
- package/src/storage/implementation/MongoParameterCompactor.ts +37 -24
- package/src/storage/implementation/MongoPersistedSyncRules.ts +76 -0
- package/src/storage/implementation/MongoPersistedSyncRulesContent.ts +18 -1
- package/src/storage/implementation/MongoStorageProvider.ts +1 -1
- package/src/storage/implementation/MongoSyncBucketStorage.ts +190 -457
- package/src/storage/implementation/MongoSyncRulesLock.ts +12 -14
- package/src/storage/implementation/MongoWriteCheckpointAPI.ts +4 -2
- package/src/storage/implementation/OperationBatch.ts +1 -1
- package/src/storage/implementation/common/BucketDataDoc.ts +37 -0
- package/src/storage/implementation/common/MongoSyncBucketStorageContext.ts +15 -0
- package/src/storage/implementation/common/PersistedBatch.ts +364 -0
- package/src/storage/implementation/common/SingleBucketStore.ts +63 -0
- package/src/storage/implementation/common/SourceRecordStore.ts +49 -0
- package/src/storage/implementation/common/VersionedPowerSyncMongoBase.ts +80 -0
- package/src/storage/implementation/createMongoSyncBucketStorage.ts +25 -0
- package/src/storage/implementation/db.ts +107 -128
- package/src/storage/implementation/models.ts +84 -38
- package/src/storage/implementation/v1/MongoBucketBatchV1.ts +32 -0
- package/src/storage/implementation/v1/MongoChecksumsV1.ts +75 -0
- package/src/storage/implementation/v1/MongoCompactorV1.ts +93 -0
- package/src/storage/implementation/v1/MongoParameterCompactorV1.ts +26 -0
- package/src/storage/implementation/v1/MongoSyncBucketStorageV1.ts +448 -0
- package/src/storage/implementation/v1/PersistedBatchV1.ts +230 -0
- package/src/storage/implementation/v1/SingleBucketStoreV1.ts +74 -0
- package/src/storage/implementation/v1/SourceRecordStoreV1.ts +156 -0
- package/src/storage/implementation/v1/VersionedPowerSyncMongoV1.ts +28 -0
- package/src/storage/implementation/v1/models.ts +84 -0
- package/src/storage/implementation/v3/MongoBucketBatchV3.ts +44 -0
- package/src/storage/implementation/v3/MongoChecksumsV3.ts +120 -0
- package/src/storage/implementation/v3/MongoCompactorV3.ts +107 -0
- package/src/storage/implementation/v3/MongoParameterCompactorV3.ts +24 -0
- package/src/storage/implementation/v3/MongoParameterLookupV3.ts +12 -0
- package/src/storage/implementation/v3/MongoSyncBucketStorageV3.ts +550 -0
- package/src/storage/implementation/v3/PersistedBatchV3.ts +318 -0
- package/src/storage/implementation/v3/SingleBucketStoreV3.ts +68 -0
- package/src/storage/implementation/v3/SourceRecordStoreV3.ts +226 -0
- package/src/storage/implementation/v3/VersionedPowerSyncMongoV3.ts +112 -0
- package/src/storage/implementation/v3/models.ts +96 -0
- package/src/storage/storage-index.ts +8 -5
- package/src/utils/util.ts +36 -7
- package/test/src/__snapshots__/storage_sync.test.ts.snap +282 -0
- package/test/src/connection-report-storage.test.ts +3 -3
- package/test/src/setup.ts +1 -1
- package/test/src/storage.test.ts +2 -2
- package/test/src/storage_compacting.test.ts +57 -29
- package/test/src/storage_sync.test.ts +351 -5
- package/test/tsconfig.json +0 -1
- package/tsconfig.tsbuildinfo +1 -1
- package/dist/storage/implementation/PersistedBatch.d.ts +0 -71
- package/dist/storage/implementation/PersistedBatch.js +0 -354
- package/dist/storage/implementation/PersistedBatch.js.map +0 -1
- package/src/storage/implementation/PersistedBatch.ts +0 -432
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import {
|
|
2
|
+
bson,
|
|
3
|
+
BucketChecksum,
|
|
4
|
+
FetchPartialBucketChecksum,
|
|
5
|
+
InternalOpId,
|
|
6
|
+
PartialChecksumMap,
|
|
7
|
+
PartialOrFullChecksum
|
|
8
|
+
} from '@powersync/service-core';
|
|
9
|
+
import { BucketDefinitionMapping } from '../BucketDefinitionMapping.js';
|
|
10
|
+
import {
|
|
11
|
+
emptyChecksumForRequest,
|
|
12
|
+
FetchPartialBucketChecksumV3,
|
|
13
|
+
MongoChecksumOptions,
|
|
14
|
+
MongoChecksums
|
|
15
|
+
} from '../MongoChecksums.js';
|
|
16
|
+
import { VersionedPowerSyncMongoV3 } from './VersionedPowerSyncMongoV3.js';
|
|
17
|
+
|
|
18
|
+
export class MongoChecksumsV3 extends MongoChecksums {
|
|
19
|
+
declare protected readonly db: VersionedPowerSyncMongoV3;
|
|
20
|
+
private readonly mapping: BucketDefinitionMapping;
|
|
21
|
+
|
|
22
|
+
constructor(db: VersionedPowerSyncMongoV3, group_id: number, options: MongoChecksumOptions) {
|
|
23
|
+
super(db, group_id, options);
|
|
24
|
+
this.mapping = options.mapping!;
|
|
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
|
+
}));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async computePartialChecksumsDirectByDefinition(batch: FetchPartialBucketChecksumV3[]): Promise<PartialChecksumMap> {
|
|
37
|
+
const results = new Map<string, PartialOrFullChecksum>();
|
|
38
|
+
const requestsByDefinition = new Map<string, FetchPartialBucketChecksumV3[]>();
|
|
39
|
+
|
|
40
|
+
for (const request of batch) {
|
|
41
|
+
const existing = requestsByDefinition.get(request.definitionId) ?? [];
|
|
42
|
+
existing.push(request);
|
|
43
|
+
requestsByDefinition.set(request.definitionId, existing);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
for (const [definitionId, requests] of requestsByDefinition.entries()) {
|
|
47
|
+
const groupResults = await this.computePartialChecksumsForCollection(
|
|
48
|
+
requests,
|
|
49
|
+
this.db.bucketDataV3(this.group_id, definitionId),
|
|
50
|
+
createV3BucketFilter
|
|
51
|
+
);
|
|
52
|
+
for (const checksum of groupResults.values()) {
|
|
53
|
+
results.set(checksum.bucket, checksum);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return new Map<string, PartialOrFullChecksum>(
|
|
58
|
+
batch.map((request) => [request.bucket, results.get(request.bucket) ?? emptyChecksumForRequest(request)])
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
protected async fetchPreStates(
|
|
63
|
+
batch: FetchPartialBucketChecksum[]
|
|
64
|
+
): Promise<Map<string, { opId: InternalOpId; checksum: BucketChecksum }>> {
|
|
65
|
+
const preFilters = this.normalizeBatch(batch)
|
|
66
|
+
.filter((request) => request.start == null)
|
|
67
|
+
.map((request) => ({
|
|
68
|
+
_id: {
|
|
69
|
+
d: request.definitionId,
|
|
70
|
+
b: request.bucket
|
|
71
|
+
},
|
|
72
|
+
'compacted_state.op_id': { $exists: true, $lte: request.end }
|
|
73
|
+
}));
|
|
74
|
+
|
|
75
|
+
const preStates = new Map<string, { opId: InternalOpId; checksum: BucketChecksum }>();
|
|
76
|
+
if (preFilters.length == 0) {
|
|
77
|
+
return preStates;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const states = await this.db
|
|
81
|
+
.bucketStateV3(this.group_id)
|
|
82
|
+
.find({
|
|
83
|
+
$or: preFilters
|
|
84
|
+
})
|
|
85
|
+
.toArray();
|
|
86
|
+
|
|
87
|
+
for (const state of states) {
|
|
88
|
+
const compactedState = state.compacted_state!;
|
|
89
|
+
preStates.set(state._id.b, {
|
|
90
|
+
opId: compactedState.op_id,
|
|
91
|
+
checksum: {
|
|
92
|
+
bucket: state._id.b,
|
|
93
|
+
checksum: Number(compactedState.checksum),
|
|
94
|
+
count: compactedState.count
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return preStates;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
protected async computePartialChecksumsInternal(batch: FetchPartialBucketChecksum[]): Promise<PartialChecksumMap> {
|
|
103
|
+
return this.computePartialChecksumsDirectByDefinition(this.normalizeBatch(batch));
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function createV3BucketFilter(request: Pick<FetchPartialBucketChecksumV3, 'bucket' | 'start' | 'end'>) {
|
|
108
|
+
return {
|
|
109
|
+
_id: {
|
|
110
|
+
$gt: {
|
|
111
|
+
b: request.bucket,
|
|
112
|
+
o: request.start ?? new bson.MinKey()
|
|
113
|
+
},
|
|
114
|
+
$lte: {
|
|
115
|
+
b: request.bucket,
|
|
116
|
+
o: request.end
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { mongo } from '@powersync/lib-service-mongodb';
|
|
2
|
+
import { ReplicationAssertionError, ServiceAssertionError } from '@powersync/lib-services-framework';
|
|
3
|
+
import { storage } from '@powersync/service-core';
|
|
4
|
+
import { BucketDefinitionId } from '../BucketDefinitionMapping.js';
|
|
5
|
+
import { SingleBucketStore } from '../common/SingleBucketStore.js';
|
|
6
|
+
import { BucketStateDocumentBase } from '../models.js';
|
|
7
|
+
import { DirtyBucket, MongoCompactor } from '../MongoCompactor.js';
|
|
8
|
+
import { BucketStateDocumentV3 } from './models.js';
|
|
9
|
+
import type { MongoSyncBucketStorageV3 } from './MongoSyncBucketStorageV3.js';
|
|
10
|
+
import { SingleBucketStoreV3 } from './SingleBucketStoreV3.js';
|
|
11
|
+
import { VersionedPowerSyncMongoV3 } from './VersionedPowerSyncMongoV3.js';
|
|
12
|
+
|
|
13
|
+
export class MongoCompactorV3 extends MongoCompactor {
|
|
14
|
+
declare protected readonly db: VersionedPowerSyncMongoV3;
|
|
15
|
+
declare protected readonly storage: MongoSyncBucketStorageV3;
|
|
16
|
+
|
|
17
|
+
public async *dirtyBucketBatches(options: {
|
|
18
|
+
minBucketChanges: number;
|
|
19
|
+
minChangeRatio: number;
|
|
20
|
+
}): AsyncGenerator<DirtyBucket[]> {
|
|
21
|
+
if (options.minBucketChanges <= 0) {
|
|
22
|
+
throw new ReplicationAssertionError('minBucketChanges must be >= 1');
|
|
23
|
+
}
|
|
24
|
+
// Same scan strategy as V1, but with the V3 bucket_state key shape.
|
|
25
|
+
yield* this.dirtyBucketBatchesForCollection(
|
|
26
|
+
this.db.bucketStateV3(this.group_id),
|
|
27
|
+
{ d: new mongo.MinKey() as any, b: new mongo.MinKey() as any },
|
|
28
|
+
{ d: new mongo.MaxKey() as any, b: new mongo.MaxKey() as any },
|
|
29
|
+
options,
|
|
30
|
+
(bucketState) => (bucketState as BucketStateDocumentV3)._id.d
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public async dirtyBucketBatchForChecksums(options: { minBucketChanges: number }): Promise<DirtyBucket[]> {
|
|
35
|
+
if (options.minBucketChanges <= 0) {
|
|
36
|
+
throw new ReplicationAssertionError('minBucketChanges must be >= 1');
|
|
37
|
+
}
|
|
38
|
+
return this.dirtyBucketBatchForChecksumsForCollection(
|
|
39
|
+
this.db.bucketStateV3(this.group_id),
|
|
40
|
+
{
|
|
41
|
+
'estimate_since_compact.count': { $gte: options.minBucketChanges }
|
|
42
|
+
},
|
|
43
|
+
(bucketState) => (bucketState as BucketStateDocumentV3)._id.d
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
protected async writeBucketStateUpdates(): Promise<void> {
|
|
48
|
+
await this.db
|
|
49
|
+
.bucketStateV3(this.group_id)
|
|
50
|
+
.bulkWrite(this.bucketStateUpdates as mongo.AnyBulkWriteOperation<BucketStateDocumentV3>[], { ordered: false });
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
protected async computeChecksumsForBuckets(
|
|
54
|
+
buckets: Pick<DirtyBucket, 'bucket' | 'definitionId'>[]
|
|
55
|
+
): Promise<storage.PartialChecksumMap> {
|
|
56
|
+
return this.storage.checksums.computePartialChecksumsDirectByDefinition(
|
|
57
|
+
buckets.map(({ bucket, definitionId }) => {
|
|
58
|
+
if (definitionId == null) {
|
|
59
|
+
throw new ServiceAssertionError(`Missing definitionId for V3 bucket checksum update on bucket ${bucket}`);
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
bucket,
|
|
63
|
+
definitionId,
|
|
64
|
+
end: this.maxOpId
|
|
65
|
+
};
|
|
66
|
+
})
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
protected bucketStateFilter(
|
|
71
|
+
bucket: string,
|
|
72
|
+
definitionId: BucketDefinitionId | null
|
|
73
|
+
): mongo.Filter<BucketStateDocumentBase> {
|
|
74
|
+
if (definitionId == null) {
|
|
75
|
+
throw new ServiceAssertionError(`Missing definitionId for V3 bucket state filter on bucket ${bucket}`);
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
_id: {
|
|
79
|
+
d: definitionId,
|
|
80
|
+
b: bucket
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
protected async getBucketDataContext(
|
|
86
|
+
bucket: string,
|
|
87
|
+
definitionId: BucketDefinitionId | null
|
|
88
|
+
): Promise<SingleBucketStore | null> {
|
|
89
|
+
if (definitionId == null) {
|
|
90
|
+
// Not the _most_ efficient approach, but this is not used often
|
|
91
|
+
const allDefinitionIds = this.storage.mapping.allBucketDefinitionIds();
|
|
92
|
+
if (allDefinitionIds.length == 0) {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
const potentialIds = allDefinitionIds.map((definitionId) => ({ d: definitionId, b: bucket }));
|
|
96
|
+
const bucketState = await this.db.bucketStateV3(this.group_id).findOne({
|
|
97
|
+
_id: { $in: potentialIds }
|
|
98
|
+
});
|
|
99
|
+
if (bucketState == null) {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
definitionId = bucketState._id.d;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return new SingleBucketStoreV3(this.db, { bucket, definitionId, replicationStreamId: this.group_id });
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { mongo } from '@powersync/lib-service-mongodb';
|
|
2
|
+
import { MongoParameterCompactor } from '../MongoParameterCompactor.js';
|
|
3
|
+
import { VersionedPowerSyncMongoV3 } from './VersionedPowerSyncMongoV3.js';
|
|
4
|
+
|
|
5
|
+
export class MongoParameterCompactorV3 extends MongoParameterCompactor {
|
|
6
|
+
declare protected readonly db: VersionedPowerSyncMongoV3;
|
|
7
|
+
|
|
8
|
+
protected async getCollections(): Promise<mongo.Collection<mongo.Document>[]> {
|
|
9
|
+
const collections = await this.db.listParameterIndexCollectionsV3(this.group_id);
|
|
10
|
+
return collections.map((collection) => collection.collection as unknown as mongo.Collection<mongo.Document>);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
protected collectionFilter(): mongo.Document {
|
|
14
|
+
return {};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
protected deleteFilter(doc: mongo.Document): mongo.Document {
|
|
18
|
+
return {
|
|
19
|
+
lookup: doc.lookup,
|
|
20
|
+
_id: { $lte: doc._id },
|
|
21
|
+
key: doc.key
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { deserializeParameterLookup } from '@powersync/service-core';
|
|
2
|
+
import { ScopedParameterLookup, SqliteJsonValue } from '@powersync/service-sync-rules';
|
|
3
|
+
import * as bson from 'bson';
|
|
4
|
+
import { ParameterIndexId } from '../BucketDefinitionMapping.js';
|
|
5
|
+
|
|
6
|
+
export function serializeParameterLookupV3(lookup: ScopedParameterLookup): bson.Binary {
|
|
7
|
+
return new bson.Binary(bson.serialize({ l: lookup.values.slice(2) }));
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function deserializeParameterLookupV3(lookup: bson.Binary, indexId: ParameterIndexId): SqliteJsonValue[] {
|
|
11
|
+
return [indexId, '', ...deserializeParameterLookup(lookup)];
|
|
12
|
+
}
|