Search in sources :

Example 1 with MutationResult

use of com.google.firebase.firestore.model.mutation.MutationResult 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 2 with MutationResult

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

the class RemoteSerializer method decodeMutationResult.

public MutationResult decodeMutationResult(com.google.firestore.v1.WriteResult proto, SnapshotVersion commitVersion) {
    // NOTE: Deletes don't have an updateTime but the commit timestamp from the containing
    // CommitResponse or WriteResponse indicates essentially that the delete happened no later than
    // that. For our purposes we don't care exactly when the delete happened so long as we can tell
    // when an update on the watch stream is at or later than that change.
    SnapshotVersion version = decodeVersion(proto.getUpdateTime());
    if (SnapshotVersion.NONE.equals(version)) {
        version = commitVersion;
    }
    int transformResultsCount = proto.getTransformResultsCount();
    List<Value> transformResults = new ArrayList<>(transformResultsCount);
    for (int i = 0; i < transformResultsCount; i++) {
        transformResults.add(proto.getTransformResults(i));
    }
    return new MutationResult(version, transformResults);
}
Also used : SnapshotVersion(com.google.firebase.firestore.model.SnapshotVersion) ObjectValue(com.google.firebase.firestore.model.ObjectValue) Value(com.google.firestore.v1.Value) Int32Value(com.google.protobuf.Int32Value) ArrayValue(com.google.firestore.v1.ArrayValue) ArrayList(java.util.ArrayList) MutationResult(com.google.firebase.firestore.model.mutation.MutationResult)

Example 3 with MutationResult

use of com.google.firebase.firestore.model.mutation.MutationResult 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 4 with MutationResult

use of com.google.firebase.firestore.model.mutation.MutationResult 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 5 with MutationResult

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

the class SpecTestCase method doWriteAck.

private void doWriteAck(JSONObject writeAckSpec) throws Exception {
    long version = writeAckSpec.getLong("version");
    boolean keepInQueue = writeAckSpec.optBoolean("keepInQueue", false);
    assertFalse("'keepInQueue=true' is not supported on Android and should only be set in multi-client tests", keepInQueue);
    Pair<Mutation, Task<Void>> write = getCurrentOutstandingWrites().remove(0);
    validateNextWriteSent(write.first);
    MutationResult mutationResult = new MutationResult(version(version), /*transformResults=*/
    Collections.emptyList());
    queue.runSync(() -> datastore.ackWrite(version(version), singletonList(mutationResult)));
}
Also used : Task(com.google.android.gms.tasks.Task) LoadBundleTask(com.google.firebase.firestore.LoadBundleTask) Mutation(com.google.firebase.firestore.model.mutation.Mutation) TestUtil.setMutation(com.google.firebase.firestore.testutil.TestUtil.setMutation) TestUtil.deleteMutation(com.google.firebase.firestore.testutil.TestUtil.deleteMutation) TestUtil.patchMutation(com.google.firebase.firestore.testutil.TestUtil.patchMutation) MutationResult(com.google.firebase.firestore.model.mutation.MutationResult)

Aggregations

MutationResult (com.google.firebase.firestore.model.mutation.MutationResult)5 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)4 ArrayList (java.util.ArrayList)3 Mutation (com.google.firebase.firestore.model.mutation.Mutation)2 Task (com.google.android.gms.tasks.Task)1 FirebaseFirestoreException (com.google.firebase.firestore.FirebaseFirestoreException)1 LoadBundleTask (com.google.firebase.firestore.LoadBundleTask)1 ObjectValue (com.google.firebase.firestore.model.ObjectValue)1 MutationBatch (com.google.firebase.firestore.model.mutation.MutationBatch)1 MutationBatchResult (com.google.firebase.firestore.model.mutation.MutationBatchResult)1 TestUtil.deleteMutation (com.google.firebase.firestore.testutil.TestUtil.deleteMutation)1 TestUtil.patchMutation (com.google.firebase.firestore.testutil.TestUtil.patchMutation)1 TestUtil.setMutation (com.google.firebase.firestore.testutil.TestUtil.setMutation)1 ArrayValue (com.google.firestore.v1.ArrayValue)1 CommitRequest (com.google.firestore.v1.CommitRequest)1 CommitResponse (com.google.firestore.v1.CommitResponse)1 Value (com.google.firestore.v1.Value)1 Int32Value (com.google.protobuf.Int32Value)1