Fix attributes removal by entity id

This commit is contained in:
ViacheslavKlimov 2024-03-21 12:43:34 +02:00
parent be3e78044c
commit a529abeced
4 changed files with 23 additions and 10 deletions

View File

@ -17,6 +17,10 @@ package org.thingsboard.server.common.data;
import lombok.Getter; import lombok.Getter;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;
public enum AttributeScope { public enum AttributeScope {
CLIENT_SCOPE(1), CLIENT_SCOPE(1),
@ -25,8 +29,15 @@ public enum AttributeScope {
@Getter @Getter
private final int id; private final int id;
private static final Map<Integer, AttributeScope> values = Arrays.stream(values())
.collect(Collectors.toMap(AttributeScope::getId, scope -> scope));
AttributeScope(int id) { AttributeScope(int id) {
this.id = id; this.id = id;
} }
public static AttributeScope valueOf(int id) {
return values.get(id);
}
} }

View File

@ -164,7 +164,7 @@ public class BaseAttributesService implements AttributesService {
@Override @Override
public int removeAllByEntityId(TenantId tenantId, EntityId entityId) { public int removeAllByEntityId(TenantId tenantId, EntityId entityId) {
List<Pair<String, String>> deleted = attributesDao.removeAllByEntityId(tenantId, entityId); List<Pair<AttributeScope, String>> deleted = attributesDao.removeAllByEntityId(tenantId, entityId);
return deleted.size(); return deleted.size();
} }

View File

@ -19,6 +19,7 @@ import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService; import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.MoreExecutors;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
@ -40,7 +41,6 @@ import org.thingsboard.server.dao.cache.CacheExecutorService;
import org.thingsboard.server.dao.service.Validator; import org.thingsboard.server.dao.service.Validator;
import org.thingsboard.server.dao.sql.JpaExecutorService; import org.thingsboard.server.dao.sql.JpaExecutorService;
import jakarta.annotation.PostConstruct;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
@ -308,11 +308,13 @@ public class CachedAttributesService implements AttributesService {
@Override @Override
public int removeAllByEntityId(TenantId tenantId, EntityId entityId) { public int removeAllByEntityId(TenantId tenantId, EntityId entityId) {
List<Pair<String, String>> result = attributesDao.removeAllByEntityId(tenantId, entityId); List<Pair<AttributeScope, String>> result = attributesDao.removeAllByEntityId(tenantId, entityId);
result.forEach(deleted -> { result.forEach(deleted -> {
String scope = deleted.getKey(); AttributeScope scope = deleted.getKey();
String key = deleted.getValue(); String key = deleted.getValue();
if (scope != null && key != null) {
cache.evict(new AttributeCacheKey(scope, entityId, key)); cache.evict(new AttributeCacheKey(scope, entityId, key));
}
}); });
return result.size(); return result.size();
} }

View File

@ -35,8 +35,8 @@ import org.thingsboard.server.common.data.kv.AttributeKvEntry;
import org.thingsboard.server.common.stats.StatsFactory; import org.thingsboard.server.common.stats.StatsFactory;
import org.thingsboard.server.dao.DaoUtil; import org.thingsboard.server.dao.DaoUtil;
import org.thingsboard.server.dao.attributes.AttributesDao; import org.thingsboard.server.dao.attributes.AttributesDao;
import org.thingsboard.server.dao.model.ModelConstants;
import org.thingsboard.server.dao.dictionary.KeyDictionaryDao; import org.thingsboard.server.dao.dictionary.KeyDictionaryDao;
import org.thingsboard.server.dao.model.ModelConstants;
import org.thingsboard.server.dao.model.sql.AttributeKvCompositeKey; import org.thingsboard.server.dao.model.sql.AttributeKvCompositeKey;
import org.thingsboard.server.dao.model.sql.AttributeKvEntity; import org.thingsboard.server.dao.model.sql.AttributeKvEntity;
import org.thingsboard.server.dao.sql.JpaAbstractDaoListeningExecutorService; import org.thingsboard.server.dao.sql.JpaAbstractDaoListeningExecutorService;
@ -209,10 +209,10 @@ public class JpaAttributeDao extends JpaAbstractDaoListeningExecutorService impl
@Transactional @Transactional
@Override @Override
public List<Pair<AttributeScope, String>> removeAllByEntityId(TenantId tenantId, EntityId entityId) { public List<Pair<AttributeScope, String>> removeAllByEntityId(TenantId tenantId, EntityId entityId) {
return jdbcTemplate.queryForList("DELETE FROM attribute_kv WHERE entity_type = ? and entity_id = ? " + return jdbcTemplate.queryForList("DELETE FROM attribute_kv WHERE entity_id = ? " +
"RETURNING attribute_type, attribute_key", entityId.getEntityType().name(), entityId.getId()).stream() "RETURNING attribute_type, attribute_key", entityId.getId()).stream()
.map(row -> Pair.of(row.get(ModelConstants.ATTRIBUTE_TYPE_COLUMN), .map(row -> Pair.of(AttributeScope.valueOf((Integer) row.get(ModelConstants.ATTRIBUTE_TYPE_COLUMN)),
(String) row.get(ModelConstants.ATTRIBUTE_KEY_COLUMN))) keyDictionaryDao.getKey((Integer) row.get(ModelConstants.ATTRIBUTE_KEY_COLUMN))))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }