Search in sources :

Example 21 with DocumentKey

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

the class MemoryRemoteDocumentCache method getAll.

@Override
public Map<DocumentKey, MutableDocument> getAll(ResourcePath collection, IndexOffset offset) {
    Map<DocumentKey, MutableDocument> 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.
    DocumentKey prefix = DocumentKey.fromPath(collection.append(""));
    Iterator<Map.Entry<DocumentKey, Document>> iterator = docs.iteratorFrom(prefix);
    while (iterator.hasNext()) {
        Map.Entry<DocumentKey, Document> entry = iterator.next();
        Document doc = entry.getValue();
        DocumentKey key = entry.getKey();
        if (!collection.isPrefixOf(key.getPath())) {
            // We are now scanning the next collection. Abort.
            break;
        }
        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;
        }
        result.put(doc.getKey(), doc.mutableCopy());
    }
    return result;
}
Also used : HashMap(java.util.HashMap) DocumentKey(com.google.firebase.firestore.model.DocumentKey) MutableDocument(com.google.firebase.firestore.model.MutableDocument) Document(com.google.firebase.firestore.model.Document) MutableDocument(com.google.firebase.firestore.model.MutableDocument) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableSortedMap(com.google.firebase.database.collection.ImmutableSortedMap) DocumentCollections.emptyDocumentMap(com.google.firebase.firestore.model.DocumentCollections.emptyDocumentMap)

Example 22 with DocumentKey

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

the class MemoryMutationQueue method containsKey.

boolean containsKey(DocumentKey key) {
    // Create a reference with a zero ID as the start position to find any document reference with
    // this key.
    DocumentReference reference = new DocumentReference(key, 0);
    Iterator<DocumentReference> iterator = batchesByDocumentKey.iteratorFrom(reference);
    if (!iterator.hasNext()) {
        return false;
    }
    DocumentKey firstKey = iterator.next().getKey();
    return firstKey.equals(key);
}
Also used : DocumentKey(com.google.firebase.firestore.model.DocumentKey)

Example 23 with DocumentKey

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

the class MemoryTargetCache method removeMatchingKeys.

@Override
public void removeMatchingKeys(ImmutableSortedSet<DocumentKey> keys, int targetId) {
    references.removeReferences(keys, targetId);
    ReferenceDelegate referenceDelegate = persistence.getReferenceDelegate();
    for (DocumentKey key : keys) {
        referenceDelegate.removeReference(key);
    }
}
Also used : DocumentKey(com.google.firebase.firestore.model.DocumentKey)

Example 24 with DocumentKey

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

the class MemoryDocumentOverlayCache method removeOverlaysForBatchId.

@Override
public void removeOverlaysForBatchId(int batchId) {
    if (overlayByBatchId.containsKey(batchId)) {
        Set<DocumentKey> keys = overlayByBatchId.get(batchId);
        overlayByBatchId.remove(batchId);
        for (DocumentKey key : keys) {
            overlays.remove(key);
        }
    }
}
Also used : DocumentKey(com.google.firebase.firestore.model.DocumentKey)

Example 25 with DocumentKey

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

the class LocalDocumentsView method getDocumentsMatchingCollectionGroupQuery.

private ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingCollectionGroupQuery(Query query, IndexOffset offset) {
    hardAssert(query.getPath().isEmpty(), "Currently we only support collection group queries at the root.");
    String collectionId = query.getCollectionGroup();
    ImmutableSortedMap<DocumentKey, Document> results = emptyDocumentMap();
    List<ResourcePath> parents = indexManager.getCollectionParents(collectionId);
    // aggregate the results.
    for (ResourcePath parent : parents) {
        Query collectionQuery = query.asCollectionQueryAtPath(parent.append(collectionId));
        ImmutableSortedMap<DocumentKey, Document> collectionResults = getDocumentsMatchingCollectionQuery(collectionQuery, offset);
        for (Map.Entry<DocumentKey, Document> docEntry : collectionResults) {
            results = results.insert(docEntry.getKey(), docEntry.getValue());
        }
    }
    return results;
}
Also used : ResourcePath(com.google.firebase.firestore.model.ResourcePath) Query(com.google.firebase.firestore.core.Query) DocumentKey(com.google.firebase.firestore.model.DocumentKey) Document(com.google.firebase.firestore.model.Document) MutableDocument(com.google.firebase.firestore.model.MutableDocument) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) ImmutableSortedMap(com.google.firebase.database.collection.ImmutableSortedMap) DocumentCollections.emptyDocumentMap(com.google.firebase.firestore.model.DocumentCollections.emptyDocumentMap)

Aggregations

DocumentKey (com.google.firebase.firestore.model.DocumentKey)134 MutableDocument (com.google.firebase.firestore.model.MutableDocument)52 Test (org.junit.Test)36 HashMap (java.util.HashMap)29 ArrayList (java.util.ArrayList)25 Document (com.google.firebase.firestore.model.Document)23 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)16 Mutation (com.google.firebase.firestore.model.mutation.Mutation)15 Map (java.util.Map)15 ResourcePath (com.google.firebase.firestore.model.ResourcePath)13 MutationBatch (com.google.firebase.firestore.model.mutation.MutationBatch)11 HashSet (java.util.HashSet)11 Overlay (com.google.firebase.firestore.model.mutation.Overlay)10 ImmutableSortedMap (com.google.firebase.database.collection.ImmutableSortedMap)9 TestUtil.patchMutation (com.google.firebase.firestore.testutil.TestUtil.patchMutation)7 TestUtil.setMutation (com.google.firebase.firestore.testutil.TestUtil.setMutation)7 ByteString (com.google.protobuf.ByteString)7 Query (com.google.firebase.firestore.core.Query)6 ObjectValue (com.google.firebase.firestore.model.ObjectValue)6 Timestamp (com.google.firebase.Timestamp)5