Search in sources :

Example 1 with CassandraFutureAdapter

use of org.springframework.data.cassandra.core.cql.util.CassandraFutureAdapter in project spring-data-cassandra by spring-projects.

the class AsyncCqlTemplate method query.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.cassandra.core.cql.AsyncCqlOperations#query(org.springframework.data.cassandra.core.cql.AsyncPreparedStatementCreator, org.springframework.data.cassandra.core.cql.PreparedStatementBinder, org.springframework.data.cassandra.core.cql.AsyncResultSetExtractor)
	 */
@Override
public <T> ListenableFuture<T> query(AsyncPreparedStatementCreator preparedStatementCreator, @Nullable PreparedStatementBinder psb, AsyncResultSetExtractor<T> resultSetExtractor) throws DataAccessException {
    Assert.notNull(preparedStatementCreator, "AsyncPreparedStatementCreator must not be null");
    Assert.notNull(resultSetExtractor, "AsyncResultSetExtractor object must not be null");
    PersistenceExceptionTranslator exceptionTranslator = ex -> translateExceptionIfPossible("Query", toCql(preparedStatementCreator), ex);
    try {
        if (logger.isDebugEnabled()) {
            logger.debug(String.format("Preparing statement [%s] using %s", toCql(preparedStatementCreator), preparedStatementCreator));
        }
        CqlSession session = getCurrentSession();
        ListenableFuture<Statement<?>> statementFuture = new MappingListenableFutureAdapter<>(preparedStatementCreator.createPreparedStatement(session), preparedStatement -> {
            if (logger.isDebugEnabled()) {
                logger.debug(String.format("Executing prepared statement [%s]", toCql(preparedStatement)));
            }
            return applyStatementSettings(psb != null ? psb.bindValues(preparedStatement) : preparedStatement.bind());
        });
        CompletableFuture<T> result = // 
        statementFuture.completable().thenCompose(// 
        session::executeAsync).thenApply(// 
        resultSetExtractor::extractData).thenCompose(ListenableFuture::completable);
        return new CassandraFutureAdapter<>(result, exceptionTranslator);
    } catch (DriverException e) {
        throw translateException(exceptionTranslator, e);
    }
}
Also used : DataAccessException(org.springframework.dao.DataAccessException) ListenableFuture(org.springframework.util.concurrent.ListenableFuture) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) CompletableFuture(java.util.concurrent.CompletableFuture) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) Function(java.util.function.Function) ExecutionException(java.util.concurrent.ExecutionException) PersistenceExceptionTranslator(org.springframework.dao.support.PersistenceExceptionTranslator) List(java.util.List) AsyncResultSet(com.datastax.oss.driver.api.core.cql.AsyncResultSet) CompletionStage(java.util.concurrent.CompletionStage) CassandraFutureAdapter(org.springframework.data.cassandra.core.cql.util.CassandraFutureAdapter) DriverException(com.datastax.oss.driver.api.core.DriverException) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Map(java.util.Map) Nullable(org.springframework.lang.Nullable) SessionFactory(org.springframework.data.cassandra.SessionFactory) Statement(com.datastax.oss.driver.api.core.cql.Statement) DataAccessUtils(org.springframework.dao.support.DataAccessUtils) Assert(org.springframework.util.Assert) PersistenceExceptionTranslator(org.springframework.dao.support.PersistenceExceptionTranslator) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) Statement(com.datastax.oss.driver.api.core.cql.Statement) ListenableFuture(org.springframework.util.concurrent.ListenableFuture) SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) DriverException(com.datastax.oss.driver.api.core.DriverException) CqlSession(com.datastax.oss.driver.api.core.CqlSession) CassandraFutureAdapter(org.springframework.data.cassandra.core.cql.util.CassandraFutureAdapter)

Aggregations

CqlSession (com.datastax.oss.driver.api.core.CqlSession)1 DriverException (com.datastax.oss.driver.api.core.DriverException)1 AsyncResultSet (com.datastax.oss.driver.api.core.cql.AsyncResultSet)1 PreparedStatement (com.datastax.oss.driver.api.core.cql.PreparedStatement)1 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)1 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)1 Statement (com.datastax.oss.driver.api.core.cql.Statement)1 List (java.util.List)1 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionStage (java.util.concurrent.CompletionStage)1 ExecutionException (java.util.concurrent.ExecutionException)1 Function (java.util.function.Function)1 DataAccessException (org.springframework.dao.DataAccessException)1 DataAccessUtils (org.springframework.dao.support.DataAccessUtils)1 PersistenceExceptionTranslator (org.springframework.dao.support.PersistenceExceptionTranslator)1 SessionFactory (org.springframework.data.cassandra.SessionFactory)1 CassandraFutureAdapter (org.springframework.data.cassandra.core.cql.util.CassandraFutureAdapter)1 Nullable (org.springframework.lang.Nullable)1 Assert (org.springframework.util.Assert)1