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/09 23:12:05 UTC
[2/5] incubator-usergrid git commit: Changed format to work with new
file format.
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
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();
- }
-
- }
/**