Repository: incubator-unomi Updated Branches: refs/heads/feature-UNOMI-5-KARAF4-1 f367d4680 -> bf8c6096a (forced update)
UNOMI-168 Update/Delete PropertyType in-mem cache Update PropertyType in-memory cache on update / delete operation. Also remove unnecessary delays in integration tests. Project: http://git-wip-us.apache.org/repos/asf/incubator-unomi/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-unomi/commit/e0b2c359 Tree: http://git-wip-us.apache.org/repos/asf/incubator-unomi/tree/e0b2c359 Diff: http://git-wip-us.apache.org/repos/asf/incubator-unomi/diff/e0b2c359 Branch: refs/heads/feature-UNOMI-5-KARAF4-1 Commit: e0b2c359f3c6bcb6f6468177a72e8dadec3af370 Parents: c638a13 Author: Jarek Lipski <jlip...@jahia.com> Authored: Tue Feb 27 17:25:49 2018 +0100 Committer: Jarek Lipski <jlip...@jahia.com> Committed: Wed Feb 28 11:56:19 2018 +0100 ---------------------------------------------------------------------- .../unomi/itests/ProfileImportActorsIT.java | 3 - .../unomi/itests/ProfileImportRankingIT.java | 3 - .../unomi/itests/ProfileImportSurfersIT.java | 3 - .../services/services/ProfileServiceImpl.java | 76 ++++++++++++++------ 4 files changed, 53 insertions(+), 32 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/e0b2c359/itests/src/test/java/org/apache/unomi/itests/ProfileImportActorsIT.java ---------------------------------------------------------------------- diff --git a/itests/src/test/java/org/apache/unomi/itests/ProfileImportActorsIT.java b/itests/src/test/java/org/apache/unomi/itests/ProfileImportActorsIT.java index 5d9ba37..a25fbd1 100644 --- a/itests/src/test/java/org/apache/unomi/itests/ProfileImportActorsIT.java +++ b/itests/src/test/java/org/apache/unomi/itests/ProfileImportActorsIT.java @@ -68,9 +68,6 @@ public class ProfileImportActorsIT extends BaseIT { profileService.setPropertyType(propertyTypeTwitterId); profileService.setPropertyType(propertyTypeActorsGenres); - //Wait for data to be processed - Thread.sleep(10000); - PropertyType propTwitterId = profileService.getPropertyType("twitterId"); Assert.assertNotNull(propTwitterId); http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/e0b2c359/itests/src/test/java/org/apache/unomi/itests/ProfileImportRankingIT.java ---------------------------------------------------------------------- diff --git a/itests/src/test/java/org/apache/unomi/itests/ProfileImportRankingIT.java b/itests/src/test/java/org/apache/unomi/itests/ProfileImportRankingIT.java index 38c19c3..7641348 100644 --- a/itests/src/test/java/org/apache/unomi/itests/ProfileImportRankingIT.java +++ b/itests/src/test/java/org/apache/unomi/itests/ProfileImportRankingIT.java @@ -71,9 +71,6 @@ public class ProfileImportRankingIT extends BaseIT { profileService.setPropertyType(propertyTypeRank); - //Wait for data to be processed - Thread.sleep(10000); - PropertyType propUciId = profileService.getPropertyType("uciId"); Assert.assertNotNull(propUciId); http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/e0b2c359/itests/src/test/java/org/apache/unomi/itests/ProfileImportSurfersIT.java ---------------------------------------------------------------------- diff --git a/itests/src/test/java/org/apache/unomi/itests/ProfileImportSurfersIT.java b/itests/src/test/java/org/apache/unomi/itests/ProfileImportSurfersIT.java index 49cee03..d1ee1ad 100644 --- a/itests/src/test/java/org/apache/unomi/itests/ProfileImportSurfersIT.java +++ b/itests/src/test/java/org/apache/unomi/itests/ProfileImportSurfersIT.java @@ -68,9 +68,6 @@ public class ProfileImportSurfersIT extends BaseIT { profileService.setPropertyType(propertyType); - //Wait for data to be processed - Thread.sleep(10000); - PropertyType propAlive = profileService.getPropertyType("alive"); Assert.assertNotNull(propAlive); http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/e0b2c359/services/src/main/java/org/apache/unomi/services/services/ProfileServiceImpl.java ---------------------------------------------------------------------- diff --git a/services/src/main/java/org/apache/unomi/services/services/ProfileServiceImpl.java b/services/src/main/java/org/apache/unomi/services/services/ProfileServiceImpl.java index fd55496..fc1398d 100644 --- a/services/src/main/java/org/apache/unomi/services/services/ProfileServiceImpl.java +++ b/services/src/main/java/org/apache/unomi/services/services/ProfileServiceImpl.java @@ -39,6 +39,8 @@ import org.slf4j.LoggerFactory; import java.io.IOException; import java.net.URL; import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.Stream; public class ProfileServiceImpl implements ProfileService, SynchronousBundleListener { @@ -50,8 +52,21 @@ public class ProfileServiceImpl implements ProfileService, SynchronousBundleList private Map<String, List<PropertyType>> propertyTypesByTarget = new HashMap<>(); public PropertyTypes(List<PropertyType> allPropertyTypes) { - this.allPropertyTypes = allPropertyTypes; - this.updateMaps(); + this.allPropertyTypes = new ArrayList<>(allPropertyTypes); + propertyTypesById = new HashMap<>(); + propertyTypesByTags = new HashMap<>(); + propertyTypesBySystemTags = new HashMap<>(); + propertyTypesByTarget = new HashMap<>(); + for (PropertyType propertyType : allPropertyTypes) { + propertyTypesById.put(propertyType.getItemId(), propertyType); + for (String propertyTypeTag : propertyType.getMetadata().getTags()) { + updateListMap(propertyTypesByTags, propertyType, propertyTypeTag); + } + for (String propertyTypeSystemTag : propertyType.getMetadata().getSystemTags()) { + updateListMap(propertyTypesBySystemTags, propertyType, propertyTypeSystemTag); + } + updateListMap(propertyTypesByTarget, propertyType, propertyType.getTarget()); + } } public List<PropertyType> getAll() { @@ -78,25 +93,32 @@ public class ProfileServiceImpl implements ProfileService, SynchronousBundleList return propertyTypesByTarget.get(target); } - private void updateMaps() { - Map<String,PropertyType> newPropertyTypesById = new HashMap<>(); - Map<String,List<PropertyType>> newPropertyTypesByTags = new HashMap<>(); - Map<String,List<PropertyType>> newPropertyTypesBySystemTags = new HashMap<>(); - Map<String,List<PropertyType>> newPropertyTypesByTarget = new HashMap<>(); - for (PropertyType propertyType : allPropertyTypes) { - newPropertyTypesById.put(propertyType.getItemId(), propertyType); - for (String propertyTypeTag : propertyType.getMetadata().getTags()) { - updateListMap(newPropertyTypesByTags, propertyType, propertyTypeTag); - } - for (String propertyTypeSystemTag : propertyType.getMetadata().getSystemTags()) { - updateListMap(newPropertyTypesBySystemTags, propertyType, propertyTypeSystemTag); + public PropertyTypes with(PropertyType newProperty) { + return with(Collections.singletonList(newProperty)); + } + + public PropertyTypes with(List<PropertyType> newProperties) { + Map<String, PropertyType> updatedProperties = new HashMap<>(); + for (PropertyType property : newProperties) { + if (propertyTypesById.containsKey(property.getItemId())) { + updatedProperties.put(property.getItemId(), property); } - updateListMap(newPropertyTypesByTarget, propertyType, propertyType.getTarget()); } - propertyTypesById = newPropertyTypesById; - propertyTypesByTags = newPropertyTypesByTags; - propertyTypesBySystemTags = newPropertyTypesBySystemTags; - propertyTypesByTarget = newPropertyTypesByTarget; + + List<PropertyType> newPropertyTypes = Stream.concat( + allPropertyTypes.stream().map(property -> updatedProperties.getOrDefault(property.getItemId(), property)), + newProperties.stream().filter(property -> !propertyTypesById.containsKey(property.getItemId())) + ).collect(Collectors.toList()); + + return new PropertyTypes(newPropertyTypes); + } + + public PropertyTypes without(String propertyId) { + List<PropertyType> newPropertyTypes = allPropertyTypes.stream() + .filter(property -> property.getItemId().equals(propertyId)) + .collect(Collectors.toList()); + + return new PropertyTypes(newPropertyTypes); } private void updateListMap(Map<String, List<PropertyType>> listMap, PropertyType propertyType, String key) { @@ -359,17 +381,22 @@ public class ProfileServiceImpl implements ProfileService, SynchronousBundleList @Override public boolean setPropertyType(PropertyType property) { PropertyType previousProperty = persistenceService.load(property.getItemId(), PropertyType.class); + boolean result = false; if (previousProperty == null) { - return persistenceService.save(property); + result = persistenceService.save(property); + propertyTypes = propertyTypes.with(property); } else if (merge(previousProperty, property)) { - return persistenceService.save(previousProperty); + result = persistenceService.save(previousProperty); + propertyTypes = propertyTypes.with(previousProperty); } - return false; + return result; } @Override public boolean deletePropertyType(String propertyId) { - return persistenceService.remove(propertyId, PropertyType.class); + boolean result = persistenceService.remove(propertyId, PropertyType.class); + propertyTypes = propertyTypes.without(propertyId); + return result; } @Override @@ -896,6 +923,7 @@ public class ProfileServiceImpl implements ProfileService, SynchronousBundleList return; } + List<PropertyType> allPropertyTypes = new ArrayList<>(); while (predefinedPropertyTypeEntries.hasMoreElements()) { URL predefinedPropertyTypeURL = predefinedPropertyTypeEntries.nextElement(); logger.debug("Found predefined property type at " + predefinedPropertyTypeURL + ", loading... "); @@ -909,6 +937,7 @@ public class ProfileServiceImpl implements ProfileService, SynchronousBundleList propertyType.setTarget(target); persistenceService.save(propertyType); + allPropertyTypes.add(propertyType); logger.info("Predefined property type with id {} registered", propertyType.getMetadata().getId()); } else { logger.info("The predefined property type with id {} is already registered, this property type will be skipped", propertyType.getMetadata().getId()); @@ -917,6 +946,7 @@ public class ProfileServiceImpl implements ProfileService, SynchronousBundleList logger.error("Error while loading properties " + predefinedPropertyTypeURL, e); } } + propertyTypes = propertyTypes.with(allPropertyTypes); }