Mutate method

Future<Response> Mutate(
  1. ClientContext ctx,
  2. Request req
)

Implementation

Future<api.Response> Mutate(ClientContext ctx, api.Request req) async {
  if (readOnly) {
    throw ErrReadOnly;
  }
  if (finished) {
    throw ErrFinished;
  }
  mutated = true;
  req.startTs = context!.startTs;
  api.Response? res;
  try {
    res = await dc!.query(ctx, req);
    if (req.commitNow) {
      finished = true;
    }
    mergeContext(res.txn);
  } on GrpcError catch (e) {
    // Since a mutation error occurred, the txn should no longer be used
    // (some mutations could have applied but not others, but we don't know
    // which ones).  Discarding the transaction enforces that the user
    // cannot use the txn further.
    try {
      await Discard(ctx);
    } catch (e) {
      // Ignore error - user should see the original error.
    }
    // Transaction could be aborted(codes.Aborted) if CommitNow was true, or server could send a
    // message that this mutation conflicts(codes.FailedPrecondition) with another transaction.
    int s = e.code;
    if ((s == StatusCode.aborted) || (s == StatusCode.failedPrecondition)) {
      throw ErrAborted;
    }
  } finally {
    res ??= api.Response();
    return res;
  }
}