Repository: atlas
Updated Branches:
  refs/heads/branch-0.8 fdecadd1d -> 917a0c09c


ATLAS-2325: fix to prevent duplicate elements in SET type attributes

(cherry picked from commit d5b438b5db08cc4ecff69912b425104cd7ecd924)


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

Branch: refs/heads/branch-0.8
Commit: 917a0c09c4dddfe9e9a23322172e4213c6445455
Parents: fdecadd
Author: Madhan Neethiraj <[email protected]>
Authored: Tue Jul 31 16:57:31 2018 -0700
Committer: Madhan Neethiraj <[email protected]>
Committed: Tue Jul 31 16:58:21 2018 -0700

----------------------------------------------------------------------
 .../store/graph/v1/EntityGraphMapper.java       | 25 +++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/atlas/blob/917a0c09/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/EntityGraphMapper.java
----------------------------------------------------------------------
diff --git 
a/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/EntityGraphMapper.java
 
b/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/EntityGraphMapper.java
index 682c7bf..25c91be 100644
--- 
a/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/EntityGraphMapper.java
+++ 
b/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/EntityGraphMapper.java
@@ -31,6 +31,7 @@ import org.apache.atlas.model.instance.AtlasStruct;
 import org.apache.atlas.model.instance.EntityMutationResponse;
 import org.apache.atlas.model.instance.EntityMutations.EntityOperation;
 import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef;
+import 
org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef.Cardinality;
 import org.apache.atlas.repository.Constants;
 import org.apache.atlas.repository.RepositoryException;
 import org.apache.atlas.repository.graph.GraphHelper;
@@ -571,9 +572,14 @@ public class EntityGraphMapper {
         List<Object>   currentElements = getArrayElementsProperty(elementType, 
ctx.getReferringVertex(), ctx.getVertexProperty());
         boolean isReference = AtlasGraphUtilsV1.isReference(elementType);
         AtlasAttribute inverseRefAttribute = 
attribute.getInverseRefAttribute();
+        Cardinality cardinality         = 
attribute.getAttributeDef().getCardinality();
         List<Object> newElementsCreated = new ArrayList<>();
 
         if (CollectionUtils.isNotEmpty(newElements)) {
+            if (cardinality == Cardinality.SET) {
+                newElements = getUniqueElementsList(newElements);
+            }
+
             for (int index = 0; index < newElements.size(); index++) {
                 AtlasEdge               existingEdge = 
getEdgeAt(currentElements, index, elementType);
                 AttributeMutationContext arrCtx      = new 
AttributeMutationContext(ctx.getOp(), ctx.getReferringVertex(), 
ctx.getAttribute(), newElements.get(index),
@@ -604,7 +610,6 @@ public class EntityGraphMapper {
         return newElementsCreated;
     }
 
-
     private AtlasEdge createVertex(AtlasStruct struct, AtlasVertex 
referringVertex, String edgeLabel, EntityMutationContext context) throws 
AtlasBaseException {
         AtlasVertex vertex = createStructVertex(struct);
 
@@ -1038,4 +1043,22 @@ public class EntityGraphMapper {
             }
         }
     }
+
+    private List getUniqueElementsList(List list) {
+        final List ret;
+
+        if (CollectionUtils.isEmpty(list) || list.size() == 1) {
+            ret = list;
+        } else {
+            ret = new ArrayList(list.size());
+
+            for (Object elem : list) {
+                if (!ret.contains(elem)) {
+                    ret.add(elem);
+                }
+            }
+        }
+
+        return ret;
+    }
 }

Reply via email to