Showing
1 changed file
with
14 additions
and
62 deletions
@@ -15,7 +15,6 @@ | @@ -15,7 +15,6 @@ | ||
15 | */ | 15 | */ |
16 | package org.thingsboard.server.dao.entityview; | 16 | package org.thingsboard.server.dao.entityview; |
17 | 17 | ||
18 | -import com.google.common.util.concurrent.FutureCallback; | ||
19 | import com.google.common.util.concurrent.Futures; | 18 | import com.google.common.util.concurrent.Futures; |
20 | import com.google.common.util.concurrent.ListenableFuture; | 19 | import com.google.common.util.concurrent.ListenableFuture; |
21 | import lombok.extern.slf4j.Slf4j; | 20 | import lombok.extern.slf4j.Slf4j; |
@@ -24,6 +23,8 @@ import org.springframework.beans.factory.annotation.Autowired; | @@ -24,6 +23,8 @@ import org.springframework.beans.factory.annotation.Autowired; | ||
24 | import org.springframework.cache.Cache; | 23 | import org.springframework.cache.Cache; |
25 | import org.springframework.cache.CacheManager; | 24 | import org.springframework.cache.CacheManager; |
26 | import org.springframework.cache.annotation.CacheEvict; | 25 | import org.springframework.cache.annotation.CacheEvict; |
26 | +import org.springframework.cache.annotation.Cacheable; | ||
27 | +import org.springframework.cache.annotation.Caching; | ||
27 | import org.springframework.stereotype.Service; | 28 | import org.springframework.stereotype.Service; |
28 | import org.thingsboard.server.common.data.Customer; | 29 | import org.thingsboard.server.common.data.Customer; |
29 | import org.thingsboard.server.common.data.DataConstants; | 30 | import org.thingsboard.server.common.data.DataConstants; |
@@ -40,7 +41,6 @@ import org.thingsboard.server.common.data.page.TextPageData; | @@ -40,7 +41,6 @@ import org.thingsboard.server.common.data.page.TextPageData; | ||
40 | import org.thingsboard.server.common.data.page.TextPageLink; | 41 | import org.thingsboard.server.common.data.page.TextPageLink; |
41 | import org.thingsboard.server.common.data.relation.EntityRelation; | 42 | import org.thingsboard.server.common.data.relation.EntityRelation; |
42 | import org.thingsboard.server.common.data.relation.EntitySearchDirection; | 43 | import org.thingsboard.server.common.data.relation.EntitySearchDirection; |
43 | -import org.thingsboard.server.dao.DaoUtil; | ||
44 | import org.thingsboard.server.dao.attributes.AttributesService; | 44 | import org.thingsboard.server.dao.attributes.AttributesService; |
45 | import org.thingsboard.server.dao.customer.CustomerDao; | 45 | import org.thingsboard.server.dao.customer.CustomerDao; |
46 | import org.thingsboard.server.dao.entity.AbstractEntityService; | 46 | import org.thingsboard.server.dao.entity.AbstractEntityService; |
@@ -49,12 +49,10 @@ import org.thingsboard.server.dao.service.DataValidator; | @@ -49,12 +49,10 @@ import org.thingsboard.server.dao.service.DataValidator; | ||
49 | import org.thingsboard.server.dao.service.PaginatedRemover; | 49 | import org.thingsboard.server.dao.service.PaginatedRemover; |
50 | import org.thingsboard.server.dao.tenant.TenantDao; | 50 | import org.thingsboard.server.dao.tenant.TenantDao; |
51 | 51 | ||
52 | -import javax.annotation.Nullable; | ||
53 | import java.util.ArrayList; | 52 | import java.util.ArrayList; |
54 | import java.util.Arrays; | 53 | import java.util.Arrays; |
55 | import java.util.Collection; | 54 | import java.util.Collection; |
56 | import java.util.List; | 55 | import java.util.List; |
57 | -import java.util.UUID; | ||
58 | import java.util.concurrent.ExecutionException; | 56 | import java.util.concurrent.ExecutionException; |
59 | import java.util.stream.Collectors; | 57 | import java.util.stream.Collectors; |
60 | 58 | ||
@@ -74,7 +72,6 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti | @@ -74,7 +72,6 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti | ||
74 | public static final String INCORRECT_PAGE_LINK = "Incorrect page link "; | 72 | public static final String INCORRECT_PAGE_LINK = "Incorrect page link "; |
75 | public static final String INCORRECT_CUSTOMER_ID = "Incorrect customerId "; | 73 | public static final String INCORRECT_CUSTOMER_ID = "Incorrect customerId "; |
76 | public static final String INCORRECT_ENTITY_VIEW_ID = "Incorrect entityViewId "; | 74 | public static final String INCORRECT_ENTITY_VIEW_ID = "Incorrect entityViewId "; |
77 | - private static final int DEFAULT_LIMIT = 100; | ||
78 | 75 | ||
79 | @Autowired | 76 | @Autowired |
80 | private EntityViewDao entityViewDao; | 77 | private EntityViewDao entityViewDao; |
@@ -91,12 +88,15 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti | @@ -91,12 +88,15 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti | ||
91 | @Autowired | 88 | @Autowired |
92 | private CacheManager cacheManager; | 89 | private CacheManager cacheManager; |
93 | 90 | ||
94 | - @CacheEvict(cacheNames = ENTITY_VIEW_CACHE, key = "{#entityView.tenantId, #entityView.name}") | 91 | + @Caching(evict = { |
92 | + @CacheEvict(cacheNames = ENTITY_VIEW_CACHE, key = "{#entityView.tenantId, #entityView.entityId}"), | ||
93 | + @CacheEvict(cacheNames = ENTITY_VIEW_CACHE, key = "{#entityView.id}")}) | ||
95 | @Override | 94 | @Override |
96 | public EntityView saveEntityView(EntityView entityView) { | 95 | public EntityView saveEntityView(EntityView entityView) { |
97 | log.trace("Executing save entity view [{}]", entityView); | 96 | log.trace("Executing save entity view [{}]", entityView); |
98 | entityViewValidator.validate(entityView); | 97 | entityViewValidator.validate(entityView); |
99 | EntityView savedEntityView = entityViewDao.save(entityView); | 98 | EntityView savedEntityView = entityViewDao.save(entityView); |
99 | + | ||
100 | List<ListenableFuture<List<Void>>> futures = new ArrayList<>(); | 100 | List<ListenableFuture<List<Void>>> futures = new ArrayList<>(); |
101 | if (savedEntityView.getKeys() != null) { | 101 | if (savedEntityView.getKeys() != null) { |
102 | futures.add(copyAttributesFromEntityToEntityView(savedEntityView, DataConstants.CLIENT_SCOPE, savedEntityView.getKeys().getAttributes().getCs())); | 102 | futures.add(copyAttributesFromEntityToEntityView(savedEntityView, DataConstants.CLIENT_SCOPE, savedEntityView.getKeys().getAttributes().getCs())); |
@@ -121,6 +121,7 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti | @@ -121,6 +121,7 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti | ||
121 | return saveEntityView(entityView); | 121 | return saveEntityView(entityView); |
122 | } | 122 | } |
123 | 123 | ||
124 | + @CacheEvict(cacheNames = ENTITY_VIEW_CACHE, key = "{#entityViewId}") | ||
124 | @Override | 125 | @Override |
125 | public EntityView unassignEntityViewFromCustomer(EntityViewId entityViewId) { | 126 | public EntityView unassignEntityViewFromCustomer(EntityViewId entityViewId) { |
126 | EntityView entityView = findEntityViewById(entityViewId); | 127 | EntityView entityView = findEntityViewById(entityViewId); |
@@ -136,34 +137,11 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti | @@ -136,34 +137,11 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti | ||
136 | new CustomerEntityViewsUnAssigner(tenantId).removeEntities(customerId); | 137 | new CustomerEntityViewsUnAssigner(tenantId).removeEntities(customerId); |
137 | } | 138 | } |
138 | 139 | ||
139 | - | 140 | + @Cacheable(cacheNames = ENTITY_VIEW_CACHE, key = "{#entityViewId}") |
140 | @Override | 141 | @Override |
141 | public EntityView findEntityViewById(EntityViewId entityViewId) { | 142 | public EntityView findEntityViewById(EntityViewId entityViewId) { |
142 | log.trace("Executing findEntityViewById [{}]", entityViewId); | 143 | log.trace("Executing findEntityViewById [{}]", entityViewId); |
143 | validateId(entityViewId, INCORRECT_ENTITY_VIEW_ID + entityViewId); | 144 | validateId(entityViewId, INCORRECT_ENTITY_VIEW_ID + entityViewId); |
144 | - List<Object> ids = Arrays.asList(entityViewId.getId()); | ||
145 | - Cache cache = cacheManager.getCache(ENTITY_VIEW_CACHE); | ||
146 | - EntityView fromCache = cache.get(ids, EntityView.class); | ||
147 | - if (fromCache != null) { | ||
148 | - return fromCache; | ||
149 | - } else { | ||
150 | - ListenableFuture<EntityView> entityViewFuture | ||
151 | - = Futures.immediateFuture(entityViewDao.findById(entityViewId.getId())); | ||
152 | - Futures.addCallback(entityViewFuture, | ||
153 | - new FutureCallback<EntityView>() { | ||
154 | - @Override | ||
155 | - public void onSuccess(@Nullable EntityView result) { | ||
156 | - cache.putIfAbsent(ids, result); | ||
157 | - } | ||
158 | - @Override | ||
159 | - public void onFailure(Throwable t) {} | ||
160 | - }); | ||
161 | - try { | ||
162 | - return entityViewFuture.get(); | ||
163 | - } catch (Exception e) { | ||
164 | - log.error(e.getMessage()); | ||
165 | - } | ||
166 | - } | ||
167 | return entityViewDao.findById(entityViewId.getId()); | 145 | return entityViewDao.findById(entityViewId.getId()); |
168 | } | 146 | } |
169 | 147 | ||
@@ -203,7 +181,6 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti | @@ -203,7 +181,6 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti | ||
203 | } | 181 | } |
204 | return Futures.successfulAsList(futures); | 182 | return Futures.successfulAsList(futures); |
205 | }); | 183 | }); |
206 | - | ||
207 | return entityViews; | 184 | return entityViews; |
208 | } | 185 | } |
209 | 186 | ||
@@ -214,39 +191,24 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti | @@ -214,39 +191,24 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti | ||
214 | return entityViewDao.findByIdAsync(entityViewId.getId()); | 191 | return entityViewDao.findByIdAsync(entityViewId.getId()); |
215 | } | 192 | } |
216 | 193 | ||
194 | + @Cacheable(cacheNames = ENTITY_VIEW_CACHE, key = "{#tenantId, #entityId}") | ||
217 | @Override | 195 | @Override |
218 | public ListenableFuture<List<EntityView>> findEntityViewsByTenantIdAndEntityIdAsync(TenantId tenantId, EntityId entityId) { | 196 | public ListenableFuture<List<EntityView>> findEntityViewsByTenantIdAndEntityIdAsync(TenantId tenantId, EntityId entityId) { |
219 | log.trace("Executing findEntityViewsByTenantIdAndEntityIdAsync, tenantId [{}], entityId [{}]", tenantId, entityId); | 197 | log.trace("Executing findEntityViewsByTenantIdAndEntityIdAsync, tenantId [{}], entityId [{}]", tenantId, entityId); |
220 | validateId(tenantId, INCORRECT_TENANT_ID + tenantId); | 198 | validateId(tenantId, INCORRECT_TENANT_ID + tenantId); |
221 | validateId(entityId.getId(), "Incorrect entityId" + entityId); | 199 | validateId(entityId.getId(), "Incorrect entityId" + entityId); |
222 | - | ||
223 | - List<Object> tenantAndEntityIds = Arrays.asList(tenantId, entityId); | ||
224 | - Cache cache = cacheManager.getCache(ENTITY_VIEW_CACHE); | ||
225 | - List<EntityView> fromCache = cache.get(tenantAndEntityIds, List.class); | ||
226 | - if (fromCache != null) { | ||
227 | - return Futures.immediateFuture(fromCache); | ||
228 | - } else { | ||
229 | - ListenableFuture<List<EntityView>> entityViewsFuture = | ||
230 | - entityViewDao.findEntityViewsByTenantIdAndEntityIdAsync(tenantId.getId(), entityId.getId()); | ||
231 | - Futures.addCallback(entityViewsFuture, | ||
232 | - new FutureCallback<List<EntityView>>() { | ||
233 | - @Override | ||
234 | - public void onSuccess(@Nullable List<EntityView> result) { | ||
235 | - cache.putIfAbsent(tenantAndEntityIds, result); | ||
236 | - } | ||
237 | - @Override | ||
238 | - public void onFailure(Throwable t) {} | ||
239 | - }); | ||
240 | - return entityViewsFuture; | ||
241 | - } | 200 | + return entityViewDao.findEntityViewsByTenantIdAndEntityIdAsync(tenantId.getId(), entityId.getId()); |
242 | } | 201 | } |
243 | 202 | ||
203 | + @CacheEvict(cacheNames = ENTITY_VIEW_CACHE, key = "{#entityViewId}") | ||
244 | @Override | 204 | @Override |
245 | public void deleteEntityView(EntityViewId entityViewId) { | 205 | public void deleteEntityView(EntityViewId entityViewId) { |
246 | log.trace("Executing deleteEntityView [{}]", entityViewId); | 206 | log.trace("Executing deleteEntityView [{}]", entityViewId); |
247 | validateId(entityViewId, INCORRECT_ENTITY_VIEW_ID + entityViewId); | 207 | validateId(entityViewId, INCORRECT_ENTITY_VIEW_ID + entityViewId); |
248 | deleteEntityRelations(entityViewId); | 208 | deleteEntityRelations(entityViewId); |
249 | - cacheEvict(entityViewId, cacheManager.getCache(ENTITY_VIEW_CACHE)); | 209 | + Cache cache = cacheManager.getCache(ENTITY_VIEW_CACHE); |
210 | + EntityView entityView = entityViewDao.findById(entityViewId.getId()); | ||
211 | + cache.evict(Arrays.asList(entityView.getTenantId(), entityView.getEntityId())); | ||
250 | entityViewDao.removeById(entityViewId.getId()); | 212 | entityViewDao.removeById(entityViewId.getId()); |
251 | } | 213 | } |
252 | 214 | ||
@@ -254,19 +216,9 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti | @@ -254,19 +216,9 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti | ||
254 | public void deleteEntityViewsByTenantId(TenantId tenantId) { | 216 | public void deleteEntityViewsByTenantId(TenantId tenantId) { |
255 | log.trace("Executing deleteEntityViewsByTenantId, tenantId [{}]", tenantId); | 217 | log.trace("Executing deleteEntityViewsByTenantId, tenantId [{}]", tenantId); |
256 | validateId(tenantId, INCORRECT_TENANT_ID + tenantId); | 218 | validateId(tenantId, INCORRECT_TENANT_ID + tenantId); |
257 | - entityViewDao.findEntityViewsByTenantId(tenantId.getId(), new TextPageLink(DEFAULT_LIMIT)).stream() | ||
258 | - .map(view -> view.getId()) | ||
259 | - .collect(Collectors.toList()) | ||
260 | - .forEach(id -> cacheEvict(id, cacheManager.getCache(ENTITY_VIEW_CACHE))); | ||
261 | tenantEntityViewRemover.removeEntities(tenantId); | 219 | tenantEntityViewRemover.removeEntities(tenantId); |
262 | } | 220 | } |
263 | 221 | ||
264 | - private void cacheEvict(EntityViewId entityViewId, Cache cache) { | ||
265 | - EntityView entityView = entityViewDao.findById(entityViewId.getId()); | ||
266 | - cache.evict(Arrays.asList(entityView.getTenantId(), entityView.getName())); | ||
267 | - cache.evict(Arrays.asList(entityView.getTenantId(), entityView.getEntityId())); | ||
268 | - } | ||
269 | - | ||
270 | private ListenableFuture<List<Void>> copyAttributesFromEntityToEntityView(EntityView entityView, String scope, Collection<String> keys) { | 222 | private ListenableFuture<List<Void>> copyAttributesFromEntityToEntityView(EntityView entityView, String scope, Collection<String> keys) { |
271 | if (keys != null && !keys.isEmpty()) { | 223 | if (keys != null && !keys.isEmpty()) { |
272 | ListenableFuture<List<AttributeKvEntry>> getAttrFuture = attributesService.find(entityView.getEntityId(), scope, keys); | 224 | ListenableFuture<List<AttributeKvEntry>> getAttrFuture = attributesService.find(entityView.getEntityId(), scope, keys); |