Search in sources :

Example 1 with TsKvLatestEntity

use of org.thingsboard.server.dao.model.sqlts.latest.TsKvLatestEntity in project thingsboard by thingsboard.

the class CassandraTsLatestToSqlMigrateService method getTsKvLatestEntity.

private TsKvLatestEntity getTsKvLatestEntity(CassandraToSqlColumnData[] data) {
    TsKvLatestEntity latestEntity = new TsKvLatestEntity();
    latestEntity.setEntityId(UUIDConverter.fromString(data[0].getValue()));
    latestEntity.setKey(getOrSaveKeyId(data[1].getValue()));
    latestEntity.setTs(Long.parseLong(data[2].getValue()));
    String strV = data[4].getValue();
    if (strV != null) {
        if (strV.length() > MAX_STR_V_LENGTH) {
            log.warn("[ts_kv_latest] Value size [{}] exceeds maximum size [{}] of column [str_v] and will be truncated!", strV.length(), MAX_STR_V_LENGTH);
            log.warn("Affected data:\n{}", strV);
            strV = strV.substring(0, MAX_STR_V_LENGTH);
        }
        latestEntity.setStrValue(strV);
    } else {
        Long longV = null;
        try {
            longV = Long.parseLong(data[5].getValue());
        } catch (Exception e) {
        }
        if (longV != null) {
            latestEntity.setLongValue(longV);
        } else {
            Double doubleV = null;
            try {
                doubleV = Double.parseDouble(data[6].getValue());
            } catch (Exception e) {
            }
            if (doubleV != null) {
                latestEntity.setDoubleValue(doubleV);
            } else {
                String jsonV = data[7].getValue();
                if (StringUtils.isNoneEmpty(jsonV)) {
                    latestEntity.setJsonValue(jsonV);
                } else {
                    Boolean boolV = null;
                    try {
                        boolV = Boolean.parseBoolean(data[3].getValue());
                    } catch (Exception e) {
                    }
                    if (boolV != null) {
                        latestEntity.setBooleanValue(boolV);
                    } else {
                        log.warn("All values in key-value row are nullable ");
                    }
                }
            }
        }
    }
    return latestEntity;
}
Also used : TsKvLatestEntity(org.thingsboard.server.dao.model.sqlts.latest.TsKvLatestEntity) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException)

Example 2 with TsKvLatestEntity

use of org.thingsboard.server.dao.model.sqlts.latest.TsKvLatestEntity in project thingsboard by thingsboard.

the class SqlTimeseriesLatestDao method getSaveLatestFuture.

protected ListenableFuture<Void> getSaveLatestFuture(EntityId entityId, TsKvEntry tsKvEntry) {
    TsKvLatestEntity latestEntity = new TsKvLatestEntity();
    latestEntity.setEntityId(entityId.getId());
    latestEntity.setTs(tsKvEntry.getTs());
    latestEntity.setKey(getOrSaveKeyId(tsKvEntry.getKey()));
    latestEntity.setStrValue(tsKvEntry.getStrValue().orElse(null));
    latestEntity.setDoubleValue(tsKvEntry.getDoubleValue().orElse(null));
    latestEntity.setLongValue(tsKvEntry.getLongValue().orElse(null));
    latestEntity.setBooleanValue(tsKvEntry.getBooleanValue().orElse(null));
    latestEntity.setJsonValue(tsKvEntry.getJsonValue().orElse(null));
    return tsLatestQueue.add(latestEntity);
}
Also used : TsKvLatestEntity(org.thingsboard.server.dao.model.sqlts.latest.TsKvLatestEntity)

Example 3 with TsKvLatestEntity

use of org.thingsboard.server.dao.model.sqlts.latest.TsKvLatestEntity 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 4 with TsKvLatestEntity

use of org.thingsboard.server.dao.model.sqlts.latest.TsKvLatestEntity in project thingsboard by thingsboard.

the class SqlTimeseriesLatestDao method getFindLatestFuture.

protected ListenableFuture<TsKvEntry> getFindLatestFuture(EntityId entityId, String key) {
    TsKvLatestCompositeKey compositeKey = new TsKvLatestCompositeKey(entityId.getId(), getOrSaveKeyId(key));
    Optional<TsKvLatestEntity> entry = tsKvLatestRepository.findById(compositeKey);
    TsKvEntry result;
    if (entry.isPresent()) {
        TsKvLatestEntity tsKvLatestEntity = entry.get();
        tsKvLatestEntity.setStrKey(key);
        result = DaoUtil.getData(tsKvLatestEntity);
    } else {
        result = new BasicTsKvEntry(System.currentTimeMillis(), new StringDataEntry(key, null));
    }
    return Futures.immediateFuture(result);
}
Also used : TsKvLatestEntity(org.thingsboard.server.dao.model.sqlts.latest.TsKvLatestEntity) TsKvLatestCompositeKey(org.thingsboard.server.dao.model.sqlts.latest.TsKvLatestCompositeKey) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry)

Example 5 with TsKvLatestEntity

use of org.thingsboard.server.dao.model.sqlts.latest.TsKvLatestEntity in project thingsboard by thingsboard.

the class SqlTimeseriesLatestDao method init.

@PostConstruct
protected void init() {
    TbSqlBlockingQueueParams tsLatestParams = TbSqlBlockingQueueParams.builder().logName("TS Latest").batchSize(tsLatestBatchSize).maxDelay(tsLatestMaxDelay).statsPrintIntervalMs(tsLatestStatsPrintIntervalMs).statsNamePrefix("ts.latest").batchSortEnabled(false).build();
    java.util.function.Function<TsKvLatestEntity, Integer> hashcodeFunction = entity -> entity.getEntityId().hashCode();
    tsLatestQueue = new TbSqlBlockingQueueWrapper<>(tsLatestParams, hashcodeFunction, tsLatestBatchThreads, statsFactory);
    tsLatestQueue.init(logExecutor, v -> {
        Map<TsKey, TsKvLatestEntity> trueLatest = new HashMap<>();
        v.forEach(ts -> {
            TsKey key = new TsKey(ts.getEntityId(), ts.getKey());
            trueLatest.merge(key, ts, (oldTs, newTs) -> oldTs.getTs() <= newTs.getTs() ? newTs : oldTs);
        });
        List<TsKvLatestEntity> latestEntities = new ArrayList<>(trueLatest.values());
        if (batchSortEnabled) {
            latestEntities.sort(Comparator.comparing((Function<TsKvLatestEntity, UUID>) AbstractTsKvEntity::getEntityId).thenComparingInt(AbstractTsKvEntity::getKey));
        }
        insertLatestTsRepository.saveOrUpdate(latestEntities);
    }, (l, r) -> 0);
}
Also used : MoreExecutors(com.google.common.util.concurrent.MoreExecutors) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) TbSqlBlockingQueueWrapper(org.thingsboard.server.dao.sql.TbSqlBlockingQueueWrapper) Autowired(org.springframework.beans.factory.annotation.Autowired) DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) TsKvLatestCompositeKey(org.thingsboard.server.dao.model.sqlts.latest.TsKvLatestCompositeKey) HashMap(java.util.HashMap) ScheduledLogExecutorComponent(org.thingsboard.server.dao.sql.ScheduledLogExecutorComponent) StatsFactory(org.thingsboard.server.common.stats.StatsFactory) Function(java.util.function.Function) TenantId(org.thingsboard.server.common.data.id.TenantId) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) PreDestroy(javax.annotation.PreDestroy) Lists(com.google.common.collect.Lists) AbstractTsKvEntity(org.thingsboard.server.dao.model.sql.AbstractTsKvEntity) TbSqlBlockingQueueParams(org.thingsboard.server.dao.sql.TbSqlBlockingQueueParams) Map(java.util.Map) EntityId(org.thingsboard.server.common.data.id.EntityId) TsKvLatestRemovingResult(org.thingsboard.server.common.data.kv.TsKvLatestRemovingResult) TsKvLatestEntity(org.thingsboard.server.dao.model.sqlts.latest.TsKvLatestEntity) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) InsertLatestTsRepository(org.thingsboard.server.dao.sqlts.insert.latest.InsertLatestTsRepository) TimeseriesLatestDao(org.thingsboard.server.dao.timeseries.TimeseriesLatestDao) SqlTsLatestAnyDao(org.thingsboard.server.dao.util.SqlTsLatestAnyDao) Aggregation(org.thingsboard.server.common.data.kv.Aggregation) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) DeleteTsKvQuery(org.thingsboard.server.common.data.kv.DeleteTsKvQuery) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) Futures(com.google.common.util.concurrent.Futures) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) List(java.util.List) TsKvLatestRepository(org.thingsboard.server.dao.sqlts.latest.TsKvLatestRepository) PostConstruct(javax.annotation.PostConstruct) DaoUtil(org.thingsboard.server.dao.DaoUtil) SearchTsKvLatestRepository(org.thingsboard.server.dao.sqlts.latest.SearchTsKvLatestRepository) Optional(java.util.Optional) Comparator(java.util.Comparator) ReadTsKvQuery(org.thingsboard.server.common.data.kv.ReadTsKvQuery) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) TsKvLatestEntity(org.thingsboard.server.dao.model.sqlts.latest.TsKvLatestEntity) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) UUID(java.util.UUID) TbSqlBlockingQueueParams(org.thingsboard.server.dao.sql.TbSqlBlockingQueueParams) PostConstruct(javax.annotation.PostConstruct)

Aggregations

TsKvLatestEntity (org.thingsboard.server.dao.model.sqlts.latest.TsKvLatestEntity)6 BasicTsKvEntry (org.thingsboard.server.common.data.kv.BasicTsKvEntry)3 TsKvEntry (org.thingsboard.server.common.data.kv.TsKvEntry)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 StringDataEntry (org.thingsboard.server.common.data.kv.StringDataEntry)2 TsKvLatestRemovingResult (org.thingsboard.server.common.data.kv.TsKvLatestRemovingResult)2 TsKvLatestCompositeKey (org.thingsboard.server.dao.model.sqlts.latest.TsKvLatestCompositeKey)2 Lists (com.google.common.collect.Lists)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 PreparedStatement (java.sql.PreparedStatement)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Optional (java.util.Optional)1 UUID (java.util.UUID)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1