Search in sources :

Example 36 with SnapshotVersion

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;
}
Also used : Status(io.grpc.Status) DocumentRemove(com.google.firestore.v1.DocumentRemove) MutableDocument(com.google.firebase.firestore.model.MutableDocument) WatchTargetChangeType(com.google.firebase.firestore.remote.WatchChange.WatchTargetChangeType) DocumentChange(com.google.firestore.v1.DocumentChange) WatchTargetChange(com.google.firebase.firestore.remote.WatchChange.WatchTargetChange) DocumentDelete(com.google.firestore.v1.DocumentDelete) ObjectValue(com.google.firebase.firestore.model.ObjectValue) SnapshotVersion(com.google.firebase.firestore.model.SnapshotVersion) ExistenceFilterWatchChange(com.google.firebase.firestore.remote.WatchChange.ExistenceFilterWatchChange) DocumentKey(com.google.firebase.firestore.model.DocumentKey) ExistenceFilterWatchChange(com.google.firebase.firestore.remote.WatchChange.ExistenceFilterWatchChange)

Example 37 with SnapshotVersion

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);
    }
}
Also used : SnapshotVersion(com.google.firebase.firestore.model.SnapshotVersion) ArrayList(java.util.ArrayList) MutationResult(com.google.firebase.firestore.model.mutation.MutationResult)

Example 38 with SnapshotVersion

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);
}
Also used : BundledQuery(com.google.firebase.firestore.bundle.BundledQuery) Query(com.google.firebase.firestore.core.Query) ByteString(com.google.protobuf.ByteString) Target(com.google.firebase.firestore.core.Target) SnapshotVersion(com.google.firebase.firestore.model.SnapshotVersion) Test(org.junit.Test)

Example 39 with SnapshotVersion

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);
}
Also used : MutationBatchResult(com.google.firebase.firestore.model.mutation.MutationBatchResult) MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch) SnapshotVersion(com.google.firebase.firestore.model.SnapshotVersion) MutationResult(com.google.firebase.firestore.model.mutation.MutationResult)

Example 40 with SnapshotVersion

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());
}
Also used : BundledQuery(com.google.firebase.firestore.bundle.BundledQuery) NamedQuery(com.google.firebase.firestore.bundle.NamedQuery) Query(com.google.firebase.firestore.core.Query) SnapshotVersion(com.google.firebase.firestore.model.SnapshotVersion) ArrayList(java.util.ArrayList) Timestamp(com.google.firebase.Timestamp) Test(org.junit.Test)

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