Commit 600da893858415fb0cb80111629126d845e0e0f7

Authored by Andrew Shvayka
1 parent dcba4f6b

Refactoring of Attribute keys

@@ -20,14 +20,29 @@ import lombok.Data; @@ -20,14 +20,29 @@ import lombok.Data;
20 import lombok.NoArgsConstructor; 20 import lombok.NoArgsConstructor;
21 import org.thingsboard.server.common.data.EntityType; 21 import org.thingsboard.server.common.data.EntityType;
22 22
  23 +import javax.persistence.Column;
  24 +import javax.persistence.Embeddable;
  25 +import javax.persistence.EnumType;
  26 +import javax.persistence.Enumerated;
23 import java.io.Serializable; 27 import java.io.Serializable;
24 28
  29 +import static org.thingsboard.server.dao.model.ModelConstants.ATTRIBUTE_KEY_COLUMN;
  30 +import static org.thingsboard.server.dao.model.ModelConstants.ATTRIBUTE_TYPE_COLUMN;
  31 +import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_ID_COLUMN;
  32 +import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_TYPE_COLUMN;
  33 +
25 @Data 34 @Data
26 @AllArgsConstructor 35 @AllArgsConstructor
27 @NoArgsConstructor 36 @NoArgsConstructor
  37 +@Embeddable
28 public class AttributeKvCompositeKey implements Serializable { 38 public class AttributeKvCompositeKey implements Serializable {
  39 + @Enumerated(EnumType.STRING)
  40 + @Column(name = ENTITY_TYPE_COLUMN)
29 private EntityType entityType; 41 private EntityType entityType;
  42 + @Column(name = ENTITY_ID_COLUMN)
30 private String entityId; 43 private String entityId;
  44 + @Column(name = ATTRIBUTE_TYPE_COLUMN)
31 private String attributeType; 45 private String attributeType;
  46 + @Column(name = ATTRIBUTE_KEY_COLUMN)
32 private String attributeKey; 47 private String attributeKey;
33 } 48 }
@@ -27,6 +27,7 @@ import org.thingsboard.server.common.data.kv.StringDataEntry; @@ -27,6 +27,7 @@ import org.thingsboard.server.common.data.kv.StringDataEntry;
27 import org.thingsboard.server.dao.model.ToData; 27 import org.thingsboard.server.dao.model.ToData;
28 28
29 import javax.persistence.Column; 29 import javax.persistence.Column;
  30 +import javax.persistence.EmbeddedId;
30 import javax.persistence.Entity; 31 import javax.persistence.Entity;
31 import javax.persistence.EnumType; 32 import javax.persistence.EnumType;
32 import javax.persistence.Enumerated; 33 import javax.persistence.Enumerated;
@@ -48,25 +49,10 @@ import static org.thingsboard.server.dao.model.ModelConstants.STRING_VALUE_COLUM @@ -48,25 +49,10 @@ import static org.thingsboard.server.dao.model.ModelConstants.STRING_VALUE_COLUM
48 @Data 49 @Data
49 @Entity 50 @Entity
50 @Table(name = "attribute_kv") 51 @Table(name = "attribute_kv")
51 -@IdClass(AttributeKvCompositeKey.class)  
52 public class AttributeKvEntity implements ToData<AttributeKvEntry>, Serializable { 52 public class AttributeKvEntity implements ToData<AttributeKvEntry>, Serializable {
53 53
54 - @Id  
55 - @Enumerated(EnumType.STRING)  
56 - @Column(name = ENTITY_TYPE_COLUMN)  
57 - private EntityType entityType;  
58 -  
59 - @Id  
60 - @Column(name = ENTITY_ID_COLUMN)  
61 - private String entityId;  
62 -  
63 - @Id  
64 - @Column(name = ATTRIBUTE_TYPE_COLUMN)  
65 - private String attributeType;  
66 -  
67 - @Id  
68 - @Column(name = ATTRIBUTE_KEY_COLUMN)  
69 - private String attributeKey; 54 + @EmbeddedId
  55 + private AttributeKvCompositeKey id;
70 56
71 @Column(name = BOOLEAN_VALUE_COLUMN) 57 @Column(name = BOOLEAN_VALUE_COLUMN)
72 private Boolean booleanValue; 58 private Boolean booleanValue;
@@ -87,13 +73,13 @@ public class AttributeKvEntity implements ToData<AttributeKvEntry>, Serializable @@ -87,13 +73,13 @@ public class AttributeKvEntity implements ToData<AttributeKvEntry>, Serializable
87 public AttributeKvEntry toData() { 73 public AttributeKvEntry toData() {
88 KvEntry kvEntry = null; 74 KvEntry kvEntry = null;
89 if (strValue != null) { 75 if (strValue != null) {
90 - kvEntry = new StringDataEntry(attributeKey, strValue); 76 + kvEntry = new StringDataEntry(id.getAttributeKey(), strValue);
91 } else if (booleanValue != null) { 77 } else if (booleanValue != null) {
92 - kvEntry = new BooleanDataEntry(attributeKey, booleanValue); 78 + kvEntry = new BooleanDataEntry(id.getAttributeKey(), booleanValue);
93 } else if (doubleValue != null) { 79 } else if (doubleValue != null) {
94 - kvEntry = new DoubleDataEntry(attributeKey, doubleValue); 80 + kvEntry = new DoubleDataEntry(id.getAttributeKey(), doubleValue);
95 } else if (longValue != null) { 81 } else if (longValue != null) {
96 - kvEntry = new LongDataEntry(attributeKey, longValue); 82 + kvEntry = new LongDataEntry(id.getAttributeKey(), longValue);
97 } 83 }
98 return new BaseAttributeKvEntry(kvEntry, lastUpdateTs); 84 return new BaseAttributeKvEntry(kvEntry, lastUpdateTs);
99 } 85 }
@@ -15,7 +15,9 @@ @@ -15,7 +15,9 @@
15 */ 15 */
16 package org.thingsboard.server.dao.sql.attributes; 16 package org.thingsboard.server.dao.sql.attributes;
17 17
  18 +import org.springframework.data.jpa.repository.Query;
18 import org.springframework.data.repository.CrudRepository; 19 import org.springframework.data.repository.CrudRepository;
  20 +import org.springframework.data.repository.query.Param;
19 import org.thingsboard.server.common.data.EntityType; 21 import org.thingsboard.server.common.data.EntityType;
20 import org.thingsboard.server.dao.model.sql.AttributeKvCompositeKey; 22 import org.thingsboard.server.dao.model.sql.AttributeKvCompositeKey;
21 import org.thingsboard.server.dao.model.sql.AttributeKvEntity; 23 import org.thingsboard.server.dao.model.sql.AttributeKvEntity;
@@ -26,8 +28,11 @@ import java.util.List; @@ -26,8 +28,11 @@ import java.util.List;
26 @SqlDao 28 @SqlDao
27 public interface AttributeKvRepository extends CrudRepository<AttributeKvEntity, AttributeKvCompositeKey> { 29 public interface AttributeKvRepository extends CrudRepository<AttributeKvEntity, AttributeKvCompositeKey> {
28 30
29 - List<AttributeKvEntity> findAllByEntityTypeAndEntityIdAndAttributeType(EntityType entityType,  
30 - String entityId,  
31 - String attributeType); 31 + @Query("SELECT a FROM AttributeKvEntity a WHERE a.id.entityType = :entityType " +
  32 + "AND a.id.entityId = :entityId " +
  33 + "AND a.id.attributeType = :attributeType")
  34 + List<AttributeKvEntity> findAllByEntityTypeAndEntityIdAndAttributeType(@Param("entityType") EntityType entityType,
  35 + @Param("entityId") String entityId,
  36 + @Param("attributeType") String attributeType);
32 } 37 }
33 38
@@ -79,10 +79,7 @@ public class JpaAttributeDao extends JpaAbstractDaoListeningExecutorService impl @@ -79,10 +79,7 @@ public class JpaAttributeDao extends JpaAbstractDaoListeningExecutorService impl
79 @Override 79 @Override
80 public ListenableFuture<Void> save(EntityId entityId, String attributeType, AttributeKvEntry attribute) { 80 public ListenableFuture<Void> save(EntityId entityId, String attributeType, AttributeKvEntry attribute) {
81 AttributeKvEntity entity = new AttributeKvEntity(); 81 AttributeKvEntity entity = new AttributeKvEntity();
82 - entity.setEntityType(entityId.getEntityType());  
83 - entity.setEntityId(fromTimeUUID(entityId.getId()));  
84 - entity.setAttributeType(attributeType);  
85 - entity.setAttributeKey(attribute.getKey()); 82 + entity.setId(new AttributeKvCompositeKey(entityId.getEntityType(), fromTimeUUID(entityId.getId()), attributeType, attribute.getKey()));
86 entity.setLastUpdateTs(attribute.getLastUpdateTs()); 83 entity.setLastUpdateTs(attribute.getLastUpdateTs());
87 entity.setStrValue(attribute.getStrValue().orElse(null)); 84 entity.setStrValue(attribute.getStrValue().orElse(null));
88 entity.setDoubleValue(attribute.getDoubleValue().orElse(null)); 85 entity.setDoubleValue(attribute.getDoubleValue().orElse(null));
@@ -100,10 +97,7 @@ public class JpaAttributeDao extends JpaAbstractDaoListeningExecutorService impl @@ -100,10 +97,7 @@ public class JpaAttributeDao extends JpaAbstractDaoListeningExecutorService impl
100 .stream() 97 .stream()
101 .map(key -> { 98 .map(key -> {
102 AttributeKvEntity entityToDelete = new AttributeKvEntity(); 99 AttributeKvEntity entityToDelete = new AttributeKvEntity();
103 - entityToDelete.setEntityType(entityId.getEntityType());  
104 - entityToDelete.setEntityId(fromTimeUUID(entityId.getId()));  
105 - entityToDelete.setAttributeType(attributeType);  
106 - entityToDelete.setAttributeKey(key); 100 + entityToDelete.setId(new AttributeKvCompositeKey(entityId.getEntityType(), fromTimeUUID(entityId.getId()), attributeType, key));
107 return entityToDelete; 101 return entityToDelete;
108 }).collect(Collectors.toList()); 102 }).collect(Collectors.toList());
109 103