Skip to content

Firestore overlays #6283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 19, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 124 additions & 7 deletions packages/firestore/test/unit/local/query_engine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ import {
import { SnapshotVersion } from '../../../src/core/snapshot_version';
import { View } from '../../../src/core/view';
import { DocumentOverlayCache } from '../../../src/local/document_overlay_cache';
import { IndexedDbPersistence } from '../../../src/local/indexeddb_persistence';
import {
INDEXING_ENABLED,
INDEXING_SCHEMA_VERSION
} from '../../../src/local/indexeddb_schema';
import { LocalDocumentsView } from '../../../src/local/local_documents_view';
import { MemoryIndexManager } from '../../../src/local/memory_index_manager';
import { MutationQueue } from '../../../src/local/mutation_queue';
import { Persistence } from '../../../src/local/persistence';
import { PersistencePromise } from '../../../src/local/persistence_promise';
Expand All @@ -41,7 +45,8 @@ import { TargetCache } from '../../../src/local/target_cache';
import {
documentKeySet,
DocumentMap,
newMutationMap
newMutationMap,
documentMap
} from '../../../src/model/collections';
import { Document, MutableDocument } from '../../../src/model/document';
import { DocumentKey } from '../../../src/model/document_key';
Expand All @@ -61,6 +66,9 @@ import {
key,
orderBy,
query,
fieldIndex,
patchMutation,
setMutation,
version
} from '../../util/helpers';

Expand Down Expand Up @@ -139,7 +147,6 @@ function genericQueryEngineTest(
): void {
let persistence!: Persistence;
let remoteDocumentCache!: RemoteDocumentCache;
let mutationQueue!: MutationQueue;
let documentOverlayCache!: DocumentOverlayCache;
let targetCache!: TargetCache;
let queryEngine!: QueryEngine;
Expand Down Expand Up @@ -252,10 +259,10 @@ function genericQueryEngineTest(
User.UNAUTHENTICATED
);
remoteDocumentCache = persistence.getRemoteDocumentCache();
remoteDocumentCache.setIndexManager(indexManager);
remoteDocumentCache.setIndexManager(underlyingIndexManager);
mutationQueue = persistence.getMutationQueue(
User.UNAUTHENTICATED,
indexManager
underlyingIndexManager
);
documentOverlayCache = persistence.getDocumentOverlayCache(
User.UNAUTHENTICATED
Expand All @@ -264,7 +271,7 @@ function genericQueryEngineTest(
remoteDocumentCache,
mutationQueue,
documentOverlayCache,
new MemoryIndexManager()
underlyingIndexManager
);
queryEngine.initialize(localDocuments, underlyingIndexManager);

Expand Down Expand Up @@ -509,7 +516,117 @@ function genericQueryEngineTest(
);
verifyResult(docs, [MATCHING_DOC_A]);
});
});

if (!durable) {
return;
}

if (!INDEXING_ENABLED) {
return;
}

it('combines indexed with non-indexed results', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we port this to Android? I could not find it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why GitHub shows this as a new addition in the diff.

These tests exist in the tip-of-tree in the Web SDK. They also exist on Android. Sebastian ported these from Android to the Web via #6153

debugAssert(durable, 'Test requires durable persistence');

const doc1 = doc('coll/a', 1, { 'foo': true });
const doc2 = doc('coll/b', 2, { 'foo': true });
const doc3 = doc('coll/c', 3, { 'foo': true });
const doc4 = doc('coll/d', 3, { 'foo': true }).setHasLocalMutations();

await indexManager.addFieldIndex(
fieldIndex('coll', { fields: [['foo', IndexKind.ASCENDING]] })
);

await addDocument(doc1);
await addDocument(doc2);
await indexManager.updateIndexEntries(documentMap(doc1, doc2));
await indexManager.updateCollectionGroup(
'coll',
newIndexOffsetFromDocument(doc2)
);

await addDocument(doc3);
await addMutation(setMutation('coll/d', { 'foo': true }));

const queryWithFilter = queryWithAddedFilter(
query('coll'),
filter('foo', '==', true)
);
const results = await expectOptimizedCollectionQuery(() =>
runQuery(queryWithFilter, SnapshotVersion.min())
);

verifyResult(results, [doc1, doc2, doc3, doc4]);
});

it('uses partial index for limit queries', async () => {
debugAssert(durable, 'Test requires durable persistence');

const doc1 = doc('coll/1', 1, { 'a': 1, 'b': 0 });
const doc2 = doc('coll/2', 1, { 'a': 1, 'b': 1 });
const doc3 = doc('coll/3', 1, { 'a': 1, 'b': 2 });
const doc4 = doc('coll/4', 1, { 'a': 1, 'b': 3 });
const doc5 = doc('coll/5', 1, { 'a': 2, 'b': 3 });
await addDocument(doc1, doc2, doc3, doc4, doc5);

await indexManager.addFieldIndex(
fieldIndex('coll', { fields: [['a', IndexKind.ASCENDING]] })
);
await indexManager.updateIndexEntries(
documentMap(doc1, doc2, doc3, doc4, doc5)
);
await indexManager.updateCollectionGroup(
'coll',
newIndexOffsetFromDocument(doc5)
);

const q = queryWithLimit(
queryWithAddedFilter(
queryWithAddedFilter(query('coll'), filter('a', '==', 1)),
filter('b', '==', 1)
),
3,
LimitType.First
);
const results = await expectOptimizedCollectionQuery(() =>
runQuery(q, SnapshotVersion.min())
);

verifyResult(results, [doc2]);
});

it('re-fills indexed limit queries', async () => {
debugAssert(durable, 'Test requires durable persistence');

const doc1 = doc('coll/1', 1, { 'a': 1 });
const doc2 = doc('coll/2', 1, { 'a': 2 });
const doc3 = doc('coll/3', 1, { 'a': 3 });
const doc4 = doc('coll/4', 1, { 'a': 4 });
await addDocument(doc1, doc2, doc3, doc4);

await indexManager.addFieldIndex(
fieldIndex('coll', { fields: [['a', IndexKind.ASCENDING]] })
);
await indexManager.updateIndexEntries(documentMap(doc1, doc2, doc3, doc4));
await indexManager.updateCollectionGroup(
'coll',
newIndexOffsetFromDocument(doc4)
);

await addMutation(patchMutation('coll/3', { 'a': 5 }));

const q = queryWithLimit(
queryWithAddedOrderBy(query('coll'), orderBy('a')),
3,
LimitType.First
);
const results = await expectOptimizedCollectionQuery(() =>
runQuery(q, SnapshotVersion.min())
);

verifyResult(results, [doc1, doc2, doc4]);
});
}

function verifyResult(actualDocs: DocumentSet, expectedDocs: Document[]): void {
for (const doc of expectedDocs) {
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.