Skip to content

Commit ff6ae3f

Browse files
author
Greg Soltis
authored
Don't leak orphaned docs when deleting (#32)
Don't leak orphaned docs when deleting in memory lru reference delegate
1 parent f1a2d0a commit ff6ae3f

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.firebase.firestore.core.ListenSequence;
2020
import com.google.firebase.firestore.model.DocumentKey;
21+
import com.google.firebase.firestore.model.MaybeDocument;
2122
import com.google.firebase.firestore.util.Consumer;
2223
import java.util.HashMap;
2324
import java.util.Map;
@@ -99,7 +100,17 @@ public int removeTargets(long upperBound, Set<Integer> activeTargetIds) {
99100

100101
@Override
101102
public int removeOrphanedDocuments(long upperBound) {
102-
return persistence.getRemoteDocumentCache().removeOrphanedDocuments(this, upperBound);
103+
int count = 0;
104+
MemoryRemoteDocumentCache cache = persistence.getRemoteDocumentCache();
105+
for (Map.Entry<DocumentKey, MaybeDocument> entry : cache.getDocuments()) {
106+
DocumentKey key = entry.getKey();
107+
if (!isPinned(key, upperBound)) {
108+
cache.remove(key);
109+
orphanedSequenceNumbers.remove(key);
110+
count++;
111+
}
112+
}
113+
return count;
103114
}
104115

105116
@Override
@@ -139,7 +150,11 @@ private boolean mutationQueuesContainsKey(DocumentKey key) {
139150
return false;
140151
}
141152

142-
public boolean isPinned(DocumentKey key, long upperBound) {
153+
/**
154+
* @return true if there is anything that would keep the given document alive or if the document's
155+
* sequence number is greater than the provided upper bound.
156+
*/
157+
private boolean isPinned(DocumentKey key, long upperBound) {
143158
if (mutationQueuesContainsKey(key)) {
144159
return true;
145160
}

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

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,7 @@ public ImmutableSortedMap<DocumentKey, Document> getAllDocumentsMatchingQuery(Qu
8383
return result;
8484
}
8585

86-
/** Remove any documents that the delegate reports as not pinned at the given upper bound. */
87-
int removeOrphanedDocuments(MemoryLruReferenceDelegate delegate, long upperBound) {
88-
int count = 0;
89-
ImmutableSortedMap<DocumentKey, MaybeDocument> updated = docs;
90-
for (Map.Entry<DocumentKey, MaybeDocument> entry : docs) {
91-
if (!delegate.isPinned(entry.getKey(), upperBound)) {
92-
updated = updated.remove(entry.getKey());
93-
count++;
94-
}
95-
}
96-
docs = updated;
97-
return count;
86+
ImmutableSortedMap<DocumentKey, MaybeDocument> getDocuments() {
87+
return docs;
9888
}
9989
}

0 commit comments

Comments
 (0)