Minor fixes to Attributes and Timeseries DAO

This commit is contained in:
Andrew Shvayka 2017-07-19 14:44:53 +03:00
parent e0f753025d
commit ded769a3a7
2 changed files with 16 additions and 11 deletions

View File

@ -16,6 +16,7 @@
package org.thingsboard.server.dao.sql.attributes; package org.thingsboard.server.dao.sql.attributes;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListenableFuture;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -49,8 +50,8 @@ public class JpaAttributeDao extends JpaAbstractDaoListeningExecutorService impl
public ListenableFuture<Optional<AttributeKvEntry>> find(EntityId entityId, String attributeType, String attributeKey) { public ListenableFuture<Optional<AttributeKvEntry>> find(EntityId entityId, String attributeType, String attributeKey) {
AttributeKvCompositeKey compositeKey = AttributeKvCompositeKey compositeKey =
getAttributeKvCompositeKey(entityId, attributeType, attributeKey); getAttributeKvCompositeKey(entityId, attributeType, attributeKey);
return service.submit(() -> return Futures.immediateFuture(
Optional.of(DaoUtil.getData(attributeKvRepository.findOne(compositeKey)))); Optional.ofNullable(DaoUtil.getData(attributeKvRepository.findOne(compositeKey))));
} }
@Override @Override
@ -61,13 +62,13 @@ public class JpaAttributeDao extends JpaAbstractDaoListeningExecutorService impl
.map(attributeKey -> .map(attributeKey ->
getAttributeKvCompositeKey(entityId, attributeType, attributeKey)) getAttributeKvCompositeKey(entityId, attributeType, attributeKey))
.collect(Collectors.toList()); .collect(Collectors.toList());
return service.submit(() -> return Futures.immediateFuture(
DaoUtil.convertDataList(Lists.newArrayList(attributeKvRepository.findAll(compositeKeys)))); DaoUtil.convertDataList(Lists.newArrayList(attributeKvRepository.findAll(compositeKeys))));
} }
@Override @Override
public ListenableFuture<List<AttributeKvEntry>> findAll(EntityId entityId, String attributeType) { public ListenableFuture<List<AttributeKvEntry>> findAll(EntityId entityId, String attributeType) {
return service.submit(() -> return Futures.immediateFuture(
DaoUtil.convertDataList(Lists.newArrayList( DaoUtil.convertDataList(Lists.newArrayList(
attributeKvRepository.findAllByEntityTypeAndEntityIdAndAttributeType( attributeKvRepository.findAllByEntityTypeAndEntityIdAndAttributeType(
entityId.getEntityType(), entityId.getEntityType(),

View File

@ -26,9 +26,7 @@ import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.thingsboard.server.common.data.UUIDConverter; import org.thingsboard.server.common.data.UUIDConverter;
import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.kv.Aggregation; import org.thingsboard.server.common.data.kv.*;
import org.thingsboard.server.common.data.kv.TsKvEntry;
import org.thingsboard.server.common.data.kv.TsKvQuery;
import org.thingsboard.server.dao.DaoUtil; import org.thingsboard.server.dao.DaoUtil;
import org.thingsboard.server.dao.model.sql.TsKvEntity; import org.thingsboard.server.dao.model.sql.TsKvEntity;
import org.thingsboard.server.dao.model.sql.TsKvLatestCompositeKey; import org.thingsboard.server.dao.model.sql.TsKvLatestCompositeKey;
@ -187,7 +185,7 @@ public class JpaTimeseriesDao extends JpaAbstractDaoListeningExecutorService imp
} }
private ListenableFuture<List<TsKvEntry>> findAllAsyncWithLimit(EntityId entityId, TsKvQuery query) { private ListenableFuture<List<TsKvEntry>> findAllAsyncWithLimit(EntityId entityId, TsKvQuery query) {
return service.submit(() -> return Futures.immediateFuture(
DaoUtil.convertDataList( DaoUtil.convertDataList(
tsKvRepository.findAllWithLimit( tsKvRepository.findAllWithLimit(
fromTimeUUID(entityId.getId()), fromTimeUUID(entityId.getId()),
@ -205,13 +203,19 @@ public class JpaTimeseriesDao extends JpaAbstractDaoListeningExecutorService imp
entityId.getEntityType(), entityId.getEntityType(),
fromTimeUUID(entityId.getId()), fromTimeUUID(entityId.getId()),
key); key);
return service.submit(() -> TsKvLatestEntity entry = tsKvLatestRepository.findOne(compositeKey);
DaoUtil.getData(tsKvLatestRepository.findOne(compositeKey))); TsKvEntry result;
if (entry != null) {
result = DaoUtil.getData(entry);
} else {
result = new BasicTsKvEntry(System.currentTimeMillis(), new StringDataEntry(key, null));
}
return Futures.immediateFuture(result);
} }
@Override @Override
public ListenableFuture<List<TsKvEntry>> findAllLatest(EntityId entityId) { public ListenableFuture<List<TsKvEntry>> findAllLatest(EntityId entityId) {
return service.submit(() -> return Futures.immediateFuture(
DaoUtil.convertDataList(Lists.newArrayList( DaoUtil.convertDataList(Lists.newArrayList(
tsKvLatestRepository.findAllByEntityTypeAndEntityId( tsKvLatestRepository.findAllByEntityTypeAndEntityId(
entityId.getEntityType(), entityId.getEntityType(),