Skip to content

Commit 4c726ef

Browse files
committed
addressing comments #2
1 parent 96aed4d commit 4c726ef

File tree

2 files changed

+54
-56
lines changed

2 files changed

+54
-56
lines changed

packages/firestore/src/local/local_documents_view.ts

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ import { Query } from '../core/query';
1919
import { SnapshotVersion } from '../core/snapshot_version';
2020
import {
2121
DocumentKeySet,
22+
documentKeySet,
2223
DocumentMap,
2324
documentMap,
2425
MaybeDocumentMap,
2526
maybeDocumentMap,
2627
NullableMaybeDocumentMap,
2728
nullableMaybeDocumentMap,
28-
documentKeySet
2929
} from '../model/collections';
3030
import { Document, MaybeDocument, NoDocument } from '../model/document';
3131
import { DocumentKey } from '../model/document_key';
@@ -35,10 +35,10 @@ import { ResourcePath } from '../model/path';
3535
import { assert } from '../util/assert';
3636
import { IndexManager } from './index_manager';
3737
import { MutationQueue } from './mutation_queue';
38+
import { PatchMutation } from '../model/mutation';
3839
import { PersistenceTransaction } from './persistence';
3940
import { PersistencePromise } from './persistence_promise';
4041
import { RemoteDocumentCache } from './remote_document_cache';
41-
import { PatchMutation } from '../model/mutation';
4242

4343
/**
4444
* A readonly view of the local state of all documents we're tracking (i.e. we
@@ -51,7 +51,7 @@ export class LocalDocumentsView {
5151
private remoteDocumentCache: RemoteDocumentCache,
5252
private mutationQueue: MutationQueue,
5353
private indexManager: IndexManager
54-
) {}
54+
) { }
5555

5656
/**
5757
* Get the local view of the document identified by `key`.
@@ -228,57 +228,31 @@ export class LocalDocumentsView {
228228
// that are not in `result` yet, and back fill them via `remoteDocumentCache.getEntries`,
229229
// otherwise those `PatchMutations` will be ignored because no base document can be found,
230230
// and lead to missing result for the query.
231-
let missingBaseDocEntriesForPatching = documentKeySet();
232-
for (const batch of matchingMutationBatches) {
233-
for (const mutation of batch.mutations) {
234-
// Only process documents belonging to the collection.
235-
if (!query.path.isImmediateParentOf(mutation.key.path)) {
236-
continue;
237-
}
238-
if (
239-
mutation instanceof PatchMutation &&
240-
results.get(mutation.key) === null
241-
) {
242-
missingBaseDocEntriesForPatching = missingBaseDocEntriesForPatching.add(
243-
mutation.key
244-
);
245-
}
246-
}
247-
}
248-
return this.remoteDocumentCache.getEntries(
249-
transaction,
250-
missingBaseDocEntriesForPatching
251-
);
252-
})
253-
.next(docsWithPendingPatches => {
254-
docsWithPendingPatches.forEach((key, doc) => {
255-
if (doc !== null && doc instanceof Document) {
256-
results = results.insert(key, doc);
257-
}
258-
});
259-
for (const batch of mutationBatches) {
260-
for (const mutation of batch.mutations) {
261-
const key = mutation.key;
262-
// Only process documents belonging to the collection.
263-
if (!query.path.isImmediateParentOf(key.path)) {
264-
continue;
265-
}
266-
267-
const baseDoc = results.get(key);
268-
const mutatedDoc = mutation.applyToLocalView(
269-
baseDoc,
270-
baseDoc,
271-
batch.localWriteTime
272-
);
273-
if (mutatedDoc instanceof Document) {
274-
results = results.insert(key, mutatedDoc);
275-
} else {
276-
results = results.remove(key);
231+
return this.getDocumentWithPendingPatches(transaction, mutationBatches, results)
232+
.next(docsWithPendingPatches => {
233+
docsWithPendingPatches.forEach((key, doc) => {
234+
if (doc !== null && doc instanceof Document) {
235+
results = results.insert(key, doc);
236+
}
237+
});
238+
for (const batch of mutationBatches) {
239+
for (const mutation of batch.mutations) {
240+
const key = mutation.key;
241+
const baseDoc = results.get(key);
242+
const mutatedDoc = mutation.applyToLocalView(
243+
baseDoc,
244+
baseDoc,
245+
batch.localWriteTime
246+
);
247+
if (mutatedDoc instanceof Document) {
248+
results = results.insert(key, mutatedDoc);
249+
} else {
250+
results = results.remove(key);
251+
}
252+
}
277253
}
278-
}
279-
}
280-
})
281-
.next(() => {
254+
});
255+
}).next(() => {
282256
// Finally, filter out any documents that don't actually match
283257
// the query.
284258
results.forEach((key, doc) => {
@@ -290,4 +264,28 @@ export class LocalDocumentsView {
290264
return results;
291265
});
292266
}
267+
268+
private getDocumentWithPendingPatches(
269+
transaction: PersistenceTransaction,
270+
matchingMutationBatches: MutationBatch[],
271+
results: DocumentMap
272+
): PersistencePromise<NullableMaybeDocumentMap> {
273+
let missingBaseDocEntriesForPatching = documentKeySet();
274+
for (const batch of matchingMutationBatches) {
275+
for (const mutation of batch.mutations) {
276+
if (
277+
mutation instanceof PatchMutation &&
278+
results.get(mutation.key) === null
279+
) {
280+
missingBaseDocEntriesForPatching = missingBaseDocEntriesForPatching.add(
281+
mutation.key
282+
);
283+
}
284+
}
285+
}
286+
return this.remoteDocumentCache.getEntries(
287+
transaction,
288+
missingBaseDocEntriesForPatching
289+
);
290+
}
293291
}

packages/firestore/test/unit/specs/query_spec.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
import { Query } from '../../../src/core/query';
19-
import { doc, filter } from '../../util/helpers';
19+
import { doc, filter, path } from '../../util/helpers';
2020

2121
import { Document } from '../../../src/model/document';
2222
import { ResourcePath } from '../../../src/model/path';
@@ -100,10 +100,10 @@ describeSpec('Queries:', [], () => {
100100
});
101101

102102
specTest(
103-
'Ensure correct query results for latency-compensated updates',
103+
'Latency-compensated updates are included in query results',
104104
[],
105105
() => {
106-
const fullQuery = Query.atPath(ResourcePath.fromString('collection'));
106+
const fullQuery = Query.atPath(path('collection'));
107107
const filteredQuery = fullQuery.addFilter(filter('match', '==', true));
108108
const docA = doc('collection/a', 1000, { match: false });
109109
const docAv2Local = doc(

0 commit comments

Comments
 (0)