UNOMI-168 Improve comments on PropertyTypes cache
Project: http://git-wip-us.apache.org/repos/asf/incubator-unomi/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-unomi/commit/5c0cc738 Tree: http://git-wip-us.apache.org/repos/asf/incubator-unomi/tree/5c0cc738 Diff: http://git-wip-us.apache.org/repos/asf/incubator-unomi/diff/5c0cc738 Branch: refs/heads/master Commit: 5c0cc7385b8df102c8f82366173aa78f5ff5ea62 Parents: 6728e50 Author: Jarek Lipski <jlip...@jahia.com> Authored: Wed Feb 28 14:48:43 2018 +0100 Committer: Jarek Lipski <jlip...@jahia.com> Committed: Wed Feb 28 14:48:43 2018 +0100 ---------------------------------------------------------------------- .../services/services/ProfileServiceImpl.java | 22 +++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/5c0cc738/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 fc1398d..a37ed7a 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 @@ -44,6 +44,11 @@ import java.util.stream.Stream; public class ProfileServiceImpl implements ProfileService, SynchronousBundleListener { + /** + * This class is responsible for storing property types and permits optimized access to them. + * In order to assure data consistency, thread-safety and performance, this class is immutable and every operation on + * property types requires creating a new instance (copy-on-write). + */ private static class PropertyTypes { private List<PropertyType> allPropertyTypes; private Map<String, PropertyType> propertyTypesById = new HashMap<>(); @@ -97,6 +102,12 @@ public class ProfileServiceImpl implements ProfileService, SynchronousBundleList return with(Collections.singletonList(newProperty)); } + /** + * Creates a new instance of this class containing given property types. + * If property types with the same ID existed before, they will be replaced by the new ones. + * @param newProperties list of property types to change + * @return new instance + */ public PropertyTypes with(List<PropertyType> newProperties) { Map<String, PropertyType> updatedProperties = new HashMap<>(); for (PropertyType property : newProperties) { @@ -113,6 +124,11 @@ public class ProfileServiceImpl implements ProfileService, SynchronousBundleList return new PropertyTypes(newPropertyTypes); } + /** + * Creates a new instance of this class containing all property types except the one with given ID. + * @param propertyId ID of the property to delete + * @return new instance + */ public PropertyTypes without(String propertyId) { List<PropertyType> newPropertyTypes = allPropertyTypes.stream() .filter(property -> property.getItemId().equals(propertyId)) @@ -923,7 +939,7 @@ public class ProfileServiceImpl implements ProfileService, SynchronousBundleList return; } - List<PropertyType> allPropertyTypes = new ArrayList<>(); + List<PropertyType> bundlePropertyTypes = new ArrayList<>(); while (predefinedPropertyTypeEntries.hasMoreElements()) { URL predefinedPropertyTypeURL = predefinedPropertyTypeEntries.nextElement(); logger.debug("Found predefined property type at " + predefinedPropertyTypeURL + ", loading... "); @@ -937,7 +953,7 @@ public class ProfileServiceImpl implements ProfileService, SynchronousBundleList propertyType.setTarget(target); persistenceService.save(propertyType); - allPropertyTypes.add(propertyType); + bundlePropertyTypes.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()); @@ -946,7 +962,7 @@ public class ProfileServiceImpl implements ProfileService, SynchronousBundleList logger.error("Error while loading properties " + predefinedPropertyTypeURL, e); } } - propertyTypes = propertyTypes.with(allPropertyTypes); + propertyTypes = propertyTypes.with(bundlePropertyTypes); }