Search in sources :

Example 61 with DocumentKey

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

the class RemoteDocumentCacheTestCase method testGetAllFromUsesReadTimeNotUpdateTime.

@Test
public void testGetAllFromUsesReadTimeNotUpdateTime() {
    addTestDocumentAtPath("b/old", /* updateTime= */
    1, /* readTime= */
    2);
    addTestDocumentAtPath("b/new", /* updateTime= */
    2, /* readTime= */
    1);
    ResourcePath collection = path("b");
    Map<DocumentKey, MutableDocument> results = remoteDocumentCache.getAll(collection, IndexOffset.createSuccessor(version(1), -1));
    assertThat(results.values()).containsExactly(doc("b/old", 1, DOC_DATA));
}
Also used : ResourcePath(com.google.firebase.firestore.model.ResourcePath) DocumentKey(com.google.firebase.firestore.model.DocumentKey) MutableDocument(com.google.firebase.firestore.model.MutableDocument) Test(org.junit.Test)

Example 62 with DocumentKey

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

the class RemoteSerializer method decodeMissingDocument.

private MutableDocument decodeMissingDocument(BatchGetDocumentsResponse response) {
    Assert.hardAssert(response.getResultCase().equals(ResultCase.MISSING), "Tried to deserialize a missing document from a found document.");
    DocumentKey key = decodeKey(response.getMissing());
    SnapshotVersion version = decodeVersion(response.getReadTime());
    hardAssert(!version.equals(SnapshotVersion.NONE), "Got a no document response with no snapshot version");
    return MutableDocument.newNoDocument(key, version);
}
Also used : SnapshotVersion(com.google.firebase.firestore.model.SnapshotVersion) DocumentKey(com.google.firebase.firestore.model.DocumentKey)

Example 63 with DocumentKey

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

the class TargetState method toTargetChange.

/**
 * Creates a target change from the current set of changes.
 *
 * <p>To reset the document changes after raising this snapshot, call `clearChanges()`.
 */
TargetChange toTargetChange() {
    ImmutableSortedSet<DocumentKey> addedDocuments = DocumentKey.emptyKeySet();
    ImmutableSortedSet<DocumentKey> modifiedDocuments = DocumentKey.emptyKeySet();
    ImmutableSortedSet<DocumentKey> removedDocuments = DocumentKey.emptyKeySet();
    for (Map.Entry<DocumentKey, DocumentViewChange.Type> entry : this.documentChanges.entrySet()) {
        DocumentKey key = entry.getKey();
        DocumentViewChange.Type changeType = entry.getValue();
        switch(changeType) {
            case ADDED:
                addedDocuments = addedDocuments.insert(key);
                break;
            case MODIFIED:
                modifiedDocuments = modifiedDocuments.insert(key);
                break;
            case REMOVED:
                removedDocuments = removedDocuments.insert(key);
                break;
            default:
                throw fail("Encountered invalid change type: %s", changeType);
        }
    }
    return new TargetChange(resumeToken, current, addedDocuments, modifiedDocuments, removedDocuments);
}
Also used : DocumentViewChange(com.google.firebase.firestore.core.DocumentViewChange) DocumentKey(com.google.firebase.firestore.model.DocumentKey) Map(java.util.Map) HashMap(java.util.HashMap)

Example 64 with DocumentKey

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

the class WatchChangeAggregator method handleExistenceFilter.

/**
 * Handles existence filters and synthesizes deletes for filter mismatches. Targets that are
 * invalidated by filter mismatches are added to `pendingTargetResets`.
 */
public void handleExistenceFilter(ExistenceFilterWatchChange watchChange) {
    int targetId = watchChange.getTargetId();
    int expectedCount = watchChange.getExistenceFilter().getCount();
    TargetData targetData = queryDataForActiveTarget(targetId);
    if (targetData != null) {
        Target target = targetData.getTarget();
        if (target.isDocumentQuery()) {
            if (expectedCount == 0) {
                // The existence filter told us the document does not exist. We deduce that this document
                // does not exist and apply a deleted document to our updates. Without applying this
                // deleted document there might be another query that will raise this document as part of
                // a snapshot  until it is resolved, essentially exposing inconsistency between queries.
                DocumentKey key = DocumentKey.fromPath(target.getPath());
                MutableDocument result = MutableDocument.newNoDocument(key, SnapshotVersion.NONE);
                removeDocumentFromTarget(targetId, key, result);
            } else {
                hardAssert(expectedCount == 1, "Single document existence filter with count: %d", expectedCount);
            }
        } else {
            long currentSize = getCurrentDocumentCountForTarget(targetId);
            if (currentSize != expectedCount) {
                // Existence filter mismatch: We reset the mapping and raise a new snapshot with
                // `isFromCache:true`.
                resetTarget(targetId);
                pendingTargetResets.add(targetId);
            }
        }
    }
}
Also used : TargetData(com.google.firebase.firestore.local.TargetData) Target(com.google.firebase.firestore.core.Target) DocumentKey(com.google.firebase.firestore.model.DocumentKey) MutableDocument(com.google.firebase.firestore.model.MutableDocument)

Example 65 with DocumentKey

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

the class WatchChangeAggregator method handleDocumentChange.

/**
 * Processes and adds the DocumentWatchChange to the current set of changes.
 */
public void handleDocumentChange(DocumentChange documentChange) {
    MutableDocument document = documentChange.getNewDocument();
    DocumentKey documentKey = documentChange.getDocumentKey();
    for (int targetId : documentChange.getUpdatedTargetIds()) {
        if (document != null && document.isFoundDocument()) {
            addDocumentToTarget(targetId, document);
        } else {
            removeDocumentFromTarget(targetId, documentKey, document);
        }
    }
    for (int targetId : documentChange.getRemovedTargetIds()) {
        removeDocumentFromTarget(targetId, documentKey, documentChange.getNewDocument());
    }
}
Also used : MutableDocument(com.google.firebase.firestore.model.MutableDocument) DocumentKey(com.google.firebase.firestore.model.DocumentKey)

Aggregations

DocumentKey (com.google.firebase.firestore.model.DocumentKey)139 MutableDocument (com.google.firebase.firestore.model.MutableDocument)53 Test (org.junit.Test)36 HashMap (java.util.HashMap)34 ArrayList (java.util.ArrayList)24 Document (com.google.firebase.firestore.model.Document)20 Mutation (com.google.firebase.firestore.model.mutation.Mutation)18 Map (java.util.Map)18 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)16 ResourcePath (com.google.firebase.firestore.model.ResourcePath)13 MutationBatch (com.google.firebase.firestore.model.mutation.MutationBatch)11 ImmutableSortedMap (com.google.firebase.database.collection.ImmutableSortedMap)10 Overlay (com.google.firebase.firestore.model.mutation.Overlay)10 HashSet (java.util.HashSet)10 TestUtil.patchMutation (com.google.firebase.firestore.testutil.TestUtil.patchMutation)8 TestUtil.setMutation (com.google.firebase.firestore.testutil.TestUtil.setMutation)8 ByteString (com.google.protobuf.ByteString)7 Target (com.google.firebase.firestore.core.Target)6 ObjectValue (com.google.firebase.firestore.model.ObjectValue)6 TestUtil.deleteMutation (com.google.firebase.firestore.testutil.TestUtil.deleteMutation)6