usergrid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From toddn...@apache.org
Subject [32/50] incubator-usergrid git commit: fixed migration logic
Date Mon, 13 Apr 2015 17:45:57 GMT
fixed migration logic

fixed migration logic

fixed migration logic


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

Branch: refs/heads/USERGRID-536
Commit: a62091dfb0e17ca535b93a48bbe2e32fb28e4f0a
Parents: 15db4e8
Author: Shawn Feldman <sfeldman@apache.org>
Authored: Wed Apr 8 09:30:48 2015 -0600
Committer: Shawn Feldman <sfeldman@apache.org>
Committed: Wed Apr 8 10:11:12 2015 -0600

----------------------------------------------------------------------
 .../corepersistence/CpEntityManagerFactory.java | 139 +++++-----
 .../migration/AppInfoMigrationPlugin.java       | 252 ++++++++-----------
 .../persistence/EntityManagerFactory.java       |  71 ++++--
 .../cassandra/EntityManagerFactoryImpl.java     |  12 +
 .../migration/AppInfoMigrationPluginTest.java   |  11 +-
 5 files changed, 235 insertions(+), 250 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/a62091df/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 3700003..ce036f5 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
@@ -24,14 +24,12 @@ import com.google.inject.TypeLiteral;
 
 import java.util.Arrays;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.TreeMap;
 import java.util.UUID;
 import java.util.concurrent.atomic.AtomicBoolean;
 
-import org.apache.usergrid.persistence.graph.impl.SimpleEdge;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.BeansException;
@@ -68,16 +66,9 @@ import org.apache.usergrid.persistence.model.entity.Id;
 import org.apache.usergrid.persistence.model.entity.SimpleId;
 import org.apache.usergrid.persistence.model.util.UUIDGenerator;
 import org.apache.usergrid.utils.UUIDUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.beans.BeansException;
-import org.springframework.context.ApplicationContext;
-import org.springframework.context.ApplicationContextAware;
 import rx.Observable;
-import rx.observables.BlockingObservable;
 
 import java.util.*;
-import java.util.concurrent.atomic.AtomicBoolean;
 
 import static java.lang.String.CASE_INSENSITIVE_ORDER;
 import static org.apache.usergrid.persistence.Schema.*;
@@ -245,7 +236,7 @@ public class CpEntityManagerFactory implements EntityManagerFactory, Application
         // Ensure our management system exists before creating our application
         init();
 
-        EntityManager managementEm = getEntityManager( CpNamingUtils.MANAGEMENT_APPLICATION_ID
);
+        EntityManager managementEm = getEntityManager( getManagementAppId() );
 
         final String appName = buildAppName( organizationName, name );
 
@@ -303,41 +294,66 @@ public class CpEntityManagerFactory implements EntityManagerFactory,
Application
     public void deleteApplication(UUID applicationId) throws Exception {
 
         // find application_info for application to delete
+        String collectionFromName = CpNamingUtils.APPLICATION_INFO;
+        String collectionToName = CpNamingUtils.DELETED_APPLICATION_INFO;
 
+        migrateAppInfo(applicationId, collectionFromName, collectionToName).toBlocking()
+            .lastOrDefault(null);
+    }
 
+    @Override
+    public Entity restoreApplication(UUID applicationId) throws Exception {
+
+        // get the deleted_application_info for the deleted app
+
+        final EntityManager managementEm = getEntityManager(getManagementAppId());
+
+        migrateAppInfo(applicationId,CpNamingUtils.DELETED_APPLICATION_INFO,CpNamingUtils.APPLICATION_INFO)
+            .toBlocking().lastOrDefault(null);
+
+        this.rebuildApplicationIndexes(applicationId, new ProgressObserver() {
+            @Override
+            public void onProgress(EntityRef entity) {
+                logger.info("Restored entity {}:{}", entity.getType(), entity.getUuid());
+            }
+        });
+
+        return managementEm.get(new SimpleEntityRef(CpNamingUtils.APPLICATION_INFO,applicationId));
+    }
+
+    @Override
+    public Observable migrateAppInfo(UUID applicationUUID, String collectionFromName, String
collectionToName) throws Exception {
+
+        final ApplicationScope managementAppScope = CpNamingUtils.getApplicationScope(CpNamingUtils.MANAGEMENT_APPLICATION_ID);
         final EntityManager managementEm = getEntityManager(CpNamingUtils.MANAGEMENT_APPLICATION_ID);
-        final Application app = managementEm.getApplication();
-        final Id managementAppId = new SimpleId(app.getUuid(), app.getType());
-        final ApplicationScope managementAppScope = new ApplicationScopeImpl(managementAppId);
-        final GraphManager managementGraphManager = managerCache.getGraphManager(managementAppScope);
-        final Id deletedAppId = new SimpleId(applicationId, CpNamingUtils.APPLICATION_INFO);
-        final ApplicationScope deletedAppScope = new ApplicationScopeImpl(deletedAppId);
 
-        Entity appInfoToDelete = managementEm.get(new SimpleEntityRef(CpNamingUtils.APPLICATION_INFO,
applicationId));
+        final Id applicationId = new SimpleId(applicationUUID, collectionFromName);
+        final ApplicationScope applicationScope = new ApplicationScopeImpl(applicationId);
+
+        Entity oldAppEntity = managementEm.get(new SimpleEntityRef(collectionFromName, applicationUUID));
         Observable copyConnections = Observable.empty();
-        if(appInfoToDelete!=null) {
+        if(oldAppEntity!=null) {
             // ensure that there is not already a deleted app with the same name
 
-            final EntityRef alias = managementEm.getAlias(
-                CpNamingUtils.DELETED_APPLICATION_INFO, appInfoToDelete.getName());
+            final EntityRef alias = managementEm.getAlias(collectionToName, oldAppEntity.getName());
             if (alias != null) {
                 throw new ConflictException("Cannot delete app with same name as already
deleted app");
             }
             // make a copy of the app to delete application_info entity
             // and put it in a deleted_application_info collection
 
-            final Entity deletedApp = managementEm.create(new SimpleId(appInfoToDelete.getUuid(),
-                CpNamingUtils.DELETED_APPLICATION_INFO), appInfoToDelete.getProperties());
+            final Entity newAppEntity = managementEm.create(new SimpleId(applicationUUID,
+                collectionToName), oldAppEntity.getProperties());
             // copy its connections too
 
-            final Set<String> connectionTypes = managementEm.getConnectionTypes(appInfoToDelete);
+            final Set<String> connectionTypes = managementEm.getConnectionTypes(oldAppEntity);
             copyConnections = Observable.from(connectionTypes).doOnNext(connType -> {
                 try {
                     final Results connResults =
-                        managementEm.getConnectedEntities(appInfoToDelete, connType, null,
Query.Level.ALL_PROPERTIES);
+                        managementEm.getConnectedEntities(oldAppEntity, connType, null, Query.Level.ALL_PROPERTIES);
                     connResults.getEntities().forEach(entity -> {
                         try {
-                            managementEm.createConnection(deletedApp, connType, entity);
+                            managementEm.createConnection(newAppEntity, connType, entity);
                         } catch (Exception e) {
                             throw new RuntimeException(e);
                         }
@@ -347,67 +363,23 @@ public class CpEntityManagerFactory implements EntityManagerFactory,
Application
                 }
             });
         }
-        final ApplicationEntityIndex aei = entityIndexFactory.createApplicationEntityIndex(deletedAppScope);
-        final Observable deleteNodeGraph = managementGraphManager.deleteNode(deletedAppId,
Long.MAX_VALUE);
+        final ApplicationEntityIndex aei = entityIndexFactory.createApplicationEntityIndex(applicationScope);
+        final GraphManager managementGraphManager = managerCache.getGraphManager(managementAppScope);
+        final Observable deleteNodeGraph = managementGraphManager.deleteNode(applicationId,
Long.MAX_VALUE);
         final Observable deleteAppFromIndex = aei.deleteApplication();
 
-        Observable.concat(copyConnections, deleteNodeGraph, deleteAppFromIndex)
+        return Observable.concat(copyConnections, deleteNodeGraph, deleteAppFromIndex)
             .doOnCompleted(() -> {
                 try {
-                    managementEm.delete(appInfoToDelete);
-                    applicationIdCache.evictAppId(appInfoToDelete.getName());
+                    if (oldAppEntity != null) {
+                        managementEm.delete(oldAppEntity);
+                        applicationIdCache.evictAppId(oldAppEntity.getName());
+                        entityIndex.refresh();
+                    }
                 } catch (Exception e) {
                     throw new RuntimeException(e);
                 }
-            }).toBlocking().lastOrDefault(null);
-
-    }
-
-
-    @Override
-    public Entity restoreApplication(UUID applicationId) throws Exception {
-
-        // get the deleted_application_info for the deleted app
-
-        final EntityManager managementEm = getEntityManager(getManagementAppId());
-
-        final Entity deletedAppInfo = managementEm.get(new SimpleEntityRef(CpNamingUtils.DELETED_APPLICATION_INFO,applicationId));
-
-        if ( deletedAppInfo == null ) {
-            throw new EntityNotFoundException("Cannot restore. Deleted Application not found:
" + applicationId );
-        }
-
-
-        // create application_info for restored app
-
-        Entity restoredAppInfo = managementEm.create(new SimpleId( deletedAppInfo.getUuid(),CpNamingUtils.APPLICATION_INFO)
-            , deletedAppInfo.getProperties());
-
-        // copy connections from deleted app entity
-
-        final Set<String> connectionTypes = managementEm.getConnectionTypes(deletedAppInfo);
-        for ( String connType : connectionTypes ) {
-            final Results connResults =
-                managementEm.getConnectedEntities(deletedAppInfo, connType, null, Query.Level.ALL_PROPERTIES);
-            for ( Entity entity : connResults.getEntities() ) {
-                managementEm.createConnection( restoredAppInfo, connType, entity );
-            }
-        }
-
-        // delete the deleted app entity rebuild the app index
-        managementEm.delete(deletedAppInfo);
-
-        entityIndex.refresh();
-
-        this.rebuildApplicationIndexes(applicationId, new ProgressObserver() {
-            @Override
-            public void onProgress(EntityRef entity) {
-                logger.info("Restored entity {}:{}", entity.getType(), entity.getUuid());
-            }
-        });
-        applicationIdCache.evictAppId(restoredAppInfo.getName());
-
-        return restoredAppInfo;
+            });
     }
 
 
@@ -536,7 +508,7 @@ public class CpEntityManagerFactory implements EntityManagerFactory, Application
     public boolean updateServiceProperties(Map<String, String> properties) {
 
         EntityManager em = getEntityManager(getManagementAppId());
-        Query q = Query.fromQL( "select *");
+        Query q = Query.fromQL("select *");
         Results results = null;
         try {
             results = em.searchCollection( em.getApplicationRef(), "propertymaps", q);
@@ -558,7 +530,7 @@ public class CpEntityManagerFactory implements EntityManagerFactory, Application
         // intentionally going only one-level deep into fields and treating all
         // values as strings because that is all we need for service properties
         for ( String key : properties.keySet() ) {
-            propsEntity.setProperty( key, properties.get(key).toString() );
+            propsEntity.setProperty(key, properties.get(key).toString());
         }
 
         try {
@@ -647,6 +619,11 @@ public class CpEntityManagerFactory implements EntityManagerFactory,
Application
         return CpNamingUtils.MANAGEMENT_APPLICATION_ID;
     }
 
+    @Override
+    public EntityManager getManagementEntityManager() {
+        return getEntityManager(CpNamingUtils.MANAGEMENT_APPLICATION_ID);
+    }
+
 
     /**
      * Gets the setup.

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/a62091df/stack/core/src/main/java/org/apache/usergrid/corepersistence/migration/AppInfoMigrationPlugin.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/migration/AppInfoMigrationPlugin.java
b/stack/core/src/main/java/org/apache/usergrid/corepersistence/migration/AppInfoMigrationPlugin.java
index 53ffe79..92c8895 100644
--- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/migration/AppInfoMigrationPlugin.java
+++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/migration/AppInfoMigrationPlugin.java
@@ -36,7 +36,6 @@ import org.apache.usergrid.persistence.graph.GraphManager;
 import org.apache.usergrid.persistence.graph.GraphManagerFactory;
 import org.apache.usergrid.persistence.graph.SearchByEdgeType;
 import org.apache.usergrid.persistence.graph.impl.SimpleSearchByEdgeType;
-import org.apache.usergrid.persistence.index.query.Query;
 import org.apache.usergrid.persistence.model.entity.*;
 import org.apache.usergrid.utils.UUIDUtils;
 import org.slf4j.Logger;
@@ -45,7 +44,6 @@ import rx.Observable;
 import rx.functions.Func1;
 
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.Map;
 import java.util.UUID;
 
@@ -99,7 +97,7 @@ public class AppInfoMigrationPlugin implements MigrationPlugin {
 
     @Override
     public int getMaxVersion() {
-        return 1; // standalone plugin, happens once
+        return 2; // standalone plugin, happens once
     }
 
 
@@ -112,153 +110,123 @@ public class AppInfoMigrationPlugin implements MigrationPlugin {
     @Override
     public void run(ProgressObserver observer) {
 
-        final int version = migrationInfoSerialization.getVersion( getName() );
+        final int version = migrationInfoSerialization.getVersion(getName());
 
-        if ( version == getMaxVersion() ) {
+        if (version == getMaxVersion()) {
             logger.debug("Skipping Migration Plugin: " + getName());
             return;
         }
-
         observer.start();
-
-        // Get appinfos from the Graph, we don't expect many so use iterator
-
-        final Iterator<org.apache.usergrid.persistence.model.entity.Entity> iterator
=
-            getOldAppInfos().toBlocking().getIterator();
-
-        if ( iterator.hasNext() ) {
-
-            // we found appinfos, now migrate them to application_infos in the Management
App
-
-            final EntityManager em = emf.getEntityManager( emf.getManagementAppId());
-
-            String currentAppName = null;
-            try {
-                logger.info("Migrating old appinfos");
-
-                while ( iterator.hasNext() ) {
-
-                    Map oldAppInfo = CpEntityMapUtils.toMap( iterator.next() );
-                    currentAppName = (String)oldAppInfo.get( PROPERTY_NAME );
-
-                    migragrateOneAppInfo(em, oldAppInfo, observer);
+        //get old app infos to migrate
+        final Observable<org.apache.usergrid.persistence.model.entity.Entity> oldAppInfos
= getOldAppInfos();
+        oldAppInfos
+            .doOnNext(oldAppInfoEntity -> {
+                try {
+                    migrateAppInfo( oldAppInfoEntity, observer);
+                }catch (Exception e){
+                    logger.error("Failed to migrate app info"+oldAppInfoEntity.getId().getUuid(),e);
+                    throw new RuntimeException(e);
                 }
 
-                // note that the old appinfos are not deleted
+            })
+            .doOnCompleted(() -> {
+                migrationInfoSerialization.setVersion(getName(), getMaxVersion());
+                observer.complete();
+            }).toBlocking().lastOrDefault(null);
 
-                migrationInfoSerialization.setVersion( getName(), getMaxVersion() );
-
-            } catch (Exception e) {
+    }
 
-                // stop on any exception and return failure
 
-                String msg = "Exception writing application_info for " + currentAppName;
-                logger.error(msg, e);
-                observer.failed( getMaxVersion(), msg);
+    private void migrateAppInfo( org.apache.usergrid.persistence.model.entity.Entity oldAppInfoEntity,
ProgressObserver observer) {
+        // Get appinfos from the Graph, we don't expect many so use iterator
+        final EntityManager managementEm = emf.getManagementEntityManager();
+
+        Map oldAppInfoMap = CpEntityMapUtils.toMap(oldAppInfoEntity);
+
+        final String name = (String) oldAppInfoMap.get(PROPERTY_NAME);
+
+        try {
+            final String orgName = name.split("/")[0];
+            final String appName = name.split("/")[1];
+            UUID applicationId = getUuid(oldAppInfoMap,"applicationUuid");
+            UUID originalUuid = getUuid(oldAppInfoMap,"uuid");
+
+            //get app info from graph to see if it has been migrated already
+            Entity appInfo = getApplicationInfo(applicationId);
+            if (appInfo == null) {
+                // create and connect new APPLICATION_INFO oldAppInfo to Organization
+                appInfo = createNewAppInfo(managementEm, name, applicationId);
+                deleteOldAppInfo(originalUuid);
+                observer.update(getMaxVersion(), "Created application_info for " + appName);
+                // create org->app connections, but not for apps in dummy "usergrid" internal
organization
+                if (!orgName.equals("usergrid")) {
+                    EntityRef orgRef = managementEm.getAlias(Group.ENTITY_TYPE, orgName);
+                    managementEm.createConnection(orgRef, "owns", appInfo);
+                }
+            } else {
+                //already migrated don't do anything
+                observer.update(getMaxVersion(), "Received existing application_info for
" + appName + " don't do anything");
             }
 
-        } else {
-            logger.info("No old appinfos found, no need for migration");
+        } catch (Exception e) {
+            String msg = "Exception writing application_info for " + name;
+            logger.error(msg, e);
+            observer.failed(getMaxVersion(), msg);
+            throw new RuntimeException(e);
         }
 
-        observer.complete();
-
     }
 
-
-    private void migragrateOneAppInfo(
-        EntityManager em, Map oldAppInfo, ProgressObserver observer) throws Exception {
-
-        final String name = (String)oldAppInfo.get( PROPERTY_NAME );
-        final String orgName = name.split("/")[0];
-        final String appName = name.split("/")[1];
-
+    private UUID getUuid(Map oldAppInfoMap, String key) {
         UUID applicationId;
-
-        Object uuidObject = oldAppInfo.get("applicationUuid");
+        Object uuidObject = oldAppInfoMap.get(key);
         if (uuidObject instanceof UUID) {
             applicationId = (UUID) uuidObject;
         } else {
             applicationId = UUIDUtils.tryExtractUUID(uuidObject.toString());
         }
+        return applicationId;
+    }
 
-        // create and connect new APPLICATION_INFO oldAppInfo to Organization
-
-        final UUID appId = applicationId;
-
-        Entity appInfo = getApplicationInfo( emf, appId );
-        if ( appInfo == null ) {
-            Map<String, Object> appInfoMap = new HashMap<String, Object>() {{
-                put(PROPERTY_NAME, name);
-                put(PROPERTY_APPLICATION_ID, appId);
-            }};
-            appInfo = em.create(appId, CpNamingUtils.APPLICATION_INFO, appInfoMap);
-            observer.update( getMaxVersion(), "Created application_info for " + appName);
-
-        } else {
-            appInfo.setProperty(PROPERTY_APPLICATION_ID, appId);
-            em.update(appInfo);
-            observer.update( getMaxVersion(), "Updated existing application_info for " +
appName);
-        }
-
-        // create org->app connections, but not for apps in dummy "usergrid" internal
organization
+    private Entity createNewAppInfo(EntityManager managementEm, final String name, final
UUID applicationId) throws Exception {
+        Entity appInfo;Map<String, Object> appInfoMap = new HashMap<String, Object>()
{{
+            put(PROPERTY_NAME, name);
+            put(PROPERTY_APPLICATION_ID, applicationId);
+        }};
+        appInfo = managementEm.create(new SimpleId(applicationId, CpNamingUtils.APPLICATION_INFO),
appInfoMap);
+        return appInfo;
+    }
 
-        if ( !orgName.equals("usergrid") ) {
-            EntityRef orgRef = em.getAlias(Group.ENTITY_TYPE, orgName );
-            em.createConnection(orgRef, "owns", appInfo);
-        }
+    private void deleteOldAppInfo(UUID uuid) {
+        final ApplicationScope systemAppScope = getApplicationScope(CpNamingUtils.SYSTEM_APP_ID
);
+        final EntityCollectionManager systemCollectionManager =
+            entityCollectionManagerFactory.createCollectionManager( systemAppScope );
+        systemCollectionManager.delete(new SimpleId(uuid, "appinfos")).toBlocking().last();
     }
 
 
     /**
      * TODO: Use Graph to get application_info for an specified Application.
      */
-    private Entity getApplicationInfo( final EntityManagerFactory emf, final UUID appId )
throws Exception {
-
-        final ApplicationScope appScope = getApplicationScope( emf.getManagementAppId() );
-
-        final EntityCollectionManager collectionManager =
-            entityCollectionManagerFactory.createCollectionManager( appScope );
-
-        final GraphManager gm = graphManagerFactory.createEdgeManager(appScope);
-
-        String edgeType = CpNamingUtils.getEdgeTypeFromCollectionName( CpNamingUtils.APPLICATION_INFOS
);
+    private Entity getApplicationInfo( final UUID appId ) throws Exception {
 
-        Id rootAppId = appScope.getApplication();
-
-        final SimpleSearchByEdgeType simpleSearchByEdgeType =  new SimpleSearchByEdgeType(
-            rootAppId, edgeType, Long.MAX_VALUE, SearchByEdgeType.Order.DESCENDING, null);
-
-        // TODO: is there a better way?
+        final ApplicationScope managementAppScope = getApplicationScope(CpNamingUtils.MANAGEMENT_APPLICATION_ID);
+        final EntityCollectionManager managementCollectionManager = entityCollectionManagerFactory.createCollectionManager(managementAppScope);
 
+        Observable<Edge> edgesObservable = getApplicationInfoEdges();
+        //get the graph for all app infos
         Observable<org.apache.usergrid.persistence.model.entity.Entity> entityObs =
-            gm.loadEdgesFromSource( simpleSearchByEdgeType )
-                .flatMap(new Func1<Edge, Observable<org.apache.usergrid.persistence.model.entity.Entity>>()
{
-
-                    @Override
-                    public Observable<org.apache.usergrid.persistence.model.entity.Entity>
call(final Edge edge) {
-
-                        final Id appInfoId = edge.getTargetNode();
-
-                        return collectionManager.load(appInfoId)
-                            .filter(new Func1<org.apache.usergrid.persistence.model.entity.Entity,
Boolean>() {
-                                @Override
-                                public Boolean call(final org.apache.usergrid.persistence.model.entity.Entity
entity) {
-                                    if (entity == null) {
-                                        logger.warn("Encountered a null application info
for id {}", appInfoId);
-                                        return false;
-                                    }
-                                    if ( entity.getId().getUuid().equals( appId )) {
-                                        return true;
-                                    }
-                                    return false;
-                                }
-                            });
-                    }
+                edgesObservable.flatMap(edge -> {
+                    final Id appInfoId = edge.getTargetNode();
+                    return managementCollectionManager
+                        .load(appInfoId)
+                        .filter( entity ->{
+                            //check for app id
+                            return  entity != null ? entity.getId().getUuid().equals(appId)
: false;
+                        });
                 });
 
         // don't expect many applications, so we block
-
         org.apache.usergrid.persistence.model.entity.Entity applicationInfo =
             entityObs.toBlocking().lastOrDefault(null);
 
@@ -275,14 +243,6 @@ public class AppInfoMigrationPlugin implements MigrationPlugin {
 
         return entity;
 
-//        UUID mgmtAppId = emf.getManagementAppId();
-//        EntityManager rootEm = emf.getEntityManager( mgmtAppId );
-//
-//        final Results applicationInfoResults = rootEm.searchCollection(
-//            new SimpleEntityRef("application", mgmtAppId), "application_infos",
-//            Query.fromQL("select * where applicationId=" + appId.toString()));
-//
-//        return applicationInfoResults.getEntity();
     }
 
 
@@ -291,43 +251,43 @@ public class AppInfoMigrationPlugin implements MigrationPlugin {
      */
     public Observable<org.apache.usergrid.persistence.model.entity.Entity> getOldAppInfos(
) {
 
-        final ApplicationScope appScope = getApplicationScope( CpNamingUtils.SYSTEM_APP_ID
);
+        final ApplicationScope systemAppScope = getApplicationScope(CpNamingUtils.SYSTEM_APP_ID);
 
-        final EntityCollectionManager collectionManager =
-            entityCollectionManagerFactory.createCollectionManager( appScope );
+        final EntityCollectionManager systemCollectionManager =
+            entityCollectionManagerFactory.createCollectionManager(systemAppScope);
 
-        final GraphManager gm = graphManagerFactory.createEdgeManager(appScope);
+        final GraphManager gm = graphManagerFactory.createEdgeManager(systemAppScope);
 
-        String edgeType = CpNamingUtils.getEdgeTypeFromCollectionName( "appinfos" );
+        String edgeType = CpNamingUtils.getEdgeTypeFromCollectionName("appinfos");
 
-        Id rootAppId = appScope.getApplication();
+        Id rootAppId = systemAppScope.getApplication();
 
         final SimpleSearchByEdgeType simpleSearchByEdgeType =  new SimpleSearchByEdgeType(
             rootAppId, edgeType, Long.MAX_VALUE, SearchByEdgeType.Order.DESCENDING, null);
 
         Observable<org.apache.usergrid.persistence.model.entity.Entity> entityObs =
             gm.loadEdgesFromSource( simpleSearchByEdgeType )
-            .flatMap(new Func1<Edge, Observable<org.apache.usergrid.persistence.model.entity.Entity>>()
{
+            .flatMap(edge -> {
+                final Id appInfoId = edge.getTargetNode();
 
-                @Override
-                public Observable<org.apache.usergrid.persistence.model.entity.Entity>
call(final Edge edge) {
-
-                    final Id appInfoId = edge.getTargetNode();
-
-                    return collectionManager.load(appInfoId)
-                        .filter(new Func1<org.apache.usergrid.persistence.model.entity.Entity,
Boolean>() {
-                            @Override
-                            public Boolean call(final org.apache.usergrid.persistence.model.entity.Entity
entity) {
-                                if (entity == null) {
-                                    logger.warn("Encountered a null application info for
id {}", appInfoId);
-                                    return false;
-                                }
-                                return true;
-                            }
-                        });
-                }
+                return systemCollectionManager.load(appInfoId)
+                    .filter(entity -> (entity != null));
             });
 
         return entityObs;
     }
+
+    Observable<Edge> edgesObservable;
+    public Observable<Edge> getApplicationInfoEdges() {
+        final ApplicationScope managementAppScope = getApplicationScope(CpNamingUtils.MANAGEMENT_APPLICATION_ID);
+        final GraphManager gm = graphManagerFactory.createEdgeManager(managementAppScope);
+
+        String edgeType = CpNamingUtils.getEdgeTypeFromCollectionName(CpNamingUtils.APPLICATION_INFOS);
+
+
+        final SimpleSearchByEdgeType simpleSearchByEdgeType =  new SimpleSearchByEdgeType(
+            CpNamingUtils.generateApplicationId(CpNamingUtils.MANAGEMENT_APPLICATION_ID),
edgeType, Long.MAX_VALUE, SearchByEdgeType.Order.DESCENDING, null);
+        edgesObservable = edgesObservable !=null ? edgesObservable : gm.loadEdgesFromSource(
simpleSearchByEdgeType );
+        return edgesObservable;
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/a62091df/stack/core/src/main/java/org/apache/usergrid/persistence/EntityManagerFactory.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/persistence/EntityManagerFactory.java
b/stack/core/src/main/java/org/apache/usergrid/persistence/EntityManagerFactory.java
index 4182732..445cef4 100644
--- a/stack/core/src/main/java/org/apache/usergrid/persistence/EntityManagerFactory.java
+++ b/stack/core/src/main/java/org/apache/usergrid/persistence/EntityManagerFactory.java
@@ -21,6 +21,7 @@ import java.util.Map;
 import java.util.UUID;
 
 import org.apache.usergrid.persistence.core.util.Health;
+import rx.Observable;
 
 
 /**
@@ -39,21 +40,28 @@ public interface EntityManagerFactory {
      *
      * @return EntityDao for the specified parameters
      */
-    public abstract EntityManager getEntityManager( UUID applicationId );
+    EntityManager getEntityManager( UUID applicationId );
 
     /**
-     * Creates a new application.
-     *
-     * @param name a unique application name.
-     *
-     * @return Entity of type application_info that represents the newly created Application
-     *
-     * @throws Exception the exception
+     * get management app em
+     * @return
      */
-    public abstract Entity createApplicationV2( String organizationName, String name ) throws
Exception;
+    EntityManager getManagementEntityManager();
+
+
+        /**
+         * Creates a new application.
+         *
+         * @param name a unique application name.
+         *
+         * @return Entity of type application_info that represents the newly created Application
+         *
+         * @throws Exception the exception
+         */
+    Entity createApplicationV2( String organizationName, String name ) throws Exception;
 
     @Deprecated
-    public abstract UUID createApplication( String organizationName, String name ) throws
Exception;
+    UUID createApplication( String organizationName, String name ) throws Exception;
 
     /**
      * Creates a Application entity. All entities except for applications must be attached
to a
@@ -66,11 +74,11 @@ public interface EntityManagerFactory {
      *
      * @throws Exception the exception
      */
-    public abstract Entity createApplicationV2(
+    Entity createApplicationV2(
         String organizationName, String name, Map<String, Object> properties ) throws
Exception;
 
     @Deprecated
-    public abstract UUID createApplication(
+    UUID createApplication(
         String organizationName, String name, Map<String, Object> properties ) throws
Exception;
 
     /**
@@ -78,14 +86,33 @@ public interface EntityManagerFactory {
      *
      * @param applicationId UUID of Application to be deleted.
      */
-    public abstract void deleteApplication( UUID applicationId ) throws Exception;
+    void deleteApplication( UUID applicationId ) throws Exception;
+
+    /**
+     *
+     * @param applicationUUID
+     * @param collectionFromName
+     * @param collectionToName
+     * @return
+     * @throws Exception
+     */
+    Observable migrateAppInfo( UUID applicationUUID, String collectionFromName, String collectionToName)
throws Exception;
 
     /**
      * Restore deleted application.
      */
-    public Entity restoreApplication( UUID applicationId) throws Exception;
+    Entity restoreApplication( UUID applicationId) throws Exception;
 
-    public abstract UUID importApplication( String organization, UUID applicationId, String
name,
+    /**
+     *
+     * @param organization
+     * @param applicationId
+     * @param name
+     * @param properties
+     * @return
+     * @throws Exception
+     */
+    UUID importApplication( String organization, UUID applicationId, String name,
                                             Map<String, Object> properties ) throws
Exception;
 
     /**
@@ -97,7 +124,7 @@ public interface EntityManagerFactory {
      *
      * @throws Exception the exception
      */
-    public abstract UUID lookupApplication( String name ) throws Exception;
+    UUID lookupApplication( String name ) throws Exception;
 
     /**
      * Returns all the applications in the system.
@@ -106,19 +133,19 @@ public interface EntityManagerFactory {
      *
      * @throws Exception the exception
      */
-    public abstract Map<String, UUID> getApplications() throws Exception;
+    Map<String, UUID> getApplications() throws Exception;
 
     public Map<String, UUID> getDeletedApplications() throws Exception;
 
-    public abstract void setup() throws Exception;
+    void setup() throws Exception;
 
-    public abstract Map<String, String> getServiceProperties();
+    Map<String, String> getServiceProperties();
 
-    public abstract boolean updateServiceProperties( Map<String, String> properties
);
+    boolean updateServiceProperties( Map<String, String> properties );
 
-    public abstract boolean setServiceProperty( String name, String value );
+    boolean setServiceProperty( String name, String value );
 
-    public abstract boolean deleteServiceProperty( String name );
+    boolean deleteServiceProperty( String name );
 
     /**
      * @return Entity of type application_info that represents the newly created application.

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/a62091df/stack/core/src/main/java/org/apache/usergrid/persistence/cassandra/EntityManagerFactoryImpl.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/persistence/cassandra/EntityManagerFactoryImpl.java
b/stack/core/src/main/java/org/apache/usergrid/persistence/cassandra/EntityManagerFactoryImpl.java
index 882e7d9..91d7524 100644
--- a/stack/core/src/main/java/org/apache/usergrid/persistence/cassandra/EntityManagerFactoryImpl.java
+++ b/stack/core/src/main/java/org/apache/usergrid/persistence/cassandra/EntityManagerFactoryImpl.java
@@ -22,6 +22,7 @@ import java.util.Map;
 import java.util.TreeMap;
 import java.util.UUID;
 
+import org.apache.usergrid.exception.NotImplementedException;
 import org.apache.usergrid.persistence.Entity;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -68,6 +69,7 @@ import static org.apache.usergrid.persistence.cassandra.CassandraService.RETRY_C
 import static org.apache.usergrid.utils.ConversionUtils.uuid;
 import static org.apache.usergrid.persistence.cassandra.Serializers.*;
 import org.apache.usergrid.persistence.core.util.Health;
+import rx.Observable;
 
 
 /**
@@ -133,6 +135,11 @@ public class EntityManagerFactoryImpl implements EntityManagerFactory,
Applicati
         return _getEntityManager( applicationId );
     }
 
+    @Override
+    public EntityManager getManagementEntityManager() {
+        throw new NotImplementedException();
+    }
+
 
     private EntityManager _getEntityManager( UUID applicationId ) {
         EntityManagerImpl em = new EntityManagerImpl();
@@ -214,6 +221,11 @@ public class EntityManagerFactoryImpl implements EntityManagerFactory,
Applicati
         throw new UnsupportedOperationException("Not supported.");
     }
 
+    @Override
+    public Observable migrateAppInfo(UUID applicationUUID, String collectionFromName, String
collectionToName) throws Exception {
+        throw new UnsupportedOperationException("Not supported.");
+    }
+
 
     private String buildAppName( String organizationName, String name ) {
         return StringUtils.lowerCase( name.contains( "/" ) ? name : organizationName + "/"
+ name );

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/a62091df/stack/services/src/test/java/org/apache/usergrid/corepersistence/migration/AppInfoMigrationPluginTest.java
----------------------------------------------------------------------
diff --git a/stack/services/src/test/java/org/apache/usergrid/corepersistence/migration/AppInfoMigrationPluginTest.java
b/stack/services/src/test/java/org/apache/usergrid/corepersistence/migration/AppInfoMigrationPluginTest.java
index 9cb99be..5c36876 100644
--- a/stack/services/src/test/java/org/apache/usergrid/corepersistence/migration/AppInfoMigrationPluginTest.java
+++ b/stack/services/src/test/java/org/apache/usergrid/corepersistence/migration/AppInfoMigrationPluginTest.java
@@ -27,8 +27,11 @@ import org.apache.usergrid.corepersistence.util.CpNamingUtils;
 import org.apache.usergrid.management.OrganizationOwnerInfo;
 import org.apache.usergrid.persistence.*;
 import org.apache.usergrid.persistence.cassandra.CassandraService;
+import org.apache.usergrid.persistence.collection.EntityCollectionManagerFactory;
+import org.apache.usergrid.persistence.core.migration.data.MigrationInfoSerialization;
 import org.apache.usergrid.persistence.core.migration.data.ProgressObserver;
 import org.apache.usergrid.persistence.entities.Application;
+import org.apache.usergrid.persistence.graph.GraphManagerFactory;
 import org.apache.usergrid.persistence.index.query.Query;
 import org.junit.ClassRule;
 import org.junit.Rule;
@@ -65,6 +68,12 @@ public class AppInfoMigrationPluginTest {
 
     @Test
     public void testRun() throws Exception {
+        MigrationInfoSerialization serialization = Mockito.mock(MigrationInfoSerialization.class);
+        Mockito.when(serialization.getVersion(Mockito.any())).thenReturn(0);
+        EntityCollectionManagerFactory ecmf = setup.getInjector().getInstance(EntityCollectionManagerFactory.class);
+        GraphManagerFactory gmf = setup.getInjector().getInstance(GraphManagerFactory.class);
+
+        AppInfoMigrationPlugin appInfoMigrationPlugin = new AppInfoMigrationPlugin(setup.getEmf(),serialization,ecmf,gmf);
 
        // create 10 applications, each with 10 entities
 
@@ -148,7 +157,7 @@ public class AppInfoMigrationPluginTest {
         logger.debug("\n\nRun the migration\n");
 
         ProgressObserver po = Mockito.mock(ProgressObserver.class);
-        setup.getAppInfoMigrationPlugin().run(po);
+        appInfoMigrationPlugin.run(po);
 
         logger.debug("\n\nVerify migration results\n");
 


Mime
View raw message