99} from '@powersync/lib-services-framework' ;
1010import {
1111 BroadcastIterable ,
12+ CHECKPOINT_INVALIDATE_ALL ,
1213 CheckpointChanges ,
1314 GetCheckpointChangesOptions ,
1415 InternalOpId ,
@@ -903,6 +904,7 @@ export class MongoSyncBucketStorage
903904 private async getParameterBucketChanges (
904905 options : GetCheckpointChangesOptions
905906 ) : Promise < Pick < CheckpointChanges , 'updatedParameterBucketDefinitions' | 'invalidateParameterBuckets' > > {
907+ // TODO: limit max query running time
906908 const parameterUpdates = await this . db . bucket_parameters
907909 . find (
908910 {
@@ -929,6 +931,9 @@ export class MongoSyncBucketStorage
929931 } ;
930932 }
931933
934+ // TODO:
935+ // We can optimize this by implementing it like ChecksumCache: We can use partial cache results to do
936+ // more efficient lookups in some cases.
932937 private checkpointChangesCache = new LRUCache < string , CheckpointChanges , { options : GetCheckpointChangesOptions } > ( {
933938 max : 50 ,
934939 maxSize : 10 * 1024 * 1024 ,
@@ -940,7 +945,31 @@ export class MongoSyncBucketStorage
940945 }
941946 } ) ;
942947
948+ private _hasDynamicBucketsCached : boolean | undefined = undefined ;
949+
950+ private hasDynamicBucketQueries ( ) : boolean {
951+ if ( this . _hasDynamicBucketsCached != null ) {
952+ return this . _hasDynamicBucketsCached ;
953+ }
954+ const syncRules = this . getParsedSyncRules ( {
955+ defaultSchema : 'default' // n/a
956+ } ) ;
957+ const hasDynamicBuckets = syncRules . hasDynamicBucketQueries ( ) ;
958+ this . _hasDynamicBucketsCached = hasDynamicBuckets ;
959+ return hasDynamicBuckets ;
960+ }
961+
943962 async getCheckpointChanges ( options : GetCheckpointChangesOptions ) : Promise < CheckpointChanges > {
963+ if ( ! this . hasDynamicBucketQueries ( ) ) {
964+ // Special case when we have no dynamic parameter queries.
965+ // In this case, we can avoid doing any queries.
966+ return {
967+ invalidateDataBuckets : true ,
968+ updatedDataBuckets : [ ] ,
969+ invalidateParameterBuckets : false ,
970+ updatedParameterBucketDefinitions : [ ]
971+ } ;
972+ }
944973 const key = `${ options . lastCheckpoint } _${ options . nextCheckpoint } ` ;
945974 const result = await this . checkpointChangesCache . fetch ( key , { context : { options } } ) ;
946975 return result ! ;
0 commit comments