Skip to content

Don't leak orphaned docs when deleting #32

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
Sep 20, 2018
Merged
Show file tree
Hide file tree
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 @@ -18,6 +18,7 @@

import com.google.firebase.firestore.core.ListenSequence;
import com.google.firebase.firestore.model.DocumentKey;
import com.google.firebase.firestore.model.MaybeDocument;
import com.google.firebase.firestore.util.Consumer;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -99,7 +100,17 @@ public int removeTargets(long upperBound, Set<Integer> activeTargetIds) {

@Override
public int removeOrphanedDocuments(long upperBound) {
return persistence.getRemoteDocumentCache().removeOrphanedDocuments(this, upperBound);
int count = 0;
MemoryRemoteDocumentCache cache = persistence.getRemoteDocumentCache();
for (Map.Entry<DocumentKey, MaybeDocument> entry : cache.getDocuments()) {
DocumentKey key = entry.getKey();
if (!isPinned(key, upperBound)) {
cache.remove(key);
orphanedSequenceNumbers.remove(key);
count++;
}
}
return count;
}

@Override
Expand Down Expand Up @@ -139,7 +150,11 @@ private boolean mutationQueuesContainsKey(DocumentKey key) {
return false;
}

public boolean isPinned(DocumentKey key, long upperBound) {
/**
* @return true if there is anything that would keep the given document alive or if the document's
* sequence number is greater than the provided upper bound.
*/
private boolean isPinned(DocumentKey key, long upperBound) {
if (mutationQueuesContainsKey(key)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,7 @@ public ImmutableSortedMap<DocumentKey, Document> getAllDocumentsMatchingQuery(Qu
return result;
}

/** Remove any documents that the delegate reports as not pinned at the given upper bound. */
int removeOrphanedDocuments(MemoryLruReferenceDelegate delegate, long upperBound) {
int count = 0;
ImmutableSortedMap<DocumentKey, MaybeDocument> updated = docs;
for (Map.Entry<DocumentKey, MaybeDocument> entry : docs) {
if (!delegate.isPinned(entry.getKey(), upperBound)) {
updated = updated.remove(entry.getKey());
count++;
}
}
docs = updated;
return count;
ImmutableSortedMap<DocumentKey, MaybeDocument> getDocuments() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Rename to getAllDocuments() to match getAllDocumentsMatchingQuery().

Feel free to disagree :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The other one should change to getDocumentsMatchingQuery().

Copy link
Contributor

Choose a reason for hiding this comment

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

That's fine by me. Can you make this change (if you want, in a follow up?). getDocumentsMatchingQuery() is already the name we use on Web.

return docs;
}
}