Skip to content

Commit 8ac01c1

Browse files
Review
1 parent bfba080 commit 8ac01c1

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package com.google.firebase.firestore.local;
1616

1717
import static com.google.firebase.firestore.util.Assert.fail;
18+
import static com.google.firebase.firestore.util.Assert.hardAssert;
1819

1920
import android.database.Cursor;
2021
import androidx.annotation.Nullable;
@@ -60,21 +61,22 @@ public Overlay getOverlay(DocumentKey key) {
6061

6162
@Override
6263
public Map<DocumentKey, Overlay> getOverlays(SortedSet<DocumentKey> keys) {
64+
hardAssert(keys.comparator() == null, "getOverlays() requires natural order");
6365
Map<DocumentKey, Overlay> result = new HashMap<>();
6466

6567
BackgroundQueue backgroundQueue = new BackgroundQueue();
66-
ResourcePath currentCollectionPath = ResourcePath.EMPTY;
67-
List<Object> currentDocumentIds = new ArrayList<>();
68+
ResourcePath currentCollection = ResourcePath.EMPTY;
69+
List<Object> accumulatedDocumentIds = new ArrayList<>();
6870
for (DocumentKey key : keys) {
69-
if (!currentCollectionPath.equals(key.getCollectionPath())) {
70-
processSingleCollection(result, backgroundQueue, currentCollectionPath, currentDocumentIds);
71-
currentDocumentIds = new ArrayList<>();
71+
if (!currentCollection.equals(key.getCollectionPath())) {
72+
processSingleCollection(result, backgroundQueue, currentCollection, accumulatedDocumentIds);
73+
currentCollection = key.getCollectionPath();
74+
accumulatedDocumentIds.clear();
7275
}
73-
currentCollectionPath = key.getCollectionPath();
74-
currentDocumentIds.add(key.getDocumentId());
76+
accumulatedDocumentIds.add(key.getDocumentId());
7577
}
7678

77-
processSingleCollection(result, backgroundQueue, currentCollectionPath, currentDocumentIds);
79+
processSingleCollection(result, backgroundQueue, currentCollection, accumulatedDocumentIds);
7880
backgroundQueue.drain();
7981
return result;
8082
}
@@ -85,6 +87,10 @@ private void processSingleCollection(
8587
BackgroundQueue backgroundQueue,
8688
ResourcePath collectionPath,
8789
List<Object> documentIds) {
90+
if (documentIds.isEmpty()) {
91+
return;
92+
}
93+
8894
SQLitePersistence.LongQuery longQuery =
8995
new SQLitePersistence.LongQuery(
9096
db,
@@ -148,9 +154,9 @@ public Map<DocumentKey, Overlay> getOverlays(ResourcePath collection, int sinceB
148154
public Map<DocumentKey, Overlay> getOverlays(
149155
String collectionGroup, int sinceBatchId, int count) {
150156
Map<DocumentKey, Overlay> result = new HashMap<>();
151-
String[] lastCollectionPath = new String[] {null};
152-
String[] lastDocumentPath = new String[] {null};
153-
int[] lastLargestBatchId = new int[] {0};
157+
String[] lastCollectionPath = new String[1];
158+
String[] lastDocumentPath = new String[1];
159+
int[] lastLargestBatchId = new int[1];
154160

155161
BackgroundQueue backgroundQueue = new BackgroundQueue();
156162
db.query(
@@ -202,9 +208,9 @@ private void processOverlaysInBackground(
202208
Executor executor = row.isLast() ? Executors.DIRECT_EXECUTOR : backgroundQueue;
203209
executor.execute(
204210
() -> {
205-
Overlay document = decodeOverlay(rawMutation, largestBatchId);
211+
Overlay overlay = decodeOverlay(rawMutation, largestBatchId);
206212
synchronized (results) {
207-
results.put(document.getKey(), document);
213+
results.put(overlay.getKey(), overlay);
208214
}
209215
});
210216
}

firebase-firestore/src/test/java/com/google/firebase/firestore/local/DocumentOverlayCacheTestCase.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ public void testSkipsNonExistingOverlayInBatchLookup() {
9797
assertTrue(overlays.isEmpty());
9898
}
9999

100+
@Test
101+
public void testSupportsEmptyBatchInBatchLookup() {
102+
Map<DocumentKey, Overlay> overlays = cache.getOverlays(new TreeSet<>());
103+
assertTrue(overlays.isEmpty());
104+
}
105+
100106
@Test
101107
public void testCanReadSavedOverlay() {
102108
Mutation m = patchMutation("coll/doc1", map("foo", "bar"));

0 commit comments

Comments
 (0)