Search in sources :

Example 6 with BoundStatement

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

the class AbstractBufferedRateExecutor method queryToString.

private String queryToString(AsyncTaskContext<T, V> taskCtx) {
    CassandraStatementTask cassStmtTask = (CassandraStatementTask) taskCtx.getTask();
    if (cassStmtTask.getStatement() instanceof BoundStatement) {
        BoundStatement stmt = (BoundStatement) cassStmtTask.getStatement();
        String query = stmt.getPreparedStatement().getQuery();
        try {
            query = toStringWithValues(stmt, ProtocolVersion.V5);
        } catch (Exception e) {
            log.warn("Can't convert to query with values", e);
        }
        return query;
    } else {
        return "Not Cassandra Statement Task";
    }
}
Also used : CassandraStatementTask(org.thingsboard.server.dao.nosql.CassandraStatementTask) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement) TimeoutException(java.util.concurrent.TimeoutException)

Example 7 with BoundStatement

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

the class Mementoize method execute.

/**
 * Store a Memento. Note that the value for {@code modified} is truncated to seconds because Memento requires HTTP
 * time management.
 *
 * @param ixnModel an {@link IRI} for the interaction model for this resource
 * @param mimeType if this resource has a binary, the mimeType therefor
 * @param container if this resource has a container, the {@link IRI} therefor
 * @param data RDF for this resource
 * @param modified the time at which this resource was last modified
 * @param binaryIdentifier if this resource has a binary, the identifier therefor
 * @param creation a time-based (version 1) UUID for the moment this resource is created
 * @param id an {@link IRI} that identifies this resource
 * @return whether and when it has been inserted
 */
public CompletionStage<Void> execute(IRI ixnModel, String mimeType, IRI container, Dataset data, Instant modified, IRI binaryIdentifier, UUID creation, IRI id) {
    Instant mementoModified = modified.truncatedTo(SECONDS);
    BoundStatement statement = preparedStatement().bind(ixnModel, mimeType, container, data, modified, binaryIdentifier, creation, id, mementoModified);
    return executeWrite(statement);
}
Also used : Instant(java.time.Instant) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement)

Example 8 with BoundStatement

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

the class CassandraBaseTimeseriesDao method doSavePartition.

private ListenableFuture<Integer> doSavePartition(TenantId tenantId, EntityId entityId, String key, long ttl, long partition) {
    log.debug("Saving partition {} for the entity [{}-{}] and key {}", partition, entityId.getEntityType(), entityId.getId(), key);
    PreparedStatement preparedStatement = ttl == 0 ? getPartitionInsertStmt() : getPartitionInsertTtlStmt();
    BoundStatement stmt = preparedStatement.bind();
    stmt = stmt.setString(0, entityId.getEntityType().name()).setUuid(1, entityId.getId()).setLong(2, partition).setString(3, key);
    if (ttl > 0) {
        stmt = stmt.setInt(4, (int) ttl);
    }
    return getFuture(executeAsyncWrite(tenantId, stmt), rs -> 0);
}
Also used : PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement)

Example 9 with BoundStatement

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

the class CassandraBaseTimeseriesDao method save.

@Override
public ListenableFuture<Integer> save(TenantId tenantId, EntityId entityId, TsKvEntry tsKvEntry, long ttl) {
    List<ListenableFuture<Void>> futures = new ArrayList<>();
    ttl = computeTtl(ttl);
    int dataPointDays = tsKvEntry.getDataPoints() * Math.max(1, (int) (ttl / SECONDS_IN_DAY));
    long partition = toPartitionTs(tsKvEntry.getTs());
    DataType type = tsKvEntry.getDataType();
    if (setNullValuesEnabled) {
        processSetNullValues(tenantId, entityId, tsKvEntry, ttl, futures, partition, 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());
    addValue(tsKvEntry, stmtBuilder, 5);
    if (ttl > 0) {
        stmtBuilder.setInt(6, (int) ttl);
    }
    BoundStatement stmt = stmtBuilder.build();
    futures.add(getFuture(executeAsyncWrite(tenantId, stmt), rs -> null));
    return Futures.transform(Futures.allAsList(futures), result -> dataPointDays, MoreExecutors.directExecutor());
}
Also used : QueryBuilder(com.datastax.oss.driver.api.querybuilder.QueryBuilder) Arrays(java.util.Arrays) Autowired(org.springframework.beans.factory.annotation.Autowired) TenantId(org.thingsboard.server.common.data.id.TenantId) AggregationTimeseriesDao(org.thingsboard.server.dao.sqlts.AggregationTimeseriesDao) TbResultSet(org.thingsboard.server.dao.nosql.TbResultSet) PreDestroy(javax.annotation.PreDestroy) AsyncResultSet(com.datastax.oss.driver.api.core.cql.AsyncResultSet) Profiles(org.springframework.core.env.Profiles) ZoneOffset(java.time.ZoneOffset) Select(com.datastax.oss.driver.api.querybuilder.select.Select) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) Function(com.google.common.base.Function) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) DeleteTsKvQuery(org.thingsboard.server.common.data.kv.DeleteTsKvQuery) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) QueryBuilder.literal(com.datastax.oss.driver.api.querybuilder.QueryBuilder.literal) Environment(org.springframework.core.env.Environment) KvEntry(org.thingsboard.server.common.data.kv.KvEntry) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) ModelConstants(org.thingsboard.server.dao.model.ModelConstants) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) LocalDateTime(java.time.LocalDateTime) NoSqlTsDao(org.thingsboard.server.dao.util.NoSqlTsDao) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) DataType(org.thingsboard.server.common.data.kv.DataType) EntityId(org.thingsboard.server.common.data.id.EntityId) Row(com.datastax.oss.driver.api.core.cql.Row) Nullable(javax.annotation.Nullable) TbResultSetFuture(org.thingsboard.server.dao.nosql.TbResultSetFuture) ReentrantLock(java.util.concurrent.locks.ReentrantLock) BoundStatementBuilder(com.datastax.oss.driver.api.core.cql.BoundStatementBuilder) Aggregation(org.thingsboard.server.common.data.kv.Aggregation) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) FutureCallback(com.google.common.util.concurrent.FutureCallback) TimeUnit(java.util.concurrent.TimeUnit) Futures(com.google.common.util.concurrent.Futures) Component(org.springframework.stereotype.Component) Lock(java.util.concurrent.locks.Lock) ChronoUnit(java.time.temporal.ChronoUnit) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) ReadTsKvQuery(org.thingsboard.server.common.data.kv.ReadTsKvQuery) Collections(java.util.Collections) BoundStatementBuilder(com.datastax.oss.driver.api.core.cql.BoundStatementBuilder) ArrayList(java.util.ArrayList) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) DataType(org.thingsboard.server.common.data.kv.DataType) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement)

Example 10 with BoundStatement

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

the class CassandraBaseTimeseriesDao method deleteAsync.

private void deleteAsync(TenantId tenantId, final QueryCursor cursor, final SimpleListenableFuture<Void> resultFuture) {
    if (!cursor.hasNextPartition()) {
        resultFuture.set(null);
    } else {
        PreparedStatement proto = getDeleteStmt();
        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());
        BoundStatement stmt = stmtBuilder.build();
        Futures.addCallback(executeAsyncWrite(tenantId, stmt), new FutureCallback<AsyncResultSet>() {

            @Override
            public void onSuccess(@Nullable AsyncResultSet result) {
                deleteAsync(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)

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