You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@usergrid.apache.org by to...@apache.org on 2015/02/16 19:17:52 UTC

incubator-usergrid git commit: Added direct conversion rather than looking up an instance that's already loaded in memory

Repository: incubator-usergrid
Updated Branches:
  refs/heads/two-dot-o-import 0fe3fe84e -> b9aae3cb4


Added direct conversion rather than looking up an instance that's already loaded in memory


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

Branch: refs/heads/two-dot-o-import
Commit: b9aae3cb4b0a834ef54be7e600db221ea3c408cc
Parents: 0fe3fe8
Author: Todd Nine <tn...@apigee.com>
Authored: Mon Feb 16 10:17:48 2015 -0800
Committer: Todd Nine <tn...@apigee.com>
Committed: Mon Feb 16 10:17:48 2015 -0800

----------------------------------------------------------------------
 .../corepersistence/CpEntityManager.java        | 14 +----
 .../corepersistence/CpRelationManager.java      | 40 ++++++++------
 .../corepersistence/util/CpEntityMapUtils.java  | 55 ++++++++++++++------
 3 files changed, 64 insertions(+), 45 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/b9aae3cb/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 a0a9283..9c7eeb9 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
@@ -56,6 +56,8 @@ import org.apache.usergrid.persistence.RelationManager;
 import org.apache.usergrid.persistence.Results;
 import org.apache.usergrid.persistence.Schema;
 import org.apache.usergrid.persistence.SimpleEntityRef;
+
+import static org.apache.usergrid.corepersistence.util.CpEntityMapUtils.entityToCpEntity;
 import static org.apache.usergrid.persistence.SimpleEntityRef.getUuid;
 
 import org.apache.usergrid.persistence.SimpleRoleRef;
@@ -2754,19 +2756,7 @@ public class CpEntityManager implements EntityManager {
     }
 
 
-    public static org.apache.usergrid.persistence.model.entity.Entity entityToCpEntity( Entity entity, UUID importId ) {
-
-        UUID uuid = importId != null ? importId : entity.getUuid();
-
-        org.apache.usergrid.persistence.model.entity.Entity cpEntity =
-                new org.apache.usergrid.persistence.model.entity.Entity( new SimpleId( uuid, entity.getType() ) );
-
-        cpEntity = CpEntityMapUtils.fromMap( cpEntity, entity.getProperties(), entity.getType(), true );
-
-        cpEntity = CpEntityMapUtils.fromMap( cpEntity, entity.getDynamicProperties(), entity.getType(), true );
 
-        return cpEntity;
-    }
 
 
     @Override

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/b9aae3cb/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpRelationManager.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpRelationManager.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpRelationManager.java
index 5039a41..b7a82fd 100644
--- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpRelationManager.java
+++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpRelationManager.java
@@ -118,6 +118,7 @@ import rx.functions.Func1;
 import static java.util.Arrays.asList;
 
 import static me.prettyprint.hector.api.factory.HFactory.createMutator;
+import static org.apache.usergrid.corepersistence.util.CpEntityMapUtils.entityToCpEntity;
 import static org.apache.usergrid.corepersistence.util.CpNamingUtils.getCollectionScopeNameFromEntityType;
 import static org.apache.usergrid.persistence.Schema.COLLECTION_ROLES;
 import static org.apache.usergrid.persistence.Schema.DICTIONARY_CONNECTED_ENTITIES;
@@ -233,8 +234,13 @@ public class CpRelationManager implements RelationManager {
         }
 
         Id entityId = new SimpleId( headEntity.getUuid(), headEntity.getType() );
-        this.cpHeadEntity = ((CpEntityManager)em).load(
-            new CpEntityManager.EntityScope( headEntityScope, entityId));
+
+        if(headEntity instanceof Entity){
+            cpHeadEntity = entityToCpEntity( (Entity)headEntity, headEntity.getUuid() );
+        }else {
+            this.cpHeadEntity =
+                ( ( CpEntityManager ) em ).load( new CpEntityManager.EntityScope( headEntityScope, entityId ) );
+        }
 
         // commented out because it is possible that CP entity has not been created yet
         Assert.notNull( cpHeadEntity, "cpHeadEntity cannot be null" );
@@ -409,14 +415,14 @@ public class CpRelationManager implements RelationManager {
                         if ( CpNamingUtils.isCollectionEdgeType( edge.getType() ) ) {
 
                             String collName = CpNamingUtils.getCollectionName( edge.getType() );
-                            indexScope = new IndexScopeImpl( 
+                            indexScope = new IndexScopeImpl(
                                 new SimpleId( sourceEntity.getUuid(), sourceEntity.getType()),
                                 CpNamingUtils.getCollectionScopeNameFromCollectionName( collName ));
                         }
                         else {
 
                             String connName = CpNamingUtils.getConnectionType( edge.getType() );
-                            indexScope = new IndexScopeImpl( 
+                            indexScope = new IndexScopeImpl(
                                 new SimpleId( sourceEntity.getUuid(), sourceEntity.getType() ),
                                 CpNamingUtils.getConnectionScopeName( connName ) );
                         }
@@ -600,14 +606,14 @@ public class CpRelationManager implements RelationManager {
     }
 
 
-    public Entity addToCollection( String collName, EntityRef itemRef, boolean connectBack ) 
+    public Entity addToCollection( String collName, EntityRef itemRef, boolean connectBack )
             throws Exception {
 
         CollectionScope memberScope = getCollectionScopeNameFromEntityType(
                 applicationScope.getApplication(), itemRef.getType());
 
-        Id entityId = new SimpleId( itemRef.getUuid(), itemRef.getType() ); 
-        org.apache.usergrid.persistence.model.entity.Entity memberEntity = 
+        Id entityId = new SimpleId( itemRef.getUuid(), itemRef.getType() );
+        org.apache.usergrid.persistence.model.entity.Entity memberEntity =
             ((CpEntityManager)em).load( new CpEntityManager.EntityScope( memberScope, entityId));
 
         return addToCollection(collName, itemRef, memberEntity, connectBack);
@@ -697,7 +703,7 @@ public class CpRelationManager implements RelationManager {
         //            headEntityScope.getName()});
 
         if ( connectBack && collection != null && collection.getLinkedCollection() != null ) {
-            getRelationManager( itemEntity ).addToCollection( 
+            getRelationManager( itemEntity ).addToCollection(
                     collection.getLinkedCollection(), headEntity, cpHeadEntity, false );
             getRelationManager( itemEntity ).addToCollection(
                     collection.getLinkedCollection(), headEntity, false );
@@ -819,7 +825,7 @@ public class CpRelationManager implements RelationManager {
 
         batch.deindex( indexScope, memberEntity );
 
-        // remove collection from item index 
+        // remove collection from item index
         IndexScope itemScope = new IndexScopeImpl(
             memberEntity.getId(),
             CpNamingUtils.getCollectionScopeNameFromCollectionName(
@@ -830,7 +836,7 @@ public class CpRelationManager implements RelationManager {
 
         batch.execute();
 
-        // remove edge from collection to item 
+        // remove edge from collection to item
         GraphManager gm = managerCache.getGraphManager( applicationScope );
         Edge collectionToItemEdge = new SimpleEdge(
                 cpHeadEntity.getId(),
@@ -938,8 +944,8 @@ public class CpRelationManager implements RelationManager {
         query.setEntityType( collection.getType() );
         query = adjustQuery( query );
 
-        // Because of possible stale entities, which are filtered out by buildResults(), 
-        // we loop until the we've got enough results to satisfy the query limit. 
+        // Because of possible stale entities, which are filtered out by buildResults(),
+        // we loop until the we've got enough results to satisfy the query limit.
 
         int maxQueries = 10; // max re-queries to satisfy query limit
 
@@ -1336,7 +1342,7 @@ public class CpRelationManager implements RelationManager {
             headEntity = em.validate( headEntity );
 
 
-            IndexScope indexScope = new IndexScopeImpl( 
+            IndexScope indexScope = new IndexScopeImpl(
                     cpHeadEntity.getId(), CpNamingUtils.getConnectionScopeName( connectionType ) );
 
             final SearchTypes searchTypes = SearchTypes.fromNullableTypes( connectedEntityType );
@@ -1458,7 +1464,7 @@ public class CpRelationManager implements RelationManager {
 
                 Identifier ident = query.getSingleIdentifier();
 
-                // an email was specified.  An edge case that only applies to users.  
+                // an email was specified.  An edge case that only applies to users.
                 // This is fulgy to put here, but required.
                 if ( query.getEntityType().equals( User.ENTITY_TYPE ) && ident.isEmail() ) {
 
@@ -1568,7 +1574,7 @@ public class CpRelationManager implements RelationManager {
      * @param crs Candidates to be considered for results
      * @param collName Name of collection or null if querying all types
      */
-    private Results buildResults( final IndexScope indexScope, final Query query, 
+    private Results buildResults( final IndexScope indexScope, final Query query,
             final CandidateResults crs, final String collName ) {
 
         logger.debug( "buildResults() for {} from {} candidates", collName, crs.size() );
@@ -1605,7 +1611,7 @@ public class CpRelationManager implements RelationManager {
         IndexUpdate indexUpdate = batchStartIndexUpdate( batch, entity, setName, elementValue,
                 timestampUuid, true, true, removeFromSet, false );
 
-        // Update collections 
+        // Update collections
 
         Map<String, Set<CollectionInfo>> containers =
                 getDefaultSchema().getContainersIndexingDictionary( entity.getType(), setName );
@@ -1950,7 +1956,7 @@ public class CpRelationManager implements RelationManager {
 
                 final ConnectedEntityRef sourceEntity = ( ConnectedEntityRef ) connection;
 
-                //we need to create a connection ref from the source entity (found via reverse edge) 
+                //we need to create a connection ref from the source entity (found via reverse edge)
                 // to the entity we're about to update.  This is the index that needs updated
                 final ConnectionRefImpl connectionRef =
                         new ConnectionRefImpl( sourceEntity, connectionType, indexUpdate.getEntity() );

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/b9aae3cb/stack/core/src/main/java/org/apache/usergrid/corepersistence/util/CpEntityMapUtils.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/util/CpEntityMapUtils.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/util/CpEntityMapUtils.java
index 344bcd3..50993c4 100644
--- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/util/CpEntityMapUtils.java
+++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/util/CpEntityMapUtils.java
@@ -32,6 +32,7 @@ import java.util.UUID;
 
 import org.apache.usergrid.persistence.Schema;
 import org.apache.usergrid.persistence.model.entity.Entity;
+import org.apache.usergrid.persistence.model.entity.SimpleId;
 import org.apache.usergrid.persistence.model.field.ArrayField;
 import org.apache.usergrid.persistence.model.field.BooleanField;
 import org.apache.usergrid.persistence.model.field.ByteArrayField;
@@ -64,11 +65,33 @@ public class CpEntityMapUtils {
 
     public static ObjectMapper objectMapper = new ObjectMapper(  );
 
+
+    /**
+     * Convert a usergrid 1.0 entity into a usergrid 2.0 entity
+     */
+    public static org.apache.usergrid.persistence.model.entity.Entity entityToCpEntity(
+        org.apache.usergrid.persistence.Entity entity, UUID importId ) {
+
+        UUID uuid = importId != null ? importId : entity.getUuid();
+
+        org.apache.usergrid.persistence.model.entity.Entity cpEntity =
+            new org.apache.usergrid.persistence.model.entity.Entity( new SimpleId( uuid, entity.getType() ) );
+
+        cpEntity = CpEntityMapUtils.fromMap( cpEntity, entity.getProperties(), entity.getType(), true );
+
+        cpEntity = CpEntityMapUtils.fromMap( cpEntity, entity.getDynamicProperties(), entity.getType(), true );
+
+        return cpEntity;
+    }
+
+
     public static Entity fromMap( Map<String, Object> map, String entityType, boolean topLevel ) {
         return fromMap( null, map, entityType, topLevel );
     }
 
-    public static Entity fromMap( 
+
+
+    public static Entity fromMap(
             Entity entity, Map<String, Object> map, String entityType, boolean topLevel ) {
 
         if ( entity == null ) {
@@ -89,7 +112,7 @@ public class CpEntityMapUtils {
 
             } else if ( value instanceof Boolean ) {
                 entity.setField( new BooleanField( fieldName, (Boolean)value, unique && topLevel ));
-                        
+
             } else if ( value instanceof Integer ) {
                 entity.setField( new IntegerField( fieldName, (Integer)value, unique && topLevel ));
 
@@ -98,13 +121,13 @@ public class CpEntityMapUtils {
 
 		    } else if ( value instanceof Float ) {
                 entity.setField( new FloatField( fieldName, (Float)value, unique && topLevel ));
-				
+
             } else if ( value instanceof Long ) {
                 entity.setField( new LongField( fieldName, (Long)value, unique && topLevel ));
 
             } else if ( value instanceof List) {
-                entity.setField( listToListField( fieldName, (List)value, entityType ));  
-            
+                entity.setField( listToListField( fieldName, (List)value, entityType ));
+
             } else if ( value instanceof UUID) {
                 entity.setField( new UUIDField( fieldName, (UUID)value, unique && topLevel ));
 
@@ -113,7 +136,7 @@ public class CpEntityMapUtils {
 
             } else if ( value instanceof Enum ) {
                 entity.setField( new StringField( fieldName, value.toString(), unique && topLevel ));
-	
+
 			} else if ( value != null ) {
                 byte[] valueSerialized;
                 try {
@@ -195,7 +218,7 @@ public class CpEntityMapUtils {
         }
     }
 
-    
+
     private static ListField listToListField( String fieldName, List list, String entityType ) {
 
         if (list.isEmpty()) {
@@ -209,13 +232,13 @@ public class CpEntityMapUtils {
 
         } else if ( sample instanceof List ) {
             return new ListField<List>( fieldName, processListForField( list, entityType ));
-            
+
         } else if ( sample instanceof String ) {
             return new ListField<String>( fieldName, (List<String>)list );
-                    
+
         } else if ( sample instanceof Boolean ) {
             return new ListField<Boolean>( fieldName, (List<Boolean>)list );
-                    
+
         } else if ( sample instanceof Integer ) {
             return new ListField<Integer>( fieldName, (List<Integer>)list );
 
@@ -230,7 +253,7 @@ public class CpEntityMapUtils {
         }
     }
 
-    
+
     private static List processListForField( List list, String entityType ) {
         if ( list.isEmpty() ) {
             return list;
@@ -246,10 +269,10 @@ public class CpEntityMapUtils {
 
         } else if ( sample instanceof List ) {
             return processListForField( list, entityType ); // recursion
-            
-        } else { 
+
+        } else {
             return list;
-        } 
+        }
     }
 
 
@@ -285,7 +308,7 @@ public class CpEntityMapUtils {
                 LocationField locField = (LocationField) f;
                 Map<String, Object> locMap = new HashMap<String, Object>();
 
-                // field names lat and lon trigger ElasticSearch geo location 
+                // field names lat and lon trigger ElasticSearch geo location
                 locMap.put("lat", locField.getValue().getLatitude());
                 locMap.put("lon", locField.getValue().getLongitude());
                 entityMap.put( field.getName(), field.getValue());
@@ -311,7 +334,7 @@ public class CpEntityMapUtils {
         return entityMap;
     }
 
-    
+
     private static Collection processCollectionForMap(Collection c) {
         if (c.isEmpty()) {
             return c;