Skip to content

A simple refactor on LocalStore.java #4758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ private DocumentChangeResult populateDocumentChanges(
* have to be too frequent.
*/
private static boolean shouldPersistTargetData(
TargetData oldTargetData, TargetData newTargetData, TargetChange change) {
TargetData oldTargetData, TargetData newTargetData, @Nullable TargetChange change) {
// Always persist query data if we don't already have a resume token.
if (oldTargetData.getResumeToken().isEmpty()) return true;

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

// Update the target cache if sufficient time has passed since the last
// LastLimboFreeSnapshotVersion
long newLimboFreeSeconds =
newTargetData.getLastLimboFreeSnapshotVersion().getTimestamp().getSeconds();
long oldLimboFreeSeconds =
oldTargetData.getLastLimboFreeSnapshotVersion().getTimestamp().getSeconds();
long limboFreeTimeDelta = newLimboFreeSeconds - oldLimboFreeSeconds;
if (limboFreeTimeDelta >= RESUME_TOKEN_MAX_AGE_SECONDS) return true;

// Otherwise if the only thing that has changed about a target is its resume token it's not
// worth persisting. Note that the RemoteStore keeps an in-memory view of the currently active
// targets which includes the current resume token, so stream failure or user changes will still
// use an up-to-date resume token regardless of what we do here.
if (change == null) return false;
int changes =
change.getAddedDocuments().size()
+ change.getModifiedDocuments().size()
Expand Down Expand Up @@ -607,17 +617,7 @@ public void notifyLocalViewChanges(List<LocalViewChanges> viewChanges) {
targetData.withLastLimboFreeSnapshotVersion(lastLimboFreeSnapshotVersion);
queryDataByTarget.put(targetId, updatedTargetData);

long newSeconds =
updatedTargetData.getLastLimboFreeSnapshotVersion().getTimestamp().getSeconds();

long oldSeconds =
targetData.getLastLimboFreeSnapshotVersion().getTimestamp().getSeconds();

long timeDelta = newSeconds - oldSeconds;

// Update the target cache if sufficient time has passed since the last
// LastLimboFreeSnapshotVersion
if (timeDelta >= RESUME_TOKEN_MAX_AGE_SECONDS) {
if (shouldPersistTargetData(targetData, updatedTargetData, /*change*/ null)) {
targetCache.updateTargetData(updatedTargetData);
}
}
Expand Down