Search in sources :

Example 16 with SnapshotVersion

use of com.google.firebase.firestore.model.SnapshotVersion 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 17 with SnapshotVersion

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

the class WatchStream method onNext.

@Override
public void onNext(com.google.firestore.v1.ListenResponse listenResponse) {
    // A successful response means the stream is healthy
    backoff.reset();
    WatchChange watchChange = serializer.decodeWatchChange(listenResponse);
    SnapshotVersion snapshotVersion = serializer.decodeVersionFromListenResponse(listenResponse);
    listener.onWatchChange(snapshotVersion, watchChange);
}
Also used : SnapshotVersion(com.google.firebase.firestore.model.SnapshotVersion)

Example 18 with SnapshotVersion

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

the class Datastore method commit.

public Task<List<MutationResult>> commit(List<Mutation> mutations) {
    CommitRequest.Builder builder = CommitRequest.newBuilder();
    builder.setDatabase(serializer.databaseName());
    for (Mutation mutation : mutations) {
        builder.addWrites(serializer.encodeMutation(mutation));
    }
    return channel.runRpc(FirestoreGrpc.getCommitMethod(), builder.build()).continueWith(workerQueue.getExecutor(), task -> {
        if (!task.isSuccessful()) {
            if (task.getException() instanceof FirebaseFirestoreException && ((FirebaseFirestoreException) task.getException()).getCode() == FirebaseFirestoreException.Code.UNAUTHENTICATED) {
                channel.invalidateToken();
            }
            throw task.getException();
        }
        CommitResponse response = task.getResult();
        SnapshotVersion commitVersion = serializer.decodeVersion(response.getCommitTime());
        int count = response.getWriteResultsCount();
        ArrayList<MutationResult> results = new ArrayList<>(count);
        for (int i = 0; i < count; i++) {
            com.google.firestore.v1.WriteResult result = response.getWriteResults(i);
            results.add(serializer.decodeMutationResult(result, commitVersion));
        }
        return results;
    });
}
Also used : CommitRequest(com.google.firestore.v1.CommitRequest) ArrayList(java.util.ArrayList) FirebaseFirestoreException(com.google.firebase.firestore.FirebaseFirestoreException) CommitResponse(com.google.firestore.v1.CommitResponse) SnapshotVersion(com.google.firebase.firestore.model.SnapshotVersion) Mutation(com.google.firebase.firestore.model.mutation.Mutation) MutationResult(com.google.firebase.firestore.model.mutation.MutationResult)

Example 19 with SnapshotVersion

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

the class MutationBatchResult method create.

/**
 * Creates a new MutationBatchResult for the given batch and results. There must be one result for
 * each mutation in the batch. This static factory caches a document=>version mapping (as
 * docVersions).
 */
public static MutationBatchResult create(MutationBatch batch, SnapshotVersion commitVersion, List<MutationResult> mutationResults, ByteString streamToken) {
    Assert.hardAssert(batch.getMutations().size() == mutationResults.size(), "Mutations sent %d must equal results received %d", batch.getMutations().size(), mutationResults.size());
    ImmutableSortedMap<DocumentKey, SnapshotVersion> docVersions = DocumentCollections.emptyVersionMap();
    List<Mutation> mutations = batch.getMutations();
    for (int i = 0; i < mutations.size(); i++) {
        SnapshotVersion version = mutationResults.get(i).getVersion();
        docVersions = docVersions.insert(mutations.get(i).getKey(), version);
    }
    return new MutationBatchResult(batch, commitVersion, mutationResults, streamToken, docVersions);
}
Also used : SnapshotVersion(com.google.firebase.firestore.model.SnapshotVersion) DocumentKey(com.google.firebase.firestore.model.DocumentKey)

Example 20 with SnapshotVersion

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

the class LocalSerializer method decodeDocument.

/**
 * Decodes a Document proto to the equivalent model.
 */
private MutableDocument decodeDocument(com.google.firestore.v1.Document document, boolean hasCommittedMutations) {
    DocumentKey key = rpcSerializer.decodeKey(document.getName());
    SnapshotVersion version = rpcSerializer.decodeVersion(document.getUpdateTime());
    MutableDocument result = MutableDocument.newFoundDocument(key, version, ObjectValue.fromMap(document.getFieldsMap()));
    return hasCommittedMutations ? result.setHasCommittedMutations() : result;
}
Also used : SnapshotVersion(com.google.firebase.firestore.model.SnapshotVersion) DocumentKey(com.google.firebase.firestore.model.DocumentKey) MutableDocument(com.google.firebase.firestore.model.MutableDocument)

Aggregations

SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)43 DocumentKey (com.google.firebase.firestore.model.DocumentKey)16 Test (org.junit.Test)13 MutableDocument (com.google.firebase.firestore.model.MutableDocument)9 Target (com.google.firebase.firestore.core.Target)8 BundledQuery (com.google.firebase.firestore.bundle.BundledQuery)7 ByteString (com.google.protobuf.ByteString)7 NamedQuery (com.google.firebase.firestore.bundle.NamedQuery)6 ArrayList (java.util.ArrayList)6 Timestamp (com.google.firebase.Timestamp)5 ObjectValue (com.google.firebase.firestore.model.ObjectValue)5 MutationResult (com.google.firebase.firestore.model.mutation.MutationResult)4 JSONObject (org.json.JSONObject)4 Query (com.google.firebase.firestore.core.Query)3 Timestamp (com.google.protobuf.Timestamp)3 ResourcePath (com.google.firebase.firestore.model.ResourcePath)2 MutationBatch (com.google.firebase.firestore.model.mutation.MutationBatch)2 DocumentChange (com.google.firebase.firestore.remote.WatchChange.DocumentChange)2 ExistenceFilterWatchChange (com.google.firebase.firestore.remote.WatchChange.ExistenceFilterWatchChange)2 WatchTargetChange (com.google.firebase.firestore.remote.WatchChange.WatchTargetChange)2