15
15
package com .google .firebase .firestore .local ;
16
16
17
17
import static com .google .firebase .firestore .util .Assert .fail ;
18
+ import static com .google .firebase .firestore .util .Assert .hardAssert ;
18
19
19
20
import android .database .Cursor ;
20
21
import androidx .annotation .Nullable ;
@@ -60,21 +61,22 @@ public Overlay getOverlay(DocumentKey key) {
60
61
61
62
@ Override
62
63
public Map <DocumentKey , Overlay > getOverlays (SortedSet <DocumentKey > keys ) {
64
+ hardAssert (keys .comparator () == null , "getOverlays() requires natural order" );
63
65
Map <DocumentKey , Overlay > result = new HashMap <>();
64
66
65
67
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 <>();
68
70
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 ();
72
75
}
73
- currentCollectionPath = key .getCollectionPath ();
74
- currentDocumentIds .add (key .getDocumentId ());
76
+ accumulatedDocumentIds .add (key .getDocumentId ());
75
77
}
76
78
77
- processSingleCollection (result , backgroundQueue , currentCollectionPath , currentDocumentIds );
79
+ processSingleCollection (result , backgroundQueue , currentCollection , accumulatedDocumentIds );
78
80
backgroundQueue .drain ();
79
81
return result ;
80
82
}
@@ -85,6 +87,10 @@ private void processSingleCollection(
85
87
BackgroundQueue backgroundQueue ,
86
88
ResourcePath collectionPath ,
87
89
List <Object > documentIds ) {
90
+ if (documentIds .isEmpty ()) {
91
+ return ;
92
+ }
93
+
88
94
SQLitePersistence .LongQuery longQuery =
89
95
new SQLitePersistence .LongQuery (
90
96
db ,
@@ -148,9 +154,9 @@ public Map<DocumentKey, Overlay> getOverlays(ResourcePath collection, int sinceB
148
154
public Map <DocumentKey , Overlay > getOverlays (
149
155
String collectionGroup , int sinceBatchId , int count ) {
150
156
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 ] ;
154
160
155
161
BackgroundQueue backgroundQueue = new BackgroundQueue ();
156
162
db .query (
@@ -202,9 +208,9 @@ private void processOverlaysInBackground(
202
208
Executor executor = row .isLast () ? Executors .DIRECT_EXECUTOR : backgroundQueue ;
203
209
executor .execute (
204
210
() -> {
205
- Overlay document = decodeOverlay (rawMutation , largestBatchId );
211
+ Overlay overlay = decodeOverlay (rawMutation , largestBatchId );
206
212
synchronized (results ) {
207
- results .put (document .getKey (), document );
213
+ results .put (overlay .getKey (), overlay );
208
214
}
209
215
});
210
216
}
0 commit comments