Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor transaction #6188

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading