
net.oneandone.troilus.MutationQuery Maven / Gradle / Ivy
/*
* Copyright 1&1 Internet AG, https://github.com/1and1/
*
* 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 net.oneandone.troilus;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import net.oneandone.troilus.java7.Batchable;
import com.datastax.driver.core.BatchStatement;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Statement;
import com.datastax.driver.core.BatchStatement.Type;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.google.common.collect.UnmodifiableIterator;
import com.google.common.util.concurrent.AbstractFuture;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;
/**
* abstract mutation query implementation
*/
abstract class MutationQuery extends AbstractQuery {
/**
* @param ctx the context
*/
MutationQuery(Context ctx) {
super(ctx);
}
public Result execute() {
return ListenableFutures.getUninterruptibly(executeAsync());
}
public ListenableFuture executeAsync() {
ListenableFuture future = performAsync(getStatementAsync());
Function mapEntity = new Function() {
@Override
public Result apply(ResultSet resultSet) {
return newResult(resultSet);
}
};
return Futures.transform(future, mapEntity);
}
public abstract ListenableFuture getStatementAsync();
protected ListenableFuture mergeStatements(ListenableFuture statementFuture, ListenableFuture> cascadingStatmentsFuture) {
ListenableFuture> statementsFuture = ListenableFutures.join(cascadingStatmentsFuture, statementFuture, getContext().getTaskExecutor());
Function, Statement> statementsBatcher = new Function, Statement>() {
public Statement apply(ImmutableSet statements) {
BatchStatement batchStatement = new BatchStatement(Type.LOGGED);
for (Statement statement : statements) {
batchStatement.add(statement);
}
return batchStatement;
};
};
return Futures.transform(statementsFuture, statementsBatcher);
}
protected ListenableFuture> transformBatchablesToStatement(ListenableFuture>> batchablesFutureSet) {
Function>, ImmutableSet>> batchablesToStatement = new Function>, ImmutableSet>>() {
@Override
public ImmutableSet> apply(ImmutableSet extends Batchable>> batchables) {
Set> statementFutureSet = Sets.newHashSet();
for(Batchable> batchable : batchables) {
statementFutureSet.add(batchable.getStatementAsync());
}
return ImmutableSet.copyOf(statementFutureSet);
}
};
ListenableFuture>> statementFutureSet = Futures.transform(batchablesFutureSet, batchablesToStatement);
return ListenableFutures.flat(statementFutureSet, getContext().getTaskExecutor());
}
protected ListenableFuture mergeToBatch(Type batchType, UnmodifiableIterator batchablesIt, Function> statementFetcher) {
return new BatchQueryFutureAdapter<>(new BatchStatement(batchType), batchablesIt, statementFetcher);
}
protected static final class BatchQueryFutureAdapter extends AbstractFuture {
BatchQueryFutureAdapter(BatchStatement batchStmt, UnmodifiableIterator batchablesIt, Function> statementFetcher) {
handle(batchStmt, batchablesIt, statementFetcher);
}
private void handle(final BatchStatement batchStmt, final UnmodifiableIterator batchablesIt, final Function> statementFetcher) {
if (batchablesIt.hasNext()) {
final ListenableFuture statementFuture = statementFetcher.apply(batchablesIt.next());
Runnable resultHandler = new Runnable() {
@Override
public void run() {
try {
batchStmt.add(statementFuture.get());
handle(batchStmt, batchablesIt, statementFetcher);
} catch (InterruptedException | ExecutionException | RuntimeException e) {
setException(ListenableFutures.unwrapIfNecessary(e));
}
}
};
statementFuture.addListener(resultHandler, MoreExecutors.directExecutor());
} else {
set(batchStmt);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy