Commit 9586569386da4461faa1b05b251656d41852377b

Authored by vparomskiy
1 parent 142a422b

use direction in relation search cache

... ... @@ -39,7 +39,6 @@ import org.thingsboard.server.dao.exception.DataValidationException;
39 39
40 40 import javax.annotation.Nullable;
41 41 import java.util.ArrayList;
42   -import java.util.Arrays;
43 42 import java.util.Collections;
44 43 import java.util.HashSet;
45 44 import java.util.List;
... ... @@ -92,10 +91,10 @@ public class BaseRelationService implements RelationService {
92 91
93 92 @Caching(evict = {
94 93 @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.from, #relation.to, #relation.type, #relation.typeGroup}"),
95   - @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.from, #relation.type, #relation.typeGroup}"),
96   - @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.from, #relation.typeGroup}"),
97   - @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.to, #relation.typeGroup}"),
98   - @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.to, #relation.type, #relation.typeGroup}")
  94 + @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.from, #relation.type, #relation.typeGroup, 'FROM'}"),
  95 + @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.from, #relation.typeGroup, 'FROM'}"),
  96 + @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.to, #relation.typeGroup, 'TO'}"),
  97 + @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.to, #relation.type, #relation.typeGroup, 'TO'}")
99 98 })
100 99 @Override
101 100 public boolean saveRelation(EntityRelation relation) {
... ... @@ -106,10 +105,10 @@ public class BaseRelationService implements RelationService {
106 105
107 106 @Caching(evict = {
108 107 @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.from, #relation.to, #relation.type, #relation.typeGroup}"),
109   - @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.from, #relation.type, #relation.typeGroup}"),
110   - @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.from, #relation.typeGroup}"),
111   - @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.to, #relation.typeGroup}"),
112   - @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.to, #relation.type, #relation.typeGroup}")
  108 + @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.from, #relation.type, #relation.typeGroup, 'FROM'}"),
  109 + @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.from, #relation.typeGroup, 'FROM'}"),
  110 + @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.to, #relation.typeGroup, 'TO'}"),
  111 + @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.to, #relation.type, #relation.typeGroup, 'TO'}")
113 112 })
114 113 @Override
115 114 public ListenableFuture<Boolean> saveRelationAsync(EntityRelation relation) {
... ... @@ -120,10 +119,10 @@ public class BaseRelationService implements RelationService {
120 119
121 120 @Caching(evict = {
122 121 @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.from, #relation.to, #relation.type, #relation.typeGroup}"),
123   - @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.from, #relation.type, #relation.typeGroup}"),
124   - @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.from, #relation.typeGroup}"),
125   - @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.to, #relation.typeGroup}"),
126   - @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.to, #relation.type, #relation.typeGroup}")
  122 + @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.from, #relation.type, #relation.typeGroup, 'FROM'}"),
  123 + @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.from, #relation.typeGroup, 'FROM'}"),
  124 + @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.to, #relation.typeGroup, 'TO'}"),
  125 + @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.to, #relation.type, #relation.typeGroup, 'TO'}")
127 126 })
128 127 @Override
129 128 public boolean deleteRelation(EntityRelation relation) {
... ... @@ -134,10 +133,10 @@ public class BaseRelationService implements RelationService {
134 133
135 134 @Caching(evict = {
136 135 @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.from, #relation.to, #relation.type, #relation.typeGroup}"),
137   - @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.from, #relation.type, #relation.typeGroup}"),
138   - @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.from, #relation.typeGroup}"),
139   - @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.to, #relation.typeGroup}"),
140   - @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.to, #relation.type, #relation.typeGroup}")
  136 + @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.from, #relation.type, #relation.typeGroup, 'FROM'}"),
  137 + @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.from, #relation.typeGroup, 'FROM'}"),
  138 + @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.to, #relation.typeGroup, 'TO'}"),
  139 + @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#relation.to, #relation.type, #relation.typeGroup, 'TO'}")
141 140 })
142 141 @Override
143 142 public ListenableFuture<Boolean> deleteRelationAsync(EntityRelation relation) {
... ... @@ -148,10 +147,10 @@ public class BaseRelationService implements RelationService {
148 147
149 148 @Caching(evict = {
150 149 @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#from, #to, #relationType, #typeGroup}"),
151   - @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#from, #relationType, #typeGroup}"),
152   - @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#from, #typeGroup}"),
153   - @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#to, #typeGroup}"),
154   - @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#to, #relationType, #typeGroup}")
  150 + @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#from, #relationType, #typeGroup, 'FROM'}"),
  151 + @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#from, #typeGroup, 'FROM'}"),
  152 + @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#to, #typeGroup, 'TO'}"),
  153 + @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#to, #relationType, #typeGroup, 'TO'}")
155 154 })
156 155 @Override
157 156 public boolean deleteRelation(EntityId from, EntityId to, String relationType, RelationTypeGroup typeGroup) {
... ... @@ -162,10 +161,10 @@ public class BaseRelationService implements RelationService {
162 161
163 162 @Caching(evict = {
164 163 @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#from, #to, #relationType, #typeGroup}"),
165   - @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#from, #relationType, #typeGroup}"),
166   - @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#from, #typeGroup}"),
167   - @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#to, #typeGroup}"),
168   - @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#to, #relationType, #typeGroup}")
  164 + @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#from, #relationType, #typeGroup, 'FROM'}"),
  165 + @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#from, #typeGroup, 'FROM'}"),
  166 + @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#to, #typeGroup, 'TO'}"),
  167 + @CacheEvict(cacheNames = RELATIONS_CACHE, key = "{#to, #relationType, #typeGroup, 'TO'}")
169 168 })
170 169 @Override
171 170 public ListenableFuture<Boolean> deleteRelationAsync(EntityId from, EntityId to, String relationType, RelationTypeGroup typeGroup) {
... ... @@ -248,26 +247,30 @@ public class BaseRelationService implements RelationService {
248 247 fromTypeAndTypeGroup.add(relation.getFrom());
249 248 fromTypeAndTypeGroup.add(relation.getType());
250 249 fromTypeAndTypeGroup.add(relation.getTypeGroup());
  250 + fromTypeAndTypeGroup.add(EntitySearchDirection.FROM.name());
251 251 cache.evict(fromTypeAndTypeGroup);
252 252
253 253 List<Object> fromAndTypeGroup = new ArrayList<>();
254 254 fromAndTypeGroup.add(relation.getFrom());
255 255 fromAndTypeGroup.add(relation.getTypeGroup());
  256 + fromAndTypeGroup.add(EntitySearchDirection.FROM.name());
256 257 cache.evict(fromAndTypeGroup);
257 258
258 259 List<Object> toAndTypeGroup = new ArrayList<>();
259 260 toAndTypeGroup.add(relation.getTo());
260 261 toAndTypeGroup.add(relation.getTypeGroup());
  262 + toAndTypeGroup.add(EntitySearchDirection.TO.name());
261 263 cache.evict(toAndTypeGroup);
262 264
263 265 List<Object> toTypeAndTypeGroup = new ArrayList<>();
264   - fromTypeAndTypeGroup.add(relation.getTo());
265   - fromTypeAndTypeGroup.add(relation.getType());
266   - fromTypeAndTypeGroup.add(relation.getTypeGroup());
  266 + toTypeAndTypeGroup.add(relation.getTo());
  267 + toTypeAndTypeGroup.add(relation.getType());
  268 + toTypeAndTypeGroup.add(relation.getTypeGroup());
  269 + toTypeAndTypeGroup.add(EntitySearchDirection.TO.name());
267 270 cache.evict(toTypeAndTypeGroup);
268 271 }
269 272
270   - @Cacheable(cacheNames = RELATIONS_CACHE, key = "{#from, #typeGroup}")
  273 + @Cacheable(cacheNames = RELATIONS_CACHE, key = "{#from, #typeGroup, 'FROM'}")
271 274 @Override
272 275 public List<EntityRelation> findByFrom(EntityId from, RelationTypeGroup typeGroup) {
273 276 validate(from);
... ... @@ -288,6 +291,7 @@ public class BaseRelationService implements RelationService {
288 291 List<Object> fromAndTypeGroup = new ArrayList<>();
289 292 fromAndTypeGroup.add(from);
290 293 fromAndTypeGroup.add(typeGroup);
  294 + fromAndTypeGroup.add(EntitySearchDirection.FROM.name());
291 295
292 296 Cache cache = cacheManager.getCache(RELATIONS_CACHE);
293 297 List<EntityRelation> fromCache = cache.get(fromAndTypeGroup, List.class);
... ... @@ -326,7 +330,7 @@ public class BaseRelationService implements RelationService {
326 330 });
327 331 }
328 332
329   - @Cacheable(cacheNames = RELATIONS_CACHE, key = "{#from, #relationType, #typeGroup}")
  333 + @Cacheable(cacheNames = RELATIONS_CACHE, key = "{#from, #relationType, #typeGroup, 'FROM'}")
330 334 @Override
331 335 public List<EntityRelation> findByFromAndType(EntityId from, String relationType, RelationTypeGroup typeGroup) {
332 336 try {
... ... @@ -345,7 +349,7 @@ public class BaseRelationService implements RelationService {
345 349 return relationDao.findAllByFromAndType(from, relationType, typeGroup);
346 350 }
347 351
348   - @Cacheable(cacheNames = RELATIONS_CACHE, key = "{#to, #typeGroup}")
  352 + @Cacheable(cacheNames = RELATIONS_CACHE, key = "{#to, #typeGroup, 'TO'}")
349 353 @Override
350 354 public List<EntityRelation> findByTo(EntityId to, RelationTypeGroup typeGroup) {
351 355 validate(to);
... ... @@ -366,6 +370,7 @@ public class BaseRelationService implements RelationService {
366 370 List<Object> toAndTypeGroup = new ArrayList<>();
367 371 toAndTypeGroup.add(to);
368 372 toAndTypeGroup.add(typeGroup);
  373 + toAndTypeGroup.add(EntitySearchDirection.TO.name());
369 374
370 375 Cache cache = cacheManager.getCache(RELATIONS_CACHE);
371 376 List<EntityRelation> fromCache = cache.get(toAndTypeGroup, List.class);
... ... @@ -415,7 +420,7 @@ public class BaseRelationService implements RelationService {
415 420 });
416 421 }
417 422
418   - @Cacheable(cacheNames = RELATIONS_CACHE, key = "{#to, #relationType, #typeGroup}")
  423 + @Cacheable(cacheNames = RELATIONS_CACHE, key = "{#to, #relationType, #typeGroup, 'TO'}")
419 424 @Override
420 425 public List<EntityRelation> findByToAndType(EntityId to, String relationType, RelationTypeGroup typeGroup) {
421 426 try {
... ...