@@ -18,7 +18,9 @@ import {
1818 ReplicationCheckpoint ,
1919 storage ,
2020 utils ,
21- WatchWriteCheckpointOptions
21+ WatchWriteCheckpointOptions ,
22+ CHECKPOINT_INVALIDATE_ALL ,
23+ deserializeParameterLookup
2224} from '@powersync/service-core' ;
2325import { SqliteJsonRow , SqliteJsonValue , SqlSyncRules } from '@powersync/service-sync-rules' ;
2426import * as bson from 'bson' ;
@@ -39,6 +41,7 @@ import { MongoBucketBatch } from './MongoBucketBatch.js';
3941import { MongoCompactor } from './MongoCompactor.js' ;
4042import { MongoWriteCheckpointAPI } from './MongoWriteCheckpointAPI.js' ;
4143import { idPrefixFilter , mapOpEntry , readSingleBatch } from './util.js' ;
44+ import { JSONBig } from '@powersync/service-jsonbig' ;
4245
4346export class MongoSyncBucketStorage
4447 extends BaseObserver < storage . SyncRulesBucketStorageListener >
@@ -795,12 +798,7 @@ export class MongoSyncBucketStorage
795798
796799 const updates : CheckpointChanges =
797800 lastCheckpoint == null
798- ? {
799- invalidateDataBuckets : true ,
800- invalidateParameterBuckets : true ,
801- updatedDataBuckets : [ ] ,
802- updatedParameterBucketDefinitions : [ ]
803- }
801+ ? CHECKPOINT_INVALIDATE_ALL
804802 : await this . getCheckpointChanges ( {
805803 lastCheckpoint : lastCheckpoint ,
806804 nextCheckpoint : checkpoint
@@ -877,7 +875,7 @@ export class MongoSyncBucketStorage
877875 // Or we could store updated buckets in a separate collection, and query those.
878876 // For now, we ignore this optimization
879877
880- // const dataBuckets = await this.db.bucket_data
878+ // const dataBucketDocuments = await this.db.bucket_data
881879 // .find(
882880 // {
883881 // '_id.g': this.group_id,
@@ -894,6 +892,14 @@ export class MongoSyncBucketStorage
894892 // )
895893 // .toArray();
896894
895+ // const buckets = dataBucketDocuments.map((doc) => doc._id.b);
896+ // const invalidateDataBuckets = buckets.length > 1000;
897+
898+ // return {
899+ // invalidateDataBuckets: invalidateDataBuckets,
900+ // updatedDataBuckets: invalidateDataBuckets ? [] : buckets
901+ // };
902+
897903 return {
898904 invalidateDataBuckets : true ,
899905 updatedDataBuckets : [ ]
@@ -902,7 +908,7 @@ export class MongoSyncBucketStorage
902908
903909 private async getParameterBucketChanges (
904910 options : GetCheckpointChangesOptions
905- ) : Promise < Pick < CheckpointChanges , 'updatedParameterBucketDefinitions ' | 'invalidateParameterBuckets' > > {
911+ ) : Promise < Pick < CheckpointChanges , 'updatedParameterLookups ' | 'invalidateParameterBuckets' > > {
906912 // TODO: limit max query running time
907913 const parameterUpdates = await this . db . bucket_parameters
908914 . find (
@@ -924,9 +930,9 @@ export class MongoSyncBucketStorage
924930
925931 return {
926932 invalidateParameterBuckets : invalidateParameterUpdates ,
927- updatedParameterBucketDefinitions : invalidateParameterUpdates
928- ? [ ]
929- : [ ... new Set < string > ( parameterUpdates . map ( ( p ) => getLookupBucketDefinitionName ( p . lookup ) ) ) ]
933+ updatedParameterLookups : invalidateParameterUpdates
934+ ? new Set < string > ( )
935+ : new Set < string > ( parameterUpdates . map ( ( p ) => JSONBig . stringify ( deserializeParameterLookup ( p . lookup ) ) ) )
930936 } ;
931937 }
932938
@@ -937,7 +943,7 @@ export class MongoSyncBucketStorage
937943 max : 50 ,
938944 maxSize : 10 * 1024 * 1024 ,
939945 sizeCalculation : ( value : CheckpointChanges ) => {
940- return 100 + value . updatedParameterBucketDefinitions . reduce < number > ( ( a , b ) => a + b . length , 0 ) ;
946+ return 100 + [ ... value . updatedParameterLookups ] . reduce < number > ( ( a , b ) => a + b . length , 0 ) ;
941947 } ,
942948 fetchMethod : async ( _key , _staleValue , options ) => {
943949 return this . getCheckpointChangesInternal ( options . context . options ) ;
@@ -966,7 +972,7 @@ export class MongoSyncBucketStorage
966972 invalidateDataBuckets : true ,
967973 updatedDataBuckets : [ ] ,
968974 invalidateParameterBuckets : false ,
969- updatedParameterBucketDefinitions : [ ]
975+ updatedParameterLookups : new Set < string > ( )
970976 } ;
971977 }
972978 const key = `${ options . lastCheckpoint } _${ options . nextCheckpoint } ` ;
0 commit comments