You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@atlas.apache.org by ma...@apache.org on 2016/11/28 19:39:01 UTC

incubator-atlas git commit: ATLAS-1335: multi-value attribute handling in AtlasStructType to be consistent with TypeSystem for backward compatibility

Repository: incubator-atlas
Updated Branches:
  refs/heads/master 5d8c80a75 -> 20aa6cf25


ATLAS-1335: multi-value attribute handling in AtlasStructType to be consistent with TypeSystem for backward compatibility


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

Branch: refs/heads/master
Commit: 20aa6cf2536b4ca7c8fbf3fde285bb545898659c
Parents: 5d8c80a
Author: Madhan Neethiraj <ma...@apache.org>
Authored: Fri Nov 25 20:15:13 2016 -0800
Committer: Madhan Neethiraj <ma...@apache.org>
Committed: Mon Nov 28 10:00:27 2016 -0800

----------------------------------------------------------------------
 .../java/org/apache/atlas/AtlasErrorCode.java   |  1 +
 .../atlas/model/typedef/AtlasStructDef.java     |  2 +-
 .../org/apache/atlas/type/AtlasArrayType.java   |  8 +++--
 .../org/apache/atlas/type/AtlasStructType.java  | 12 +++++--
 .../apache/atlas/type/TestAtlasStructType.java  | 35 +++++++++++++++++---
 5 files changed, 47 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/20aa6cf2/intg/src/main/java/org/apache/atlas/AtlasErrorCode.java
----------------------------------------------------------------------
diff --git a/intg/src/main/java/org/apache/atlas/AtlasErrorCode.java b/intg/src/main/java/org/apache/atlas/AtlasErrorCode.java
index 65726c3..84d8173 100644
--- a/intg/src/main/java/org/apache/atlas/AtlasErrorCode.java
+++ b/intg/src/main/java/org/apache/atlas/AtlasErrorCode.java
@@ -56,6 +56,7 @@ public enum AtlasErrorCode {
     TYPE_HAS_REFERENCES(409, "ATLAS4092E", "Given type {0} has references"),
     TYPE_MATCH_FAILED(409, "ATLAS4093E", "Given type {0} doesn't match {1}"),
     INVALID_TYPE_DEFINITION(409, "ATLAS4094E", "Invalid type definition {0}"),
+    INVALID_ATTRIBUTE_TYPE_FOR_CARDINALITY(409, "ATLAS4095E", "Cardinality of attribute {0}.{1} requires a list or set type"),
 
     INTERNAL_ERROR(500, "ATLAS5001E", "Internal server error {0}"),
     INDEX_CREATION_FAILED(500, "ATLAS5002E", "Index creation failed for {0}"),

http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/20aa6cf2/intg/src/main/java/org/apache/atlas/model/typedef/AtlasStructDef.java
----------------------------------------------------------------------
diff --git a/intg/src/main/java/org/apache/atlas/model/typedef/AtlasStructDef.java b/intg/src/main/java/org/apache/atlas/model/typedef/AtlasStructDef.java
index ef12896..16e0344 100644
--- a/intg/src/main/java/org/apache/atlas/model/typedef/AtlasStructDef.java
+++ b/intg/src/main/java/org/apache/atlas/model/typedef/AtlasStructDef.java
@@ -280,7 +280,7 @@ public class AtlasStructDef extends AtlasBaseTypeDef implements Serializable {
         public AtlasAttributeDef() { this(null, null); }
 
         public AtlasAttributeDef(String name, String typeName) {
-            this(name, typeName, false, Cardinality.SINGLE, 1, 1, false, false, null);
+            this(name, typeName, false, Cardinality.SINGLE, COUNT_NOT_SET, COUNT_NOT_SET, false, false, null);
         }
 
         public AtlasAttributeDef(String name, String typeName, boolean isOptional, Cardinality cardinality,

http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/20aa6cf2/intg/src/main/java/org/apache/atlas/type/AtlasArrayType.java
----------------------------------------------------------------------
diff --git a/intg/src/main/java/org/apache/atlas/type/AtlasArrayType.java b/intg/src/main/java/org/apache/atlas/type/AtlasArrayType.java
index 156ed43..48d0a27 100644
--- a/intg/src/main/java/org/apache/atlas/type/AtlasArrayType.java
+++ b/intg/src/main/java/org/apache/atlas/type/AtlasArrayType.java
@@ -39,8 +39,8 @@ public class AtlasArrayType extends AtlasType {
     private static final Logger LOG = LoggerFactory.getLogger(AtlasArrayType.class);
 
     private final String elementTypeName;
-    private final int    minCount;
-    private final int    maxCount;
+    private int          minCount;
+    private int          maxCount;
 
     private AtlasType elementType;
 
@@ -89,10 +89,14 @@ public class AtlasArrayType extends AtlasType {
         return elementTypeName;
     }
 
+    public void setMinCount(int minCount) { this.minCount = minCount; }
+
     public int getMinCount() {
         return minCount;
     }
 
+    public void setMaxCount(int maxCount) { this.maxCount = maxCount; }
+
     public int getMaxCount() {
         return maxCount;
     }

http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/20aa6cf2/intg/src/main/java/org/apache/atlas/type/AtlasStructType.java
----------------------------------------------------------------------
diff --git a/intg/src/main/java/org/apache/atlas/type/AtlasStructType.java b/intg/src/main/java/org/apache/atlas/type/AtlasStructType.java
index 534f2a9..ef5f736 100644
--- a/intg/src/main/java/org/apache/atlas/type/AtlasStructType.java
+++ b/intg/src/main/java/org/apache/atlas/type/AtlasStructType.java
@@ -111,9 +111,15 @@ public class AtlasStructType extends AtlasType {
             Cardinality cardinality = attributeDef.getCardinality();
 
             if (cardinality == Cardinality.LIST || cardinality == Cardinality.SET) {
-                attrType = new AtlasArrayType(attrType,
-                                              attributeDef.getValuesMinCount(),
-                                              attributeDef.getValuesMaxCount());
+                if (!(attrType instanceof AtlasArrayType)) {
+                    throw new AtlasBaseException(AtlasErrorCode.INVALID_ATTRIBUTE_TYPE_FOR_CARDINALITY,
+                                                 getTypeName(), attributeDef.getName());
+                }
+
+                AtlasArrayType arrayType = (AtlasArrayType)attrType;
+
+                arrayType.setMinCount(attributeDef.getValuesMinCount());
+                arrayType.setMaxCount(attributeDef.getValuesMaxCount());
             }
 
             a.put(attributeDef.getName(), attrType);

http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/20aa6cf2/intg/src/test/java/org/apache/atlas/type/TestAtlasStructType.java
----------------------------------------------------------------------
diff --git a/intg/src/test/java/org/apache/atlas/type/TestAtlasStructType.java b/intg/src/test/java/org/apache/atlas/type/TestAtlasStructType.java
index 1a15232..ee05ba3 100644
--- a/intg/src/test/java/org/apache/atlas/type/TestAtlasStructType.java
+++ b/intg/src/test/java/org/apache/atlas/type/TestAtlasStructType.java
@@ -22,6 +22,7 @@ import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 
+import org.apache.atlas.AtlasErrorCode;
 import org.apache.atlas.exception.AtlasBaseException;
 import org.apache.atlas.model.ModelTestUtil;
 import org.apache.atlas.model.instance.AtlasStruct;
@@ -29,6 +30,7 @@ import static org.apache.atlas.model.typedef.AtlasBaseTypeDef.ATLAS_TYPE_INT;
 import static org.apache.atlas.model.typedef.AtlasBaseTypeDef.ATLAS_TYPE_DATE;
 import static org.apache.atlas.model.typedef.AtlasBaseTypeDef.ATLAS_TYPE_BIGINTEGER;
 
+import org.apache.atlas.model.typedef.AtlasBaseTypeDef;
 import org.apache.atlas.model.typedef.AtlasStructDef;
 import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef;
 import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef.Cardinality;
@@ -49,18 +51,24 @@ public class TestAtlasStructType {
     private final List<Object>    invalidValues;
 
     {
-        AtlasAttributeDef multiValuedAttribMinMax = new AtlasAttributeDef(MULTI_VAL_ATTR_NAME_MIN_MAX, ATLAS_TYPE_INT);
-        AtlasAttributeDef multiValuedAttribMin    = new AtlasAttributeDef(MULTI_VAL_ATTR_NAME_MIN, ATLAS_TYPE_INT);
-        AtlasAttributeDef multiValuedAttribMax    = new AtlasAttributeDef(MULTI_VAL_ATTR_NAME_MAX, ATLAS_TYPE_INT);
+        AtlasAttributeDef multiValuedAttribMinMax = new AtlasAttributeDef();
+        AtlasAttributeDef multiValuedAttribMin    = new AtlasAttributeDef();
+        AtlasAttributeDef multiValuedAttribMax    = new AtlasAttributeDef();
 
+        multiValuedAttribMinMax.setName(MULTI_VAL_ATTR_NAME_MIN_MAX);
+        multiValuedAttribMinMax.setTypeName(AtlasBaseTypeDef.getArrayTypeName(ATLAS_TYPE_INT));
         multiValuedAttribMinMax.setCardinality(Cardinality.LIST);
         multiValuedAttribMinMax.setValuesMinCount(MULTI_VAL_ATTR_MIN_COUNT);
         multiValuedAttribMinMax.setValuesMaxCount(MULTI_VAL_ATTR_MAX_COUNT);
 
-        multiValuedAttribMinMax.setCardinality(Cardinality.SET);
+        multiValuedAttribMin.setName(MULTI_VAL_ATTR_NAME_MIN);
+        multiValuedAttribMin.setTypeName(AtlasBaseTypeDef.getArrayTypeName(ATLAS_TYPE_INT));
+        multiValuedAttribMin.setCardinality(Cardinality.SET);
         multiValuedAttribMin.setValuesMinCount(MULTI_VAL_ATTR_MIN_COUNT);
 
-        multiValuedAttribMinMax.setCardinality(Cardinality.LIST);
+        multiValuedAttribMax.setName(MULTI_VAL_ATTR_NAME_MAX);
+        multiValuedAttribMax.setTypeName(AtlasBaseTypeDef.getArrayTypeName(ATLAS_TYPE_INT));
+        multiValuedAttribMax.setCardinality(Cardinality.LIST);
         multiValuedAttribMax.setValuesMaxCount(MULTI_VAL_ATTR_MAX_COUNT);
 
         AtlasStructDef structDef = ModelTestUtil.newStructDef();
@@ -173,6 +181,23 @@ public class TestAtlasStructType {
         }
     }
 
+    @Test
+    public void testInvalidStructDef_MultiValuedAttributeNotArray() {
+        AtlasAttributeDef invalidMultiValuedAttrib = new AtlasAttributeDef("invalidAttributeDef", ATLAS_TYPE_INT);
+        invalidMultiValuedAttrib.setCardinality(Cardinality.LIST);
+
+        AtlasStructDef invalidStructDef = ModelTestUtil.newStructDef();
+        invalidStructDef.addAttribute(invalidMultiValuedAttrib);
+
+        try {
+            AtlasStructType invalidStructType = new AtlasStructType(invalidStructDef, ModelTestUtil.getTypesRegistry());
+
+            fail("invalidStructDef not detected: structDef=" + invalidStructDef + "; structType=" + invalidStructType);
+        } catch (AtlasBaseException excp) {
+            assertTrue(excp.getAtlasErrorCode() == AtlasErrorCode.INVALID_ATTRIBUTE_TYPE_FOR_CARDINALITY);
+        }
+    }
+
     private static AtlasStructType getStructType(AtlasStructDef structDef) {
         try {
             return new AtlasStructType(structDef, ModelTestUtil.getTypesRegistry());