Skip to content

Commit

Permalink
Optimization: skip checking for bucket_parameters changes if there are
Browse files Browse the repository at this point in the history
none.
  • Loading branch information
rkistner committed Feb 12, 2025
1 parent a807c9a commit ea7591a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '@powersync/lib-services-framework';
import {
BroadcastIterable,
CHECKPOINT_INVALIDATE_ALL,
CheckpointChanges,
GetCheckpointChangesOptions,
getLookupBucketDefinitionName,
Expand Down Expand Up @@ -884,6 +885,7 @@ export class MongoSyncBucketStorage
private async getParameterBucketChanges(
options: GetCheckpointChangesOptions
): Promise<Pick<CheckpointChanges, 'updatedParameterBucketDefinitions' | 'invalidateParameterBuckets'>> {
// TODO: limit max query running time
const parameterUpdates = await this.db.bucket_parameters
.find(
{
Expand Down Expand Up @@ -921,7 +923,31 @@ export class MongoSyncBucketStorage
}
});

private _hasDynamicBucketsCached: boolean | undefined = undefined;

private hasDynamicBucketQueries(): boolean {
if (this._hasDynamicBucketsCached != null) {
return this._hasDynamicBucketsCached;
}
const syncRules = this.getParsedSyncRules({
defaultSchema: 'default' // n/a
});
const hasDynamicBuckets = syncRules.hasDynamicBucketQueries();
this._hasDynamicBucketsCached = hasDynamicBuckets;
return hasDynamicBuckets;
}

async getCheckpointChanges(options: GetCheckpointChangesOptions): Promise<CheckpointChanges> {
if (!this.hasDynamicBucketQueries()) {
// Special case when we have no dynamic parameter queries.
// In this case, we can avoid doing any queries.
return {
invalidateDataBuckets: true,
updatedDataBuckets: [],
invalidateParameterBuckets: false,
updatedParameterBucketDefinitions: []
};
}
const key = `${options.lastCheckpoint}_${options.nextCheckpoint}`;
const result = await this.checkpointChangesCache.fetch(key, { context: { options } });
return result!;
Expand Down
4 changes: 4 additions & 0 deletions packages/sync-rules/src/SqlBucketDescriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ export class SqlBucketDescriptor {
return results;
}

hasDynamicBucketQueries(): boolean {
return this.parameter_queries.length > 0;
}

getSourceTables(): Set<TablePattern> {
let result = new Set<TablePattern>();
for (let query of this.parameter_queries) {
Expand Down
4 changes: 4 additions & 0 deletions packages/sync-rules/src/SqlSyncRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@ export class SqlSyncRules implements SyncRules {
return mergeBucketParameterQueriers(queriers);
}

hasDynamicBucketQueries() {
return this.bucket_descriptors.some((query) => query.hasDynamicBucketQueries());
}

getSourceTables(): TablePattern[] {
const sourceTables = new Map<String, TablePattern>();
for (const bucket of this.bucket_descriptors) {
Expand Down
2 changes: 2 additions & 0 deletions packages/sync-rules/test/src/sync_rules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ bucket_definitions:
bucket: 'mybucket[]'
}
]);
expect(rules.hasDynamicBucketQueries).toBe(false);
expect(rules.getBucketParameterQuerier(normalizeTokenParameters({}))).toMatchObject({
staticBuckets: [{ bucket: 'mybucket[]', priority: 3 }],
hasDynamicBuckets: false,
Expand Down Expand Up @@ -936,6 +937,7 @@ bucket_definitions:
);
const bucket = rules.bucket_descriptors[0];
expect(bucket.bucket_parameters).toEqual(['user_id']);
expect(rules.hasDynamicBucketQueries).toBe(true);

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

0 comments on commit ea7591a

Please sign in to comment.