ApiFuture<List<WriteResult>> commit(@Nullable ByteString transactionId) {
    Tracing.getTracer()
        .getCurrentSpan()
        .addAnnotation(
            "CloudFirestore.Commit",
            ImmutableMap.of("numDocuments", AttributeValue.longAttributeValue(mutations.size())));

    final CommitRequest.Builder request = CommitRequest.newBuilder();
    request.setDatabase(firestore.getDatabaseName());

    for (Mutation mutation : mutations) {
      Preconditions.checkState(
          mutation.document != null || mutation.transform != null,
          "Either a write or transform must be set");

      if (mutation.precondition != null) {
        (mutation.document != null ? mutation.document : mutation.transform)
            .setCurrentDocument(mutation.precondition);
      }

      if (mutation.document != null) {
        request.addWrites(mutation.document);
      }

      if (mutation.transform != null) {
        request.addWrites(mutation.transform);
      }
    }

    if (transactionId != null) {
      request.setTransaction(transactionId);
    }

    committed = true;

    ApiFuture<CommitResponse> response =
        firestore.sendRequest(request.build(), firestore.getClient().commitCallable());

    return ApiFutures.transform(
        response,
        new ApiFunction<CommitResponse, List<WriteResult>>() {
          @Override
          public List<WriteResult> apply(CommitResponse commitResponse) {
            List<com.google.firestore.v1.WriteResult> writeResults =
                commitResponse.getWriteResultsList();

            List<WriteResult> result = new ArrayList<>();

            Preconditions.checkState(
                request.getWritesCount() == writeResults.size(),
                "Expected one write result per operation, but got %s results for %s operations.",
                writeResults.size(),
                request.getWritesCount());

            Iterator<Mutation> mutationIterator = mutations.iterator();
            Iterator<com.google.firestore.v1.WriteResult> responseIterator =
                writeResults.iterator();

            while (mutationIterator.hasNext()) {
              Mutation mutation = mutationIterator.next();

              // Don't return both write results for a write that contains a transform, as the fact
              // that we have to split one write operation into two distinct write requests is an
              // implementation detail.
              if (mutation.document != null && mutation.transform != null) {
                // The document transform is always sent last and produces the latest update time.
                responseIterator.next();
              }

              result.add(
                  WriteResult.fromProto(responseIterator.next(), commitResponse.getCommitTime()));
            }

            return result;
          }
        });
  }
ApiFuture < List < WriteResult > > commit ( @ Nullable ByteString transactionId ) { Tracing . getTracer ( ) . getCurrentSpan ( ) . addAnnotation ( "CloudFirestore.Commit" , ImmutableMap . of ( "numDocuments" , AttributeValue . longAttributeValue ( mutations . size ( ) ) ) ) ; final CommitRequest . Builder request = CommitRequest . newBuilder ( ) ; request . setDatabase ( firestore . getDatabaseName ( ) ) ; for ( Mutation mutation : mutations ) { Preconditions . checkState ( mutation . document != null || mutation . transform != null , "Either a write or transform must be set" ) ; if ( mutation . precondition != null ) { ( mutation . document != null ? mutation . document : mutation . transform ) . setCurrentDocument ( mutation . precondition ) ; } if ( mutation . document != null ) { request . addWrites ( mutation . document ) ; } if ( mutation . transform != null ) { request . addWrites ( mutation . transform ) ; } } if ( transactionId != null ) { request . setTransaction ( transactionId ) ; } committed = true ; ApiFuture < CommitResponse > response = firestore . sendRequest ( request . build ( ) , firestore . getClient ( ) . commitCallable ( ) ) ; return ApiFutures . transform ( response , new ApiFunction < CommitResponse , List < WriteResult > > ( ) { @ Override public List < WriteResult > apply ( CommitResponse commitResponse ) { List < com . google . firestore . v1 . WriteResult > writeResults = commitResponse . getWriteResultsList ( ) ; List < WriteResult > result = new ArrayList < > ( ) ; Preconditions . checkState ( request . getWritesCount ( ) == writeResults . size ( ) , "Expected one write result per operation, but got %s results for %s operations." , writeResults . size ( ) , request . getWritesCount ( ) ) ; Iterator < Mutation > mutationIterator = mutations . iterator ( ) ; Iterator < com . google . firestore . v1 . WriteResult > responseIterator = writeResults . iterator ( ) ; while ( mutationIterator . hasNext ( ) ) { Mutation mutation = mutationIterator . next ( ) ; if ( mutation . document != null && mutation . transform != null ) { responseIterator . next ( ) ; } result . add ( WriteResult . fromProto ( responseIterator . next ( ) , commitResponse . getCommitTime ( ) ) ) ; } return result ; } } ) ; }
Commit the current batch.