diff --git a/firebase-firestore/src/main/java/com/google/firebase/firestore/local/LocalDocumentsView.java b/firebase-firestore/src/main/java/com/google/firebase/firestore/local/LocalDocumentsView.java index 24a0d91315c..8445dfefcd6 100644 --- a/firebase-firestore/src/main/java/com/google/firebase/firestore/local/LocalDocumentsView.java +++ b/firebase-firestore/src/main/java/com/google/firebase/firestore/local/LocalDocumentsView.java @@ -254,26 +254,21 @@ private ImmutableSortedMap getDocumentsMatchingCollection /** Queries the remote documents and overlays by doing a full collection scan. */ private ImmutableSortedMap getDocumentsMatchingCollectionQuery( Query query, IndexOffset offset) { - ImmutableSortedMap remoteDocuments = - remoteDocumentCache.getAllDocumentsMatchingQuery(query, offset); + Map remoteDocuments = + remoteDocumentCache.getAll(query.getPath(), offset); Map overlays = documentOverlayCache.getOverlays(query.getPath(), -1); - // As documents might match the query because of their overlay we need to include all documents - // in the result. - Set missingDocuments = new HashSet<>(); + // As documents might match the query because of their overlay we need to include documents + // for all overlays in the initial document set. for (Map.Entry entry : overlays.entrySet()) { if (!remoteDocuments.containsKey(entry.getKey())) { - missingDocuments.add(entry.getKey()); + remoteDocuments.put(entry.getKey(), MutableDocument.newInvalidDocument(entry.getKey())); } } - for (Map.Entry entry : - remoteDocumentCache.getAll(missingDocuments).entrySet()) { - remoteDocuments = remoteDocuments.insert(entry.getKey(), entry.getValue()); - } // Apply the overlays and match against the query. ImmutableSortedMap results = emptyDocumentMap(); - for (Map.Entry docEntry : remoteDocuments) { + for (Map.Entry docEntry : remoteDocuments.entrySet()) { Mutation overlay = overlays.get(docEntry.getKey()); if (overlay != null) { overlay.applyToLocalView(docEntry.getValue(), null, Timestamp.now()); diff --git a/firebase-firestore/src/main/java/com/google/firebase/firestore/local/MemoryRemoteDocumentCache.java b/firebase-firestore/src/main/java/com/google/firebase/firestore/local/MemoryRemoteDocumentCache.java index acd84633979..1434e0e0d9b 100644 --- a/firebase-firestore/src/main/java/com/google/firebase/firestore/local/MemoryRemoteDocumentCache.java +++ b/firebase-firestore/src/main/java/com/google/firebase/firestore/local/MemoryRemoteDocumentCache.java @@ -14,12 +14,10 @@ package com.google.firebase.firestore.local; -import static com.google.firebase.firestore.model.DocumentCollections.emptyMutableDocumentMap; import static com.google.firebase.firestore.util.Assert.hardAssert; import androidx.annotation.NonNull; import com.google.firebase.database.collection.ImmutableSortedMap; -import com.google.firebase.firestore.core.Query; import com.google.firebase.firestore.model.DocumentKey; import com.google.firebase.firestore.model.FieldIndex.IndexOffset; import com.google.firebase.firestore.model.MutableDocument; @@ -89,41 +87,35 @@ public Map getAll( } @Override - public ImmutableSortedMap getAllDocumentsMatchingQuery( - Query query, IndexOffset offset) { - hardAssert( - !query.isCollectionGroupQuery(), - "CollectionGroup queries should be handled in LocalDocumentsView"); - ImmutableSortedMap result = emptyMutableDocumentMap(); + public Map getAll(ResourcePath collection, IndexOffset offset) { + Map result = new HashMap<>(); // Documents are ordered by key, so we can use a prefix scan to narrow down the documents // we need to match the query against. - ResourcePath queryPath = query.getPath(); - DocumentKey prefix = DocumentKey.fromPath(queryPath.append("")); + DocumentKey prefix = DocumentKey.fromPath(collection.append("")); Iterator> iterator = docs.iteratorFrom(prefix); while (iterator.hasNext()) { Map.Entry entry = iterator.next(); + MutableDocument doc = entry.getValue(); DocumentKey key = entry.getKey(); - if (!queryPath.isPrefixOf(key.getPath())) { + if (!collection.isPrefixOf(key.getPath())) { + // We are now scanning the next collection. Abort. break; } - MutableDocument doc = entry.getValue(); - if (!doc.isFoundDocument()) { + if (key.getPath().length() > collection.length() + 1) { + // Exclude entries from subcollections. continue; } if (IndexOffset.fromDocument(doc).compareTo(offset) <= 0) { + // The document sorts before the offset. continue; } - if (!query.matches(doc)) { - continue; - } - - result = result.insert(doc.getKey(), doc.clone()); + result.put(doc.getKey(), doc.clone()); } return result; diff --git a/firebase-firestore/src/main/java/com/google/firebase/firestore/local/RemoteDocumentCache.java b/firebase-firestore/src/main/java/com/google/firebase/firestore/local/RemoteDocumentCache.java index 87283229ee2..a25f017c0a7 100644 --- a/firebase-firestore/src/main/java/com/google/firebase/firestore/local/RemoteDocumentCache.java +++ b/firebase-firestore/src/main/java/com/google/firebase/firestore/local/RemoteDocumentCache.java @@ -14,11 +14,10 @@ package com.google.firebase.firestore.local; -import com.google.firebase.database.collection.ImmutableSortedMap; -import com.google.firebase.firestore.core.Query; import com.google.firebase.firestore.model.DocumentKey; import com.google.firebase.firestore.model.FieldIndex.IndexOffset; import com.google.firebase.firestore.model.MutableDocument; +import com.google.firebase.firestore.model.ResourcePath; import com.google.firebase.firestore.model.SnapshotVersion; import java.util.Map; @@ -72,24 +71,18 @@ interface RemoteDocumentCache { * @param collectionGroup The collection group to scan. * @param offset The offset to start the scan at. * @param limit The maximum number of results to return. - * @return A map with next set of documents. + * @return A newly created map with next set of documents. */ Map getAll(String collectionGroup, IndexOffset offset, int limit); /** - * Executes a query against the cached Document entries + * Returns the documents from the provided collection. * - *

Implementations may return extra documents if convenient. The results should be re-filtered - * by the consumer before presenting them to the user. - * - *

Cached entries for non-existing documents have no bearing on query results. - * - * @param query The query to match documents against. + * @param collection The collection to read. * @param offset The read time and document key to start scanning at (exclusive). - * @return The set of matching documents. + * @return A newly created map with the set of documents in the collection. */ - ImmutableSortedMap getAllDocumentsMatchingQuery( - Query query, IndexOffset offset); + Map getAll(ResourcePath collection, IndexOffset offset); /** Returns the latest read time of any document in the cache. */ SnapshotVersion getLatestReadTime(); diff --git a/firebase-firestore/src/main/java/com/google/firebase/firestore/local/SQLiteRemoteDocumentCache.java b/firebase-firestore/src/main/java/com/google/firebase/firestore/local/SQLiteRemoteDocumentCache.java index fb45c355c46..747496e0f4a 100644 --- a/firebase-firestore/src/main/java/com/google/firebase/firestore/local/SQLiteRemoteDocumentCache.java +++ b/firebase-firestore/src/main/java/com/google/firebase/firestore/local/SQLiteRemoteDocumentCache.java @@ -21,9 +21,6 @@ import androidx.annotation.VisibleForTesting; import com.google.firebase.Timestamp; -import com.google.firebase.database.collection.ImmutableSortedMap; -import com.google.firebase.firestore.core.Query; -import com.google.firebase.firestore.model.DocumentCollections; import com.google.firebase.firestore.model.DocumentKey; import com.google.firebase.firestore.model.FieldIndex.IndexOffset; import com.google.firebase.firestore.model.MutableDocument; @@ -234,23 +231,8 @@ private Map getAll( } @Override - public ImmutableSortedMap getAllDocumentsMatchingQuery( - final Query query, IndexOffset offset) { - hardAssert( - !query.isCollectionGroupQuery(), - "CollectionGroup queries should be handled in LocalDocumentsView"); - - Map allDocuments = - getAll(Collections.singletonList(query.getPath()), offset, Integer.MAX_VALUE); - - ImmutableSortedMap matchingDocuments = - DocumentCollections.emptyMutableDocumentMap(); - for (MutableDocument document : allDocuments.values()) { - if (document.isFoundDocument() && query.matches(document)) { - matchingDocuments = matchingDocuments.insert(document.getKey(), document); - } - } - return matchingDocuments; + public Map getAll(ResourcePath collection, IndexOffset offset) { + return getAll(Collections.singletonList(collection), offset, Integer.MAX_VALUE); } @Override diff --git a/firebase-firestore/src/main/java/com/google/firebase/firestore/model/MutableDocument.java b/firebase-firestore/src/main/java/com/google/firebase/firestore/model/MutableDocument.java index e359c992578..0d55286d788 100644 --- a/firebase-firestore/src/main/java/com/google/firebase/firestore/model/MutableDocument.java +++ b/firebase-firestore/src/main/java/com/google/firebase/firestore/model/MutableDocument.java @@ -62,22 +62,25 @@ private enum DocumentState { private final DocumentKey key; private DocumentType documentType; private SnapshotVersion version; - private SnapshotVersion readTime = SnapshotVersion.NONE; + private SnapshotVersion readTime; private ObjectValue value; private DocumentState documentState; private MutableDocument(DocumentKey key) { this.key = key; + this.readTime = SnapshotVersion.NONE; } private MutableDocument( DocumentKey key, DocumentType documentType, SnapshotVersion version, + SnapshotVersion readTime, ObjectValue value, DocumentState documentState) { this.key = key; this.version = version; + this.readTime = readTime; this.documentType = documentType; this.documentState = documentState; this.value = value; @@ -92,6 +95,7 @@ public static MutableDocument newInvalidDocument(DocumentKey documentKey) { documentKey, DocumentType.INVALID, SnapshotVersion.NONE, + SnapshotVersion.NONE, new ObjectValue(), DocumentState.SYNCED); } @@ -226,7 +230,7 @@ public boolean isUnknownDocument() { @Override @NonNull public MutableDocument clone() { - return new MutableDocument(key, documentType, version, value.clone(), documentState); + return new MutableDocument(key, documentType, version, readTime, value.clone(), documentState); } @Override @@ -238,6 +242,8 @@ public boolean equals(Object o) { if (!key.equals(document.key)) return false; if (!version.equals(document.version)) return false; + // TODO(mrschmidt): Include readTime (requires a lot of test updates) + // if (!readTime.equals(document.readTime)) return false; if (!documentType.equals(document.documentType)) return false; if (!documentState.equals(document.documentState)) return false; return value.equals(document.value); diff --git a/firebase-firestore/src/test/java/com/google/firebase/firestore/local/CountingQueryEngine.java b/firebase-firestore/src/test/java/com/google/firebase/firestore/local/CountingQueryEngine.java index a434d2af4ca..7b80198b890 100644 --- a/firebase-firestore/src/test/java/com/google/firebase/firestore/local/CountingQueryEngine.java +++ b/firebase-firestore/src/test/java/com/google/firebase/firestore/local/CountingQueryEngine.java @@ -23,6 +23,7 @@ import com.google.firebase.firestore.model.DocumentKey; import com.google.firebase.firestore.model.FieldIndex.IndexOffset; import com.google.firebase.firestore.model.MutableDocument; +import com.google.firebase.firestore.model.ResourcePath; import com.google.firebase.firestore.model.SnapshotVersion; import com.google.firebase.firestore.model.mutation.Mutation; import com.google.firebase.firestore.model.mutation.MutationBatch; @@ -37,9 +38,9 @@ class CountingQueryEngine implements QueryEngine { private final QueryEngine queryEngine; - private final int[] mutationsReadByQuery = new int[] {0}; + private final int[] mutationsReadByCollection = new int[] {0}; private final int[] mutationsReadByKey = new int[] {0}; - private final int[] documentsReadByQuery = new int[] {0}; + private final int[] documentsReadByCollection = new int[] {0}; private final int[] documentsReadByKey = new int[] {0}; CountingQueryEngine(QueryEngine queryEngine) { @@ -47,9 +48,9 @@ class CountingQueryEngine implements QueryEngine { } void resetCounts() { - mutationsReadByQuery[0] = 0; + mutationsReadByCollection[0] = 0; mutationsReadByKey[0] = 0; - documentsReadByQuery[0] = 0; + documentsReadByCollection[0] = 0; documentsReadByKey[0] = 0; } @@ -83,11 +84,11 @@ QueryEngine getSubject() { } /** - * Returns the number of documents returned by the RemoteDocumentCache's - * `getDocumentsMatchingQuery()` API (since the last call to `resetCounts()`) + * Returns the number of documents returned by the RemoteDocumentCache's `getAll()` API (since the + * last call to `resetCounts()`) */ - int getDocumentsReadByQuery() { - return documentsReadByQuery[0]; + int getDocumentsReadByCollection() { + return documentsReadByCollection[0]; } /** @@ -102,8 +103,8 @@ int getDocumentsReadByKey() { * Returns the number of mutations returned by the MutationQueue's * `getAllMutationBatchesAffectingQuery()` API (since the last call to `resetCounts()`) */ - int getMutationsReadByQuery() { - return mutationsReadByQuery[0]; + int getMutationsReadByCollection() { + return mutationsReadByCollection[0]; } /** @@ -153,16 +154,15 @@ public Map getAll(Iterable documentKe public Map getAll( String collectionGroup, IndexOffset offset, int limit) { Map result = subject.getAll(collectionGroup, offset, limit); - documentsReadByQuery[0] += result.size(); + documentsReadByCollection[0] += result.size(); + return result; } @Override - public ImmutableSortedMap getAllDocumentsMatchingQuery( - Query query, IndexOffset offset) { - ImmutableSortedMap result = - subject.getAllDocumentsMatchingQuery(query, offset); - documentsReadByQuery[0] += result.size(); + public Map getAll(ResourcePath collection, IndexOffset offset) { + Map result = subject.getAll(collection, offset); + documentsReadByCollection[0] += result.size(); return result; } @@ -250,7 +250,7 @@ public List getAllMutationBatchesAffectingDocumentKeys( @Override public List getAllMutationBatchesAffectingQuery(Query query) { List result = subject.getAllMutationBatchesAffectingQuery(query); - mutationsReadByQuery[0] += result.size(); + mutationsReadByCollection[0] += result.size(); return result; } diff --git a/firebase-firestore/src/test/java/com/google/firebase/firestore/local/LocalStoreTestCase.java b/firebase-firestore/src/test/java/com/google/firebase/firestore/local/LocalStoreTestCase.java index a1ab2497f70..e1377030cac 100644 --- a/firebase-firestore/src/test/java/com/google/firebase/firestore/local/LocalStoreTestCase.java +++ b/firebase-firestore/src/test/java/com/google/firebase/firestore/local/LocalStoreTestCase.java @@ -321,8 +321,9 @@ private void assertHasNamedQuery(NamedQuery expectedNamedQuery) { * Asserts the expected numbers of mutations read by the MutationQueue since the last call to * `resetPersistenceStats()`. */ - private void assertMutationsRead(int byKey, int byQuery) { - assertEquals("Mutations read (by query)", byQuery, queryEngine.getMutationsReadByQuery()); + private void assertMutationsRead(int byKey, int byCollection) { + assertEquals( + "Mutations read (by collection)", byCollection, queryEngine.getMutationsReadByCollection()); assertEquals("Mutations read (by key)", byKey, queryEngine.getMutationsReadByKey()); } @@ -330,9 +331,11 @@ private void assertMutationsRead(int byKey, int byQuery) { * Asserts the expected numbers of documents read by the RemoteDocumentCache since the last call * to `resetPersistenceStats()`. */ - private void assertRemoteDocumentsRead(int byKey, int byQuery) { + private void assertRemoteDocumentsRead(int byKey, int byCollection) { assertEquals( - "Remote documents read (by query)", byQuery, queryEngine.getDocumentsReadByQuery()); + "Remote documents read (by collection)", + byCollection, + queryEngine.getDocumentsReadByCollection()); assertEquals("Remote documents read (by key)", byKey, queryEngine.getDocumentsReadByKey()); } @@ -976,10 +979,9 @@ public void testReadsAllDocumentsForInitialCollectionQueries() { resetPersistenceStats(); localStore.executeQuery(query, /* usePreviousResults= */ true); - - assertRemoteDocumentsRead(/* byKey= */ 0, /* byQuery= */ 2); + assertRemoteDocumentsRead(/* byKey= */ 0, /* byCollection= */ 2); // No mutations are read because only overlay is needed. - assertMutationsRead(/* byKey= */ 0, /* byQuery= */ 0); + assertMutationsRead(/* byKey= */ 0, /* byCollection= */ 0); } @Test @@ -1104,7 +1106,7 @@ public void testUsesTargetMappingToExecuteQueries() { // Execute the query, but note that we read all existing documents from the RemoteDocumentCache // since we do not yet have target mapping. executeQuery(query); - assertRemoteDocumentsRead(/* byKey= */ 0, /* byQuery= */ 2); + assertRemoteDocumentsRead(/* byKey= */ 0, /* byCollection= */ 3); // Issue a RemoteEvent to persist the target mapping. applyRemoteEvent( @@ -1118,7 +1120,7 @@ public void testUsesTargetMappingToExecuteQueries() { // Execute the query again, this time verifying that we only read the two documents that match // the query. executeQuery(query); - assertRemoteDocumentsRead(/* byKey= */ 2, /* byQuery= */ 0); + assertRemoteDocumentsRead(/* byKey= */ 2, /* byCollection= */ 0); assertQueryReturned("foo/a", "foo/b"); } diff --git a/firebase-firestore/src/test/java/com/google/firebase/firestore/local/RemoteDocumentCacheTestCase.java b/firebase-firestore/src/test/java/com/google/firebase/firestore/local/RemoteDocumentCacheTestCase.java index 18eda812756..d363d22042f 100644 --- a/firebase-firestore/src/test/java/com/google/firebase/firestore/local/RemoteDocumentCacheTestCase.java +++ b/firebase-firestore/src/test/java/com/google/firebase/firestore/local/RemoteDocumentCacheTestCase.java @@ -21,18 +21,16 @@ import static com.google.firebase.firestore.testutil.TestUtil.key; import static com.google.firebase.firestore.testutil.TestUtil.map; import static com.google.firebase.firestore.testutil.TestUtil.path; -import static com.google.firebase.firestore.testutil.TestUtil.values; import static com.google.firebase.firestore.testutil.TestUtil.version; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; -import com.google.firebase.database.collection.ImmutableSortedMap; import com.google.firebase.firestore.auth.User; -import com.google.firebase.firestore.core.Query; import com.google.firebase.firestore.model.DocumentKey; import com.google.firebase.firestore.model.FieldIndex.IndexOffset; import com.google.firebase.firestore.model.MutableDocument; +import com.google.firebase.firestore.model.ResourcePath; import com.google.firebase.firestore.model.SnapshotVersion; import java.util.ArrayList; import java.util.Arrays; @@ -171,79 +169,78 @@ public void testRemoveNonExistentDocument() { } @Test - public void testDocumentsMatchingQuery() { - // TODO: This just verifies that we do a prefix scan against the - // query path. We'll need more tests once we add index support. + public void testGetAllFromCollection() { addTestDocumentAtPath("a/1"); addTestDocumentAtPath("b/1"); addTestDocumentAtPath("b/2"); addTestDocumentAtPath("c/1"); - Query query = Query.atPath(path("b")); - ImmutableSortedMap results = - remoteDocumentCache.getAllDocumentsMatchingQuery(query, IndexOffset.NONE); - assertThat(values(results)).containsExactly(doc("b/1", 42, DOC_DATA), doc("b/2", 42, DOC_DATA)); + ResourcePath collection = path("b"); + Map results = + remoteDocumentCache.getAll(collection, IndexOffset.NONE); + assertThat(results.values()) + .containsExactly(doc("b/1", 42, DOC_DATA), doc("b/2", 42, DOC_DATA)); } @Test - public void testDocumentsMatchingQueryExcludesSubcollections() { + public void testGetAllFromExcludesSubcollections() { addTestDocumentAtPath("a/1"); addTestDocumentAtPath("a/1/b/1"); addTestDocumentAtPath("a/2"); - Query query = Query.atPath(path("a")); - ImmutableSortedMap results = - remoteDocumentCache.getAllDocumentsMatchingQuery(query, IndexOffset.NONE); - assertThat(values(results)).containsExactly(doc("a/1", 42, DOC_DATA), doc("a/2", 42, DOC_DATA)); + ResourcePath collection = path("a"); + Map results = + remoteDocumentCache.getAll(collection, IndexOffset.NONE); + assertThat(results.values()) + .containsExactly(doc("a/1", 42, DOC_DATA), doc("a/2", 42, DOC_DATA)); } @Test - public void testDocumentsMatchingQuerySinceReadTimeAndSeconds() { + public void testGetAllFromSinceReadTimeAndSeconds() { addTestDocumentAtPath("b/old", /* updateTime= */ 1, /* readTime= */ 11); addTestDocumentAtPath("b/current", /* updateTime= */ 2, /* readTime= = */ 12); addTestDocumentAtPath("b/new", /* updateTime= */ 3, /* readTime= = */ 13); - Query query = Query.atPath(path("b")); - ImmutableSortedMap results = - remoteDocumentCache.getAllDocumentsMatchingQuery(query, IndexOffset.create(version(12))); - assertThat(values(results)).containsExactly(doc("b/new", 3, DOC_DATA)); + ResourcePath collection = path("b"); + Map results = + remoteDocumentCache.getAll(collection, IndexOffset.create(version(12))); + assertThat(results.values()).containsExactly(doc("b/new", 3, DOC_DATA)); } @Test - public void testDocumentsMatchingQuerySinceReadTimeAndNanoseconds() { + public void testGetAllFromSinceReadTimeAndNanoseconds() { add(doc("b/old", 1, DOC_DATA), version(1, 1)); add(doc("b/current", 1, DOC_DATA), version(1, 2)); add(doc("b/new", 1, DOC_DATA), version(1, 3)); - Query query = Query.atPath(path("b")); - ImmutableSortedMap results = - remoteDocumentCache.getAllDocumentsMatchingQuery(query, IndexOffset.create(version(1, 2))); - assertThat(values(results)).containsExactly(doc("b/new", 1, DOC_DATA)); + ResourcePath collection = path("b"); + Map results = + remoteDocumentCache.getAll(collection, IndexOffset.create(version(1, 2))); + assertThat(results.values()).containsExactly(doc("b/new", 1, DOC_DATA)); } @Test - public void testDocumentsMatchingQuerySinceReadTimeAndDocumentKey() { + public void testGetAllFromSinceReadTimeAndDocumentKey() { addTestDocumentAtPath("b/a", /* updateTime= */ 1, /* readTime= */ 11); addTestDocumentAtPath("b/b", /* updateTime= */ 2, /* readTime= = */ 11); addTestDocumentAtPath("b/c", /* updateTime= */ 3, /* readTime= = */ 11); addTestDocumentAtPath("b/d", /* updateTime= */ 4, /* readTime= = */ 12); - Query query = Query.atPath(path("b")); - ImmutableSortedMap results = - remoteDocumentCache.getAllDocumentsMatchingQuery( - query, IndexOffset.create(version(11), key("b/b"))); - assertThat(values(results)).containsExactly(doc("b/c", 3, DOC_DATA), doc("b/d", 4, DOC_DATA)); + ResourcePath collection = path("b"); + Map results = + remoteDocumentCache.getAll(collection, IndexOffset.create(version(11), key("b/b"))); + assertThat(results.values()).containsExactly(doc("b/c", 3, DOC_DATA), doc("b/d", 4, DOC_DATA)); } @Test - public void testDocumentsMatchingUsesReadTimeNotUpdateTime() { + public void testGetAllFromUsesReadTimeNotUpdateTime() { addTestDocumentAtPath("b/old", /* updateTime= */ 1, /* readTime= */ 2); addTestDocumentAtPath("b/new", /* updateTime= */ 2, /* readTime= */ 1); - Query query = Query.atPath(path("b")); - ImmutableSortedMap results = - remoteDocumentCache.getAllDocumentsMatchingQuery(query, IndexOffset.create(version(1))); - assertThat(values(results)).containsExactly(doc("b/old", 1, DOC_DATA)); + ResourcePath collection = path("b"); + Map results = + remoteDocumentCache.getAll(collection, IndexOffset.create(version(1))); + assertThat(results.values()).containsExactly(doc("b/old", 1, DOC_DATA)); } @Test diff --git a/firebase-firestore/src/test/java/com/google/firebase/firestore/local/SQLiteSchemaTest.java b/firebase-firestore/src/test/java/com/google/firebase/firestore/local/SQLiteSchemaTest.java index 5ca1e2e897f..7a86def78ed 100644 --- a/firebase-firestore/src/test/java/com/google/firebase/firestore/local/SQLiteSchemaTest.java +++ b/firebase-firestore/src/test/java/com/google/firebase/firestore/local/SQLiteSchemaTest.java @@ -14,11 +14,11 @@ package com.google.firebase.firestore.local; +import static com.google.common.truth.Truth.assertThat; import static com.google.firebase.firestore.local.EncodedPath.decodeResourcePath; import static com.google.firebase.firestore.local.EncodedPath.encode; import static com.google.firebase.firestore.local.SQLiteSchema.VERSION; import static com.google.firebase.firestore.testutil.TestUtil.filter; -import static com.google.firebase.firestore.testutil.TestUtil.key; import static com.google.firebase.firestore.testutil.TestUtil.map; import static com.google.firebase.firestore.testutil.TestUtil.path; import static com.google.firebase.firestore.testutil.TestUtil.query; @@ -34,7 +34,6 @@ import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import androidx.test.core.app.ApplicationProvider; -import com.google.firebase.database.collection.ImmutableSortedMap; import com.google.firebase.firestore.core.Query; import com.google.firebase.firestore.model.DatabaseId; import com.google.firebase.firestore.model.DocumentKey; @@ -45,11 +44,13 @@ import com.google.firebase.firestore.proto.Target; import com.google.firebase.firestore.proto.WriteBatch; import com.google.firebase.firestore.remote.RemoteSerializer; +import com.google.firebase.firestore.testutil.TestUtil; import com.google.firestore.v1.Document; import com.google.firestore.v1.Write; import com.google.protobuf.InvalidProtocolBufferException; import com.google.protobuf.Timestamp; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -443,15 +444,13 @@ public void existingDocumentsRemainReadableAfterIndexFreeMigration() { // Verify that queries with SnapshotVersion.NONE return all results, regardless of whether the // read time has been set. - ImmutableSortedMap results = - remoteDocumentCache.getAllDocumentsMatchingQuery(query("coll"), IndexOffset.NONE); + Map results = + remoteDocumentCache.getAll(path("coll"), IndexOffset.NONE); assertResultsContain(results, "coll/existing", "coll/old", "coll/current", "coll/new"); // Queries that filter by read time only return documents that were written after the index-free // migration. - results = - remoteDocumentCache.getAllDocumentsMatchingQuery( - query("coll"), IndexOffset.create(version(2))); + results = remoteDocumentCache.getAll(path("coll"), IndexOffset.create(version(2))); assertResultsContain(results, "coll/new"); } @@ -727,11 +726,9 @@ private Target createDummyQueryTargetWithLimboFreeVersion(int targetId) { } private void assertResultsContain( - ImmutableSortedMap actualResults, String... docs) { - for (String doc : docs) { - assertTrue("Expected result for " + doc, actualResults.containsKey(key(doc))); - } - assertEquals("Results contain unexpected entries", docs.length, actualResults.size()); + Map actualResults, String... docs) { + assertThat(actualResults.keySet()) + .containsExactly(Arrays.stream(docs).map(TestUtil::key).toArray()); } private void assertTableExists(String tableName) {