improvements

This commit is contained in:
YevhenBondarenko 2020-07-10 17:07:15 +03:00
parent 98c24632e3
commit 2f4ca3b5be
2 changed files with 12 additions and 12 deletions

View File

@ -52,7 +52,8 @@ import org.thingsboard.server.dao.timeseries.SimpleListenableFuture;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy; import javax.annotation.PreDestroy;
import java.util.Comparator; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
@ -129,16 +130,15 @@ public abstract class AbstractSqlTimeseriesDao extends JpaAbstractDaoListeningEx
tsLatestQueue = new TbSqlBlockingQueueWrapper<>(tsLatestParams, hashcodeFunction, tsLatestBatchThreads); tsLatestQueue = new TbSqlBlockingQueueWrapper<>(tsLatestParams, hashcodeFunction, tsLatestBatchThreads);
tsLatestQueue.init(logExecutor, v -> { tsLatestQueue.init(logExecutor, v -> {
Map<TsKey, List<TsKvLatestEntity>> tsMap = Map<TsKey, TsKvLatestEntity> trueLatest = new HashMap<>();
v.stream().collect(Collectors.groupingBy(ts -> new TsKey(ts.getEntityId(), ts.getStrKey()))); v.forEach(ts -> {
TsKey key = new TsKey(ts.getEntityId(), ts.getKey());
List<TsKvLatestEntity> latestEntities = TsKvLatestEntity old = trueLatest.get(key);
tsMap.keySet() if (old == null || old.getTs() < ts.getTs()) {
.stream() trueLatest.put(key, ts);
.map(tsMap::get) }
.map(list -> list.stream().max(Comparator.comparing(TsKvLatestEntity::getTs)).get()) });
.collect(Collectors.toList()); List<TsKvLatestEntity> latestEntities = new ArrayList<>(trueLatest.values());
insertLatestTsRepository.saveOrUpdate(latestEntities); insertLatestTsRepository.saveOrUpdate(latestEntities);
}); });
} }

View File

@ -22,5 +22,5 @@ import java.util.UUID;
@Data @Data
public class TsKey { public class TsKey {
private final UUID entityId; private final UUID entityId;
private final String key; private final int key;
} }