use of com.datastax.driver.core.ResultSetFuture in project cassandra-driver-mapping by valchkou.
the class MappingSessionAsyncTest method saveEntityWithEnumTest.
@Test
public void saveEntityWithEnumTest() throws Exception {
UUID uuid = UUID.randomUUID();
EntityWithEnum obj = new EntityWithEnum();
obj.setId(uuid);
obj.setMonth(Month.JUNE);
EntityWithEnum loaded = target.get(EntityWithEnum.class, uuid);
assertNull(loaded);
ResultSetFuture f = target.saveAsync(obj);
f.getUninterruptibly();
loaded = target.get(EntityWithEnum.class, uuid);
assertEquals(obj, loaded);
obj.setMonth(Month.APRIL);
f = target.saveAsync(obj);
f.getUninterruptibly();
loaded = target.get(EntityWithEnum.class, uuid);
assertEquals(obj, loaded);
f = target.deleteAsync(loaded);
f.getUninterruptibly();
loaded = target.get(EntityWithEnum.class, uuid);
assertNull(loaded);
}
use of com.datastax.driver.core.ResultSetFuture in project cassandra-driver-mapping by valchkou.
the class MappingSessionAsyncTest method updateSelectedPropertiesTest.
@Test
public void updateSelectedPropertiesTest() throws Exception {
UUID uuid = UUID.randomUUID();
Simple obj = new Simple();
obj.setName("myName");
obj.setAge(55);
obj.setId(uuid);
ResultSetFuture f = target.saveAsync(obj);
f.getUninterruptibly();
String[] props = { "name", "age" };
Object[] vals = { "yourName", 25 };
f = target.updateValuesAsync(uuid, Simple.class, props, vals);
f.getUninterruptibly();
Simple loaded = target.get(Simple.class, uuid);
assertEquals(25, loaded.getAge());
assertEquals("yourName", loaded.getName());
}
use of com.datastax.driver.core.ResultSetFuture in project cassandra by apache.
the class DeleteTest method lostDeletesTest.
@Test
public void lostDeletesTest() {
Session session = sessionNet();
for (int i = 0; i < 500; i++) {
session.execute(pstmtI.bind(1, 1, "inhB", "valB"));
ResultSetFuture[] futures = load();
Assert.assertTrue(futures[0].getUninterruptibly().isExhausted());
Assert.assertTrue(futures[1].getUninterruptibly().isExhausted());
Assert.assertNotNull(futures[2].getUninterruptibly().one());
Assert.assertTrue(futures[3].getUninterruptibly().isExhausted());
Assert.assertTrue(futures[4].getUninterruptibly().isExhausted());
session.execute(pstmtU.bind("inhBu", "valBu", 1, 1));
futures = load();
Assert.assertTrue(futures[0].getUninterruptibly().isExhausted());
Assert.assertTrue(futures[1].getUninterruptibly().isExhausted());
Assert.assertNotNull(futures[2].getUninterruptibly().one());
Assert.assertTrue(futures[3].getUninterruptibly().isExhausted());
Assert.assertTrue(futures[4].getUninterruptibly().isExhausted());
session.execute(pstmtD.bind(1, 1));
futures = load();
Assert.assertTrue(futures[0].getUninterruptibly().isExhausted());
Assert.assertTrue(futures[1].getUninterruptibly().isExhausted());
Assert.assertTrue(futures[2].getUninterruptibly().isExhausted());
Assert.assertTrue(futures[3].getUninterruptibly().isExhausted());
Assert.assertTrue(futures[4].getUninterruptibly().isExhausted());
}
}
use of com.datastax.driver.core.ResultSetFuture in project storm by apache.
the class AsyncExecutor method execAsync.
/**
* Asynchronously executes the specified batch statement. Inputs will be passed to
* the {@link #handler} once query succeed or failed.
*/
public SettableFuture<T> execAsync(final Statement statement, final T inputs, final AsyncResultHandler<T> handler) {
final SettableFuture<T> settableFuture = SettableFuture.create();
pending.incrementAndGet();
ResultSetFuture future = session.executeAsync(statement);
Futures.addCallback(future, new FutureCallback<ResultSet>() {
public void release() {
pending.decrementAndGet();
}
@Override
public void onSuccess(ResultSet result) {
release();
settableFuture.set(inputs);
handler.success(inputs);
}
@Override
public void onFailure(Throwable t) {
LOG.error(String.format("Failed to execute statement '%s' ", statement), t);
release();
settableFuture.setException(t);
handler.failure(t, inputs);
}
}, executorService);
return settableFuture;
}
use of com.datastax.driver.core.ResultSetFuture in project storm by apache.
the class AsyncExecutor method execAsync.
/**
* Asynchronously executes the specified select statements. Results will be passed to the {@link AsyncResultSetHandler}
* once each query has succeed or failed.
*/
public SettableFuture<List<T>> execAsync(final List<Statement> statements, final List<T> inputs, Semaphore throttle, final AsyncResultSetHandler<T> handler) {
final SettableFuture<List<T>> settableFuture = SettableFuture.create();
if (inputs.size() == 0) {
settableFuture.set(new ArrayList<T>());
return settableFuture;
}
final AsyncContext<T> asyncContext = new AsyncContext<>(inputs, throttle, settableFuture);
for (int i = 0; i < statements.size(); i++) {
// Acquire a slot
if (asyncContext.acquire()) {
try {
pending.incrementAndGet();
final T input = inputs.get(i);
final Statement statement = statements.get(i);
ResultSetFuture future = session.executeAsync(statement);
Futures.addCallback(future, new FutureCallback<ResultSet>() {
@Override
public void onSuccess(ResultSet result) {
try {
handler.success(input, result);
} catch (Throwable throwable) {
asyncContext.exception(throwable);
} finally {
pending.decrementAndGet();
asyncContext.release();
}
}
@Override
public void onFailure(Throwable throwable) {
try {
handler.failure(throwable, input);
} catch (Throwable throwable2) {
asyncContext.exception(throwable2);
} finally {
asyncContext.exception(throwable).release();
pending.decrementAndGet();
LOG.error(String.format("Failed to execute statement '%s' ", statement), throwable);
}
}
}, executorService);
} catch (Throwable throwable) {
asyncContext.exception(throwable).release();
pending.decrementAndGet();
break;
}
}
}
return settableFuture;
}
Aggregations