Search in sources :

Example 1 with TsKvLatestRemovingResult

use of org.thingsboard.server.common.data.kv.TsKvLatestRemovingResult in project thingsboard by thingsboard.

the class CassandraBaseTimeseriesLatestDao method getNewLatestEntryFuture.

private ListenableFuture<TsKvLatestRemovingResult> getNewLatestEntryFuture(TenantId tenantId, EntityId entityId, DeleteTsKvQuery query) {
    long startTs = 0;
    long endTs = query.getStartTs() - 1;
    ReadTsKvQuery findNewLatestQuery = new BaseReadTsKvQuery(query.getKey(), startTs, endTs, endTs - startTs, 1, Aggregation.NONE, DESC_ORDER);
    ListenableFuture<List<TsKvEntry>> future = aggregationTimeseriesDao.findAllAsync(tenantId, entityId, findNewLatestQuery);
    return Futures.transformAsync(future, entryList -> {
        if (entryList.size() == 1) {
            TsKvEntry entry = entryList.get(0);
            return Futures.transform(saveLatest(tenantId, entityId, entryList.get(0)), v -> new TsKvLatestRemovingResult(entry), MoreExecutors.directExecutor());
        } else {
            log.trace("Could not find new latest value for [{}], key - {}", entityId, query.getKey());
        }
        return Futures.immediateFuture(new TsKvLatestRemovingResult(query.getKey(), true));
    }, readResultsProcessingExecutor);
}
Also used : TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) ReadTsKvQuery(org.thingsboard.server.common.data.kv.ReadTsKvQuery) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) List(java.util.List) TsKvLatestRemovingResult(org.thingsboard.server.common.data.kv.TsKvLatestRemovingResult)

Example 2 with TsKvLatestRemovingResult

use of org.thingsboard.server.common.data.kv.TsKvLatestRemovingResult in project thingsboard by thingsboard.

the class SqlTimeseriesLatestDao method getRemoveLatestFuture.

protected ListenableFuture<TsKvLatestRemovingResult> getRemoveLatestFuture(TenantId tenantId, EntityId entityId, DeleteTsKvQuery query) {
    ListenableFuture<TsKvEntry> latestFuture = getFindLatestFuture(entityId, query.getKey());
    ListenableFuture<Boolean> booleanFuture = Futures.transform(latestFuture, tsKvEntry -> {
        long ts = tsKvEntry.getTs();
        return ts > query.getStartTs() && ts <= query.getEndTs();
    }, service);
    ListenableFuture<Boolean> removedLatestFuture = Futures.transformAsync(booleanFuture, isRemove -> {
        if (isRemove) {
            TsKvLatestEntity latestEntity = new TsKvLatestEntity();
            latestEntity.setEntityId(entityId.getId());
            latestEntity.setKey(getOrSaveKeyId(query.getKey()));
            return service.submit(() -> {
                tsKvLatestRepository.delete(latestEntity);
                return true;
            });
        }
        return Futures.immediateFuture(false);
    }, service);
    return Futures.transformAsync(removedLatestFuture, isRemoved -> {
        if (isRemoved && query.getRewriteLatestIfDeleted()) {
            return getNewLatestEntryFuture(tenantId, entityId, query);
        }
        return Futures.immediateFuture(new TsKvLatestRemovingResult(query.getKey(), isRemoved));
    }, MoreExecutors.directExecutor());
}
Also used : TsKvLatestEntity(org.thingsboard.server.dao.model.sqlts.latest.TsKvLatestEntity) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) TsKvLatestRemovingResult(org.thingsboard.server.common.data.kv.TsKvLatestRemovingResult)

Example 3 with TsKvLatestRemovingResult

use of org.thingsboard.server.common.data.kv.TsKvLatestRemovingResult in project thingsboard by thingsboard.

the class CassandraBaseTimeseriesLatestDao method removeLatest.

@Override
public ListenableFuture<TsKvLatestRemovingResult> removeLatest(TenantId tenantId, EntityId entityId, DeleteTsKvQuery query) {
    ListenableFuture<TsKvEntry> latestEntryFuture = findLatest(tenantId, entityId, query.getKey());
    ListenableFuture<Boolean> booleanFuture = Futures.transform(latestEntryFuture, latestEntry -> {
        long ts = latestEntry.getTs();
        if (ts > query.getStartTs() && ts <= query.getEndTs()) {
            return true;
        } else {
            log.trace("Won't be deleted latest value for [{}], key - {}", entityId, query.getKey());
        }
        return false;
    }, readResultsProcessingExecutor);
    ListenableFuture<Boolean> removedLatestFuture = Futures.transformAsync(booleanFuture, isRemove -> {
        if (isRemove) {
            return Futures.transform(deleteLatest(tenantId, entityId, query.getKey()), res -> true, MoreExecutors.directExecutor());
        }
        return Futures.immediateFuture(false);
    }, readResultsProcessingExecutor);
    return Futures.transformAsync(removedLatestFuture, isRemoved -> {
        if (isRemoved && query.getRewriteLatestIfDeleted()) {
            return getNewLatestEntryFuture(tenantId, entityId, query);
        }
        return Futures.immediateFuture(new TsKvLatestRemovingResult(query.getKey(), isRemoved));
    }, MoreExecutors.directExecutor());
}
Also used : TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) TsKvLatestRemovingResult(org.thingsboard.server.common.data.kv.TsKvLatestRemovingResult)

Aggregations

TsKvEntry (org.thingsboard.server.common.data.kv.TsKvEntry)3 TsKvLatestRemovingResult (org.thingsboard.server.common.data.kv.TsKvLatestRemovingResult)3 List (java.util.List)1 BaseReadTsKvQuery (org.thingsboard.server.common.data.kv.BaseReadTsKvQuery)1 BasicTsKvEntry (org.thingsboard.server.common.data.kv.BasicTsKvEntry)1 ReadTsKvQuery (org.thingsboard.server.common.data.kv.ReadTsKvQuery)1 TsKvLatestEntity (org.thingsboard.server.dao.model.sqlts.latest.TsKvLatestEntity)1