Search in sources :

Example 1 with BoundStatement

use of com.datastax.oss.driver.api.core.cql.BoundStatement in project trellis-cassandra by ajs6f.

the class CassandraQuery method executeWrite.

/**
 * @param statement the CQL statement to execute
 * @return when and whether the statement completed
 */
protected CompletionStage<Void> executeWrite(BoundStatement statement) {
    String queryString = statement.getPreparedStatement().getQuery();
    log.debug("Executing CQL write: {}", queryString);
    BoundStatement consistentStatement = statement.setConsistencyLevel(consistency);
    return session.executeAsync(consistentStatement).thenAccept(r -> log.debug("Executed CQL write: {}", queryString));
}
Also used : BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement)

Example 2 with BoundStatement

use of com.datastax.oss.driver.api.core.cql.BoundStatement in project thingsboard by thingsboard.

the class CassandraBaseTimeseriesDao method deletePartitionAsync.

private void deletePartitionAsync(TenantId tenantId, final QueryCursor cursor, final SimpleListenableFuture<Void> resultFuture) {
    if (!cursor.hasNextPartition()) {
        resultFuture.set(null);
    } else {
        PreparedStatement proto = getDeletePartitionStmt();
        BoundStatementBuilder stmtBuilder = new BoundStatementBuilder(proto.bind());
        stmtBuilder.setString(0, cursor.getEntityType());
        stmtBuilder.setUuid(1, cursor.getEntityId());
        stmtBuilder.setLong(2, cursor.getNextPartition());
        stmtBuilder.setString(3, cursor.getKey());
        BoundStatement stmt = stmtBuilder.build();
        Futures.addCallback(executeAsyncWrite(tenantId, stmt), new FutureCallback<AsyncResultSet>() {

            @Override
            public void onSuccess(@Nullable AsyncResultSet result) {
                deletePartitionAsync(tenantId, cursor, resultFuture);
            }

            @Override
            public void onFailure(Throwable t) {
                log.error("[{}][{}] Failed to delete data for query {}-{}", stmt, t);
            }
        }, readResultsProcessingExecutor);
    }
}
Also used : BoundStatementBuilder(com.datastax.oss.driver.api.core.cql.BoundStatementBuilder) AsyncResultSet(com.datastax.oss.driver.api.core.cql.AsyncResultSet) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement)

Example 3 with BoundStatement

use of com.datastax.oss.driver.api.core.cql.BoundStatement 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)

Example 4 with BoundStatement

use of com.datastax.oss.driver.api.core.cql.BoundStatement in project thingsboard by thingsboard.

the class CassandraBaseTimeseriesDao method saveNull.

private ListenableFuture<Void> saveNull(TenantId tenantId, EntityId entityId, TsKvEntry tsKvEntry, long ttl, long partition, DataType type) {
    BoundStatementBuilder stmtBuilder = new BoundStatementBuilder((ttl == 0 ? getSaveStmt(type) : getSaveTtlStmt(type)).bind());
    stmtBuilder.setString(0, entityId.getEntityType().name()).setUuid(1, entityId.getId()).setString(2, tsKvEntry.getKey()).setLong(3, partition).setLong(4, tsKvEntry.getTs());
    stmtBuilder.setToNull(getColumnName(type));
    if (ttl > 0) {
        stmtBuilder.setInt(6, (int) ttl);
    }
    BoundStatement stmt = stmtBuilder.build();
    return getFuture(executeAsyncWrite(tenantId, stmt), rs -> null);
}
Also used : BoundStatementBuilder(com.datastax.oss.driver.api.core.cql.BoundStatementBuilder) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement)

Example 5 with BoundStatement

use of com.datastax.oss.driver.api.core.cql.BoundStatement in project thingsboard by thingsboard.

the class CassandraBaseTimeseriesLatestDao method findLatest.

@Override
public ListenableFuture<TsKvEntry> findLatest(TenantId tenantId, EntityId entityId, String key) {
    BoundStatementBuilder stmtBuilder = new BoundStatementBuilder(getFindLatestStmt().bind());
    stmtBuilder.setString(0, entityId.getEntityType().name());
    stmtBuilder.setUuid(1, entityId.getId());
    stmtBuilder.setString(2, key);
    BoundStatement stmt = stmtBuilder.build();
    log.debug(GENERATED_QUERY_FOR_ENTITY_TYPE_AND_ENTITY_ID, stmt, entityId.getEntityType(), entityId.getId());
    return getFuture(executeAsyncRead(tenantId, stmt), rs -> convertResultToTsKvEntry(key, rs.one()));
}
Also used : BoundStatementBuilder(com.datastax.oss.driver.api.core.cql.BoundStatementBuilder) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement)

Aggregations

BoundStatement (com.datastax.oss.driver.api.core.cql.BoundStatement)12 BoundStatementBuilder (com.datastax.oss.driver.api.core.cql.BoundStatementBuilder)8 PreparedStatement (com.datastax.oss.driver.api.core.cql.PreparedStatement)5 AsyncResultSet (com.datastax.oss.driver.api.core.cql.AsyncResultSet)3 Row (com.datastax.oss.driver.api.core.cql.Row)2 FutureCallback (com.google.common.util.concurrent.FutureCallback)2 Instant (java.time.Instant)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Nullable (javax.annotation.Nullable)2 QueryBuilder (com.datastax.oss.driver.api.querybuilder.QueryBuilder)1 QueryBuilder.literal (com.datastax.oss.driver.api.querybuilder.QueryBuilder.literal)1 Select (com.datastax.oss.driver.api.querybuilder.select.Select)1 Function (com.google.common.base.Function)1 AsyncFunction (com.google.common.util.concurrent.AsyncFunction)1 Futures (com.google.common.util.concurrent.Futures)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)1 LocalDateTime (java.time.LocalDateTime)1 ZoneOffset (java.time.ZoneOffset)1