99} from '@powersync/lib-services-framework' ;
1010import {
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 ! ;
0 commit comments