Search in sources :

Example 1 with ReactiveResultSet

use of org.springframework.data.cassandra.ReactiveResultSet in project spring-data-cassandra by spring-projects.

the class ReactiveCassandraTemplate method truncate.

/* (non-Javadoc)
	 * @see org.springframework.data.cassandra.core.ReactiveCassandraOperations#truncate(java.lang.Class)
	 */
@Override
public Mono<Void> truncate(Class<?> entityClass) {
    Assert.notNull(entityClass, "Entity type must not be null");
    CqlIdentifier tableName = getTableName(entityClass);
    Truncate truncate = QueryBuilder.truncate(tableName);
    SimpleStatement statement = truncate.build();
    Mono<Boolean> result = doExecute(statement, ReactiveResultSet::wasApplied).doOnSubscribe(it -> maybeEmitEvent(new BeforeDeleteEvent<>(statement, entityClass, tableName)));
    return result.doOnNext(it -> maybeEmitEvent(new AfterDeleteEvent<>(statement, entityClass, tableName))).then();
}
Also used : QueryBuilder(com.datastax.oss.driver.api.querybuilder.QueryBuilder) EntityCallbacks(org.springframework.data.mapping.callback.EntityCallbacks) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) SimpleUserTypeResolver(org.springframework.data.cassandra.core.mapping.SimpleUserTypeResolver) Insert(com.datastax.oss.driver.api.querybuilder.insert.Insert) Delete(com.datastax.oss.driver.api.querybuilder.delete.Delete) AfterLoadEvent(org.springframework.data.cassandra.core.mapping.event.AfterLoadEvent) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) EntityProjection(org.springframework.data.projection.EntityProjection) CassandraPersistentEntity(org.springframework.data.cassandra.core.mapping.CassandraPersistentEntity) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) ReactiveSessionFactory(org.springframework.data.cassandra.ReactiveSessionFactory) DriverException(com.datastax.oss.driver.api.core.DriverException) BatchType(com.datastax.oss.driver.api.core.cql.BatchType) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) AfterDeleteEvent(org.springframework.data.cassandra.core.mapping.event.AfterDeleteEvent) Select(com.datastax.oss.driver.api.querybuilder.select.Select) ReactiveEntityCallbacks(org.springframework.data.mapping.callback.ReactiveEntityCallbacks) DriverContext(com.datastax.oss.driver.api.core.context.DriverContext) Update(com.datastax.oss.driver.api.querybuilder.update.Update) ReactiveBeforeSaveCallback(org.springframework.data.cassandra.core.mapping.event.ReactiveBeforeSaveCallback) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement) DefaultReactiveSessionFactory(org.springframework.data.cassandra.core.cql.session.DefaultReactiveSessionFactory) CassandraMappingEvent(org.springframework.data.cassandra.core.mapping.event.CassandraMappingEvent) Truncate(com.datastax.oss.driver.api.querybuilder.truncate.Truncate) org.springframework.data.cassandra.core.cql(org.springframework.data.cassandra.core.cql) Slice(org.springframework.data.domain.Slice) BeforeDeleteEvent(org.springframework.data.cassandra.core.mapping.event.BeforeDeleteEvent) LogFactory(org.apache.commons.logging.LogFactory) BeforeSaveEvent(org.springframework.data.cassandra.core.mapping.event.BeforeSaveEvent) SpelAwareProxyProjectionFactory(org.springframework.data.projection.SpelAwareProxyProjectionFactory) ApplicationContextAware(org.springframework.context.ApplicationContextAware) DataAccessException(org.springframework.dao.DataAccessException) AfterConvertEvent(org.springframework.data.cassandra.core.mapping.event.AfterConvertEvent) SliceImpl(org.springframework.data.domain.SliceImpl) ProjectionFactory(org.springframework.data.projection.ProjectionFactory) Function(java.util.function.Function) ReactiveBeforeConvertCallback(org.springframework.data.cassandra.core.mapping.event.ReactiveBeforeConvertCallback) DefaultDriverOption(com.datastax.oss.driver.api.core.config.DefaultDriverOption) BiConsumer(java.util.function.BiConsumer) StatementBuilder(org.springframework.data.cassandra.core.cql.util.StatementBuilder) AfterSaveEvent(org.springframework.data.cassandra.core.mapping.event.AfterSaveEvent) ReactiveSession(org.springframework.data.cassandra.ReactiveSession) Nullable(org.springframework.lang.Nullable) Row(com.datastax.oss.driver.api.core.cql.Row) MappingCassandraConverter(org.springframework.data.cassandra.core.convert.MappingCassandraConverter) DataAccessUtils(org.springframework.dao.support.DataAccessUtils) RegularInsert(com.datastax.oss.driver.api.querybuilder.insert.RegularInsert) ApplicationEventPublisherAware(org.springframework.context.ApplicationEventPublisherAware) CassandraConverter(org.springframework.data.cassandra.core.convert.CassandraConverter) Publisher(org.reactivestreams.Publisher) ReactiveResultSet(org.springframework.data.cassandra.ReactiveResultSet) Mono(reactor.core.publisher.Mono) BeansException(org.springframework.beans.BeansException) Query(org.springframework.data.cassandra.core.query.Query) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) ApplicationContext(org.springframework.context.ApplicationContext) SynchronousSink(reactor.core.publisher.SynchronousSink) Flux(reactor.core.publisher.Flux) AdaptibleEntity(org.springframework.data.cassandra.core.EntityOperations.AdaptibleEntity) Log(org.apache.commons.logging.Log) Collections(java.util.Collections) Statement(com.datastax.oss.driver.api.core.cql.Statement) Columns(org.springframework.data.cassandra.core.query.Columns) Assert(org.springframework.util.Assert) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) Truncate(com.datastax.oss.driver.api.querybuilder.truncate.Truncate) BeforeDeleteEvent(org.springframework.data.cassandra.core.mapping.event.BeforeDeleteEvent) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier)

Example 2 with ReactiveResultSet

use of org.springframework.data.cassandra.ReactiveResultSet in project spring-data-cassandra by spring-projects.

the class ReactiveCassandraBatchTemplateIntegrationTests method shouldUpdateMonoCollectionOfEntitiesWithTtl.

// DATACASS-574
@Test
void shouldUpdateMonoCollectionOfEntitiesWithTtl() {
    walter.setEmail("walter@white.com");
    mike.setEmail("mike@sauls.com");
    int ttl = 30;
    WriteOptions options = WriteOptions.builder().ttl(ttl).build();
    ReactiveCassandraBatchOperations batchOperations = new ReactiveCassandraBatchTemplate(template, BatchType.LOGGED);
    Mono<ReactiveResultSet> resultSet = batchOperations.update(Collections.singletonList(walter), options).update(Mono.just(Collections.singletonList(mike)), options).execute().then(template.getReactiveCqlOperations().queryForResultSet("SELECT TTL(email) FROM group;"));
    // 
    resultSet.flatMapMany(ReactiveResultSet::availableRows).as(// 
    StepVerifier::create).assertNext(row -> assertThat(row.getInt(0)).isBetween(1, ttl)).assertNext(row -> assertThat(row.getInt(0)).isBetween(1, ttl)).verifyComplete();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) DefaultBridgedReactiveSession(org.springframework.data.cassandra.core.cql.session.DefaultBridgedReactiveSession) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) ReactiveResultSet(org.springframework.data.cassandra.ReactiveResultSet) GroupKey(org.springframework.data.cassandra.domain.GroupKey) SchemaTestUtils(org.springframework.data.cassandra.repository.support.SchemaTestUtils) Mono(reactor.core.publisher.Mono) Random(java.util.Random) AbstractKeyspaceCreatingIntegrationTests(org.springframework.data.cassandra.test.util.AbstractKeyspaceCreatingIntegrationTests) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) FlatGroup(org.springframework.data.cassandra.domain.FlatGroup) Group(org.springframework.data.cassandra.domain.Group) BatchType(com.datastax.oss.driver.api.core.cql.BatchType) ReactiveCqlTemplate(org.springframework.data.cassandra.core.cql.ReactiveCqlTemplate) Assertions(org.assertj.core.api.Assertions) Schedulers(reactor.core.scheduler.Schedulers) Collections(java.util.Collections) WriteOptions(org.springframework.data.cassandra.core.cql.WriteOptions) Row(com.datastax.oss.driver.api.core.cql.Row) MappingCassandraConverter(org.springframework.data.cassandra.core.convert.MappingCassandraConverter) WriteOptions(org.springframework.data.cassandra.core.cql.WriteOptions) ReactiveResultSet(org.springframework.data.cassandra.ReactiveResultSet) Test(org.junit.jupiter.api.Test)

Example 3 with ReactiveResultSet

use of org.springframework.data.cassandra.ReactiveResultSet in project spring-data-cassandra by spring-projects.

the class ReactiveCassandraBatchTemplateIntegrationTests method shouldInsertMonoOfEntitiesWithTtl.

// DATACASS-574
@Test
void shouldInsertMonoOfEntitiesWithTtl() {
    walter.setEmail("walter@white.com");
    mike.setEmail("mike@sauls.com");
    int ttl = 30;
    WriteOptions options = WriteOptions.builder().ttl(30).build();
    ReactiveCassandraBatchOperations batchOperations = new ReactiveCassandraBatchTemplate(template, BatchType.LOGGED);
    Mono<ReactiveResultSet> resultSet = batchOperations.insert(Mono.just(Arrays.asList(walter, mike)), options).execute().then(template.getReactiveCqlOperations().queryForResultSet("SELECT TTL(email) FROM group;"));
    // 
    resultSet.flatMapMany(ReactiveResultSet::availableRows).as(// 
    StepVerifier::create).assertNext(row -> assertThat(row.getInt(0)).isBetween(1, ttl)).assertNext(row -> assertThat(row.getInt(0)).isBetween(1, ttl)).verifyComplete();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) DefaultBridgedReactiveSession(org.springframework.data.cassandra.core.cql.session.DefaultBridgedReactiveSession) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) ReactiveResultSet(org.springframework.data.cassandra.ReactiveResultSet) GroupKey(org.springframework.data.cassandra.domain.GroupKey) SchemaTestUtils(org.springframework.data.cassandra.repository.support.SchemaTestUtils) Mono(reactor.core.publisher.Mono) Random(java.util.Random) AbstractKeyspaceCreatingIntegrationTests(org.springframework.data.cassandra.test.util.AbstractKeyspaceCreatingIntegrationTests) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) FlatGroup(org.springframework.data.cassandra.domain.FlatGroup) Group(org.springframework.data.cassandra.domain.Group) BatchType(com.datastax.oss.driver.api.core.cql.BatchType) ReactiveCqlTemplate(org.springframework.data.cassandra.core.cql.ReactiveCqlTemplate) Assertions(org.assertj.core.api.Assertions) Schedulers(reactor.core.scheduler.Schedulers) Collections(java.util.Collections) WriteOptions(org.springframework.data.cassandra.core.cql.WriteOptions) Row(com.datastax.oss.driver.api.core.cql.Row) MappingCassandraConverter(org.springframework.data.cassandra.core.convert.MappingCassandraConverter) WriteOptions(org.springframework.data.cassandra.core.cql.WriteOptions) ReactiveResultSet(org.springframework.data.cassandra.ReactiveResultSet) Test(org.junit.jupiter.api.Test)

Example 4 with ReactiveResultSet

use of org.springframework.data.cassandra.ReactiveResultSet in project spring-data-cassandra by spring-projects.

the class DefaultBridgedReactiveSessionUnitTests method shouldReadAvailableResults.

// DATACASS-529
@Test
void shouldReadAvailableResults() {
    AsyncResultSet resultSet = mock(AsyncResultSet.class);
    when(resultSet.remaining()).thenReturn(10);
    when(resultSet.currentPage()).thenReturn(IntStream.range(0, 10).mapToObj(value -> mock(Row.class)).collect(Collectors.toList()));
    future.complete(resultSet);
    Flux<Row> flux = reactiveSession.execute(SimpleStatement.newInstance("")).flatMapMany(ReactiveResultSet::availableRows);
    flux.as(StepVerifier::create).expectNextCount(10).verifyComplete();
    verify(resultSet, never()).fetchNextPage();
}
Also used : AsyncResultSet(com.datastax.oss.driver.api.core.cql.AsyncResultSet) ReactiveResultSet(org.springframework.data.cassandra.ReactiveResultSet) Row(com.datastax.oss.driver.api.core.cql.Row) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 5 with ReactiveResultSet

use of org.springframework.data.cassandra.ReactiveResultSet in project spring-data-cassandra by spring-projects.

the class ReactiveCassandraTemplateUnitTests method setUp.

@BeforeEach
void setUp() {
    when(driverContext.getCodecRegistry()).thenReturn(codecRegistry);
    when(session.getContext()).thenReturn(driverContext);
    when(session.execute(any(Statement.class))).thenReturn(Mono.just(reactiveResultSet));
    when(row.getColumnDefinitions()).thenReturn(columnDefinitions);
    ReactiveEntityCallbacks callbacks = ReactiveEntityCallbacks.create();
    callbacks.addEntityCallback((ReactiveBeforeSaveCallback<Object>) (entity, tableName, statement) -> {
        assertThat(tableName).isNotNull();
        assertThat(statement).isNotNull();
        beforeSave = entity;
        return Mono.just(entity);
    });
    callbacks.addEntityCallback((ReactiveBeforeConvertCallback<Object>) (entity, tableName) -> {
        assertThat(tableName).isNotNull();
        beforeConvert = entity;
        return Mono.just(entity);
    });
    template = new ReactiveCassandraTemplate(session);
    template.setUsePreparedStatements(false);
    template.setEntityCallbacks(callbacks);
}
Also used : Strictness(org.mockito.quality.Strictness) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers(org.mockito.ArgumentMatchers) StepVerifier(reactor.test.StepVerifier) MockitoSettings(org.mockito.junit.jupiter.MockitoSettings) DefaultCodecRegistry(com.datastax.oss.driver.internal.core.type.codec.registry.DefaultCodecRegistry) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) Mock(org.mockito.Mock) Filter(org.springframework.data.cassandra.core.query.Filter) VersionedUser(org.springframework.data.cassandra.domain.VersionedUser) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) Captor(org.mockito.Captor) ReactiveBeforeConvertCallback(org.springframework.data.cassandra.core.mapping.event.ReactiveBeforeConvertCallback) ArgumentCaptor(org.mockito.ArgumentCaptor) User(org.springframework.data.cassandra.domain.User) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ColumnDefinitions(com.datastax.oss.driver.api.core.cql.ColumnDefinitions) NoNodeAvailableException(com.datastax.oss.driver.api.core.NoNodeAvailableException) Assertions(org.assertj.core.api.Assertions) ReactiveSession(org.springframework.data.cassandra.ReactiveSession) Row(com.datastax.oss.driver.api.core.cql.Row) ColumnDefinition(com.datastax.oss.driver.api.core.cql.ColumnDefinition) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) CodecRegistry(com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry) Criteria(org.springframework.data.cassandra.core.query.Criteria) Update(org.springframework.data.cassandra.core.query.Update) ReactiveEntityCallbacks(org.springframework.data.mapping.callback.ReactiveEntityCallbacks) DriverContext(com.datastax.oss.driver.api.core.context.DriverContext) ReactiveResultSet(org.springframework.data.cassandra.ReactiveResultSet) Mono(reactor.core.publisher.Mono) ReactiveBeforeSaveCallback(org.springframework.data.cassandra.core.mapping.event.ReactiveBeforeSaveCallback) Query(org.springframework.data.cassandra.core.query.Query) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) Flux(reactor.core.publisher.Flux) List(java.util.List) DataTypes(com.datastax.oss.driver.api.core.type.DataTypes) Statement(com.datastax.oss.driver.api.core.cql.Statement) ReactiveEntityCallbacks(org.springframework.data.mapping.callback.ReactiveEntityCallbacks) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) Statement(com.datastax.oss.driver.api.core.cql.Statement) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

Row (com.datastax.oss.driver.api.core.cql.Row)9 ReactiveResultSet (org.springframework.data.cassandra.ReactiveResultSet)9 Test (org.junit.jupiter.api.Test)8 Mono (reactor.core.publisher.Mono)6 StepVerifier (reactor.test.StepVerifier)6 BatchType (com.datastax.oss.driver.api.core.cql.BatchType)5 Collections (java.util.Collections)5 Assertions (org.assertj.core.api.Assertions)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 MappingCassandraConverter (org.springframework.data.cassandra.core.convert.MappingCassandraConverter)5 Arrays (java.util.Arrays)4 Random (java.util.Random)4 TimeUnit (java.util.concurrent.TimeUnit)4 ReactiveCqlTemplate (org.springframework.data.cassandra.core.cql.ReactiveCqlTemplate)4 WriteOptions (org.springframework.data.cassandra.core.cql.WriteOptions)4 DefaultBridgedReactiveSession (org.springframework.data.cassandra.core.cql.session.DefaultBridgedReactiveSession)4 FlatGroup (org.springframework.data.cassandra.domain.FlatGroup)4 Group (org.springframework.data.cassandra.domain.Group)4 GroupKey (org.springframework.data.cassandra.domain.GroupKey)4 SchemaTestUtils (org.springframework.data.cassandra.repository.support.SchemaTestUtils)4