Skip to content

Commit 4b70186

Browse files
committed
Optimization: skip checking for bucket_parameters changes if there are
none.
1 parent a807c9a commit 4b70186

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

modules/module-mongodb-storage/src/storage/implementation/MongoSyncBucketStorage.ts

+29
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from '@powersync/lib-services-framework';
1010
import {
1111
BroadcastIterable,
12+
CHECKPOINT_INVALIDATE_ALL,
1213
CheckpointChanges,
1314
GetCheckpointChangesOptions,
1415
getLookupBucketDefinitionName,
@@ -884,6 +885,7 @@ export class MongoSyncBucketStorage
884885
private async getParameterBucketChanges(
885886
options: GetCheckpointChangesOptions
886887
): Promise<Pick<CheckpointChanges, 'updatedParameterBucketDefinitions' | 'invalidateParameterBuckets'>> {
888+
// TODO: limit max query running time
887889
const parameterUpdates = await this.db.bucket_parameters
888890
.find(
889891
{
@@ -910,6 +912,9 @@ export class MongoSyncBucketStorage
910912
};
911913
}
912914

915+
// TODO:
916+
// We can optimize this by implementing it like ChecksumCache: We can use partial cache results to do
917+
// more efficient lookups in some cases.
913918
private checkpointChangesCache = new LRUCache<string, CheckpointChanges, { options: GetCheckpointChangesOptions }>({
914919
max: 50,
915920
maxSize: 10 * 1024 * 1024,
@@ -921,7 +926,31 @@ export class MongoSyncBucketStorage
921926
}
922927
});
923928

929+
private _hasDynamicBucketsCached: boolean | undefined = undefined;
930+
931+
private hasDynamicBucketQueries(): boolean {
932+
if (this._hasDynamicBucketsCached != null) {
933+
return this._hasDynamicBucketsCached;
934+
}
935+
const syncRules = this.getParsedSyncRules({
936+
defaultSchema: 'default' // n/a
937+
});
938+
const hasDynamicBuckets = syncRules.hasDynamicBucketQueries();
939+
this._hasDynamicBucketsCached = hasDynamicBuckets;
940+
return hasDynamicBuckets;
941+
}
942+
924943
async getCheckpointChanges(options: GetCheckpointChangesOptions): Promise<CheckpointChanges> {
944+
if (!this.hasDynamicBucketQueries()) {
945+
// Special case when we have no dynamic parameter queries.
946+
// In this case, we can avoid doing any queries.
947+
return {
948+
invalidateDataBuckets: true,
949+
updatedDataBuckets: [],
950+
invalidateParameterBuckets: false,
951+
updatedParameterBucketDefinitions: []
952+
};
953+
}
925954
const key = `${options.lastCheckpoint}_${options.nextCheckpoint}`;
926955
const result = await this.checkpointChangesCache.fetch(key, { context: { options } });
927956
return result!;

packages/sync-rules/src/SqlBucketDescriptor.ts

+4
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ export class SqlBucketDescriptor {
133133
return results;
134134
}
135135

136+
hasDynamicBucketQueries(): boolean {
137+
return this.parameter_queries.length > 0;
138+
}
139+
136140
getSourceTables(): Set<TablePattern> {
137141
let result = new Set<TablePattern>();
138142
for (let query of this.parameter_queries) {

packages/sync-rules/src/SqlSyncRules.ts

+4
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,10 @@ export class SqlSyncRules implements SyncRules {
326326
return mergeBucketParameterQueriers(queriers);
327327
}
328328

329+
hasDynamicBucketQueries() {
330+
return this.bucket_descriptors.some((query) => query.hasDynamicBucketQueries());
331+
}
332+
329333
getSourceTables(): TablePattern[] {
330334
const sourceTables = new Map<String, TablePattern>();
331335
for (const bucket of this.bucket_descriptors) {

packages/sync-rules/test/src/sync_rules.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ bucket_definitions:
3737
bucket: 'mybucket[]'
3838
}
3939
]);
40+
expect(rules.hasDynamicBucketQueries).toBe(false);
4041
expect(rules.getBucketParameterQuerier(normalizeTokenParameters({}))).toMatchObject({
4142
staticBuckets: [{ bucket: 'mybucket[]', priority: 3 }],
4243
hasDynamicBuckets: false,
@@ -936,6 +937,7 @@ bucket_definitions:
936937
);
937938
const bucket = rules.bucket_descriptors[0];
938939
expect(bucket.bucket_parameters).toEqual(['user_id']);
940+
expect(rules.hasDynamicBucketQueries).toBe(true);
939941

940942
expect(rules.getBucketParameterQuerier(normalizeTokenParameters({ user_id: 'user1' }))).toMatchObject({
941943
hasDynamicBuckets: true,

0 commit comments

Comments
 (0)