Merge branch 'findRelationsRecursively_refactoring' of github.com:smatvienko-tb/thingsboard

This commit is contained in:
Andrii Shvaika 2022-11-03 17:28:21 +02:00
commit 5cbb8b8ca5

View File

@ -52,6 +52,7 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import static org.thingsboard.server.dao.service.Validator.validateId; import static org.thingsboard.server.dao.service.Validator.validateId;
@ -512,22 +513,19 @@ public class BaseRelationService implements RelationService {
private ListenableFuture<Set<EntityRelation>> findRelationsRecursively(final TenantId tenantId, final EntityId rootId, final EntitySearchDirection direction, private ListenableFuture<Set<EntityRelation>> findRelationsRecursively(final TenantId tenantId, final EntityId rootId, final EntitySearchDirection direction,
RelationTypeGroup relationTypeGroup, int lvl, boolean fetchLastLevelOnly, RelationTypeGroup relationTypeGroup, int lvl, boolean fetchLastLevelOnly,
final ConcurrentHashMap<EntityId, Boolean> uniqueMap) throws Exception { final ConcurrentHashMap<EntityId, Boolean> uniqueMap) {
if (lvl == 0) { if (lvl == 0) {
return Futures.immediateFuture(Collections.emptySet()); return Futures.immediateFuture(Collections.emptySet());
} }
lvl--; final int currentLvl = --lvl;
//TODO: try to remove this blocking operation final Set<EntityRelation> children = new HashSet<>();
Set<EntityRelation> children = new HashSet<>(findRelations(tenantId, rootId, direction, relationTypeGroup).get()); ListenableFuture<List<EntityRelation>> rootRelationsFuture = findRelations(tenantId, rootId, direction, relationTypeGroup);
ListenableFuture<Set<EntityId>> childrenIdsFuture = Futures.transform(rootRelationsFuture, relations -> {
children.addAll(relations);
Set<EntityId> childrenIds = new HashSet<>(); Set<EntityId> childrenIds = new HashSet<>();
for (EntityRelation childRelation : children) { for (EntityRelation childRelation : children) {
log.trace("Found Relation: {}", childRelation); log.trace("Found Relation: {}", childRelation);
EntityId childId; EntityId childId = direction == EntitySearchDirection.FROM ? childRelation.getTo() : childRelation.getFrom();
if (direction == EntitySearchDirection.FROM) {
childId = childRelation.getTo();
} else {
childId = childRelation.getFrom();
}
if (uniqueMap.putIfAbsent(childId, Boolean.TRUE) == null) { if (uniqueMap.putIfAbsent(childId, Boolean.TRUE) == null) {
log.trace("Adding Relation: {}", childId); log.trace("Adding Relation: {}", childId);
if (childrenIds.add(childId)) { if (childrenIds.add(childId)) {
@ -535,17 +533,22 @@ public class BaseRelationService implements RelationService {
} }
} }
} }
List<ListenableFuture<Set<EntityRelation>>> futures = new ArrayList<>(); return childrenIds;
for (EntityId entityId : childrenIds) { }, MoreExecutors.directExecutor());
futures.add(findRelationsRecursively(tenantId, entityId, direction, relationTypeGroup, lvl, fetchLastLevelOnly, uniqueMap));
} ListenableFuture<List<Set<EntityRelation>>> recursiveFutures = Futures.transformAsync(childrenIdsFuture, childrenIds ->
//TODO: try to remove this blocking operation Futures.successfulAsList(childrenIds.stream()
List<Set<EntityRelation>> relations = Futures.successfulAsList(futures).get(); .map(entityId -> findRelationsRecursively(tenantId, entityId, direction, relationTypeGroup, currentLvl, fetchLastLevelOnly, uniqueMap))
if (fetchLastLevelOnly && lvl > 0) { .collect(Collectors.toList())), MoreExecutors.directExecutor());
ListenableFuture<Set<EntityRelation>> relationsFuture = Futures.transform(recursiveFutures, recursiveRelations -> {
if (fetchLastLevelOnly && currentLvl > 0) {
children.clear(); children.clear();
} }
relations.forEach(r -> r.forEach(children::add)); recursiveRelations.forEach(children::addAll);
return Futures.immediateFuture(children); return children;
}, MoreExecutors.directExecutor());
return relationsFuture;
} }
private ListenableFuture<List<EntityRelation>> findRelations(final TenantId tenantId, final EntityId rootId, final EntitySearchDirection direction, RelationTypeGroup relationTypeGroup) { private ListenableFuture<List<EntityRelation>> findRelations(final TenantId tenantId, final EntityId rootId, final EntitySearchDirection direction, RelationTypeGroup relationTypeGroup) {