Search in sources :

Example 21 with ResultSetFuture

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);
}
Also used : ResultSetFuture(com.datastax.driver.core.ResultSetFuture) UUID(java.util.UUID) EntityResultSet(org.thingsboard.server.dao.model.wrapper.EntityResultSet) ResultSet(com.datastax.driver.core.ResultSet) Nullable(javax.annotation.Nullable) Result(com.datastax.driver.mapping.Result)

Example 22 with ResultSetFuture

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;
    }
}
Also used : ResultSetFuture(com.datastax.driver.core.ResultSetFuture) ResultSet(com.datastax.driver.core.ResultSet)

Example 23 with ResultSetFuture

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.");
}
Also used : ResultSetFuture(com.datastax.driver.core.ResultSetFuture)

Example 24 with ResultSetFuture

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));
}
Also used : ResultSetFuture(com.datastax.driver.core.ResultSetFuture) ArrayList(java.util.ArrayList) EntityWithCollections(com.datastax.driver.mapping.entity.EntityWithCollections) UUID(java.util.UUID) Test(org.junit.Test)

Example 25 with ResultSetFuture

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());
}
Also used : ResultSetFuture(com.datastax.driver.core.ResultSetFuture) EntityWithCollections(com.datastax.driver.mapping.entity.EntityWithCollections) UUID(java.util.UUID) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ResultSetFuture (com.datastax.driver.core.ResultSetFuture)78 Test (org.junit.Test)35 UUID (java.util.UUID)26 ResultSet (com.datastax.driver.core.ResultSet)20 EntityWithCollections (com.datastax.driver.mapping.entity.EntityWithCollections)13 BoundStatement (com.datastax.driver.core.BoundStatement)12 PreparedStatement (com.datastax.driver.core.PreparedStatement)10 ArrayList (java.util.ArrayList)8 Date (java.util.Date)8 Row (com.datastax.driver.core.Row)7 Statement (com.datastax.driver.core.Statement)7 List (java.util.List)6 Session (com.datastax.driver.core.Session)5 Simple (com.datastax.driver.mapping.entity.Simple)5 Cell (com.palantir.atlasdb.keyvalue.api.Cell)5 BigDecimal (java.math.BigDecimal)5 HashSet (java.util.HashSet)5 Result (com.datastax.driver.mapping.Result)4 WriteOptions (com.datastax.driver.mapping.option.WriteOptions)4 Nullable (javax.annotation.Nullable)4