Commit 5d8fae55 by kevalbhatt

ATLAS-1513: updated AtlasEntityType with methods to get foreign-key references;…

ATLAS-1513: updated AtlasEntityType with methods to get foreign-key references; added helper methods in AtlasAttribute
parent a7870cde
...@@ -470,6 +470,7 @@ public class AtlasStructDef extends AtlasBaseTypeDef implements Serializable { ...@@ -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_REF_ATTRIBUTE = "refAttribute";
public static final String CONSTRAINT_PARAM_ON_DELETE = "onDelete"; 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_CASCADE = "cascade";
public static final String CONSTRAINT_PARAM_VAL_UPDATE = "update";
private String type; // foreignKey/mappedFromRef/valueInRange private String type; // foreignKey/mappedFromRef/valueInRange
private Map<String, Object> params; // onDelete=cascade/refAttribute=attr2/min=0,max=23 private Map<String, Object> params; // onDelete=cascade/refAttribute=attr2/min=0,max=23
......
...@@ -260,7 +260,7 @@ public class AtlasClassificationType extends AtlasStructType { ...@@ -260,7 +260,7 @@ public class AtlasClassificationType extends AtlasStructType {
if (CollectionUtils.isNotEmpty(classificationDef.getAttributeDefs())) { if (CollectionUtils.isNotEmpty(classificationDef.getAttributeDefs())) {
for (AtlasAttributeDef attributeDef : classificationDef.getAttributeDefs()) { for (AtlasAttributeDef attributeDef : classificationDef.getAttributeDefs()) {
AtlasType type = typeRegistry.getType(attributeDef.getTypeName()); 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));
} }
} }
} }
......
...@@ -20,16 +20,20 @@ package org.apache.atlas.type; ...@@ -20,16 +20,20 @@ package org.apache.atlas.type;
import org.apache.atlas.AtlasErrorCode; import org.apache.atlas.AtlasErrorCode;
import org.apache.atlas.exception.AtlasBaseException; 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.AtlasEntity;
import org.apache.atlas.model.instance.AtlasObjectId; import org.apache.atlas.model.instance.AtlasObjectId;
import org.apache.atlas.model.typedef.AtlasEntityDef; import org.apache.atlas.model.typedef.AtlasEntityDef;
import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef; 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.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
...@@ -37,6 +41,8 @@ import java.util.List; ...@@ -37,6 +41,8 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import static org.apache.atlas.model.typedef.AtlasStructDef.AtlasConstraintDef.*;
/** /**
* class that implements behaviour of an entity-type. * class that implements behaviour of an entity-type.
*/ */
...@@ -45,9 +51,11 @@ public class AtlasEntityType extends AtlasStructType { ...@@ -45,9 +51,11 @@ public class AtlasEntityType extends AtlasStructType {
private final AtlasEntityDef entityDef; private final AtlasEntityDef entityDef;
private List<AtlasEntityType> superTypes = Collections.emptyList(); private List<AtlasEntityType> superTypes = Collections.emptyList();
private Set<String> allSuperTypes = Collections.emptySet(); private Set<String> allSuperTypes = Collections.emptySet();
private Set<String> allSubTypes = 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) { public AtlasEntityType(AtlasEntityDef entityDef) {
super(entityDef); super(entityDef);
...@@ -88,23 +96,22 @@ public class AtlasEntityType extends AtlasStructType { ...@@ -88,23 +96,22 @@ public class AtlasEntityType extends AtlasStructType {
this.superTypes = Collections.unmodifiableList(s); this.superTypes = Collections.unmodifiableList(s);
this.allSuperTypes = Collections.unmodifiableSet(allS); this.allSuperTypes = Collections.unmodifiableSet(allS);
this.allAttributes = Collections.unmodifiableMap(allA); 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 @Override
public void resolveReferencesPhase2(AtlasTypeRegistry typeRegistry) throws AtlasBaseException { public void resolveReferencesPhase2(AtlasTypeRegistry typeRegistry) throws AtlasBaseException {
super.resolveReferencesPhase2(typeRegistry); super.resolveReferencesPhase2(typeRegistry);
mappedFromRefAttributes = Collections.unmodifiableMap(resolveMappedFromRefConstraint(allAttributes.values()));
for (String superTypeName : allSuperTypes) { for (String superTypeName : allSuperTypes) {
AtlasEntityType superType = typeRegistry.getEntityTypeByName(superTypeName); AtlasEntityType superType = typeRegistry.getEntityTypeByName(superTypeName);
superType.addSubType(this); superType.addSubType(this);
} }
} }
private void addSubType(AtlasEntityType subType) {
allSubTypes.add(subType.getTypeName());
}
public Set<String> getSuperTypes() { public Set<String> getSuperTypes() {
return entityDef.getSuperTypes(); return entityDef.getSuperTypes();
} }
...@@ -113,8 +120,47 @@ public class AtlasEntityType extends AtlasStructType { ...@@ -113,8 +120,47 @@ public class AtlasEntityType extends AtlasStructType {
return allSuperTypes; return allSuperTypes;
} }
public Set<String> getAllSubTypes() { public Set<String> getAllSubTypes() { return Collections.unmodifiableSet(allSubTypes); }
return 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) { public boolean isSuperTypeOf(AtlasEntityType entityType) {
...@@ -184,7 +230,7 @@ public class AtlasEntityType extends AtlasStructType { ...@@ -184,7 +230,7 @@ public class AtlasEntityType extends AtlasStructType {
@Override @Override
public AtlasAttribute getAttribute(String attributeName) { public AtlasAttribute getAttribute(String attributeName) {
return findAttribute(allAttributes.values(), attributeName); return allAttributes.get(attributeName);
} }
@Override @Override
...@@ -238,6 +284,14 @@ public class AtlasEntityType extends AtlasStructType { ...@@ -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, private void getTypeHierarchyInfo(AtlasTypeRegistry typeRegistry,
Set<String> allSuperTypeNames, Set<String> allSuperTypeNames,
Map<String, AtlasAttribute> allAttributes) throws AtlasBaseException { Map<String, AtlasAttribute> allAttributes) throws AtlasBaseException {
...@@ -276,9 +330,80 @@ public class AtlasEntityType extends AtlasStructType { ...@@ -276,9 +330,80 @@ public class AtlasEntityType extends AtlasStructType {
for (AtlasAttributeDef attributeDef : entityDef.getAttributeDefs()) { for (AtlasAttributeDef attributeDef : entityDef.getAttributeDefs()) {
AtlasType type = typeRegistry.getType(attributeDef.getTypeName()); 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) { private boolean validateAtlasObjectId(AtlasObjectId objId) {
...@@ -293,4 +418,43 @@ public class AtlasEntityType extends AtlasStructType { ...@@ -293,4 +418,43 @@ public class AtlasEntityType extends AtlasStructType {
return AtlasEntity.isAssigned(objId.getGuid()) || AtlasEntity.isUnAssigned((objId.getGuid())); 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;
}
}
} }
...@@ -20,14 +20,13 @@ package org.apache.atlas.type; ...@@ -20,14 +20,13 @@ package org.apache.atlas.type;
import org.apache.atlas.AtlasErrorCode; import org.apache.atlas.AtlasErrorCode;
import org.apache.atlas.exception.AtlasBaseException; import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.model.TypeCategory; 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.instance.AtlasStruct;
import org.apache.atlas.model.typedef.AtlasStructDef; import org.apache.atlas.model.typedef.AtlasStructDef;
import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef; import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef;
import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef.Cardinality; import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef.Cardinality;
import org.apache.atlas.model.typedef.AtlasStructDef.AtlasConstraintDef; import org.apache.atlas.model.typedef.AtlasStructDef.AtlasConstraintDef;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -40,9 +39,6 @@ import java.util.List; ...@@ -40,9 +39,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; 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. * class that implements behaviour of a struct-type.
...@@ -52,9 +48,8 @@ public class AtlasStructType extends AtlasType { ...@@ -52,9 +48,8 @@ public class AtlasStructType extends AtlasType {
private final AtlasStructDef structDef; private final AtlasStructDef structDef;
private Set<String> foreignKeyAttributes = new HashSet<>(); private Map<String, AtlasConstraintDef> foreignKeyAttributes = Collections.emptyMap();
private Map<String, TypeAttributePair> mappedFromRefAttributes = new HashMap<>(); protected Map<String, AtlasAttribute> allAttributes = Collections.emptyMap();
protected Map<String, AtlasAttribute> allAttributes = Collections.emptyMap();
public AtlasStructType(AtlasStructDef structDef) { public AtlasStructType(AtlasStructDef structDef) {
super(structDef); super(structDef);
...@@ -73,47 +68,60 @@ public class AtlasStructType extends AtlasType { ...@@ -73,47 +68,60 @@ public class AtlasStructType extends AtlasType {
public AtlasStructDef getStructDef() { return structDef; } public AtlasStructDef getStructDef() { return structDef; }
public AtlasType getAttributeType(String attributeName) { public AtlasType getAttributeType(String attributeName) {
AtlasAttribute attribute = allAttributes.get(attributeName); AtlasAttribute attribute = getAttribute(attributeName);
return attribute != null ? attribute.getAttributeType() : null; 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) { public boolean isForeignKeyAttribute(String attributeName) {
return foreignKeyAttributes.contains(attributeName); return foreignKeyAttributes.containsKey(attributeName);
} }
public boolean isMappedFromRefAttribute(String attributeName) { public AtlasConstraintDef getForeignKeyConstraint(String attributeName) {
return mappedFromRefAttributes.containsKey(attributeName); return foreignKeyAttributes.get(attributeName);
} }
public String getMappedFromRefAttribute(String typeName, String attribName) { public String getForeignKeyOnDeleteAction(String attributeName) {
String ret = null; String ret = null;
for (Map.Entry<String, TypeAttributePair> e : mappedFromRefAttributes.entrySet()) { AtlasConstraintDef fkConstraint = getForeignKeyConstraint(attributeName);
String refTypeName = e.getValue().typeName;
String refAttribName = e.getValue().attributeName;
if(StringUtils.equals(refTypeName, typeName) && StringUtils.equals(refAttribName, attribName)) { if (fkConstraint != null && MapUtils.isNotEmpty(fkConstraint.getParams())) {
ret = e.getKey(); Object onDeleteAction = fkConstraint.getParams().get(AtlasConstraintDef.CONSTRAINT_PARAM_ON_DELETE);
break; if (onDeleteAction != null) {
ret = onDeleteAction.toString();
} }
} }
return ret; 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 @Override
public void resolveReferences(AtlasTypeRegistry typeRegistry) throws AtlasBaseException { public void resolveReferences(AtlasTypeRegistry typeRegistry) throws AtlasBaseException {
Map<String, AtlasAttribute> a = new HashMap<>(); Map<String, AtlasAttribute> a = new HashMap<>();
for (AtlasAttributeDef attributeDef : structDef.getAttributeDefs()) { for (AtlasAttributeDef attributeDef : structDef.getAttributeDefs()) {
AtlasType attrType = typeRegistry.getType(attributeDef.getTypeName());
AtlasType attrType = typeRegistry.getType(attributeDef.getTypeName()); AtlasAttribute attribute = new AtlasAttribute(this, attributeDef, attrType);
AtlasAttribute attribute = new AtlasAttribute(this, structDef, attributeDef, attrType);
resolveConstraints(attributeDef, attrType);
Cardinality cardinality = attributeDef.getCardinality(); Cardinality cardinality = attributeDef.getCardinality();
...@@ -133,6 +141,25 @@ public class AtlasStructType extends AtlasType { ...@@ -133,6 +141,25 @@ public class AtlasStructType extends AtlasType {
} }
this.allAttributes = Collections.unmodifiableMap(a); 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 @Override
...@@ -149,22 +176,7 @@ public class AtlasStructType extends AtlasType { ...@@ -149,22 +176,7 @@ public class AtlasStructType extends AtlasType {
} }
public AtlasAttribute getAttribute(String attributeName) { public AtlasAttribute getAttribute(String attributeName) {
return findAttribute(allAttributes.values(), attributeName); return allAttributes.get(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;
} }
@Override @Override
...@@ -374,116 +386,55 @@ public class AtlasStructType extends AtlasType { ...@@ -374,116 +386,55 @@ public class AtlasStructType extends AtlasType {
return null; 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: * valid conditions for foreign-key constraint:
* - supported only in entity-type * - supported only in entity-type
* - attribute should be an entity-type or an array of entity-type * - attribute should be an entity-type or an array of entity-type
*/ */
private void resolveForeignKeyConstraint(AtlasAttributeDef attribDef, AtlasConstraintDef constraintDef, private Map<String, AtlasConstraintDef> resolveForeignKeyConstraints(Collection<AtlasAttribute> attributes)
AtlasType attribType) throws AtlasBaseException { throws AtlasBaseException {
if (this.getTypeCategory() != TypeCategory.ENTITY) { Map<String, AtlasConstraintDef> ret = null;
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());
}
foreignKeyAttributes.add(attribDef.getName()); for (AtlasAttribute attribute : attributes) {
} AtlasAttributeDef attribDef = attribute.getAttributeDef();
/* if (CollectionUtils.isEmpty(attribDef.getConstraintDefs())) {
* valid conditions for mapped-from-ref constraint: continue;
* - 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 (this.getTypeCategory() != TypeCategory.ENTITY) { for (AtlasConstraintDef constraintDef : attribDef.getConstraintDefs()) {
throw new AtlasBaseException(AtlasErrorCode.UNSUPPORTED_CONSTRAINT, getTypeName(), if (!StringUtils.equals(constraintDef.getType(), AtlasConstraintDef.CONSTRAINT_TYPE_FOREIGN_KEY)) {
attribDef.getName(), CONSTRAINT_TYPE_MAPPED_FROM_REF); continue;
} }
if (attribType.getTypeCategory() == TypeCategory.ARRAY) { if (this.getTypeCategory() != TypeCategory.ENTITY) {
attribType = ((AtlasArrayType)attribType).getElementType(); throw new AtlasBaseException(AtlasErrorCode.UNSUPPORTED_CONSTRAINT,
} AtlasConstraintDef.CONSTRAINT_TYPE_FOREIGN_KEY, getTypeName(), attribute.getName());
}
if (attribType.getTypeCategory() != TypeCategory.ENTITY) { AtlasType attribType = attribute.getAttributeType();
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 (attribType.getTypeCategory() == TypeCategory.ARRAY) {
attribType = ((AtlasArrayType) attribType).getElementType();
}
if (StringUtils.isBlank(refAttribName)) { if (attribType.getTypeCategory() != TypeCategory.ENTITY) {
throw new AtlasBaseException(AtlasErrorCode.CONSTRAINT_MISSING_PARAMS, throw new AtlasBaseException(AtlasErrorCode.CONSTRAINT_NOT_SATISFIED,
getTypeName(), attribDef.getName(), getTypeName(), attribute.getName(), AtlasConstraintDef.CONSTRAINT_TYPE_FOREIGN_KEY,
CONSTRAINT_PARAM_REF_ATTRIBUTE, CONSTRAINT_TYPE_MAPPED_FROM_REF, attribType.getTypeName());
String.valueOf(constraintDef.getParams())); }
}
AtlasStructType structType = (AtlasStructType) attribType; if (ret == null) {
AtlasAttributeDef refAttrib = structType.getStructDef().getAttribute(refAttribName); ret = new HashMap<>();
}
if (refAttrib == null) { ret.put(attribute.getName(), constraintDef);
throw new AtlasBaseException(AtlasErrorCode.CONSTRAINT_NOT_EXIST,
getTypeName(), attribDef.getName(),
CONSTRAINT_PARAM_REF_ATTRIBUTE, structType.getTypeName(), refAttribName);
}
if (!StringUtils.equals(getTypeName(), refAttrib.getTypeName())) { break;
throw new AtlasBaseException(AtlasErrorCode.CONSTRAINT_NOT_MATCHED, }
getTypeName(), attribDef.getName(),
CONSTRAINT_PARAM_REF_ATTRIBUTE, structType.getTypeName(), refAttribName,
getTypeName(), refAttrib.getTypeName());
} }
mappedFromRefAttributes.put(attribDef.getName(), new TypeAttributePair(attribType.getTypeName(), refAttribName)); return ret == null ? Collections.<String, AtlasConstraintDef>emptyMap() : ret;
}
private class TypeAttributePair {
public final String typeName;
public final String attributeName;
public TypeAttributePair(String typeName, String attributeName) {
this.typeName = typeName;
this.attributeName = attributeName;
}
} }
public String getQualifiedAttributeName(String attrName) throws AtlasBaseException { public String getQualifiedAttributeName(String attrName) throws AtlasBaseException {
...@@ -496,31 +447,21 @@ public class AtlasStructType extends AtlasType { ...@@ -496,31 +447,21 @@ public class AtlasStructType extends AtlasType {
public static class AtlasAttribute { public static class AtlasAttribute {
private final AtlasStructType structType; private final AtlasStructType structType;
private final AtlasStructDef structDef; private final AtlasType attributeType;
private final AtlasType attributeType;
private final AtlasAttributeDef attributeDef; private final AtlasAttributeDef attributeDef;
private final String qualifiedName; private final String qualifiedName;
public AtlasAttribute(AtlasStructType structType, AtlasStructDef structDef, AtlasAttributeDef attrDef, AtlasType attributeType) { public AtlasAttribute(AtlasStructType structType, AtlasAttributeDef attrDef, AtlasType attributeType) {
this.structType = structType; this.structType = structType;
this.structDef = structDef; this.attributeDef = attrDef;
this.attributeDef = attrDef;
this.attributeType = attributeType; this.attributeType = attributeType;
this.qualifiedName = getQualifiedAttributeName(structDef, attributeDef.getName()); this.qualifiedName = getQualifiedAttributeName(structType.getStructDef(), attributeDef.getName());
} }
public AtlasStructType getStructType() { public AtlasStructType getStructType() { return structType; }
return structType;
}
public String getQualifiedName() { public AtlasStructDef getStructDef() { return structType.getStructDef(); }
return qualifiedName;
}
public AtlasStructDef getStructDef() {
return structDef;
}
public AtlasType getAttributeType() { public AtlasType getAttributeType() {
return attributeType; return attributeType;
...@@ -530,6 +471,12 @@ public class AtlasStructType extends AtlasType { ...@@ -530,6 +471,12 @@ public class AtlasStructType extends AtlasType {
return attributeDef; return attributeDef;
} }
public String getName() { return attributeDef.getName(); }
public String getTypeName() { return attributeDef.getTypeName(); }
public String getQualifiedName() { return qualifiedName; }
public String getQualifiedAttributeName() { public String getQualifiedAttributeName() {
return qualifiedName; return qualifiedName;
} }
......
...@@ -29,6 +29,7 @@ import org.apache.atlas.model.typedef.AtlasEnumDef.AtlasEnumElementDef; ...@@ -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;
import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef; import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef;
import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef.Cardinality; 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.AtlasTypeDefHeader;
import org.apache.atlas.model.typedef.AtlasTypesDef; import org.apache.atlas.model.typedef.AtlasTypesDef;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
...@@ -153,54 +154,54 @@ public class AtlasTypeUtil { ...@@ -153,54 +154,54 @@ public class AtlasTypeUtil {
return new AtlasAttributeDef(name, dataType.getTypeName(), true, return new AtlasAttributeDef(name, dataType.getTypeName(), true,
Cardinality.SINGLE, 0, 1, Cardinality.SINGLE, 0, 1,
false, false, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()); Collections.<AtlasConstraintDef>emptyList());
} }
public static AtlasAttributeDef createOptionalAttrDef(String name, String dataType) { public static AtlasAttributeDef createOptionalAttrDef(String name, String dataType) {
return new AtlasAttributeDef(name, dataType, true, return new AtlasAttributeDef(name, dataType, true,
Cardinality.SINGLE, 0, 1, Cardinality.SINGLE, 0, 1,
false, false, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()); Collections.<AtlasConstraintDef>emptyList());
} }
public static AtlasAttributeDef createRequiredAttrDef(String name, String dataType) { public static AtlasAttributeDef createRequiredAttrDef(String name, String dataType) {
return new AtlasAttributeDef(name, dataType, false, return new AtlasAttributeDef(name, dataType, false,
Cardinality.SINGLE, 1, 1, Cardinality.SINGLE, 1, 1,
false, true, false, true,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()); Collections.<AtlasConstraintDef>emptyList());
} }
public static AtlasAttributeDef createListRequiredAttrDef(String name, String dataType) { public static AtlasAttributeDef createListRequiredAttrDef(String name, String dataType) {
return new AtlasAttributeDef(name, dataType, false, return new AtlasAttributeDef(name, dataType, false,
Cardinality.LIST, 1, Integer.MAX_VALUE, Cardinality.LIST, 1, Integer.MAX_VALUE,
false, true, false, true,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()); Collections.<AtlasConstraintDef>emptyList());
} }
public static AtlasAttributeDef createOptionalListAttrDef(String name, String dataType) { public static AtlasAttributeDef createOptionalListAttrDef(String name, String dataType) {
return new AtlasAttributeDef(name, dataType, true, return new AtlasAttributeDef(name, dataType, true,
Cardinality.LIST, 1, Integer.MAX_VALUE, Cardinality.LIST, 1, Integer.MAX_VALUE,
false, true, false, true,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()); Collections.<AtlasConstraintDef>emptyList());
} }
public static AtlasAttributeDef createRequiredListAttrDefWithConstraint(String name, String dataType, String type, Map param) { public static AtlasAttributeDef createRequiredListAttrDefWithConstraint(String name, String dataType, String type, Map param) {
AtlasAttributeDef ret = AtlasTypeUtil.createListRequiredAttrDef(name, dataType); AtlasAttributeDef ret = AtlasTypeUtil.createListRequiredAttrDef(name, dataType);
ret.addConstraint(new AtlasStructDef.AtlasConstraintDef(type, param)); ret.addConstraint(new AtlasConstraintDef(type, param));
return ret; return ret;
} }
public static AtlasAttributeDef createRequiredAttrDefWithConstraint(String name, String typeName, String type, Map param) { public static AtlasAttributeDef createRequiredAttrDefWithConstraint(String name, String typeName, String type, Map param) {
AtlasAttributeDef ret = AtlasTypeUtil.createRequiredAttrDef(name, typeName); AtlasAttributeDef ret = AtlasTypeUtil.createRequiredAttrDef(name, typeName);
ret.addConstraint(new AtlasStructDef.AtlasConstraintDef(type, param)); ret.addConstraint(new AtlasConstraintDef(type, param));
return ret; return ret;
} }
public static AtlasAttributeDef createOptionalAttrDefWithConstraint(String name, String typeName, String type, Map param) { public static AtlasAttributeDef createOptionalAttrDefWithConstraint(String name, String typeName, String type, Map param) {
AtlasAttributeDef ret = AtlasTypeUtil.createOptionalAttrDef(name, typeName); AtlasAttributeDef ret = AtlasTypeUtil.createOptionalAttrDef(name, typeName);
ret.addConstraint(new AtlasStructDef.AtlasConstraintDef(type, param)); ret.addConstraint(new AtlasConstraintDef(type, param));
return ret; return ret;
} }
...@@ -209,21 +210,21 @@ public class AtlasTypeUtil { ...@@ -209,21 +210,21 @@ public class AtlasTypeUtil {
return new AtlasAttributeDef(name, dataType.getTypeName(), false, return new AtlasAttributeDef(name, dataType.getTypeName(), false,
Cardinality.SINGLE, 1, 1, Cardinality.SINGLE, 1, 1,
true, true, true, true,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()); Collections.<AtlasConstraintDef>emptyList());
} }
public static AtlasAttributeDef createUniqueRequiredAttrDef(String name, String typeName) { public static AtlasAttributeDef createUniqueRequiredAttrDef(String name, String typeName) {
return new AtlasAttributeDef(name, typeName, false, return new AtlasAttributeDef(name, typeName, false,
Cardinality.SINGLE, 1, 1, Cardinality.SINGLE, 1, 1,
true, true, true, true,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()); Collections.<AtlasConstraintDef>emptyList());
} }
public static AtlasAttributeDef createRequiredAttrDef(String name, AtlasType dataType) { public static AtlasAttributeDef createRequiredAttrDef(String name, AtlasType dataType) {
return new AtlasAttributeDef(name, dataType.getTypeName(), false, return new AtlasAttributeDef(name, dataType.getTypeName(), false,
Cardinality.SINGLE, 1, 1, Cardinality.SINGLE, 1, 1,
false, true, false, true,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()); Collections.<AtlasConstraintDef>emptyList());
} }
public static AtlasEnumDef createEnumTypeDef(String name, String description, AtlasEnumElementDef... enumValues) { public static AtlasEnumDef createEnumTypeDef(String name, String description, AtlasEnumElementDef... enumValues) {
......
...@@ -29,6 +29,7 @@ import org.apache.atlas.model.typedef.AtlasEnumDef; ...@@ -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.AtlasEnumDef.AtlasEnumElementDef;
import org.apache.atlas.model.typedef.AtlasStructDef; import org.apache.atlas.model.typedef.AtlasStructDef;
import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef; 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.model.typedef.AtlasTypesDef;
import org.apache.atlas.type.AtlasTypeUtil; import org.apache.atlas.type.AtlasTypeUtil;
import org.apache.commons.lang.RandomStringUtils; import org.apache.commons.lang.RandomStringUtils;
...@@ -84,12 +85,12 @@ public final class TestUtilsV2 { ...@@ -84,12 +85,12 @@ public final class TestUtilsV2 {
AtlasTypeUtil.createUniqueRequiredAttrDef("name", "string"), AtlasTypeUtil.createUniqueRequiredAttrDef("name", "string"),
new AtlasAttributeDef("employees", String.format("array<%s>", "Employee"), true, new AtlasAttributeDef("employees", String.format("array<%s>", "Employee"), true,
AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false,
new ArrayList<AtlasStructDef.AtlasConstraintDef>())); new ArrayList<AtlasConstraintDef>()));
deptTypeDef.getAttribute("employees").addConstraint( deptTypeDef.getAttribute("employees").addConstraint(
new AtlasStructDef.AtlasConstraintDef( new AtlasConstraintDef(
AtlasStructDef.AtlasConstraintDef.CONSTRAINT_TYPE_MAPPED_FROM_REF, new HashMap<String, Object>() {{ AtlasConstraintDef.CONSTRAINT_TYPE_MAPPED_FROM_REF, new HashMap<String, Object>() {{
put(AtlasStructDef.AtlasConstraintDef.CONSTRAINT_PARAM_REF_ATTRIBUTE, "department"); put(AtlasConstraintDef.CONSTRAINT_PARAM_REF_ATTRIBUTE, "department");
}})); }}));
AtlasEntityDef personTypeDef = AtlasTypeUtil.createClassTypeDef("Person", "Person"+_description, ImmutableSet.<String>of(), AtlasEntityDef personTypeDef = AtlasTypeUtil.createClassTypeDef("Person", "Person"+_description, ImmutableSet.<String>of(),
...@@ -110,29 +111,29 @@ public final class TestUtilsV2 { ...@@ -110,29 +111,29 @@ public final class TestUtilsV2 {
new AtlasAttributeDef("department", "Department", false, new AtlasAttributeDef("department", "Department", false,
AtlasAttributeDef.Cardinality.SINGLE, 1, 1, AtlasAttributeDef.Cardinality.SINGLE, 1, 1,
false, false, false, false,
new ArrayList<AtlasStructDef.AtlasConstraintDef>()), new ArrayList<AtlasConstraintDef>()),
new AtlasAttributeDef("manager", "Manager", true, new AtlasAttributeDef("manager", "Manager", true,
AtlasAttributeDef.Cardinality.SINGLE, 0, 1, AtlasAttributeDef.Cardinality.SINGLE, 0, 1,
false, false, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), Collections.<AtlasConstraintDef>emptyList()),
new AtlasAttributeDef("mentor", EMPLOYEE_TYPE, true, new AtlasAttributeDef("mentor", EMPLOYEE_TYPE, true,
AtlasAttributeDef.Cardinality.SINGLE, 0, 1, AtlasAttributeDef.Cardinality.SINGLE, 0, 1,
false, false, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), Collections.<AtlasConstraintDef>emptyList()),
AtlasTypeUtil.createOptionalAttrDef("shares", "long"), AtlasTypeUtil.createOptionalAttrDef("shares", "long"),
AtlasTypeUtil.createOptionalAttrDef("salary", "double") AtlasTypeUtil.createOptionalAttrDef("salary", "double")
); );
employeeTypeDef.getAttribute("department").addConstraint( employeeTypeDef.getAttribute("department").addConstraint(
new AtlasStructDef.AtlasConstraintDef( new AtlasConstraintDef(
AtlasStructDef.AtlasConstraintDef.CONSTRAINT_TYPE_FOREIGN_KEY, new HashMap<String, Object>() {{ AtlasConstraintDef.CONSTRAINT_TYPE_FOREIGN_KEY, new HashMap<String, Object>() {{
put(AtlasStructDef.AtlasConstraintDef.CONSTRAINT_PARAM_ON_DELETE, AtlasStructDef.AtlasConstraintDef.CONSTRAINT_PARAM_VAL_CASCADE); put(AtlasConstraintDef.CONSTRAINT_PARAM_ON_DELETE, AtlasConstraintDef.CONSTRAINT_PARAM_VAL_CASCADE);
}})); }}));
AtlasEntityDef managerTypeDef = AtlasTypeUtil.createClassTypeDef("Manager", "Manager"+_description, ImmutableSet.of("Employee"), AtlasEntityDef managerTypeDef = AtlasTypeUtil.createClassTypeDef("Manager", "Manager"+_description, ImmutableSet.of("Employee"),
new AtlasAttributeDef("subordinates", String.format("array<%s>", "Employee"), false, AtlasAttributeDef.Cardinality.SET, new AtlasAttributeDef("subordinates", String.format("array<%s>", "Employee"), false, AtlasAttributeDef.Cardinality.SET,
1, 10, false, false, 1, 10, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList())); Collections.<AtlasConstraintDef>emptyList()));
AtlasClassificationDef securityClearanceTypeDef = AtlasClassificationDef securityClearanceTypeDef =
AtlasTypeUtil.createTraitTypeDef("SecurityClearance", "SecurityClearance"+_description, ImmutableSet.<String>of(), AtlasTypeUtil.createTraitTypeDef("SecurityClearance", "SecurityClearance"+_description, ImmutableSet.<String>of(),
...@@ -165,7 +166,7 @@ public final class TestUtilsV2 { ...@@ -165,7 +166,7 @@ public final class TestUtilsV2 {
AtlasTypeUtil.createOptionalAttrDef("dep-code", "string"), AtlasTypeUtil.createOptionalAttrDef("dep-code", "string"),
new AtlasAttributeDef("employees", String.format("array<%s>", "Employee"), true, new AtlasAttributeDef("employees", String.format("array<%s>", "Employee"), true,
AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList())); Collections.<AtlasConstraintDef>emptyList()));
AtlasEntityDef personTypeDef = AtlasTypeUtil.createClassTypeDef("Person", "Person"+_description, AtlasEntityDef personTypeDef = AtlasTypeUtil.createClassTypeDef("Person", "Person"+_description,
ImmutableSet.<String>of(), ImmutableSet.<String>of(),
...@@ -189,15 +190,15 @@ public final class TestUtilsV2 { ...@@ -189,15 +190,15 @@ public final class TestUtilsV2 {
new AtlasAttributeDef("department", "Department", false, new AtlasAttributeDef("department", "Department", false,
AtlasAttributeDef.Cardinality.SINGLE, 1, 1, AtlasAttributeDef.Cardinality.SINGLE, 1, 1,
false, false, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), Collections.<AtlasConstraintDef>emptyList()),
new AtlasAttributeDef("manager", "Manager", true, new AtlasAttributeDef("manager", "Manager", true,
AtlasAttributeDef.Cardinality.SINGLE, 0, 1, AtlasAttributeDef.Cardinality.SINGLE, 0, 1,
false, false, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), Collections.<AtlasConstraintDef>emptyList()),
new AtlasAttributeDef("mentor", EMPLOYEE_TYPE, true, new AtlasAttributeDef("mentor", EMPLOYEE_TYPE, true,
AtlasAttributeDef.Cardinality.SINGLE, 0, 1, AtlasAttributeDef.Cardinality.SINGLE, 0, 1,
false, false, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), Collections.<AtlasConstraintDef>emptyList()),
AtlasTypeUtil.createOptionalAttrDef("shares", "long"), AtlasTypeUtil.createOptionalAttrDef("shares", "long"),
AtlasTypeUtil.createOptionalAttrDef("salary", "double") AtlasTypeUtil.createOptionalAttrDef("salary", "double")
...@@ -207,7 +208,7 @@ public final class TestUtilsV2 { ...@@ -207,7 +208,7 @@ public final class TestUtilsV2 {
ImmutableSet.of("Employee"), ImmutableSet.of("Employee"),
new AtlasAttributeDef("subordinates", String.format("array<%s>", "Employee"), false, AtlasAttributeDef.Cardinality.SET, new AtlasAttributeDef("subordinates", String.format("array<%s>", "Employee"), false, AtlasAttributeDef.Cardinality.SET,
1, 10, false, false, 1, 10, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList())); Collections.<AtlasConstraintDef>emptyList()));
AtlasClassificationDef securityClearanceTypeDef = AtlasClassificationDef securityClearanceTypeDef =
AtlasTypeUtil.createTraitTypeDef("SecurityClearance", "SecurityClearance"+_description, ImmutableSet.<String>of(), AtlasTypeUtil.createTraitTypeDef("SecurityClearance", "SecurityClearance"+_description, ImmutableSet.<String>of(),
...@@ -242,7 +243,7 @@ public final class TestUtilsV2 { ...@@ -242,7 +243,7 @@ public final class TestUtilsV2 {
AtlasTypeUtil.createRequiredAttrDef("dep-code", "string"), AtlasTypeUtil.createRequiredAttrDef("dep-code", "string"),
new AtlasAttributeDef("employees", String.format("array<%s>", "Person"), true, new AtlasAttributeDef("employees", String.format("array<%s>", "Person"), true,
AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList())); Collections.<AtlasConstraintDef>emptyList()));
AtlasEntityDef personTypeDef = AtlasTypeUtil.createClassTypeDef("Person", "Person"+_description, ImmutableSet.<String>of(), AtlasEntityDef personTypeDef = AtlasTypeUtil.createClassTypeDef("Person", "Person"+_description, ImmutableSet.<String>of(),
AtlasTypeUtil.createRequiredAttrDef("name", "string"), AtlasTypeUtil.createRequiredAttrDef("name", "string"),
...@@ -252,15 +253,15 @@ public final class TestUtilsV2 { ...@@ -252,15 +253,15 @@ public final class TestUtilsV2 {
new AtlasAttributeDef("department", "Department", false, new AtlasAttributeDef("department", "Department", false,
AtlasAttributeDef.Cardinality.SINGLE, 1, 1, AtlasAttributeDef.Cardinality.SINGLE, 1, 1,
false, false, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), Collections.<AtlasConstraintDef>emptyList()),
new AtlasAttributeDef("manager", "Manager", true, new AtlasAttributeDef("manager", "Manager", true,
AtlasAttributeDef.Cardinality.SINGLE, 0, 1, AtlasAttributeDef.Cardinality.SINGLE, 0, 1,
false, false, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), Collections.<AtlasConstraintDef>emptyList()),
new AtlasAttributeDef("mentor", "Person", true, new AtlasAttributeDef("mentor", "Person", true,
AtlasAttributeDef.Cardinality.SINGLE, 0, 1, AtlasAttributeDef.Cardinality.SINGLE, 0, 1,
false, false, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), Collections.<AtlasConstraintDef>emptyList()),
AtlasTypeUtil.createOptionalAttrDef("birthday", "date"), AtlasTypeUtil.createOptionalAttrDef("birthday", "date"),
AtlasTypeUtil.createOptionalAttrDef("hasPets", "boolean"), AtlasTypeUtil.createOptionalAttrDef("hasPets", "boolean"),
AtlasTypeUtil.createOptionalAttrDef("numberOfCars", "byte"), AtlasTypeUtil.createOptionalAttrDef("numberOfCars", "byte"),
...@@ -474,23 +475,23 @@ public final class TestUtilsV2 { ...@@ -474,23 +475,23 @@ public final class TestUtilsV2 {
new AtlasAttributeDef("location", "string", true, new AtlasAttributeDef("location", "string", true,
AtlasAttributeDef.Cardinality.SINGLE, 0, 1, AtlasAttributeDef.Cardinality.SINGLE, 0, 1,
false, false, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), Collections.<AtlasConstraintDef>emptyList()),
new AtlasAttributeDef("inputFormat", "string", true, new AtlasAttributeDef("inputFormat", "string", true,
AtlasAttributeDef.Cardinality.SINGLE, 0, 1, AtlasAttributeDef.Cardinality.SINGLE, 0, 1,
false, false, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), Collections.<AtlasConstraintDef>emptyList()),
new AtlasAttributeDef("outputFormat", "string", true, new AtlasAttributeDef("outputFormat", "string", true,
AtlasAttributeDef.Cardinality.SINGLE, 0, 1, AtlasAttributeDef.Cardinality.SINGLE, 0, 1,
false, false, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), Collections.<AtlasConstraintDef>emptyList()),
new AtlasAttributeDef("compressed", "boolean", false, new AtlasAttributeDef("compressed", "boolean", false,
AtlasAttributeDef.Cardinality.SINGLE, 1, 1, AtlasAttributeDef.Cardinality.SINGLE, 1, 1,
false, false, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), Collections.<AtlasConstraintDef>emptyList()),
new AtlasAttributeDef("numBuckets", "int", true, new AtlasAttributeDef("numBuckets", "int", true,
AtlasAttributeDef.Cardinality.SINGLE, 0, 1, AtlasAttributeDef.Cardinality.SINGLE, 0, 1,
false, false, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), Collections.<AtlasConstraintDef>emptyList()),
}; };
AtlasEntityDef storageDescClsDef = AtlasEntityDef storageDescClsDef =
...@@ -502,32 +503,32 @@ public final class TestUtilsV2 { ...@@ -502,32 +503,32 @@ public final class TestUtilsV2 {
true, true,
AtlasAttributeDef.Cardinality.SINGLE, 0, 1, AtlasAttributeDef.Cardinality.SINGLE, 0, 1,
false, false, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), Collections.<AtlasConstraintDef>emptyList()),
new AtlasAttributeDef("table", TABLE_TYPE, false, new AtlasAttributeDef("table", TABLE_TYPE, false,
AtlasAttributeDef.Cardinality.SINGLE, 1, 1, AtlasAttributeDef.Cardinality.SINGLE, 1, 1,
false, false, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), Collections.<AtlasConstraintDef>emptyList()),
new AtlasAttributeDef("createTime", "long", true, new AtlasAttributeDef("createTime", "long", true,
AtlasAttributeDef.Cardinality.SINGLE, 0, 1, AtlasAttributeDef.Cardinality.SINGLE, 0, 1,
false, false, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), Collections.<AtlasConstraintDef>emptyList()),
new AtlasAttributeDef("lastAccessTime", "long", true, new AtlasAttributeDef("lastAccessTime", "long", true,
AtlasAttributeDef.Cardinality.SINGLE, 0, 1, AtlasAttributeDef.Cardinality.SINGLE, 0, 1,
false, false, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), Collections.<AtlasConstraintDef>emptyList()),
new AtlasAttributeDef("sd", STORAGE_DESC_TYPE, false, new AtlasAttributeDef("sd", STORAGE_DESC_TYPE, false,
AtlasAttributeDef.Cardinality.SINGLE, 1, 1, AtlasAttributeDef.Cardinality.SINGLE, 1, 1,
false, false, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), Collections.<AtlasConstraintDef>emptyList()),
new AtlasAttributeDef("columns", String.format("array<%s>", COLUMN_TYPE), new AtlasAttributeDef("columns", String.format("array<%s>", COLUMN_TYPE),
true, true,
AtlasAttributeDef.Cardinality.SINGLE, 0, 1, AtlasAttributeDef.Cardinality.SINGLE, 0, 1,
false, false, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), Collections.<AtlasConstraintDef>emptyList()),
new AtlasAttributeDef("parameters", String.format("map<%s,%s>", "string", "string"), true, new AtlasAttributeDef("parameters", String.format("map<%s,%s>", "string", "string"), true,
AtlasAttributeDef.Cardinality.SINGLE, 0, 1, AtlasAttributeDef.Cardinality.SINGLE, 0, 1,
false, false, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList())}; Collections.<AtlasConstraintDef>emptyList())};
AtlasEntityDef partClsDef = AtlasEntityDef partClsDef =
new AtlasEntityDef("partition_class_type", "partition_class_type" + _description, "1.0", new AtlasEntityDef("partition_class_type", "partition_class_type" + _description, "1.0",
...@@ -538,7 +539,7 @@ public final class TestUtilsV2 { ...@@ -538,7 +539,7 @@ public final class TestUtilsV2 {
Arrays.asList(new AtlasAttributeDef("outputs", "array<" + TABLE_TYPE + ">", true, Arrays.asList(new AtlasAttributeDef("outputs", "array<" + TABLE_TYPE + ">", true,
AtlasAttributeDef.Cardinality.SINGLE, 0, 1, AtlasAttributeDef.Cardinality.SINGLE, 0, 1,
false, false, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList())), Collections.<AtlasConstraintDef>emptyList())),
ImmutableSet.<String>of()); ImmutableSet.<String>of());
AtlasEntityDef tableTypeDefinition = AtlasEntityDef tableTypeDefinition =
...@@ -551,42 +552,42 @@ public final class TestUtilsV2 { ...@@ -551,42 +552,42 @@ public final class TestUtilsV2 {
new AtlasAttributeDef("tableType", "tableType", false, new AtlasAttributeDef("tableType", "tableType", false,
AtlasAttributeDef.Cardinality.SINGLE, 1, 1, AtlasAttributeDef.Cardinality.SINGLE, 1, 1,
false, false, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), Collections.<AtlasConstraintDef>emptyList()),
// array of strings // array of strings
new AtlasAttributeDef("columnNames", new AtlasAttributeDef("columnNames",
String.format("array<%s>", "string"), true, String.format("array<%s>", "string"), true,
AtlasAttributeDef.Cardinality.SINGLE, 0, 1, AtlasAttributeDef.Cardinality.SINGLE, 0, 1,
false, false, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), Collections.<AtlasConstraintDef>emptyList()),
// array of classes // array of classes
new AtlasAttributeDef("columns", String.format("array<%s>", COLUMN_TYPE), new AtlasAttributeDef("columns", String.format("array<%s>", COLUMN_TYPE),
true, true,
AtlasAttributeDef.Cardinality.SINGLE, 0, 1, AtlasAttributeDef.Cardinality.SINGLE, 0, 1,
false, false, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), Collections.<AtlasConstraintDef>emptyList()),
// array of structs // array of structs
new AtlasAttributeDef("partitions", String.format("array<%s>", "partition_struct_type"), new AtlasAttributeDef("partitions", String.format("array<%s>", "partition_struct_type"),
true, true,
AtlasAttributeDef.Cardinality.SINGLE, 0, 1, AtlasAttributeDef.Cardinality.SINGLE, 0, 1,
false, false, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), Collections.<AtlasConstraintDef>emptyList()),
// map of primitives // map of primitives
new AtlasAttributeDef("parametersMap", String.format("map<%s,%s>", "string", "string"), new AtlasAttributeDef("parametersMap", String.format("map<%s,%s>", "string", "string"),
true, true,
AtlasAttributeDef.Cardinality.SINGLE, 0, 1, AtlasAttributeDef.Cardinality.SINGLE, 0, 1,
false, false, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), Collections.<AtlasConstraintDef>emptyList()),
//map of classes - //map of classes -
new AtlasAttributeDef(COLUMNS_MAP, new AtlasAttributeDef(COLUMNS_MAP,
String.format("map<%s,%s>", "string", COLUMN_TYPE), String.format("map<%s,%s>", "string", COLUMN_TYPE),
true, true,
AtlasAttributeDef.Cardinality.SINGLE, 0, 1, AtlasAttributeDef.Cardinality.SINGLE, 0, 1,
false, false, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), Collections.<AtlasConstraintDef>emptyList()),
// new ArrayList<AtlasStructDef.AtlasConstraintDef>() {{ // new ArrayList<AtlasConstraintDef>() {{
//add(new AtlasStructDef.AtlasConstraintDef( //add(new AtlasConstraintDef(
// AtlasStructDef.AtlasConstraintDef.CONSTRAINT_TYPE_MAPPED_FROM_REF, new HashMap<String, Object>())); // AtlasConstraintDef.CONSTRAINT_TYPE_MAPPED_FROM_REF, new HashMap<String, Object>()));
//}}), //}}),
//map of structs //map of structs
new AtlasAttributeDef("partitionsMap", new AtlasAttributeDef("partitionsMap",
...@@ -594,26 +595,26 @@ public final class TestUtilsV2 { ...@@ -594,26 +595,26 @@ public final class TestUtilsV2 {
true, true,
AtlasAttributeDef.Cardinality.SINGLE, 0, 1, AtlasAttributeDef.Cardinality.SINGLE, 0, 1,
false, false, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), Collections.<AtlasConstraintDef>emptyList()),
// struct reference // struct reference
new AtlasAttributeDef("serde1", "serdeType", true, new AtlasAttributeDef("serde1", "serdeType", true,
AtlasAttributeDef.Cardinality.SINGLE, 0, 1, AtlasAttributeDef.Cardinality.SINGLE, 0, 1,
false, false, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), Collections.<AtlasConstraintDef>emptyList()),
new AtlasAttributeDef("serde2", "serdeType", true, new AtlasAttributeDef("serde2", "serdeType", true,
AtlasAttributeDef.Cardinality.SINGLE, 0, 1, AtlasAttributeDef.Cardinality.SINGLE, 0, 1,
false, false, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), Collections.<AtlasConstraintDef>emptyList()),
// class reference // class reference
new AtlasAttributeDef("database", DATABASE_TYPE, false, new AtlasAttributeDef("database", DATABASE_TYPE, false,
AtlasAttributeDef.Cardinality.SINGLE, 1, 1, AtlasAttributeDef.Cardinality.SINGLE, 1, 1,
false, false, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), Collections.<AtlasConstraintDef>emptyList()),
//class reference as composite //class reference as composite
new AtlasAttributeDef("databaseComposite", DATABASE_TYPE, true, new AtlasAttributeDef("databaseComposite", DATABASE_TYPE, true,
AtlasAttributeDef.Cardinality.SINGLE, 0, 1, AtlasAttributeDef.Cardinality.SINGLE, 0, 1,
false, false, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList())); Collections.<AtlasConstraintDef>emptyList()));
AtlasClassificationDef piiTypeDefinition = AtlasClassificationDef piiTypeDefinition =
AtlasTypeUtil.createTraitTypeDef(PII, PII + _description, ImmutableSet.<String>of()); AtlasTypeUtil.createTraitTypeDef(PII, PII + _description, ImmutableSet.<String>of());
...@@ -693,7 +694,7 @@ public final class TestUtilsV2 { ...@@ -693,7 +694,7 @@ public final class TestUtilsV2 {
AtlasEntityDef developerTypeDef = AtlasTypeUtil.createClassTypeDef("Developer", "Developer_description", ImmutableSet.of("Employee"), AtlasEntityDef developerTypeDef = AtlasTypeUtil.createClassTypeDef("Developer", "Developer_description", ImmutableSet.of("Employee"),
new AtlasAttributeDef("language", String.format("array<%s>", "string"), false, AtlasAttributeDef.Cardinality.SET, new AtlasAttributeDef("language", String.format("array<%s>", "string"), false, AtlasAttributeDef.Cardinality.SET,
1, 10, false, false, 1, 10, false, false,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList())); Collections.<AtlasConstraintDef>emptyList()));
return Arrays.asList(developerTypeDef); return Arrays.asList(developerTypeDef);
} }
......
...@@ -31,7 +31,6 @@ import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef; ...@@ -31,7 +31,6 @@ import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef;
import org.apache.atlas.type.AtlasClassificationType; import org.apache.atlas.type.AtlasClassificationType;
import org.apache.atlas.type.AtlasEntityType; import org.apache.atlas.type.AtlasEntityType;
import org.apache.atlas.type.AtlasStructType; import org.apache.atlas.type.AtlasStructType;
import org.apache.atlas.type.AtlasType;
import org.apache.atlas.type.AtlasTypeRegistry; import org.apache.atlas.type.AtlasTypeRegistry;
import org.apache.atlas.type.AtlasTypeRegistry.AtlasTransientTypeRegistry; import org.apache.atlas.type.AtlasTypeRegistry.AtlasTransientTypeRegistry;
import org.slf4j.Logger; import org.slf4j.Logger;
......
...@@ -31,12 +31,18 @@ import org.apache.atlas.model.typedef.AtlasEntityDef; ...@@ -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.AtlasAttributeDef;
import org.apache.atlas.model.typedef.AtlasStructDef.AtlasConstraintDef; import org.apache.atlas.model.typedef.AtlasStructDef.AtlasConstraintDef;
import org.apache.atlas.type.AtlasTypeRegistry.AtlasTransientTypeRegistry; import org.apache.atlas.type.AtlasTypeRegistry.AtlasTransientTypeRegistry;
import org.apache.atlas.type.AtlasEntityType.ForeignKeyReference;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import static org.testng.Assert.*; import static org.testng.Assert.*;
public class TestAtlasEntityType { 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 AtlasEntityType entityType;
private final List<Object> validValues = new ArrayList<>(); private final List<Object> validValues = new ArrayList<>();
private final List<Object> invalidValues = new ArrayList<>(); private final List<Object> invalidValues = new ArrayList<>();
...@@ -138,13 +144,34 @@ public class TestAtlasEntityType { ...@@ -138,13 +144,34 @@ public class TestAtlasEntityType {
ttr.addTypes(entityDefs); 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; commit = true;
} catch (AtlasBaseException excp) { } catch (AtlasBaseException excp) {
failureMsg = excp.getMessage(); failureMsg = excp.getMessage();
} finally { } finally {
typeRegistry.releaseTypeRegistryForUpdate(ttr, commit); 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 @Test
...@@ -230,12 +257,12 @@ public class TestAtlasEntityType { ...@@ -230,12 +257,12 @@ public class TestAtlasEntityType {
} }
private AtlasEntityDef createTableEntityDef() { private AtlasEntityDef createTableEntityDef() {
AtlasEntityDef table = new AtlasEntityDef("my_table"); AtlasEntityDef table = new AtlasEntityDef(TYPE_TABLE);
AtlasAttributeDef attrColumns = new AtlasAttributeDef("columns", AtlasAttributeDef attrColumns = new AtlasAttributeDef(ATTR_COLUMNS,
AtlasBaseTypeDef.getArrayTypeName("my_column")); AtlasBaseTypeDef.getArrayTypeName(TYPE_COLUMN));
Map<String, Object> params = new HashMap<>(); 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)); attrColumns.addConstraint(new AtlasConstraintDef(AtlasConstraintDef.CONSTRAINT_TYPE_MAPPED_FROM_REF, params));
table.addAttribute(attrColumns); table.addAttribute(attrColumns);
...@@ -244,9 +271,9 @@ public class TestAtlasEntityType { ...@@ -244,9 +271,9 @@ public class TestAtlasEntityType {
} }
private AtlasEntityDef createTableEntityDefWithMissingRefAttribute() { private AtlasEntityDef createTableEntityDefWithMissingRefAttribute() {
AtlasEntityDef table = new AtlasEntityDef("my_table"); AtlasEntityDef table = new AtlasEntityDef(TYPE_TABLE);
AtlasAttributeDef attrColumns = new AtlasAttributeDef("columns", AtlasAttributeDef attrColumns = new AtlasAttributeDef(ATTR_COLUMNS,
AtlasBaseTypeDef.getArrayTypeName("my_column")); AtlasBaseTypeDef.getArrayTypeName(TYPE_COLUMN));
attrColumns.addConstraint(new AtlasConstraintDef(AtlasConstraintDef.CONSTRAINT_TYPE_MAPPED_FROM_REF, null)); attrColumns.addConstraint(new AtlasConstraintDef(AtlasConstraintDef.CONSTRAINT_TYPE_MAPPED_FROM_REF, null));
table.addAttribute(attrColumns); table.addAttribute(attrColumns);
...@@ -255,10 +282,13 @@ public class TestAtlasEntityType { ...@@ -255,10 +282,13 @@ public class TestAtlasEntityType {
} }
private AtlasEntityDef createColumnEntityDef() { private AtlasEntityDef createColumnEntityDef() {
AtlasEntityDef column = new AtlasEntityDef("my_column"); AtlasEntityDef column = new AtlasEntityDef(TYPE_COLUMN);
AtlasAttributeDef attrTable = new AtlasAttributeDef("table", "my_table"); 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); column.addAttribute(attrTable);
return column; return column;
......
...@@ -9,6 +9,8 @@ ATLAS-1060 Add composite indexes for exact match performance improvements for al ...@@ -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) ATLAS-1127 Modify creation and modification timestamps to Date instead of Long(sumasai)
ALL CHANGES: 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-1507 fixed incorrect relationship specified in hive-model
ATLAS-1506 updated AtlasObjectId to support unqiueAttributes to identity the object 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) ATLAS-1378 Use .gitignore so git does not see binary files as changed (david_radley via dkantor)
......
...@@ -20,9 +20,8 @@ package org.apache.atlas.repository.store.graph.v1; ...@@ -20,9 +20,8 @@ package org.apache.atlas.repository.store.graph.v1;
import com.google.common.base.Optional; import com.google.common.base.Optional;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.Singleton; import com.google.inject.Singleton;
import org.apache.atlas.aspect.Monitored;
import org.apache.atlas.exception.AtlasBaseException; 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.graph.GraphHelper;
import org.apache.atlas.repository.graphdb.AtlasEdge; import org.apache.atlas.repository.graphdb.AtlasEdge;
import org.apache.atlas.repository.graphdb.AtlasVertex; import org.apache.atlas.repository.graphdb.AtlasVertex;
...@@ -111,7 +110,7 @@ public class ArrayVertexMapper implements InstanceGraphMapper<List> { ...@@ -111,7 +110,7 @@ public class ArrayVertexMapper implements InstanceGraphMapper<List> {
//Removes unused edges from the old collection, compared to the new collection //Removes unused edges from the old collection, compared to the new collection
private List<AtlasEdge> removeUnusedArrayEntries( private List<AtlasEdge> removeUnusedArrayEntries(
AtlasStructType entityType, AtlasStructType entityType,
AtlasStructDef.AtlasAttributeDef attributeDef, AtlasAttributeDef attributeDef,
List<AtlasEdge> currentEntries, List<AtlasEdge> currentEntries,
List<AtlasEdge> newEntries, List<AtlasEdge> newEntries,
AtlasType entryType) throws AtlasBaseException { AtlasType entryType) throws AtlasBaseException {
......
...@@ -25,7 +25,7 @@ import org.apache.atlas.model.TypeCategory; ...@@ -25,7 +25,7 @@ import org.apache.atlas.model.TypeCategory;
import org.apache.atlas.model.instance.AtlasEntity; import org.apache.atlas.model.instance.AtlasEntity;
import org.apache.atlas.model.instance.AtlasObjectId; import org.apache.atlas.model.instance.AtlasObjectId;
import org.apache.atlas.model.instance.AtlasStruct; 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.EntityGraphDiscoveryContext;
import org.apache.atlas.repository.store.graph.EntityGraphDiscovery; import org.apache.atlas.repository.store.graph.EntityGraphDiscovery;
import org.apache.atlas.repository.store.graph.EntityResolver; import org.apache.atlas.repository.store.graph.EntityResolver;
...@@ -146,7 +146,7 @@ public class AtlasEntityGraphDiscoveryV1 implements EntityGraphDiscovery { ...@@ -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 (val != null) {
if ( isPrimitive(attrType.getTypeCategory()) ) { if ( isPrimitive(attrType.getTypeCategory()) ) {
return; return;
...@@ -166,7 +166,8 @@ public class AtlasEntityGraphDiscoveryV1 implements EntityGraphDiscovery { ...@@ -166,7 +166,8 @@ public class AtlasEntityGraphDiscoveryV1 implements EntityGraphDiscovery {
visitReference((AtlasEntityType) attrType, val, false); visitReference((AtlasEntityType) attrType, val, false);
} else if ( val instanceof AtlasEntity ) { } else if ( val instanceof AtlasEntity ) {
//TODO - Change this to foreign key checks after changes in the model //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); visitReference((AtlasEntityType) attrType, val, true);
} else { } else {
visitReference((AtlasEntityType) attrType, val, false); visitReference((AtlasEntityType) attrType, val, false);
...@@ -176,7 +177,7 @@ public class AtlasEntityGraphDiscoveryV1 implements EntityGraphDiscovery { ...@@ -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())) { if (isPrimitive(keyType.getTypeCategory()) && isPrimitive(valueType.getTypeCategory())) {
return; return;
} }
...@@ -194,7 +195,7 @@ public class AtlasEntityGraphDiscoveryV1 implements EntityGraphDiscovery { ...@@ -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())) { if (isPrimitive(elemType.getTypeCategory())) {
return; return;
...@@ -228,7 +229,7 @@ public class AtlasEntityGraphDiscoveryV1 implements EntityGraphDiscovery { ...@@ -228,7 +229,7 @@ public class AtlasEntityGraphDiscoveryV1 implements EntityGraphDiscovery {
for (AtlasStructType.AtlasAttribute attribute : structType.getAllAttributes().values()) { for (AtlasStructType.AtlasAttribute attribute : structType.getAllAttributes().values()) {
AtlasType attrType = attribute.getAttributeType(); 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); visitAttribute(structType, attrType, attribute.getAttributeDef(), attrVal);
} }
} }
......
...@@ -30,6 +30,7 @@ import org.apache.atlas.repository.graphdb.AtlasVertex; ...@@ -30,6 +30,7 @@ import org.apache.atlas.repository.graphdb.AtlasVertex;
import org.apache.atlas.repository.store.graph.AtlasStructDefStore; import org.apache.atlas.repository.store.graph.AtlasStructDefStore;
import org.apache.atlas.repository.util.FilterUtil; import org.apache.atlas.repository.util.FilterUtil;
import org.apache.atlas.type.AtlasArrayType; import org.apache.atlas.type.AtlasArrayType;
import org.apache.atlas.type.AtlasEntityType;
import org.apache.atlas.type.AtlasStructType; import org.apache.atlas.type.AtlasStructType;
import org.apache.atlas.type.AtlasType; import org.apache.atlas.type.AtlasType;
import org.apache.atlas.type.AtlasTypeRegistry; import org.apache.atlas.type.AtlasTypeRegistry;
...@@ -496,11 +497,18 @@ public class AtlasStructDefStoreV1 extends AtlasAbstractDefStoreV1 implements At ...@@ -496,11 +497,18 @@ public class AtlasStructDefStoreV1 extends AtlasAbstractDefStoreV1 implements At
} }
private static String toJsonFromAttributeDef(AtlasAttributeDef attributeDef, AtlasStructType structType) { private static String toJsonFromAttributeDef(AtlasAttributeDef attributeDef, AtlasStructType structType) {
boolean isForeignKey = structType.isForeignKeyAttribute(attributeDef.getName()); boolean isComposite = false;
boolean isMappedFromRef = structType.isMappedFromRefAttribute(attributeDef.getName());
String reverseAttribName = null; 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()); AtlasType attribType = structType.getAttributeType(attributeDef.getName());
if (attribType.getTypeCategory() == org.apache.atlas.model.TypeCategory.ARRAY) { if (attribType.getTypeCategory() == org.apache.atlas.model.TypeCategory.ARRAY) {
...@@ -508,13 +516,13 @@ public class AtlasStructDefStoreV1 extends AtlasAbstractDefStoreV1 implements At ...@@ -508,13 +516,13 @@ public class AtlasStructDefStoreV1 extends AtlasAbstractDefStoreV1 implements At
} }
if (attribType.getTypeCategory() == org.apache.atlas.model.TypeCategory.ENTITY) { if (attribType.getTypeCategory() == org.apache.atlas.model.TypeCategory.ENTITY) {
reverseAttribName = ((AtlasStructType)attribType).getMappedFromRefAttribute(structType.getTypeName(), AtlasEntityType attribEntityType = (AtlasEntityType)attribType;
attributeDef.getName());
reverseAttribName = attribEntityType.getMappedFromRefAttribute(structType.getTypeName(),
attributeDef.getName());
} }
} }
boolean isComposite = isMappedFromRef || (isForeignKey && StringUtils.isBlank(reverseAttribName));
Map<String, Object> attribInfo = new HashMap<>(); Map<String, Object> attribInfo = new HashMap<>();
attribInfo.put("name", attributeDef.getName()); attribInfo.put("name", attributeDef.getName());
......
...@@ -24,7 +24,7 @@ import org.apache.atlas.RequestContextV1; ...@@ -24,7 +24,7 @@ import org.apache.atlas.RequestContextV1;
import org.apache.atlas.exception.AtlasBaseException; import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.model.TypeCategory; import org.apache.atlas.model.TypeCategory;
import org.apache.atlas.model.instance.AtlasEntity; 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.Constants;
import org.apache.atlas.repository.graph.AtlasEdgeLabel; import org.apache.atlas.repository.graph.AtlasEdgeLabel;
import org.apache.atlas.repository.graph.GraphHelper; import org.apache.atlas.repository.graph.GraphHelper;
...@@ -137,11 +137,11 @@ public abstract class DeleteHandlerV1 { ...@@ -137,11 +137,11 @@ public abstract class DeleteHandlerV1 {
} }
for (AtlasStructType.AtlasAttribute attributeInfo : entityType.getAllAttributes().values()) { for (AtlasStructType.AtlasAttribute attributeInfo : entityType.getAllAttributes().values()) {
if (!entityType.isMappedFromRefAttribute(attributeInfo.getAttributeDef().getName())) { if (!entityType.isMappedFromRefAttribute(attributeInfo.getName())) {
continue; continue;
} }
String edgeLabel = AtlasGraphUtilsV1.getAttributeEdgeLabel(entityType, attributeInfo.getAttributeDef().getName()); String edgeLabel = AtlasGraphUtilsV1.getAttributeEdgeLabel(entityType, attributeInfo.getName());
AtlasType attrType = typeRegistry.getType(attributeInfo.getAttributeDef().getTypeName()); AtlasType attrType = typeRegistry.getType(attributeInfo.getTypeName());
switch (attrType.getTypeCategory()) { switch (attrType.getTypeCategory()) {
case ENTITY: case ENTITY:
AtlasEdge edge = graphHelper.getEdgeForLabel(vertex, edgeLabel); AtlasEdge edge = graphHelper.getEdgeForLabel(vertex, edgeLabel);
...@@ -172,7 +172,7 @@ public abstract class DeleteHandlerV1 { ...@@ -172,7 +172,7 @@ public abstract class DeleteHandlerV1 {
if (valueTypeCategory != TypeCategory.ENTITY) { if (valueTypeCategory != TypeCategory.ENTITY) {
continue; continue;
} }
String propertyName = AtlasGraphUtilsV1.getQualifiedAttributePropertyKey(entityType, attributeInfo.getAttributeDef().getName()); String propertyName = AtlasGraphUtilsV1.getQualifiedAttributePropertyKey(entityType, attributeInfo.getName());
List<String> keys = vertex.getProperty(propertyName, List.class); List<String> keys = vertex.getProperty(propertyName, List.class);
if (keys != null) { if (keys != null) {
for (String key : keys) { for (String key : keys) {
...@@ -277,19 +277,21 @@ public abstract class DeleteHandlerV1 { ...@@ -277,19 +277,21 @@ public abstract class DeleteHandlerV1 {
AtlasType parentType = typeRegistry.getType(typeName); AtlasType parentType = typeRegistry.getType(typeName);
if (parentType instanceof AtlasStructType) { if (parentType instanceof AtlasStructType) {
AtlasStructType structType = (AtlasStructType) parentType;
boolean isEntityType = (parentType instanceof AtlasEntityType);
AtlasStructType entityType = (AtlasStructType) parentType; for (AtlasStructType.AtlasAttribute attributeInfo : getAttributes(structType)) {
for (AtlasStructType.AtlasAttribute attributeInfo : getAttributes(entityType)) { LOG.debug("Deleting attribute {} for {}", attributeInfo.getName(), string(instanceVertex));
LOG.debug("Deleting attribute {} for {}", attributeInfo.getAttributeDef().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()) { switch (attrType.getTypeCategory()) {
case ENTITY: case ENTITY:
//If its class attribute, delete the reference //If its class attribute, delete the reference
deleteEdgeReference(instanceVertex, edgeLabel, TypeCategory.ENTITY, entityType.isMappedFromRefAttribute(attributeInfo.getAttributeDef().getName())); deleteEdgeReference(instanceVertex, edgeLabel, TypeCategory.ENTITY, isComposite);
break; break;
case STRUCT: case STRUCT:
...@@ -306,7 +308,7 @@ public abstract class DeleteHandlerV1 { ...@@ -306,7 +308,7 @@ public abstract class DeleteHandlerV1 {
if (edges != null) { if (edges != null) {
while (edges.hasNext()) { while (edges.hasNext()) {
AtlasEdge edge = edges.next(); 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 { ...@@ -317,14 +319,14 @@ public abstract class DeleteHandlerV1 {
AtlasMapType mapType = (AtlasMapType) attrType; AtlasMapType mapType = (AtlasMapType) attrType;
AtlasType keyType = mapType.getKeyType(); AtlasType keyType = mapType.getKeyType();
TypeCategory valueTypeCategory = mapType.getValueType().getTypeCategory(); TypeCategory valueTypeCategory = mapType.getValueType().getTypeCategory();
String propertyName = AtlasGraphUtilsV1.getQualifiedAttributePropertyKey(entityType, attributeInfo.getAttributeDef().getName()); String propertyName = AtlasGraphUtilsV1.getQualifiedAttributePropertyKey(structType, attributeInfo.getName());
if (AtlasGraphUtilsV1.isReference(valueTypeCategory)) { if (AtlasGraphUtilsV1.isReference(valueTypeCategory)) {
List<Object> keys = ArrayVertexMapper.getArrayElementsProperty(keyType, instanceVertex, propertyName); List<Object> keys = ArrayVertexMapper.getArrayElementsProperty(keyType, instanceVertex, propertyName);
if (keys != null) { if (keys != null) {
for (Object key : keys) { for (Object key : keys) {
String mapEdgeLabel = GraphHelper.getQualifiedNameForMapKey(edgeLabel, (String) key); 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 { ...@@ -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); AtlasEdgeLabel atlasEdgeLabel = new AtlasEdgeLabel(edgeLabel);
AtlasType parentType = typeRegistry.getType(atlasEdgeLabel.getTypeName()); AtlasType parentType = typeRegistry.getType(atlasEdgeLabel.getTypeName());
...@@ -395,7 +397,7 @@ public abstract class DeleteHandlerV1 { ...@@ -395,7 +397,7 @@ public abstract class DeleteHandlerV1 {
String edgeLabel = EDGE_LABEL_PREFIX + propertyName; String edgeLabel = EDGE_LABEL_PREFIX + propertyName;
AtlasEdge edge = null; AtlasEdge edge = null;
AtlasStructDef.AtlasAttributeDef attrDef = parentType.getAttributeDef(attributeName); AtlasAttributeDef attrDef = parentType.getAttributeDef(attributeName);
AtlasType attrType = typeRegistry.getType(attrDef.getTypeName()); AtlasType attrType = typeRegistry.getType(attrDef.getTypeName());
switch (attrType.getTypeCategory()) { switch (attrType.getTypeCategory()) {
...@@ -518,7 +520,7 @@ public abstract class DeleteHandlerV1 { ...@@ -518,7 +520,7 @@ public abstract class DeleteHandlerV1 {
AtlasEntity.Status edgeState = AtlasGraphUtilsV1.getState(edge); AtlasEntity.Status edgeState = AtlasGraphUtilsV1.getState(edge);
if (edgeState == AtlasEntity.Status.ACTIVE) { if (edgeState == AtlasEntity.Status.ACTIVE) {
//Delete only the active edge references //Delete only the active edge references
AtlasStructDef.AtlasAttributeDef attribute = getAttributeForEdge(edge.getLabel()); AtlasAttributeDef attribute = getAttributeForEdge(edge.getLabel());
//TODO use delete edge instead?? //TODO use delete edge instead??
deleteEdgeBetweenVertices(edge.getOutVertex(), edge.getInVertex(), attribute.getName()); deleteEdgeBetweenVertices(edge.getOutVertex(), edge.getInVertex(), attribute.getName());
} }
......
...@@ -20,7 +20,6 @@ package org.apache.atlas.repository.store.graph.v1; ...@@ -20,7 +20,6 @@ package org.apache.atlas.repository.store.graph.v1;
import com.google.inject.Inject; import com.google.inject.Inject;
import org.apache.atlas.AtlasErrorCode; import org.apache.atlas.AtlasErrorCode;
import org.apache.atlas.RequestContextV1;
import org.apache.atlas.exception.AtlasBaseException; import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.model.TypeCategory; import org.apache.atlas.model.TypeCategory;
import org.apache.atlas.model.instance.AtlasEntity; import org.apache.atlas.model.instance.AtlasEntity;
...@@ -29,7 +28,7 @@ import org.apache.atlas.model.instance.AtlasObjectId; ...@@ -29,7 +28,7 @@ import org.apache.atlas.model.instance.AtlasObjectId;
import org.apache.atlas.model.instance.AtlasStruct; import org.apache.atlas.model.instance.AtlasStruct;
import org.apache.atlas.model.instance.EntityMutationResponse; import org.apache.atlas.model.instance.EntityMutationResponse;
import org.apache.atlas.model.instance.EntityMutations; 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.Constants;
import org.apache.atlas.repository.RepositoryException; import org.apache.atlas.repository.RepositoryException;
import org.apache.atlas.repository.graph.GraphHelper; import org.apache.atlas.repository.graph.GraphHelper;
...@@ -109,7 +108,7 @@ public class EntityGraphMapper implements InstanceGraphMapper<AtlasEdge> { ...@@ -109,7 +108,7 @@ public class EntityGraphMapper implements InstanceGraphMapper<AtlasEdge> {
public void cleanUp() throws AtlasBaseException { 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()); LOG.debug("Updating entity reference {} for reference attribute {}", attributeDef.getName());
// Update edge if it exists // Update edge if it exists
...@@ -180,7 +179,7 @@ public class EntityGraphMapper implements InstanceGraphMapper<AtlasEdge> { ...@@ -180,7 +179,7 @@ public class EntityGraphMapper implements InstanceGraphMapper<AtlasEdge> {
final Map<String, AtlasStructType.AtlasAttribute> allAttributes = type.getAllAttributes(); final Map<String, AtlasStructType.AtlasAttribute> allAttributes = type.getAllAttributes();
for (String attribute : allAttributes.keySet()) { for (String attribute : allAttributes.keySet()) {
AtlasType attributeType = allAttributes.get(attribute).getAttributeType(); 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 ( header.getAttribute(attribute) == null && (TypeCategory.PRIMITIVE == attributeType.getTypeCategory())) {
if ( attributeDef.getIsOptional()) { if ( attributeDef.getIsOptional()) {
......
...@@ -21,6 +21,7 @@ package org.apache.atlas.repository.store.graph.v1; ...@@ -21,6 +21,7 @@ package org.apache.atlas.repository.store.graph.v1;
import com.google.common.base.Optional; import com.google.common.base.Optional;
import org.apache.atlas.model.instance.EntityMutations; import org.apache.atlas.model.instance.EntityMutations;
import org.apache.atlas.model.typedef.AtlasStructDef; 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.AtlasEdge;
import org.apache.atlas.repository.graphdb.AtlasVertex; import org.apache.atlas.repository.graphdb.AtlasVertex;
import org.apache.atlas.type.AtlasStructType; import org.apache.atlas.type.AtlasStructType;
...@@ -166,7 +167,7 @@ public class GraphMutationContext { ...@@ -166,7 +167,7 @@ public class GraphMutationContext {
return attribute.getStructDef(); return attribute.getStructDef();
} }
public AtlasStructDef.AtlasAttributeDef getAttributeDef() { public AtlasAttributeDef getAttributeDef() {
return attribute.getAttributeDef(); return attribute.getAttributeDef();
} }
......
...@@ -20,16 +20,13 @@ package org.apache.atlas.repository.store.graph.v1; ...@@ -20,16 +20,13 @@ package org.apache.atlas.repository.store.graph.v1;
import com.google.common.base.Optional; import com.google.common.base.Optional;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.apache.atlas.AtlasErrorCode; import org.apache.atlas.AtlasErrorCode;
import org.apache.atlas.AtlasException; import org.apache.atlas.AtlasException;
import org.apache.atlas.exception.AtlasBaseException; import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.model.TypeCategory; import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef;
import org.apache.atlas.model.typedef.AtlasStructDef;
import org.apache.atlas.repository.graph.GraphHelper; import org.apache.atlas.repository.graph.GraphHelper;
import org.apache.atlas.repository.graphdb.AtlasEdge; import org.apache.atlas.repository.graphdb.AtlasEdge;
import org.apache.atlas.repository.graphdb.AtlasVertex; import org.apache.atlas.repository.graphdb.AtlasVertex;
import org.apache.atlas.type.AtlasEntityType;
import org.apache.atlas.type.AtlasMapType; import org.apache.atlas.type.AtlasMapType;
import org.apache.atlas.type.AtlasStructType; import org.apache.atlas.type.AtlasStructType;
import org.apache.atlas.type.AtlasType; import org.apache.atlas.type.AtlasType;
...@@ -37,7 +34,6 @@ import org.apache.commons.collections.MapUtils; ...@@ -37,7 +34,6 @@ import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import javax.inject.Provider;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
...@@ -158,7 +154,7 @@ public class MapVertexMapper implements InstanceGraphMapper<Map> { ...@@ -158,7 +154,7 @@ public class MapVertexMapper implements InstanceGraphMapper<Map> {
//Remove unused entries from map //Remove unused entries from map
private Map<String, Object> removeUnusedMapEntries( private Map<String, Object> removeUnusedMapEntries(
AtlasStructType entityType, AtlasStructType entityType,
AtlasMapType mapType, AtlasStructDef.AtlasAttributeDef attributeDef, AtlasMapType mapType, AtlasAttributeDef attributeDef,
AtlasVertex instanceVertex, String propertyName, AtlasVertex instanceVertex, String propertyName,
Map<String, Object> currentMap, Map<String, Object> currentMap,
Map<String, Object> newMap) Map<String, Object> newMap)
......
...@@ -25,10 +25,9 @@ import org.apache.atlas.model.TypeCategory; ...@@ -25,10 +25,9 @@ import org.apache.atlas.model.TypeCategory;
import org.apache.atlas.model.instance.AtlasEntity; import org.apache.atlas.model.instance.AtlasEntity;
import org.apache.atlas.model.instance.AtlasStruct; import org.apache.atlas.model.instance.AtlasStruct;
import org.apache.atlas.model.instance.EntityMutations; 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.Constants;
import org.apache.atlas.repository.RepositoryException; import org.apache.atlas.repository.RepositoryException;
import org.apache.atlas.repository.graph.AtlasEdgeLabel;
import org.apache.atlas.repository.graph.AtlasGraphProvider; import org.apache.atlas.repository.graph.AtlasGraphProvider;
import org.apache.atlas.repository.graph.GraphHelper; import org.apache.atlas.repository.graph.GraphHelper;
import org.apache.atlas.repository.graphdb.AtlasEdge; import org.apache.atlas.repository.graphdb.AtlasEdge;
...@@ -86,7 +85,7 @@ public class StructVertexMapper implements InstanceGraphMapper<AtlasEdge> { ...@@ -86,7 +85,7 @@ public class StructVertexMapper implements InstanceGraphMapper<AtlasEdge> {
} }
public static boolean shouldManageChildReferences(AtlasStructType type, String attributeName) { public static boolean shouldManageChildReferences(AtlasStructType type, String attributeName) {
return type.isMappedFromRefAttribute(attributeName); return (type instanceof AtlasEntityType) && ((AtlasEntityType)type).isMappedFromRefAttribute(attributeName);
} }
/** /**
...@@ -186,7 +185,7 @@ public class StructVertexMapper implements InstanceGraphMapper<AtlasEdge> { ...@@ -186,7 +185,7 @@ public class StructVertexMapper implements InstanceGraphMapper<AtlasEdge> {
return ctx.getValue(); return ctx.getValue();
} }
private AtlasEdge createVertex(AtlasStructType parentType, AtlasStructType attrType, AtlasStructDef.AtlasAttributeDef attributeDef, AtlasStruct struct, AtlasVertex referringVertex, String edgeLabel) throws AtlasBaseException { private AtlasEdge createVertex(AtlasStructType parentType, AtlasStructType attrType, AtlasAttributeDef attributeDef, AtlasStruct struct, AtlasVertex referringVertex, String edgeLabel) throws AtlasBaseException {
AtlasVertex vertex = createVertexTemplate(struct, attrType); AtlasVertex vertex = createVertexTemplate(struct, attrType);
mapAttributestoVertex(EntityMutations.EntityOperation.CREATE, attrType, struct, vertex); mapAttributestoVertex(EntityMutations.EntityOperation.CREATE, attrType, struct, vertex);
...@@ -198,7 +197,7 @@ public class StructVertexMapper implements InstanceGraphMapper<AtlasEdge> { ...@@ -198,7 +197,7 @@ public class StructVertexMapper implements InstanceGraphMapper<AtlasEdge> {
} }
} }
private void updateVertex(AtlasStructType parentType, AtlasStructType structAttributeType, AtlasStructDef.AtlasAttributeDef attributeDef, AtlasStruct value, AtlasVertex structVertex) throws AtlasBaseException { private void updateVertex(AtlasStructType parentType, AtlasStructType structAttributeType, AtlasAttributeDef attributeDef, AtlasStruct value, AtlasVertex structVertex) throws AtlasBaseException {
mapAttributestoVertex(EntityMutations.EntityOperation.CREATE, structAttributeType, value, structVertex); mapAttributestoVertex(EntityMutations.EntityOperation.CREATE, structAttributeType, value, structVertex);
} }
......
...@@ -107,7 +107,7 @@ public class UniqAttrBasedEntityResolver implements EntityResolver { ...@@ -107,7 +107,7 @@ public class UniqAttrBasedEntityResolver implements EntityResolver {
for (AtlasStructType.AtlasAttribute attr : entityType.getAllAttributes().values()) { for (AtlasStructType.AtlasAttribute attr : entityType.getAllAttributes().values()) {
if (attr.getAttributeDef().getIsUnique()) { if (attr.getAttributeDef().getIsUnique()) {
Object attrVal = entity.getAttribute(attr.getAttributeDef().getName()); Object attrVal = entity.getAttribute(attr.getName());
if (attrVal != null) { if (attrVal != null) {
String qualifiedAttrName = attr.getQualifiedAttributeName(); String qualifiedAttrName = attr.getQualifiedAttributeName();
AtlasVertex vertex = null; AtlasVertex vertex = null;
......
...@@ -479,7 +479,7 @@ public final class RestUtils { ...@@ -479,7 +479,7 @@ public final class RestUtils {
// 2. [ foreignKey(onDelete=cascade) -> reverseAttribute ] // 2. [ foreignKey(onDelete=cascade) -> reverseAttribute ]
AtlasStructType structType = (AtlasStructType) registry.getType(structDef.getName()); AtlasStructType structType = (AtlasStructType) registry.getType(structDef.getName());
boolean isForeignKey = structType.isForeignKeyAttribute(attrDef.getName()); boolean isForeignKey = structType.isForeignKeyAttribute(attrDef.getName());
boolean isMappedFromRef = structType.isMappedFromRefAttribute(attrDef.getName()); boolean isMappedFromRef = (structType instanceof AtlasEntityType) && ((AtlasEntityType)structType).isMappedFromRefAttribute(attrDef.getName());
AtlasType attrType = structType.getAttributeType(attrDef.getName()); AtlasType attrType = structType.getAttributeType(attrDef.getName());
if (attrType != null && isForeignKey) { if (attrType != null && isForeignKey) {
...@@ -488,7 +488,7 @@ public final class RestUtils { ...@@ -488,7 +488,7 @@ public final class RestUtils {
} }
if (attrType.getTypeCategory() == TypeCategory.ENTITY) { if (attrType.getTypeCategory() == TypeCategory.ENTITY) {
reverseAttribName = ((AtlasStructType) attrType). reverseAttribName = ((AtlasEntityType) attrType).
getMappedFromRefAttribute(structType.getTypeName(), attrDef.getName()); getMappedFromRefAttribute(structType.getTypeName(), attrDef.getName());
} }
} }
......
...@@ -124,20 +124,20 @@ public class AtlasStructFormatConverter extends AtlasAbstractFormatConverter { ...@@ -124,20 +124,20 @@ public class AtlasStructFormatConverter extends AtlasAbstractFormatConverter {
if (MapUtils.isNotEmpty(attributes)) { if (MapUtils.isNotEmpty(attributes)) {
ret = new HashMap<>(); ret = new HashMap<>();
for (AtlasStructType.AtlasAttribute attr : getAttributes(structType)) { for (AtlasStructType.AtlasAttribute attr : structType.getAllAttributes().values()) {
AtlasType attrType = structType.getAttributeType(attr.getAttributeDef().getName()); AtlasType attrType = attr.getAttributeType();
if (attrType == null) { if (attrType == null) {
LOG.warn("ignored attribute {}.{}: failed to find AtlasType", structType.getTypeName(), attr.getAttributeDef().getName()); LOG.warn("ignored attribute {}.{}: failed to find AtlasType", structType.getTypeName(), attr.getName());
continue; continue;
} }
AtlasFormatConverter attrConverter = converterRegistry.getConverter(attrType.getTypeCategory()); AtlasFormatConverter attrConverter = converterRegistry.getConverter(attrType.getTypeCategory());
Object v2Value = attributes.get(attr.getAttributeDef().getName()); Object v2Value = attributes.get(attr.getName());
Object v1Value = attrConverter.fromV2ToV1(v2Value, attrType); Object v1Value = attrConverter.fromV2ToV1(v2Value, attrType);
ret.put(attr.getAttributeDef().getName(), v1Value); ret.put(attr.getName(), v1Value);
} }
} }
...@@ -150,11 +150,11 @@ public class AtlasStructFormatConverter extends AtlasAbstractFormatConverter { ...@@ -150,11 +150,11 @@ public class AtlasStructFormatConverter extends AtlasAbstractFormatConverter {
if (MapUtils.isNotEmpty(attributes)) { if (MapUtils.isNotEmpty(attributes)) {
ret = new HashMap<>(); ret = new HashMap<>();
for (AtlasStructType.AtlasAttribute attr : getAttributes(structType)) { for (AtlasStructType.AtlasAttribute attr : structType.getAllAttributes().values()) {
AtlasType attrType = structType.getAttributeType(attr.getAttributeDef().getName()); AtlasType attrType = attr.getAttributeType();
AtlasFormatConverter attrConverter = converterRegistry.getConverter(attrType.getTypeCategory()); AtlasFormatConverter attrConverter = converterRegistry.getConverter(attrType.getTypeCategory());
Object v1Value = attributes.get(attr.getAttributeDef().getName()); Object v1Value = attributes.get(attr.getName());
Object v2Value = attrConverter.fromV1ToV2(v1Value, attrType); Object v2Value = attrConverter.fromV1ToV2(v1Value, attrType);
ret.put(attr.getAttributeDef().getName(), v2Value); ret.put(attr.getAttributeDef().getName(), v2Value);
......
...@@ -26,7 +26,7 @@ import org.apache.atlas.model.instance.AtlasClassification; ...@@ -26,7 +26,7 @@ import org.apache.atlas.model.instance.AtlasClassification;
import org.apache.atlas.model.instance.AtlasEntity; import org.apache.atlas.model.instance.AtlasEntity;
import org.apache.atlas.model.instance.AtlasEntityWithAssociations; import org.apache.atlas.model.instance.AtlasEntityWithAssociations;
import org.apache.atlas.model.instance.EntityMutationResponse; import org.apache.atlas.model.instance.EntityMutationResponse;
import org.apache.atlas.model.typedef.AtlasStructDef; import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef;
import org.apache.atlas.services.MetadataService; import org.apache.atlas.services.MetadataService;
import org.apache.atlas.type.AtlasEntityType; import org.apache.atlas.type.AtlasEntityType;
import org.apache.atlas.type.AtlasType; import org.apache.atlas.type.AtlasType;
...@@ -390,8 +390,9 @@ public class EntityREST { ...@@ -390,8 +390,9 @@ public class EntityREST {
* @param attributeName the name of the attribute * @param attributeName the name of the attribute
*/ */
private void validateUniqueAttribute(AtlasEntityType entityType, String attributeName) throws AtlasBaseException { private void validateUniqueAttribute(AtlasEntityType entityType, String attributeName) throws AtlasBaseException {
AtlasStructDef.AtlasAttributeDef attribute = entityType.getAttributeDef(attributeName); AtlasAttributeDef attribute = entityType.getAttributeDef(attributeName);
if (attribute != null && !attribute.getIsUnique()) {
if (attribute == null || !attribute.getIsUnique()) {
throw new AtlasBaseException(AtlasErrorCode.ATTRIBUTE_UNIQUE_INVALID, entityType.getTypeName(), attributeName); throw new AtlasBaseException(AtlasErrorCode.ATTRIBUTE_UNIQUE_INVALID, entityType.getTypeName(), attributeName);
} }
} }
......
...@@ -30,6 +30,7 @@ import org.apache.atlas.model.typedef.AtlasEntityDef; ...@@ -30,6 +30,7 @@ import org.apache.atlas.model.typedef.AtlasEntityDef;
import org.apache.atlas.model.typedef.AtlasEnumDef; import org.apache.atlas.model.typedef.AtlasEnumDef;
import org.apache.atlas.model.typedef.AtlasStructDef; import org.apache.atlas.model.typedef.AtlasStructDef;
import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef; 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.model.typedef.AtlasTypesDef;
import org.apache.atlas.type.AtlasTypeUtil; import org.apache.atlas.type.AtlasTypeUtil;
import org.apache.atlas.typesystem.types.DataTypes; import org.apache.atlas.typesystem.types.DataTypes;
...@@ -243,7 +244,7 @@ public class TypedefsJerseyResourceIT extends BaseResourceIT { ...@@ -243,7 +244,7 @@ public class TypedefsJerseyResourceIT extends BaseResourceIT {
@Test @Test
public void testListTypesByFilter() throws Exception { public void testListTypesByFilter() throws Exception {
AtlasStructDef.AtlasAttributeDef attr = AtlasTypeUtil.createOptionalAttrDef("attr", "string"); AtlasAttributeDef attr = AtlasTypeUtil.createOptionalAttrDef("attr", "string");
AtlasEntityDef classDefA = AtlasTypeUtil.createClassTypeDef("A" + randomString(), ImmutableSet.<String>of(), attr); AtlasEntityDef classDefA = AtlasTypeUtil.createClassTypeDef("A" + randomString(), ImmutableSet.<String>of(), attr);
AtlasEntityDef classDefA1 = AtlasTypeUtil.createClassTypeDef("A1" + randomString(), ImmutableSet.of(classDefA.getName()), attr); AtlasEntityDef classDefA1 = AtlasTypeUtil.createClassTypeDef("A1" + randomString(), ImmutableSet.of(classDefA.getName()), attr);
AtlasEntityDef classDefB = AtlasTypeUtil.createClassTypeDef("B" + randomString(), ImmutableSet.<String>of(), attr); AtlasEntityDef classDefB = AtlasTypeUtil.createClassTypeDef("B" + randomString(), ImmutableSet.<String>of(), attr);
...@@ -296,7 +297,7 @@ public class TypedefsJerseyResourceIT extends BaseResourceIT { ...@@ -296,7 +297,7 @@ public class TypedefsJerseyResourceIT extends BaseResourceIT {
false, false,
Cardinality.SINGLE, 1, 1, Cardinality.SINGLE, 1, 1,
true, true, true, true,
Collections.<AtlasStructDef.AtlasConstraintDef>emptyList())); Collections.<AtlasConstraintDef>emptyList()));
atlasTypesDef.getEntityDefs().add(tableTypeDefinition); atlasTypesDef.getEntityDefs().add(tableTypeDefinition);
AtlasClassificationDef fetlTypeDefinition = AtlasTypeUtil AtlasClassificationDef fetlTypeDefinition = AtlasTypeUtil
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment