Commit 9b1c59a915003737a043d06d4ed0dda4efe6a0f8

Authored by Andrew Shvayka
Committed by GitHub
2 parents 50e698eb eba98b80

Merge pull request #769 from dmytro-landiak/master

relation cache fixes
@@ -170,12 +170,12 @@ public class BaseRelationService implements RelationService { @@ -170,12 +170,12 @@ public class BaseRelationService implements RelationService {
170 Cache cache = cacheManager.getCache(RELATIONS_CACHE); 170 Cache cache = cacheManager.getCache(RELATIONS_CACHE);
171 log.trace("Executing deleteEntityRelations [{}]", entity); 171 log.trace("Executing deleteEntityRelations [{}]", entity);
172 validate(entity); 172 validate(entity);
173 - List<ListenableFuture<List<EntityRelation>>> inboundRelationsListTo = new ArrayList<>(); 173 + List<ListenableFuture<List<EntityRelation>>> inboundRelationsList = new ArrayList<>();
174 for (RelationTypeGroup typeGroup : RelationTypeGroup.values()) { 174 for (RelationTypeGroup typeGroup : RelationTypeGroup.values()) {
175 - inboundRelationsListTo.add(relationDao.findAllByTo(entity, typeGroup)); 175 + inboundRelationsList.add(relationDao.findAllByTo(entity, typeGroup));
176 } 176 }
177 - ListenableFuture<List<List<EntityRelation>>> inboundRelationsTo = Futures.allAsList(inboundRelationsListTo);  
178 - ListenableFuture<List<Boolean>> inboundDeletions = Futures.transform(inboundRelationsTo, (List<List<EntityRelation>> relations) -> 177 + ListenableFuture<List<List<EntityRelation>>> inboundRelations = Futures.allAsList(inboundRelationsList);
  178 + ListenableFuture<List<Boolean>> inboundDeletions = Futures.transform(inboundRelations, (List<List<EntityRelation>> relations) ->
179 getBooleans(relations, cache, true)); 179 getBooleans(relations, cache, true));
180 180
181 ListenableFuture<Boolean> inboundFuture = Futures.transform(inboundDeletions, getListToBooleanFunction()); 181 ListenableFuture<Boolean> inboundFuture = Futures.transform(inboundDeletions, getListToBooleanFunction());
@@ -186,12 +186,12 @@ public class BaseRelationService implements RelationService { @@ -186,12 +186,12 @@ public class BaseRelationService implements RelationService {
186 log.error("Error deleting entity inbound relations", e); 186 log.error("Error deleting entity inbound relations", e);
187 } 187 }
188 188
189 - List<ListenableFuture<List<EntityRelation>>> inboundRelationsListFrom = new ArrayList<>(); 189 + List<ListenableFuture<List<EntityRelation>>> outboundRelationsList = new ArrayList<>();
190 for (RelationTypeGroup typeGroup : RelationTypeGroup.values()) { 190 for (RelationTypeGroup typeGroup : RelationTypeGroup.values()) {
191 - inboundRelationsListFrom.add(relationDao.findAllByFrom(entity, typeGroup)); 191 + outboundRelationsList.add(relationDao.findAllByFrom(entity, typeGroup));
192 } 192 }
193 - ListenableFuture<List<List<EntityRelation>>> inboundRelationsFrom = Futures.allAsList(inboundRelationsListFrom);  
194 - Futures.transform(inboundRelationsFrom, (Function<List<List<EntityRelation>>, List<Boolean>>) relations -> 193 + ListenableFuture<List<List<EntityRelation>>> outboundRelations = Futures.allAsList(outboundRelationsList);
  194 + Futures.transform(outboundRelations, (Function<List<List<EntityRelation>>, List<Boolean>>) relations ->
195 getBooleans(relations, cache, false)); 195 getBooleans(relations, cache, false));
196 196
197 boolean outboundDeleteResult = relationDao.deleteOutboundRelations(entity); 197 boolean outboundDeleteResult = relationDao.deleteOutboundRelations(entity);
@@ -201,9 +201,7 @@ public class BaseRelationService implements RelationService { @@ -201,9 +201,7 @@ public class BaseRelationService implements RelationService {
201 private List<Boolean> getBooleans(List<List<EntityRelation>> relations, Cache cache, boolean isRemove) { 201 private List<Boolean> getBooleans(List<List<EntityRelation>> relations, Cache cache, boolean isRemove) {
202 List<Boolean> results = new ArrayList<>(); 202 List<Boolean> results = new ArrayList<>();
203 for (List<EntityRelation> relationList : relations) { 203 for (List<EntityRelation> relationList : relations) {
204 - relationList.stream().forEach(relation -> {  
205 - checkFromDeleteSync(cache, results, relation, isRemove);  
206 - }); 204 + relationList.stream().forEach(relation -> checkFromDeleteSync(cache, results, relation, isRemove));
207 } 205 }
208 return results; 206 return results;
209 } 207 }
@@ -211,10 +209,8 @@ public class BaseRelationService implements RelationService { @@ -211,10 +209,8 @@ public class BaseRelationService implements RelationService {
211 private void checkFromDeleteSync(Cache cache, List<Boolean> results, EntityRelation relation, boolean isRemove) { 209 private void checkFromDeleteSync(Cache cache, List<Boolean> results, EntityRelation relation, boolean isRemove) {
212 if (isRemove) { 210 if (isRemove) {
213 results.add(relationDao.deleteRelation(relation)); 211 results.add(relationDao.deleteRelation(relation));
214 - cacheEviction(relation, relation.getTo(), cache);  
215 - } else {  
216 - cacheEviction(relation, relation.getFrom(), cache);  
217 } 212 }
  213 + cacheEviction(relation, cache);
218 } 214 }
219 215
220 @Override 216 @Override
@@ -222,12 +218,12 @@ public class BaseRelationService implements RelationService { @@ -222,12 +218,12 @@ public class BaseRelationService implements RelationService {
222 Cache cache = cacheManager.getCache(RELATIONS_CACHE); 218 Cache cache = cacheManager.getCache(RELATIONS_CACHE);
223 log.trace("Executing deleteEntityRelationsAsync [{}]", entity); 219 log.trace("Executing deleteEntityRelationsAsync [{}]", entity);
224 validate(entity); 220 validate(entity);
225 - List<ListenableFuture<List<EntityRelation>>> inboundRelationsListTo = new ArrayList<>(); 221 + List<ListenableFuture<List<EntityRelation>>> inboundRelationsList = new ArrayList<>();
226 for (RelationTypeGroup typeGroup : RelationTypeGroup.values()) { 222 for (RelationTypeGroup typeGroup : RelationTypeGroup.values()) {
227 - inboundRelationsListTo.add(relationDao.findAllByTo(entity, typeGroup)); 223 + inboundRelationsList.add(relationDao.findAllByTo(entity, typeGroup));
228 } 224 }
229 - ListenableFuture<List<List<EntityRelation>>> inboundRelationsTo = Futures.allAsList(inboundRelationsListTo);  
230 - ListenableFuture<List<Boolean>> inboundDeletions = Futures.transform(inboundRelationsTo, 225 + ListenableFuture<List<List<EntityRelation>>> inboundRelations = Futures.allAsList(inboundRelationsList);
  226 + ListenableFuture<List<Boolean>> inboundDeletions = Futures.transform(inboundRelations,
231 (AsyncFunction<List<List<EntityRelation>>, List<Boolean>>) relations -> { 227 (AsyncFunction<List<List<EntityRelation>>, List<Boolean>>) relations -> {
232 List<ListenableFuture<Boolean>> results = getListenableFutures(relations, cache, true); 228 List<ListenableFuture<Boolean>> results = getListenableFutures(relations, cache, true);
233 return Futures.allAsList(results); 229 return Futures.allAsList(results);
@@ -235,12 +231,12 @@ public class BaseRelationService implements RelationService { @@ -235,12 +231,12 @@ public class BaseRelationService implements RelationService {
235 231
236 ListenableFuture<Boolean> inboundFuture = Futures.transform(inboundDeletions, getListToBooleanFunction()); 232 ListenableFuture<Boolean> inboundFuture = Futures.transform(inboundDeletions, getListToBooleanFunction());
237 233
238 - List<ListenableFuture<List<EntityRelation>>> inboundRelationsListFrom = new ArrayList<>(); 234 + List<ListenableFuture<List<EntityRelation>>> outboundRelationsList = new ArrayList<>();
239 for (RelationTypeGroup typeGroup : RelationTypeGroup.values()) { 235 for (RelationTypeGroup typeGroup : RelationTypeGroup.values()) {
240 - inboundRelationsListFrom.add(relationDao.findAllByTo(entity, typeGroup)); 236 + outboundRelationsList.add(relationDao.findAllByFrom(entity, typeGroup));
241 } 237 }
242 - ListenableFuture<List<List<EntityRelation>>> inboundRelationsFrom = Futures.allAsList(inboundRelationsListFrom);  
243 - Futures.transform(inboundRelationsFrom, (AsyncFunction<List<List<EntityRelation>>, List<Boolean>>) relations -> { 238 + ListenableFuture<List<List<EntityRelation>>> outboundRelations = Futures.allAsList(outboundRelationsList);
  239 + Futures.transform(outboundRelations, (AsyncFunction<List<List<EntityRelation>>, List<Boolean>>) relations -> {
244 List<ListenableFuture<Boolean>> results = getListenableFutures(relations, cache, false); 240 List<ListenableFuture<Boolean>> results = getListenableFutures(relations, cache, false);
245 return Futures.allAsList(results); 241 return Futures.allAsList(results);
246 }); 242 });
@@ -252,9 +248,7 @@ public class BaseRelationService implements RelationService { @@ -252,9 +248,7 @@ public class BaseRelationService implements RelationService {
252 private List<ListenableFuture<Boolean>> getListenableFutures(List<List<EntityRelation>> relations, Cache cache, boolean isRemove) { 248 private List<ListenableFuture<Boolean>> getListenableFutures(List<List<EntityRelation>> relations, Cache cache, boolean isRemove) {
253 List<ListenableFuture<Boolean>> results = new ArrayList<>(); 249 List<ListenableFuture<Boolean>> results = new ArrayList<>();
254 for (List<EntityRelation> relationList : relations) { 250 for (List<EntityRelation> relationList : relations) {
255 - relationList.stream().forEach(relation -> {  
256 - checkFromDeleteAsync(cache, results, relation, isRemove);  
257 - }); 251 + relationList.stream().forEach(relation -> checkFromDeleteAsync(cache, results, relation, isRemove));
258 } 252 }
259 return results; 253 return results;
260 } 254 }
@@ -262,25 +256,39 @@ public class BaseRelationService implements RelationService { @@ -262,25 +256,39 @@ public class BaseRelationService implements RelationService {
262 private void checkFromDeleteAsync(Cache cache, List<ListenableFuture<Boolean>> results, EntityRelation relation, boolean isRemove) { 256 private void checkFromDeleteAsync(Cache cache, List<ListenableFuture<Boolean>> results, EntityRelation relation, boolean isRemove) {
263 if (isRemove) { 257 if (isRemove) {
264 results.add(relationDao.deleteRelationAsync(relation)); 258 results.add(relationDao.deleteRelationAsync(relation));
265 - cacheEviction(relation, relation.getTo(), cache);  
266 - } else {  
267 - cacheEviction(relation, relation.getFrom(), cache);  
268 } 259 }
269 - }  
270 -  
271 - private void cacheEviction(EntityRelation relation, EntityId entityId, Cache cache) {  
272 - cache.evict(entityId);  
273 -  
274 - List<Object> toAndType = new ArrayList<>();  
275 - toAndType.add(entityId);  
276 - toAndType.add(relation.getType());  
277 - cache.evict(toAndType);  
278 -  
279 - List<Object> fromToAndType = new ArrayList<>();  
280 - fromToAndType.add(relation.getFrom());  
281 - fromToAndType.add(relation.getTo());  
282 - fromToAndType.add(relation.getType());  
283 - cache.evict(fromToAndType); 260 + cacheEviction(relation, cache);
  261 + }
  262 +
  263 + private void cacheEviction(EntityRelation relation, Cache cache) {
  264 + List<Object> toAndGroup = new ArrayList<>();
  265 + toAndGroup.add(relation.getTo());
  266 + toAndGroup.add(relation.getTypeGroup());
  267 + cache.evict(toAndGroup);
  268 +
  269 + List<Object> toTypeAndGroup = new ArrayList<>();
  270 + toTypeAndGroup.add(relation.getTo());
  271 + toTypeAndGroup.add(relation.getType());
  272 + toTypeAndGroup.add(relation.getTypeGroup());
  273 + cache.evict(toTypeAndGroup);
  274 +
  275 + List<Object> fromAndGroup = new ArrayList<>();
  276 + fromAndGroup.add(relation.getFrom());
  277 + fromAndGroup.add(relation.getTypeGroup());
  278 + cache.evict(fromAndGroup);
  279 +
  280 + List<Object> fromTypeAndGroup = new ArrayList<>();
  281 + fromTypeAndGroup.add(relation.getFrom());
  282 + fromTypeAndGroup.add(relation.getType());
  283 + fromTypeAndGroup.add(relation.getTypeGroup());
  284 + cache.evict(fromTypeAndGroup);
  285 +
  286 + List<Object> fromToTypeAndGroup = new ArrayList<>();
  287 + fromToTypeAndGroup.add(relation.getFrom());
  288 + fromToTypeAndGroup.add(relation.getTo());
  289 + fromToTypeAndGroup.add(relation.getType());
  290 + fromToTypeAndGroup.add(relation.getTypeGroup());
  291 + cache.evict(fromToTypeAndGroup);
284 } 292 }
285 293
286 @Cacheable(cacheNames = RELATIONS_CACHE, key = "{#from, #typeGroup}") 294 @Cacheable(cacheNames = RELATIONS_CACHE, key = "{#from, #typeGroup}")