From eba98b80e65fd734cc29b5333af431bd56d350d6 Mon Sep 17 00:00:00 2001 From: Dima Landiak Date: Mon, 7 May 2018 18:40:49 +0300 Subject: [PATCH] relation cache fixes --- .../dao/relation/BaseRelationService.java | 94 ++++++++++--------- 1 file changed, 51 insertions(+), 43 deletions(-) diff --git a/dao/src/main/java/org/thingsboard/server/dao/relation/BaseRelationService.java b/dao/src/main/java/org/thingsboard/server/dao/relation/BaseRelationService.java index 370d770188..e9f808aee1 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/relation/BaseRelationService.java +++ b/dao/src/main/java/org/thingsboard/server/dao/relation/BaseRelationService.java @@ -170,12 +170,12 @@ public class BaseRelationService implements RelationService { Cache cache = cacheManager.getCache(RELATIONS_CACHE); log.trace("Executing deleteEntityRelations [{}]", entity); validate(entity); - List>> inboundRelationsListTo = new ArrayList<>(); + List>> inboundRelationsList = new ArrayList<>(); for (RelationTypeGroup typeGroup : RelationTypeGroup.values()) { - inboundRelationsListTo.add(relationDao.findAllByTo(entity, typeGroup)); + inboundRelationsList.add(relationDao.findAllByTo(entity, typeGroup)); } - ListenableFuture>> inboundRelationsTo = Futures.allAsList(inboundRelationsListTo); - ListenableFuture> inboundDeletions = Futures.transform(inboundRelationsTo, (List> relations) -> + ListenableFuture>> inboundRelations = Futures.allAsList(inboundRelationsList); + ListenableFuture> inboundDeletions = Futures.transform(inboundRelations, (List> relations) -> getBooleans(relations, cache, true)); ListenableFuture inboundFuture = Futures.transform(inboundDeletions, getListToBooleanFunction()); @@ -186,12 +186,12 @@ public class BaseRelationService implements RelationService { log.error("Error deleting entity inbound relations", e); } - List>> inboundRelationsListFrom = new ArrayList<>(); + List>> outboundRelationsList = new ArrayList<>(); for (RelationTypeGroup typeGroup : RelationTypeGroup.values()) { - inboundRelationsListFrom.add(relationDao.findAllByFrom(entity, typeGroup)); + outboundRelationsList.add(relationDao.findAllByFrom(entity, typeGroup)); } - ListenableFuture>> inboundRelationsFrom = Futures.allAsList(inboundRelationsListFrom); - Futures.transform(inboundRelationsFrom, (Function>, List>) relations -> + ListenableFuture>> outboundRelations = Futures.allAsList(outboundRelationsList); + Futures.transform(outboundRelations, (Function>, List>) relations -> getBooleans(relations, cache, false)); boolean outboundDeleteResult = relationDao.deleteOutboundRelations(entity); @@ -201,9 +201,7 @@ public class BaseRelationService implements RelationService { private List getBooleans(List> relations, Cache cache, boolean isRemove) { List results = new ArrayList<>(); for (List relationList : relations) { - relationList.stream().forEach(relation -> { - checkFromDeleteSync(cache, results, relation, isRemove); - }); + relationList.stream().forEach(relation -> checkFromDeleteSync(cache, results, relation, isRemove)); } return results; } @@ -211,10 +209,8 @@ public class BaseRelationService implements RelationService { private void checkFromDeleteSync(Cache cache, List results, EntityRelation relation, boolean isRemove) { if (isRemove) { results.add(relationDao.deleteRelation(relation)); - cacheEviction(relation, relation.getTo(), cache); - } else { - cacheEviction(relation, relation.getFrom(), cache); } + cacheEviction(relation, cache); } @Override @@ -222,12 +218,12 @@ public class BaseRelationService implements RelationService { Cache cache = cacheManager.getCache(RELATIONS_CACHE); log.trace("Executing deleteEntityRelationsAsync [{}]", entity); validate(entity); - List>> inboundRelationsListTo = new ArrayList<>(); + List>> inboundRelationsList = new ArrayList<>(); for (RelationTypeGroup typeGroup : RelationTypeGroup.values()) { - inboundRelationsListTo.add(relationDao.findAllByTo(entity, typeGroup)); + inboundRelationsList.add(relationDao.findAllByTo(entity, typeGroup)); } - ListenableFuture>> inboundRelationsTo = Futures.allAsList(inboundRelationsListTo); - ListenableFuture> inboundDeletions = Futures.transform(inboundRelationsTo, + ListenableFuture>> inboundRelations = Futures.allAsList(inboundRelationsList); + ListenableFuture> inboundDeletions = Futures.transform(inboundRelations, (AsyncFunction>, List>) relations -> { List> results = getListenableFutures(relations, cache, true); return Futures.allAsList(results); @@ -235,12 +231,12 @@ public class BaseRelationService implements RelationService { ListenableFuture inboundFuture = Futures.transform(inboundDeletions, getListToBooleanFunction()); - List>> inboundRelationsListFrom = new ArrayList<>(); + List>> outboundRelationsList = new ArrayList<>(); for (RelationTypeGroup typeGroup : RelationTypeGroup.values()) { - inboundRelationsListFrom.add(relationDao.findAllByTo(entity, typeGroup)); + outboundRelationsList.add(relationDao.findAllByFrom(entity, typeGroup)); } - ListenableFuture>> inboundRelationsFrom = Futures.allAsList(inboundRelationsListFrom); - Futures.transform(inboundRelationsFrom, (AsyncFunction>, List>) relations -> { + ListenableFuture>> outboundRelations = Futures.allAsList(outboundRelationsList); + Futures.transform(outboundRelations, (AsyncFunction>, List>) relations -> { List> results = getListenableFutures(relations, cache, false); return Futures.allAsList(results); }); @@ -252,9 +248,7 @@ public class BaseRelationService implements RelationService { private List> getListenableFutures(List> relations, Cache cache, boolean isRemove) { List> results = new ArrayList<>(); for (List relationList : relations) { - relationList.stream().forEach(relation -> { - checkFromDeleteAsync(cache, results, relation, isRemove); - }); + relationList.stream().forEach(relation -> checkFromDeleteAsync(cache, results, relation, isRemove)); } return results; } @@ -262,25 +256,39 @@ public class BaseRelationService implements RelationService { private void checkFromDeleteAsync(Cache cache, List> results, EntityRelation relation, boolean isRemove) { if (isRemove) { results.add(relationDao.deleteRelationAsync(relation)); - cacheEviction(relation, relation.getTo(), cache); - } else { - cacheEviction(relation, relation.getFrom(), cache); } - } - - private void cacheEviction(EntityRelation relation, EntityId entityId, Cache cache) { - cache.evict(entityId); - - List toAndType = new ArrayList<>(); - toAndType.add(entityId); - toAndType.add(relation.getType()); - cache.evict(toAndType); - - List fromToAndType = new ArrayList<>(); - fromToAndType.add(relation.getFrom()); - fromToAndType.add(relation.getTo()); - fromToAndType.add(relation.getType()); - cache.evict(fromToAndType); + cacheEviction(relation, cache); + } + + private void cacheEviction(EntityRelation relation, Cache cache) { + List toAndGroup = new ArrayList<>(); + toAndGroup.add(relation.getTo()); + toAndGroup.add(relation.getTypeGroup()); + cache.evict(toAndGroup); + + List toTypeAndGroup = new ArrayList<>(); + toTypeAndGroup.add(relation.getTo()); + toTypeAndGroup.add(relation.getType()); + toTypeAndGroup.add(relation.getTypeGroup()); + cache.evict(toTypeAndGroup); + + List fromAndGroup = new ArrayList<>(); + fromAndGroup.add(relation.getFrom()); + fromAndGroup.add(relation.getTypeGroup()); + cache.evict(fromAndGroup); + + List fromTypeAndGroup = new ArrayList<>(); + fromTypeAndGroup.add(relation.getFrom()); + fromTypeAndGroup.add(relation.getType()); + fromTypeAndGroup.add(relation.getTypeGroup()); + cache.evict(fromTypeAndGroup); + + List fromToTypeAndGroup = new ArrayList<>(); + fromToTypeAndGroup.add(relation.getFrom()); + fromToTypeAndGroup.add(relation.getTo()); + fromToTypeAndGroup.add(relation.getType()); + fromToTypeAndGroup.add(relation.getTypeGroup()); + cache.evict(fromToTypeAndGroup); } @Cacheable(cacheNames = RELATIONS_CACHE, key = "{#from, #typeGroup}")