17
17
import static com .google .firebase .firestore .util .Assert .fail ;
18
18
import static com .google .firebase .firestore .util .Assert .hardAssert ;
19
19
20
+ import com .google .android .gms .tasks .Task ;
21
+ import com .google .android .gms .tasks .Tasks ;
20
22
import com .google .firebase .database .collection .ImmutableSortedMap ;
21
23
import com .google .firebase .firestore .core .Query ;
22
24
import com .google .firebase .firestore .model .Document ;
30
32
import java .util .HashMap ;
31
33
import java .util .List ;
32
34
import java .util .Map ;
33
- import java .util .concurrent .ConcurrentHashMap ;
34
- import java .util .concurrent .Executor ;
35
- import java .util .concurrent .Semaphore ;
35
+ import java .util .concurrent .ExecutionException ;
36
36
import javax .annotation .Nullable ;
37
37
38
38
final class SQLiteRemoteDocumentCache implements RemoteDocumentCache {
@@ -122,10 +122,7 @@ public ImmutableSortedMap<DocumentKey, Document> getAllDocumentsMatchingQuery(Qu
122
122
String prefixPath = EncodedPath .encode (prefix );
123
123
String prefixSuccessorPath = EncodedPath .prefixSuccessor (prefixPath );
124
124
125
- Map <DocumentKey , Document > allDocuments = new ConcurrentHashMap <>();
126
-
127
- int [] pendingTaskCount = new int [] {0 };
128
- Semaphore completedTasks = new Semaphore (0 );
125
+ List <Task <Document >> pendingDeserializations = new ArrayList <>();
129
126
130
127
db .query ("SELECT path, contents FROM remote_documents WHERE path >= ? AND path < ?" )
131
128
.binding (prefixPath , prefixSuccessorPath )
@@ -145,35 +142,43 @@ public ImmutableSortedMap<DocumentKey, Document> getAllDocumentsMatchingQuery(Qu
145
142
146
143
byte [] rawContents = row .getBlob (1 );
147
144
148
- ++pendingTaskCount [0 ];
149
-
150
- // Since scheduling background tasks incurs overhead, we only dispatch to a background
151
- // thread if there are still some documents remaining.
152
- Executor deserializationExecutor =
153
- row .isLast () ? Executors .DIRECT_EXECUTOR : Executors .BACKGROUND_EXECUTOR ;
154
- deserializationExecutor .execute (
155
- () -> {
156
- MaybeDocument maybeDoc = decodeMaybeDocument (rawContents );
157
- if (maybeDoc instanceof Document ) {
158
- allDocuments .put (maybeDoc .getKey (), (Document ) maybeDoc );
159
- }
160
- completedTasks .release ();
161
- });
145
+ pendingDeserializations .add (
146
+ Tasks .call (
147
+ // Since scheduling background tasks incurs overhead, we only dispatch to a
148
+ // background
149
+ // thread if there are still some documents remaining.
150
+ row .isLast () ? Executors .DIRECT_EXECUTOR : Executors .BACKGROUND_EXECUTOR ,
151
+ () -> {
152
+ MaybeDocument maybeDoc = decodeMaybeDocument (rawContents );
153
+ if (!(maybeDoc instanceof Document )) {
154
+ return null ;
155
+ }
156
+
157
+ if (!query .matches ((Document ) maybeDoc )) {
158
+ return null ;
159
+ }
160
+
161
+ return (Document ) maybeDoc ;
162
+ }));
162
163
});
163
164
165
+ ImmutableSortedMap <DocumentKey , Document > matchingDocuments =
166
+ ImmutableSortedMap .Builder .emptyMap (DocumentKey .comparator ());
167
+
164
168
try {
165
- completedTasks .acquire (pendingTaskCount [0 ]);
169
+ List <Task <Document >> result = Tasks .await (Tasks .whenAllSuccess (pendingDeserializations ));
170
+ for (Task <Document > task : result ) {
171
+ @ Nullable Document doc = task .getResult ();
172
+ if (doc != null ) {
173
+ matchingDocuments = matchingDocuments .insert (doc .getKey (), doc );
174
+ }
175
+ }
176
+ } catch (ExecutionException e ) {
177
+ throw new RuntimeException (e );
166
178
} catch (InterruptedException e ) {
167
179
Thread .currentThread ().interrupt ();
168
180
}
169
181
170
- ImmutableSortedMap <DocumentKey , Document > matchingDocuments =
171
- ImmutableSortedMap .Builder .emptyMap (DocumentKey .comparator ());
172
- for (Map .Entry <DocumentKey , Document > entry : allDocuments .entrySet ()) {
173
- if (query .matches (entry .getValue ())) {
174
- matchingDocuments = matchingDocuments .insert (entry .getKey (), entry .getValue ());
175
- }
176
- }
177
182
return matchingDocuments ;
178
183
}
179
184
0 commit comments