Search in sources :

Example 1 with TbResultSet

use of org.thingsboard.server.dao.nosql.TbResultSet in project thingsboard by thingsboard.

the class CassandraBaseTimeseriesDao method findAllAsyncSequentiallyWithLimit.

private void findAllAsyncSequentiallyWithLimit(TenantId tenantId, final TsKvQueryCursor cursor, final SimpleListenableFuture<List<TsKvEntry>> resultFuture) {
    if (cursor.isFull() || !cursor.hasNextPartition()) {
        resultFuture.set(cursor.getData());
    } else {
        PreparedStatement proto = getFetchStmt(Aggregation.NONE, cursor.getOrderBy());
        BoundStatementBuilder stmtBuilder = new BoundStatementBuilder(proto.bind());
        stmtBuilder.setString(0, cursor.getEntityType());
        stmtBuilder.setUuid(1, cursor.getEntityId());
        stmtBuilder.setString(2, cursor.getKey());
        stmtBuilder.setLong(3, cursor.getNextPartition());
        stmtBuilder.setLong(4, cursor.getStartTs());
        stmtBuilder.setLong(5, cursor.getEndTs());
        stmtBuilder.setInt(6, cursor.getCurrentLimit());
        BoundStatement stmt = stmtBuilder.build();
        Futures.addCallback(executeAsyncRead(tenantId, stmt), new FutureCallback<TbResultSet>() {

            @Override
            public void onSuccess(@Nullable TbResultSet result) {
                if (result == null) {
                    cursor.addData(convertResultToTsKvEntryList(Collections.emptyList()));
                    findAllAsyncSequentiallyWithLimit(tenantId, cursor, resultFuture);
                } else {
                    Futures.addCallback(result.allRows(readResultsProcessingExecutor), new FutureCallback<List<Row>>() {

                        @Override
                        public void onSuccess(@Nullable List<Row> result) {
                            cursor.addData(convertResultToTsKvEntryList(result == null ? Collections.emptyList() : result));
                            findAllAsyncSequentiallyWithLimit(tenantId, cursor, resultFuture);
                        }

                        @Override
                        public void onFailure(Throwable t) {
                            log.error("[{}][{}] Failed to fetch data for query {}-{}", stmt, t);
                        }
                    }, readResultsProcessingExecutor);
                }
            }

            @Override
            public void onFailure(Throwable t) {
                log.error("[{}][{}] Failed to fetch data for query {}-{}", stmt, t);
            }
        }, readResultsProcessingExecutor);
    }
}
Also used : TbResultSet(org.thingsboard.server.dao.nosql.TbResultSet) BoundStatementBuilder(com.datastax.oss.driver.api.core.cql.BoundStatementBuilder) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) List(java.util.List) ArrayList(java.util.ArrayList) Row(com.datastax.oss.driver.api.core.cql.Row) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement) FutureCallback(com.google.common.util.concurrent.FutureCallback) Nullable(javax.annotation.Nullable)

Aggregations

BoundStatement (com.datastax.oss.driver.api.core.cql.BoundStatement)1 BoundStatementBuilder (com.datastax.oss.driver.api.core.cql.BoundStatementBuilder)1 PreparedStatement (com.datastax.oss.driver.api.core.cql.PreparedStatement)1 Row (com.datastax.oss.driver.api.core.cql.Row)1 FutureCallback (com.google.common.util.concurrent.FutureCallback)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Nullable (javax.annotation.Nullable)1 TbResultSet (org.thingsboard.server.dao.nosql.TbResultSet)1