Search in sources :

Example 51 with ResultSetFuture

use of com.datastax.driver.core.ResultSetFuture in project thingsboard by thingsboard.

the class CassandraAbstractModelDao method findListByStatementAsync.

protected ListenableFuture<List<D>> findListByStatementAsync(Statement statement) {
    if (statement != null) {
        statement.setConsistencyLevel(cluster.getDefaultReadConsistencyLevel());
        ResultSetFuture resultSetFuture = executeAsyncRead(statement);
        return Futures.transform(resultSetFuture, new Function<ResultSet, List<D>>() {

            @Nullable
            @Override
            public List<D> apply(@Nullable ResultSet resultSet) {
                Result<E> result = getMapper().map(resultSet);
                if (result != null) {
                    List<E> entities = result.all();
                    return DaoUtil.convertDataList(entities);
                } else {
                    return Collections.emptyList();
                }
            }
        });
    }
    return Futures.immediateFuture(Collections.emptyList());
}
Also used : ResultSetFuture(com.datastax.driver.core.ResultSetFuture) EntityResultSet(org.thingsboard.server.dao.model.wrapper.EntityResultSet) ResultSet(com.datastax.driver.core.ResultSet) List(java.util.List) Nullable(javax.annotation.Nullable) Result(com.datastax.driver.mapping.Result)

Example 52 with ResultSetFuture

use of com.datastax.driver.core.ResultSetFuture in project thingsboard by thingsboard.

the class RateLimitedResultSetFuture method addListener.

@Override
public void addListener(Runnable listener, Executor executor) {
    originalFuture.addListener(() -> {
        try {
            ResultSetFuture resultSetFuture = Uninterruptibles.getUninterruptibly(originalFuture);
            resultSetFuture.addListener(listener, executor);
        } catch (CancellationException | ExecutionException e) {
            Futures.immediateFailedFuture(e).addListener(listener, executor);
        }
    }, executor);
}
Also used : ResultSetFuture(com.datastax.driver.core.ResultSetFuture)

Example 53 with ResultSetFuture

use of com.datastax.driver.core.ResultSetFuture in project thingsboard by thingsboard.

the class RateLimitedResultSetFuture method getUninterruptibly.

@Override
public ResultSet getUninterruptibly(long timeout, TimeUnit unit) throws TimeoutException {
    long rateLimitStart = System.nanoTime();
    ResultSetFuture resultSetFuture = null;
    try {
        resultSetFuture = originalFuture.get(timeout, unit);
    } catch (InterruptedException | ExecutionException e) {
        throw new IllegalStateException(e);
    }
    long rateLimitDurationNano = System.nanoTime() - rateLimitStart;
    long innerTimeoutNano = unit.toNanos(timeout) - rateLimitDurationNano;
    if (innerTimeoutNano > 0) {
        return resultSetFuture.getUninterruptibly(innerTimeoutNano, TimeUnit.NANOSECONDS);
    }
    throw new TimeoutException("Timeout waiting for task.");
}
Also used : ResultSetFuture(com.datastax.driver.core.ResultSetFuture)

Example 54 with ResultSetFuture

use of com.datastax.driver.core.ResultSetFuture in project thingsboard by thingsboard.

the class CassandraAssetDao method findTenantAssetTypesAsync.

@Override
public ListenableFuture<List<EntitySubtype>> findTenantAssetTypesAsync(UUID tenantId) {
    Select select = select().from(ENTITY_SUBTYPE_COLUMN_FAMILY_NAME);
    Select.Where query = select.where();
    query.and(eq(ENTITY_SUBTYPE_TENANT_ID_PROPERTY, tenantId));
    query.and(eq(ENTITY_SUBTYPE_ENTITY_TYPE_PROPERTY, EntityType.ASSET));
    query.setConsistencyLevel(cluster.getDefaultReadConsistencyLevel());
    ResultSetFuture resultSetFuture = executeAsyncRead(query);
    return Futures.transform(resultSetFuture, new Function<ResultSet, List<EntitySubtype>>() {

        @Nullable
        @Override
        public List<EntitySubtype> apply(@Nullable ResultSet resultSet) {
            Result<EntitySubtypeEntity> result = cluster.getMapper(EntitySubtypeEntity.class).map(resultSet);
            if (result != null) {
                List<EntitySubtype> entitySubtypes = new ArrayList<>();
                result.all().forEach((entitySubtypeEntity) -> entitySubtypes.add(entitySubtypeEntity.toEntitySubtype()));
                return entitySubtypes;
            } else {
                return Collections.emptyList();
            }
        }
    });
}
Also used : java.util(java.util) QueryBuilder(com.datastax.driver.core.querybuilder.QueryBuilder) Function(com.google.common.base.Function) CassandraAbstractSearchTextDao(org.thingsboard.server.dao.nosql.CassandraAbstractSearchTextDao) Result(com.datastax.driver.mapping.Result) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ResultSetFuture(com.datastax.driver.core.ResultSetFuture) NoSqlDao(org.thingsboard.server.dao.util.NoSqlDao) Futures(com.google.common.util.concurrent.Futures) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) AssetEntity(org.thingsboard.server.dao.model.nosql.AssetEntity) ResultSet(com.datastax.driver.core.ResultSet) EntityType(org.thingsboard.server.common.data.EntityType) DaoUtil(org.thingsboard.server.dao.DaoUtil) EntitySubtypeEntity(org.thingsboard.server.dao.model.EntitySubtypeEntity) Select(com.datastax.driver.core.querybuilder.Select) EntitySubtype(org.thingsboard.server.common.data.EntitySubtype) Statement(com.datastax.driver.core.Statement) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) Nullable(javax.annotation.Nullable) Asset(org.thingsboard.server.common.data.asset.Asset) ModelConstants(org.thingsboard.server.dao.model.ModelConstants) ResultSetFuture(com.datastax.driver.core.ResultSetFuture) Select(com.datastax.driver.core.querybuilder.Select) ResultSet(com.datastax.driver.core.ResultSet) Nullable(javax.annotation.Nullable) Result(com.datastax.driver.mapping.Result)

Example 55 with ResultSetFuture

use of com.datastax.driver.core.ResultSetFuture in project wildfly-swarm by wildfly-swarm.

the class StatefulTestBean method asyncQuery.

public Row asyncQuery() {
    openConnection();
    try {
        session.execute("CREATE TABLE employee (lastname varchar primary key, firstname varchar, age int, city varchar, email varchar)");
        session.execute("INSERT INTO employee (lastname, firstname, age, city, email) VALUES ('Smith','Leanne', 30, 'Boston', 'lea@yahoo.com')");
        session.execute("update employee set age = 36 where lastname = 'Smith'");
        // Select and show the change
        try {
            ResultSetFuture results = session.executeAsync("select * from employee where lastname='Smith'");
            return results.get().one();
        } catch (Throwable exception) {
            throw new RuntimeException("could not get executeAsync result for some reason", exception);
        }
    } finally {
        try {
            session.execute("DROP TABLE employee");
        } catch (Throwable ignore) {
        }
        closeConnection();
    }
}
Also used : ResultSetFuture(com.datastax.driver.core.ResultSetFuture)

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