Skip to content

Commit

Permalink
Merge 5f0586c into 8ad9ef5
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-andersen authored Sep 11, 2024
2 parents 8ad9ef5 + 5f0586c commit f8d6e1f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,27 @@ public Task<Void> write(final List<Mutation> mutations) {
return source.getTask();
}

/** Tries to execute the transaction in updateFunction. */
/**
* Takes an updateFunction in which a set of reads and writes can be performed atomically. In the
* updateFunction, the client can read and write values using the supplied transaction object.
* After the updateFunction, all changes will be committed. If a retryable error occurs (ex: some
* other client has changed any of the data referenced), then the updateFunction will be called
* again after a backoff. If the updateFunction still fails after all retries, then the
* transaction will be rejected.
*
* <p>The transaction object passed to the updateFunction contains methods for accessing documents
* and collections. Unlike other datastore access, data accessed with the transaction will not
* reflect local changes that have not been committed. For this reason, it is required that all
* reads are performed before any writes. Transactions must be performed while online.
*
* <p>The Task returned is resolved when the transaction is fully committed.
*/
public <TResult> Task<TResult> transaction(
TransactionOptions options, Function<Transaction, Task<TResult>> updateFunction) {
this.verifyNotTerminated();
return AsyncQueue.callTask(
asyncQueue.getExecutor(),
() -> syncEngine.transaction(asyncQueue, options, updateFunction));
() -> new TransactionRunner<>(asyncQueue, remoteStore, options, updateFunction).run());
}

// TODO(b/261013682): Use an explicit executor in continuations.
Expand All @@ -242,7 +256,7 @@ public Task<Map<String, Value>> runAggregateQuery(
final TaskCompletionSource<Map<String, Value>> result = new TaskCompletionSource<>();
asyncQueue.enqueueAndForget(
() ->
syncEngine
remoteStore
.runAggregateQuery(query, aggregateFields)
.addOnSuccessListener(data -> result.setResult(data))
.addOnFailureListener(e -> result.setException(e)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,12 @@

import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.TaskCompletionSource;
import com.google.firebase.database.collection.ImmutableSortedMap;
import com.google.firebase.database.collection.ImmutableSortedSet;
import com.google.firebase.firestore.AggregateField;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.LoadBundleTask;
import com.google.firebase.firestore.LoadBundleTaskProgress;
import com.google.firebase.firestore.TransactionOptions;
import com.google.firebase.firestore.auth.User;
import com.google.firebase.firestore.bundle.BundleElement;
import com.google.firebase.firestore.bundle.BundleLoader;
Expand All @@ -51,11 +48,8 @@
import com.google.firebase.firestore.remote.RemoteEvent;
import com.google.firebase.firestore.remote.RemoteStore;
import com.google.firebase.firestore.remote.TargetChange;
import com.google.firebase.firestore.util.AsyncQueue;
import com.google.firebase.firestore.util.Function;
import com.google.firebase.firestore.util.Logger;
import com.google.firebase.firestore.util.Util;
import com.google.firestore.v1.Value;
import com.google.protobuf.ByteString;
import io.grpc.Status;
import java.io.IOException;
Expand Down Expand Up @@ -337,33 +331,6 @@ private void addUserCallback(int batchId, TaskCompletionSource<Void> userTask) {
userTasks.put(batchId, userTask);
}

/**
* Takes an updateFunction in which a set of reads and writes can be performed atomically. In the
* updateFunction, the client can read and write values using the supplied transaction object.
* After the updateFunction, all changes will be committed. If a retryable error occurs (ex: some
* other client has changed any of the data referenced), then the updateFunction will be called
* again after a backoff. If the updateFunction still fails after all retries, then the
* transaction will be rejected.
*
* <p>The transaction object passed to the updateFunction contains methods for accessing documents
* and collections. Unlike other datastore access, data accessed with the transaction will not
* reflect local changes that have not been committed. For this reason, it is required that all
* reads are performed before any writes. Transactions must be performed while online.
*
* <p>The Task returned is resolved when the transaction is fully committed.
*/
public <TResult> Task<TResult> transaction(
AsyncQueue asyncQueue,
TransactionOptions options,
Function<Transaction, Task<TResult>> updateFunction) {
return new TransactionRunner<TResult>(asyncQueue, remoteStore, options, updateFunction).run();
}

public Task<Map<String, Value>> runAggregateQuery(
Query query, List<AggregateField> aggregateFields) {
return remoteStore.runAggregateQuery(query, aggregateFields);
}

/** Called by FirestoreClient to notify us of a new remote event. */
@Override
public void handleRemoteEvent(RemoteEvent event) {
Expand Down

0 comments on commit f8d6e1f

Please sign in to comment.