Skip to content

Commit 9105f02

Browse files
Remove local references from reference delegate
1 parent b3871fe commit 9105f02

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,13 @@ public void releaseQuery(Query query) {
558558
queryCache.updateQueryData(queryData);
559559
}
560560

561-
localViewReferences.removeReferencesForId(queryData.getTargetId());
561+
// We remove both locally edited documents and documents that are associated via the query
562+
// cache.
563+
ImmutableSortedSet<DocumentKey> removedReferences =
564+
localViewReferences.removeReferencesForId(queryData.getTargetId());
565+
for (DocumentKey key : removedReferences) {
566+
persistence.getReferenceDelegate().removeReference(key);
567+
}
562568
persistence.getReferenceDelegate().removeTarget(queryData);
563569
targetIds.remove(queryData.getTargetId());
564570

@@ -622,7 +628,7 @@ private Set<DocumentKey> releaseBatchResults(List<MutationBatchResult> batchResu
622628
ArrayList<MutationBatch> batches = new ArrayList<>(batchResults.size());
623629
// TODO: Call queryEngine.handleDocumentChange() as appropriate.
624630
for (MutationBatchResult batchResult : batchResults) {
625-
applyBatchResult(batchResult);
631+
applyWriteToRemoteDocuments(batchResult);
626632
batches.add(batchResult.getBatch());
627633
}
628634

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

650-
private void applyBatchResult(MutationBatchResult batchResult) {
656+
private void applyWriteToRemoteDocuments(MutationBatchResult batchResult) {
651657
MutationBatch batch = batchResult.getBatch();
652658
Set<DocumentKey> docKeys = batch.getKeys();
653659
for (DocumentKey docKey : docKeys) {

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,27 @@ public void removeReferences(ImmutableSortedSet<DocumentKey> keys, int targetOrB
7777
}
7878
}
7979

80-
/** Clears all references with a given ID. Calls removeReference() for each key removed. */
81-
public void removeReferencesForId(int targetId) {
80+
/**
81+
* Clears all references with a given ID. Calls removeReference() for each key removed.
82+
*
83+
* @return The keys of the documents that were removed.
84+
*/
85+
public ImmutableSortedSet<DocumentKey> removeReferencesForId(int targetId) {
8286
DocumentKey emptyKey = DocumentKey.empty();
8387
DocumentReference startRef = new DocumentReference(emptyKey, targetId);
8488
Iterator<DocumentReference> it = referencesByTarget.iteratorFrom(startRef);
89+
ImmutableSortedSet<DocumentKey> keys = DocumentKey.emptyKeySet();
8590
while (it.hasNext()) {
8691
DocumentReference ref = it.next();
8792
if (ref.getId() == targetId) {
93+
keys = keys.insert(ref.getKey());
8894
removeReference(ref);
8995
} else {
9096
break;
9197
}
9298
}
99+
100+
return keys;
93101
}
94102

95103
/** Clears all references for all IDs. */

0 commit comments

Comments
 (0)