Skip to content

Commit 2f1ce77

Browse files
More feedback
1 parent ac38cbe commit 2f1ce77

File tree

2 files changed

+49
-98
lines changed

2 files changed

+49
-98
lines changed

packages/firestore/src/local/index_free_query_engine.ts

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ import { PersistencePromise } from './persistence_promise';
2222
import { QueryData } from './query_data';
2323
import { Query } from '../core/query';
2424
import { SnapshotVersion } from '../core/snapshot_version';
25-
import { DocumentKeySet, DocumentMap } from '../model/collections';
25+
import {
26+
DocumentKeySet,
27+
DocumentMap,
28+
MaybeDocumentMap
29+
} from '../model/collections';
2630
import { Document } from '../model/document';
2731
import { assert } from '../util/assert';
2832
import { debug, getLogLevel, LogLevel } from '../util/log';
@@ -82,8 +86,10 @@ export class IndexFreeQueryEngine implements QueryEngine {
8286
return this.executeFullCollectionScan(transaction, query);
8387
}
8488

85-
return this.getSortedDocuments(transaction, query, remoteKeys).next(
86-
previousResults => {
89+
return this.localDocumentsView!.getDocuments(transaction, remoteKeys).next(
90+
documents => {
91+
const previousResults = this.applyQuery(query, documents);
92+
8793
if (
8894
query.hasLimit() &&
8995
this.needsRefill(
@@ -111,6 +117,9 @@ export class IndexFreeQueryEngine implements QueryEngine {
111117
query,
112118
queryData.lastLimboFreeSnapshotVersion
113119
).next(updatedResults => {
120+
// We merge `previousResults` into `updateResults`, since
121+
// `updateResults` is already a DocumentMap. If a document is
122+
// contained in both lists, then its contents are the same.
114123
previousResults.forEach(doc => {
115124
updatedResults = updatedResults.insert(doc.key, doc);
116125
});
@@ -120,31 +129,22 @@ export class IndexFreeQueryEngine implements QueryEngine {
120129
);
121130
}
122131

123-
/**
124-
* Returns the documents for the specified remote keys if they still match the
125-
* query, sorted by the query's comparator.
126-
*/
127-
private getSortedDocuments(
128-
transaction: PersistenceTransaction,
129-
sortByQuery: Query,
130-
remoteKeys: DocumentKeySet
131-
): PersistencePromise<SortedSet<Document>> {
132-
return this.localDocumentsView!.getDocuments(transaction, remoteKeys).next(
133-
previousResults => {
134-
// Sort the documents and re-apply the query filter since previously
135-
// matching documents do not necessarily still match the query.
136-
let results = new SortedSet<Document>((d1, d2) =>
137-
sortByQuery.docComparator(d1, d2)
138-
);
139-
previousResults.forEach((_, maybeDoc) => {
140-
if (maybeDoc instanceof Document && sortByQuery.matches(maybeDoc)) {
141-
results = results.add(maybeDoc);
142-
}
143-
});
144-
145-
return results;
146-
}
132+
/** Applies the query filter and sorting to the provided documents. */
133+
private applyQuery(
134+
query: Query,
135+
documents: MaybeDocumentMap
136+
): SortedSet<Document> {
137+
// Sort the documents and re-apply the query filter since previously
138+
// matching documents do not necessarily still match the query.
139+
let queyrResults = new SortedSet<Document>((d1, d2) =>
140+
query.docComparator(d1, d2)
147141
);
142+
documents.forEach((_, maybeDoc) => {
143+
if (maybeDoc instanceof Document && query.matches(maybeDoc)) {
144+
queyrResults = queyrResults.add(maybeDoc);
145+
}
146+
});
147+
return queyrResults;
148148
}
149149

150150
/**

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

Lines changed: 22 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,8 @@ import { DocumentKey } from '../../../src/model/document_key';
3838
import { DocumentSet } from '../../../src/model/document_set';
3939
import { assert } from '../../../src/util/assert';
4040
import { testMemoryEagerPersistence } from './persistence_test_helpers';
41-
import {
42-
doc,
43-
filter,
44-
key,
45-
orderBy,
46-
path,
47-
queryData as testQueryData
48-
} from '../../util/helpers';
41+
import { doc, filter, key, orderBy, path, version } from '../../util/helpers';
42+
import { ListenSequence } from '../../../src/core/listen_sequence';
4943

5044
const TEST_TARGET_ID = 1;
5145

@@ -183,13 +177,7 @@ describe('IndexFreeQueryEngine', () => {
183177
const query = Query.atPath(path('coll')).addFilter(
184178
filter('matches', '==', true)
185179
);
186-
const queryData = testQueryData(
187-
2,
188-
QueryPurpose.Listen,
189-
'coll',
190-
10,
191-
/* hasLimboFreeSnapshot= */ true
192-
);
180+
const queryData = testQueryData(query, /* hasLimboFreeSnapshot= */ true);
193181

194182
await addDocument(MATCHING_DOC_A, MATCHING_DOC_B);
195183
await persistQueryMapping(MATCHING_DOC_A.key, MATCHING_DOC_B.key);
@@ -203,13 +191,7 @@ describe('IndexFreeQueryEngine', () => {
203191
const query = Query.atPath(path('coll')).addFilter(
204192
filter('matches', '==', true)
205193
);
206-
const queryData = testQueryData(
207-
2,
208-
QueryPurpose.Listen,
209-
'coll',
210-
10,
211-
/* hasLimboFreeSnapshot= */ true
212-
);
194+
const queryData = testQueryData(query, /* hasLimboFreeSnapshot= */ true);
213195

214196
await addDocument(MATCHING_DOC_A, MATCHING_DOC_B);
215197
await persistQueryMapping(MATCHING_DOC_A.key, MATCHING_DOC_B.key);
@@ -226,13 +208,7 @@ describe('IndexFreeQueryEngine', () => {
226208
const query = Query.atPath(path('coll')).addFilter(
227209
filter('matches', '==', true)
228210
);
229-
const queryData = testQueryData(
230-
2,
231-
QueryPurpose.Listen,
232-
'coll',
233-
10,
234-
/* hasLimboFreeSnapshot= */ true
235-
);
211+
const queryData = testQueryData(query, /* hasLimboFreeSnapshot= */ true);
236212

237213
await addDocument(MATCHING_DOC_A, MATCHING_DOC_B);
238214
await persistQueryMapping(MATCHING_DOC_A.key, MATCHING_DOC_B.key);
@@ -251,13 +227,7 @@ describe('IndexFreeQueryEngine', () => {
251227
const query = Query.atPath(path('coll')).addFilter(
252228
filter('matches', '==', true)
253229
);
254-
const queryData = testQueryData(
255-
2,
256-
QueryPurpose.Listen,
257-
'coll',
258-
10,
259-
/* hasLimboFreeSnapshot= */ false
260-
);
230+
const queryData = testQueryData(query, /* hasLimboFreeSnapshot= */ false);
261231

262232
const docs = await expectFullCollectionQuery(() =>
263233
runQuery(query, queryData)
@@ -267,13 +237,7 @@ describe('IndexFreeQueryEngine', () => {
267237

268238
it('does not use initial results for unfiltered collection query', async () => {
269239
const query = Query.atPath(path('coll'));
270-
const queryData = testQueryData(
271-
2,
272-
QueryPurpose.Listen,
273-
'coll',
274-
10,
275-
/* hasLimboFreeSnapshot= */ true
276-
);
240+
const queryData = testQueryData(query, /* hasLimboFreeSnapshot= */ true);
277241

278242
const docs = await expectFullCollectionQuery(() =>
279243
runQuery(query, queryData)
@@ -294,13 +258,7 @@ describe('IndexFreeQueryEngine', () => {
294258

295259
await addDocument(MATCHING_DOC_B);
296260

297-
const queryData = testQueryData(
298-
2,
299-
QueryPurpose.Listen,
300-
'coll',
301-
10,
302-
/* hasLimboFreeSnapshot= */ true
303-
);
261+
const queryData = testQueryData(query, /* hasLimboFreeSnapshot= */ true);
304262
const docs = await expectFullCollectionQuery(() =>
305263
runQuery(query, queryData)
306264
);
@@ -319,13 +277,7 @@ describe('IndexFreeQueryEngine', () => {
319277
await addDocument(PENDING_MATCHING_DOC_A);
320278
await persistQueryMapping(PENDING_MATCHING_DOC_A.key);
321279

322-
const queryData = testQueryData(
323-
2,
324-
QueryPurpose.Listen,
325-
'coll',
326-
10,
327-
/* hasLimboFreeSnapshot= */ true
328-
);
280+
const queryData = testQueryData(query, /* hasLimboFreeSnapshot= */ true);
329281

330282
await addDocument(MATCHING_DOC_B);
331283

@@ -347,13 +299,7 @@ describe('IndexFreeQueryEngine', () => {
347299
await addDocument(UPDATED_DOC_A);
348300
await persistQueryMapping(UPDATED_DOC_A.key);
349301

350-
const queryData = testQueryData(
351-
2,
352-
QueryPurpose.Listen,
353-
'coll',
354-
10,
355-
/* hasLimboFreeSnapshot= */ true
356-
);
302+
const queryData = testQueryData(query, /* hasLimboFreeSnapshot= */ true);
357303

358304
await addDocument(MATCHING_DOC_B);
359305

@@ -372,13 +318,7 @@ describe('IndexFreeQueryEngine', () => {
372318
await addDocument(doc('coll/b', 1, { order: 3 }));
373319
await persistQueryMapping(key('coll/a'), key('coll/b'));
374320

375-
const queryData = testQueryData(
376-
2,
377-
QueryPurpose.Listen,
378-
'coll',
379-
10,
380-
/* hasLimboFreeSnapshot= */ true
381-
);
321+
const queryData = testQueryData(query, /* hasLimboFreeSnapshot= */ true);
382322

383323
// Update "coll/a" but make sure it still sorts before "coll/b"
384324
await addDocument(
@@ -412,3 +352,14 @@ function verifyResult(
412352
'Result count does not match'
413353
);
414354
}
355+
356+
function testQueryData(query: Query, hasLimboFreeSnapshot: boolean): QueryData {
357+
return new QueryData(
358+
query,
359+
TEST_TARGET_ID,
360+
QueryPurpose.Listen,
361+
ListenSequence.INVALID,
362+
version(10),
363+
hasLimboFreeSnapshot ? version(10) : SnapshotVersion.MIN
364+
);
365+
}

0 commit comments

Comments
 (0)