Skip to content

Commit fab9711

Browse files
Use the Tasks API
1 parent 4da49cf commit fab9711

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

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

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import static com.google.firebase.firestore.util.Assert.fail;
1818
import static com.google.firebase.firestore.util.Assert.hardAssert;
1919

20+
import com.google.android.gms.tasks.Task;
21+
import com.google.android.gms.tasks.Tasks;
2022
import com.google.firebase.database.collection.ImmutableSortedMap;
2123
import com.google.firebase.firestore.core.Query;
2224
import com.google.firebase.firestore.model.Document;
@@ -30,9 +32,7 @@
3032
import java.util.HashMap;
3133
import java.util.List;
3234
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;
3636
import javax.annotation.Nullable;
3737

3838
final class SQLiteRemoteDocumentCache implements RemoteDocumentCache {
@@ -122,10 +122,7 @@ public ImmutableSortedMap<DocumentKey, Document> getAllDocumentsMatchingQuery(Qu
122122
String prefixPath = EncodedPath.encode(prefix);
123123
String prefixSuccessorPath = EncodedPath.prefixSuccessor(prefixPath);
124124

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<>();
129126

130127
db.query("SELECT path, contents FROM remote_documents WHERE path >= ? AND path < ?")
131128
.binding(prefixPath, prefixSuccessorPath)
@@ -145,35 +142,43 @@ public ImmutableSortedMap<DocumentKey, Document> getAllDocumentsMatchingQuery(Qu
145142

146143
byte[] rawContents = row.getBlob(1);
147144

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+
}));
162163
});
163164

165+
ImmutableSortedMap<DocumentKey, Document> matchingDocuments =
166+
ImmutableSortedMap.Builder.emptyMap(DocumentKey.comparator());
167+
164168
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);
166178
} catch (InterruptedException e) {
167179
Thread.currentThread().interrupt();
168180
}
169181

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-
}
177182
return matchingDocuments;
178183
}
179184

0 commit comments

Comments
 (0)