@Override public <T> Mono<T> delete(T entity) { return delete(entity, QueryOptions.empty()).map(reactiveWriteResult -> entity); }
@Override public Mono<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"); return doCount(query, entityClass, getTableName(entityClass)); }
@Override public Mono<Boolean> delete(Query query, Class<?> entityClass) throws DataAccessException { Assert.notNull(query, "Query must not be null"); Assert.notNull(entityClass, "Entity type must not be null"); return doDelete(query, entityClass, getTableName(entityClass)).map(WriteResult::wasApplied); }
@Override public Mono<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"); return doExists(query, entityClass, getTableName(entityClass)); }
@Override public <T> Mono<EntityWriteResult<T>> insert(T entity, InsertOptions options) { Assert.notNull(entity, "Entity must not be null"); Assert.notNull(options, "InsertOptions must not be null"); return doInsert(entity, options, getTableName(entity)); }
<T> Mono<EntityWriteResult<T>> doInsert(T entity, WriteOptions options, CqlIdentifier tableName) { CassandraPersistentEntity<?> persistentEntity = getRequiredPersistentEntity(entity.getClass()); Insert insert = QueryUtils.createInsertQuery(tableName.toCql(), entity, options, getConverter(), persistentEntity); // noinspection ConstantConditions Mono<EntityWriteResult<T>> result = getReactiveCqlOperations() // .execute(new StatementCallback(insert)) // .doOnSubscribe(it -> maybeEmitEvent(new BeforeSaveEvent<>(entity, tableName, insert))) // .map(it -> EntityWriteResult.of(it, entity)) // .next(); return result.doOnNext(it -> maybeEmitEvent(new AfterSaveEvent<>(entity, tableName))); }
@Override public Mono<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()); Mono<WriteResult> result = getReactiveCqlOperations() // .execute(new StatementCallback(delete)) // .doOnSubscribe(it -> maybeEmitEvent(new BeforeDeleteEvent<>(delete, entity.getClass(), tableName))) // .next(); return result.doOnNext(it -> maybeEmitEvent(new AfterDeleteEvent<>(delete, entity.getClass(), tableName))); }
@SuppressWarnings("unchecked") private <T> Function<Row, T> getMapper(Class<?> entityType, Class<T> targetType, CqlIdentifier tableName) { Class<?> typeToRead = resolveTypeToRead(entityType, targetType); return row -> { maybeEmitEvent(new AfterLoadEvent<>(row, targetType, tableName)); Object source = getConverter().read(typeToRead, row); T result = (T) (targetType.isInterface() ? getProjectionFactory().createProjection(targetType, source) : source); maybeEmitEvent(new AfterConvertEvent<>(row, result, tableName)); return result; }; }
@Override public <T> Mono<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); return selectOne(select, entityClass); }
@Override public Mono<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 getReactiveCqlOperations().queryForRows(select).hasElements(); }
@Override public <T> Mono<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"); Mono<ReactiveResultSet> resultSetMono = getReactiveCqlOperations().queryForResultSet(statement); Mono<Integer> effectiveFetchSizeMono = getEffectiveFetchSize(statement); RowMapper<T> rowMapper = (row, i) -> getConverter().read(entityClass, row); return resultSetMono.zipWith(effectiveFetchSizeMono).flatMap(tuple -> { ReactiveResultSet resultSet = tuple.getT1(); Integer effectiveFetchSize = tuple.getT2(); return resultSet.availableRows().collectList().map(it -> { return QueryUtils.readSlice(it, resultSet.getExecutionInfo().getPagingState(), rowMapper, 1, effectiveFetchSize); }); }).defaultIfEmpty(new SliceImpl<>(Collections.emptyList())); }
@Override public <T> Flux<T> select(Query query, Class<T> entityClass) throws DataAccessException { Assert.notNull(query, "Query must not be null"); Assert.notNull(entityClass, "Entity type must not be null"); return doSelect(query, entityClass, getTableName(entityClass), entityClass); }
@Override public Mono<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 doUpdate(query, update, entityClass, getTableName(entityClass)).map(WriteResult::wasApplied); }
@Override public Flux<T> all() { return this.template.doSelect(this.query, this.domainType, getTableName(), this.returnType); }
@Override public Mono<WriteResult> apply(Update update) { Assert.notNull(update, "Update must not be null"); return this.template.doUpdate(this.query, update, this.domainType, getTableName()); }
@Override public Mono<EntityWriteResult<T>> one(T object) { Assert.notNull(object, "Object must not be null"); return this.template.doInsert(object, this.insertOptions, getTableName()); }
@Override public Mono<Boolean> exists() { return this.template.doExists(this.query, this.domainType, getTableName()); }
@Override public Mono<Long> count() { return this.template.doCount(this.query, this.domainType, getTableName()); }
public Mono<WriteResult> all() { return this.template.doDelete(this.query, this.domainType, getTableName()); }
/** * Creates a {@link CassandraAdminTemplate}. * * @return the {@link ReactiveCassandraTemplate}. * @see #reactiveSessionFactory() * @see #cassandraConverter() */ @Bean public ReactiveCassandraTemplate reactiveCassandraTemplate() { return new ReactiveCassandraTemplate(reactiveSessionFactory(), cassandraConverter()); }