@powersync/service-module-mongodb-storage 0.12.7 → 0.12.9

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 (75) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/dist/index.d.ts +1 -0
  3. package/dist/index.js +1 -0
  4. package/dist/index.js.map +1 -1
  5. package/dist/migrations/db/migrations/1752661449910-connection-reporting.d.ts +3 -0
  6. package/dist/migrations/db/migrations/1752661449910-connection-reporting.js +36 -0
  7. package/dist/migrations/db/migrations/1752661449910-connection-reporting.js.map +1 -0
  8. package/dist/migrations/db/migrations/1760433882550-bucket-state-index2.d.ts +3 -0
  9. package/dist/migrations/db/migrations/1760433882550-bucket-state-index2.js +25 -0
  10. package/dist/migrations/db/migrations/1760433882550-bucket-state-index2.js.map +1 -0
  11. package/dist/storage/MongoBucketStorage.js +1 -1
  12. package/dist/storage/MongoBucketStorage.js.map +1 -1
  13. package/dist/storage/MongoReportStorage.d.ts +17 -0
  14. package/dist/storage/MongoReportStorage.js +152 -0
  15. package/dist/storage/MongoReportStorage.js.map +1 -0
  16. package/dist/storage/implementation/MongoBucketBatch.js +1 -1
  17. package/dist/storage/implementation/MongoBucketBatch.js.map +1 -1
  18. package/dist/storage/implementation/MongoCompactor.d.ts +13 -3
  19. package/dist/storage/implementation/MongoCompactor.js +86 -90
  20. package/dist/storage/implementation/MongoCompactor.js.map +1 -1
  21. package/dist/storage/implementation/MongoStorageProvider.d.ts +1 -1
  22. package/dist/storage/implementation/MongoStorageProvider.js +7 -3
  23. package/dist/storage/implementation/MongoStorageProvider.js.map +1 -1
  24. package/dist/storage/implementation/MongoSyncBucketStorage.d.ts +2 -2
  25. package/dist/storage/implementation/MongoSyncBucketStorage.js +16 -5
  26. package/dist/storage/implementation/MongoSyncBucketStorage.js.map +1 -1
  27. package/dist/storage/implementation/MongoWriteCheckpointAPI.js +6 -2
  28. package/dist/storage/implementation/MongoWriteCheckpointAPI.js.map +1 -1
  29. package/dist/storage/implementation/PersistedBatch.js +1 -1
  30. package/dist/storage/implementation/PersistedBatch.js.map +1 -1
  31. package/dist/storage/implementation/db.d.ts +10 -1
  32. package/dist/storage/implementation/db.js +25 -0
  33. package/dist/storage/implementation/db.js.map +1 -1
  34. package/dist/storage/implementation/models.d.ts +3 -0
  35. package/dist/storage/storage-index.d.ts +3 -2
  36. package/dist/storage/storage-index.js +3 -2
  37. package/dist/storage/storage-index.js.map +1 -1
  38. package/dist/utils/test-utils.d.ts +13 -0
  39. package/dist/utils/test-utils.js +40 -0
  40. package/dist/utils/test-utils.js.map +1 -0
  41. package/dist/{storage/implementation → utils}/util.d.ts +2 -7
  42. package/dist/{storage/implementation → utils}/util.js +1 -16
  43. package/dist/utils/util.js.map +1 -0
  44. package/dist/utils/utils-index.d.ts +2 -0
  45. package/dist/utils/utils-index.js +3 -0
  46. package/dist/utils/utils-index.js.map +1 -0
  47. package/package.json +9 -9
  48. package/src/index.ts +1 -0
  49. package/src/migrations/db/migrations/1752661449910-connection-reporting.ts +58 -0
  50. package/src/migrations/db/migrations/1760433882550-bucket-state-index2.ts +34 -0
  51. package/src/storage/MongoBucketStorage.ts +1 -1
  52. package/src/storage/MongoReportStorage.ts +174 -0
  53. package/src/storage/implementation/MongoBucketBatch.ts +1 -1
  54. package/src/storage/implementation/MongoCompactor.ts +100 -96
  55. package/src/storage/implementation/MongoStorageProvider.ts +9 -4
  56. package/src/storage/implementation/MongoSyncBucketStorage.ts +20 -6
  57. package/src/storage/implementation/MongoWriteCheckpointAPI.ts +6 -2
  58. package/src/storage/implementation/PersistedBatch.ts +1 -1
  59. package/src/storage/implementation/db.ts +30 -0
  60. package/src/storage/implementation/models.ts +3 -0
  61. package/src/storage/storage-index.ts +3 -2
  62. package/src/utils/test-utils.ts +57 -0
  63. package/src/{storage/implementation → utils}/util.ts +3 -19
  64. package/src/utils/utils-index.ts +2 -0
  65. package/test/src/__snapshots__/connection-report-storage.test.ts.snap +215 -0
  66. package/test/src/connection-report-storage.test.ts +133 -0
  67. package/test/src/storage.test.ts +3 -51
  68. package/test/src/storage_compacting.test.ts +17 -2
  69. package/test/src/util.ts +6 -2
  70. package/tsconfig.tsbuildinfo +1 -1
  71. package/dist/storage/implementation/MongoTestStorageFactoryGenerator.d.ts +0 -9
  72. package/dist/storage/implementation/MongoTestStorageFactoryGenerator.js +0 -19
  73. package/dist/storage/implementation/MongoTestStorageFactoryGenerator.js.map +0 -1
  74. package/dist/storage/implementation/util.js.map +0 -1
  75. package/src/storage/implementation/MongoTestStorageFactoryGenerator.ts +0 -31
@@ -0,0 +1,174 @@
1
+ import { storage } from '@powersync/service-core';
2
+ import { event_types } from '@powersync/service-types';
3
+ import { PowerSyncMongo } from './implementation/db.js';
4
+ import { logger } from '@powersync/lib-services-framework';
5
+
6
+ export class MongoReportStorage implements storage.ReportStorage {
7
+ public readonly db: PowerSyncMongo;
8
+
9
+ constructor(db: PowerSyncMongo) {
10
+ this.db = db;
11
+ }
12
+ async deleteOldConnectionData(data: event_types.DeleteOldConnectionData): Promise<void> {
13
+ const { date } = data;
14
+ const result = await this.db.connection_report_events.deleteMany({
15
+ connected_at: { $lt: date },
16
+ $or: [
17
+ { disconnected_at: { $exists: true } },
18
+ { jwt_exp: { $lt: new Date() }, disconnected_at: { $exists: false } }
19
+ ]
20
+ });
21
+ if (result.deletedCount > 0) {
22
+ logger.info(
23
+ `TTL from ${date.toISOString()}: ${result.deletedCount} MongoDB documents have been removed from connection_report_events.`
24
+ );
25
+ }
26
+ }
27
+
28
+ async getClientConnectionReports(
29
+ data: event_types.ClientConnectionReportRequest
30
+ ): Promise<event_types.ClientConnectionReportResponse> {
31
+ const { start, end } = data;
32
+ const result = await this.db.connection_report_events
33
+ .aggregate<event_types.ClientConnectionReportResponse>([
34
+ {
35
+ $match: {
36
+ connected_at: { $lte: end, $gte: start }
37
+ }
38
+ },
39
+ this.connectionsFacetPipeline(),
40
+ this.connectionsProjectPipeline()
41
+ ])
42
+ .toArray();
43
+ return result[0];
44
+ }
45
+
46
+ async reportClientConnection(data: event_types.ClientConnectionBucketData): Promise<void> {
47
+ const updateFilter = this.updateDocFilter(data.user_id, data.client_id!);
48
+ await this.db.connection_report_events.findOneAndUpdate(
49
+ updateFilter,
50
+ {
51
+ $set: data,
52
+ $unset: {
53
+ disconnected_at: ''
54
+ }
55
+ },
56
+ {
57
+ upsert: true
58
+ }
59
+ );
60
+ }
61
+ async reportClientDisconnection(data: event_types.ClientDisconnectionEventData): Promise<void> {
62
+ const { connected_at, user_id, client_id } = data;
63
+ await this.db.connection_report_events.findOneAndUpdate(
64
+ {
65
+ client_id,
66
+ user_id,
67
+ connected_at
68
+ },
69
+ {
70
+ $set: {
71
+ disconnected_at: data.disconnected_at
72
+ },
73
+ $unset: {
74
+ jwt_exp: ''
75
+ }
76
+ }
77
+ );
78
+ }
79
+ async getConnectedClients(): Promise<event_types.ClientConnectionReportResponse> {
80
+ const result = await this.db.connection_report_events
81
+ .aggregate<event_types.ClientConnectionReportResponse>([
82
+ {
83
+ $match: {
84
+ disconnected_at: { $exists: false },
85
+ jwt_exp: { $gt: new Date() }
86
+ }
87
+ },
88
+ this.connectionsFacetPipeline(),
89
+ this.connectionsProjectPipeline()
90
+ ])
91
+ .toArray();
92
+ return result[0];
93
+ }
94
+
95
+ async [Symbol.asyncDispose]() {
96
+ // No-op
97
+ }
98
+
99
+ private parseJsDate(date: Date) {
100
+ const year = date.getUTCFullYear();
101
+ const month = date.getUTCMonth();
102
+ const today = date.getUTCDate();
103
+ const day = date.getUTCDay();
104
+ return {
105
+ year,
106
+ month,
107
+ today,
108
+ day,
109
+ parsedDate: date
110
+ };
111
+ }
112
+
113
+ private connectionsFacetPipeline() {
114
+ return {
115
+ $facet: {
116
+ unique_users: [
117
+ {
118
+ $group: {
119
+ _id: '$user_id'
120
+ }
121
+ },
122
+ {
123
+ $count: 'count'
124
+ }
125
+ ],
126
+ sdk_versions_array: [
127
+ {
128
+ $group: {
129
+ _id: '$sdk',
130
+ total: { $sum: 1 },
131
+ client_ids: { $addToSet: '$client_id' },
132
+ user_ids: { $addToSet: '$user_id' }
133
+ }
134
+ },
135
+ {
136
+ $project: {
137
+ _id: 0,
138
+ sdk: '$_id',
139
+ users: { $size: '$user_ids' },
140
+ clients: { $size: '$client_ids' }
141
+ }
142
+ },
143
+ {
144
+ $sort: {
145
+ sdk: 1
146
+ }
147
+ }
148
+ ]
149
+ }
150
+ };
151
+ }
152
+
153
+ private connectionsProjectPipeline() {
154
+ return {
155
+ $project: {
156
+ users: { $ifNull: [{ $arrayElemAt: ['$unique_users.count', 0] }, 0] },
157
+ sdks: '$sdk_versions_array'
158
+ }
159
+ };
160
+ }
161
+
162
+ private updateDocFilter(userId: string, clientId: string) {
163
+ const { year, month, today } = this.parseJsDate(new Date());
164
+ const nextDay = today + 1;
165
+ return {
166
+ user_id: userId,
167
+ client_id: clientId,
168
+ connected_at: {
169
+ $gte: new Date(Date.UTC(year, month, today)),
170
+ $lt: new Date(Date.UTC(year, month, nextDay))
171
+ }
172
+ };
173
+ }
174
+ }
@@ -28,7 +28,7 @@ import { MongoIdSequence } from './MongoIdSequence.js';
28
28
  import { batchCreateCustomWriteCheckpoints } from './MongoWriteCheckpointAPI.js';
29
29
  import { cacheKey, OperationBatch, RecordOperation } from './OperationBatch.js';
30
30
  import { PersistedBatch } from './PersistedBatch.js';
31
- import { idPrefixFilter } from './util.js';
31
+ import { idPrefixFilter } from '../../utils/util.js';
32
32
 
33
33
  /**
34
34
  * 15MB
@@ -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 { addChecksums, InternalOpId, isPartialChecksum, storage, utils } from '@powersync/service-core';
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.compactInternal(bucket);
107
+ await this.compactSingleBucket(bucket);
100
108
  }
101
109
  } else {
102
- await this.compactInternal(undefined);
110
+ await this.compactDirtyBuckets();
103
111
  }
104
112
  }
105
113
 
106
- async compactInternal(bucket: string | undefined) {
107
- const idLimitBytes = this.idLimitBytes;
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
- let currentState: CurrentBucketState | null = null;
128
+ private async compactSingleBucket(bucket: string) {
129
+ const idLimitBytes = this.idLimitBytes;
110
130
 
111
- let bucketLower: string | mongo.MinKey;
112
- let bucketUpper: string | mongo.MaxKey;
131
+ let currentState: CurrentBucketState = {
132
+ bucket,
133
+ seen: new Map(),
134
+ trackingSize: 0,
135
+ lastNotPut: null,
136
+ opsSincePut: 0,
113
137
 
114
- if (bucket == null) {
115
- bucketLower = new mongo.MinKey();
116
- bucketUpper = new mongo.MaxKey();
117
- } else if (bucket.includes('[')) {
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: bucketLower as string,
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: bucketUpper as string,
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
- { batchSize: this.moveBatchQueryLimit }
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
- if (currentState != null) {
289
- logger.info(`Processed batch of length ${batch.length} current bucket: ${currentState.bucket}`);
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
- await doneWithBucket();
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
- // This is updated after each batch
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
- // By filtering buckets, we effectively make this "resumeable".
494
- const filter: mongo.Filter<BucketStateDocument> = {
495
- _id: {
496
- $gt: lowerBound,
497
- $lt: upperBound
498
- },
499
- compacted_state: { $exists: false }
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
- const bucketsWithoutChecksums = await this.db.bucket_state
503
- .find(filter, {
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
- _id: 1
516
+ 'estimate_since_compact.count': -1
509
517
  },
510
518
  limit: 5_000,
511
519
  maxTimeMS: MONGO_OPERATION_TIMEOUT_MS
512
- })
513
- .toArray();
514
- if (bucketsWithoutChecksums.length == 0) {
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
- lowerBound = bucketsWithoutChecksums[bucketsWithoutChecksums.length - 1]._id;
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
  },
@@ -4,8 +4,9 @@ import { POWERSYNC_VERSION, storage } from '@powersync/service-core';
4
4
  import { MongoStorageConfig } from '../../types/types.js';
5
5
  import { MongoBucketStorage } from '../MongoBucketStorage.js';
6
6
  import { PowerSyncMongo } from './db.js';
7
+ import { MongoReportStorage } from '../MongoReportStorage.js';
7
8
 
8
- export class MongoStorageProvider implements storage.BucketStorageProvider {
9
+ export class MongoStorageProvider implements storage.StorageProvider {
9
10
  get type() {
10
11
  return lib_mongo.MONGO_CONNECTION_TYPE;
11
12
  }
@@ -37,15 +38,19 @@ export class MongoStorageProvider implements storage.BucketStorageProvider {
37
38
  await client.connect();
38
39
 
39
40
  const database = new PowerSyncMongo(client, { database: resolvedConfig.storage.database });
40
- const factory = new MongoBucketStorage(database, {
41
+ const syncStorageFactory = new MongoBucketStorage(database, {
41
42
  // TODO currently need the entire resolved config due to this
42
43
  slot_name_prefix: resolvedConfig.slot_name_prefix
43
44
  });
45
+
46
+ // Storage factory for reports
47
+ const reportStorageFactory = new MongoReportStorage(database);
44
48
  return {
45
- storage: factory,
49
+ storage: syncStorageFactory,
50
+ reportStorage: reportStorageFactory,
46
51
  shutDown: async () => {
47
52
  shuttingDown = true;
48
- await factory[Symbol.asyncDispose]();
53
+ await syncStorageFactory[Symbol.asyncDispose]();
49
54
  await client.close();
50
55
  },
51
56
  tearDown: () => {
@@ -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,
@@ -35,7 +37,8 @@ import { MongoChecksumOptions, MongoChecksums } from './MongoChecksums.js';
35
37
  import { MongoCompactor } from './MongoCompactor.js';
36
38
  import { MongoParameterCompactor } from './MongoParameterCompactor.js';
37
39
  import { MongoWriteCheckpointAPI } from './MongoWriteCheckpointAPI.js';
38
- import { idPrefixFilter, mapOpEntry, readSingleBatch, setSessionSnapshotTime } from './util.js';
40
+ import { idPrefixFilter, mapOpEntry, readSingleBatch, setSessionSnapshotTime } from '../../utils/util.js';
41
+
39
42
 
40
43
  export interface MongoSyncBucketStorageOptions {
41
44
  checksumOptions?: MongoChecksumOptions;
@@ -403,7 +406,9 @@ export class MongoSyncBucketStorage
403
406
  limit: batchLimit,
404
407
  // Increase batch size above the default 101, so that we can fill an entire batch in
405
408
  // one go.
406
- batchSize: batchLimit,
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,
407
412
  // Raw mode is returns an array of Buffer instead of parsed documents.
408
413
  // We use it so that:
409
414
  // 1. We can calculate the document size accurately without serializing again.
@@ -663,7 +668,7 @@ export class MongoSyncBucketStorage
663
668
  }
664
669
  }
665
670
 
666
- async populatePersistentChecksumCache(options: Required<Pick<CompactOptions, 'signal' | 'maxOpId'>>): Promise<void> {
671
+ async populatePersistentChecksumCache(options: PopulateChecksumCacheOptions): Promise<PopulateChecksumCacheResults> {
667
672
  logger.info(`Populating persistent checksum cache...`);
668
673
  const start = Date.now();
669
674
  // We do a minimal compact here.
@@ -674,9 +679,14 @@ export class MongoSyncBucketStorage
674
679
  memoryLimitMB: 0
675
680
  });
676
681
 
677
- 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
+ });
678
687
  const duration = Date.now() - start;
679
688
  logger.info(`Populated persistent checksum cache in ${(duration / 1000).toFixed(1)}s`);
689
+ return result;
680
690
  }
681
691
 
682
692
  /**
@@ -905,7 +915,9 @@ export class MongoSyncBucketStorage
905
915
  '_id.b': 1
906
916
  },
907
917
  limit: limit + 1,
908
- batchSize: limit + 1,
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,
909
921
  singleBatch: true
910
922
  }
911
923
  )
@@ -935,7 +947,9 @@ export class MongoSyncBucketStorage
935
947
  lookup: 1
936
948
  },
937
949
  limit: limit + 1,
938
- batchSize: limit + 1,
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,
939
953
  singleBatch: true
940
954
  }
941
955
  )
@@ -111,7 +111,9 @@ export class MongoWriteCheckpointAPI implements storage.WriteCheckpointAPI {
111
111
  },
112
112
  {
113
113
  limit: limit + 1,
114
- batchSize: limit + 1,
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: limit + 1,
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
  )
@@ -16,7 +16,7 @@ import {
16
16
  CurrentDataDocument,
17
17
  SourceKey
18
18
  } from './models.js';
19
- import { replicaIdToSubkey } from './util.js';
19
+ import { replicaIdToSubkey } from '../../utils/util.js';
20
20
 
21
21
  /**
22
22
  * Maximum size of operations we write in a single transaction.
@@ -8,6 +8,7 @@ import {
8
8
  BucketParameterDocument,
9
9
  BucketStateDocument,
10
10
  CheckpointEventDocument,
11
+ ClientConnectionDocument,
11
12
  CurrentDataDocument,
12
13
  CustomWriteCheckpointDocument,
13
14
  IdSequenceDocument,
@@ -37,6 +38,7 @@ export class PowerSyncMongo {
37
38
  readonly locks: mongo.Collection<lib_mongo.locks.Lock>;
38
39
  readonly bucket_state: mongo.Collection<BucketStateDocument>;
39
40
  readonly checkpoint_events: mongo.Collection<CheckpointEventDocument>;
41
+ readonly connection_report_events: mongo.Collection<ClientConnectionDocument>;
40
42
 
41
43
  readonly client: mongo.MongoClient;
42
44
  readonly db: mongo.Db;
@@ -61,6 +63,7 @@ export class PowerSyncMongo {
61
63
  this.locks = this.db.collection('locks');
62
64
  this.bucket_state = this.db.collection('bucket_state');
63
65
  this.checkpoint_events = this.db.collection('checkpoint_events');
66
+ this.connection_report_events = this.db.collection('connection_report_events');
64
67
  }
65
68
 
66
69
  /**
@@ -128,6 +131,20 @@ export class PowerSyncMongo {
128
131
  });
129
132
  }
130
133
 
134
+ /**
135
+ * Only use in migrations and tests.
136
+ */
137
+ async createConnectionReportingCollection() {
138
+ const existingCollections = await this.db
139
+ .listCollections({ name: 'connection_report_events' }, { nameOnly: false })
140
+ .toArray();
141
+ const collection = existingCollections[0];
142
+ if (collection != null) {
143
+ return;
144
+ }
145
+ await this.db.createCollection('connection_report_events');
146
+ }
147
+
131
148
  /**
132
149
  * Only use in migrations and tests.
133
150
  */
@@ -141,6 +158,19 @@ export class PowerSyncMongo {
141
158
  { name: 'bucket_updates', unique: true }
142
159
  );
143
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
+ }
144
174
  }
145
175
 
146
176
  export function createPowerSyncMongo(config: MongoStorageConfig, options?: lib_mongo.MongoConnectionOptions) {
@@ -1,6 +1,7 @@
1
1
  import { InternalOpId, storage } from '@powersync/service-core';
2
2
  import { SqliteJsonValue } from '@powersync/service-sync-rules';
3
3
  import * as bson from 'bson';
4
+ import { event_types } from '@powersync/service-types';
4
5
 
5
6
  /**
6
7
  * Replica id uniquely identifying a row on the source database.
@@ -238,3 +239,5 @@ export interface InstanceDocument {
238
239
  // The instance UUID
239
240
  _id: string;
240
241
  }
242
+
243
+ export interface ClientConnectionDocument extends event_types.ClientConnection {}