use of com.datastax.driver.core.ResultSetFuture in project thingsboard by thingsboard.
the class CassandraAbstractModelDao method findOneByStatementAsync.
protected ListenableFuture<D> findOneByStatementAsync(Statement statement) {
if (statement != null) {
statement.setConsistencyLevel(cluster.getDefaultReadConsistencyLevel());
ResultSetFuture resultSetFuture = executeAsyncRead(statement);
return Futures.transform(resultSetFuture, new Function<ResultSet, D>() {
@Nullable
@Override
public D apply(@Nullable ResultSet resultSet) {
Result<E> result = getMapper().map(resultSet);
if (result != null) {
E entity = result.one();
return DaoUtil.getData(entity);
} else {
return null;
}
}
});
}
return Futures.immediateFuture(null);
}
use of com.datastax.driver.core.ResultSetFuture in project thingsboard by thingsboard.
the class RateLimitedResultSetFuture method executeAsyncWithRelease.
private ResultSetFuture executeAsyncWithRelease(AsyncRateLimiter rateLimiter, Session session, Statement statement) {
try {
ResultSetFuture resultSetFuture = session.executeAsync(statement);
Futures.addCallback(resultSetFuture, new FutureCallback<ResultSet>() {
@Override
public void onSuccess(@Nullable ResultSet result) {
rateLimiter.release();
}
@Override
public void onFailure(Throwable t) {
rateLimiter.release();
}
});
return resultSetFuture;
} catch (RuntimeException re) {
rateLimiter.release();
throw re;
}
}
use of com.datastax.driver.core.ResultSetFuture in project thingsboard by thingsboard.
the class RateLimitedResultSetFuture method get.
@Override
public ResultSet get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
long rateLimitStart = System.nanoTime();
ResultSetFuture resultSetFuture = originalFuture.get(timeout, unit);
long rateLimitDurationNano = System.nanoTime() - rateLimitStart;
long innerTimeoutNano = unit.toNanos(timeout) - rateLimitDurationNano;
if (innerTimeoutNano > 0) {
return resultSetFuture.get(innerTimeoutNano, TimeUnit.NANOSECONDS);
}
throw new TimeoutException("Timeout waiting for task.");
}
use of com.datastax.driver.core.ResultSetFuture in project cassandra-driver-mapping by valchkou.
the class MappingSessionAsyncTest method appendToListTest.
@Test
public void appendToListTest() throws Exception {
UUID id = UUID.randomUUID();
EntityWithCollections obj = new EntityWithCollections();
obj.setId(id);
List<Integer> trades = new ArrayList<Integer>();
trades.add(1);
trades.add(2);
obj.setTrades(trades);
ResultSetFuture f = target.saveAsync(obj);
f.getUninterruptibly();
EntityWithCollections loaded = target.get(EntityWithCollections.class, id);
assertEquals(2, loaded.getTrades().size());
f = target.appendAsync(id, EntityWithCollections.class, "trades", 3);
f.getUninterruptibly();
loaded = target.get(EntityWithCollections.class, id);
assertTrue(loaded.getTrades().contains(3));
assertEquals(new Integer(3), loaded.getTrades().get(2));
}
use of com.datastax.driver.core.ResultSetFuture in project cassandra-driver-mapping by valchkou.
the class MappingSessionAsyncTest method appendAllToSetTest.
@Test
public void appendAllToSetTest() throws Exception {
UUID id = UUID.randomUUID();
EntityWithCollections obj = new EntityWithCollections();
obj.setId(id);
Set<String> refs = new HashSet<String>();
refs.add("100");
refs.add("abc");
obj.setRefs(refs);
ResultSetFuture f = target.saveAsync(obj);
f.getUninterruptibly();
EntityWithCollections loaded = target.get(EntityWithCollections.class, id);
assertEquals(2, loaded.getRefs().size());
Set<String> adds = new HashSet<String>();
adds.add("fgdsfgdsfgd");
adds.add("200");
f = target.appendAsync(id, EntityWithCollections.class, "refs", adds);
f.getUninterruptibly();
loaded = target.get(EntityWithCollections.class, id);
assertTrue(loaded.getRefs().contains("fgdsfgdsfgd"));
assertEquals(4, loaded.getRefs().size());
}
Aggregations