use of com.google.firebase.firestore.model.SnapshotVersion in project firebase-android-sdk by firebase.
the class RemoteSerializer method decodeWatchChange.
// Watch changes
public WatchChange decodeWatchChange(ListenResponse protoChange) {
WatchChange watchChange;
switch(protoChange.getResponseTypeCase()) {
case TARGET_CHANGE:
com.google.firestore.v1.TargetChange targetChange = protoChange.getTargetChange();
WatchTargetChangeType changeType;
Status cause = null;
switch(targetChange.getTargetChangeType()) {
case NO_CHANGE:
changeType = WatchTargetChangeType.NoChange;
break;
case ADD:
changeType = WatchTargetChangeType.Added;
break;
case REMOVE:
changeType = WatchTargetChangeType.Removed;
cause = fromStatus(targetChange.getCause());
break;
case CURRENT:
changeType = WatchTargetChangeType.Current;
break;
case RESET:
changeType = WatchTargetChangeType.Reset;
break;
case UNRECOGNIZED:
default:
throw new IllegalArgumentException("Unknown target change type");
}
watchChange = new WatchTargetChange(changeType, targetChange.getTargetIdsList(), targetChange.getResumeToken(), cause);
break;
case DOCUMENT_CHANGE:
DocumentChange docChange = protoChange.getDocumentChange();
List<Integer> added = docChange.getTargetIdsList();
List<Integer> removed = docChange.getRemovedTargetIdsList();
DocumentKey key = decodeKey(docChange.getDocument().getName());
SnapshotVersion version = decodeVersion(docChange.getDocument().getUpdateTime());
hardAssert(!version.equals(SnapshotVersion.NONE), "Got a document change without an update time");
ObjectValue data = ObjectValue.fromMap(docChange.getDocument().getFieldsMap());
MutableDocument document = MutableDocument.newFoundDocument(key, version, data);
watchChange = new WatchChange.DocumentChange(added, removed, document.getKey(), document);
break;
case DOCUMENT_DELETE:
DocumentDelete docDelete = protoChange.getDocumentDelete();
removed = docDelete.getRemovedTargetIdsList();
key = decodeKey(docDelete.getDocument());
// Note that version might be unset in which case we use SnapshotVersion.NONE
version = decodeVersion(docDelete.getReadTime());
MutableDocument doc = MutableDocument.newNoDocument(key, version);
watchChange = new WatchChange.DocumentChange(Collections.emptyList(), removed, doc.getKey(), doc);
break;
case DOCUMENT_REMOVE:
DocumentRemove docRemove = protoChange.getDocumentRemove();
removed = docRemove.getRemovedTargetIdsList();
key = decodeKey(docRemove.getDocument());
watchChange = new WatchChange.DocumentChange(Collections.emptyList(), removed, key, null);
break;
case FILTER:
com.google.firestore.v1.ExistenceFilter protoFilter = protoChange.getFilter();
// TODO: implement existence filter parsing (see b/33076578)
ExistenceFilter filter = new ExistenceFilter(protoFilter.getCount());
int targetId = protoFilter.getTargetId();
watchChange = new ExistenceFilterWatchChange(targetId, filter);
break;
case RESPONSETYPE_NOT_SET:
default:
throw new IllegalArgumentException("Unknown change type set");
}
return watchChange;
}
use of com.google.firebase.firestore.model.SnapshotVersion in project firebase-android-sdk by firebase.
the class WriteStream method onNext.
@Override
public void onNext(WriteResponse response) {
lastStreamToken = response.getStreamToken();
if (!handshakeComplete) {
// The first response is the handshake response
handshakeComplete = true;
listener.onHandshakeComplete();
} else {
// A successful first write response means the stream is healthy,
// Note, that we could consider a successful handshake healthy, however,
// the write itself might be causing an error we want to back off from.
backoff.reset();
SnapshotVersion commitVersion = serializer.decodeVersion(response.getCommitTime());
int count = response.getWriteResultsCount();
List<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));
}
listener.onWriteResponse(commitVersion, results);
}
}
use of com.google.firebase.firestore.model.SnapshotVersion in project firebase-android-sdk by firebase.
the class LocalSerializerTest method testEncodesTargetData.
@Test
public void testEncodesTargetData() {
Query query = TestUtil.query("room");
int targetId = 42;
long sequenceNumber = 10;
SnapshotVersion snapshotVersion = TestUtil.version(1039);
SnapshotVersion limboFreeVersion = TestUtil.version(1000);
ByteString resumeToken = TestUtil.resumeToken(1039);
TargetData targetData = new TargetData(query.toTarget(), targetId, sequenceNumber, QueryPurpose.LISTEN, snapshotVersion, limboFreeVersion, resumeToken);
// Let the RPC serializer test various permutations of query serialization.
com.google.firestore.v1.Target.QueryTarget queryTarget = remoteSerializer.encodeQueryTarget(query.toTarget());
com.google.firebase.firestore.proto.Target expected = com.google.firebase.firestore.proto.Target.newBuilder().setTargetId(targetId).setLastListenSequenceNumber(sequenceNumber).setSnapshotVersion(com.google.protobuf.Timestamp.newBuilder().setNanos(1039000)).setResumeToken(ByteString.copyFrom(resumeToken.toByteArray())).setQuery(com.google.firestore.v1.Target.QueryTarget.newBuilder().setParent(queryTarget.getParent()).setStructuredQuery(queryTarget.getStructuredQuery())).setLastLimboFreeSnapshotVersion(com.google.protobuf.Timestamp.newBuilder().setNanos(1000000)).build();
assertEquals(expected, serializer.encodeTargetData(targetData));
TargetData decoded = serializer.decodeTargetData(expected);
assertEquals(targetData, decoded);
}
use of com.google.firebase.firestore.model.SnapshotVersion in project firebase-android-sdk by firebase.
the class LocalStoreTestCase method acknowledgeMutationWithTransformResults.
private void acknowledgeMutationWithTransformResults(long documentVersion, Object... transformResult) {
MutationBatch batch = batches.remove(0);
SnapshotVersion version = version(documentVersion);
List<MutationResult> mutationResults = Collections.singletonList(new MutationResult(version, emptyList()));
if (transformResult.length != 0) {
mutationResults = Arrays.stream(transformResult).map(r -> new MutationResult(version, Collections.singletonList(TestUtil.wrap(r)))).collect(Collectors.toList());
}
MutationBatchResult result = MutationBatchResult.create(batch, version, mutationResults, WriteStream.EMPTY_STREAM_TOKEN);
lastChanges = localStore.acknowledgeBatch(result);
}
use of com.google.firebase.firestore.model.SnapshotVersion in project firebase-android-sdk by firebase.
the class LocalStoreTestCase method testHoldsBackTransforms.
@Test
public void testHoldsBackTransforms() {
Query query = query("foo");
allocateQuery(query);
assertTargetId(2);
writeMutation(setMutation("foo/bar", map("sum", 0, "array_union", new ArrayList<>())));
assertChanged(doc("foo/bar", 0, map("sum", 0, "array_union", new ArrayList<>())).setHasLocalMutations());
acknowledgeMutation(1);
assertChanged(doc("foo/bar", 1, map("sum", 0, "array_union", new ArrayList<>())).setHasCommittedMutations());
applyRemoteEvent(addedRemoteEvent(doc("foo/bar", 1, map("sum", 0, "array_union", new ArrayList<>())), asList(2), emptyList()));
assertChanged(doc("foo/bar", 1, map("sum", 0, "array_union", new ArrayList<>())));
writeMutation(patchMutation("foo/bar", map("sum", FieldValue.increment(1))));
assertChanged(doc("foo/bar", 1, map("sum", 1, "array_union", new ArrayList<>())).setHasLocalMutations());
writeMutation(patchMutation("foo/bar", map("array_union", FieldValue.arrayUnion("foo"))));
assertChanged(doc("foo/bar", 1, map("sum", 1, "array_union", Collections.singletonList("foo"))).setHasLocalMutations());
// The sum transform and array union transform make the SDK ignore the
// backend's updated value.
applyRemoteEvent(addedRemoteEvent(doc("foo/bar", 2, map("sum", 1337, "array_union", Collections.singletonList("bar"))), asList(2), emptyList()));
assertChanged(doc("foo/bar", 2, map("sum", 1, "array_union", asList("foo"))).setHasLocalMutations());
// With a field transform acknowledgement, the overlay is recalculated with
// remaining local mutations.
acknowledgeMutationWithTransformResults(3, 1338);
assertChanged(doc("foo/bar", 3, map("sum", 1338, "array_union", asList("bar", "foo"))).setReadTime(new SnapshotVersion(new Timestamp(0, 3000))).setHasLocalMutations());
acknowledgeMutationWithTransformResults(4, asList("bar", "foo"));
assertChanged(doc("foo/bar", 4, map("sum", 1338, "array_union", asList("bar", "foo"))).setReadTime(new SnapshotVersion(new Timestamp(0, 4000))).setHasCommittedMutations());
}
Aggregations