Skip to content

Commit 8a92291

Browse files
authored
A simple refactor on LocalStore.java (#4758)
* A simple refactor on LocalStore.java * fix
1 parent 7a0f906 commit 8a92291

File tree

1 file changed

+12
-12
lines changed
  • firebase-firestore/src/main/java/com/google/firebase/firestore/local

1 file changed

+12
-12
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/local/LocalStore.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ private DocumentChangeResult populateDocumentChanges(
555555
* have to be too frequent.
556556
*/
557557
private static boolean shouldPersistTargetData(
558-
TargetData oldTargetData, TargetData newTargetData, TargetChange change) {
558+
TargetData oldTargetData, TargetData newTargetData, @Nullable TargetChange change) {
559559
// Always persist query data if we don't already have a resume token.
560560
if (oldTargetData.getResumeToken().isEmpty()) return true;
561561

@@ -568,10 +568,20 @@ private static boolean shouldPersistTargetData(
568568
long timeDelta = newSeconds - oldSeconds;
569569
if (timeDelta >= RESUME_TOKEN_MAX_AGE_SECONDS) return true;
570570

571+
// Update the target cache if sufficient time has passed since the last
572+
// LastLimboFreeSnapshotVersion
573+
long newLimboFreeSeconds =
574+
newTargetData.getLastLimboFreeSnapshotVersion().getTimestamp().getSeconds();
575+
long oldLimboFreeSeconds =
576+
oldTargetData.getLastLimboFreeSnapshotVersion().getTimestamp().getSeconds();
577+
long limboFreeTimeDelta = newLimboFreeSeconds - oldLimboFreeSeconds;
578+
if (limboFreeTimeDelta >= RESUME_TOKEN_MAX_AGE_SECONDS) return true;
579+
571580
// Otherwise if the only thing that has changed about a target is its resume token it's not
572581
// worth persisting. Note that the RemoteStore keeps an in-memory view of the currently active
573582
// targets which includes the current resume token, so stream failure or user changes will still
574583
// use an up-to-date resume token regardless of what we do here.
584+
if (change == null) return false;
575585
int changes =
576586
change.getAddedDocuments().size()
577587
+ change.getModifiedDocuments().size()
@@ -607,17 +617,7 @@ public void notifyLocalViewChanges(List<LocalViewChanges> viewChanges) {
607617
targetData.withLastLimboFreeSnapshotVersion(lastLimboFreeSnapshotVersion);
608618
queryDataByTarget.put(targetId, updatedTargetData);
609619

610-
long newSeconds =
611-
updatedTargetData.getLastLimboFreeSnapshotVersion().getTimestamp().getSeconds();
612-
613-
long oldSeconds =
614-
targetData.getLastLimboFreeSnapshotVersion().getTimestamp().getSeconds();
615-
616-
long timeDelta = newSeconds - oldSeconds;
617-
618-
// Update the target cache if sufficient time has passed since the last
619-
// LastLimboFreeSnapshotVersion
620-
if (timeDelta >= RESUME_TOKEN_MAX_AGE_SECONDS) {
620+
if (shouldPersistTargetData(targetData, updatedTargetData, /*change*/ null)) {
621621
targetCache.updateTargetData(updatedTargetData);
622622
}
623623
}

0 commit comments

Comments
 (0)