ATLAS-1513: updated AtlasEntityType with methods to get foreign-key references; added helper methods in AtlasAttribute
Project: http://git-wip-us.apache.org/repos/asf/incubator-atlas/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-atlas/commit/5d8fae55 Tree: http://git-wip-us.apache.org/repos/asf/incubator-atlas/tree/5d8fae55 Diff: http://git-wip-us.apache.org/repos/asf/incubator-atlas/diff/5d8fae55 Branch: refs/heads/master Commit: 5d8fae555492cb53fc9ce33974184e471103b39c Parents: a7870cd Author: kevalbhatt <[email protected]> Authored: Wed Feb 1 18:32:31 2017 +0530 Committer: kevalbhatt <[email protected]> Committed: Wed Feb 1 18:32:31 2017 +0530 ---------------------------------------------------------------------- .../atlas/model/typedef/AtlasStructDef.java | 1 + .../atlas/type/AtlasClassificationType.java | 2 +- .../org/apache/atlas/type/AtlasEntityType.java | 188 +++++++++++++- .../org/apache/atlas/type/AtlasStructType.java | 253 ++++++++----------- .../org/apache/atlas/type/AtlasTypeUtil.java | 23 +- .../test/java/org/apache/atlas/TestUtilsV2.java | 97 +++---- .../org/apache/atlas/model/ModelTestUtil.java | 1 - .../apache/atlas/type/TestAtlasEntityType.java | 52 +++- release-log.txt | 2 + .../store/graph/v1/ArrayVertexMapper.java | 5 +- .../graph/v1/AtlasEntityGraphDiscoveryV1.java | 13 +- .../store/graph/v1/AtlasStructDefStoreV1.java | 22 +- .../store/graph/v1/DeleteHandlerV1.java | 36 +-- .../store/graph/v1/EntityGraphMapper.java | 7 +- .../store/graph/v1/GraphMutationContext.java | 3 +- .../store/graph/v1/MapVertexMapper.java | 8 +- .../store/graph/v1/StructVertexMapper.java | 9 +- .../graph/v1/UniqAttrBasedEntityResolver.java | 2 +- .../java/org/apache/atlas/util/RestUtils.java | 4 +- .../adapters/AtlasStructFormatConverter.java | 16 +- .../org/apache/atlas/web/rest/EntityREST.java | 7 +- .../web/resources/TypedefsJerseyResourceIT.java | 5 +- 22 files changed, 454 insertions(+), 302 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/5d8fae55/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 2c00f54..ff64415 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 @@ -470,6 +470,7 @@ public class AtlasStructDef extends AtlasBaseTypeDef implements Serializable { public static final String CONSTRAINT_PARAM_REF_ATTRIBUTE = "refAttribute"; public static final String CONSTRAINT_PARAM_ON_DELETE = "onDelete"; public static final String CONSTRAINT_PARAM_VAL_CASCADE = "cascade"; + public static final String CONSTRAINT_PARAM_VAL_UPDATE = "update"; private String type; // foreignKey/mappedFromRef/valueInRange private Map<String, Object> params; // onDelete=cascade/refAttribute=attr2/min=0,max=23 http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/5d8fae55/intg/src/main/java/org/apache/atlas/type/AtlasClassificationType.java ---------------------------------------------------------------------- diff --git a/intg/src/main/java/org/apache/atlas/type/AtlasClassificationType.java b/intg/src/main/java/org/apache/atlas/type/AtlasClassificationType.java index d06364c..f839328 100644 --- a/intg/src/main/java/org/apache/atlas/type/AtlasClassificationType.java +++ b/intg/src/main/java/org/apache/atlas/type/AtlasClassificationType.java @@ -260,7 +260,7 @@ public class AtlasClassificationType extends AtlasStructType { if (CollectionUtils.isNotEmpty(classificationDef.getAttributeDefs())) { for (AtlasAttributeDef attributeDef : classificationDef.getAttributeDefs()) { AtlasType type = typeRegistry.getType(attributeDef.getTypeName()); - allAttributes.put(attributeDef.getName(), new AtlasAttribute(this, classificationDef, attributeDef, type)); + allAttributes.put(attributeDef.getName(), new AtlasAttribute(this, attributeDef, type)); } } } http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/5d8fae55/intg/src/main/java/org/apache/atlas/type/AtlasEntityType.java ---------------------------------------------------------------------- diff --git a/intg/src/main/java/org/apache/atlas/type/AtlasEntityType.java b/intg/src/main/java/org/apache/atlas/type/AtlasEntityType.java index b83a241..fcd483c 100644 --- a/intg/src/main/java/org/apache/atlas/type/AtlasEntityType.java +++ b/intg/src/main/java/org/apache/atlas/type/AtlasEntityType.java @@ -20,16 +20,20 @@ package org.apache.atlas.type; import org.apache.atlas.AtlasErrorCode; import org.apache.atlas.exception.AtlasBaseException; +import org.apache.atlas.model.TypeCategory; import org.apache.atlas.model.instance.AtlasEntity; import org.apache.atlas.model.instance.AtlasObjectId; import org.apache.atlas.model.typedef.AtlasEntityDef; import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef; +import org.apache.atlas.model.typedef.AtlasStructDef.AtlasConstraintDef; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections.MapUtils; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -37,6 +41,8 @@ import java.util.List; import java.util.Map; import java.util.Set; +import static org.apache.atlas.model.typedef.AtlasStructDef.AtlasConstraintDef.*; + /** * class that implements behaviour of an entity-type. */ @@ -45,9 +51,11 @@ public class AtlasEntityType extends AtlasStructType { private final AtlasEntityDef entityDef; - private List<AtlasEntityType> superTypes = Collections.emptyList(); - private Set<String> allSuperTypes = Collections.emptySet(); - private Set<String> allSubTypes = Collections.emptySet(); + private List<AtlasEntityType> superTypes = Collections.emptyList(); + private Set<String> allSuperTypes = Collections.emptySet(); + private Set<String> allSubTypes = Collections.emptySet(); + private Map<String, AtlasAttribute> mappedFromRefAttributes = new HashMap<>(); + private List<ForeignKeyReference> foreignKeyReferences = Collections.emptyList(); public AtlasEntityType(AtlasEntityDef entityDef) { super(entityDef); @@ -88,23 +96,22 @@ public class AtlasEntityType extends AtlasStructType { this.superTypes = Collections.unmodifiableList(s); this.allSuperTypes = Collections.unmodifiableSet(allS); this.allAttributes = Collections.unmodifiableMap(allA); - this.allSubTypes = new HashSet<>(); // this will be populated in resolveReferencesPhase2() + this.allSubTypes = new HashSet<>(); // this will be populated in resolveReferencesPhase2() + this.foreignKeyReferences = new ArrayList<>(); // this will be populated in resolveReferencesPhase2() } @Override public void resolveReferencesPhase2(AtlasTypeRegistry typeRegistry) throws AtlasBaseException { super.resolveReferencesPhase2(typeRegistry); + mappedFromRefAttributes = Collections.unmodifiableMap(resolveMappedFromRefConstraint(allAttributes.values())); + for (String superTypeName : allSuperTypes) { AtlasEntityType superType = typeRegistry.getEntityTypeByName(superTypeName); superType.addSubType(this); } } - private void addSubType(AtlasEntityType subType) { - allSubTypes.add(subType.getTypeName()); - } - public Set<String> getSuperTypes() { return entityDef.getSuperTypes(); } @@ -113,8 +120,47 @@ public class AtlasEntityType extends AtlasStructType { return allSuperTypes; } - public Set<String> getAllSubTypes() { - return allSubTypes; + public Set<String> getAllSubTypes() { return Collections.unmodifiableSet(allSubTypes); } + + public Collection<String> getMappedFromRefAttributes() { return mappedFromRefAttributes.keySet(); } + + public boolean isMappedFromRefAttribute(String attributeName) { + return mappedFromRefAttributes.containsKey(attributeName); + } + + public String getMappedFromRefAttribute(String typeName, String attribName) { + String ret = null; + + for (Map.Entry<String, AtlasAttribute> e : mappedFromRefAttributes.entrySet()) { + AtlasAttribute attribute = e.getValue(); + + if(StringUtils.equals(attribute.getStructType().getTypeName(), typeName) && StringUtils.equals(attribute.getName(), attribName)) { + ret = e.getKey(); + + break; + } + } + + return ret; + } + + public List<ForeignKeyReference> getForeignKeyReferences() { + return Collections.unmodifiableList(foreignKeyReferences); + } + + public ForeignKeyReference getForeignKeyReference(String fromTypeName, String fromAttributeName) { + ForeignKeyReference ret = null; + + for (ForeignKeyReference fkRef : foreignKeyReferences) { + if (StringUtils.equals(fkRef.fromTypeName(), fromTypeName) && + StringUtils.equals(fkRef.fromAttributeName(), fromAttributeName)) { + ret = fkRef; + + break; + } + } + + return ret; } public boolean isSuperTypeOf(AtlasEntityType entityType) { @@ -184,7 +230,7 @@ public class AtlasEntityType extends AtlasStructType { @Override public AtlasAttribute getAttribute(String attributeName) { - return findAttribute(allAttributes.values(), attributeName); + return allAttributes.get(attributeName); } @Override @@ -238,6 +284,14 @@ public class AtlasEntityType extends AtlasStructType { } } + void addForeignKeyReference(AtlasAttribute attribute, AtlasConstraintDef refConstraint) { + foreignKeyReferences.add(new ForeignKeyReference(attribute, refConstraint)); + } + + private void addSubType(AtlasEntityType subType) { + allSubTypes.add(subType.getTypeName()); + } + private void getTypeHierarchyInfo(AtlasTypeRegistry typeRegistry, Set<String> allSuperTypeNames, Map<String, AtlasAttribute> allAttributes) throws AtlasBaseException { @@ -276,9 +330,80 @@ public class AtlasEntityType extends AtlasStructType { for (AtlasAttributeDef attributeDef : entityDef.getAttributeDefs()) { AtlasType type = typeRegistry.getType(attributeDef.getTypeName()); - allAttributes.put(attributeDef.getName(), new AtlasAttribute(this, entityDef, attributeDef, type)); + allAttributes.put(attributeDef.getName(), new AtlasAttribute(this, attributeDef, type)); + } + } + } + + /* + * valid conditions for mapped-from-ref constraint: + * - supported only in entity-type + * - attribute should be an entity-type or an array of entity-type + * - attribute's entity-type should have a foreign-key constraint to this type + */ + private Map<String, AtlasAttribute> resolveMappedFromRefConstraint(Collection<AtlasAttribute> attributes) throws AtlasBaseException { + Map<String, AtlasAttribute> ret = null; + + for (AtlasAttribute attribute : attributes) { + AtlasAttributeDef attribDef = attribute.getAttributeDef(); + + if (CollectionUtils.isEmpty(attribDef.getConstraintDefs())) { + continue; + } + + for (AtlasConstraintDef constraintDef : attribDef.getConstraintDefs()) { + if (!StringUtils.equals(constraintDef.getType(), CONSTRAINT_TYPE_MAPPED_FROM_REF)) { + continue; + } + + AtlasType attribType = attribute.getAttributeType(); + + if (attribType.getTypeCategory() == TypeCategory.ARRAY) { + attribType = ((AtlasArrayType)attribType).getElementType(); + } + + if (attribType.getTypeCategory() != TypeCategory.ENTITY) { + throw new AtlasBaseException(AtlasErrorCode.CONSTRAINT_NOT_SATISFIED, getTypeName(), + attribDef.getName(), CONSTRAINT_TYPE_MAPPED_FROM_REF, + attribDef.getTypeName()); + } + + String refAttribName = AtlasTypeUtil.getStringValue(constraintDef.getParams(), CONSTRAINT_PARAM_REF_ATTRIBUTE); + + if (StringUtils.isBlank(refAttribName)) { + throw new AtlasBaseException(AtlasErrorCode.CONSTRAINT_MISSING_PARAMS, + getTypeName(), attribDef.getName(), + CONSTRAINT_PARAM_REF_ATTRIBUTE, CONSTRAINT_TYPE_MAPPED_FROM_REF, + String.valueOf(constraintDef.getParams())); + } + + AtlasEntityType entityType = (AtlasEntityType) attribType; + AtlasAttribute refAttrib = entityType.getAttribute(refAttribName); + + if (refAttrib == null) { + throw new AtlasBaseException(AtlasErrorCode.CONSTRAINT_NOT_EXIST, + getTypeName(), attribDef.getName(), CONSTRAINT_PARAM_REF_ATTRIBUTE, + entityType.getTypeName(), refAttribName); + } + + if (!StringUtils.equals(getTypeName(), refAttrib.getTypeName())) { + throw new AtlasBaseException(AtlasErrorCode.CONSTRAINT_NOT_MATCHED, + getTypeName(), attribDef.getName(), CONSTRAINT_PARAM_REF_ATTRIBUTE, + entityType.getTypeName(), refAttribName, getTypeName(), + refAttrib.getTypeName()); + } + + if (ret == null) { + ret = new HashMap<>(); + } + + ret.put(attribDef.getName(), refAttrib); + + break; } } + + return ret == null ? Collections.<String, AtlasAttribute>emptyMap() : ret; } private boolean validateAtlasObjectId(AtlasObjectId objId) { @@ -293,4 +418,43 @@ public class AtlasEntityType extends AtlasStructType { return AtlasEntity.isAssigned(objId.getGuid()) || AtlasEntity.isUnAssigned((objId.getGuid())); } + public static class ForeignKeyReference { + private final AtlasAttribute fromAttribute; + private final AtlasConstraintDef refConstraint; + + public ForeignKeyReference(AtlasAttribute fromAttribute, AtlasConstraintDef refConstraint) { + this.fromAttribute = fromAttribute; + this.refConstraint = refConstraint; + } + + public String fromTypeName() { return fromType().getTypeName(); } + + public String fromAttributeName() { return fromAttribute.getName(); } + + public String toTypeName() { return fromAttribute.getTypeName(); } + + public AtlasStructType fromType() { return fromAttribute.getStructType(); } + + public AtlasAttribute fromAttribute() { return fromAttribute; } + + public AtlasEntityType toType() { return (AtlasEntityType)fromAttribute.getAttributeType(); } + + public AtlasConstraintDef getConstraint() { return refConstraint; } + + public boolean isOnDeleteCascade() { + return StringUtils.equals(getOnDeleteAction(), CONSTRAINT_PARAM_VAL_CASCADE); + } + + public boolean isOnDeleteUpdate() { + return StringUtils.equals(getOnDeleteAction(), CONSTRAINT_PARAM_VAL_UPDATE); + } + + private String getOnDeleteAction() { + Map<String, Object> params = refConstraint.getParams(); + + Object action = MapUtils.isNotEmpty(params) ? params.get(AtlasConstraintDef.CONSTRAINT_PARAM_ON_DELETE) : null; + + return (action != null) ? action.toString() : null; + } + } } http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/5d8fae55/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 bac5800..8bdbe93 100644 --- a/intg/src/main/java/org/apache/atlas/type/AtlasStructType.java +++ b/intg/src/main/java/org/apache/atlas/type/AtlasStructType.java @@ -20,14 +20,13 @@ package org.apache.atlas.type; import org.apache.atlas.AtlasErrorCode; import org.apache.atlas.exception.AtlasBaseException; import org.apache.atlas.model.TypeCategory; -import org.apache.atlas.model.instance.AtlasEntity; -import org.apache.atlas.model.instance.AtlasObjectId; import org.apache.atlas.model.instance.AtlasStruct; import org.apache.atlas.model.typedef.AtlasStructDef; import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef; import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef.Cardinality; import org.apache.atlas.model.typedef.AtlasStructDef.AtlasConstraintDef; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections.MapUtils; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,9 +39,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -import static org.apache.atlas.model.typedef.AtlasStructDef.AtlasConstraintDef.CONSTRAINT_PARAM_REF_ATTRIBUTE; -import static org.apache.atlas.model.typedef.AtlasStructDef.AtlasConstraintDef.CONSTRAINT_TYPE_MAPPED_FROM_REF; - /** * class that implements behaviour of a struct-type. @@ -52,9 +48,8 @@ public class AtlasStructType extends AtlasType { private final AtlasStructDef structDef; - private Set<String> foreignKeyAttributes = new HashSet<>(); - private Map<String, TypeAttributePair> mappedFromRefAttributes = new HashMap<>(); - protected Map<String, AtlasAttribute> allAttributes = Collections.emptyMap(); + private Map<String, AtlasConstraintDef> foreignKeyAttributes = Collections.emptyMap(); + protected Map<String, AtlasAttribute> allAttributes = Collections.emptyMap(); public AtlasStructType(AtlasStructDef structDef) { super(structDef); @@ -73,47 +68,60 @@ public class AtlasStructType extends AtlasType { public AtlasStructDef getStructDef() { return structDef; } public AtlasType getAttributeType(String attributeName) { - AtlasAttribute attribute = allAttributes.get(attributeName); + AtlasAttribute attribute = getAttribute(attributeName); + return attribute != null ? attribute.getAttributeType() : null; } - public AtlasAttributeDef getAttributeDef(String attributeName) { return allAttributes.get(attributeName) != null ? allAttributes.get(attributeName).getAttributeDef() : null; } + public AtlasAttributeDef getAttributeDef(String attributeName) { + AtlasAttribute attribute = getAttribute(attributeName); + + return attribute != null ? attribute.getAttributeDef() : null; + } + + public Set<String> getForeignKeyAttributes() { return foreignKeyAttributes.keySet(); } public boolean isForeignKeyAttribute(String attributeName) { - return foreignKeyAttributes.contains(attributeName); + return foreignKeyAttributes.containsKey(attributeName); } - public boolean isMappedFromRefAttribute(String attributeName) { - return mappedFromRefAttributes.containsKey(attributeName); + public AtlasConstraintDef getForeignKeyConstraint(String attributeName) { + return foreignKeyAttributes.get(attributeName); } - public String getMappedFromRefAttribute(String typeName, String attribName) { + public String getForeignKeyOnDeleteAction(String attributeName) { String ret = null; - for (Map.Entry<String, TypeAttributePair> e : mappedFromRefAttributes.entrySet()) { - String refTypeName = e.getValue().typeName; - String refAttribName = e.getValue().attributeName; + AtlasConstraintDef fkConstraint = getForeignKeyConstraint(attributeName); - if(StringUtils.equals(refTypeName, typeName) && StringUtils.equals(refAttribName, attribName)) { - ret = e.getKey(); + if (fkConstraint != null && MapUtils.isNotEmpty(fkConstraint.getParams())) { + Object onDeleteAction = fkConstraint.getParams().get(AtlasConstraintDef.CONSTRAINT_PARAM_ON_DELETE); - break; + if (onDeleteAction != null) { + ret = onDeleteAction.toString(); } } return ret; } + public boolean isForeignKeyOnDeleteActionCascade(String attributeName) { + return StringUtils.equals(getForeignKeyOnDeleteAction(attributeName), + AtlasConstraintDef.CONSTRAINT_PARAM_VAL_CASCADE); + } + + public boolean isForeignKeyOnDeleteActionUpdate(String attributeName) { + return StringUtils.equals(getForeignKeyOnDeleteAction(attributeName), + AtlasConstraintDef.CONSTRAINT_PARAM_VAL_UPDATE); + } + @Override public void resolveReferences(AtlasTypeRegistry typeRegistry) throws AtlasBaseException { Map<String, AtlasAttribute> a = new HashMap<>(); for (AtlasAttributeDef attributeDef : structDef.getAttributeDefs()) { - - AtlasType attrType = typeRegistry.getType(attributeDef.getTypeName()); - AtlasAttribute attribute = new AtlasAttribute(this, structDef, attributeDef, attrType); - - resolveConstraints(attributeDef, attrType); + AtlasType attrType = typeRegistry.getType(attributeDef.getTypeName()); + AtlasAttribute attribute = new AtlasAttribute(this, attributeDef, attrType); Cardinality cardinality = attributeDef.getCardinality(); @@ -133,6 +141,25 @@ public class AtlasStructType extends AtlasType { } this.allAttributes = Collections.unmodifiableMap(a); + + foreignKeyAttributes = Collections.unmodifiableMap(resolveForeignKeyConstraints(allAttributes.values())); + } + + @Override + public void resolveReferencesPhase2(AtlasTypeRegistry typeRegistry) throws AtlasBaseException { + super.resolveReferencesPhase2(typeRegistry); + + for (Map.Entry<String, AtlasConstraintDef> e : foreignKeyAttributes.entrySet()) { + String attributeName = e.getKey(); + AtlasAttribute attribute = getAttribute(attributeName); + AtlasConstraintDef constraint = e.getValue(); + + AtlasType attributeType = attribute.getAttributeType(); + + if (attributeType instanceof AtlasEntityType) { + ((AtlasEntityType)attributeType).addForeignKeyReference(attribute, constraint); + } + } } @Override @@ -149,22 +176,7 @@ public class AtlasStructType extends AtlasType { } public AtlasAttribute getAttribute(String attributeName) { - return findAttribute(allAttributes.values(), attributeName); - } - - public static AtlasAttribute findAttribute(Collection<AtlasAttribute> attributes, String attrName) { - AtlasAttribute ret = null; - - if (CollectionUtils.isNotEmpty(attributes)) { - for (AtlasAttribute attribute : attributes) { - if (org.apache.hadoop.util.StringUtils.equalsIgnoreCase(attribute.getAttributeDef().getName(), attrName)) { - ret = attribute; - break; - } - } - } - - return ret; + return allAttributes.get(attributeName); } @Override @@ -374,116 +386,55 @@ public class AtlasStructType extends AtlasType { return null; } - private void resolveConstraints(AtlasAttributeDef attribDef, AtlasType attribType) throws AtlasBaseException { - if (attribDef == null || CollectionUtils.isEmpty(attribDef.getConstraintDefs()) || attribType == null) { - return; - } - - for (AtlasConstraintDef constraintDef : attribDef.getConstraintDefs()) { - String constraintType = constraintDef != null ? constraintDef.getType() : null; - - if (StringUtils.isBlank(constraintType)) { - continue; - } - - switch (constraintType) { - case AtlasConstraintDef.CONSTRAINT_TYPE_FOREIGN_KEY: - resolveForeignKeyConstraint(attribDef, constraintDef, attribType); - break; - case CONSTRAINT_TYPE_MAPPED_FROM_REF: - resolveMappedFromRefConstraint(attribDef, constraintDef, attribType); - break; - default: - throw new AtlasBaseException(AtlasErrorCode.UNKNOWN_CONSTRAINT, constraintType, - getTypeName(), attribDef.getName()); - } - } - } - /* * valid conditions for foreign-key constraint: * - supported only in entity-type * - attribute should be an entity-type or an array of entity-type */ - private void resolveForeignKeyConstraint(AtlasAttributeDef attribDef, AtlasConstraintDef constraintDef, - AtlasType attribType) throws AtlasBaseException { - if (this.getTypeCategory() != TypeCategory.ENTITY) { - throw new AtlasBaseException(AtlasErrorCode.UNSUPPORTED_CONSTRAINT, - AtlasConstraintDef.CONSTRAINT_TYPE_FOREIGN_KEY , getTypeName(), attribDef.getName()); - } - - if (attribType.getTypeCategory() == TypeCategory.ARRAY) { - attribType = ((AtlasArrayType)attribType).getElementType(); - } - - if (attribType.getTypeCategory() != TypeCategory.ENTITY) { - throw new AtlasBaseException(AtlasErrorCode.CONSTRAINT_NOT_SATISFIED, - getTypeName(), attribDef.getName(), AtlasConstraintDef.CONSTRAINT_TYPE_FOREIGN_KEY, - attribType.getTypeName()); - } + private Map<String, AtlasConstraintDef> resolveForeignKeyConstraints(Collection<AtlasAttribute> attributes) + throws AtlasBaseException { + Map<String, AtlasConstraintDef> ret = null; - foreignKeyAttributes.add(attribDef.getName()); - } + for (AtlasAttribute attribute : attributes) { + AtlasAttributeDef attribDef = attribute.getAttributeDef(); - /* - * valid conditions for mapped-from-ref constraint: - * - supported only in entity-type - * - attribute should be an entity-type or an array of entity-type - * - attribute's entity-type should have a foreign-key constraint to this type - */ - private void resolveMappedFromRefConstraint(AtlasAttributeDef attribDef, AtlasConstraintDef constraintDef, - AtlasType attribType) throws AtlasBaseException { + if (CollectionUtils.isEmpty(attribDef.getConstraintDefs())) { + continue; + } - if (this.getTypeCategory() != TypeCategory.ENTITY) { - throw new AtlasBaseException(AtlasErrorCode.UNSUPPORTED_CONSTRAINT, getTypeName(), - attribDef.getName(), CONSTRAINT_TYPE_MAPPED_FROM_REF); - } + for (AtlasConstraintDef constraintDef : attribDef.getConstraintDefs()) { + if (!StringUtils.equals(constraintDef.getType(), AtlasConstraintDef.CONSTRAINT_TYPE_FOREIGN_KEY)) { + continue; + } - if (attribType.getTypeCategory() == TypeCategory.ARRAY) { - attribType = ((AtlasArrayType)attribType).getElementType(); - } + if (this.getTypeCategory() != TypeCategory.ENTITY) { + throw new AtlasBaseException(AtlasErrorCode.UNSUPPORTED_CONSTRAINT, + AtlasConstraintDef.CONSTRAINT_TYPE_FOREIGN_KEY, getTypeName(), attribute.getName()); + } - if (attribType.getTypeCategory() != TypeCategory.ENTITY) { - throw new AtlasBaseException(AtlasErrorCode.CONSTRAINT_NOT_SATISFIED, getTypeName(), - attribDef.getName(), CONSTRAINT_TYPE_MAPPED_FROM_REF, attribDef.getTypeName()); - } + AtlasType attribType = attribute.getAttributeType(); - String refAttribName = AtlasTypeUtil.getStringValue(constraintDef.getParams(), CONSTRAINT_PARAM_REF_ATTRIBUTE); + if (attribType.getTypeCategory() == TypeCategory.ARRAY) { + attribType = ((AtlasArrayType) attribType).getElementType(); + } - if (StringUtils.isBlank(refAttribName)) { - throw new AtlasBaseException(AtlasErrorCode.CONSTRAINT_MISSING_PARAMS, - getTypeName(), attribDef.getName(), - CONSTRAINT_PARAM_REF_ATTRIBUTE, CONSTRAINT_TYPE_MAPPED_FROM_REF, - String.valueOf(constraintDef.getParams())); - } + if (attribType.getTypeCategory() != TypeCategory.ENTITY) { + throw new AtlasBaseException(AtlasErrorCode.CONSTRAINT_NOT_SATISFIED, + getTypeName(), attribute.getName(), AtlasConstraintDef.CONSTRAINT_TYPE_FOREIGN_KEY, + attribType.getTypeName()); + } - AtlasStructType structType = (AtlasStructType) attribType; - AtlasAttributeDef refAttrib = structType.getStructDef().getAttribute(refAttribName); + if (ret == null) { + ret = new HashMap<>(); + } - if (refAttrib == null) { - throw new AtlasBaseException(AtlasErrorCode.CONSTRAINT_NOT_EXIST, - getTypeName(), attribDef.getName(), - CONSTRAINT_PARAM_REF_ATTRIBUTE, structType.getTypeName(), refAttribName); - } + ret.put(attribute.getName(), constraintDef); - if (!StringUtils.equals(getTypeName(), refAttrib.getTypeName())) { - throw new AtlasBaseException(AtlasErrorCode.CONSTRAINT_NOT_MATCHED, - getTypeName(), attribDef.getName(), - CONSTRAINT_PARAM_REF_ATTRIBUTE, structType.getTypeName(), refAttribName, - getTypeName(), refAttrib.getTypeName()); + break; + } } - mappedFromRefAttributes.put(attribDef.getName(), new TypeAttributePair(attribType.getTypeName(), refAttribName)); - } - - private class TypeAttributePair { - public final String typeName; - public final String attributeName; - - public TypeAttributePair(String typeName, String attributeName) { - this.typeName = typeName; - this.attributeName = attributeName; - } + return ret == null ? Collections.<String, AtlasConstraintDef>emptyMap() : ret; } public String getQualifiedAttributeName(String attrName) throws AtlasBaseException { @@ -496,31 +447,21 @@ public class AtlasStructType extends AtlasType { public static class AtlasAttribute { - private final AtlasStructType structType; - private final AtlasStructDef structDef; - private final AtlasType attributeType; + private final AtlasStructType structType; + private final AtlasType attributeType; private final AtlasAttributeDef attributeDef; - private final String qualifiedName; + private final String qualifiedName; - public AtlasAttribute(AtlasStructType structType, AtlasStructDef structDef, AtlasAttributeDef attrDef, AtlasType attributeType) { - this.structType = structType; - this.structDef = structDef; - this.attributeDef = attrDef; + public AtlasAttribute(AtlasStructType structType, AtlasAttributeDef attrDef, AtlasType attributeType) { + this.structType = structType; + this.attributeDef = attrDef; this.attributeType = attributeType; - this.qualifiedName = getQualifiedAttributeName(structDef, attributeDef.getName()); + this.qualifiedName = getQualifiedAttributeName(structType.getStructDef(), attributeDef.getName()); } - public AtlasStructType getStructType() { - return structType; - } + public AtlasStructType getStructType() { return structType; } - public String getQualifiedName() { - return qualifiedName; - } - - public AtlasStructDef getStructDef() { - return structDef; - } + public AtlasStructDef getStructDef() { return structType.getStructDef(); } public AtlasType getAttributeType() { return attributeType; @@ -530,6 +471,12 @@ public class AtlasStructType extends AtlasType { return attributeDef; } + public String getName() { return attributeDef.getName(); } + + public String getTypeName() { return attributeDef.getTypeName(); } + + public String getQualifiedName() { return qualifiedName; } + public String getQualifiedAttributeName() { return qualifiedName; } http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/5d8fae55/intg/src/main/java/org/apache/atlas/type/AtlasTypeUtil.java ---------------------------------------------------------------------- diff --git a/intg/src/main/java/org/apache/atlas/type/AtlasTypeUtil.java b/intg/src/main/java/org/apache/atlas/type/AtlasTypeUtil.java index e4f1eea..3ba7a33 100644 --- a/intg/src/main/java/org/apache/atlas/type/AtlasTypeUtil.java +++ b/intg/src/main/java/org/apache/atlas/type/AtlasTypeUtil.java @@ -29,6 +29,7 @@ import org.apache.atlas.model.typedef.AtlasEnumDef.AtlasEnumElementDef; import org.apache.atlas.model.typedef.AtlasStructDef; import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef; import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef.Cardinality; +import org.apache.atlas.model.typedef.AtlasStructDef.AtlasConstraintDef; import org.apache.atlas.model.typedef.AtlasTypeDefHeader; import org.apache.atlas.model.typedef.AtlasTypesDef; import org.apache.commons.collections.CollectionUtils; @@ -153,54 +154,54 @@ public class AtlasTypeUtil { return new AtlasAttributeDef(name, dataType.getTypeName(), true, Cardinality.SINGLE, 0, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()); + Collections.<AtlasConstraintDef>emptyList()); } public static AtlasAttributeDef createOptionalAttrDef(String name, String dataType) { return new AtlasAttributeDef(name, dataType, true, Cardinality.SINGLE, 0, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()); + Collections.<AtlasConstraintDef>emptyList()); } public static AtlasAttributeDef createRequiredAttrDef(String name, String dataType) { return new AtlasAttributeDef(name, dataType, false, Cardinality.SINGLE, 1, 1, false, true, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()); + Collections.<AtlasConstraintDef>emptyList()); } public static AtlasAttributeDef createListRequiredAttrDef(String name, String dataType) { return new AtlasAttributeDef(name, dataType, false, Cardinality.LIST, 1, Integer.MAX_VALUE, false, true, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()); + Collections.<AtlasConstraintDef>emptyList()); } public static AtlasAttributeDef createOptionalListAttrDef(String name, String dataType) { return new AtlasAttributeDef(name, dataType, true, Cardinality.LIST, 1, Integer.MAX_VALUE, false, true, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()); + Collections.<AtlasConstraintDef>emptyList()); } public static AtlasAttributeDef createRequiredListAttrDefWithConstraint(String name, String dataType, String type, Map param) { AtlasAttributeDef ret = AtlasTypeUtil.createListRequiredAttrDef(name, dataType); - ret.addConstraint(new AtlasStructDef.AtlasConstraintDef(type, param)); + ret.addConstraint(new AtlasConstraintDef(type, param)); return ret; } public static AtlasAttributeDef createRequiredAttrDefWithConstraint(String name, String typeName, String type, Map param) { AtlasAttributeDef ret = AtlasTypeUtil.createRequiredAttrDef(name, typeName); - ret.addConstraint(new AtlasStructDef.AtlasConstraintDef(type, param)); + ret.addConstraint(new AtlasConstraintDef(type, param)); return ret; } public static AtlasAttributeDef createOptionalAttrDefWithConstraint(String name, String typeName, String type, Map param) { AtlasAttributeDef ret = AtlasTypeUtil.createOptionalAttrDef(name, typeName); - ret.addConstraint(new AtlasStructDef.AtlasConstraintDef(type, param)); + ret.addConstraint(new AtlasConstraintDef(type, param)); return ret; } @@ -209,21 +210,21 @@ public class AtlasTypeUtil { return new AtlasAttributeDef(name, dataType.getTypeName(), false, Cardinality.SINGLE, 1, 1, true, true, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()); + Collections.<AtlasConstraintDef>emptyList()); } public static AtlasAttributeDef createUniqueRequiredAttrDef(String name, String typeName) { return new AtlasAttributeDef(name, typeName, false, Cardinality.SINGLE, 1, 1, true, true, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()); + Collections.<AtlasConstraintDef>emptyList()); } public static AtlasAttributeDef createRequiredAttrDef(String name, AtlasType dataType) { return new AtlasAttributeDef(name, dataType.getTypeName(), false, Cardinality.SINGLE, 1, 1, false, true, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()); + Collections.<AtlasConstraintDef>emptyList()); } public static AtlasEnumDef createEnumTypeDef(String name, String description, AtlasEnumElementDef... enumValues) { http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/5d8fae55/intg/src/test/java/org/apache/atlas/TestUtilsV2.java ---------------------------------------------------------------------- diff --git a/intg/src/test/java/org/apache/atlas/TestUtilsV2.java b/intg/src/test/java/org/apache/atlas/TestUtilsV2.java index 4f2916c..e0bf251 100755 --- a/intg/src/test/java/org/apache/atlas/TestUtilsV2.java +++ b/intg/src/test/java/org/apache/atlas/TestUtilsV2.java @@ -29,6 +29,7 @@ import org.apache.atlas.model.typedef.AtlasEnumDef; import org.apache.atlas.model.typedef.AtlasEnumDef.AtlasEnumElementDef; import org.apache.atlas.model.typedef.AtlasStructDef; import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef; +import org.apache.atlas.model.typedef.AtlasStructDef.AtlasConstraintDef; import org.apache.atlas.model.typedef.AtlasTypesDef; import org.apache.atlas.type.AtlasTypeUtil; import org.apache.commons.lang.RandomStringUtils; @@ -84,12 +85,12 @@ public final class TestUtilsV2 { AtlasTypeUtil.createUniqueRequiredAttrDef("name", "string"), new AtlasAttributeDef("employees", String.format("array<%s>", "Employee"), true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, - new ArrayList<AtlasStructDef.AtlasConstraintDef>())); + new ArrayList<AtlasConstraintDef>())); deptTypeDef.getAttribute("employees").addConstraint( - new AtlasStructDef.AtlasConstraintDef( - AtlasStructDef.AtlasConstraintDef.CONSTRAINT_TYPE_MAPPED_FROM_REF, new HashMap<String, Object>() {{ - put(AtlasStructDef.AtlasConstraintDef.CONSTRAINT_PARAM_REF_ATTRIBUTE, "department"); + new AtlasConstraintDef( + AtlasConstraintDef.CONSTRAINT_TYPE_MAPPED_FROM_REF, new HashMap<String, Object>() {{ + put(AtlasConstraintDef.CONSTRAINT_PARAM_REF_ATTRIBUTE, "department"); }})); AtlasEntityDef personTypeDef = AtlasTypeUtil.createClassTypeDef("Person", "Person"+_description, ImmutableSet.<String>of(), @@ -110,29 +111,29 @@ public final class TestUtilsV2 { new AtlasAttributeDef("department", "Department", false, AtlasAttributeDef.Cardinality.SINGLE, 1, 1, false, false, - new ArrayList<AtlasStructDef.AtlasConstraintDef>()), + new ArrayList<AtlasConstraintDef>()), new AtlasAttributeDef("manager", "Manager", true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), + Collections.<AtlasConstraintDef>emptyList()), new AtlasAttributeDef("mentor", EMPLOYEE_TYPE, true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), + Collections.<AtlasConstraintDef>emptyList()), AtlasTypeUtil.createOptionalAttrDef("shares", "long"), AtlasTypeUtil.createOptionalAttrDef("salary", "double") ); employeeTypeDef.getAttribute("department").addConstraint( - new AtlasStructDef.AtlasConstraintDef( - AtlasStructDef.AtlasConstraintDef.CONSTRAINT_TYPE_FOREIGN_KEY, new HashMap<String, Object>() {{ - put(AtlasStructDef.AtlasConstraintDef.CONSTRAINT_PARAM_ON_DELETE, AtlasStructDef.AtlasConstraintDef.CONSTRAINT_PARAM_VAL_CASCADE); + new AtlasConstraintDef( + AtlasConstraintDef.CONSTRAINT_TYPE_FOREIGN_KEY, new HashMap<String, Object>() {{ + put(AtlasConstraintDef.CONSTRAINT_PARAM_ON_DELETE, AtlasConstraintDef.CONSTRAINT_PARAM_VAL_CASCADE); }})); AtlasEntityDef managerTypeDef = AtlasTypeUtil.createClassTypeDef("Manager", "Manager"+_description, ImmutableSet.of("Employee"), new AtlasAttributeDef("subordinates", String.format("array<%s>", "Employee"), false, AtlasAttributeDef.Cardinality.SET, 1, 10, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList())); + Collections.<AtlasConstraintDef>emptyList())); AtlasClassificationDef securityClearanceTypeDef = AtlasTypeUtil.createTraitTypeDef("SecurityClearance", "SecurityClearance"+_description, ImmutableSet.<String>of(), @@ -165,7 +166,7 @@ public final class TestUtilsV2 { AtlasTypeUtil.createOptionalAttrDef("dep-code", "string"), new AtlasAttributeDef("employees", String.format("array<%s>", "Employee"), true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList())); + Collections.<AtlasConstraintDef>emptyList())); AtlasEntityDef personTypeDef = AtlasTypeUtil.createClassTypeDef("Person", "Person"+_description, ImmutableSet.<String>of(), @@ -189,15 +190,15 @@ public final class TestUtilsV2 { new AtlasAttributeDef("department", "Department", false, AtlasAttributeDef.Cardinality.SINGLE, 1, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), + Collections.<AtlasConstraintDef>emptyList()), new AtlasAttributeDef("manager", "Manager", true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), + Collections.<AtlasConstraintDef>emptyList()), new AtlasAttributeDef("mentor", EMPLOYEE_TYPE, true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), + Collections.<AtlasConstraintDef>emptyList()), AtlasTypeUtil.createOptionalAttrDef("shares", "long"), AtlasTypeUtil.createOptionalAttrDef("salary", "double") @@ -207,7 +208,7 @@ public final class TestUtilsV2 { ImmutableSet.of("Employee"), new AtlasAttributeDef("subordinates", String.format("array<%s>", "Employee"), false, AtlasAttributeDef.Cardinality.SET, 1, 10, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList())); + Collections.<AtlasConstraintDef>emptyList())); AtlasClassificationDef securityClearanceTypeDef = AtlasTypeUtil.createTraitTypeDef("SecurityClearance", "SecurityClearance"+_description, ImmutableSet.<String>of(), @@ -242,7 +243,7 @@ public final class TestUtilsV2 { AtlasTypeUtil.createRequiredAttrDef("dep-code", "string"), new AtlasAttributeDef("employees", String.format("array<%s>", "Person"), true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList())); + Collections.<AtlasConstraintDef>emptyList())); AtlasEntityDef personTypeDef = AtlasTypeUtil.createClassTypeDef("Person", "Person"+_description, ImmutableSet.<String>of(), AtlasTypeUtil.createRequiredAttrDef("name", "string"), @@ -252,15 +253,15 @@ public final class TestUtilsV2 { new AtlasAttributeDef("department", "Department", false, AtlasAttributeDef.Cardinality.SINGLE, 1, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), + Collections.<AtlasConstraintDef>emptyList()), new AtlasAttributeDef("manager", "Manager", true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), + Collections.<AtlasConstraintDef>emptyList()), new AtlasAttributeDef("mentor", "Person", true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), + Collections.<AtlasConstraintDef>emptyList()), AtlasTypeUtil.createOptionalAttrDef("birthday", "date"), AtlasTypeUtil.createOptionalAttrDef("hasPets", "boolean"), AtlasTypeUtil.createOptionalAttrDef("numberOfCars", "byte"), @@ -474,23 +475,23 @@ public final class TestUtilsV2 { new AtlasAttributeDef("location", "string", true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), + Collections.<AtlasConstraintDef>emptyList()), new AtlasAttributeDef("inputFormat", "string", true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), + Collections.<AtlasConstraintDef>emptyList()), new AtlasAttributeDef("outputFormat", "string", true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), + Collections.<AtlasConstraintDef>emptyList()), new AtlasAttributeDef("compressed", "boolean", false, AtlasAttributeDef.Cardinality.SINGLE, 1, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), + Collections.<AtlasConstraintDef>emptyList()), new AtlasAttributeDef("numBuckets", "int", true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), + Collections.<AtlasConstraintDef>emptyList()), }; AtlasEntityDef storageDescClsDef = @@ -502,32 +503,32 @@ public final class TestUtilsV2 { true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), + Collections.<AtlasConstraintDef>emptyList()), new AtlasAttributeDef("table", TABLE_TYPE, false, AtlasAttributeDef.Cardinality.SINGLE, 1, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), + Collections.<AtlasConstraintDef>emptyList()), new AtlasAttributeDef("createTime", "long", true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), + Collections.<AtlasConstraintDef>emptyList()), new AtlasAttributeDef("lastAccessTime", "long", true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), + Collections.<AtlasConstraintDef>emptyList()), new AtlasAttributeDef("sd", STORAGE_DESC_TYPE, false, AtlasAttributeDef.Cardinality.SINGLE, 1, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), + Collections.<AtlasConstraintDef>emptyList()), new AtlasAttributeDef("columns", String.format("array<%s>", COLUMN_TYPE), true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), + Collections.<AtlasConstraintDef>emptyList()), new AtlasAttributeDef("parameters", String.format("map<%s,%s>", "string", "string"), true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList())}; + Collections.<AtlasConstraintDef>emptyList())}; AtlasEntityDef partClsDef = new AtlasEntityDef("partition_class_type", "partition_class_type" + _description, "1.0", @@ -538,7 +539,7 @@ public final class TestUtilsV2 { Arrays.asList(new AtlasAttributeDef("outputs", "array<" + TABLE_TYPE + ">", true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList())), + Collections.<AtlasConstraintDef>emptyList())), ImmutableSet.<String>of()); AtlasEntityDef tableTypeDefinition = @@ -551,42 +552,42 @@ public final class TestUtilsV2 { new AtlasAttributeDef("tableType", "tableType", false, AtlasAttributeDef.Cardinality.SINGLE, 1, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), + Collections.<AtlasConstraintDef>emptyList()), // array of strings new AtlasAttributeDef("columnNames", String.format("array<%s>", "string"), true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), + Collections.<AtlasConstraintDef>emptyList()), // array of classes new AtlasAttributeDef("columns", String.format("array<%s>", COLUMN_TYPE), true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), + Collections.<AtlasConstraintDef>emptyList()), // array of structs new AtlasAttributeDef("partitions", String.format("array<%s>", "partition_struct_type"), true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), + Collections.<AtlasConstraintDef>emptyList()), // map of primitives new AtlasAttributeDef("parametersMap", String.format("map<%s,%s>", "string", "string"), true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), + Collections.<AtlasConstraintDef>emptyList()), //map of classes - new AtlasAttributeDef(COLUMNS_MAP, String.format("map<%s,%s>", "string", COLUMN_TYPE), true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), + Collections.<AtlasConstraintDef>emptyList()), - // new ArrayList<AtlasStructDef.AtlasConstraintDef>() {{ - //add(new AtlasStructDef.AtlasConstraintDef( - // AtlasStructDef.AtlasConstraintDef.CONSTRAINT_TYPE_MAPPED_FROM_REF, new HashMap<String, Object>())); + // new ArrayList<AtlasConstraintDef>() {{ + //add(new AtlasConstraintDef( + // AtlasConstraintDef.CONSTRAINT_TYPE_MAPPED_FROM_REF, new HashMap<String, Object>())); //}}), //map of structs new AtlasAttributeDef("partitionsMap", @@ -594,26 +595,26 @@ public final class TestUtilsV2 { true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), + Collections.<AtlasConstraintDef>emptyList()), // struct reference new AtlasAttributeDef("serde1", "serdeType", true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), + Collections.<AtlasConstraintDef>emptyList()), new AtlasAttributeDef("serde2", "serdeType", true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), + Collections.<AtlasConstraintDef>emptyList()), // class reference new AtlasAttributeDef("database", DATABASE_TYPE, false, AtlasAttributeDef.Cardinality.SINGLE, 1, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), + Collections.<AtlasConstraintDef>emptyList()), //class reference as composite new AtlasAttributeDef("databaseComposite", DATABASE_TYPE, true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList())); + Collections.<AtlasConstraintDef>emptyList())); AtlasClassificationDef piiTypeDefinition = AtlasTypeUtil.createTraitTypeDef(PII, PII + _description, ImmutableSet.<String>of()); @@ -693,7 +694,7 @@ public final class TestUtilsV2 { AtlasEntityDef developerTypeDef = AtlasTypeUtil.createClassTypeDef("Developer", "Developer_description", ImmutableSet.of("Employee"), new AtlasAttributeDef("language", String.format("array<%s>", "string"), false, AtlasAttributeDef.Cardinality.SET, 1, 10, false, false, - Collections.<AtlasStructDef.AtlasConstraintDef>emptyList())); + Collections.<AtlasConstraintDef>emptyList())); return Arrays.asList(developerTypeDef); } http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/5d8fae55/intg/src/test/java/org/apache/atlas/model/ModelTestUtil.java ---------------------------------------------------------------------- diff --git a/intg/src/test/java/org/apache/atlas/model/ModelTestUtil.java b/intg/src/test/java/org/apache/atlas/model/ModelTestUtil.java index 5c72470..084bcc4 100644 --- a/intg/src/test/java/org/apache/atlas/model/ModelTestUtil.java +++ b/intg/src/test/java/org/apache/atlas/model/ModelTestUtil.java @@ -31,7 +31,6 @@ import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef; import org.apache.atlas.type.AtlasClassificationType; import org.apache.atlas.type.AtlasEntityType; import org.apache.atlas.type.AtlasStructType; -import org.apache.atlas.type.AtlasType; import org.apache.atlas.type.AtlasTypeRegistry; import org.apache.atlas.type.AtlasTypeRegistry.AtlasTransientTypeRegistry; import org.slf4j.Logger; http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/5d8fae55/intg/src/test/java/org/apache/atlas/type/TestAtlasEntityType.java ---------------------------------------------------------------------- diff --git a/intg/src/test/java/org/apache/atlas/type/TestAtlasEntityType.java b/intg/src/test/java/org/apache/atlas/type/TestAtlasEntityType.java index 4e15edd..dc5bbf1 100644 --- a/intg/src/test/java/org/apache/atlas/type/TestAtlasEntityType.java +++ b/intg/src/test/java/org/apache/atlas/type/TestAtlasEntityType.java @@ -31,12 +31,18 @@ import org.apache.atlas.model.typedef.AtlasEntityDef; import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef; import org.apache.atlas.model.typedef.AtlasStructDef.AtlasConstraintDef; import org.apache.atlas.type.AtlasTypeRegistry.AtlasTransientTypeRegistry; +import org.apache.atlas.type.AtlasEntityType.ForeignKeyReference; import org.testng.annotations.Test; import static org.testng.Assert.*; public class TestAtlasEntityType { + private static final String TYPE_TABLE = "my_table"; + private static final String TYPE_COLUMN = "my_column"; + private static final String ATTR_TABLE = "table"; + private static final String ATTR_COLUMNS = "columns"; + private final AtlasEntityType entityType; private final List<Object> validValues = new ArrayList<>(); private final List<Object> invalidValues = new ArrayList<>(); @@ -138,13 +144,34 @@ public class TestAtlasEntityType { ttr.addTypes(entityDefs); + AtlasEntityType typeTable = ttr.getEntityTypeByName(TYPE_TABLE); + AtlasEntityType typeColumn = ttr.getEntityTypeByName(TYPE_COLUMN); + + assertEquals(typeTable.getForeignKeyReferences().size(), 1); + + ForeignKeyReference fkRef = typeTable.getForeignKeyReferences().get(0); + assertEquals(fkRef.fromTypeName(), TYPE_COLUMN); + assertEquals(fkRef.fromAttributeName(), ATTR_TABLE); + assertEquals(fkRef.toTypeName(), TYPE_TABLE); + assertTrue(fkRef.isOnDeleteCascade()); + assertFalse(fkRef.isOnDeleteUpdate()); + + assertEquals(typeTable.getForeignKeyAttributes().size(), 0); + assertEquals(typeTable.getMappedFromRefAttributes().size(), 1); + assertTrue(typeTable.getMappedFromRefAttributes().contains(ATTR_COLUMNS)); + + assertEquals(typeColumn.getForeignKeyReferences().size(), 0); + assertEquals(typeColumn.getForeignKeyAttributes().size(), 1); + assertTrue(typeColumn.getForeignKeyAttributes().contains(ATTR_TABLE)); + assertEquals(typeColumn.getMappedFromRefAttributes().size(), 0); + commit = true; } catch (AtlasBaseException excp) { failureMsg = excp.getMessage(); } finally { typeRegistry.releaseTypeRegistryForUpdate(ttr, commit); } - assertNull(failureMsg, "failed to create types my_table and my_column"); + assertNull(failureMsg, "failed to create types " + TYPE_TABLE + " and " + TYPE_COLUMN); } @Test @@ -230,12 +257,12 @@ public class TestAtlasEntityType { } private AtlasEntityDef createTableEntityDef() { - AtlasEntityDef table = new AtlasEntityDef("my_table"); - AtlasAttributeDef attrColumns = new AtlasAttributeDef("columns", - AtlasBaseTypeDef.getArrayTypeName("my_column")); + AtlasEntityDef table = new AtlasEntityDef(TYPE_TABLE); + AtlasAttributeDef attrColumns = new AtlasAttributeDef(ATTR_COLUMNS, + AtlasBaseTypeDef.getArrayTypeName(TYPE_COLUMN)); Map<String, Object> params = new HashMap<>(); - params.put(AtlasConstraintDef.CONSTRAINT_PARAM_REF_ATTRIBUTE, "table"); + params.put(AtlasConstraintDef.CONSTRAINT_PARAM_REF_ATTRIBUTE, ATTR_TABLE); attrColumns.addConstraint(new AtlasConstraintDef(AtlasConstraintDef.CONSTRAINT_TYPE_MAPPED_FROM_REF, params)); table.addAttribute(attrColumns); @@ -244,9 +271,9 @@ public class TestAtlasEntityType { } private AtlasEntityDef createTableEntityDefWithMissingRefAttribute() { - AtlasEntityDef table = new AtlasEntityDef("my_table"); - AtlasAttributeDef attrColumns = new AtlasAttributeDef("columns", - AtlasBaseTypeDef.getArrayTypeName("my_column")); + AtlasEntityDef table = new AtlasEntityDef(TYPE_TABLE); + AtlasAttributeDef attrColumns = new AtlasAttributeDef(ATTR_COLUMNS, + AtlasBaseTypeDef.getArrayTypeName(TYPE_COLUMN)); attrColumns.addConstraint(new AtlasConstraintDef(AtlasConstraintDef.CONSTRAINT_TYPE_MAPPED_FROM_REF, null)); table.addAttribute(attrColumns); @@ -255,10 +282,13 @@ public class TestAtlasEntityType { } private AtlasEntityDef createColumnEntityDef() { - AtlasEntityDef column = new AtlasEntityDef("my_column"); - AtlasAttributeDef attrTable = new AtlasAttributeDef("table", "my_table"); + AtlasEntityDef column = new AtlasEntityDef(TYPE_COLUMN); + AtlasAttributeDef attrTable = new AtlasAttributeDef(ATTR_TABLE, TYPE_TABLE); + + Map<String, Object> params = new HashMap<>(); + params.put(AtlasConstraintDef.CONSTRAINT_PARAM_ON_DELETE, AtlasConstraintDef.CONSTRAINT_PARAM_VAL_CASCADE); - attrTable.addConstraint(new AtlasConstraintDef(AtlasConstraintDef.CONSTRAINT_TYPE_FOREIGN_KEY)); + attrTable.addConstraint(new AtlasConstraintDef(AtlasConstraintDef.CONSTRAINT_TYPE_FOREIGN_KEY, params)); column.addAttribute(attrTable); return column; http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/5d8fae55/release-log.txt ---------------------------------------------------------------------- diff --git a/release-log.txt b/release-log.txt index d836659..fa22e6d 100644 --- a/release-log.txt +++ b/release-log.txt @@ -9,6 +9,8 @@ ATLAS-1060 Add composite indexes for exact match performance improvements for al ATLAS-1127 Modify creation and modification timestamps to Date instead of Long(sumasai) ALL CHANGES: +ATLAS-1513: updated AtlasEntityType with methods to get foreign-key references; added helper methods in AtlasAttribute (mneethiraj via kevalbhatt) +ATLAS-1502: added configuration to restrict entity-types editable via UI (Kalyanikashikar via mneethiraj) ATLAS-1507 fixed incorrect relationship specified in hive-model ATLAS-1506 updated AtlasObjectId to support unqiueAttributes to identity the object ATLAS-1378 Use .gitignore so git does not see binary files as changed (david_radley via dkantor) http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/5d8fae55/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/ArrayVertexMapper.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/ArrayVertexMapper.java b/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/ArrayVertexMapper.java index e63ea6e..ddd2242 100644 --- a/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/ArrayVertexMapper.java +++ b/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/ArrayVertexMapper.java @@ -20,9 +20,8 @@ package org.apache.atlas.repository.store.graph.v1; import com.google.common.base.Optional; import com.google.inject.Inject; import com.google.inject.Singleton; -import org.apache.atlas.aspect.Monitored; import org.apache.atlas.exception.AtlasBaseException; -import org.apache.atlas.model.typedef.AtlasStructDef; +import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef; import org.apache.atlas.repository.graph.GraphHelper; import org.apache.atlas.repository.graphdb.AtlasEdge; import org.apache.atlas.repository.graphdb.AtlasVertex; @@ -111,7 +110,7 @@ public class ArrayVertexMapper implements InstanceGraphMapper<List> { //Removes unused edges from the old collection, compared to the new collection private List<AtlasEdge> removeUnusedArrayEntries( AtlasStructType entityType, - AtlasStructDef.AtlasAttributeDef attributeDef, + AtlasAttributeDef attributeDef, List<AtlasEdge> currentEntries, List<AtlasEdge> newEntries, AtlasType entryType) throws AtlasBaseException { http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/5d8fae55/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasEntityGraphDiscoveryV1.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasEntityGraphDiscoveryV1.java b/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasEntityGraphDiscoveryV1.java index 4f5b4f4..2848a20 100644 --- a/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasEntityGraphDiscoveryV1.java +++ b/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasEntityGraphDiscoveryV1.java @@ -25,7 +25,7 @@ import org.apache.atlas.model.TypeCategory; import org.apache.atlas.model.instance.AtlasEntity; import org.apache.atlas.model.instance.AtlasObjectId; import org.apache.atlas.model.instance.AtlasStruct; -import org.apache.atlas.model.typedef.AtlasStructDef; +import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef; import org.apache.atlas.repository.store.graph.EntityGraphDiscoveryContext; import org.apache.atlas.repository.store.graph.EntityGraphDiscovery; import org.apache.atlas.repository.store.graph.EntityResolver; @@ -146,7 +146,7 @@ public class AtlasEntityGraphDiscoveryV1 implements EntityGraphDiscovery { } } - void visitAttribute(AtlasStructType parentType, AtlasType attrType, AtlasStructDef.AtlasAttributeDef attrDef, Object val) throws AtlasBaseException { + void visitAttribute(AtlasStructType parentType, AtlasType attrType, AtlasAttributeDef attrDef, Object val) throws AtlasBaseException { if (val != null) { if ( isPrimitive(attrType.getTypeCategory()) ) { return; @@ -166,7 +166,8 @@ public class AtlasEntityGraphDiscoveryV1 implements EntityGraphDiscovery { visitReference((AtlasEntityType) attrType, val, false); } else if ( val instanceof AtlasEntity ) { //TODO - Change this to foreign key checks after changes in the model - if ( parentType.isMappedFromRefAttribute(attrDef.getName())) { + if ((parentType instanceof AtlasEntityType) && + ((AtlasEntityType)parentType).isMappedFromRefAttribute(attrDef.getName())) { visitReference((AtlasEntityType) attrType, val, true); } else { visitReference((AtlasEntityType) attrType, val, false); @@ -176,7 +177,7 @@ public class AtlasEntityGraphDiscoveryV1 implements EntityGraphDiscovery { } } - void visitMapReferences(AtlasStructType parentType, final AtlasType attrType, AtlasStructDef.AtlasAttributeDef attrDef, AtlasType keyType, AtlasType valueType, Object val) throws AtlasBaseException { + void visitMapReferences(AtlasStructType parentType, final AtlasType attrType, AtlasAttributeDef attrDef, AtlasType keyType, AtlasType valueType, Object val) throws AtlasBaseException { if (isPrimitive(keyType.getTypeCategory()) && isPrimitive(valueType.getTypeCategory())) { return; } @@ -194,7 +195,7 @@ public class AtlasEntityGraphDiscoveryV1 implements EntityGraphDiscovery { } } - void visitCollectionReferences(final AtlasStructType parentType, final AtlasType attrType, final AtlasStructDef.AtlasAttributeDef attrDef, AtlasType elemType, Object val) throws AtlasBaseException { + void visitCollectionReferences(final AtlasStructType parentType, final AtlasType attrType, final AtlasAttributeDef attrDef, AtlasType elemType, Object val) throws AtlasBaseException { if (isPrimitive(elemType.getTypeCategory())) { return; @@ -228,7 +229,7 @@ public class AtlasEntityGraphDiscoveryV1 implements EntityGraphDiscovery { for (AtlasStructType.AtlasAttribute attribute : structType.getAllAttributes().values()) { AtlasType attrType = attribute.getAttributeType(); - Object attrVal = ((AtlasStruct) val).getAttribute(attribute.getAttributeDef().getName()); + Object attrVal = ((AtlasStruct) val).getAttribute(attribute.getName()); visitAttribute(structType, attrType, attribute.getAttributeDef(), attrVal); } } http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/5d8fae55/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasStructDefStoreV1.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasStructDefStoreV1.java b/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasStructDefStoreV1.java index 163ddef..7476c42 100644 --- a/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasStructDefStoreV1.java +++ b/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasStructDefStoreV1.java @@ -30,6 +30,7 @@ import org.apache.atlas.repository.graphdb.AtlasVertex; import org.apache.atlas.repository.store.graph.AtlasStructDefStore; import org.apache.atlas.repository.util.FilterUtil; import org.apache.atlas.type.AtlasArrayType; +import org.apache.atlas.type.AtlasEntityType; import org.apache.atlas.type.AtlasStructType; import org.apache.atlas.type.AtlasType; import org.apache.atlas.type.AtlasTypeRegistry; @@ -496,11 +497,18 @@ public class AtlasStructDefStoreV1 extends AtlasAbstractDefStoreV1 implements At } private static String toJsonFromAttributeDef(AtlasAttributeDef attributeDef, AtlasStructType structType) { - boolean isForeignKey = structType.isForeignKeyAttribute(attributeDef.getName()); - boolean isMappedFromRef = structType.isMappedFromRefAttribute(attributeDef.getName()); + boolean isComposite = false; String reverseAttribName = null; - if (isForeignKey) { // check if the referenced entity has foreignKeyRef to this attribute + if (structType instanceof AtlasEntityType) { + AtlasEntityType entityType = (AtlasEntityType)structType; + + isComposite = entityType.isMappedFromRefAttribute(attributeDef.getName()) || + entityType.isForeignKeyOnDeleteActionUpdate(attributeDef.getName()); + } + + // find the attribute in the referenced entity that has mappedFromRef to this attribute + if (structType.isForeignKeyAttribute(attributeDef.getName())) { AtlasType attribType = structType.getAttributeType(attributeDef.getName()); if (attribType.getTypeCategory() == org.apache.atlas.model.TypeCategory.ARRAY) { @@ -508,13 +516,13 @@ public class AtlasStructDefStoreV1 extends AtlasAbstractDefStoreV1 implements At } if (attribType.getTypeCategory() == org.apache.atlas.model.TypeCategory.ENTITY) { - reverseAttribName = ((AtlasStructType)attribType).getMappedFromRefAttribute(structType.getTypeName(), - attributeDef.getName()); + AtlasEntityType attribEntityType = (AtlasEntityType)attribType; + + reverseAttribName = attribEntityType.getMappedFromRefAttribute(structType.getTypeName(), + attributeDef.getName()); } } - boolean isComposite = isMappedFromRef || (isForeignKey && StringUtils.isBlank(reverseAttribName)); - Map<String, Object> attribInfo = new HashMap<>(); attribInfo.put("name", attributeDef.getName()); http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/5d8fae55/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/DeleteHandlerV1.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/DeleteHandlerV1.java b/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/DeleteHandlerV1.java index 34e02c6..4a0eb20 100644 --- a/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/DeleteHandlerV1.java +++ b/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/DeleteHandlerV1.java @@ -24,7 +24,7 @@ import org.apache.atlas.RequestContextV1; import org.apache.atlas.exception.AtlasBaseException; import org.apache.atlas.model.TypeCategory; import org.apache.atlas.model.instance.AtlasEntity; -import org.apache.atlas.model.typedef.AtlasStructDef; +import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef; import org.apache.atlas.repository.Constants; import org.apache.atlas.repository.graph.AtlasEdgeLabel; import org.apache.atlas.repository.graph.GraphHelper; @@ -137,11 +137,11 @@ public abstract class DeleteHandlerV1 { } for (AtlasStructType.AtlasAttribute attributeInfo : entityType.getAllAttributes().values()) { - if (!entityType.isMappedFromRefAttribute(attributeInfo.getAttributeDef().getName())) { + if (!entityType.isMappedFromRefAttribute(attributeInfo.getName())) { continue; } - String edgeLabel = AtlasGraphUtilsV1.getAttributeEdgeLabel(entityType, attributeInfo.getAttributeDef().getName()); - AtlasType attrType = typeRegistry.getType(attributeInfo.getAttributeDef().getTypeName()); + String edgeLabel = AtlasGraphUtilsV1.getAttributeEdgeLabel(entityType, attributeInfo.getName()); + AtlasType attrType = typeRegistry.getType(attributeInfo.getTypeName()); switch (attrType.getTypeCategory()) { case ENTITY: AtlasEdge edge = graphHelper.getEdgeForLabel(vertex, edgeLabel); @@ -172,7 +172,7 @@ public abstract class DeleteHandlerV1 { if (valueTypeCategory != TypeCategory.ENTITY) { continue; } - String propertyName = AtlasGraphUtilsV1.getQualifiedAttributePropertyKey(entityType, attributeInfo.getAttributeDef().getName()); + String propertyName = AtlasGraphUtilsV1.getQualifiedAttributePropertyKey(entityType, attributeInfo.getName()); List<String> keys = vertex.getProperty(propertyName, List.class); if (keys != null) { for (String key : keys) { @@ -277,19 +277,21 @@ public abstract class DeleteHandlerV1 { AtlasType parentType = typeRegistry.getType(typeName); if (parentType instanceof AtlasStructType) { + AtlasStructType structType = (AtlasStructType) parentType; + boolean isEntityType = (parentType instanceof AtlasEntityType); - AtlasStructType entityType = (AtlasStructType) parentType; - for (AtlasStructType.AtlasAttribute attributeInfo : getAttributes(entityType)) { - LOG.debug("Deleting attribute {} for {}", attributeInfo.getAttributeDef().getName(), string(instanceVertex)); + for (AtlasStructType.AtlasAttribute attributeInfo : getAttributes(structType)) { + LOG.debug("Deleting attribute {} for {}", attributeInfo.getName(), string(instanceVertex)); + boolean isComposite = isEntityType && ((AtlasEntityType)structType).isMappedFromRefAttribute(attributeInfo.getName()); - AtlasType attrType = typeRegistry.getType(attributeInfo.getAttributeType().getTypeName()); + AtlasType attrType = typeRegistry.getType(attributeInfo.getTypeName()); - String edgeLabel = AtlasGraphUtilsV1.getAttributeEdgeLabel(entityType, attributeInfo.getAttributeDef().getName()); + String edgeLabel = AtlasGraphUtilsV1.getAttributeEdgeLabel(structType, attributeInfo.getName()); switch (attrType.getTypeCategory()) { case ENTITY: //If its class attribute, delete the reference - deleteEdgeReference(instanceVertex, edgeLabel, TypeCategory.ENTITY, entityType.isMappedFromRefAttribute(attributeInfo.getAttributeDef().getName())); + deleteEdgeReference(instanceVertex, edgeLabel, TypeCategory.ENTITY, isComposite); break; case STRUCT: @@ -306,7 +308,7 @@ public abstract class DeleteHandlerV1 { if (edges != null) { while (edges.hasNext()) { AtlasEdge edge = edges.next(); - deleteEdgeReference(edge, elemType.getTypeCategory(), entityType.isMappedFromRefAttribute(attributeInfo.getAttributeDef().getName()), false); + deleteEdgeReference(edge, elemType.getTypeCategory(), isComposite, false); } } } @@ -317,14 +319,14 @@ public abstract class DeleteHandlerV1 { AtlasMapType mapType = (AtlasMapType) attrType; AtlasType keyType = mapType.getKeyType(); TypeCategory valueTypeCategory = mapType.getValueType().getTypeCategory(); - String propertyName = AtlasGraphUtilsV1.getQualifiedAttributePropertyKey(entityType, attributeInfo.getAttributeDef().getName()); + String propertyName = AtlasGraphUtilsV1.getQualifiedAttributePropertyKey(structType, attributeInfo.getName()); if (AtlasGraphUtilsV1.isReference(valueTypeCategory)) { List<Object> keys = ArrayVertexMapper.getArrayElementsProperty(keyType, instanceVertex, propertyName); if (keys != null) { for (Object key : keys) { String mapEdgeLabel = GraphHelper.getQualifiedNameForMapKey(edgeLabel, (String) key); - deleteEdgeReference(instanceVertex, mapEdgeLabel, valueTypeCategory, entityType.isMappedFromRefAttribute(attributeInfo.getAttributeDef().getName())); + deleteEdgeReference(instanceVertex, mapEdgeLabel, valueTypeCategory, isComposite); } } } @@ -359,7 +361,7 @@ public abstract class DeleteHandlerV1 { } } - protected AtlasStructDef.AtlasAttributeDef getAttributeForEdge(String edgeLabel) throws AtlasBaseException { + protected AtlasAttributeDef getAttributeForEdge(String edgeLabel) throws AtlasBaseException { AtlasEdgeLabel atlasEdgeLabel = new AtlasEdgeLabel(edgeLabel); AtlasType parentType = typeRegistry.getType(atlasEdgeLabel.getTypeName()); @@ -395,7 +397,7 @@ public abstract class DeleteHandlerV1 { String edgeLabel = EDGE_LABEL_PREFIX + propertyName; AtlasEdge edge = null; - AtlasStructDef.AtlasAttributeDef attrDef = parentType.getAttributeDef(attributeName); + AtlasAttributeDef attrDef = parentType.getAttributeDef(attributeName); AtlasType attrType = typeRegistry.getType(attrDef.getTypeName()); switch (attrType.getTypeCategory()) { @@ -518,7 +520,7 @@ public abstract class DeleteHandlerV1 { AtlasEntity.Status edgeState = AtlasGraphUtilsV1.getState(edge); if (edgeState == AtlasEntity.Status.ACTIVE) { //Delete only the active edge references - AtlasStructDef.AtlasAttributeDef attribute = getAttributeForEdge(edge.getLabel()); + AtlasAttributeDef attribute = getAttributeForEdge(edge.getLabel()); //TODO use delete edge instead?? deleteEdgeBetweenVertices(edge.getOutVertex(), edge.getInVertex(), attribute.getName()); } http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/5d8fae55/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 c98d340..f88661a 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 @@ -20,7 +20,6 @@ package org.apache.atlas.repository.store.graph.v1; import com.google.inject.Inject; import org.apache.atlas.AtlasErrorCode; -import org.apache.atlas.RequestContextV1; import org.apache.atlas.exception.AtlasBaseException; import org.apache.atlas.model.TypeCategory; import org.apache.atlas.model.instance.AtlasEntity; @@ -29,7 +28,7 @@ import org.apache.atlas.model.instance.AtlasObjectId; import org.apache.atlas.model.instance.AtlasStruct; import org.apache.atlas.model.instance.EntityMutationResponse; import org.apache.atlas.model.instance.EntityMutations; -import org.apache.atlas.model.typedef.AtlasStructDef; +import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef; import org.apache.atlas.repository.Constants; import org.apache.atlas.repository.RepositoryException; import org.apache.atlas.repository.graph.GraphHelper; @@ -109,7 +108,7 @@ public class EntityGraphMapper implements InstanceGraphMapper<AtlasEdge> { public void cleanUp() throws AtlasBaseException { } - private AtlasEdge updateEdge(AtlasStructDef.AtlasAttributeDef attributeDef, Object value, AtlasEdge currentEdge, final AtlasVertex entityVertex) throws AtlasBaseException { + private AtlasEdge updateEdge(AtlasAttributeDef attributeDef, Object value, AtlasEdge currentEdge, final AtlasVertex entityVertex) throws AtlasBaseException { LOG.debug("Updating entity reference {} for reference attribute {}", attributeDef.getName()); // Update edge if it exists @@ -180,7 +179,7 @@ public class EntityGraphMapper implements InstanceGraphMapper<AtlasEdge> { final Map<String, AtlasStructType.AtlasAttribute> allAttributes = type.getAllAttributes(); for (String attribute : allAttributes.keySet()) { AtlasType attributeType = allAttributes.get(attribute).getAttributeType(); - AtlasStructDef.AtlasAttributeDef attributeDef = allAttributes.get(attribute).getAttributeDef(); + AtlasAttributeDef attributeDef = allAttributes.get(attribute).getAttributeDef(); if ( header.getAttribute(attribute) == null && (TypeCategory.PRIMITIVE == attributeType.getTypeCategory())) { if ( attributeDef.getIsOptional()) { http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/5d8fae55/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/GraphMutationContext.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/GraphMutationContext.java b/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/GraphMutationContext.java index 89419d9..0a1b91d 100644 --- a/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/GraphMutationContext.java +++ b/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/GraphMutationContext.java @@ -21,6 +21,7 @@ package org.apache.atlas.repository.store.graph.v1; import com.google.common.base.Optional; import org.apache.atlas.model.instance.EntityMutations; import org.apache.atlas.model.typedef.AtlasStructDef; +import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef; import org.apache.atlas.repository.graphdb.AtlasEdge; import org.apache.atlas.repository.graphdb.AtlasVertex; import org.apache.atlas.type.AtlasStructType; @@ -166,7 +167,7 @@ public class GraphMutationContext { return attribute.getStructDef(); } - public AtlasStructDef.AtlasAttributeDef getAttributeDef() { + public AtlasAttributeDef getAttributeDef() { return attribute.getAttributeDef(); }
