9
9
} from '@powersync/lib-services-framework' ;
10
10
import {
11
11
BroadcastIterable ,
12
+ CHECKPOINT_INVALIDATE_ALL ,
12
13
CheckpointChanges ,
13
14
GetCheckpointChangesOptions ,
14
15
getLookupBucketDefinitionName ,
@@ -884,6 +885,7 @@ export class MongoSyncBucketStorage
884
885
private async getParameterBucketChanges (
885
886
options : GetCheckpointChangesOptions
886
887
) : Promise < Pick < CheckpointChanges , 'updatedParameterBucketDefinitions' | 'invalidateParameterBuckets' > > {
888
+ // TODO: limit max query running time
887
889
const parameterUpdates = await this . db . bucket_parameters
888
890
. find (
889
891
{
@@ -910,6 +912,9 @@ export class MongoSyncBucketStorage
910
912
} ;
911
913
}
912
914
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.
913
918
private checkpointChangesCache = new LRUCache < string , CheckpointChanges , { options : GetCheckpointChangesOptions } > ( {
914
919
max : 50 ,
915
920
maxSize : 10 * 1024 * 1024 ,
@@ -921,7 +926,31 @@ export class MongoSyncBucketStorage
921
926
}
922
927
} ) ;
923
928
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
+
924
943
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
+ }
925
954
const key = `${ options . lastCheckpoint } _${ options . nextCheckpoint } ` ;
926
955
const result = await this . checkpointChangesCache . fetch ( key , { context : { options } } ) ;
927
956
return result ! ;
0 commit comments