4
4
BucketSource ,
5
5
BucketSourceType ,
6
6
RequestedStream ,
7
+ RequestJwtPayload ,
7
8
RequestParameters ,
8
9
ResolvedBucket ,
9
10
SqlSyncRules
@@ -28,10 +29,9 @@ export interface BucketChecksumStateOptions {
28
29
syncContext : SyncContext ;
29
30
bucketStorage : BucketChecksumStateStorage ;
30
31
syncRules : SqlSyncRules ;
31
- syncParams : RequestParameters ;
32
+ tokenPayload : RequestJwtPayload ;
32
33
syncRequest : util . StreamingSyncRequest ;
33
34
logger ?: Logger ;
34
- initialBucketPositions ?: { name : string ; after : util . InternalOpId } [ ] ;
35
35
}
36
36
37
37
type BucketSyncState = {
@@ -79,14 +79,14 @@ export class BucketChecksumState {
79
79
options . syncContext ,
80
80
options . bucketStorage ,
81
81
options . syncRules ,
82
- options . syncParams ,
82
+ options . tokenPayload ,
83
83
options . syncRequest ,
84
84
this . logger
85
85
) ;
86
86
this . bucketDataPositions = new Map ( ) ;
87
87
88
- for ( let { name, after : start } of options . initialBucketPositions ?? [ ] ) {
89
- this . bucketDataPositions . set ( name , { start_op_id : start } ) ;
88
+ for ( let { name, after : start } of options . syncRequest . buckets ?? [ ] ) {
89
+ this . bucketDataPositions . set ( name , { start_op_id : BigInt ( start ) } ) ;
90
90
}
91
91
}
92
92
@@ -199,7 +199,7 @@ export class BucketChecksumState {
199
199
} ) ) ;
200
200
bucketsToFetch = [ ...generateBucketsToFetch ] . map ( ( b ) => {
201
201
return {
202
- ... bucketDescriptionMap . get ( b ) ! ,
202
+ priority : bucketDescriptionMap . get ( b ) ! . priority ,
203
203
bucket : b
204
204
} ;
205
205
} ) ;
@@ -233,11 +233,11 @@ export class BucketChecksumState {
233
233
message += `buckets: ${ allBuckets . length } ${ limitedBuckets ( allBuckets , 20 ) } ` ;
234
234
this . logger . info ( message , { checkpoint : base . checkpoint , user_id : user_id , buckets : allBuckets . length } ) ;
235
235
} ;
236
- bucketsToFetch = allBuckets ;
236
+ bucketsToFetch = allBuckets . map ( ( b ) => ( { bucket : b . bucket , priority : b . priority } ) ) ;
237
237
238
238
const subscriptions : util . StreamDescription [ ] = [ ] ;
239
239
for ( const source of this . parameterState . syncRules . bucketSources ) {
240
- if ( source . type == BucketSourceType . SYNC_STREAM && this . parameterState . isSubscribedToStream ( source ) ) {
240
+ if ( this . parameterState . isSubscribedToStream ( source ) ) {
241
241
subscriptions . push ( {
242
242
name : source . name ,
243
243
is_default : source . subscribedToByDefault
@@ -360,7 +360,11 @@ export class BucketParameterState {
360
360
public readonly syncRules : SqlSyncRules ;
361
361
public readonly syncParams : RequestParameters ;
362
362
private readonly querier : BucketParameterQuerier ;
363
- private readonly staticBuckets : Map < string , BucketDescription > ;
363
+ /**
364
+ * Static buckets. This map is guaranteed not to change during a request, since resolving static buckets can only
365
+ * take request parameters into account,
366
+ */
367
+ private readonly staticBuckets : Map < string , ResolvedBucket > ;
364
368
private readonly includeDefaultStreams : boolean ;
365
369
// Indexed by the client-side id
366
370
private readonly explicitStreamSubscriptions : Record < string , util . RequestedStreamSubscription > ;
@@ -375,22 +379,22 @@ export class BucketParameterState {
375
379
context : SyncContext ,
376
380
bucketStorage : BucketChecksumStateStorage ,
377
381
syncRules : SqlSyncRules ,
378
- syncParams : RequestParameters ,
382
+ tokenPayload : RequestJwtPayload ,
379
383
request : util . StreamingSyncRequest ,
380
384
logger : Logger
381
385
) {
382
386
this . context = context ;
383
387
this . bucketStorage = bucketStorage ;
384
388
this . syncRules = syncRules ;
385
- this . syncParams = syncParams ;
389
+ this . syncParams = new RequestParameters ( tokenPayload , request . parameters ?? { } ) ;
386
390
this . logger = logger ;
387
391
388
392
const idToStreamSubscription : Record < string , util . RequestedStreamSubscription > = { } ;
389
393
const streamsByName : Record < string , RequestedStream [ ] > = { } ;
390
- const subscriptions = request . subscriptions ;
394
+ const subscriptions = request . streams ;
391
395
if ( subscriptions ) {
392
- for ( const subscription of subscriptions . opened ) {
393
- idToStreamSubscription [ subscription . stream ] = subscription ;
396
+ for ( const subscription of subscriptions . subscriptions ) {
397
+ idToStreamSubscription [ subscription . client_id ] = subscription ;
394
398
395
399
const syncRuleStream : RequestedStream = {
396
400
parameters : subscription . parameters ?? { } ,
@@ -412,7 +416,7 @@ export class BucketParameterState {
412
416
streams : streamsByName
413
417
} ) ;
414
418
415
- this . staticBuckets = new Map < string , BucketDescription > (
419
+ this . staticBuckets = new Map < string , ResolvedBucket > (
416
420
mergeBuckets ( this . querier . staticBuckets ) . map ( ( b ) => [ b . bucket , b ] )
417
421
) ;
418
422
this . lookups = new Set < string > ( this . querier . parameterQueryLookups . map ( ( l ) => JSONBig . stringify ( l . values ) ) ) ;
@@ -441,14 +445,13 @@ export class BucketParameterState {
441
445
}
442
446
443
447
return {
444
- definition : description . definition ,
445
448
bucket : description . bucket ,
446
449
priority : priorityOverride ?? description . priority ,
447
450
subscriptions : description . inclusion_reasons . map ( ( reason ) => {
448
451
if ( reason == 'default' ) {
449
- return { def : description . definition } ;
452
+ return { default : 0 } ; // TODO
450
453
} else {
451
- return { sub : reason . subscription } ;
454
+ return reason . subscription ;
452
455
}
453
456
} )
454
457
} ;
@@ -489,19 +492,19 @@ export class BucketParameterState {
489
492
* For static buckets, we can keep track of which buckets have been updated.
490
493
*/
491
494
private async getCheckpointUpdateStatic ( checkpoint : storage . StorageCheckpointUpdate ) : Promise < CheckpointUpdate > {
492
- const querier = this . querier ;
495
+ const staticBuckets = [ ... this . staticBuckets . values ( ) ] ;
493
496
const update = checkpoint . update ;
494
497
495
498
if ( update . invalidateDataBuckets ) {
496
499
return {
497
- buckets : querier . staticBuckets ,
500
+ buckets : staticBuckets ,
498
501
updatedBuckets : INVALIDATE_ALL_BUCKETS
499
502
} ;
500
503
}
501
504
502
505
const updatedBuckets = new Set < string > ( getIntersection ( this . staticBuckets , update . updatedDataBuckets ) ) ;
503
506
return {
504
- buckets : querier . staticBuckets ,
507
+ buckets : staticBuckets ,
505
508
updatedBuckets
506
509
} ;
507
510
}
@@ -512,7 +515,7 @@ export class BucketParameterState {
512
515
private async getCheckpointUpdateDynamic ( checkpoint : storage . StorageCheckpointUpdate ) : Promise < CheckpointUpdate > {
513
516
const querier = this . querier ;
514
517
const storage = this . bucketStorage ;
515
- const staticBuckets = querier . staticBuckets ;
518
+ const staticBuckets = this . staticBuckets . values ( ) ;
516
519
const update = checkpoint . update ;
517
520
518
521
let hasParameterChange = false ;
@@ -556,7 +559,7 @@ export class BucketParameterState {
556
559
}
557
560
}
558
561
}
559
- const allBuckets = [ ...staticBuckets , ...dynamicBuckets ] ;
562
+ const allBuckets = [ ...staticBuckets , ...mergeBuckets ( dynamicBuckets ) ] ;
560
563
561
564
if ( invalidateDataBuckets ) {
562
565
return {
@@ -632,15 +635,15 @@ function limitedBuckets(buckets: string[] | { bucket: string }[], limit: number)
632
635
* bucket.
633
636
*/
634
637
function mergeBuckets ( buckets : ResolvedBucket [ ] ) : ResolvedBucket [ ] {
635
- const byDefinition : Record < string , ResolvedBucket > = { } ;
638
+ const byBucketId : Record < string , ResolvedBucket > = { } ;
636
639
637
640
for ( const bucket of buckets ) {
638
- if ( Object . hasOwn ( byDefinition , bucket . definition ) ) {
639
- byDefinition [ bucket . definition ] . inclusion_reasons . push ( ...bucket . inclusion_reasons ) ;
641
+ if ( Object . hasOwn ( byBucketId , bucket . bucket ) ) {
642
+ byBucketId [ bucket . bucket ] . inclusion_reasons . push ( ...bucket . inclusion_reasons ) ;
640
643
} else {
641
- byDefinition [ bucket . definition ] = bucket ;
644
+ byBucketId [ bucket . bucket ] = structuredClone ( bucket ) ;
642
645
}
643
646
}
644
647
645
- return Object . values ( byDefinition ) ;
648
+ return Object . values ( byBucketId ) ;
646
649
}
0 commit comments