atlas-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mad...@apache.org
Subject incubator-atlas git commit: ATLAS-1522: v1 to v2 entity converter fixes
Date Sat, 04 Feb 2017 19:19:27 GMT
Repository: incubator-atlas
Updated Branches:
  refs/heads/master d8c2a10e0 -> 4f4ab9b98


ATLAS-1522: v1 to v2 entity converter fixes


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

Branch: refs/heads/master
Commit: 4f4ab9b98d2f0cfa9b6e9fddac0e993c2b1203c1
Parents: d8c2a10
Author: Madhan Neethiraj <madhan@apache.org>
Authored: Fri Feb 3 20:58:50 2017 -0800
Committer: Madhan Neethiraj <madhan@apache.org>
Committed: Fri Feb 3 20:58:50 2017 -0800

----------------------------------------------------------------------
 .../adapters/AtlasEntityFormatConverter.java    | 67 ++++++++++----------
 .../web/adapters/AtlasFormatConverter.java      | 25 ++------
 .../web/adapters/AtlasInstanceRestAdapters.java |  5 +-
 .../adapters/AtlasStructFormatConverter.java    | 15 ++---
 .../org/apache/atlas/web/rest/EntityREST.java   | 38 +++++------
 5 files changed, 69 insertions(+), 81 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/4f4ab9b9/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasEntityFormatConverter.java
----------------------------------------------------------------------
diff --git a/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasEntityFormatConverter.java
b/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasEntityFormatConverter.java
index 8f85052..6818899 100644
--- a/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasEntityFormatConverter.java
+++ b/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasEntityFormatConverter.java
@@ -54,7 +54,7 @@ public class AtlasEntityFormatConverter extends AtlasStructFormatConverter
{
 
     @Override
     public Object fromV1ToV2(Object v1Obj, AtlasType type, ConverterContext context) throws
AtlasBaseException {
-        Object ret = null;
+        AtlasObjectId ret = null;
 
         if (v1Obj != null) {
             AtlasEntityType entityType = (AtlasEntityType) type;
@@ -64,43 +64,46 @@ public class AtlasEntityFormatConverter extends AtlasStructFormatConverter
{
 
                 ret = new AtlasObjectId(id.getTypeName(), id._getId());
             } else if (v1Obj instanceof IReferenceableInstance) {
-                IReferenceableInstance entity    = (IReferenceableInstance) v1Obj;
-                Map<String, Object>    v1Attribs = null;
+                IReferenceableInstance entRef = (IReferenceableInstance) v1Obj;
 
-                ret = new AtlasObjectId(entity.getTypeName(), entity.getId()._getId());
+                ret = new AtlasObjectId(entRef.getTypeName(), entRef.getId()._getId());
 
-                try {
-                    v1Attribs = entity.getValuesMap();
-                } catch (AtlasException excp) {
-                    LOG.error("IReferenceableInstance.getValuesMap() failed", excp);
-                }
+                if (!context.entityExists(ret.getGuid())) {
+                    Map<String, Object> v1Attribs = null;
 
-                AtlasEntityWithAssociations ret1 =  new AtlasEntityWithAssociations(entity.getTypeName(),
super.fromV1ToV2(entityType, v1Attribs, context));
-                ret1.setGuid(entity.getId()._getId());
-                ret1.setStatus(convertState(entity.getId().getState()));
-                AtlasSystemAttributes systemAttributes = entity.getSystemAttributes();
-                ret1.setCreatedBy(systemAttributes.createdBy);
-                ret1.setCreateTime(systemAttributes.createdTime);
-                ret1.setUpdatedBy(systemAttributes.modifiedBy);
-                ret1.setUpdateTime(systemAttributes.modifiedTime);
-                ret1.setVersion(new Long(entity.getId().version));
-
-                if (CollectionUtils.isNotEmpty(entity.getTraits())) {
-                    List<AtlasClassification> classifications = new ArrayList<>();
-                    AtlasFormatConverter      traitConverter  = converterRegistry.getConverter(TypeCategory.CLASSIFICATION);
-
-                    for (String traitName : entity.getTraits()) {
-                        IStruct             trait          = entity.getTrait(traitName);
-                        AtlasType           classifiType   = typeRegistry.getType(traitName);
-                        AtlasClassification classification = (AtlasClassification) traitConverter.fromV1ToV2(trait,
classifiType, context);
-
-                        classifications.add(classification);
+                    try {
+                        v1Attribs = entRef.getValuesMap();
+                    } catch (AtlasException excp) {
+                        LOG.error("IReferenceableInstance.getValuesMap() failed", excp);
                     }
 
-                    ret1.setClassifications(classifications);
-                }
+                    AtlasEntityWithAssociations entity = new AtlasEntityWithAssociations(entRef.getTypeName(),
+                                                                      super.fromV1ToV2(entityType,
v1Attribs, context));
+                    entity.setGuid(entRef.getId()._getId());
+                    entity.setStatus(convertState(entRef.getId().getState()));
+                    entity.setCreatedBy(entRef.getSystemAttributes().createdBy);
+                    entity.setCreateTime(entRef.getSystemAttributes().createdTime);
+                    entity.setUpdatedBy(entRef.getSystemAttributes().modifiedBy);
+                    entity.setUpdateTime(entRef.getSystemAttributes().modifiedTime);
+                    entity.setVersion(new Long(entRef.getId().version));
+
+                    if (CollectionUtils.isNotEmpty(entRef.getTraits())) {
+                        List<AtlasClassification> classifications = new ArrayList<>();
+                        AtlasFormatConverter traitConverter = converterRegistry.getConverter(TypeCategory.CLASSIFICATION);
+
+                        for (String traitName : entRef.getTraits()) {
+                            IStruct trait = entRef.getTrait(traitName);
+                            AtlasType classifiType = typeRegistry.getType(traitName);
+                            AtlasClassification classification = (AtlasClassification) traitConverter.fromV1ToV2(trait,
classifiType, context);
+
+                            classifications.add(classification);
+                        }
+
+                        entity.setClassifications(classifications);
+                    }
 
-                context.addEntity(ret1);
+                    context.addEntity(entity);
+                }
             } else {
                 throw new AtlasBaseException(AtlasErrorCode.UNEXPECTED_TYPE, "IReferenceableInstance",
                                              v1Obj.getClass().getCanonicalName());

http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/4f4ab9b9/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasFormatConverter.java
----------------------------------------------------------------------
diff --git a/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasFormatConverter.java
b/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasFormatConverter.java
index 1272543..a4799e8 100644
--- a/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasFormatConverter.java
+++ b/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasFormatConverter.java
@@ -46,18 +46,14 @@ public interface AtlasFormatConverter {
         }
 
         public void addEntity(AtlasEntity entity) {
-            if (entities == null) {
-                entities = new HashMap<>();
+            if (entity instanceof AtlasEntityWithAssociations) {
+                this.addEntity((AtlasEntityWithAssociations)entity);
+            } else {
+                this.addEntity(new AtlasEntityWithAssociations(entity));
             }
-            entities.put(entity.getGuid(), new AtlasEntityWithAssociations(entity));
         }
 
-        public boolean exists(AtlasEntityWithAssociations entity) {
-            return entities != null ? entities.containsKey(entity.getGuid()) : false;
-        }
-
-        public AtlasEntity getById(String guid) {
-
+        public AtlasEntityWithAssociations getById(String guid) {
             if( entities != null) {
                 return entities.get(guid);
             }
@@ -65,17 +61,10 @@ public interface AtlasFormatConverter {
             return null;
         }
 
+        public boolean entityExists(String guid) { return entities != null && entities.containsKey(guid);
}
+
         public Map<String, AtlasEntityWithAssociations> getEntities() {
             return entities;
         }
-
-        public void addEntities(Map<String, AtlasEntity> entities) {
-            if (this.entities == null) {
-                this.entities = new HashMap<>(entities.size());
-            }
-            for (String entityId : entities.keySet()) {
-                this.entities.put(entityId, new AtlasEntityWithAssociations(entities.get(entityId)));
-            }
-        }
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/4f4ab9b9/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasInstanceRestAdapters.java
----------------------------------------------------------------------
diff --git a/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasInstanceRestAdapters.java
b/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasInstanceRestAdapters.java
index 692f917..7f5a056 100644
--- a/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasInstanceRestAdapters.java
+++ b/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasInstanceRestAdapters.java
@@ -129,13 +129,16 @@ public class AtlasInstanceRestAdapters {
     public Map<String, AtlasEntityWithAssociations> getAtlasEntity(IReferenceableInstance
referenceable) throws AtlasBaseException {
 
         AtlasFormatConverter converter  = instanceFormatters.getConverter(TypeCategory.ENTITY);
-        AtlasEntityType entityType = typeRegistry.getEntityTypeByName(referenceable.getTypeName());
+        AtlasEntityType      entityType = typeRegistry.getEntityTypeByName(referenceable.getTypeName());
+
         if (entityType == null) {
             throw new AtlasBaseException(AtlasErrorCode.TYPE_NAME_INVALID, TypeCategory.ENTITY.name(),
referenceable.getTypeName());
         }
 
         AtlasFormatConverter.ConverterContext ctx = new AtlasFormatConverter.ConverterContext();
+
         converter.fromV1ToV2(referenceable, entityType, ctx);
+
         return ctx.getEntities();
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/4f4ab9b9/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasStructFormatConverter.java
----------------------------------------------------------------------
diff --git a/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasStructFormatConverter.java
b/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasStructFormatConverter.java
index 4d2e123..f486cda 100644
--- a/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasStructFormatConverter.java
+++ b/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasStructFormatConverter.java
@@ -157,24 +157,17 @@ public class AtlasStructFormatConverter extends AtlasAbstractFormatConverter
{
             ret = new HashMap<>();
 
             for (AtlasStructType.AtlasAttribute attr : structType.getAllAttributes().values())
{
-                AtlasType            attrType      = attr.getAttributeType();
+                AtlasType attrType = attr.getAttributeType();
 
                 if (attrType == null) {
                     LOG.warn("ignored attribute {}.{}: failed to find AtlasType", structType.getTypeName(),
attr.getName());
                     continue;
                 }
 
-                Object v1Value = attributes.get(attr.getName());
-                Object v2Value = null;
+                AtlasFormatConverter attrConverter = converterRegistry.getConverter(attrType.getTypeCategory());
+                Object               v1Value       = attributes.get(attr.getName());
+                Object               v2Value       = attrConverter.fromV1ToV2(v1Value, attrType,
context);
 
-                AtlasFormatConverter attrConverter = null;
-                if (attrType.getTypeCategory() == TypeCategory.ENTITY && !attr.isContainedAttribute())
{
-                    attrConverter = new AtlasObjectIdConverter(converterRegistry, typeRegistry);
-                    v2Value = attrConverter.fromV1ToV2(v1Value, attrType, context);
-                } else {
-                    attrConverter = converterRegistry.getConverter(attrType.getTypeCategory());
-                    v2Value = attrConverter.fromV1ToV2(v1Value, attrType, context);
-                }
                 ret.put(attr.getAttributeDef().getName(), v2Value);
             }
         }

http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/4f4ab9b9/webapp/src/main/java/org/apache/atlas/web/rest/EntityREST.java
----------------------------------------------------------------------
diff --git a/webapp/src/main/java/org/apache/atlas/web/rest/EntityREST.java b/webapp/src/main/java/org/apache/atlas/web/rest/EntityREST.java
index d82e5ba..9c0ccf6 100644
--- a/webapp/src/main/java/org/apache/atlas/web/rest/EntityREST.java
+++ b/webapp/src/main/java/org/apache/atlas/web/rest/EntityREST.java
@@ -84,13 +84,11 @@ public class EntityREST {
     @Path("/guid/{guid}")
     @Produces(Servlets.JSON_MEDIA_TYPE)
     public List<AtlasEntityWithAssociations> getById(@PathParam("guid") String guid)
throws AtlasBaseException {
-        List<AtlasEntityWithAssociations> entityList = new ArrayList<>();
-
         try {
-            ITypedReferenceableInstance ref = metadataService.getEntityDefinition(guid);
-            Map<String, AtlasEntityWithAssociations> entityRet = restAdapters.getAtlasEntity(ref);
-            entityList.addAll(entityRet.values());
-            return entityList;
+            ITypedReferenceableInstance              ref      = metadataService.getEntityDefinition(guid);
+            Map<String, AtlasEntityWithAssociations> entities = restAdapters.getAtlasEntity(ref);
+
+            return getOrderedEntityList(entities, guid);
         } catch (AtlasException e) {
             throw toAtlasBaseException(e);
         }
@@ -106,20 +104,9 @@ public class EntityREST {
     @Path("/guid/{guid}/associations")
     @Produces(Servlets.JSON_MEDIA_TYPE)
     public List<AtlasEntityWithAssociations> getWithAssociationsByGuid(@PathParam("guid")
String guid) throws AtlasBaseException {
-
-        List<AtlasEntityWithAssociations> entityList = new ArrayList<>();
-        try {
-            ITypedReferenceableInstance ref = metadataService.getEntityDefinition(guid);
-            Map<String, AtlasEntityWithAssociations> entityRet = restAdapters.getAtlasEntity(ref);
-            entityList.addAll(entityRet.values());
-            return entityList;
-        } catch (AtlasException e) {
-            throw toAtlasBaseException(e);
-        }
+        return this.getById(guid);
     }
 
-
-
     /**
      * Delete an entity identified by its GUID
      *
@@ -212,7 +199,6 @@ public class EntityREST {
         return entityList;
     }
 
-
     /**
      * Gets the list of classifications for a given entity represented by a guid.
      *
@@ -373,4 +359,18 @@ public class EntityREST {
             throw new AtlasBaseException(AtlasErrorCode.ATTRIBUTE_UNIQUE_INVALID, entityType.getTypeName(),
attributeName);
         }
     }
+
+    private List<AtlasEntityWithAssociations> getOrderedEntityList(Map<String, AtlasEntityWithAssociations>
entities, String firstItemGuid) {
+        List<AtlasEntityWithAssociations> ret = new ArrayList<>(entities.size());
+
+        for (AtlasEntityWithAssociations entity : entities.values()) {
+            if (StringUtils.equals(entity.getGuid(), firstItemGuid)) {
+                ret.add(0, entity);
+            } else {
+                ret.add(entity);
+            }
+        }
+
+        return ret;
+    }
 }


Mime
View raw message