Search in sources :

Example 41 with Futures

use of com.google.common.util.concurrent.Futures in project thingsboard by thingsboard.

the class CassandraBaseTimeseriesDao method findAllAsync.

@Override
public ListenableFuture<List<TsKvEntry>> findAllAsync(TenantId tenantId, EntityId entityId, ReadTsKvQuery query) {
    if (query.getAggregation() == Aggregation.NONE) {
        return findAllAsyncWithLimit(tenantId, entityId, query);
    } else {
        long step = Math.max(query.getInterval(), MIN_AGGREGATION_STEP_MS);
        long stepTs = query.getStartTs();
        List<ListenableFuture<Optional<TsKvEntry>>> futures = new ArrayList<>();
        while (stepTs < query.getEndTs()) {
            long startTs = stepTs;
            long endTs = stepTs + step;
            ReadTsKvQuery subQuery = new BaseReadTsKvQuery(query.getKey(), startTs, endTs, step, 1, query.getAggregation(), query.getOrder());
            futures.add(findAndAggregateAsync(tenantId, entityId, subQuery, toPartitionTs(startTs), toPartitionTs(endTs)));
            stepTs = endTs;
        }
        ListenableFuture<List<Optional<TsKvEntry>>> future = Futures.allAsList(futures);
        return Futures.transform(future, new Function<List<Optional<TsKvEntry>>, List<TsKvEntry>>() {

            @Nullable
            @Override
            public List<TsKvEntry> apply(@Nullable List<Optional<TsKvEntry>> input) {
                return input == null ? Collections.emptyList() : input.stream().filter(v -> v.isPresent()).map(v -> v.get()).collect(Collectors.toList());
            }
        }, readResultsProcessingExecutor);
    }
}
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) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) Optional(java.util.Optional) ArrayList(java.util.ArrayList) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) ReadTsKvQuery(org.thingsboard.server.common.data.kv.ReadTsKvQuery) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) List(java.util.List) ArrayList(java.util.ArrayList) Nullable(javax.annotation.Nullable)

Example 42 with Futures

use of com.google.common.util.concurrent.Futures 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)

Aggregations

Futures (com.google.common.util.concurrent.Futures)42 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)42 List (java.util.List)38 ArrayList (java.util.ArrayList)32 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)26 Map (java.util.Map)21 ExecutionException (java.util.concurrent.ExecutionException)21 Collectors (java.util.stream.Collectors)21 Set (java.util.Set)19 Nullable (javax.annotation.Nullable)19 FutureCallback (com.google.common.util.concurrent.FutureCallback)18 Collections (java.util.Collections)18 Collection (java.util.Collection)17 Optional (java.util.Optional)17 Logger (org.slf4j.Logger)16 LoggerFactory (org.slf4j.LoggerFactory)16 HashSet (java.util.HashSet)15 TimeUnit (java.util.concurrent.TimeUnit)15 Inject (javax.inject.Inject)15 Lists (com.google.common.collect.Lists)14