You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@unomi.apache.org by sh...@apache.org on 2018/03/06 14:55:29 UTC

[02/26] incubator-unomi git commit: UNOMI-168 Refactor PropertyType in-memory cache

UNOMI-168 Refactor PropertyType in-memory 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/c638a131
Tree: http://git-wip-us.apache.org/repos/asf/incubator-unomi/tree/c638a131
Diff: http://git-wip-us.apache.org/repos/asf/incubator-unomi/diff/c638a131

Branch: refs/heads/feature-UNOMI-5-KARAF4-1
Commit: c638a13139fcab60867141384d4b9ffb2a0d0c72
Parents: d8bbbfa
Author: Jarek Lipski <jl...@jahia.com>
Authored: Tue Feb 27 16:45:57 2018 +0100
Committer: Jarek Lipski <jl...@jahia.com>
Committed: Wed Feb 28 11:56:19 2018 +0100

----------------------------------------------------------------------
 .../services/services/ProfileServiceImpl.java   | 130 ++++++++++++-------
 1 file changed, 82 insertions(+), 48 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/c638a131/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 895af37..fd55496 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
@@ -42,6 +42,74 @@ import java.util.*;
 
 public class ProfileServiceImpl implements ProfileService, SynchronousBundleListener {
 
+    private static class PropertyTypes {
+        private List<PropertyType> allPropertyTypes;
+        private Map<String, PropertyType> propertyTypesById = new HashMap<>();
+        private Map<String, List<PropertyType>> propertyTypesByTags = new HashMap<>();
+        private Map<String, List<PropertyType>> propertyTypesBySystemTags = new HashMap<>();
+        private Map<String, List<PropertyType>> propertyTypesByTarget = new HashMap<>();
+
+        public PropertyTypes(List<PropertyType> allPropertyTypes) {
+            this.allPropertyTypes = allPropertyTypes;
+            this.updateMaps();
+        }
+
+        public List<PropertyType> getAll() {
+            return allPropertyTypes;
+        }
+
+        public PropertyType get(String propertyId) {
+            return propertyTypesById.get(propertyId);
+        }
+
+        public Map<String, List<PropertyType>> getAllByTarget() {
+            return propertyTypesByTarget;
+        }
+
+        public List<PropertyType> getByTag(String tag) {
+            return propertyTypesByTags.get(tag);
+        }
+
+        public List<PropertyType> getBySystemTag(String systemTag) {
+            return propertyTypesBySystemTags.get(systemTag);
+        }
+
+        public List<PropertyType> getByTarget(String target) {
+            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);
+                }
+                updateListMap(newPropertyTypesByTarget, propertyType, propertyType.getTarget());
+            }
+            propertyTypesById = newPropertyTypesById;
+            propertyTypesByTags = newPropertyTypesByTags;
+            propertyTypesBySystemTags = newPropertyTypesBySystemTags;
+            propertyTypesByTarget = newPropertyTypesByTarget;
+        }
+
+        private void updateListMap(Map<String, List<PropertyType>> listMap, PropertyType propertyType, String key) {
+            List<PropertyType> propertyTypes = listMap.get(key);
+            if (propertyTypes == null) {
+                propertyTypes = new ArrayList<>();
+            }
+            propertyTypes.add(propertyType);
+            listMap.put(key, propertyTypes);
+        }
+
+    }
+
     private static final Logger logger = LoggerFactory.getLogger(ProfileServiceImpl.class.getName());
 
     private BundleContext bundleContext;
@@ -64,11 +132,7 @@ public class ProfileServiceImpl implements ProfileService, SynchronousBundleList
 
     private Timer purgeProfileTimer;
 
-    private List<PropertyType> allPropertyTypes;
-    private Map<String,PropertyType> propertyTypesById = new HashMap<>();
-    private Map<String,List<PropertyType>> propertyTypesByTags = new HashMap<>();
-    private Map<String,List<PropertyType>> propertyTypesBySystemTags = new HashMap<>();
-    private Map<String,List<PropertyType>> propertyTypesByTarget = new HashMap<>();
+    private PropertyTypes propertyTypes;
 
     private boolean forceRefreshOnSave = false;
 
@@ -164,39 +228,12 @@ public class ProfileServiceImpl implements ProfileService, SynchronousBundleList
 
     private void loadPropertyTypesFromPersistence() {
         try {
-            allPropertyTypes = persistenceService.getAllItems(PropertyType.class, 0, -1, "rank").getList();
-            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);
-                }
-                updateListMap(newPropertyTypesByTarget, propertyType, propertyType.getTarget());
-            }
-            propertyTypesById = newPropertyTypesById;
-            propertyTypesByTags = newPropertyTypesByTags;
-            propertyTypesBySystemTags = newPropertyTypesBySystemTags;
-            propertyTypesByTarget = newPropertyTypesByTarget;
+            this.propertyTypes = new PropertyTypes(persistenceService.getAllItems(PropertyType.class, 0, -1, "rank").getList());
         } catch (Exception e) {
             logger.error("Error loading property types from persistence service", e);
         }
     }
 
-    private void updateListMap(Map<String, List<PropertyType>> listMap, PropertyType propertyType, String key) {
-        List<PropertyType> propertyTypes = listMap.get(key);
-        if (propertyTypes == null) {
-            propertyTypes = new ArrayList<>();
-        }
-        propertyTypes.add(propertyType);
-        listMap.put(key, propertyTypes);
-    }
-
     private void cancelPropertyTypeLoad() {
         if (allPropertyTypesTimer != null) {
             allPropertyTypesTimer.cancel();
@@ -730,40 +767,37 @@ public class ProfileServiceImpl implements ProfileService, SynchronousBundleList
         if (target == null) {
             return null;
         }
-        List<PropertyType> propertyTypes = propertyTypesByTarget.get(target);
-        if (propertyTypes == null) {
+        Collection<PropertyType> result = propertyTypes.getByTarget(target);
+        if (result == null) {
             return new ArrayList<>();
         }
-        return propertyTypes;
+        return result;
     }
 
     public Map<String, Collection<PropertyType>> getTargetPropertyTypes() {
-        if (propertyTypesByTarget == null) {
-            return new HashMap<>();
-        }
-        return new HashMap<>(propertyTypesByTarget);
+        return new HashMap<>(propertyTypes.getAllByTarget());
     }
 
     public Set<PropertyType> getPropertyTypeByTag(String tag) {
         if (tag == null) {
             return null;
         }
-        List<PropertyType> propertyTypes = propertyTypesByTags.get(tag);
-        if (propertyTypes == null) {
+        List<PropertyType> result = propertyTypes.getByTag(tag);
+        if (result == null) {
             return new LinkedHashSet<>();
         }
-        return new LinkedHashSet<>(propertyTypes);
+        return new LinkedHashSet<>(result);
     }
 
     public Set<PropertyType> getPropertyTypeBySystemTag(String tag) {
         if (tag == null) {
             return null;
         }
-        List<PropertyType> propertyTypes = propertyTypesBySystemTags.get(tag);
-        if (propertyTypes == null) {
+        List<PropertyType> result = propertyTypes.getBySystemTag(tag);
+        if (result == null) {
             return new LinkedHashSet<>();
         }
-        return new LinkedHashSet<>(propertyTypes);
+        return new LinkedHashSet<>(result);
     }
 
     public Collection<PropertyType> getPropertyTypeByMapping(String propertyName) {
@@ -780,7 +814,7 @@ public class ProfileServiceImpl implements ProfileService, SynchronousBundleList
             }
         });
 
-        for (PropertyType propertyType : allPropertyTypes) {
+        for (PropertyType propertyType : propertyTypes.getAll()) {
             if (propertyType.getAutomaticMappingsFrom() != null && propertyType.getAutomaticMappingsFrom().contains(propertyName)) {
                 l.add(propertyType);
             }
@@ -789,7 +823,7 @@ public class ProfileServiceImpl implements ProfileService, SynchronousBundleList
     }
 
     public PropertyType getPropertyType(String id) {
-        return propertyTypesById.get(id);
+        return propertyTypes.get(id);
     }
 
     public PartialList<Session> getPersonaSessions(String personaId, int offset, int size, String sortBy) {