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/07 00:56:29 UTC

[1/2] incubator-usergrid git commit: Updated export format.

Repository: incubator-usergrid
Updated Branches:
  refs/heads/two-dot-o-import-multicollection 2e05f2496 -> f977a89be


Updated export format.


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

Branch: refs/heads/two-dot-o-import-multicollection
Commit: 6e0991f27deb3406512ff74d14acbfa45f1da48a
Parents: 2e05f24
Author: Todd Nine <tn...@apigee.com>
Authored: Fri Feb 6 13:16:42 2015 -0700
Committer: Todd Nine <tn...@apigee.com>
Committed: Fri Feb 6 13:16:42 2015 -0700

----------------------------------------------------------------------
 .../management/export/ExportServiceImpl.java        | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/6e0991f2/stack/services/src/main/java/org/apache/usergrid/management/export/ExportServiceImpl.java
----------------------------------------------------------------------
diff --git a/stack/services/src/main/java/org/apache/usergrid/management/export/ExportServiceImpl.java b/stack/services/src/main/java/org/apache/usergrid/management/export/ExportServiceImpl.java
index e27857f..8caecbc 100644
--- a/stack/services/src/main/java/org/apache/usergrid/management/export/ExportServiceImpl.java
+++ b/stack/services/src/main/java/org/apache/usergrid/management/export/ExportServiceImpl.java
@@ -570,7 +570,8 @@ public class ExportServiceImpl implements ExportService {
 
         JsonGenerator jg = getJsonGenerator( ephemeral );
 
-        jg.writeStartArray();
+        jg.writeStartObject();
+        jg.writeObjectFieldStart( "collections" );
 
         for ( String collectionName : metadata.keySet() ) {
 
@@ -579,6 +580,10 @@ public class ExportServiceImpl implements ExportService {
             }
             //if the collection you are looping through doesn't match the name of the one you want. Don't export it.
             if ( ( config.get( "collectionName" ) == null ) || collectionName.equalsIgnoreCase((String)config.get( "collectionName" ) ) ) {
+
+                //write out the collection name at the start of the file
+                jg.writeArrayFieldStart( collectionName.toLowerCase() );
+
                 //Query entity manager for the entities in a collection
                 Query query = null;
                 if ( config.get( "query" ) == null ) {
@@ -609,10 +614,17 @@ public class ExportServiceImpl implements ExportService {
                     saveCollectionMembers( jg, em, ( String ) config.get( "collectionName" ), entity );
                     jg.writeEndObject();
                     jg.flush();
+
                 }
+
+
+
+                //write out the end collection
+                jg.writeEndArray();
             }
         }
-        jg.writeEndArray();
+        jg.writeEndObject();
+        jg.writeEndObject();
         jg.flush();
         jg.close();
 


[2/2] incubator-usergrid git commit: Changed format to work with new file format.

Posted by to...@apache.org.
Changed format to work with new file format.


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

Branch: refs/heads/two-dot-o-import-multicollection
Commit: f977a89bef04b235659e88889423b1354dcf6f1f
Parents: 6e0991f
Author: Todd Nine <tn...@apigee.com>
Authored: Fri Feb 6 16:56:16 2015 -0700
Committer: Todd Nine <tn...@apigee.com>
Committed: Fri Feb 6 16:56:16 2015 -0700

----------------------------------------------------------------------
 .../management/importer/ImportServiceImpl.java  | 147 +++++++++++--------
 .../management/importer/ImportCollectionIT.java |  39 -----
 2 files changed, 89 insertions(+), 97 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/f977a89b/stack/services/src/main/java/org/apache/usergrid/management/importer/ImportServiceImpl.java
----------------------------------------------------------------------
diff --git a/stack/services/src/main/java/org/apache/usergrid/management/importer/ImportServiceImpl.java b/stack/services/src/main/java/org/apache/usergrid/management/importer/ImportServiceImpl.java
index 51e9a09..7b58796 100644
--- a/stack/services/src/main/java/org/apache/usergrid/management/importer/ImportServiceImpl.java
+++ b/stack/services/src/main/java/org/apache/usergrid/management/importer/ImportServiceImpl.java
@@ -854,9 +854,7 @@ public class ImportServiceImpl implements ImportService {
             this.properties = properties;
         }
 
-        public UUID getEntityUuid() {
-            return entityUuid;
-        }
+
 
         // Creates entities
         @Override
@@ -865,6 +863,7 @@ public class ImportServiceImpl implements ImportService {
                 logger.debug("Writing imported entity {}:{} into app {}",
                     new Object[]{entityType, entityUuid, em.getApplication().getUuid()});
 
+
                 em.create(entityUuid, entityType, properties);
 
                 tracker.entityWritten();
@@ -935,7 +934,6 @@ public class ImportServiceImpl implements ImportService {
         // adds map to the dictionary
         @Override
         public void doWrite(EntityManager em, FileImport fileImport, FileImportTracker stats) {
-            EntityManager rootEm = emf.getEntityManager(CpNamingUtils.MANAGEMENT_APPLICATION_ID);
             try {
 
                 logger.debug("Adding map to {}:{} dictionary {}",
@@ -944,24 +942,27 @@ public class ImportServiceImpl implements ImportService {
                 em.addMapToDictionary(ownerEntityRef, dictionaryName, dictionary);
 
             } catch (Exception e) {
-                logger.error("Error writing dictionary", e);
-                fileImport.setErrorMessage(e.getMessage());
-                try {
-
-                    rootEm.update(fileImport);
-
-                } catch (Exception ex) {
 
-                    // TODO should we abort at this point?
-                    logger.error("Error updating file import report with error message: "
-                        + fileImport.getErrorMessage(), ex);
-                }
+                //TODO add statistics for dictionary writes and failures
+//                logger.error("Error writing dictionary", e);
+//                fileImport.setErrorMessage(e.getMessage());
+//                try {
+//
+//                    rootEm.update(fileImport);
+//
+//                } catch (Exception ex) {
+//
+//                    // TODO should we abort at this point?
+//                    logger.error("Error updating file import report with error message: "
+//                        + fileImport.getErrorMessage(), ex);
+//                }
             }
         }
     }
 
 
     private final class JsonEntityParserObservable implements Observable.OnSubscribe<WriteEvent> {
+        public static final String COLLECTION_OBJECT_NAME = "collections";
         private final JsonParser jp;
         EntityManager em;
         EntityManager rootEm;
@@ -993,85 +994,115 @@ public class ImportServiceImpl implements ImportService {
         private void process(final Subscriber<? super WriteEvent> subscriber) {
 
             try {
-                boolean done = false;
 
-                // we ignore imported entity type information, entities get the type of the collection
-                String collectionType = InflectionUtils.singularize( fileImport.getCollectionName() );
 
-                Stack tokenStack = new Stack();
+                // we ignore imported entity type information, entities get the type of the collection
+                Stack<JsonToken> objectStartStack = new Stack();
+                Stack<String> objectNameStack = new Stack();
                 EntityRef lastEntity = null;
 
-                while (!done) {
+//                String collectionName = null;
+                String entityType = null;
+
+                while ( true ) {
+
 
                     JsonToken token = jp.nextToken();
-                    String name = jp.getCurrentName();
 
-                    String indent = "";
-                    for (int i = 0; i < tokenStack.size(); i++) {
-                        indent += "   ";
+                    //nothing left to do.
+                    if ( token == null ) {
+                        break;
                     }
 
-                    //logger.debug("{}Token {} name {}", new Object[]{indent, token, name});
+                    String name = jp.getCurrentName();
 
-                    if (token.equals(JsonToken.START_OBJECT) && "Metadata".equals(name)) {
 
-                        Map<String, Object> entityMap = jp.readValueAs(HashMap.class);
+                    //start of an object with a field name
 
-                        UUID uuid = UUID.fromString((String) entityMap.get("uuid"));
-                        lastEntity = new SimpleEntityRef(collectionType, uuid);
+                    if ( token.equals( JsonToken.START_OBJECT ) ) {
 
-                        if (entitiesOnly) {
-                            //logger.debug("{}Got entity with uuid {}", indent, lastEntity);
+                        objectStartStack.push( token );
 
-                            WriteEvent event = new EntityEvent(uuid, collectionType, entityMap);
-                            subscriber.onNext(event);
+                        //nothing to do
+                        if ( name == null ) {
+                            continue;
                         }
 
-                    } else if (token.equals(JsonToken.START_OBJECT) && "connections".equals(name)) {
 
-                        Map<String, Object> connectionMap = jp.readValueAs(HashMap.class);
+                        if ( "Metadata".equals( name ) ) {
 
-                        for (String type : connectionMap.keySet()) {
-                            List targets = (List) connectionMap.get(type);
 
-                            for (Object targetObject : targets) {
-                                UUID target = UUID.fromString((String) targetObject);
+                            Map<String, Object> entityMap = jp.readValueAs( HashMap.class );
 
-                                if (!entitiesOnly) {
-                                    //logger.debug("{}Got connection {} to {}",
+                            UUID uuid = UUID.fromString( ( String ) entityMap.get( "uuid" ) );
+                            lastEntity = new SimpleEntityRef( entityType, uuid );
+
+                            if ( entitiesOnly ) {
+                                //logger.debug("{}Got entity with uuid {}", indent, lastEntity);
+
+                                WriteEvent event = new EntityEvent( uuid, entityType, entityMap );
+                                subscriber.onNext( event );
+                            }
+
+                            objectStartStack.pop();
+                        }
+                        else if ( "connections".equals( name ) ) {
+
+
+                            Map<String, Object> connectionMap = jp.readValueAs( HashMap.class );
+
+                            for ( String type : connectionMap.keySet() ) {
+                                List targets = ( List ) connectionMap.get( type );
+
+                                for ( Object targetObject : targets ) {
+                                    UUID target = UUID.fromString( ( String ) targetObject );
+
+                                    if ( !entitiesOnly ) {
+                                        //logger.debug("{}Got connection {} to {}",
                                         //new Object[]{indent, type, target.toString()});
 
-                                    EntityRef entryRef = new SimpleEntityRef(target);
-                                    WriteEvent event = new ConnectionEvent(lastEntity, type, entryRef);
-                                    subscriber.onNext(event);
+                                        EntityRef entryRef = new SimpleEntityRef( target );
+                                        WriteEvent event = new ConnectionEvent( lastEntity, type, entryRef );
+                                        subscriber.onNext( event );
+                                    }
                                 }
                             }
+
+                            objectStartStack.pop();
                         }
+                        else if ( "dictionaries".equals( name ) ) {
 
-                    } else if (token.equals(JsonToken.START_OBJECT) && "dictionaries".equals(name)) {
 
-                        Map<String, Object> dictionariesMap = jp.readValueAs(HashMap.class);
-                        for (String dname : dictionariesMap.keySet()) {
-                            Map dmap = (Map) dictionariesMap.get(dname);
+                            Map<String, Object> dictionariesMap = jp.readValueAs( HashMap.class );
+                            for ( String dname : dictionariesMap.keySet() ) {
+                                Map dmap = ( Map ) dictionariesMap.get( dname );
 
-                            if (!entitiesOnly) {
-                                //logger.debug("{}Got dictionary {} size {}",
+                                if ( !entitiesOnly ) {
+                                    //logger.debug("{}Got dictionary {} size {}",
                                     //new Object[] {indent, dname, dmap.size() });
 
-                                WriteEvent event = new DictionaryEvent(lastEntity, dname, dmap);
-                                subscriber.onNext(event);
+                                    WriteEvent event = new DictionaryEvent( lastEntity, dname, dmap );
+                                    subscriber.onNext( event );
+                                }
                             }
+
+                            objectStartStack.pop();
+                        }
+                        //push onto object names we don't immediately understand.  Used for parent detection
+                        else{
+                            objectNameStack.push( name );
                         }
 
-                    } else if (token.equals(JsonToken.START_OBJECT)) {
-                        tokenStack.push(token);
+                    }
 
-                    } else if (token.equals(JsonToken.END_OBJECT)) {
-                        tokenStack.pop();
+                    else if (token.equals( JsonToken.START_ARRAY )){
+                         if( objectNameStack.size() == 1 && COLLECTION_OBJECT_NAME.equals( objectNameStack.peek() )) {
+                            entityType = InflectionUtils.singularize( name );
+                         }
                     }
 
-                    if (token.equals(JsonToken.END_ARRAY) && tokenStack.isEmpty()) {
-                        done = true;
+                    else if ( token.equals( JsonToken.END_OBJECT ) ) {
+                        objectStartStack.pop();
                     }
                 }
 

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/f977a89b/stack/services/src/test/java/org/apache/usergrid/management/importer/ImportCollectionIT.java
----------------------------------------------------------------------
diff --git a/stack/services/src/test/java/org/apache/usergrid/management/importer/ImportCollectionIT.java b/stack/services/src/test/java/org/apache/usergrid/management/importer/ImportCollectionIT.java
index ef335c8..9fdb001 100644
--- a/stack/services/src/test/java/org/apache/usergrid/management/importer/ImportCollectionIT.java
+++ b/stack/services/src/test/java/org/apache/usergrid/management/importer/ImportCollectionIT.java
@@ -285,46 +285,7 @@ public class ImportCollectionIT {
     }
 
 
-    /**
-     * Test that the types of i* ncoming entities is ignored.
-     */
-    @Test
-    public void testImportWithWrongTypes() throws Exception {
-
-        deleteBucket();
-
-        try {
-
-            // create an app with a collection of cats, export it to S3
-
-            String appName = "import-test-" + RandomStringUtils.randomAlphanumeric(10);
-            UUID appId = setup.getMgmtSvc().createApplication(organization.getUuid(), appName).getId();
-
-            Map<UUID, Entity> catsMap = new HashMap<>();
-            List<Entity> cats = new ArrayList<>();
-
-            EntityManager emApp = setup.getEmf().getEntityManager(appId);
-            createTestEntities(emApp, catsMap, cats, "cat");
-            exportCollection(emApp, "cats");
-
-            // import the cats data into a new collection called dogs in the default test app
 
-            final EntityManager emDefaultApp = setup.getEmf().getEntityManager(applicationId);
-            importCollection(emDefaultApp, "dogs");
-
-            // check that we now have a collection of dogs in the default test app
-
-            List<Entity> importedThings = emDefaultApp.getCollection(
-                emDefaultApp.getApplicationId(), "dogs", null, Level.ALL_PROPERTIES).getEntities();
-
-            assertTrue(!importedThings.isEmpty());
-            assertEquals(10, importedThings.size());
-
-        } finally {
-            deleteBucket();
-        }
-
-    }
 
 
    /**