/** * Creates an instance of {@link AsyncCassandraTemplate} initialized with the given {@link SessionFactory} and * {@link CassandraConverter}. * * @param sessionFactory {@link SessionFactory} used to interact with Cassandra; must not be {@literal null}. * @param converter {@link CassandraConverter} used to convert between Java and Cassandra types; must not be * {@literal null}. * @see CassandraConverter * @see Session */ public AsyncCassandraTemplate(SessionFactory sessionFactory, CassandraConverter converter) { this(new AsyncCqlTemplate(sessionFactory), converter); }
@Override public ListenableFuture<Boolean> execute(String cql, Object... args) throws DataAccessException { return execute(cql, newPreparedStatementBinder(args)); }
@Override public <T> ListenableFuture<List<T>> queryForList(String cql, Class<T> elementType, Object... args) throws DataAccessException { return query(newAsyncPreparedStatementCreator(cql), newPreparedStatementBinder(args), newResultSetExtractor(newSingleColumnRowMapper(elementType))); }
@Override public <T> ListenableFuture<T> query(String cql, ResultSetExtractor<T> resultSetExtractor, Object... args) throws DataAccessException { return query(newAsyncPreparedStatementCreator(cql), newPreparedStatementBinder(args), resultSetExtractor); }
@Override public <T> ListenableFuture<List<T>> query(AsyncPreparedStatementCreator preparedStatementCreator, RowMapper<T> rowMapper) throws DataAccessException { return query(preparedStatementCreator, null, newResultSetExtractor(rowMapper)); }
@Override public <T> ListenableFuture<T> execute(AsyncPreparedStatementCreator preparedStatementCreator, PreparedStatementCallback<T> action) throws DataAccessException { Assert.notNull(preparedStatementCreator, "PreparedStatementCreator must not be null"); Assert.notNull(action, "PreparedStatementCallback object must not be null"); PersistenceExceptionTranslator exceptionTranslator = ex -> translateExceptionIfPossible("PreparedStatementCallback", toCql(preparedStatementCreator), ex); try { if (logger.isDebugEnabled()) { logger.debug("Preparing statement [{}] using {}", toCql(preparedStatementCreator), preparedStatementCreator); } Session currentSession = getCurrentSession(); return new ExceptionTranslatingListenableFutureAdapter<>(new MappingListenableFutureAdapter<>( preparedStatementCreator.createPreparedStatement(currentSession), preparedStatement -> { try { return action.doInPreparedStatement(currentSession, applyStatementSettings(preparedStatement)); } catch (DriverException e) { throw translateException(exceptionTranslator, e); } }), getExceptionTranslator()); } catch (DriverException e) { throw translateException(exceptionTranslator, e); } }
@Override public ListenableFuture<Void> query(Statement statement, RowCallbackHandler rowCallbackHandler) throws DataAccessException { ListenableFuture<?> result = query(statement, newResultSetExtractor(rowCallbackHandler)); return new ExceptionTranslatingListenableFutureAdapter<>(new MappingListenableFutureAdapter<>(result, o -> null), getExceptionTranslator()); }
@Override public <T> ListenableFuture<List<T>> queryForList(Statement statement, Class<T> elementType) throws DataAccessException { return query(statement, newResultSetExtractor(newSingleColumnRowMapper(elementType))); }
@Override public ListenableFuture<List<Map<String, Object>>> queryForList(String cql) throws DataAccessException { return query(cql, newResultSetExtractor(newColumnMapRowMapper())); }
@Override public <T> ListenableFuture<List<T>> query(String cql, @Nullable PreparedStatementBinder psb, RowMapper<T> rowMapper) throws DataAccessException { return query(newAsyncPreparedStatementCreator(cql), psb, newResultSetExtractor(rowMapper)); }
@Override public <T> ListenableFuture<T> execute(AsyncSessionCallback<T> action) throws DataAccessException { Assert.notNull(action, "Callback object must not be null"); try { return action.doInSession(getCurrentSession()); } catch (DriverException e) { throw translateException("SessionCallback", toCql(action), e); } }
@Override public ListenableFuture<Map<String, Object>> queryForMap(String cql) throws DataAccessException { return queryForObject(cql, newColumnMapRowMapper()); }
@Override public <T> ListenableFuture<T> query(String cql, @Nullable PreparedStatementBinder psb, ResultSetExtractor<T> resultSetExtractor) throws DataAccessException { return query(newAsyncPreparedStatementCreator(cql), psb, resultSetExtractor); }
@Override public <T> ListenableFuture<T> execute(String cql, PreparedStatementCallback<T> action) throws DataAccessException { return execute(newAsyncPreparedStatementCreator(cql), action); }
private Session getCurrentSession() { SessionFactory sessionFactory = getSessionFactory(); Assert.state(sessionFactory != null, "SessionFactory is null"); return sessionFactory.getSession(); }
/** * Creates an instance of {@link AsyncCassandraTemplate} initialized with the given {@link AsyncCqlTemplate} and * {@link CassandraConverter}. * * @param asyncCqlTemplate {@link AsyncCqlTemplate} used to interact with Cassandra; must not be {@literal null}. * @param converter {@link CassandraConverter} used to convert between Java and Cassandra types; must not be * {@literal null}. * @see CassandraConverter * @see Session */ public AsyncCassandraTemplate(AsyncCqlTemplate asyncCqlTemplate, CassandraConverter converter) { Assert.notNull(asyncCqlTemplate, "AsyncCqlTemplate must not be null"); Assert.notNull(converter, "CassandraConverter must not be null"); this.converter = converter; this.mappingContext = converter.getMappingContext(); this.cqlOperations = asyncCqlTemplate; this.exceptionTranslator = asyncCqlTemplate.getExceptionTranslator(); this.projectionFactory = new SpelAwareProxyProjectionFactory(); this.statementFactory = new StatementFactory(new QueryMapper(converter), new UpdateMapper(converter)); }
@Override public <T> ListenableFuture<T> query(Statement statement, ResultSetExtractor<T> resultSetExtractor) throws DataAccessException { Assert.notNull(statement, "CQL Statement must not be null"); Assert.notNull(resultSetExtractor, "ResultSetExtractor must not be null"); try { if (logger.isDebugEnabled()) { logger.debug("Executing CQL Statement [{}]", statement); } ResultSetFuture results = getCurrentSession().executeAsync(applyStatementSettings(statement)); return new ExceptionTranslatingListenableFutureAdapter<>( new MappingListenableFutureAdapter<>( new GuavaListenableFutureAdapter<>(results, ex -> translateExceptionIfPossible("Query", statement.toString(), ex)), resultSetExtractor::extractData), getExceptionTranslator()); } catch (DriverException e) { throw translateException("Query", statement.toString(), e); } }
@Override public ListenableFuture<Void> query(String cql, RowCallbackHandler rowCallbackHandler) throws DataAccessException { ListenableFuture<?> results = query(cql, newResultSetExtractor(rowCallbackHandler)); return new ExceptionTranslatingListenableFutureAdapter<>(new MappingListenableFutureAdapter<>(results, o -> null), getExceptionTranslator()); }
@Override public <T> ListenableFuture<List<T>> queryForList(String cql, Class<T> elementType) throws DataAccessException { return query(cql, newResultSetExtractor(newSingleColumnRowMapper(elementType))); }
@Override public ListenableFuture<List<Map<String, Object>>> queryForList(Statement statement) throws DataAccessException { return query(statement, newResultSetExtractor(newColumnMapRowMapper())); }