Search in sources :

Example 11 with DocumentChange

use of com.google.firebase.firestore.remote.WatchChange.DocumentChange in project firebase-android-sdk by firebase.

the class RemoteStore method handleWatchChange.

private void handleWatchChange(SnapshotVersion snapshotVersion, WatchChange watchChange) {
    // Mark the connection as ONLINE because we got a message from the server.
    onlineStateTracker.updateState(OnlineState.ONLINE);
    hardAssert((watchStream != null) && (watchChangeAggregator != null), "WatchStream and WatchStreamAggregator should both be non-null");
    WatchTargetChange watchTargetChange = watchChange instanceof WatchTargetChange ? (WatchTargetChange) watchChange : null;
    if (watchTargetChange != null && watchTargetChange.getChangeType().equals(WatchTargetChangeType.Removed) && watchTargetChange.getCause() != null) {
        // There was an error on a target, don't wait for a consistent snapshot to raise events
        processTargetError(watchTargetChange);
    } else {
        if (watchChange instanceof DocumentChange) {
            watchChangeAggregator.handleDocumentChange((DocumentChange) watchChange);
        } else if (watchChange instanceof ExistenceFilterWatchChange) {
            watchChangeAggregator.handleExistenceFilter((ExistenceFilterWatchChange) watchChange);
        } else {
            hardAssert(watchChange instanceof WatchTargetChange, "Expected watchChange to be an instance of WatchTargetChange");
            watchChangeAggregator.handleTargetChange((WatchTargetChange) watchChange);
        }
        if (!snapshotVersion.equals(SnapshotVersion.NONE)) {
            SnapshotVersion lastRemoteSnapshotVersion = this.localStore.getLastRemoteSnapshotVersion();
            if (snapshotVersion.compareTo(lastRemoteSnapshotVersion) >= 0) {
                // We have received a target change with a global snapshot if the snapshot
                // version is not equal to SnapshotVersion.MIN.
                raiseWatchSnapshot(snapshotVersion);
            }
        }
    }
}
Also used : SnapshotVersion(com.google.firebase.firestore.model.SnapshotVersion) DocumentChange(com.google.firebase.firestore.remote.WatchChange.DocumentChange) ExistenceFilterWatchChange(com.google.firebase.firestore.remote.WatchChange.ExistenceFilterWatchChange) WatchTargetChange(com.google.firebase.firestore.remote.WatchChange.WatchTargetChange)

Example 12 with DocumentChange

use of com.google.firebase.firestore.remote.WatchChange.DocumentChange in project firebase-android-sdk by firebase.

the class RemoteEventTest method testWillIgnoreEventsForRemovedTargets.

@Test
public void testWillIgnoreEventsForRemovedTargets() {
    Map<Integer, TargetData> targetMap = activeQueries();
    MutableDocument doc1 = doc("docs/1", 1, map("value", 1));
    // We're waiting for the unwatch ack
    Map<Integer, Integer> outstanding = new HashMap<>();
    outstanding.put(1, 1);
    WatchChange change1 = new DocumentChange(asList(1), emptyList(), doc1.getKey(), doc1);
    WatchChange change2 = new WatchTargetChange(WatchTargetChangeType.Removed, asList(1));
    RemoteEvent event = createRemoteEvent(3, targetMap, outstanding, noExistingKeys, change1, change2);
    assertEquals(version(3), event.getSnapshotVersion());
    // doc1 is ignored because it was not apart of an active target.
    assertEquals(0, event.getDocumentUpdates().size());
    // Target 1 is ignored because it was removed
    assertEquals(0, event.getTargetChanges().size());
}
Also used : TargetData(com.google.firebase.firestore.local.TargetData) HashMap(java.util.HashMap) MutableDocument(com.google.firebase.firestore.model.MutableDocument) DocumentChange(com.google.firebase.firestore.remote.WatchChange.DocumentChange) WatchTargetChange(com.google.firebase.firestore.remote.WatchChange.WatchTargetChange) Test(org.junit.Test)

Example 13 with DocumentChange

use of com.google.firebase.firestore.remote.WatchChange.DocumentChange in project firebase-android-sdk by firebase.

the class RemoteEventTest method testExistenceFilterMismatchClearsTarget.

@Test
public void testExistenceFilterMismatchClearsTarget() {
    Map<Integer, TargetData> targetMap = activeQueries(1, 2);
    MutableDocument doc1 = doc("docs/1", 1, map("value", 1));
    MutableDocument doc2 = doc("docs/2", 2, map("value", 2));
    WatchChange change1 = new DocumentChange(asList(1), emptyList(), doc1.getKey(), doc1);
    WatchChange change2 = new DocumentChange(asList(1), emptyList(), doc2.getKey(), doc2);
    WatchChange change3 = new WatchTargetChange(WatchTargetChangeType.Current, asList(1));
    WatchChangeAggregator aggregator = createAggregator(targetMap, noOutstandingResponses, keySet(doc1.getKey(), doc2.getKey()), change1, change2, change3);
    RemoteEvent event = aggregator.createRemoteEvent(version(3));
    assertEquals(version(3), event.getSnapshotVersion());
    assertEquals(2, event.getDocumentUpdates().size());
    assertEquals(doc1, event.getDocumentUpdates().get(doc1.getKey()));
    assertEquals(doc2, event.getDocumentUpdates().get(doc2.getKey()));
    assertEquals(2, event.getTargetChanges().size());
    TargetChange mapping1 = targetChange(resumeToken, true, null, asList(doc1, doc2), null);
    assertEquals(mapping1, event.getTargetChanges().get(1));
    TargetChange mapping2 = targetChange(resumeToken, false, null, null, null);
    assertEquals(mapping2, event.getTargetChanges().get(2));
    WatchChange.ExistenceFilterWatchChange watchChange = new WatchChange.ExistenceFilterWatchChange(1, new ExistenceFilter(1));
    aggregator.handleExistenceFilter(watchChange);
    event = aggregator.createRemoteEvent(version(3));
    TargetChange mapping3 = targetChange(ByteString.EMPTY, false, null, null, asList(doc1, doc2));
    assertEquals(1, event.getTargetChanges().size());
    assertEquals(mapping3, event.getTargetChanges().get(1));
    assertEquals(1, event.getTargetMismatches().size());
    assertEquals(0, event.getDocumentUpdates().size());
}
Also used : MutableDocument(com.google.firebase.firestore.model.MutableDocument) DocumentChange(com.google.firebase.firestore.remote.WatchChange.DocumentChange) WatchTargetChange(com.google.firebase.firestore.remote.WatchChange.WatchTargetChange) TargetData(com.google.firebase.firestore.local.TargetData) WatchTargetChange(com.google.firebase.firestore.remote.WatchChange.WatchTargetChange) Test(org.junit.Test)

Example 14 with DocumentChange

use of com.google.firebase.firestore.remote.WatchChange.DocumentChange in project firebase-android-sdk by firebase.

the class RemoteEventTest method testWillIgnoreEventsForPendingTargets.

@Test
public void testWillIgnoreEventsForPendingTargets() {
    Map<Integer, TargetData> targetMap = activeQueries(1);
    MutableDocument doc1 = doc("docs/1", 1, map("value", 1));
    MutableDocument doc2 = doc("docs/2", 2, map("value", 2));
    // We're waiting for the watch and unwatch ack
    Map<Integer, Integer> outstanding = new HashMap<>();
    outstanding.put(1, 2);
    WatchChange change1 = new DocumentChange(asList(1), emptyList(), doc1.getKey(), doc1);
    WatchChange change2 = new WatchTargetChange(WatchTargetChangeType.Removed, asList(1));
    WatchChange change3 = new WatchTargetChange(WatchTargetChangeType.Added, asList(1));
    WatchChange change4 = new DocumentChange(asList(1), emptyList(), doc2.getKey(), doc2);
    RemoteEvent event = createRemoteEvent(3, targetMap, outstanding, noExistingKeys, change1, change2, change3, change4);
    assertEquals(version(3), event.getSnapshotVersion());
    // doc1 is ignored because the target was not active at the time, but for
    // doc2 the target is active.
    assertEquals(1, event.getDocumentUpdates().size());
    assertEquals(doc2, event.getDocumentUpdates().get(doc2.getKey()));
    assertEquals(1, event.getTargetChanges().size());
}
Also used : TargetData(com.google.firebase.firestore.local.TargetData) HashMap(java.util.HashMap) MutableDocument(com.google.firebase.firestore.model.MutableDocument) DocumentChange(com.google.firebase.firestore.remote.WatchChange.DocumentChange) WatchTargetChange(com.google.firebase.firestore.remote.WatchChange.WatchTargetChange) Test(org.junit.Test)

Example 15 with DocumentChange

use of com.google.firebase.firestore.remote.WatchChange.DocumentChange in project firebase-android-sdk by firebase.

the class RemoteEventTest method testDocumentUpdate.

@Test
public void testDocumentUpdate() {
    Map<Integer, TargetData> targetMap = activeQueries(1);
    MutableDocument doc1 = doc("docs/1", 1, map("value", 1));
    WatchChange change1 = new DocumentChange(asList(1), emptyList(), doc1.getKey(), doc1);
    MutableDocument doc2 = doc("docs/2", 2, map("value", 2));
    WatchChange change2 = new DocumentChange(asList(1), emptyList(), doc2.getKey(), doc2);
    WatchChangeAggregator aggregator = createAggregator(targetMap, noOutstandingResponses, noExistingKeys, change1, change2);
    RemoteEvent event = aggregator.createRemoteEvent(version(3));
    assertEquals(version(3), event.getSnapshotVersion());
    assertEquals(2, event.getDocumentUpdates().size());
    assertEquals(doc1, event.getDocumentUpdates().get(doc1.getKey()));
    assertEquals(doc2, event.getDocumentUpdates().get(doc2.getKey()));
    targetMetadataProvider.setSyncedKeys(targetMap.get(1), keySet(doc1.getKey(), doc2.getKey()));
    MutableDocument deletedDoc1 = deletedDoc("docs/1", 3);
    DocumentChange change3 = new DocumentChange(asList(1), emptyList(), deletedDoc1.getKey(), deletedDoc1);
    aggregator.handleDocumentChange(change3);
    MutableDocument updatedDoc2 = doc("docs/2", 3, map("value", 3));
    DocumentChange change4 = new DocumentChange(asList(1), emptyList(), updatedDoc2.getKey(), updatedDoc2);
    aggregator.handleDocumentChange(change4);
    MutableDocument doc3 = doc("docs/3", 3, map("value", 3));
    DocumentChange change5 = new DocumentChange(asList(1), emptyList(), doc3.getKey(), doc3);
    aggregator.handleDocumentChange(change5);
    event = aggregator.createRemoteEvent(version(3));
    assertEquals(version(3), event.getSnapshotVersion());
    assertEquals(3, event.getDocumentUpdates().size());
    // doc1 is replaced
    assertEquals(deletedDoc1, event.getDocumentUpdates().get(doc1.getKey()));
    // doc2 is updated
    assertEquals(updatedDoc2, event.getDocumentUpdates().get(doc2.getKey()));
    // doc3 is new
    assertEquals(doc3, event.getDocumentUpdates().get(doc3.getKey()));
    // Target is unchanged
    assertEquals(1, event.getTargetChanges().size());
    TargetChange mapping1 = targetChange(resumeToken, false, asList(doc3), asList(updatedDoc2), asList(deletedDoc1));
    assertEquals(mapping1, event.getTargetChanges().get(1));
}
Also used : TargetData(com.google.firebase.firestore.local.TargetData) WatchTargetChange(com.google.firebase.firestore.remote.WatchChange.WatchTargetChange) MutableDocument(com.google.firebase.firestore.model.MutableDocument) DocumentChange(com.google.firebase.firestore.remote.WatchChange.DocumentChange) Test(org.junit.Test)

Aggregations

DocumentChange (com.google.firebase.firestore.remote.WatchChange.DocumentChange)16 TargetData (com.google.firebase.firestore.local.TargetData)14 MutableDocument (com.google.firebase.firestore.model.MutableDocument)13 WatchTargetChange (com.google.firebase.firestore.remote.WatchChange.WatchTargetChange)13 Test (org.junit.Test)11 HashMap (java.util.HashMap)5 ImmutableSortedSet (com.google.firebase.database.collection.ImmutableSortedSet)2 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)2 ExistenceFilterWatchChange (com.google.firebase.firestore.remote.WatchChange.ExistenceFilterWatchChange)2 WatchChangeAggregator (com.google.firebase.firestore.remote.WatchChangeAggregator)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 DocumentKey (com.google.firebase.firestore.model.DocumentKey)1 ResourcePath (com.google.firebase.firestore.model.ResourcePath)1 WatchChange (com.google.firebase.firestore.remote.WatchChange)1 ByteString (com.google.protobuf.ByteString)1 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1