// Path: cloud-spanner-r2dbc/src/main/java/com/google/cloud/spanner/r2dbc/v2/DatabaseClientReactiveAdapter.java
// public static final Executor REACTOR_EXECUTOR =
//     runnable -> Schedulers.parallel().schedule(runnable);
// 
// Path: cloud-spanner-r2dbc/src/main/java/com/google/cloud/spanner/r2dbc/TransactionInProgressException.java
// public class TransactionInProgressException extends R2dbcNonTransientException {
// 
//   public static final String MSG_READONLY =
//       "Cannot begin a new transaction because a readonly transaction is already in progress.";
//   public static final String MSG_READWRITE =
//       "Cannot begin a new transaction because a read/write transaction is already in progress.";
// 
//   public TransactionInProgressException(boolean isReadwrite) {
//     super(isReadwrite ? MSG_READWRITE : MSG_READONLY);
//   }
// 
// }


import java.util.function.Function;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static com.google.cloud.spanner.r2dbc.v2.DatabaseClientReactiveAdapter.REACTOR_EXECUTOR;
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
import com.google.cloud.Timestamp;
import com.google.cloud.spanner.AsyncTransactionManager;
import com.google.cloud.spanner.AsyncTransactionManager.AsyncTransactionStep;
import com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture;
import com.google.cloud.spanner.DatabaseClient;
import com.google.cloud.spanner.ReadContext;
import com.google.cloud.spanner.ReadOnlyTransaction;
import com.google.cloud.spanner.TimestampBound;
import com.google.cloud.spanner.TransactionContext;
import com.google.cloud.spanner.r2dbc.TransactionInProgressException;
/*
 * Copyright 2020-2020 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.cloud.spanner.r2dbc.v2;



/**
 * Helper class encapsulating read/write and readonly transaction management for client library
 * based interaction with Cloud Spanner.
 * Partitioned DML is out of scope for transaction handling, as they are not atomic.
 */
class DatabaseClientTransactionManager {

  private static final Logger LOGGER =
      LoggerFactory.getLogger(DatabaseClientTransactionManager.class);

  private final DatabaseClient dbClient;

  private AsyncTransactionManager transactionManager;

  private TransactionContextFuture txnContextFuture;

  private ReadOnlyTransaction readOnlyTransaction;

  private AsyncTransactionStep<?, ? extends Object> lastStep;

  public DatabaseClientTransactionManager(DatabaseClient dbClient) {
    this.dbClient = dbClient;
  }

  boolean isInReadWriteTransaction() {
    return this.txnContextFuture != null;
  }

  boolean isInReadonlyTransaction() {
    return this.readOnlyTransaction != null;
  }

  boolean isInTransaction() {
    return isInReadWriteTransaction() || isInReadonlyTransaction();
  }

  /**
   * Starts a Cloud Spanner Read/Write transaction.
   *
   * @return chainable {@link TransactionContextFuture} for the current transaction.
   */
  TransactionContextFuture beginTransaction() {
    if (this.isInTransaction()) {
      throw new TransactionInProgressException(this.isInReadWriteTransaction());
    }
    this.transactionManager = this.dbClient.transactionManagerAsync();
    this.txnContextFuture = this.transactionManager.beginAsync();
    return this.txnContextFuture;
  }

  /**
   * Starts a Cloud Spanner Read-only transaction with specified staleness.
   *
   * @param timestampBound staleness settings
   *
   * @return chainable {@link TransactionContextFuture} for the current transaction.
   */
// Path: cloud-spanner-r2dbc/src/main/java/com/google/cloud/spanner/r2dbc/v2/DatabaseClientReactiveAdapter.java
// public static final Executor REACTOR_EXECUTOR =
//     runnable -> Schedulers.parallel().schedule(runnable);
// 
// Path: cloud-spanner-r2dbc/src/main/java/com/google/cloud/spanner/r2dbc/TransactionInProgressException.java
// public class TransactionInProgressException extends R2dbcNonTransientException {
// 
//   public static final String MSG_READONLY =
//       "Cannot begin a new transaction because a readonly transaction is already in progress.";
//   public static final String MSG_READWRITE =
//       "Cannot begin a new transaction because a read/write transaction is already in progress.";
// 
//   public TransactionInProgressException(boolean isReadwrite) {
//     super(isReadwrite ? MSG_READWRITE : MSG_READONLY);
//   }
// 
// }


// Path: cloud-spanner-r2dbc/src/main/java/com/google/cloud/spanner/r2dbc/v2/DatabaseClientTransactionManager.java
import java.util.function.Function;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static com.google.cloud.spanner.r2dbc.v2.DatabaseClientReactiveAdapter.REACTOR_EXECUTOR;
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
import com.google.cloud.Timestamp;
import com.google.cloud.spanner.AsyncTransactionManager;
import com.google.cloud.spanner.AsyncTransactionManager.AsyncTransactionStep;
import com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture;
import com.google.cloud.spanner.DatabaseClient;
import com.google.cloud.spanner.ReadContext;
import com.google.cloud.spanner.ReadOnlyTransaction;
import com.google.cloud.spanner.TimestampBound;
import com.google.cloud.spanner.TransactionContext;
import com.google.cloud.spanner.r2dbc.TransactionInProgressException;

/*
 * Copyright 2020-2020 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.cloud.spanner.r2dbc.v2;



/**
 * Helper class encapsulating read/write and readonly transaction management for client library
 * based interaction with Cloud Spanner.
 * Partitioned DML is out of scope for transaction handling, as they are not atomic.
 */
class DatabaseClientTransactionManager {

  private static final Logger LOGGER =
      LoggerFactory.getLogger(DatabaseClientTransactionManager.class);

  private final DatabaseClient dbClient;

  private AsyncTransactionManager transactionManager;

  private TransactionContextFuture txnContextFuture;

  private ReadOnlyTransaction readOnlyTransaction;

  private AsyncTransactionStep<?, ? extends Object> lastStep;

  public DatabaseClientTransactionManager(DatabaseClient dbClient) {
    this.dbClient = dbClient;
  }

  boolean isInReadWriteTransaction() {
    return this.txnContextFuture != null;
  }

  boolean isInReadonlyTransaction() {
    return this.readOnlyTransaction != null;
  }

  boolean isInTransaction() {
    return isInReadWriteTransaction() || isInReadonlyTransaction();
  }

  /**
   * Starts a Cloud Spanner Read/Write transaction.
   *
   * @return chainable {@link TransactionContextFuture} for the current transaction.
   */
  TransactionContextFuture beginTransaction() {
    if (this.isInTransaction()) {
      throw new TransactionInProgressException(this.isInReadWriteTransaction());
    }
    this.transactionManager = this.dbClient.transactionManagerAsync();
    this.txnContextFuture = this.transactionManager.beginAsync();
    return this.txnContextFuture;
  }

  /**
   * Starts a Cloud Spanner Read-only transaction with specified staleness.
   *
   * @param timestampBound staleness settings
   *
   * @return chainable {@link TransactionContextFuture} for the current transaction.
   */
  void beginReadonlyTransaction(TimestampBound timestampBound) {