Skip to content

Commit acfb058

Browse files
Use RemoteDocumentCache.getAll()
1 parent bf93a38 commit acfb058

8 files changed

+76
-153
lines changed

packages/firestore/src/local/indexeddb_remote_document_cache.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { isCollectionGroupQuery, Query, queryMatches } from '../core/query';
1918
import { SnapshotVersion } from '../core/snapshot_version';
2019
import {
2120
DocumentKeySet,
@@ -246,30 +245,26 @@ class IndexedDbRemoteDocumentCacheImpl implements IndexedDbRemoteDocumentCache {
246245
});
247246
}
248247

249-
getDocumentsMatchingQuery(
248+
getAll(
250249
transaction: PersistenceTransaction,
251-
query: Query,
250+
collection: ResourcePath,
252251
sinceReadTime: SnapshotVersion
253252
): PersistencePromise<MutableDocumentMap> {
254-
debugAssert(
255-
!isCollectionGroupQuery(query),
256-
'CollectionGroup queries should be handled in LocalDocumentsView'
257-
);
258253
let results = mutableDocumentMap();
259254

260-
const immediateChildrenPathLength = query.path.length + 1;
255+
const immediateChildrenPathLength = collection.length + 1;
261256

262257
const iterationOptions: IterateOptions = {};
263258
if (sinceReadTime.isEqual(SnapshotVersion.min())) {
264259
// Documents are ordered by key, so we can use a prefix scan to narrow
265260
// down the documents we need to match the query against.
266-
const startKey = query.path.toArray();
261+
const startKey = collection.toArray();
267262
iterationOptions.range = IDBKeyRange.lowerBound(startKey);
268263
} else {
269264
// Execute an index-free query and filter by read time. This is safe
270265
// since all document changes to queries that have a
271266
// lastLimboFreeSnapshotVersion (`sinceReadTime`) have a read time set.
272-
const collectionKey = query.path.toArray();
267+
const collectionKey = collection.toArray();
273268
const readTimeKey = toDbTimestampKey(sinceReadTime);
274269
iterationOptions.range = IDBKeyRange.lowerBound(
275270
[collectionKey, readTimeKey],
@@ -293,10 +288,10 @@ class IndexedDbRemoteDocumentCacheImpl implements IndexedDbRemoteDocumentCache {
293288
DocumentKey.fromSegments(key),
294289
dbRemoteDoc
295290
);
296-
if (!query.path.isPrefixOf(document.key.path)) {
297-
control.done();
298-
} else if (queryMatches(query, document)) {
291+
if (collection.isPrefixOf(document.key.path)) {
299292
results = results.insert(document.key, document);
293+
} else {
294+
control.done();
300295
}
301296
})
302297
.next(() => results);

packages/firestore/src/local/local_documents_view.ts

Lines changed: 16 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,13 @@ import {
2525
import { SnapshotVersion } from '../core/snapshot_version';
2626
import {
2727
DocumentKeySet,
28-
documentKeySet,
2928
DocumentMap,
3029
documentMap,
3130
MutableDocumentMap
3231
} from '../model/collections';
3332
import { Document, MutableDocument } from '../model/document';
3433
import { DocumentKey } from '../model/document_key';
35-
import { mutationApplyToLocalView, PatchMutation } from '../model/mutation';
34+
import { mutationApplyToLocalView } from '../model/mutation';
3635
import { MutationBatch } from '../model/mutation_batch';
3736
import { ResourcePath } from '../model/path';
3837
import { debugAssert } from '../util/assert';
@@ -217,51 +216,31 @@ export class LocalDocumentsView {
217216
): PersistencePromise<DocumentMap> {
218217
// Query the remote documents and overlay mutations.
219218
let results: MutableDocumentMap;
220-
let mutationBatches: MutationBatch[];
221219
return this.remoteDocumentCache
222-
.getDocumentsMatchingQuery(transaction, query, sinceReadTime)
220+
.getAll(transaction, query.path, sinceReadTime)
223221
.next(queryResults => {
224222
results = queryResults;
225223
return this.mutationQueue.getAllMutationBatchesAffectingQuery(
226224
transaction,
227225
query
228226
);
229227
})
230-
.next(matchingMutationBatches => {
231-
mutationBatches = matchingMutationBatches;
232-
// It is possible that a PatchMutation can make a document match a query, even if
233-
// the version in the RemoteDocumentCache is not a match yet (waiting for server
234-
// to ack). To handle this, we find all document keys affected by the PatchMutations
235-
// that are not in `result` yet, and back fill them via `remoteDocumentCache.getEntries`,
236-
// otherwise those `PatchMutations` will be ignored because no base document can be found,
237-
// and lead to missing result for the query.
238-
return this.addMissingBaseDocuments(
239-
transaction,
240-
mutationBatches,
241-
results
242-
).next(mergedDocuments => {
243-
results = mergedDocuments;
244-
245-
for (const batch of mutationBatches) {
246-
for (const mutation of batch.mutations) {
247-
const key = mutation.key;
248-
let document = results.get(key);
249-
if (document == null) {
250-
// Create invalid document to apply mutations on top of
251-
document = MutableDocument.newInvalidDocument(key);
252-
results = results.insert(key, document);
253-
}
254-
mutationApplyToLocalView(
255-
mutation,
256-
document,
257-
batch.localWriteTime
258-
);
259-
if (!document.isFoundDocument()) {
260-
results = results.remove(key);
261-
}
228+
.next(mutationBatches => {
229+
for (const batch of mutationBatches) {
230+
for (const mutation of batch.mutations) {
231+
const key = mutation.key;
232+
let document = results.get(key);
233+
if (document == null) {
234+
// Create invalid document to apply mutations on top of
235+
document = MutableDocument.newInvalidDocument(key);
236+
results = results.insert(key, document);
237+
}
238+
mutationApplyToLocalView(mutation, document, batch.localWriteTime);
239+
if (!document.isFoundDocument()) {
240+
results = results.remove(key);
262241
}
263242
}
264-
});
243+
}
265244
})
266245
.next(() => {
267246
// Finally, filter out any documents that don't actually match
@@ -275,35 +254,4 @@ export class LocalDocumentsView {
275254
return results as DocumentMap;
276255
});
277256
}
278-
279-
private addMissingBaseDocuments(
280-
transaction: PersistenceTransaction,
281-
matchingMutationBatches: MutationBatch[],
282-
existingDocuments: MutableDocumentMap
283-
): PersistencePromise<MutableDocumentMap> {
284-
let missingBaseDocEntriesForPatching = documentKeySet();
285-
for (const batch of matchingMutationBatches) {
286-
for (const mutation of batch.mutations) {
287-
if (
288-
mutation instanceof PatchMutation &&
289-
existingDocuments.get(mutation.key) === null
290-
) {
291-
missingBaseDocEntriesForPatching =
292-
missingBaseDocEntriesForPatching.add(mutation.key);
293-
}
294-
}
295-
}
296-
297-
let mergedDocuments = existingDocuments;
298-
return this.remoteDocumentCache
299-
.getEntries(transaction, missingBaseDocEntriesForPatching)
300-
.next(missingBaseDocs => {
301-
missingBaseDocs.forEach((key, doc) => {
302-
if (doc.isFoundDocument()) {
303-
mergedDocuments = mergedDocuments.insert(key, doc);
304-
}
305-
});
306-
return mergedDocuments;
307-
});
308-
}
309257
}

packages/firestore/src/local/memory_remote_document_cache.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { isCollectionGroupQuery, Query, queryMatches } from '../core/query';
1918
import { SnapshotVersion } from '../core/snapshot_version';
2019
import {
2120
DocumentKeySet,
@@ -24,6 +23,7 @@ import {
2423
} from '../model/collections';
2524
import { Document, MutableDocument } from '../model/document';
2625
import { DocumentKey } from '../model/document_key';
26+
import { ResourcePath } from '../model/path';
2727
import { debugAssert } from '../util/assert';
2828
import { SortedMap } from '../util/sorted_map';
2929

@@ -152,33 +152,30 @@ class MemoryRemoteDocumentCacheImpl implements MemoryRemoteDocumentCache {
152152
return PersistencePromise.resolve(results);
153153
}
154154

155-
getDocumentsMatchingQuery(
155+
getAll(
156156
transaction: PersistenceTransaction,
157-
query: Query,
157+
collectionPath: ResourcePath,
158158
sinceReadTime: SnapshotVersion
159159
): PersistencePromise<MutableDocumentMap> {
160-
debugAssert(
161-
!isCollectionGroupQuery(query),
162-
'CollectionGroup queries should be handled in LocalDocumentsView'
163-
);
164160
let results = mutableDocumentMap();
165161

166162
// Documents are ordered by key, so we can use a prefix scan to narrow down
167163
// the documents we need to match the query against.
168-
const prefix = new DocumentKey(query.path.child(''));
164+
const prefix = new DocumentKey(collectionPath.child(''));
169165
const iterator = this.docs.getIteratorFrom(prefix);
170166
while (iterator.hasNext()) {
171167
const {
172168
key,
173169
value: { document }
174170
} = iterator.getNext();
175-
if (!query.path.isPrefixOf(key.path)) {
171+
if (!collectionPath.isPrefixOf(key.path)) {
176172
break;
177173
}
178-
if (document.readTime.compareTo(sinceReadTime) <= 0) {
174+
if (key.path.length > collectionPath.length + 1) {
175+
// Exclude entries from subcollections.
179176
continue;
180177
}
181-
if (!queryMatches(query, document)) {
178+
if (document.readTime.compareTo(sinceReadTime) <= 0) {
182179
continue;
183180
}
184181
results = results.insert(document.key, document.mutableCopy());

packages/firestore/src/local/remote_document_cache.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { Query } from '../core/query';
1918
import { SnapshotVersion } from '../core/snapshot_version';
2019
import { DocumentKeySet, MutableDocumentMap } from '../model/collections';
2120
import { MutableDocument } from '../model/document';
2221
import { DocumentKey } from '../model/document_key';
22+
import { ResourcePath } from '../model/path';
2323

2424
import { PersistencePromise } from './persistence_promise';
2525
import { PersistenceTransaction } from './persistence_transaction';
@@ -58,21 +58,16 @@ export interface RemoteDocumentCache {
5858
): PersistencePromise<MutableDocumentMap>;
5959

6060
/**
61-
* Executes a query against the cached Document entries.
61+
* Returns the documents from the provided collection.
6262
*
63-
* Implementations may return extra documents if convenient. The results
64-
* should be re-filtered by the consumer before presenting them to the user.
65-
*
66-
* Cached NoDocument entries have no bearing on query results.
67-
*
68-
* @param query - The query to match documents against.
63+
* @param collection - The collection to read.
6964
* @param sinceReadTime - If not set to SnapshotVersion.min(), return only
7065
* documents that have been read since this snapshot version (exclusive).
7166
* @returns The set of matching documents.
7267
*/
73-
getDocumentsMatchingQuery(
68+
getAll(
7469
transaction: PersistenceTransaction,
75-
query: Query,
70+
collection: ResourcePath,
7671
sinceReadTime: SnapshotVersion
7772
): PersistencePromise<MutableDocumentMap>;
7873

packages/firestore/test/unit/local/counting_query_engine.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class CountingQueryEngine extends QueryEngine {
3535
* `getAllMutationBatchesAffectingQuery()` API (since the last call to
3636
* `resetCounts()`)
3737
*/
38-
mutationsReadByQuery = 0;
38+
mutationsReadByCollection = 0;
3939

4040
/**
4141
* The number of mutations returned by the MutationQueue's
@@ -47,9 +47,9 @@ export class CountingQueryEngine extends QueryEngine {
4747

4848
/**
4949
* The number of documents returned by the RemoteDocumentCache's
50-
* `getDocumentsMatchingQuery()` API (since the last call to `resetCounts()`)
50+
* `getAll()` API (since the last call to `resetCounts()`)
5151
*/
52-
documentsReadByQuery = 0;
52+
documentsReadByCollection = 0;
5353

5454
/**
5555
* The number of documents returned by the RemoteDocumentCache's `getEntry()`
@@ -58,9 +58,9 @@ export class CountingQueryEngine extends QueryEngine {
5858
documentsReadByKey = 0;
5959

6060
resetCounts(): void {
61-
this.mutationsReadByQuery = 0;
61+
this.mutationsReadByCollection = 0;
6262
this.mutationsReadByKey = 0;
63-
this.documentsReadByQuery = 0;
63+
this.documentsReadByCollection = 0;
6464
this.documentsReadByKey = 0;
6565
}
6666

@@ -92,11 +92,11 @@ export class CountingQueryEngine extends QueryEngine {
9292
subject: RemoteDocumentCache
9393
): RemoteDocumentCache {
9494
return {
95-
getDocumentsMatchingQuery: (transaction, query, sinceReadTime) => {
95+
getAll: (transaction, collectionGroup, sinceReadTime) => {
9696
return subject
97-
.getDocumentsMatchingQuery(transaction, query, sinceReadTime)
97+
.getAll(transaction, collectionGroup, sinceReadTime)
9898
.next(result => {
99-
this.documentsReadByQuery += result.size;
99+
this.documentsReadByCollection += result.size;
100100
return result;
101101
});
102102
},
@@ -150,7 +150,7 @@ export class CountingQueryEngine extends QueryEngine {
150150
return subject
151151
.getAllMutationBatchesAffectingQuery(transaction, query)
152152
.next(result => {
153-
this.mutationsReadByQuery += result.length;
153+
this.mutationsReadByCollection += result.length;
154154
return result;
155155
});
156156
},

0 commit comments

Comments
 (0)