Search in sources :

Example 21 with TargetData

use of com.google.firebase.firestore.local.TargetData in project firebase-android-sdk by firebase.

the class TestUtil method addedRemoteEvent.

public static RemoteEvent addedRemoteEvent(List<MutableDocument> docs, List<Integer> updatedInTargets, List<Integer> removedFromTargets) {
    Preconditions.checkArgument(!docs.isEmpty(), "Cannot pass empty docs array");
    WatchChangeAggregator aggregator = new WatchChangeAggregator(new WatchChangeAggregator.TargetMetadataProvider() {

        @Override
        public ImmutableSortedSet<DocumentKey> getRemoteKeysForTarget(int targetId) {
            return DocumentKey.emptyKeySet();
        }

        @Override
        public TargetData getTargetDataForTarget(int targetId) {
            ResourcePath collectionPath = docs.get(0).getKey().getCollectionPath();
            return targetData(targetId, QueryPurpose.LISTEN, collectionPath.toString());
        }
    });
    SnapshotVersion version = SnapshotVersion.NONE;
    for (MutableDocument doc : docs) {
        DocumentChange change = new DocumentChange(updatedInTargets, removedFromTargets, doc.getKey(), doc);
        aggregator.handleDocumentChange(change);
        version = doc.getVersion().compareTo(version) > 0 ? doc.getVersion() : version;
    }
    return aggregator.createRemoteEvent(version);
}
Also used : TargetData(com.google.firebase.firestore.local.TargetData) WatchChangeAggregator(com.google.firebase.firestore.remote.WatchChangeAggregator) ResourcePath(com.google.firebase.firestore.model.ResourcePath) SnapshotVersion(com.google.firebase.firestore.model.SnapshotVersion) ImmutableSortedSet(com.google.firebase.database.collection.ImmutableSortedSet) MutableDocument(com.google.firebase.firestore.model.MutableDocument) DocumentChange(com.google.firebase.firestore.remote.WatchChange.DocumentChange)

Example 22 with TargetData

use of com.google.firebase.firestore.local.TargetData in project firebase-android-sdk by firebase.

the class TestUtil method noChangeEvent.

public static RemoteEvent noChangeEvent(int targetId, int version, ByteString resumeToken) {
    TargetData targetData = TestUtil.targetData(targetId, QueryPurpose.LISTEN, "foo/bar");
    TestTargetMetadataProvider testTargetMetadataProvider = new TestTargetMetadataProvider();
    testTargetMetadataProvider.setSyncedKeys(targetData, DocumentKey.emptyKeySet());
    WatchChangeAggregator aggregator = new WatchChangeAggregator(testTargetMetadataProvider);
    WatchChange.WatchTargetChange watchChange = new WatchChange.WatchTargetChange(WatchChange.WatchTargetChangeType.NoChange, asList(targetId), resumeToken);
    aggregator.handleTargetChange(watchChange);
    return aggregator.createRemoteEvent(version(version));
}
Also used : TargetData(com.google.firebase.firestore.local.TargetData) WatchChangeAggregator(com.google.firebase.firestore.remote.WatchChangeAggregator) WatchChange(com.google.firebase.firestore.remote.WatchChange)

Example 23 with TargetData

use of com.google.firebase.firestore.local.TargetData in project firebase-android-sdk by firebase.

the class RemoteStore method stopListening.

/**
 * Stops listening to the target with the given target ID.
 *
 * <p>It is an error if the given target id is not being listened to.
 *
 * <p>If this is called with the last active targetId, the watch stream enters idle mode and will
 * be torn down after one minute of inactivity.
 */
public void stopListening(int targetId) {
    TargetData targetData = listenTargets.remove(targetId);
    hardAssert(targetData != null, "stopListening called on target no currently watched: %d", targetId);
    // The watch stream might not be started if we're in a disconnected state
    if (watchStream.isOpen()) {
        sendUnwatchRequest(targetId);
    }
    if (listenTargets.isEmpty()) {
        if (watchStream.isOpen()) {
            watchStream.markIdle();
        } else if (this.canUseNetwork()) {
            // Revert to OnlineState.UNKNOWN if the watch stream is not open and we have no listeners,
            // since without any listens to send we cannot confirm if the stream is healthy and upgrade
            // to OnlineState.ONLINE.
            this.onlineStateTracker.updateState(OnlineState.UNKNOWN);
        }
    }
}
Also used : TargetData(com.google.firebase.firestore.local.TargetData)

Example 24 with TargetData

use of com.google.firebase.firestore.local.TargetData in project firebase-android-sdk by firebase.

the class SyncEngine method pumpEnqueuedLimboResolutions.

/**
 * Starts listens for documents in limbo that are enqueued for resolution, subject to a maximum
 * number of concurrent resolutions.
 *
 * <p>Without bounding the number of concurrent resolutions, the server can fail with "resource
 * exhausted" errors which can lead to pathological client behavior as seen in
 * https://github.com/firebase/firebase-js-sdk/issues/2683.
 */
private void pumpEnqueuedLimboResolutions() {
    while (!enqueuedLimboResolutions.isEmpty() && activeLimboTargetsByKey.size() < maxConcurrentLimboResolutions) {
        Iterator<DocumentKey> it = enqueuedLimboResolutions.iterator();
        DocumentKey key = it.next();
        it.remove();
        int limboTargetId = targetIdGenerator.nextId();
        activeLimboResolutionsByTarget.put(limboTargetId, new LimboResolution(key));
        activeLimboTargetsByKey.put(key, limboTargetId);
        remoteStore.listen(new TargetData(Query.atPath(key.getPath()).toTarget(), limboTargetId, ListenSequence.INVALID, QueryPurpose.LIMBO_RESOLUTION));
    }
}
Also used : TargetData(com.google.firebase.firestore.local.TargetData) DocumentKey(com.google.firebase.firestore.model.DocumentKey)

Example 25 with TargetData

use of com.google.firebase.firestore.local.TargetData in project firebase-android-sdk by firebase.

the class SyncEngine method listen.

/**
 * Initiates a new listen. The LocalStore will be queried for initial data and the listen will be
 * sent to the RemoteStore to get remote data. The registered SyncEngineCallback will be notified
 * of resulting view snapshots and/or listen errors.
 *
 * @return the target ID assigned to the query.
 */
public int listen(Query query) {
    assertCallback("listen");
    hardAssert(!queryViewsByQuery.containsKey(query), "We already listen to query: %s", query);
    TargetData targetData = localStore.allocateTarget(query.toTarget());
    remoteStore.listen(targetData);
    ViewSnapshot viewSnapshot = initializeViewAndComputeSnapshot(query, targetData.getTargetId());
    syncEngineListener.onViewSnapshots(Collections.singletonList(viewSnapshot));
    return targetData.getTargetId();
}
Also used : TargetData(com.google.firebase.firestore.local.TargetData)

Aggregations

TargetData (com.google.firebase.firestore.local.TargetData)38 Test (org.junit.Test)23 WatchTargetChange (com.google.firebase.firestore.remote.WatchChange.WatchTargetChange)22 MutableDocument (com.google.firebase.firestore.model.MutableDocument)15 DocumentChange (com.google.firebase.firestore.remote.WatchChange.DocumentChange)14 HashMap (java.util.HashMap)8 Query (com.google.firebase.firestore.core.Query)7 StructuredQuery (com.google.firestore.v1.StructuredQuery)6 DocumentKey (com.google.firebase.firestore.model.DocumentKey)5 WatchChangeAggregator (com.google.firebase.firestore.remote.WatchChangeAggregator)4 ByteString (com.google.protobuf.ByteString)4 ImmutableSortedSet (com.google.firebase.database.collection.ImmutableSortedSet)3 Target (com.google.firestore.v1.Target)3 DocumentsTarget (com.google.firestore.v1.Target.DocumentsTarget)3 QueryTarget (com.google.firestore.v1.Target.QueryTarget)3 Map (java.util.Map)3 WatchChange (com.google.firebase.firestore.remote.WatchChange)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 Target (com.google.firebase.firestore.core.Target)1