Search in sources :

Example 31 with ResourcePath

use of com.google.firebase.firestore.model.ResourcePath in project firebase-android-sdk by firebase.

the class MemoryMutationQueue method getAllMutationBatchesAffectingQuery.

@Override
public List<MutationBatch> getAllMutationBatchesAffectingQuery(Query query) {
    hardAssert(!query.isCollectionGroupQuery(), "CollectionGroup queries should be handled in LocalDocumentsView");
    // Use the query path as a prefix for testing if a document matches the query.
    ResourcePath prefix = query.getPath();
    int immediateChildrenPathLength = prefix.length() + 1;
    // Construct a document reference for actually scanning the index. Unlike the prefix, the
    // document key in this reference must have an even number of segments. The empty segment can be
    // used as a suffix of the query path because it precedes all other segments in an ordered
    // traversal.
    ResourcePath startPath = prefix;
    if (!DocumentKey.isDocumentKey(startPath)) {
        startPath = startPath.append("");
    }
    DocumentReference start = new DocumentReference(DocumentKey.fromPath(startPath), 0);
    // Find unique batchIDs referenced by all documents potentially matching the query.
    ImmutableSortedSet<Integer> uniqueBatchIDs = new ImmutableSortedSet<Integer>(emptyList(), Util.comparator());
    Iterator<DocumentReference> iterator = batchesByDocumentKey.iteratorFrom(start);
    while (iterator.hasNext()) {
        DocumentReference reference = iterator.next();
        ResourcePath rowKeyPath = reference.getKey().getPath();
        if (!prefix.isPrefixOf(rowKeyPath)) {
            break;
        }
        // TODO: we'll need a different scanner when we implement ancestor queries.
        if (rowKeyPath.length() == immediateChildrenPathLength) {
            uniqueBatchIDs = uniqueBatchIDs.insert(reference.getId());
        }
    }
    return lookupMutationBatches(uniqueBatchIDs);
}
Also used : ResourcePath(com.google.firebase.firestore.model.ResourcePath) ImmutableSortedSet(com.google.firebase.database.collection.ImmutableSortedSet)

Example 32 with ResourcePath

use of com.google.firebase.firestore.model.ResourcePath in project firebase-android-sdk by firebase.

the class CountingQueryEngine method wrapRemoteDocumentCache.

private RemoteDocumentCache wrapRemoteDocumentCache(RemoteDocumentCache subject) {
    return new RemoteDocumentCache() {

        @Override
        public void setIndexManager(IndexManager indexManager) {
        // Not implemented.
        }

        @Override
        public void add(MutableDocument document, SnapshotVersion readTime) {
            subject.add(document, readTime);
        }

        @Override
        public void removeAll(Collection<DocumentKey> keys) {
            subject.removeAll(keys);
        }

        @Override
        public MutableDocument get(DocumentKey documentKey) {
            MutableDocument result = subject.get(documentKey);
            documentsReadByKey[0] += result.isValidDocument() ? 1 : 0;
            return result;
        }

        @Override
        public Map<DocumentKey, MutableDocument> getAll(Iterable<DocumentKey> documentKeys) {
            Map<DocumentKey, MutableDocument> result = subject.getAll(documentKeys);
            for (MutableDocument document : result.values()) {
                documentsReadByKey[0] += document.isValidDocument() ? 1 : 0;
            }
            return result;
        }

        @Override
        public Map<DocumentKey, MutableDocument> getAll(String collectionGroup, IndexOffset offset, int limit) {
            Map<DocumentKey, MutableDocument> result = subject.getAll(collectionGroup, offset, limit);
            documentsReadByCollection[0] += result.size();
            return result;
        }

        @Override
        public Map<DocumentKey, MutableDocument> getAll(ResourcePath collection, IndexOffset offset) {
            Map<DocumentKey, MutableDocument> result = subject.getAll(collection, offset);
            documentsReadByCollection[0] += result.size();
            return result;
        }
    };
}
Also used : ResourcePath(com.google.firebase.firestore.model.ResourcePath) SnapshotVersion(com.google.firebase.firestore.model.SnapshotVersion) MutableDocument(com.google.firebase.firestore.model.MutableDocument) DocumentKey(com.google.firebase.firestore.model.DocumentKey) Collection(java.util.Collection) IndexOffset(com.google.firebase.firestore.model.FieldIndex.IndexOffset)

Example 33 with ResourcePath

use of com.google.firebase.firestore.model.ResourcePath in project firebase-android-sdk by firebase.

the class IndexManagerTestCase method assertParents.

private void assertParents(IndexManager indexManager, String collectionId, List<String> expected) {
    List<ResourcePath> actualPaths = indexManager.getCollectionParents(collectionId);
    List<String> actual = new ArrayList<String>();
    for (ResourcePath actualPath : actualPaths) {
        actual.add(actualPath.toString());
    }
    expected.sort(String::compareTo);
    actual.sort(String::compareTo);
    assertEquals(expected, actual);
}
Also used : ResourcePath(com.google.firebase.firestore.model.ResourcePath) ArrayList(java.util.ArrayList)

Example 34 with ResourcePath

use of com.google.firebase.firestore.model.ResourcePath in project firebase-android-sdk by firebase.

the class QueryTest method testMatchesBasedDocumentKey.

@Test
public void testMatchesBasedDocumentKey() {
    ResourcePath queryPath = ResourcePath.fromString("rooms/eros/messages/1");
    MutableDocument doc1 = doc("rooms/eros/messages/1", 0, map("text", "msg1"));
    MutableDocument doc2 = doc("rooms/eros/messages/2", 0, map("text", "msg2"));
    MutableDocument doc3 = doc("rooms/other/messages/1", 0, map("text", "msg3"));
    Query query = Query.atPath(queryPath);
    assertTrue(query.matches(doc1));
    assertFalse(query.matches(doc2));
    assertFalse(query.matches(doc3));
}
Also used : ResourcePath(com.google.firebase.firestore.model.ResourcePath) MutableDocument(com.google.firebase.firestore.model.MutableDocument) Test(org.junit.Test)

Aggregations

ResourcePath (com.google.firebase.firestore.model.ResourcePath)34 MutableDocument (com.google.firebase.firestore.model.MutableDocument)13 DocumentKey (com.google.firebase.firestore.model.DocumentKey)12 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)8 OrderBy (com.google.firebase.firestore.core.OrderBy)4 HashMap (java.util.HashMap)4 Bound (com.google.firebase.firestore.core.Bound)3 Cursor (android.database.Cursor)2 SQLiteStatement (android.database.sqlite.SQLiteStatement)2 ImmutableSortedMap (com.google.firebase.database.collection.ImmutableSortedMap)2 ImmutableSortedSet (com.google.firebase.database.collection.ImmutableSortedSet)2 FieldFilter (com.google.firebase.firestore.core.FieldFilter)2 Filter (com.google.firebase.firestore.core.Filter)2 Query (com.google.firebase.firestore.core.Query)2 EncodedPath.decodeResourcePath (com.google.firebase.firestore.local.EncodedPath.decodeResourcePath)2 DocumentCollections.emptyDocumentMap (com.google.firebase.firestore.model.DocumentCollections.emptyDocumentMap)2 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)2 BackgroundQueue (com.google.firebase.firestore.util.BackgroundQueue)2 CollectionSelector (com.google.firestore.v1.StructuredQuery.CollectionSelector)2