Skip to content

Instantly share code, notes, and snippets.

@mp911de
Created September 2, 2016 15:23
Show Gist options
  • Select an option

  • Save mp911de/518f1757cc1d3519c1ecf995d6af164b to your computer and use it in GitHub Desktop.

Select an option

Save mp911de/518f1757cc1d3519c1ecf995d6af164b to your computer and use it in GitHub Desktop.
Reactive Spring Data Cassandra Experiment
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cassandra.core;
import java.util.concurrent.CompletableFuture;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.cassandra.support.CassandraAccessor;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.ResultSetFuture;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.Statement;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Scheduler;
import reactor.core.scheduler.Schedulers;
/**
* @author Mark Paluch
*/
public class ReactiveCqlTemplate extends CassandraAccessor implements DisposableBean {
private Scheduler scheduler;
@Override
public void afterPropertiesSet() {
super.afterPropertiesSet();
scheduler = Schedulers.newElastic(getClass().getSimpleName());
}
@Override
public void destroy() throws Exception {
scheduler.shutdown();
}
public Mono<ResultSet> doWithSession(SessionCallback<ResultSetFuture> callback) {
Session session = getSession();
return Mono.defer(() -> {
ResultSetFuture resultSetFuture = callback.doInSession(session);
final CompletableFuture<ResultSet> future = new CompletableFuture<>();
resultSetFuture.addListener(() -> {
if (resultSetFuture.isDone()) {
try {
future.complete(resultSetFuture.getUninterruptibly());
} catch (Exception e) {
future.completeExceptionally(e);
}
}
}, Runnable::run);
return Mono.fromFuture(future);
}).subscribeOn(scheduler);
}
/**
* Return a {@link Mono} that emits the {@link ResultSet}.
*
* @param cql
* @return
*/
public Mono<ResultSet> execute(String cql) {
return doWithSession(s -> s.executeAsync(cql));
}
/**
* Return a {@link Flux} that emits {@link Row rows} from the result set.
*
* @param cql
* @return
*/
public Flux<Row> query(String cql) {
return execute(cql).flatMap(rows -> Flux.fromIterable(rows));
}
/**
* Return a {@link Mono} that emits the {@link ResultSet}.
*
* @param statement
* @return
*/
public Mono<ResultSet> execute(Statement statement) {
return doWithSession(s -> s.executeAsync(statement));
}
/**
* Return a {@link Flux} that emits {@link Row rows} from the result set.
*
* @param statement
* @return
*/
public Flux<Row> query(Statement statement) {
return execute(statement).flatMap(rows -> Flux.fromIterable(rows));
}
}
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cassandra.core;
import org.reactivestreams.Publisher;
import com.datastax.driver.core.ColumnDefinitions;
import com.datastax.driver.core.ExecutionInfo;
import com.datastax.driver.core.PagingState;
import com.datastax.driver.core.Row;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
/**
* @author Mark Paluch
*/
public interface RowPublisher extends Publisher<Row> {
/**
* Returns the columns returned in this RowPublisher.
*
* @return the columns returned in this RowPublisher.
*/
Mono<ColumnDefinitions> getColumnDefinitions();
/**
* Returns the next paging state in this RowPublisher.
*
* @return the next paging state in this RowPublisher.
*/
Mono<PagingState> getPagingState();
/**
* Returns the next paging state in this RowPublisher.
*
* @return the next paging state in this RowPublisher.
*/
Mono<PagingState> getNextPagingState();
/**
* Returns information on the execution of the last query made for this RowPublisher.
*
* @return the execution info for the last query made for this RowPublisher.
*/
Mono<ExecutionInfo> getExecutionInfo();
/**
* Return the execution information for all queries made to retrieve this RowPublisher.
*
* @return a list of the execution info for all the queries made for this RowPublisher.
*/
Flux<ExecutionInfo> getAllExecutionInfo();
/**
* If the query that produced this RowPublisher was a conditional update, return whether it was successfully applied.
*/
Mono<Boolean> wasApplied();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment