@powersync/service-module-mongodb-storage 0.0.0-dev-20251015143910 → 0.0.0-dev-20251110113516
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 +37 -12
- package/dist/migrations/db/migrations/1760433882550-bucket-state-index2.d.ts +3 -0
- package/dist/migrations/db/migrations/1760433882550-bucket-state-index2.js +25 -0
- package/dist/migrations/db/migrations/1760433882550-bucket-state-index2.js.map +1 -0
- package/dist/storage/MongoReportStorage.d.ts +1 -0
- package/dist/storage/MongoReportStorage.js +13 -0
- package/dist/storage/MongoReportStorage.js.map +1 -1
- package/dist/storage/implementation/MongoCompactor.d.ts +13 -3
- package/dist/storage/implementation/MongoCompactor.js +86 -90
- package/dist/storage/implementation/MongoCompactor.js.map +1 -1
- package/dist/storage/implementation/MongoSyncBucketStorage.d.ts +2 -2
- package/dist/storage/implementation/MongoSyncBucketStorage.js +15 -4
- package/dist/storage/implementation/MongoSyncBucketStorage.js.map +1 -1
- package/dist/storage/implementation/MongoWriteCheckpointAPI.js +6 -2
- package/dist/storage/implementation/MongoWriteCheckpointAPI.js.map +1 -1
- package/dist/storage/implementation/db.d.ts +4 -0
- package/dist/storage/implementation/db.js +10 -0
- package/dist/storage/implementation/db.js.map +1 -1
- package/dist/utils/util.d.ts +9 -1
- package/dist/utils/util.js +39 -1
- package/dist/utils/util.js.map +1 -1
- package/package.json +9 -9
- package/src/migrations/db/migrations/1760433882550-bucket-state-index2.ts +34 -0
- package/src/storage/MongoReportStorage.ts +22 -0
- package/src/storage/implementation/MongoCompactor.ts +100 -96
- package/src/storage/implementation/MongoSyncBucketStorage.ts +18 -5
- package/src/storage/implementation/MongoWriteCheckpointAPI.ts +6 -2
- package/src/storage/implementation/db.ts +13 -0
- package/src/utils/util.ts +48 -1
- package/test/src/__snapshots__/connection-report-storage.test.ts.snap +159 -2
- package/test/src/storage_compacting.test.ts +17 -2
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import { mongo, MONGO_OPERATION_TIMEOUT_MS } from '@powersync/lib-service-mongodb';
|
|
2
2
|
import { logger, ReplicationAssertionError, ServiceAssertionError } from '@powersync/lib-services-framework';
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
addChecksums,
|
|
5
|
+
InternalOpId,
|
|
6
|
+
isPartialChecksum,
|
|
7
|
+
PopulateChecksumCacheResults,
|
|
8
|
+
storage,
|
|
9
|
+
utils
|
|
10
|
+
} from '@powersync/service-core';
|
|
4
11
|
|
|
5
12
|
import { PowerSyncMongo } from './db.js';
|
|
6
13
|
import { BucketDataDocument, BucketDataKey, BucketStateDocument } from './models.js';
|
|
@@ -10,6 +17,7 @@ import { cacheKey } from './OperationBatch.js';
|
|
|
10
17
|
interface CurrentBucketState {
|
|
11
18
|
/** Bucket name */
|
|
12
19
|
bucket: string;
|
|
20
|
+
|
|
13
21
|
/**
|
|
14
22
|
* Rows seen in the bucket, with the last op_id of each.
|
|
15
23
|
*/
|
|
@@ -96,67 +104,56 @@ export class MongoCompactor {
|
|
|
96
104
|
// We can make this more efficient later on by iterating
|
|
97
105
|
// through the buckets in a single query.
|
|
98
106
|
// That makes batching more tricky, so we leave for later.
|
|
99
|
-
await this.
|
|
107
|
+
await this.compactSingleBucket(bucket);
|
|
100
108
|
}
|
|
101
109
|
} else {
|
|
102
|
-
await this.
|
|
110
|
+
await this.compactDirtyBuckets();
|
|
103
111
|
}
|
|
104
112
|
}
|
|
105
113
|
|
|
106
|
-
async
|
|
107
|
-
|
|
114
|
+
private async compactDirtyBuckets() {
|
|
115
|
+
while (!this.signal?.aborted) {
|
|
116
|
+
// Process all buckets with 1 or more changes since last time
|
|
117
|
+
const buckets = await this.dirtyBucketBatch({ minBucketChanges: 1 });
|
|
118
|
+
if (buckets.length == 0) {
|
|
119
|
+
// All done
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
for (let bucket of buckets) {
|
|
123
|
+
await this.compactSingleBucket(bucket);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
108
127
|
|
|
109
|
-
|
|
128
|
+
private async compactSingleBucket(bucket: string) {
|
|
129
|
+
const idLimitBytes = this.idLimitBytes;
|
|
110
130
|
|
|
111
|
-
let
|
|
112
|
-
|
|
131
|
+
let currentState: CurrentBucketState = {
|
|
132
|
+
bucket,
|
|
133
|
+
seen: new Map(),
|
|
134
|
+
trackingSize: 0,
|
|
135
|
+
lastNotPut: null,
|
|
136
|
+
opsSincePut: 0,
|
|
113
137
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
}
|
|
118
|
-
// Exact bucket name
|
|
119
|
-
bucketLower = bucket;
|
|
120
|
-
bucketUpper = bucket;
|
|
121
|
-
} else {
|
|
122
|
-
// Bucket definition name
|
|
123
|
-
bucketLower = `${bucket}[`;
|
|
124
|
-
bucketUpper = `${bucket}[\uFFFF`;
|
|
125
|
-
}
|
|
138
|
+
checksum: 0,
|
|
139
|
+
opCount: 0,
|
|
140
|
+
opBytes: 0
|
|
141
|
+
};
|
|
126
142
|
|
|
127
143
|
// Constant lower bound
|
|
128
144
|
const lowerBound: BucketDataKey = {
|
|
129
145
|
g: this.group_id,
|
|
130
|
-
b:
|
|
146
|
+
b: bucket,
|
|
131
147
|
o: new mongo.MinKey() as any
|
|
132
148
|
};
|
|
133
149
|
|
|
134
150
|
// Upper bound is adjusted for each batch
|
|
135
151
|
let upperBound: BucketDataKey = {
|
|
136
152
|
g: this.group_id,
|
|
137
|
-
b:
|
|
153
|
+
b: bucket,
|
|
138
154
|
o: new mongo.MaxKey() as any
|
|
139
155
|
};
|
|
140
156
|
|
|
141
|
-
const doneWithBucket = async () => {
|
|
142
|
-
if (currentState == null) {
|
|
143
|
-
return;
|
|
144
|
-
}
|
|
145
|
-
// Free memory before clearing bucket
|
|
146
|
-
currentState.seen.clear();
|
|
147
|
-
if (currentState.lastNotPut != null && currentState.opsSincePut >= 1) {
|
|
148
|
-
logger.info(
|
|
149
|
-
`Inserting CLEAR at ${this.group_id}:${currentState.bucket}:${currentState.lastNotPut} to remove ${currentState.opsSincePut} operations`
|
|
150
|
-
);
|
|
151
|
-
// Need flush() before clear()
|
|
152
|
-
await this.flush();
|
|
153
|
-
await this.clearBucket(currentState);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// Do this _after_ clearBucket so that we have accurate counts.
|
|
157
|
-
this.updateBucketChecksums(currentState);
|
|
158
|
-
};
|
|
159
|
-
|
|
160
157
|
while (!this.signal?.aborted) {
|
|
161
158
|
// Query one batch at a time, to avoid cursor timeouts
|
|
162
159
|
const cursor = this.db.bucket_data.aggregate<BucketDataDocument & { size: number | bigint }>(
|
|
@@ -184,7 +181,11 @@ export class MongoCompactor {
|
|
|
184
181
|
}
|
|
185
182
|
}
|
|
186
183
|
],
|
|
187
|
-
{
|
|
184
|
+
{
|
|
185
|
+
// batchSize is 1 more than limit to auto-close the cursor.
|
|
186
|
+
// See https://github.com/mongodb/node-mongodb-native/pull/4580
|
|
187
|
+
batchSize: this.moveBatchQueryLimit + 1
|
|
188
|
+
}
|
|
188
189
|
);
|
|
189
190
|
// We don't limit to a single batch here, since that often causes MongoDB to scan through more than it returns.
|
|
190
191
|
// Instead, we load up to the limit.
|
|
@@ -199,22 +200,6 @@ export class MongoCompactor {
|
|
|
199
200
|
upperBound = batch[batch.length - 1]._id;
|
|
200
201
|
|
|
201
202
|
for (let doc of batch) {
|
|
202
|
-
if (currentState == null || doc._id.b != currentState.bucket) {
|
|
203
|
-
await doneWithBucket();
|
|
204
|
-
|
|
205
|
-
currentState = {
|
|
206
|
-
bucket: doc._id.b,
|
|
207
|
-
seen: new Map(),
|
|
208
|
-
trackingSize: 0,
|
|
209
|
-
lastNotPut: null,
|
|
210
|
-
opsSincePut: 0,
|
|
211
|
-
|
|
212
|
-
checksum: 0,
|
|
213
|
-
opCount: 0,
|
|
214
|
-
opBytes: 0
|
|
215
|
-
};
|
|
216
|
-
}
|
|
217
|
-
|
|
218
203
|
if (doc._id.o > this.maxOpId) {
|
|
219
204
|
continue;
|
|
220
205
|
}
|
|
@@ -285,12 +270,22 @@ export class MongoCompactor {
|
|
|
285
270
|
}
|
|
286
271
|
}
|
|
287
272
|
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
273
|
+
logger.info(`Processed batch of length ${batch.length} current bucket: ${bucket}`);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// Free memory before clearing bucket
|
|
277
|
+
currentState.seen.clear();
|
|
278
|
+
if (currentState.lastNotPut != null && currentState.opsSincePut >= 1) {
|
|
279
|
+
logger.info(
|
|
280
|
+
`Inserting CLEAR at ${this.group_id}:${bucket}:${currentState.lastNotPut} to remove ${currentState.opsSincePut} operations`
|
|
281
|
+
);
|
|
282
|
+
// Need flush() before clear()
|
|
283
|
+
await this.flush();
|
|
284
|
+
await this.clearBucket(currentState);
|
|
291
285
|
}
|
|
292
286
|
|
|
293
|
-
|
|
287
|
+
// Do this _after_ clearBucket so that we have accurate counts.
|
|
288
|
+
this.updateBucketChecksums(currentState);
|
|
294
289
|
|
|
295
290
|
// Need another flush after updateBucketChecksums()
|
|
296
291
|
await this.flush();
|
|
@@ -478,50 +473,55 @@ export class MongoCompactor {
|
|
|
478
473
|
/**
|
|
479
474
|
* Subset of compact, only populating checksums where relevant.
|
|
480
475
|
*/
|
|
481
|
-
async populateChecksums() {
|
|
482
|
-
|
|
483
|
-
let lowerBound: BucketStateDocument['_id'] = {
|
|
484
|
-
g: this.group_id,
|
|
485
|
-
b: new mongo.MinKey() as any
|
|
486
|
-
};
|
|
487
|
-
// This is static
|
|
488
|
-
const upperBound: BucketStateDocument['_id'] = {
|
|
489
|
-
g: this.group_id,
|
|
490
|
-
b: new mongo.MaxKey() as any
|
|
491
|
-
};
|
|
476
|
+
async populateChecksums(options: { minBucketChanges: number }): Promise<PopulateChecksumCacheResults> {
|
|
477
|
+
let count = 0;
|
|
492
478
|
while (!this.signal?.aborted) {
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
479
|
+
const buckets = await this.dirtyBucketBatch(options);
|
|
480
|
+
if (buckets.length == 0) {
|
|
481
|
+
// All done
|
|
482
|
+
break;
|
|
483
|
+
}
|
|
484
|
+
const start = Date.now();
|
|
485
|
+
logger.info(`Calculating checksums for batch of ${buckets.length} buckets, starting at ${buckets[0]}`);
|
|
486
|
+
|
|
487
|
+
await this.updateChecksumsBatch(buckets);
|
|
488
|
+
logger.info(`Updated checksums for batch of ${buckets.length} buckets in ${Date.now() - start}ms`);
|
|
489
|
+
count += buckets.length;
|
|
490
|
+
}
|
|
491
|
+
return { buckets: count };
|
|
492
|
+
}
|
|
501
493
|
|
|
502
|
-
|
|
503
|
-
|
|
494
|
+
/**
|
|
495
|
+
* Returns a batch of dirty buckets - buckets with most changes first.
|
|
496
|
+
*
|
|
497
|
+
* This cannot be used to iterate on its own - the client is expected to process these buckets and
|
|
498
|
+
* set estimate_since_compact.count: 0 when done, before fetching the next batch.
|
|
499
|
+
*/
|
|
500
|
+
private async dirtyBucketBatch(options: { minBucketChanges: number }): Promise<string[]> {
|
|
501
|
+
if (options.minBucketChanges <= 0) {
|
|
502
|
+
throw new ReplicationAssertionError('minBucketChanges must be >= 1');
|
|
503
|
+
}
|
|
504
|
+
// We make use of an index on {_id.g: 1, 'estimate_since_compact.count': -1}
|
|
505
|
+
const dirtyBuckets = await this.db.bucket_state
|
|
506
|
+
.find(
|
|
507
|
+
{
|
|
508
|
+
'_id.g': this.group_id,
|
|
509
|
+
'estimate_since_compact.count': { $gte: options.minBucketChanges }
|
|
510
|
+
},
|
|
511
|
+
{
|
|
504
512
|
projection: {
|
|
505
513
|
_id: 1
|
|
506
514
|
},
|
|
507
515
|
sort: {
|
|
508
|
-
|
|
516
|
+
'estimate_since_compact.count': -1
|
|
509
517
|
},
|
|
510
518
|
limit: 5_000,
|
|
511
519
|
maxTimeMS: MONGO_OPERATION_TIMEOUT_MS
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
// All done
|
|
516
|
-
break;
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
logger.info(`Calculating checksums for batch of ${bucketsWithoutChecksums.length} buckets`);
|
|
520
|
-
|
|
521
|
-
await this.updateChecksumsBatch(bucketsWithoutChecksums.map((b) => b._id.b));
|
|
520
|
+
}
|
|
521
|
+
)
|
|
522
|
+
.toArray();
|
|
522
523
|
|
|
523
|
-
|
|
524
|
-
}
|
|
524
|
+
return dirtyBuckets.map((bucket) => bucket._id.b);
|
|
525
525
|
}
|
|
526
526
|
|
|
527
527
|
private async updateChecksumsBatch(buckets: string[]) {
|
|
@@ -555,6 +555,10 @@ export class MongoCompactor {
|
|
|
555
555
|
count: bucketChecksum.count,
|
|
556
556
|
checksum: BigInt(bucketChecksum.checksum),
|
|
557
557
|
bytes: null
|
|
558
|
+
},
|
|
559
|
+
estimate_since_compact: {
|
|
560
|
+
count: 0,
|
|
561
|
+
bytes: 0
|
|
558
562
|
}
|
|
559
563
|
}
|
|
560
564
|
},
|
|
@@ -16,6 +16,8 @@ import {
|
|
|
16
16
|
InternalOpId,
|
|
17
17
|
internalToExternalOpId,
|
|
18
18
|
maxLsn,
|
|
19
|
+
PopulateChecksumCacheOptions,
|
|
20
|
+
PopulateChecksumCacheResults,
|
|
19
21
|
ProtocolOpId,
|
|
20
22
|
ReplicationCheckpoint,
|
|
21
23
|
storage,
|
|
@@ -404,7 +406,9 @@ export class MongoSyncBucketStorage
|
|
|
404
406
|
limit: batchLimit,
|
|
405
407
|
// Increase batch size above the default 101, so that we can fill an entire batch in
|
|
406
408
|
// one go.
|
|
407
|
-
batchSize
|
|
409
|
+
// batchSize is 1 more than limit to auto-close the cursor.
|
|
410
|
+
// See https://github.com/mongodb/node-mongodb-native/pull/4580
|
|
411
|
+
batchSize: batchLimit + 1,
|
|
408
412
|
// Raw mode is returns an array of Buffer instead of parsed documents.
|
|
409
413
|
// We use it so that:
|
|
410
414
|
// 1. We can calculate the document size accurately without serializing again.
|
|
@@ -664,7 +668,7 @@ export class MongoSyncBucketStorage
|
|
|
664
668
|
}
|
|
665
669
|
}
|
|
666
670
|
|
|
667
|
-
async populatePersistentChecksumCache(options:
|
|
671
|
+
async populatePersistentChecksumCache(options: PopulateChecksumCacheOptions): Promise<PopulateChecksumCacheResults> {
|
|
668
672
|
logger.info(`Populating persistent checksum cache...`);
|
|
669
673
|
const start = Date.now();
|
|
670
674
|
// We do a minimal compact here.
|
|
@@ -675,9 +679,14 @@ export class MongoSyncBucketStorage
|
|
|
675
679
|
memoryLimitMB: 0
|
|
676
680
|
});
|
|
677
681
|
|
|
678
|
-
await compactor.populateChecksums(
|
|
682
|
+
const result = await compactor.populateChecksums({
|
|
683
|
+
// There are cases with millions of small buckets, in which case it can take very long to
|
|
684
|
+
// populate the checksums, with minimal benefit. We skip the small buckets here.
|
|
685
|
+
minBucketChanges: options.minBucketChanges ?? 10
|
|
686
|
+
});
|
|
679
687
|
const duration = Date.now() - start;
|
|
680
688
|
logger.info(`Populated persistent checksum cache in ${(duration / 1000).toFixed(1)}s`);
|
|
689
|
+
return result;
|
|
681
690
|
}
|
|
682
691
|
|
|
683
692
|
/**
|
|
@@ -906,7 +915,9 @@ export class MongoSyncBucketStorage
|
|
|
906
915
|
'_id.b': 1
|
|
907
916
|
},
|
|
908
917
|
limit: limit + 1,
|
|
909
|
-
batchSize
|
|
918
|
+
// batchSize is 1 more than limit to auto-close the cursor.
|
|
919
|
+
// See https://github.com/mongodb/node-mongodb-native/pull/4580
|
|
920
|
+
batchSize: limit + 2,
|
|
910
921
|
singleBatch: true
|
|
911
922
|
}
|
|
912
923
|
)
|
|
@@ -936,7 +947,9 @@ export class MongoSyncBucketStorage
|
|
|
936
947
|
lookup: 1
|
|
937
948
|
},
|
|
938
949
|
limit: limit + 1,
|
|
939
|
-
batchSize
|
|
950
|
+
// batchSize is 1 more than limit to auto-close the cursor.
|
|
951
|
+
// See https://github.com/mongodb/node-mongodb-native/pull/4580
|
|
952
|
+
batchSize: limit + 2,
|
|
940
953
|
singleBatch: true
|
|
941
954
|
}
|
|
942
955
|
)
|
|
@@ -111,7 +111,9 @@ export class MongoWriteCheckpointAPI implements storage.WriteCheckpointAPI {
|
|
|
111
111
|
},
|
|
112
112
|
{
|
|
113
113
|
limit: limit + 1,
|
|
114
|
-
batchSize
|
|
114
|
+
// batchSize is 1 more than limit to auto-close the cursor.
|
|
115
|
+
// See https://github.com/mongodb/node-mongodb-native/pull/4580
|
|
116
|
+
batchSize: limit + 2,
|
|
115
117
|
singleBatch: true
|
|
116
118
|
}
|
|
117
119
|
)
|
|
@@ -140,7 +142,9 @@ export class MongoWriteCheckpointAPI implements storage.WriteCheckpointAPI {
|
|
|
140
142
|
},
|
|
141
143
|
{
|
|
142
144
|
limit: limit + 1,
|
|
143
|
-
batchSize
|
|
145
|
+
// batchSize is 1 more than limit to auto-close the cursor.
|
|
146
|
+
// See https://github.com/mongodb/node-mongodb-native/pull/4580
|
|
147
|
+
batchSize: limit + 2,
|
|
144
148
|
singleBatch: true
|
|
145
149
|
}
|
|
146
150
|
)
|
|
@@ -158,6 +158,19 @@ export class PowerSyncMongo {
|
|
|
158
158
|
{ name: 'bucket_updates', unique: true }
|
|
159
159
|
);
|
|
160
160
|
}
|
|
161
|
+
/**
|
|
162
|
+
* Only use in migrations and tests.
|
|
163
|
+
*/
|
|
164
|
+
async createBucketStateIndex2() {
|
|
165
|
+
// TODO: Implement a better mechanism to use migrations in tests
|
|
166
|
+
await this.bucket_state.createIndex(
|
|
167
|
+
{
|
|
168
|
+
'_id.g': 1,
|
|
169
|
+
'estimate_since_compact.count': -1
|
|
170
|
+
},
|
|
171
|
+
{ name: 'dirty_count' }
|
|
172
|
+
);
|
|
173
|
+
}
|
|
161
174
|
}
|
|
162
175
|
|
|
163
176
|
export function createPowerSyncMongo(config: MongoStorageConfig, options?: lib_mongo.MongoConnectionOptions) {
|
package/src/utils/util.ts
CHANGED
|
@@ -39,7 +39,7 @@ export function generateSlotName(prefix: string, sync_rules_id: number) {
|
|
|
39
39
|
* However, that makes `has_more` detection very difficult, since the cursor is always closed
|
|
40
40
|
* after the first batch. Instead, we do a workaround to only fetch a single batch below.
|
|
41
41
|
*
|
|
42
|
-
* For this to be effective, set batchSize = limit in the find command.
|
|
42
|
+
* For this to be effective, set batchSize = limit + 1 in the find command.
|
|
43
43
|
*/
|
|
44
44
|
export async function readSingleBatch<T>(cursor: mongo.AbstractCursor<T>): Promise<{ data: T[]; hasMore: boolean }> {
|
|
45
45
|
try {
|
|
@@ -114,3 +114,50 @@ export function setSessionSnapshotTime(session: mongo.ClientSession, time: bson.
|
|
|
114
114
|
throw new ServiceAssertionError(`Session snapshotTime is already set`);
|
|
115
115
|
}
|
|
116
116
|
}
|
|
117
|
+
|
|
118
|
+
export const createPaginatedConnectionQuery = async <T extends mongo.Document>(
|
|
119
|
+
query: mongo.Filter<T>,
|
|
120
|
+
collection: mongo.Collection<T>,
|
|
121
|
+
limit: number,
|
|
122
|
+
cursor?: string
|
|
123
|
+
) => {
|
|
124
|
+
const createQuery = (cursor?: string) => {
|
|
125
|
+
if (!cursor) {
|
|
126
|
+
return query;
|
|
127
|
+
}
|
|
128
|
+
return {
|
|
129
|
+
$and: [
|
|
130
|
+
query,
|
|
131
|
+
{
|
|
132
|
+
/** We are using the connected at date as the cursor so that the functionality works the same on Postgres implementation
|
|
133
|
+
* The id field in postgres is an uuid, this will work similarly to the ObjectId in Mongodb
|
|
134
|
+
* */
|
|
135
|
+
connected_at: {
|
|
136
|
+
$lt: new Date(cursor)
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
]
|
|
140
|
+
} as mongo.Filter<T>;
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
/** cursor.count() deprecated */
|
|
144
|
+
const total = await collection.countDocuments(query);
|
|
145
|
+
|
|
146
|
+
const findCursor = collection.find(createQuery(cursor), {
|
|
147
|
+
sort: {
|
|
148
|
+
/** We are sorting by connected at date descending to match cursor Postgres implementation */
|
|
149
|
+
connected_at: -1
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
const items = await findCursor.limit(limit).toArray();
|
|
154
|
+
const count = items.length;
|
|
155
|
+
return {
|
|
156
|
+
items,
|
|
157
|
+
total,
|
|
158
|
+
count,
|
|
159
|
+
/** Setting the cursor to the connected at date of the last item in the list */
|
|
160
|
+
cursor: count === limit ? items[items.length - 1].connected_at.toISOString() : undefined,
|
|
161
|
+
more: count < total
|
|
162
|
+
};
|
|
163
|
+
};
|
|
@@ -3,13 +3,13 @@
|
|
|
3
3
|
exports[`Connection reporting storage > Should create a connection report if its after a day 1`] = `
|
|
4
4
|
[
|
|
5
5
|
{
|
|
6
|
-
"client_id": "
|
|
6
|
+
"client_id": "client_one",
|
|
7
7
|
"sdk": "powersync-js/1.24.5",
|
|
8
8
|
"user_agent": "powersync-js/1.21.0 powersync-web Firefox/141 linux",
|
|
9
9
|
"user_id": "user_week",
|
|
10
10
|
},
|
|
11
11
|
{
|
|
12
|
-
"client_id": "
|
|
12
|
+
"client_id": "client_one",
|
|
13
13
|
"sdk": "powersync-js/1.24.5",
|
|
14
14
|
"user_agent": "powersync-js/1.21.0 powersync-web Firefox/141 linux",
|
|
15
15
|
"user_id": "user_week",
|
|
@@ -213,3 +213,160 @@ exports[`Report storage tests > Should show currently connected users 1`] = `
|
|
|
213
213
|
"users": 2,
|
|
214
214
|
}
|
|
215
215
|
`;
|
|
216
|
+
|
|
217
|
+
exports[`Report storage tests > Should show paginated response of all connections of specified client_id 1`] = `
|
|
218
|
+
{
|
|
219
|
+
"count": 1,
|
|
220
|
+
"cursor": undefined,
|
|
221
|
+
"items": [
|
|
222
|
+
{
|
|
223
|
+
"client_id": "client_two",
|
|
224
|
+
"sdk": "powersync-js/1.21.1",
|
|
225
|
+
"user_agent": "powersync-js/1.21.0 powersync-web Chromium/138 linux",
|
|
226
|
+
"user_id": "user_two",
|
|
227
|
+
},
|
|
228
|
+
],
|
|
229
|
+
"more": false,
|
|
230
|
+
"total": 1,
|
|
231
|
+
}
|
|
232
|
+
`;
|
|
233
|
+
|
|
234
|
+
exports[`Report storage tests > Should show paginated response of all connections with a limit 1`] = `
|
|
235
|
+
{
|
|
236
|
+
"count": 4,
|
|
237
|
+
"cursor": "<removed-for-snapshot>",
|
|
238
|
+
"items": [
|
|
239
|
+
{
|
|
240
|
+
"client_id": "client_one",
|
|
241
|
+
"sdk": "powersync-dart/1.6.4",
|
|
242
|
+
"user_agent": "powersync-dart/1.6.4 Dart (flutter-web) Chrome/128 android",
|
|
243
|
+
"user_id": "user_one",
|
|
244
|
+
},
|
|
245
|
+
{
|
|
246
|
+
"client_id": "client_four",
|
|
247
|
+
"sdk": "powersync-js/1.21.4",
|
|
248
|
+
"user_agent": "powersync-js/1.21.0 powersync-web Firefox/141 linux",
|
|
249
|
+
"user_id": "user_four",
|
|
250
|
+
},
|
|
251
|
+
{
|
|
252
|
+
"client_id": "",
|
|
253
|
+
"sdk": "unknown",
|
|
254
|
+
"user_agent": "Dart (flutter-web) Chrome/128 android",
|
|
255
|
+
"user_id": "user_one",
|
|
256
|
+
},
|
|
257
|
+
{
|
|
258
|
+
"client_id": "client_two",
|
|
259
|
+
"sdk": "powersync-js/1.21.1",
|
|
260
|
+
"user_agent": "powersync-js/1.21.0 powersync-web Chromium/138 linux",
|
|
261
|
+
"user_id": "user_two",
|
|
262
|
+
},
|
|
263
|
+
],
|
|
264
|
+
"more": true,
|
|
265
|
+
"total": 8,
|
|
266
|
+
}
|
|
267
|
+
`;
|
|
268
|
+
|
|
269
|
+
exports[`Report storage tests > Should show paginated response of all connections with a limit 2`] = `
|
|
270
|
+
{
|
|
271
|
+
"count": 4,
|
|
272
|
+
"cursor": undefined,
|
|
273
|
+
"items": [
|
|
274
|
+
{
|
|
275
|
+
"client_id": "client_one",
|
|
276
|
+
"sdk": "powersync-dart/1.6.4",
|
|
277
|
+
"user_agent": "powersync-dart/1.6.4 Dart (flutter-web) Chrome/128 android",
|
|
278
|
+
"user_id": "user_one",
|
|
279
|
+
},
|
|
280
|
+
{
|
|
281
|
+
"client_id": "client_four",
|
|
282
|
+
"sdk": "powersync-js/1.21.4",
|
|
283
|
+
"user_agent": "powersync-js/1.21.0 powersync-web Firefox/141 linux",
|
|
284
|
+
"user_id": "user_four",
|
|
285
|
+
},
|
|
286
|
+
{
|
|
287
|
+
"client_id": "",
|
|
288
|
+
"sdk": "unknown",
|
|
289
|
+
"user_agent": "Dart (flutter-web) Chrome/128 android",
|
|
290
|
+
"user_id": "user_one",
|
|
291
|
+
},
|
|
292
|
+
{
|
|
293
|
+
"client_id": "client_two",
|
|
294
|
+
"sdk": "powersync-js/1.21.1",
|
|
295
|
+
"user_agent": "powersync-js/1.21.0 powersync-web Chromium/138 linux",
|
|
296
|
+
"user_id": "user_two",
|
|
297
|
+
},
|
|
298
|
+
],
|
|
299
|
+
"more": true,
|
|
300
|
+
"total": 8,
|
|
301
|
+
}
|
|
302
|
+
`;
|
|
303
|
+
|
|
304
|
+
exports[`Report storage tests > Should show paginated response of connections of specified user_id 1`] = `
|
|
305
|
+
{
|
|
306
|
+
"count": 2,
|
|
307
|
+
"cursor": undefined,
|
|
308
|
+
"items": [
|
|
309
|
+
{
|
|
310
|
+
"client_id": "client_one",
|
|
311
|
+
"sdk": "powersync-dart/1.6.4",
|
|
312
|
+
"user_agent": "powersync-dart/1.6.4 Dart (flutter-web) Chrome/128 android",
|
|
313
|
+
"user_id": "user_one",
|
|
314
|
+
},
|
|
315
|
+
{
|
|
316
|
+
"client_id": "",
|
|
317
|
+
"sdk": "unknown",
|
|
318
|
+
"user_agent": "Dart (flutter-web) Chrome/128 android",
|
|
319
|
+
"user_id": "user_one",
|
|
320
|
+
},
|
|
321
|
+
],
|
|
322
|
+
"more": false,
|
|
323
|
+
"total": 2,
|
|
324
|
+
}
|
|
325
|
+
`;
|
|
326
|
+
|
|
327
|
+
exports[`Report storage tests > Should show paginated response of connections over a date range 1`] = `
|
|
328
|
+
{
|
|
329
|
+
"count": 6,
|
|
330
|
+
"cursor": undefined,
|
|
331
|
+
"items": [
|
|
332
|
+
{
|
|
333
|
+
"client_id": "client_one",
|
|
334
|
+
"sdk": "powersync-dart/1.6.4",
|
|
335
|
+
"user_agent": "powersync-dart/1.6.4 Dart (flutter-web) Chrome/128 android",
|
|
336
|
+
"user_id": "user_one",
|
|
337
|
+
},
|
|
338
|
+
{
|
|
339
|
+
"client_id": "client_four",
|
|
340
|
+
"sdk": "powersync-js/1.21.4",
|
|
341
|
+
"user_agent": "powersync-js/1.21.0 powersync-web Firefox/141 linux",
|
|
342
|
+
"user_id": "user_four",
|
|
343
|
+
},
|
|
344
|
+
{
|
|
345
|
+
"client_id": "client_two",
|
|
346
|
+
"sdk": "powersync-js/1.21.1",
|
|
347
|
+
"user_agent": "powersync-js/1.21.0 powersync-web Chromium/138 linux",
|
|
348
|
+
"user_id": "user_two",
|
|
349
|
+
},
|
|
350
|
+
{
|
|
351
|
+
"client_id": "",
|
|
352
|
+
"sdk": "unknown",
|
|
353
|
+
"user_agent": "Dart (flutter-web) Chrome/128 android",
|
|
354
|
+
"user_id": "user_one",
|
|
355
|
+
},
|
|
356
|
+
{
|
|
357
|
+
"client_id": "client_three",
|
|
358
|
+
"sdk": "powersync-js/1.21.2",
|
|
359
|
+
"user_agent": "powersync-js/1.21.0 powersync-web Firefox/141 linux",
|
|
360
|
+
"user_id": "user_three",
|
|
361
|
+
},
|
|
362
|
+
{
|
|
363
|
+
"client_id": "client_one",
|
|
364
|
+
"sdk": "powersync-js/1.24.5",
|
|
365
|
+
"user_agent": "powersync-js/1.21.0 powersync-web Firefox/141 linux",
|
|
366
|
+
"user_id": "user_week",
|
|
367
|
+
},
|
|
368
|
+
],
|
|
369
|
+
"more": false,
|
|
370
|
+
"total": 6,
|
|
371
|
+
}
|
|
372
|
+
`;
|
|
@@ -97,10 +97,25 @@ bucket_definitions:
|
|
|
97
97
|
await populate(bucketStorage);
|
|
98
98
|
const { checkpoint } = await bucketStorage.getCheckpoint();
|
|
99
99
|
|
|
100
|
-
|
|
100
|
+
// Default is to small small numbers - should be a no-op
|
|
101
|
+
const result0 = await bucketStorage.populatePersistentChecksumCache({
|
|
102
|
+
maxOpId: checkpoint
|
|
103
|
+
});
|
|
104
|
+
expect(result0.buckets).toEqual(0);
|
|
105
|
+
|
|
106
|
+
// This should cache the checksums for the two buckets
|
|
107
|
+
const result1 = await bucketStorage.populatePersistentChecksumCache({
|
|
108
|
+
maxOpId: checkpoint,
|
|
109
|
+
minBucketChanges: 1
|
|
110
|
+
});
|
|
111
|
+
expect(result1.buckets).toEqual(2);
|
|
112
|
+
|
|
113
|
+
// This should be a no-op, as the checksums are already cached
|
|
114
|
+
const result2 = await bucketStorage.populatePersistentChecksumCache({
|
|
101
115
|
maxOpId: checkpoint,
|
|
102
|
-
|
|
116
|
+
minBucketChanges: 1
|
|
103
117
|
});
|
|
118
|
+
expect(result2.buckets).toEqual(0);
|
|
104
119
|
|
|
105
120
|
const checksumAfter = await bucketStorage.getChecksums(checkpoint, ['by_user2["u1"]', 'by_user2["u2"]']);
|
|
106
121
|
expect(checksumAfter.get('by_user2["u1"]')).toEqual({
|