@@ -119,10 +119,10 @@ public final class LocalStore {
119
119
private final ReferenceSet localViewReferences ;
120
120
121
121
/** Maps a query to the data about that query. */
122
- private final QueryCache queryCache ;
122
+ private final TargetCache targetCache ;
123
123
124
124
/** Maps a targetId to data about its query. */
125
- private final SparseArray <QueryData > queryDataByTarget ;
125
+ private final SparseArray <TargetData > queryDataByTarget ;
126
126
127
127
/** Maps a target to its targetID. */
128
128
private final Map <Target , Integer > targetIdByTarget ;
@@ -134,8 +134,8 @@ public LocalStore(Persistence persistence, QueryEngine queryEngine, User initial
134
134
hardAssert (
135
135
persistence .isStarted (), "LocalStore was passed an unstarted persistence implementation" );
136
136
this .persistence = persistence ;
137
- queryCache = persistence .getQueryCache ();
138
- targetIdGenerator = TargetIdGenerator .forQueryCache ( queryCache .getHighestTargetId ());
137
+ targetCache = persistence .getTargetCache ();
138
+ targetIdGenerator = TargetIdGenerator .forTargetCache ( targetCache .getHighestTargetId ());
139
139
mutationQueue = persistence .getMutationQueue (initialUser );
140
140
remoteDocuments = persistence .getRemoteDocumentCache ();
141
141
localDocuments =
@@ -320,7 +320,7 @@ public void setLastStreamToken(ByteString streamToken) {
320
320
* buffer incoming snapshots from the backend).
321
321
*/
322
322
public SnapshotVersion getLastRemoteSnapshotVersion () {
323
- return queryCache .getLastRemoteSnapshotVersion ();
323
+ return targetCache .getLastRemoteSnapshotVersion ();
324
324
}
325
325
326
326
/**
@@ -345,29 +345,29 @@ public ImmutableSortedMap<DocumentKey, MaybeDocument> applyRemoteEvent(RemoteEve
345
345
int targetId = boxedTargetId ;
346
346
TargetChange change = entry .getValue ();
347
347
348
- QueryData oldQueryData = queryDataByTarget .get (targetId );
349
- if (oldQueryData == null ) {
348
+ TargetData oldTargetData = queryDataByTarget .get (targetId );
349
+ if (oldTargetData == null ) {
350
350
// We don't update the remote keys if the query is not active. This ensures that
351
351
// we persist the updated query data along with the updated assignment.
352
352
continue ;
353
353
}
354
354
355
- queryCache .removeMatchingKeys (change .getRemovedDocuments (), targetId );
356
- queryCache .addMatchingKeys (change .getAddedDocuments (), targetId );
355
+ targetCache .removeMatchingKeys (change .getRemovedDocuments (), targetId );
356
+ targetCache .addMatchingKeys (change .getAddedDocuments (), targetId );
357
357
358
358
ByteString resumeToken = change .getResumeToken ();
359
359
// Update the resume token if the change includes one.
360
360
if (!resumeToken .isEmpty ()) {
361
- QueryData newQueryData =
362
- oldQueryData
361
+ TargetData newTargetData =
362
+ oldTargetData
363
363
.withResumeToken (resumeToken , remoteEvent .getSnapshotVersion ())
364
364
.withSequenceNumber (sequenceNumber );
365
- queryDataByTarget .put (targetId , newQueryData );
365
+ queryDataByTarget .put (targetId , newTargetData );
366
366
367
367
// Update the query data if there are target changes (or if sufficient time has
368
368
// passed since the last update).
369
- if (shouldPersistQueryData ( oldQueryData , newQueryData , change )) {
370
- queryCache . updateQueryData ( newQueryData );
369
+ if (shouldPersistTargetData ( oldTargetData , newTargetData , change )) {
370
+ targetCache . updateTargetData ( newTargetData );
371
371
}
372
372
}
373
373
}
@@ -420,45 +420,45 @@ public ImmutableSortedMap<DocumentKey, MaybeDocument> applyRemoteEvent(RemoteEve
420
420
// HACK: The only reason we allow snapshot version NONE is so that we can synthesize
421
421
// remote events when we get permission denied errors while trying to resolve the
422
422
// state of a locally cached document that is in limbo.
423
- SnapshotVersion lastRemoteVersion = queryCache .getLastRemoteSnapshotVersion ();
423
+ SnapshotVersion lastRemoteVersion = targetCache .getLastRemoteSnapshotVersion ();
424
424
if (!remoteVersion .equals (SnapshotVersion .NONE )) {
425
425
hardAssert (
426
426
remoteVersion .compareTo (lastRemoteVersion ) >= 0 ,
427
427
"Watch stream reverted to previous snapshot?? (%s < %s)" ,
428
428
remoteVersion ,
429
429
lastRemoteVersion );
430
- queryCache .setLastRemoteSnapshotVersion (remoteVersion );
430
+ targetCache .setLastRemoteSnapshotVersion (remoteVersion );
431
431
}
432
432
433
433
return localDocuments .getLocalViewOfDocuments (changedDocs );
434
434
});
435
435
}
436
436
437
437
/**
438
- * Returns true if the newQueryData should be persisted during an update of an active target.
439
- * QueryData should always be persisted when a target is being released and should not call this
438
+ * Returns true if the newTargetData should be persisted during an update of an active target.
439
+ * TargetData should always be persisted when a target is being released and should not call this
440
440
* function.
441
441
*
442
- * <p>While the target is active, QueryData updates can be omitted when nothing about the target
442
+ * <p>While the target is active, TargetData updates can be omitted when nothing about the target
443
443
* has changed except metadata like the resume token or snapshot version. Occasionally it's worth
444
444
* the extra write to prevent these values from getting too stale after a crash, but this doesn't
445
445
* have to be too frequent.
446
446
*/
447
- private static boolean shouldPersistQueryData (
448
- QueryData oldQueryData , QueryData newQueryData , TargetChange change ) {
447
+ private static boolean shouldPersistTargetData (
448
+ TargetData oldTargetData , TargetData newTargetData , TargetChange change ) {
449
449
hardAssert (
450
- !newQueryData .getResumeToken ().isEmpty (),
450
+ !newTargetData .getResumeToken ().isEmpty (),
451
451
"Attempted to persist query data with empty resume token" );
452
452
453
453
// Always persist query data if we don't already have a resume token.
454
- if (oldQueryData .getResumeToken ().isEmpty ()) return true ;
454
+ if (oldTargetData .getResumeToken ().isEmpty ()) return true ;
455
455
456
456
// Don't allow resume token changes to be buffered indefinitely. This allows us to be reasonably
457
457
// up-to-date after a crash and avoids needing to loop over all active queries on shutdown.
458
458
// Especially in the browser we may not get time to do anything interesting while the current
459
459
// tab is closing.
460
- long newSeconds = newQueryData .getSnapshotVersion ().getTimestamp ().getSeconds ();
461
- long oldSeconds = oldQueryData .getSnapshotVersion ().getTimestamp ().getSeconds ();
460
+ long newSeconds = newTargetData .getSnapshotVersion ().getTimestamp ().getSeconds ();
461
+ long oldSeconds = oldTargetData .getSnapshotVersion ().getTimestamp ().getSeconds ();
462
462
long timeDelta = newSeconds - oldSeconds ;
463
463
if (timeDelta >= RESUME_TOKEN_MAX_AGE_SECONDS ) return true ;
464
464
@@ -489,17 +489,17 @@ public void notifyLocalViewChanges(List<LocalViewChanges> viewChanges) {
489
489
localViewReferences .removeReferences (removed , targetId );
490
490
491
491
if (!viewChange .isFromCache ()) {
492
- QueryData queryData = queryDataByTarget .get (targetId );
492
+ TargetData targetData = queryDataByTarget .get (targetId );
493
493
hardAssert (
494
- queryData != null ,
494
+ targetData != null ,
495
495
"Can't set limbo-free snapshot version for unknown target: %s" ,
496
496
targetId );
497
497
498
498
// Advance the last limbo free snapshot version
499
- SnapshotVersion lastLimboFreeSnapshotVersion = queryData .getSnapshotVersion ();
500
- QueryData updatedQueryData =
501
- queryData .withLastLimboFreeSnapshotVersion (lastLimboFreeSnapshotVersion );
502
- queryDataByTarget .put (targetId , updatedQueryData );
499
+ SnapshotVersion lastLimboFreeSnapshotVersion = targetData .getSnapshotVersion ();
500
+ TargetData updatedTargetData =
501
+ targetData .withLastLimboFreeSnapshotVersion (lastLimboFreeSnapshotVersion );
502
+ queryDataByTarget .put (targetId , updatedTargetData );
503
503
}
504
504
}
505
505
});
@@ -526,12 +526,12 @@ public MaybeDocument readDocument(DocumentKey key) {
526
526
* GC'd. A query must be allocated in the local store before the store can be used to manage its
527
527
* view.
528
528
*
529
- * <p>Allocating an already allocated target will return the existing @{code QueryData } for that
529
+ * <p>Allocating an already allocated target will return the existing @{code TargetData } for that
530
530
* target.
531
531
*/
532
- public QueryData allocateTarget (Target target ) {
532
+ public TargetData allocateTarget (Target target ) {
533
533
int targetId ;
534
- QueryData cached = queryCache . getQueryData (target );
534
+ TargetData cached = targetCache . getTargetData (target );
535
535
if (cached != null ) {
536
536
// This query has been listened to previously, so reuse the previous targetID.
537
537
// TODO: freshen last accessed date?
@@ -543,12 +543,12 @@ public QueryData allocateTarget(Target target) {
543
543
() -> {
544
544
holder .targetId = targetIdGenerator .nextId ();
545
545
holder .cached =
546
- new QueryData (
546
+ new TargetData (
547
547
target ,
548
548
holder .targetId ,
549
549
persistence .getReferenceDelegate ().getCurrentSequenceNumber (),
550
550
QueryPurpose .LISTEN );
551
- queryCache . addQueryData (holder .cached );
551
+ targetCache . addTargetData (holder .cached );
552
552
});
553
553
targetId = holder .targetId ;
554
554
cached = holder .cached ;
@@ -562,22 +562,22 @@ public QueryData allocateTarget(Target target) {
562
562
}
563
563
564
564
/**
565
- * Returns the QueryData as seen by the LocalStore, including updates that may have not yet been
566
- * persisted to the QueryCache .
565
+ * Returns the TargetData as seen by the LocalStore, including updates that may have not yet been
566
+ * persisted to the TargetCache .
567
567
*/
568
568
@ VisibleForTesting
569
569
@ Nullable
570
- QueryData getQueryData (Target target ) {
570
+ TargetData getTargetData (Target target ) {
571
571
Integer targetId = targetIdByTarget .get (target );
572
572
if (targetId != null ) {
573
573
return queryDataByTarget .get (targetId );
574
574
}
575
- return queryCache . getQueryData (target );
575
+ return targetCache . getTargetData (target );
576
576
}
577
577
578
578
/** Mutable state for the transaction in allocateQuery. */
579
579
private static class AllocateQueryHolder {
580
- QueryData cached ;
580
+ TargetData cached ;
581
581
int targetId ;
582
582
}
583
583
@@ -590,8 +590,8 @@ public void releaseTarget(int targetId) {
590
590
persistence .runTransaction (
591
591
"Release target" ,
592
592
() -> {
593
- QueryData queryData = queryDataByTarget .get (targetId );
594
- hardAssert (queryData != null , "Tried to release nonexistent target: %s" , targetId );
593
+ TargetData targetData = queryDataByTarget .get (targetId );
594
+ hardAssert (targetData != null , "Tried to release nonexistent target: %s" , targetId );
595
595
596
596
// References for documents sent via Watch are automatically removed when we delete a
597
597
// query's target data from the reference delegate. Since this does not remove references
@@ -604,9 +604,9 @@ public void releaseTarget(int targetId) {
604
604
}
605
605
606
606
// Note: This also updates the query cache
607
- persistence .getReferenceDelegate ().removeTarget (queryData );
607
+ persistence .getReferenceDelegate ().removeTarget (targetData );
608
608
queryDataByTarget .remove (targetId );
609
- targetIdByTarget .remove (queryData .getTarget ());
609
+ targetIdByTarget .remove (targetData .getTarget ());
610
610
});
611
611
}
612
612
@@ -618,13 +618,13 @@ public void releaseTarget(int targetId) {
618
618
* query execution.
619
619
*/
620
620
public QueryResult executeQuery (Query query , boolean usePreviousResults ) {
621
- QueryData queryData = getQueryData (query .toTarget ());
621
+ TargetData targetData = getTargetData (query .toTarget ());
622
622
SnapshotVersion lastLimboFreeSnapshotVersion = SnapshotVersion .NONE ;
623
623
ImmutableSortedSet <DocumentKey > remoteKeys = DocumentKey .emptyKeySet ();
624
624
625
- if (queryData != null ) {
626
- lastLimboFreeSnapshotVersion = queryData .getLastLimboFreeSnapshotVersion ();
627
- remoteKeys = this .queryCache .getMatchingKeysForTargetId (queryData .getTargetId ());
625
+ if (targetData != null ) {
626
+ lastLimboFreeSnapshotVersion = targetData .getLastLimboFreeSnapshotVersion ();
627
+ remoteKeys = this .targetCache .getMatchingKeysForTargetId (targetData .getTargetId ());
628
628
}
629
629
630
630
ImmutableSortedMap <DocumentKey , Document > documents =
@@ -640,7 +640,7 @@ public QueryResult executeQuery(Query query, boolean usePreviousResults) {
640
640
* table.
641
641
*/
642
642
public ImmutableSortedSet <DocumentKey > getRemoteDocumentKeys (int targetId ) {
643
- return queryCache .getMatchingKeysForTargetId (targetId );
643
+ return targetCache .getMatchingKeysForTargetId (targetId );
644
644
}
645
645
646
646
private void applyWriteToRemoteDocuments (MutationBatchResult batchResult ) {
0 commit comments