Skip to content

Remove local references from reference delegate #42

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 3 commits into from
Sep 24, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -558,7 +558,13 @@ public void releaseQuery(Query query) {
queryCache.updateQueryData(queryData);
}

localViewReferences.removeReferencesForId(queryData.getTargetId());
// We remove both locally edited documents and documents that are associated via the query
// cache.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add more to this comment. Something like: "We may have determined that a document is associated with a target, but if that hasn't been confirmed by the backend, the query cache won't know to remove the reference for those documents. Make sure we remove them here."

Or some variation on that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the comment to:

// References for documents sent via Watch are automatically removed when we delete a
// query's target data from the reference delegate. Since this does not remove references
// for locally mutated documents, we have to remove the target associations for these
// documents manually.

ImmutableSortedSet<DocumentKey> removedReferences =
localViewReferences.removeReferencesForId(queryData.getTargetId());
for (DocumentKey key : removedReferences) {
persistence.getReferenceDelegate().removeReference(key);
}
persistence.getReferenceDelegate().removeTarget(queryData);
targetIds.remove(queryData.getTargetId());

Expand Down Expand Up @@ -622,7 +628,7 @@ private Set<DocumentKey> releaseBatchResults(List<MutationBatchResult> batchResu
ArrayList<MutationBatch> batches = new ArrayList<>(batchResults.size());
// TODO: Call queryEngine.handleDocumentChange() as appropriate.
for (MutationBatchResult batchResult : batchResults) {
applyBatchResult(batchResult);
applyWriteToRemoteDocuments(batchResult);
batches.add(batchResult.getBatch());
}

Expand All @@ -647,7 +653,7 @@ private Set<DocumentKey> removeMutationBatches(List<MutationBatch> batches) {
return affectedDocs;
}

private void applyBatchResult(MutationBatchResult batchResult) {
private void applyWriteToRemoteDocuments(MutationBatchResult batchResult) {
MutationBatch batch = batchResult.getBatch();
Set<DocumentKey> docKeys = batch.getKeys();
for (DocumentKey docKey : docKeys) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,27 @@ public void removeReferences(ImmutableSortedSet<DocumentKey> keys, int targetOrB
}
}

/** Clears all references with a given ID. Calls removeReference() for each key removed. */
public void removeReferencesForId(int targetId) {
/**
* Clears all references with a given ID. Calls removeReference() for each key removed.
*
* @return The keys of the documents that were removed.
*/
public ImmutableSortedSet<DocumentKey> removeReferencesForId(int targetId) {
DocumentKey emptyKey = DocumentKey.empty();
DocumentReference startRef = new DocumentReference(emptyKey, targetId);
Iterator<DocumentReference> it = referencesByTarget.iteratorFrom(startRef);
ImmutableSortedSet<DocumentKey> keys = DocumentKey.emptyKeySet();
while (it.hasNext()) {
DocumentReference ref = it.next();
if (ref.getId() == targetId) {
keys = keys.insert(ref.getKey());
removeReference(ref);
} else {
break;
}
}

return keys;
}

/** Clears all references for all IDs. */
Expand Down