You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@usergrid.apache.org by sn...@apache.org on 2014/10/07 15:28:05 UTC

[4/5] git commit: Use new Core Persistence Map Module to look up Entity Types by UUID (from Cassandra) instead of relying entirely on the Query Index module (and ElasticSearch).

Use new Core Persistence Map Module to look up Entity Types by UUID (from Cassandra) instead of relying entirely on the Query Index module (and ElasticSearch).


Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/5ad79e30
Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/5ad79e30
Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/5ad79e30

Branch: refs/heads/two-dot-o
Commit: 5ad79e304d31b4a2f30014f7bab9ca5a6c62fcc2
Parents: 79bebe6
Author: Dave Johnson <dm...@apigee.com>
Authored: Tue Oct 7 09:19:16 2014 -0400
Committer: Dave Johnson <dm...@apigee.com>
Committed: Tue Oct 7 09:19:16 2014 -0400

----------------------------------------------------------------------
 stack/core/pom.xml                              |  7 +++
 .../corepersistence/CpEntityManager.java        | 45 +++++++++++++++-----
 .../corepersistence/CpEntityManagerFactory.java |  5 ++-
 .../corepersistence/CpManagerCache.java         | 29 +++++++++++--
 .../usergrid/corepersistence/GuiceModule.java   | 10 ++---
 5 files changed, 76 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/5ad79e30/stack/core/pom.xml
----------------------------------------------------------------------
diff --git a/stack/core/pom.xml b/stack/core/pom.xml
index 6cd5dac..ea37fce 100644
--- a/stack/core/pom.xml
+++ b/stack/core/pom.xml
@@ -526,6 +526,13 @@
 	    <type>jar</type>
     </dependency>
 
+    <dependency>
+	    <groupId>org.apache.usergrid</groupId>
+	    <artifactId>map</artifactId>
+	    <version>2.0.0-SNAPSHOT</version>
+	    <type>jar</type>
+    </dependency>
+
     <!--<dependency>-->
       <!--<artifactId>lucene-core</artifactId>-->
       <!--<groupId>org.apache.lucene</groupId>-->

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/5ad79e30/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManager.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManager.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManager.java
index d7f3a3b..1a8f17c 100644
--- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManager.java
+++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManager.java
@@ -133,6 +133,9 @@ import org.apache.usergrid.persistence.index.query.Identifier;
 import org.apache.usergrid.persistence.index.query.Query;
 import org.apache.usergrid.persistence.index.query.Query.Level;
 import static org.apache.usergrid.persistence.index.query.Query.Level.REFS;
+import org.apache.usergrid.persistence.map.MapManager;
+import org.apache.usergrid.persistence.map.MapScope;
+import org.apache.usergrid.persistence.map.impl.MapScopeImpl;
 import org.apache.usergrid.persistence.model.entity.Id;
 import org.apache.usergrid.persistence.model.entity.SimpleId;
 import org.apache.usergrid.persistence.model.field.Field;
@@ -181,6 +184,8 @@ public class CpEntityManager implements EntityManager {
 
     private boolean skipAggregateCounters;
 
+    private String TYPES_BY_UUID_MAP = "zzz_typesbyuuid_zzz";
+
 
     public CpEntityManager() {}
 
@@ -482,8 +487,8 @@ public class CpEntityManager implements EntityManager {
 //                ecm.load( entityId ).toBlockingObservable().last();
 
 
-        org.apache.usergrid.persistence.model.entity.Entity cpEntity = new org.apache.usergrid.persistence.model
-                .entity.Entity( entityId );
+        org.apache.usergrid.persistence.model.entity.Entity cpEntity = 
+                new org.apache.usergrid.persistence.model.entity.Entity( entityId );
 
         cpEntity = CpEntityMapUtils.fromMap(
                 cpEntity, entity.getProperties(), entity.getType(), true );
@@ -2187,13 +2192,30 @@ public class CpEntityManager implements EntityManager {
 
 
     @Override
-    public Entity get( UUID id ) throws Exception {
+    public Entity get( UUID uuid ) throws Exception {
+
+        Id mapOwner = new SimpleId( applicationId, TYPE_APPLICATION );
+        MapScope ms = new MapScopeImpl( mapOwner, TYPES_BY_UUID_MAP );
+        MapManager mm = managerCache.getMapManager( ms );
+        String entityType = mm.getString(uuid.toString() );
+
+        final Entity entity;
 
-        Query q = Query.fromQL("select * where " + PROPERTY_UUID + " = '" + id.toString() + "'");
-        q.setResultsLevel( Level.ALL_PROPERTIES );
-        Results r = getRelationManager( getApplication() ).searchConnectedEntities( q );
+        if ( entityType == null ) {
 
-        return r.getEntity();
+            Query q = Query.fromQL(
+                "select * where " + PROPERTY_UUID + " = '" + uuid.toString() + "'");
+            q.setResultsLevel( Level.ALL_PROPERTIES );
+            Results r = getRelationManager( getApplication() ).searchConnectedEntities( q );
+            entity = r.getEntity();
+
+            mm.putString(uuid.toString(), entity.getType() );
+        
+        } else { 
+            entity = get(new SimpleEntityRef( entityType, uuid ));
+        }
+
+        return entity;
     } 
 
 
@@ -2505,7 +2527,8 @@ public class CpEntityManager implements EntityManager {
 //            if ( entity.getType().equals("group")) {
 //                logger.debug("Writing Group");
 //                for ( Field field : cpEntity.getFields() ) {
-//                    logger.debug("   Writing Group name={} value={}", field.getName(), field.getValue() );
+//                    logger.debug(
+//                        "   Writing Group name={} value={}", field.getName(), field.getValue() );
 //                }
 //            }
         }
@@ -2839,7 +2862,7 @@ public class CpEntityManager implements EntityManager {
                             CollectionScope collScope = new CollectionScopeImpl(
                                 applicationScope.getApplication(),
                                 applicationScope.getApplication(),
-                                    CpNamingUtils.getCollectionScopeNameFromEntityType( entity.getType() ));
+                                CpNamingUtils.getCollectionScopeNameFromEntityType( entity.getType()));
                             EntityCollectionManager collMgr = 
                                 managerCache.getEntityCollectionManager(collScope);
 
@@ -2905,7 +2928,7 @@ public class CpEntityManager implements EntityManager {
                             CollectionScope sourceScope = new CollectionScopeImpl(
                                 applicationScope.getApplication(),
                                 applicationScope.getApplication(),
-                                    CpNamingUtils.getCollectionScopeNameFromEntityType( sourceEntityType ));
+                                CpNamingUtils.getCollectionScopeNameFromEntityType( sourceEntityType ));
                             EntityCollectionManager sourceEcm = 
                                 managerCache.getEntityCollectionManager(sourceScope);
 
@@ -2928,7 +2951,7 @@ public class CpEntityManager implements EntityManager {
                             CollectionScope targetScope = new CollectionScopeImpl(
                                 applicationScope.getApplication(),
                                 applicationScope.getApplication(),
-                                    CpNamingUtils.getCollectionScopeNameFromEntityType( targetEntityType ));
+                                CpNamingUtils.getCollectionScopeNameFromEntityType( targetEntityType ));
                             EntityCollectionManager targetEcm = 
                                 managerCache.getEntityCollectionManager(targetScope);
 

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/5ad79e30/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManagerFactory.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManagerFactory.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManagerFactory.java
index 26f4234..f44eac3 100644
--- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManagerFactory.java
+++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManagerFactory.java
@@ -53,6 +53,7 @@ import org.apache.usergrid.persistence.graph.SearchByEdgeType;
 import org.apache.usergrid.persistence.graph.impl.SimpleSearchByEdgeType;
 import org.apache.usergrid.persistence.index.EntityIndexFactory;
 import org.apache.usergrid.persistence.index.query.Query;
+import org.apache.usergrid.persistence.map.MapManagerFactory;
 import org.apache.usergrid.persistence.model.entity.Id;
 import org.apache.usergrid.persistence.model.entity.SimpleId;
 import org.apache.usergrid.utils.UUIDUtils;
@@ -154,12 +155,14 @@ public class CpEntityManagerFactory implements EntityManagerFactory, Application
             EntityCollectionManagerFactory ecmf;
             EntityIndexFactory eif;
             GraphManagerFactory gmf;
+            MapManagerFactory mmf;
 
             ecmf = injector.getInstance( EntityCollectionManagerFactory.class );
             eif = injector.getInstance( EntityIndexFactory.class );
             gmf = injector.getInstance( GraphManagerFactory.class );
+            mmf = injector.getInstance( MapManagerFactory.class );
 
-            managerCache = new CpManagerCache( ecmf, eif, gmf );
+            managerCache = new CpManagerCache( ecmf, eif, gmf, mmf );
         }
         return managerCache;
     }

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/5ad79e30/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpManagerCache.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpManagerCache.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpManagerCache.java
index 0e7c084..d10025c 100644
--- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpManagerCache.java
+++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpManagerCache.java
@@ -23,7 +23,9 @@ import org.apache.usergrid.persistence.graph.GraphManager;
 import org.apache.usergrid.persistence.graph.GraphManagerFactory;
 import org.apache.usergrid.persistence.index.EntityIndex;
 import org.apache.usergrid.persistence.index.EntityIndexFactory;
-import org.apache.usergrid.persistence.index.IndexScope;
+import org.apache.usergrid.persistence.map.MapManager;
+import org.apache.usergrid.persistence.map.MapManagerFactory;
+import org.apache.usergrid.persistence.map.MapScope;
 import org.apache.usergrid.utils.LRUCache2;
 
 class CpManagerCache {
@@ -31,8 +33,10 @@ class CpManagerCache {
     private final EntityCollectionManagerFactory ecmf;
     private final EntityIndexFactory eif;
     private final GraphManagerFactory gmf;
+    private final MapManagerFactory mmf;
 
     // TODO: consider making these cache sizes and timeouts configurable
+    // TODO: replace with Guava cache
     private final LRUCache2<CollectionScope, EntityCollectionManager> ecmCache
             = new LRUCache2<CollectionScope, EntityCollectionManager>(50, 1 * 60 * 60 * 1000);
 
@@ -42,12 +46,20 @@ class CpManagerCache {
     private final LRUCache2<ApplicationScope, GraphManager> gmCache
             = new LRUCache2<ApplicationScope, GraphManager>(50, 1 * 60 * 60 * 1000);
 
+    private final LRUCache2<MapScope, MapManager> mmCache
+            = new LRUCache2<MapScope, MapManager>(50, 1 * 60 * 60 * 1000);
+
+
     public CpManagerCache(
-            EntityCollectionManagerFactory ecmf, EntityIndexFactory eif, GraphManagerFactory gmf) {
+            EntityCollectionManagerFactory ecmf, 
+            EntityIndexFactory eif, 
+            GraphManagerFactory gmf,
+            MapManagerFactory mmf) {
+
         this.ecmf = ecmf;
         this.eif = eif;
         this.gmf = gmf;
-
+        this.mmf = mmf;
     }
 
     public EntityCollectionManager getEntityCollectionManager(CollectionScope scope) {
@@ -83,6 +95,17 @@ class CpManagerCache {
         return gm;
     }
 
+    public MapManager getMapManager( MapScope mapScope) {
+
+        MapManager mm = mmCache.get(mapScope);
+
+        if (mm == null) {
+            mm = mmf.createMapManager(mapScope);
+            mmCache.put(mapScope, mm);
+        }
+        return mm;
+    }
+
     void flush() {
         gmCache.purge();
         ecmCache.purge();

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/5ad79e30/stack/core/src/main/java/org/apache/usergrid/corepersistence/GuiceModule.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/GuiceModule.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/GuiceModule.java
index 1f3d615..f85c508 100644
--- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/GuiceModule.java
+++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/GuiceModule.java
@@ -17,15 +17,14 @@
 package org.apache.usergrid.corepersistence;
 
 
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
+import com.google.inject.AbstractModule;
 import org.apache.usergrid.persistence.collection.guice.CollectionModule;
 import org.apache.usergrid.persistence.core.guice.CommonModule;
 import org.apache.usergrid.persistence.graph.guice.GraphModule;
 import org.apache.usergrid.persistence.index.guice.IndexModule;
-
-import com.google.inject.AbstractModule;
+import org.apache.usergrid.persistence.map.guice.MapModule;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 
 /**
@@ -41,6 +40,7 @@ public class GuiceModule  extends AbstractModule {
         install(new CollectionModule());
         install(new GraphModule());
         install(new IndexModule());
+        install(new MapModule());
 
         bind(CpEntityDeleteListener.class).asEagerSingleton();
         bind(CpEntityIndexDeleteListener.class).asEagerSingleton();