@Override public ListenableFuture<Boolean> update(Query query, org.springframework.data.cassandra.core.query.Update update, Class<?> entityClass) throws DataAccessException { Assert.notNull(query, "Query must not be null"); Assert.notNull(update, "Update must not be null"); Assert.notNull(entityClass, "Entity type must not be null"); return getAsyncCqlOperations().execute( getStatementFactory().update(query, update, getRequiredPersistentEntity(entityClass))); }
@Override public <T> ListenableFuture<Void> select(Statement statement, Consumer<T> entityConsumer, Class<T> entityClass) throws DataAccessException { Assert.notNull(statement, "Statement must not be null"); Assert.notNull(entityConsumer, "Entity Consumer must not be empty"); Assert.notNull(entityClass, "Entity type must not be null"); Function<Row, T> mapper = getMapper(entityClass, entityClass, QueryUtils.getTableName(statement)); return getAsyncCqlOperations().query(statement, row -> { entityConsumer.accept(mapper.apply(row)); }); }
@Override public ListenableFuture<Long> count(Query query, Class<?> entityClass) throws DataAccessException { Assert.notNull(query, "Query must not be null"); Assert.notNull(entityClass, "Entity type must not be null"); RegularStatement count = getStatementFactory().count(query, getRequiredPersistentEntity(entityClass)); ListenableFuture<Long> result = getAsyncCqlOperations().queryForObject(count, Long.class); return new MappingListenableFutureAdapter<>(result, it -> it != null ? it : 0L); }
@Override public <T> ListenableFuture<Slice<T>> slice(Statement statement, Class<T> entityClass) { Assert.notNull(statement, "Statement must not be null"); Assert.notNull(entityClass, "Entity type must not be null"); ListenableFuture<ResultSet> resultSet = getAsyncCqlOperations().queryForResultSet(statement); Function<Row, T> mapper = getMapper(entityClass, entityClass, QueryUtils.getTableName(statement)); return new MappingListenableFutureAdapter<>(resultSet, rs -> QueryUtils.readSlice(rs, (row, rowNum) -> mapper.apply(row), 0, getEffectiveFetchSize(statement))); }
@Override public ListenableFuture<Boolean> exists(Query query, Class<?> entityClass) throws DataAccessException { Assert.notNull(query, "Query must not be null"); Assert.notNull(entityClass, "Entity type must not be null"); RegularStatement select = getStatementFactory().select(query.limit(1), getRequiredPersistentEntity(entityClass)); return new MappingListenableFutureAdapter<>(getAsyncCqlOperations().queryForResultSet(select), resultSet -> resultSet.iterator().hasNext()); }
@Override public ListenableFuture<WriteResult> delete(Object entity, QueryOptions options) { Assert.notNull(entity, "Entity must not be null"); Assert.notNull(options, "QueryOptions must not be null"); CqlIdentifier tableName = getTableName(entity); Delete delete = QueryUtils.createDeleteQuery(tableName.toCql(), entity, options, getConverter()); maybeEmitEvent(new BeforeDeleteEvent<>(delete, entity.getClass(), tableName)); return new MappingListenableFutureAdapter<>(getAsyncCqlOperations().execute(new AsyncStatementCallback(delete)), resultSet -> { maybeEmitEvent(new AfterDeleteEvent<>(delete, entity.getClass(), tableName)); return WriteResult.of(resultSet); }); }
@Override public ListenableFuture<Boolean> exists(Object id, Class<?> entityClass) { Assert.notNull(id, "Id must not be null"); Assert.notNull(entityClass, "Entity type must not be null"); CassandraPersistentEntity<?> entity = getRequiredPersistentEntity(entityClass); Select select = QueryBuilder.select().from(entity.getTableName().toCql()); getConverter().write(id, select.where(), entity); return new MappingListenableFutureAdapter<>(getAsyncCqlOperations().queryForResultSet(select), resultSet -> resultSet.iterator().hasNext()); }
@Override public <T> ListenableFuture<List<T>> select(Statement statement, Class<T> entityClass) { Assert.notNull(statement, "Statement must not be null"); Assert.notNull(entityClass, "Entity type must not be null"); Function<Row, T> mapper = getMapper(entityClass, entityClass, QueryUtils.getTableName(statement)); return getAsyncCqlOperations().query(statement, (row, rowNum) -> mapper.apply(row)); }
@Override public ListenableFuture<Long> count(Class<?> entityClass) { Assert.notNull(entityClass, "Entity type must not be null"); Select select = QueryBuilder.select().countAll().from(getTableName(entityClass).toCql()); return getAsyncCqlOperations().queryForObject(select, Long.class); }
@Override public <T> ListenableFuture<EntityWriteResult<T>> insert(T entity, InsertOptions options) { Assert.notNull(entity, "Entity must not be null"); Assert.notNull(options, "InsertOptions must not be null"); CassandraPersistentEntity<?> persistentEntity = getRequiredPersistentEntity(entity.getClass()); CqlIdentifier tableName = persistentEntity.getTableName(); Insert insert = QueryUtils.createInsertQuery(tableName.toCql(), entity, options, getConverter(), persistentEntity); maybeEmitEvent(new BeforeSaveEvent<>(entity, tableName, insert)); return new MappingListenableFutureAdapter<>(getAsyncCqlOperations().execute(new AsyncStatementCallback(insert)), resultSet -> { maybeEmitEvent(new AfterSaveEvent<>(entity, tableName)); return EntityWriteResult.of(resultSet, entity); }); }
@Override public <T> ListenableFuture<T> selectOneById(Object id, Class<T> entityClass) { Assert.notNull(id, "Id must not be null"); Assert.notNull(entityClass, "Entity type must not be null"); CassandraPersistentEntity<?> entity = getRequiredPersistentEntity(entityClass); Select select = QueryBuilder.select().all().from(entity.getTableName().toCql()); getConverter().write(id, select.where(), entity); Function<Row, T> mapper = getMapper(entityClass, entityClass, entity.getTableName()); return new MappingListenableFutureAdapter<>( getAsyncCqlOperations().query(select, (row, rowNum) -> mapper.apply(row)), it -> it.isEmpty() ? null : (T) it.get(0)); }
@Override public ListenableFuture<Void> truncate(Class<?> entityClass) { Assert.notNull(entityClass, "Entity type must not be null"); CqlIdentifier tableName = getTableName(entityClass); Truncate truncate = QueryBuilder.truncate(tableName.toCql()); maybeEmitEvent(new BeforeDeleteEvent<>(truncate, entityClass, tableName)); ListenableFuture<Boolean> future = getAsyncCqlOperations().execute(truncate); future.addCallback(success -> maybeEmitEvent(new AfterDeleteEvent<>(truncate, entityClass, tableName)), e -> {}); return new MappingListenableFutureAdapter<>(future, aBoolean -> null); }
@SuppressWarnings("ConstantConditions") private int getEffectiveFetchSize(Statement statement) { if (statement.getFetchSize() > 0) { return statement.getFetchSize(); } if (getAsyncCqlOperations() instanceof CassandraAccessor) { CassandraAccessor accessor = (CassandraAccessor) getAsyncCqlOperations(); if (accessor.getFetchSize() != -1) { return accessor.getFetchSize(); } } return getAsyncCqlOperations() .execute((AsyncSessionCallback<Integer>) session -> AsyncResult.forValue(getConfiguredFetchSize(session))) .completable().join(); }
@Override public ListenableFuture<Boolean> deleteById(Object id, Class<?> entityClass) { Assert.notNull(id, "Id must not be null"); Assert.notNull(entityClass, "Entity type must not be null"); CassandraPersistentEntity<?> entity = getRequiredPersistentEntity(entityClass); CqlIdentifier tableName = entity.getTableName(); Delete delete = QueryBuilder.delete().from(tableName.toCql()); getConverter().write(id, delete.where(), entity); maybeEmitEvent(new BeforeDeleteEvent<>(delete, entityClass, tableName)); ListenableFuture<Boolean> future = getAsyncCqlOperations().execute(delete); future.addCallback(success -> maybeEmitEvent(new AfterDeleteEvent<>(delete, entityClass, tableName)), e -> {}); return future; }
@Override public <T> ListenableFuture<EntityWriteResult<T>> update(T entity, UpdateOptions options) { Assert.notNull(entity, "Entity must not be null"); Assert.notNull(options, "UpdateOptions must not be null"); CqlIdentifier tableName = getTableName(entity); Update update = QueryUtils.createUpdateQuery(tableName.toCql(), entity, options, getConverter()); maybeEmitEvent(new BeforeSaveEvent<>(entity, tableName, update)); return new MappingListenableFutureAdapter<>(getAsyncCqlOperations().execute(new AsyncStatementCallback(update)), resultSet -> { maybeEmitEvent(new AfterSaveEvent<>(entity, tableName)); return EntityWriteResult.of(resultSet, entity); }); }
private ListenableFuture<Boolean> doDelete(Query query, Class<?> entityClass, CqlIdentifier tableName) { RegularStatement delete = getStatementFactory().delete(query, getRequiredPersistentEntity(entityClass), tableName); maybeEmitEvent(new BeforeDeleteEvent<>(delete, entityClass, tableName)); ListenableFuture<Boolean> future = getAsyncCqlOperations() .execute(getStatementFactory().delete(query, getRequiredPersistentEntity(entityClass))); future.addCallback(success -> maybeEmitEvent(new AfterDeleteEvent<>(delete, entityClass, tableName)), e -> {}); return future; }