Search in sources :

Example 1 with WatchTargetChange

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

the class SpecTestCase method doWatchReset.

private void doWatchReset(JSONArray targetIds) throws Exception {
    List<Integer> targets = parseIntList(targetIds);
    WatchChange change = new WatchTargetChange(WatchTargetChangeType.Reset, targets);
    writeWatchChange(change, SnapshotVersion.NONE);
}
Also used : WatchChange(com.google.firebase.firestore.remote.WatchChange) ExistenceFilterWatchChange(com.google.firebase.firestore.remote.WatchChange.ExistenceFilterWatchChange) WatchTargetChange(com.google.firebase.firestore.remote.WatchChange.WatchTargetChange)

Example 2 with WatchTargetChange

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

the class SpecTestCase method doWatchCurrent.

private void doWatchCurrent(JSONArray currentSpec) throws Exception {
    List<Integer> currentTargets = parseIntList(currentSpec.getJSONArray(0));
    ByteString resumeToken = ByteString.copyFromUtf8(currentSpec.getString(1));
    WatchTargetChange change = new WatchTargetChange(WatchTargetChangeType.Current, currentTargets, resumeToken);
    writeWatchChange(change, SnapshotVersion.NONE);
}
Also used : ByteString(com.google.protobuf.ByteString) WatchTargetChange(com.google.firebase.firestore.remote.WatchChange.WatchTargetChange)

Example 3 with WatchTargetChange

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

the class SpecTestCase method doWatchSnapshot.

private void doWatchSnapshot(JSONObject watchSnapshot) throws Exception {
    // The client will only respond to watchSnapshots if they are on a target change with an empty
    // set of target IDs.
    List<Integer> targets = watchSnapshot.has("targetIds") ? parseIntList(watchSnapshot.getJSONArray("targetIds")) : Collections.emptyList();
    String resumeToken = watchSnapshot.optString("resumeToken");
    WatchChange change = new WatchTargetChange(WatchTargetChangeType.NoChange, targets, ByteString.copyFromUtf8(resumeToken));
    writeWatchChange(change, version(watchSnapshot.getLong("version")));
}
Also used : WatchChange(com.google.firebase.firestore.remote.WatchChange) ExistenceFilterWatchChange(com.google.firebase.firestore.remote.WatchChange.ExistenceFilterWatchChange) ByteString(com.google.protobuf.ByteString) WatchTargetChange(com.google.firebase.firestore.remote.WatchChange.WatchTargetChange)

Example 4 with WatchTargetChange

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

the class RemoteEventTest method createAggregator.

/**
 * Creates an aggregator initialized with the set of provided WatchChanges. Tests can add further
 * changes via `handleDocumentChange`, `handleTargetChange` and `handleExistenceFilterChange`.
 *
 * @param targetMap A map of query data for all active targets. The map must include an entry for
 *     every target referenced by any of the watch changes.
 * @param outstandingResponses The number of outstanding ACKs a target has to receive before it is
 *     considered active, or `noOutstandingResponses` if all targets are already active.
 * @param existingKeys The set of documents that are considered synced with the test targets as
 *     part of a previous listen. To modify this set during test execution, invoke
 *     `targetMetadataProvider.setSyncedKeys()`.
 * @param watchChanges The watch changes to apply before returning the aggregator. Supported
 *     changes are DocumentWatchChange and WatchTargetChange.
 */
private WatchChangeAggregator createAggregator(Map<Integer, TargetData> targetMap, Map<Integer, Integer> outstandingResponses, ImmutableSortedSet<DocumentKey> existingKeys, WatchChange... watchChanges) {
    WatchChangeAggregator aggregator = new WatchChangeAggregator(targetMetadataProvider);
    List<Integer> targetIds = new ArrayList<>();
    for (Map.Entry<Integer, TargetData> entry : targetMap.entrySet()) {
        targetIds.add(entry.getKey());
        targetMetadataProvider.setSyncedKeys(entry.getValue(), existingKeys);
    }
    for (Map.Entry<Integer, Integer> entry : outstandingResponses.entrySet()) {
        for (int i = 0; i < entry.getValue(); ++i) {
            aggregator.recordPendingTargetRequest(entry.getKey());
        }
    }
    for (WatchChange watchChange : watchChanges) {
        if (watchChange instanceof DocumentChange) {
            aggregator.handleDocumentChange((DocumentChange) watchChange);
        } else if (watchChange instanceof WatchTargetChange) {
            aggregator.handleTargetChange((WatchTargetChange) watchChange);
        } else {
            fail("Encountered unexpected type of WatchChange");
        }
    }
    aggregator.handleTargetChange(new WatchTargetChange(WatchTargetChangeType.NoChange, targetIds, resumeToken));
    return aggregator;
}
Also used : TargetData(com.google.firebase.firestore.local.TargetData) ArrayList(java.util.ArrayList) DocumentChange(com.google.firebase.firestore.remote.WatchChange.DocumentChange) HashMap(java.util.HashMap) Map(java.util.Map) WatchTargetChange(com.google.firebase.firestore.remote.WatchChange.WatchTargetChange)

Example 5 with WatchTargetChange

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

the class RemoteEventTest method testExistenceFilterMismatchRemovesCurrentChanges.

@Test
public void testExistenceFilterMismatchRemovesCurrentChanges() {
    Map<Integer, TargetData> targetMap = activeQueries(1);
    WatchChangeAggregator aggregator = createAggregator(targetMap, noOutstandingResponses, noExistingKeys);
    WatchTargetChange markCurrent = new WatchTargetChange(WatchTargetChangeType.Current, asList(1));
    aggregator.handleTargetChange(markCurrent);
    MutableDocument doc1 = doc("docs/1", 1, map("value", 1));
    DocumentChange addDoc = new DocumentChange(asList(1), emptyList(), doc1.getKey(), doc1);
    aggregator.handleDocumentChange(addDoc);
    // The existence filter mismatch will remove the document from target 1, but not synthesize a
    // document delete.
    WatchChange.ExistenceFilterWatchChange existenceFilter = new WatchChange.ExistenceFilterWatchChange(1, new ExistenceFilter(0));
    aggregator.handleExistenceFilter(existenceFilter);
    RemoteEvent event = aggregator.createRemoteEvent(version(3));
    assertEquals(version(3), event.getSnapshotVersion());
    assertEquals(1, event.getDocumentUpdates().size());
    assertEquals(1, event.getTargetMismatches().size());
    assertEquals(doc1, event.getDocumentUpdates().get(doc1.getKey()));
    assertEquals(1, event.getTargetChanges().size());
    TargetChange mapping1 = targetChange(ByteString.EMPTY, false, null, null, null);
    assertEquals(mapping1, event.getTargetChanges().get(1));
}
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)

Aggregations

WatchTargetChange (com.google.firebase.firestore.remote.WatchChange.WatchTargetChange)26 Test (org.junit.Test)18 TargetData (com.google.firebase.firestore.local.TargetData)16 MutableDocument (com.google.firebase.firestore.model.MutableDocument)9 DocumentChange (com.google.firebase.firestore.remote.WatchChange.DocumentChange)9 ExistenceFilterWatchChange (com.google.firebase.firestore.remote.WatchChange.ExistenceFilterWatchChange)4 ByteString (com.google.protobuf.ByteString)4 HashMap (java.util.HashMap)4 DocumentKey (com.google.firebase.firestore.model.DocumentKey)3 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)2 WatchChange (com.google.firebase.firestore.remote.WatchChange)2 Status (io.grpc.Status)2 ObjectValue (com.google.firebase.firestore.model.ObjectValue)1 WatchTargetChangeType (com.google.firebase.firestore.remote.WatchChange.WatchTargetChangeType)1 DocumentChange (com.google.firestore.v1.DocumentChange)1 DocumentDelete (com.google.firestore.v1.DocumentDelete)1 DocumentRemove (com.google.firestore.v1.DocumentRemove)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 JSONObject (org.json.JSONObject)1