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);
}
}
}
}
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);
}
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;
});
}
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);
}
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;
}
Aggregations