http://git-wip-us.apache.org/repos/asf/atlas/blob/0877e47c/repository/src/main/java/org/apache/atlas/repository/converters/AtlasStructFormatConverter.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/converters/AtlasStructFormatConverter.java b/repository/src/main/java/org/apache/atlas/repository/converters/AtlasStructFormatConverter.java index 6b6ee01..b03eda3 100644 --- a/repository/src/main/java/org/apache/atlas/repository/converters/AtlasStructFormatConverter.java +++ b/repository/src/main/java/org/apache/atlas/repository/converters/AtlasStructFormatConverter.java @@ -18,21 +18,24 @@ package org.apache.atlas.repository.converters; import org.apache.atlas.AtlasErrorCode; -import org.apache.atlas.AtlasException; import org.apache.atlas.exception.AtlasBaseException; import org.apache.atlas.model.TypeCategory; +import org.apache.atlas.model.instance.AtlasEntity; +import org.apache.atlas.model.instance.AtlasObjectId; import org.apache.atlas.model.instance.AtlasStruct; -import org.apache.atlas.type.AtlasStructType; +import org.apache.atlas.model.v1.instance.Struct; +import org.apache.atlas.type.*; +import org.apache.atlas.type.AtlasBuiltInTypes.AtlasObjectIdType; import org.apache.atlas.type.AtlasStructType.AtlasAttribute; -import org.apache.atlas.type.AtlasType; -import org.apache.atlas.type.AtlasTypeRegistry; -import org.apache.atlas.typesystem.IStruct; -import org.apache.atlas.typesystem.Struct; import org.apache.commons.collections.MapUtils; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; +import java.util.List; import java.util.Map; public class AtlasStructFormatConverter extends AtlasAbstractFormatConverter { @@ -64,19 +67,12 @@ public class AtlasStructFormatConverter extends AtlasAbstractFormatConverter { } else { ret = new AtlasStruct(type.getTypeName()); } - } else if (v1Obj instanceof IStruct) { - IStruct struct = (IStruct) v1Obj; - Map<String, Object> v1Attribs = null; - - try { - v1Attribs = struct.getValuesMap(); - } catch (AtlasException excp) { - LOG.error("IStruct.getValuesMap() failed", excp); - } + } else if (v1Obj instanceof Struct) { + Struct struct = (Struct) v1Obj; - ret = new AtlasStruct(type.getTypeName(), fromV1ToV2(structType, v1Attribs, converterContext)); + ret = new AtlasStruct(type.getTypeName(), fromV1ToV2(structType, struct.getValues(), converterContext)); } else { - throw new AtlasBaseException(AtlasErrorCode.UNEXPECTED_TYPE, "Map or IStruct", v1Obj.getClass().getCanonicalName()); + throw new AtlasBaseException(AtlasErrorCode.UNEXPECTED_TYPE, "Map or Struct", v1Obj.getClass().getCanonicalName()); } } @@ -118,7 +114,8 @@ public class AtlasStructFormatConverter extends AtlasAbstractFormatConverter { } protected Map<String, Object> fromV2ToV1(AtlasStructType structType, Map<String, Object> attributes, ConverterContext context) throws AtlasBaseException { - Map<String, Object> ret = null; + Map<String, Object> ret = null; + boolean isEntityType = structType instanceof AtlasEntityType; if (MapUtils.isNotEmpty(attributes)) { ret = new HashMap<>(); @@ -132,13 +129,80 @@ public class AtlasStructFormatConverter extends AtlasAbstractFormatConverter { continue; } - AtlasType attrType = attr.getAttributeType(); + AtlasType attrType = attr.getAttributeType(); + AtlasFormatConverter attrConverter = converterRegistry.getConverter(attrType.getTypeCategory()); + Object v2Value = attributes.get(attr.getName()); + + if (v2Value != null && isEntityType && attr.isOwnedRef()) { + if (LOG.isDebugEnabled()) { + LOG.debug("{}: is ownedRef, attrType={}", attr.getQualifiedName(), attrType.getTypeName()); + } + + if (attrType instanceof AtlasArrayType) { + AtlasArrayType arrayType = (AtlasArrayType) attrType; + AtlasType elemType = arrayType.getElementType(); + String elemTypeName; + + if (elemType instanceof AtlasObjectIdType) { + elemTypeName = ((AtlasObjectIdType) elemType).getObjectType(); + } else { + elemTypeName = elemType.getTypeName(); + } + + AtlasEntityType entityType = typeRegistry.getEntityTypeByName(elemTypeName);; + + if (entityType != null) { + Collection<?> arrayValue = (Collection<?>) v2Value; + List<AtlasEntity> entities = new ArrayList<>(arrayValue.size()); + + for (Object arrayElem : arrayValue) { + String entityGuid = getGuid(arrayElem); + AtlasEntity entity = StringUtils.isNotEmpty(entityGuid) ? context.getById(entityGuid) : null; + + if (entity != null) { + entities.add(entity); + } else { + LOG.warn("{}: not replacing objIdList with entityList - entity not found guid={}", attr.getQualifiedName(), entityGuid); + + entities = null; + break; + } + } + + if (entities != null) { + v2Value = entities; + attrType = new AtlasArrayType(entityType); + + if (LOG.isDebugEnabled()) { + LOG.debug("{}: replaced objIdList with entityList", attr.getQualifiedName()); + } + } + } else { + LOG.warn("{}: not replacing objIdList with entityList - elementType {} is not an entityType", attr.getQualifiedName(), elemTypeName); + } + } else if (attrType instanceof AtlasObjectIdType) { + String entityGuid = getGuid(v2Value); + AtlasEntity entity = StringUtils.isNotEmpty(entityGuid) ? context.getById(entityGuid) : null; + AtlasEntityType entityType = entity != null ? typeRegistry.getEntityTypeByName(entity.getTypeName()) : null; + + if (entity != null && entityType != null) { + v2Value = entity; + attrType = entityType; + attrConverter = converterRegistry.getConverter(attrType.getTypeCategory()); + + if (LOG.isDebugEnabled()) { + LOG.debug("{}: replaced objId with entity guid={}", attr.getQualifiedName(), entityGuid); + } + } else { + LOG.warn("{}: not replacing objId with entity - entity not found guid={}", attr.getQualifiedName(), entityGuid); + } + } else { + LOG.warn("{}: not replacing objId with entity - unexpected attribute-type {}", attr.getQualifiedName(), attrType.getTypeName()); + } + } - Object v2Value = attributes.get(attr.getName()); - Object v1Value; + Object v1Value = attrConverter.fromV2ToV1(v2Value, attrType, context); - AtlasFormatConverter attrConverter = converterRegistry.getConverter(attrType.getTypeCategory()); - v1Value = attrConverter.fromV2ToV1(v2Value, attrType, context); ret.put(attr.getName(), v1Value); } } @@ -146,6 +210,24 @@ public class AtlasStructFormatConverter extends AtlasAbstractFormatConverter { return ret; } + private String getGuid(Object obj) { + final String ret; + + if (obj instanceof AtlasObjectId) { + AtlasObjectId objId = (AtlasObjectId) obj; + + ret = objId.getGuid(); + } else if (obj instanceof Map) { + Map v2Map = (Map) obj; + + ret = (String)v2Map.get(AtlasObjectId.KEY_GUID); + } else { + ret = null; + } + + return ret; + } + protected Map<String, Object> fromV1ToV2(AtlasStructType structType, Map attributes, ConverterContext context) throws AtlasBaseException { Map<String, Object> ret = null; @@ -162,8 +244,7 @@ public class AtlasStructFormatConverter extends AtlasAbstractFormatConverter { continue; } - AtlasType attrType = attr.getAttributeType(); - + AtlasType attrType = attr.getAttributeType(); AtlasFormatConverter attrConverter = converterRegistry.getConverter(attrType.getTypeCategory()); Object v1Value = attributes.get(attr.getName()); Object v2Value = attrConverter.fromV1ToV2(v1Value, attrType, context);
http://git-wip-us.apache.org/repos/asf/atlas/blob/0877e47c/repository/src/main/java/org/apache/atlas/repository/converters/TypeConverterUtil.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/converters/TypeConverterUtil.java b/repository/src/main/java/org/apache/atlas/repository/converters/TypeConverterUtil.java index 7902100..564f459 100644 --- a/repository/src/main/java/org/apache/atlas/repository/converters/TypeConverterUtil.java +++ b/repository/src/main/java/org/apache/atlas/repository/converters/TypeConverterUtil.java @@ -24,10 +24,7 @@ import static org.apache.atlas.model.typedef.AtlasStructDef.AtlasConstraintDef.C import static org.apache.atlas.model.typedef.AtlasStructDef.AtlasConstraintDef.CONSTRAINT_PARAM_ATTRIBUTE; import static org.apache.atlas.type.AtlasTypeUtil.isArrayType; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Set; +import java.util.*; import org.apache.atlas.exception.AtlasBaseException; import org.apache.atlas.model.typedef.AtlasClassificationDef; @@ -40,6 +37,13 @@ import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef.Cardinali import org.apache.atlas.model.typedef.AtlasStructDef.AtlasConstraintDef; import org.apache.atlas.model.typedef.AtlasTypeDefHeader; import org.apache.atlas.model.typedef.AtlasTypesDef; +import org.apache.atlas.model.v1.typedef.AttributeDefinition; +import org.apache.atlas.model.v1.typedef.ClassTypeDefinition; +import org.apache.atlas.model.v1.typedef.EnumTypeDefinition; +import org.apache.atlas.model.v1.typedef.Multiplicity; +import org.apache.atlas.model.v1.typedef.StructTypeDefinition; +import org.apache.atlas.model.v1.typedef.TraitTypeDefinition; +import org.apache.atlas.model.v1.typedef.TypesDef; import org.apache.atlas.repository.store.graph.v1.AtlasStructDefStoreV1; import org.apache.atlas.type.AtlasClassificationType; import org.apache.atlas.type.AtlasEntityType; @@ -49,25 +53,12 @@ import org.apache.atlas.type.AtlasStructType.AtlasAttribute; import org.apache.atlas.type.AtlasType; import org.apache.atlas.type.AtlasTypeRegistry; import org.apache.atlas.type.AtlasTypeUtil; -import org.apache.atlas.typesystem.TypesDef; -import org.apache.atlas.typesystem.json.TypesSerialization; -import org.apache.atlas.typesystem.types.AttributeDefinition; -import org.apache.atlas.typesystem.types.ClassType; -import org.apache.atlas.typesystem.types.EnumTypeDefinition; -import org.apache.atlas.typesystem.types.EnumValue; -import org.apache.atlas.typesystem.types.HierarchicalTypeDefinition; -import org.apache.atlas.typesystem.types.Multiplicity; -import org.apache.atlas.typesystem.types.StructTypeDefinition; -import org.apache.atlas.typesystem.types.TraitType; -import org.apache.atlas.typesystem.types.utils.TypesUtil; +import org.apache.atlas.model.v1.typedef.EnumTypeDefinition.EnumValue; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSet; - public final class TypeConverterUtil { private TypeConverterUtil() {} @@ -92,75 +83,58 @@ public final class TypeConverterUtil { } private static TypesDef enumToTypesDef(AtlasEnumType enumType) { - TypesDef ret = null; - AtlasEnumDef enumDef = enumType.getEnumDef(); - String enumName = enumDef.getName(); - String enumDesc = enumDef.getDescription(); - String enumVersion = enumDef.getTypeVersion(); - EnumValue[] enumValues = getEnumValues(enumDef.getElementDefs()); + String enumName = enumDef.getName(); + String enumDesc = enumDef.getDescription(); + String enumVersion = enumDef.getTypeVersion(); + List<EnumValue> enumValues = getEnumValues(enumDef.getElementDefs()); - if (enumName != null && enumValues != null && enumValues.length > 0) { - EnumTypeDefinition enumTypeDef = new EnumTypeDefinition(enumName, enumDesc, enumVersion, enumValues); + EnumTypeDefinition enumTypeDef = new EnumTypeDefinition(enumName, enumDesc, enumVersion, enumValues); - ret = TypesUtil.getTypesDef(ImmutableList.of(enumTypeDef), - ImmutableList.<StructTypeDefinition>of(), - ImmutableList.<HierarchicalTypeDefinition<TraitType>>of(), - ImmutableList.<HierarchicalTypeDefinition<ClassType>>of()); - } + TypesDef ret = new TypesDef(Arrays.asList(enumTypeDef), null, null, null); return ret; } - private static TypesDef structToTypesDef(AtlasStructType structType, AtlasTypeRegistry registry) - throws AtlasBaseException { - String typeName = structType.getStructDef().getName(); - String typeDesc = structType.getStructDef().getDescription(); - String typeVersion = structType.getStructDef().getTypeVersion(); - AttributeDefinition[] attributes = getAttributes(structType, registry); - StructTypeDefinition structTypeDef = TypesUtil.createStructTypeDef(typeName, typeDesc, typeVersion, attributes); + private static TypesDef structToTypesDef(AtlasStructType structType, AtlasTypeRegistry registry) { + String typeName = structType.getStructDef().getName(); + String typeDesc = structType.getStructDef().getDescription(); + String typeVersion = structType.getStructDef().getTypeVersion(); + List<AttributeDefinition> attributes = getAttributes(structType, registry); - TypesDef ret = TypesUtil.getTypesDef(ImmutableList.<EnumTypeDefinition>of(), - ImmutableList.of(structTypeDef), - ImmutableList.<HierarchicalTypeDefinition<TraitType>>of(), - ImmutableList.<HierarchicalTypeDefinition<ClassType>>of()); + StructTypeDefinition structTypeDef = new StructTypeDefinition(typeName, typeDesc, typeVersion, attributes); + + TypesDef ret = new TypesDef(null, Arrays.asList(structTypeDef), null, null); return ret; } - private static TypesDef entityToTypesDef(AtlasEntityType entityType, AtlasTypeRegistry registry) - throws AtlasBaseException { - String typeName = entityType.getEntityDef().getName(); - String typeDesc = entityType.getEntityDef().getDescription(); - String typeVersion = entityType.getEntityDef().getTypeVersion(); - ImmutableSet superTypes = ImmutableSet.copyOf(entityType.getEntityDef().getSuperTypes()); - AttributeDefinition[] attributes = getAttributes(entityType, registry); - - HierarchicalTypeDefinition<ClassType> classType = TypesUtil.createClassTypeDef(typeName, typeDesc, typeVersion, - superTypes, attributes); - TypesDef ret = TypesUtil.getTypesDef(ImmutableList.<EnumTypeDefinition>of(), - ImmutableList.<StructTypeDefinition>of(), - ImmutableList.<HierarchicalTypeDefinition<TraitType>>of(), - ImmutableList.of(classType)); + private static TypesDef entityToTypesDef(AtlasEntityType entityType, AtlasTypeRegistry registry) { + String typeName = entityType.getEntityDef().getName(); + String typeDesc = entityType.getEntityDef().getDescription(); + String typeVersion = entityType.getEntityDef().getTypeVersion(); + Set<String> superTypes = entityType.getEntityDef().getSuperTypes(); + List<AttributeDefinition> attributes = getAttributes(entityType, registry); + + ClassTypeDefinition classTypeDef = new ClassTypeDefinition(typeName, typeDesc, typeVersion, attributes, superTypes); + + TypesDef ret = new TypesDef(null, null, null, Arrays.asList(classTypeDef)); return ret; } - private static TypesDef classificationToTypesDef(AtlasClassificationType classificationType, - AtlasTypeRegistry registry) throws AtlasBaseException { - String typeName = classificationType.getClassificationDef().getName(); - String typeDesc = classificationType.getClassificationDef().getDescription(); - String typeVersion = classificationType.getClassificationDef().getTypeVersion(); - ImmutableSet superTypes = ImmutableSet.copyOf(classificationType.getClassificationDef().getSuperTypes()); - AttributeDefinition[] attributes = getAttributes(classificationType, registry); - - HierarchicalTypeDefinition traitType = TypesUtil.createTraitTypeDef(typeName, typeDesc, typeVersion, superTypes, - attributes); - TypesDef ret = TypesUtil.getTypesDef(ImmutableList.<EnumTypeDefinition>of(), - ImmutableList.<StructTypeDefinition>of(), - ImmutableList.<HierarchicalTypeDefinition<TraitType>>of(traitType), - ImmutableList.<HierarchicalTypeDefinition<ClassType>>of()); + private static TypesDef classificationToTypesDef(AtlasClassificationType classificationType, AtlasTypeRegistry registry) { + String typeName = classificationType.getClassificationDef().getName(); + String typeDesc = classificationType.getClassificationDef().getDescription(); + String typeVersion = classificationType.getClassificationDef().getTypeVersion(); + Set<String> superTypes = new HashSet<>(classificationType.getClassificationDef().getSuperTypes()); + List<AttributeDefinition> attributes = getAttributes(classificationType, registry); + + TraitTypeDefinition traitTypeDef = new TraitTypeDefinition(typeName, typeDesc, typeVersion, attributes, superTypes); + + TypesDef ret = new TypesDef(null, null, Arrays.asList(traitTypeDef), null); + return ret; } @@ -174,24 +148,24 @@ public final class TypeConverterUtil { throw new AtlasBaseException(INVALID_TYPE_DEFINITION, typeDefinition); } - TypesDef typesDef = TypesSerialization.fromJson(typeDefinition); - if (CollectionUtils.isNotEmpty(typesDef.enumTypesAsJavaList())) { - List<AtlasEnumDef> enumDefs = toAtlasEnumDefs(typesDef.enumTypesAsJavaList()); + TypesDef typesDef = AtlasType.fromV1Json(typeDefinition, TypesDef.class); + if (CollectionUtils.isNotEmpty(typesDef.getEnumTypes())) { + List<AtlasEnumDef> enumDefs = toAtlasEnumDefs(typesDef.getEnumTypes()); ret.setEnumDefs(enumDefs); } - if (CollectionUtils.isNotEmpty(typesDef.structTypesAsJavaList())) { - List<AtlasStructDef> structDefs = toAtlasStructDefs(typesDef.structTypesAsJavaList()); + if (CollectionUtils.isNotEmpty(typesDef.getStructTypes())) { + List<AtlasStructDef> structDefs = toAtlasStructDefs(typesDef.getStructTypes()); ret.setStructDefs(structDefs); } - if (CollectionUtils.isNotEmpty(typesDef.classTypesAsJavaList())) { - List<AtlasEntityDef> entityDefs = toAtlasEntityDefs(typesDef.classTypesAsJavaList(), registry); + if (CollectionUtils.isNotEmpty(typesDef.getClassTypes())) { + List<AtlasEntityDef> entityDefs = toAtlasEntityDefs(typesDef.getClassTypes(), registry); ret.setEntityDefs(entityDefs); } - if (CollectionUtils.isNotEmpty(typesDef.traitTypesAsJavaList())) { - List<AtlasClassificationDef> classificationDefs = toAtlasClassificationDefs(typesDef.traitTypesAsJavaList()); + if (CollectionUtils.isNotEmpty(typesDef.getTraitTypes())) { + List<AtlasClassificationDef> classificationDefs = toAtlasClassificationDefs(typesDef.getTraitTypes()); ret.setClassificationDefs(classificationDefs); } @@ -203,7 +177,7 @@ public final class TypeConverterUtil { return ret; } - public static ImmutableList<String> getTypeNames(List<AtlasTypeDefHeader> atlasTypesDefs) { + public static List<String> getTypeNames(List<AtlasTypeDefHeader> atlasTypesDefs) { List<String> ret = new ArrayList<String>(); if (CollectionUtils.isNotEmpty(atlasTypesDefs)) { for (AtlasTypeDefHeader atlasTypesDef : atlasTypesDefs) { @@ -211,7 +185,7 @@ public final class TypeConverterUtil { } } - return ImmutableList.copyOf(ret); + return ret; } public static List<String> getTypeNames(AtlasTypesDef typesDef) { @@ -224,10 +198,10 @@ public final class TypeConverterUtil { for (EnumTypeDefinition enumType : enumTypeDefinitions) { AtlasEnumDef enumDef = new AtlasEnumDef(); - enumDef.setName(enumType.name); - enumDef.setDescription(enumType.description); - enumDef.setTypeVersion(enumType.version); - enumDef.setElementDefs(getAtlasEnumElementDefs(enumType.enumValues)); + enumDef.setName(enumType.getName()); + enumDef.setDescription(enumType.getDescription()); + enumDef.setTypeVersion(enumType.getVersion()); + enumDef.setElementDefs(getAtlasEnumElementDefs(enumType.getEnumValues())); ret.add(enumDef); } @@ -235,80 +209,65 @@ public final class TypeConverterUtil { return ret; } - private static List<AtlasStructDef> toAtlasStructDefs(List<StructTypeDefinition> structTypeDefinitions) - throws AtlasBaseException { - List<AtlasStructDef> ret = new ArrayList<AtlasStructDef>(); + private static List<AtlasStructDef> toAtlasStructDefs(List<StructTypeDefinition> structTypeDefinitions) { + List<AtlasStructDef> ret = new ArrayList<>(); for (StructTypeDefinition structType : structTypeDefinitions) { - AtlasStructDef structDef = new AtlasStructDef(); List<AtlasAttributeDef> attrDefs = new ArrayList<AtlasAttributeDef>(); - structDef.setName(structType.typeName); - structDef.setDescription(structType.typeDescription); - structDef.setTypeVersion(structType.typeVersion); - - AttributeDefinition[] attrDefinitions = structType.attributeDefinitions; - for (AttributeDefinition attrDefinition : attrDefinitions) { - attrDefs.add(toAtlasAttributeDef(attrDefinition)); + if (CollectionUtils.isNotEmpty(structType.getAttributeDefinitions())) { + for (AttributeDefinition attrDefinition : structType.getAttributeDefinitions()) { + attrDefs.add(toAtlasAttributeDef(attrDefinition)); + } } - structDef.setAttributeDefs(attrDefs); + AtlasStructDef structDef = new AtlasStructDef(structType.getTypeName(), structType.getTypeDescription(), structType.getTypeVersion(), attrDefs); + ret.add(structDef); } return ret; } - private static List<AtlasClassificationDef> toAtlasClassificationDefs(List<HierarchicalTypeDefinition<TraitType>> traitTypeDefinitions) - throws AtlasBaseException { - List<AtlasClassificationDef> ret = new ArrayList<AtlasClassificationDef>(); + private static List<AtlasClassificationDef> toAtlasClassificationDefs(List<TraitTypeDefinition> traitTypeDefinitions) { + List<AtlasClassificationDef> ret = new ArrayList<>(); - for (HierarchicalTypeDefinition<TraitType> traitType : traitTypeDefinitions) { - AtlasClassificationDef classifDef = new AtlasClassificationDef(); + for (TraitTypeDefinition traitType : traitTypeDefinitions) { List<AtlasAttributeDef> attrDefs = new ArrayList<AtlasAttributeDef>(); - classifDef.setName(traitType.typeName); - classifDef.setDescription(traitType.typeDescription); - classifDef.setTypeVersion(traitType.typeVersion); - classifDef.setSuperTypes(traitType.superTypes); - - AttributeDefinition[] attrDefinitions = traitType.attributeDefinitions; - for (AttributeDefinition attrDefinition : attrDefinitions) { - attrDefs.add(toAtlasAttributeDef(attrDefinition)); + if (CollectionUtils.isNotEmpty(traitType.getAttributeDefinitions())) { + for (AttributeDefinition attrDefinition : traitType.getAttributeDefinitions()) { + attrDefs.add(toAtlasAttributeDef(attrDefinition)); + } } - classifDef.setAttributeDefs(attrDefs); + AtlasClassificationDef classifDef = new AtlasClassificationDef(traitType.getTypeName(), traitType.getTypeDescription(), traitType.getTypeVersion(), attrDefs, traitType.getSuperTypes()); + ret.add(classifDef); } return ret; } - private static List<AtlasEntityDef> toAtlasEntityDefs(List<HierarchicalTypeDefinition<ClassType>> classTypeDefinitions, - AtlasTypeRegistry registry) throws AtlasBaseException { - List<AtlasEntityDef> atlasEntityDefs = new ArrayList<AtlasEntityDef>(); + private static List<AtlasEntityDef> toAtlasEntityDefs(List<ClassTypeDefinition> classTypeDefinitions, AtlasTypeRegistry registry) { + List<AtlasEntityDef> ret = new ArrayList<>(); - for (HierarchicalTypeDefinition<ClassType> classType : classTypeDefinitions) { + for (ClassTypeDefinition classType : classTypeDefinitions) { List<AtlasAttributeDef> attrDefs = new ArrayList<AtlasAttributeDef>(); - AtlasEntityDef atlasEntityDef = new AtlasEntityDef(); - String classTypeDefName = classType.typeName; - - atlasEntityDef.setName(classTypeDefName); - atlasEntityDef.setDescription(classType.typeDescription); - atlasEntityDef.setTypeVersion(classType.typeVersion); - atlasEntityDef.setSuperTypes(classType.superTypes); - - AttributeDefinition[] attrDefinitions = classType.attributeDefinitions; - for (AttributeDefinition oldAttr : attrDefinitions) { - AtlasAttributeDef newAttr = toAtlasAttributeDef(oldAttr); - attrDefs.add(newAttr); + + if (CollectionUtils.isNotEmpty(classType.getAttributeDefinitions())) { + for (AttributeDefinition oldAttr : classType.getAttributeDefinitions()) { + AtlasAttributeDef newAttr = toAtlasAttributeDef(oldAttr); + attrDefs.add(newAttr); + } } - atlasEntityDef.setAttributeDefs(attrDefs); - atlasEntityDefs.add(atlasEntityDef); + AtlasEntityDef entityDef = new AtlasEntityDef(classType.getTypeName(), classType.getTypeDescription(), classType.getTypeVersion(), attrDefs, classType.getSuperTypes()); + + ret.add(entityDef); } - return atlasEntityDefs; + return ret; } private static String getArrayTypeName(String attrType) { @@ -323,17 +282,17 @@ public final class TypeConverterUtil { return ret; } - private static List<AtlasEnumElementDef> getAtlasEnumElementDefs(EnumValue[] enums) { - List<AtlasEnumElementDef> ret = new ArrayList<AtlasEnumElementDef>(); + private static List<AtlasEnumElementDef> getAtlasEnumElementDefs(List<EnumValue> enums) { + List<AtlasEnumElementDef> ret = new ArrayList<>(); for (EnumValue enumElem : enums) { - ret.add(new AtlasEnumElementDef(enumElem.value, null, enumElem.ordinal)); + ret.add(new AtlasEnumElementDef(enumElem.getValue(), null, enumElem.getOrdinal())); } return ret; } - private static EnumValue[] getEnumValues(List<AtlasEnumElementDef> enumDefs) { + private static List<EnumValue> getEnumValues(List<AtlasEnumElementDef> enumDefs) { List<EnumValue> ret = new ArrayList<EnumValue>(); if (CollectionUtils.isNotEmpty(enumDefs)) { @@ -344,32 +303,30 @@ public final class TypeConverterUtil { } } - return ret.toArray(new EnumValue[ret.size()]); + return ret; } public static AtlasAttributeDef toAtlasAttributeDef(final AttributeDefinition attrDefinition) { - AtlasAttributeDef ret = new AtlasAttributeDef(); + AtlasAttributeDef ret = new AtlasAttributeDef(attrDefinition.getName(), attrDefinition.getDataTypeName()); - ret.setName(attrDefinition.name); - ret.setTypeName(attrDefinition.dataTypeName); - ret.setIsIndexable(attrDefinition.isIndexable); - ret.setIsUnique(attrDefinition.isUnique); - if (attrDefinition.isComposite) { + ret.setIsIndexable(attrDefinition.getIsIndexable()); + ret.setIsUnique(attrDefinition.getIsUnique()); + if (attrDefinition.getIsComposite()) { ret.addConstraint(new AtlasConstraintDef(CONSTRAINT_TYPE_OWNED_REF)); } - if (StringUtils.isNotBlank(attrDefinition.reverseAttributeName)) { + if (StringUtils.isNotBlank(attrDefinition.getReverseAttributeName())) { ret.addConstraint(new AtlasConstraintDef(CONSTRAINT_TYPE_INVERSE_REF, new HashMap<String, Object>() {{ - put(CONSTRAINT_PARAM_ATTRIBUTE, attrDefinition.reverseAttributeName); + put(CONSTRAINT_PARAM_ATTRIBUTE, attrDefinition.getReverseAttributeName()); }})); } // Multiplicity attribute mapping - Multiplicity multiplicity = attrDefinition.multiplicity; - int minCount = multiplicity.lower; - int maxCount = multiplicity.upper; - boolean isUnique = multiplicity.isUnique; + Multiplicity multiplicity = attrDefinition.getMultiplicity(); + int minCount = multiplicity.getLower(); + int maxCount = multiplicity.getUpper(); + boolean isUnique = multiplicity.getIsUnique(); if (minCount == 0) { ret.setIsOptional(true); @@ -395,7 +352,7 @@ public final class TypeConverterUtil { return ret; } - private static AttributeDefinition[] getAttributes(AtlasStructType structType, AtlasTypeRegistry registry) throws AtlasBaseException { + private static List<AttributeDefinition> getAttributes(AtlasStructType structType, AtlasTypeRegistry registry) { List<AttributeDefinition> ret = new ArrayList<>(); List<AtlasAttributeDef> attrDefs = structType.getStructDef().getAttributeDefs(); @@ -403,10 +360,12 @@ public final class TypeConverterUtil { for (AtlasAttributeDef attrDef : attrDefs) { AtlasAttribute attribute = structType.getAttribute(attrDef.getName()); - ret.add(AtlasStructDefStoreV1.toAttributeDefintion(attribute)); + AttributeDefinition oldAttrDef = AtlasStructDefStoreV1.toAttributeDefintion(attribute); + + ret.add(new AttributeDefinition(oldAttrDef.getName(), oldAttrDef.getDataTypeName(), new Multiplicity(oldAttrDef.getMultiplicity()), oldAttrDef.getIsComposite(), oldAttrDef.getIsUnique(), oldAttrDef.getIsIndexable(), oldAttrDef.getReverseAttributeName())); } } - return ret.toArray(new AttributeDefinition[ret.size()]); + return ret; } } http://git-wip-us.apache.org/repos/asf/atlas/blob/0877e47c/repository/src/main/java/org/apache/atlas/repository/graph/DeleteHandler.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/graph/DeleteHandler.java b/repository/src/main/java/org/apache/atlas/repository/graph/DeleteHandler.java deleted file mode 100644 index f0fef1f..0000000 --- a/repository/src/main/java/org/apache/atlas/repository/graph/DeleteHandler.java +++ /dev/null @@ -1,468 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * <p/> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p/> - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.atlas.repository.graph; - -import static org.apache.atlas.repository.graph.GraphHelper.EDGE_LABEL_PREFIX; -import static org.apache.atlas.repository.graph.GraphHelper.string; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Set; - -import org.apache.atlas.AtlasException; -import org.apache.atlas.RequestContext; -import org.apache.atlas.repository.Constants; -import org.apache.atlas.repository.graph.GraphHelper.VertexInfo; -import org.apache.atlas.repository.graphdb.AtlasEdge; -import org.apache.atlas.repository.graphdb.AtlasEdgeDirection; -import org.apache.atlas.repository.graphdb.AtlasVertex; -import org.apache.atlas.typesystem.exception.NullRequiredAttributeException; -import org.apache.atlas.typesystem.persistence.Id; -import org.apache.atlas.typesystem.types.AttributeInfo; -import org.apache.atlas.typesystem.types.DataTypes; -import org.apache.atlas.typesystem.types.FieldMapping; -import org.apache.atlas.typesystem.types.HierarchicalType; -import org.apache.atlas.typesystem.types.IDataType; -import org.apache.atlas.typesystem.types.StructType; -import org.apache.atlas.typesystem.types.TypeSystem; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public abstract class DeleteHandler { - public static final Logger LOG = LoggerFactory.getLogger(DeleteHandler.class); - - protected static final GraphHelper graphHelper = GraphHelper.getInstance(); - - protected TypeSystem typeSystem; - private boolean shouldUpdateReverseAttribute; - private boolean softDelete; - - public DeleteHandler(TypeSystem typeSystem, boolean shouldUpdateReverseAttribute, boolean softDelete) { - this.typeSystem = typeSystem; - this.shouldUpdateReverseAttribute = shouldUpdateReverseAttribute; - this.softDelete = softDelete; - } - - /** - * Deletes the specified entity vertices. - * Deletes any traits, composite entities, and structs owned by each entity. - * Also deletes all the references from/to the entity. - * - * @param instanceVertices - * @throws AtlasException - */ - public void deleteEntities(Collection<AtlasVertex> instanceVertices) throws AtlasException { - RequestContext requestContext = RequestContext.get(); - - Set<AtlasVertex> deletionCandidateVertices = new HashSet<>(); - - for (AtlasVertex instanceVertex : instanceVertices) { - String guid = GraphHelper.getGuid(instanceVertex); - Id.EntityState state = GraphHelper.getState(instanceVertex); - if (requestContext.getDeletedEntityIds().contains(guid) || state == Id.EntityState.DELETED) { - if (LOG.isDebugEnabled()) { - LOG.debug("Skipping deletion of {} as it is already deleted", guid); - } - - continue; - } - - // Get GUIDs and vertices for all deletion candidates. - Set<VertexInfo> compositeVertices = graphHelper.getCompositeVertices(instanceVertex); - - // Record all deletion candidate GUIDs in RequestContext - // and gather deletion candidate vertices. - for (VertexInfo vertexInfo : compositeVertices) { - requestContext.recordEntityDelete(vertexInfo.getGuid(), vertexInfo.getTypeName()); - deletionCandidateVertices.add(vertexInfo.getVertex()); - } - } - - // Delete traits and vertices. - for (AtlasVertex deletionCandidateVertex : deletionCandidateVertices) { - deleteAllTraits(deletionCandidateVertex); - deleteTypeVertex(deletionCandidateVertex, false); - } - } - - protected abstract void deleteEdge(AtlasEdge edge, boolean force) throws AtlasException; - - /** - * Deletes a type vertex - can be entity(class type) or just vertex(struct/trait type) - * @param instanceVertex - * @param typeCategory - * @throws AtlasException - */ - protected void deleteTypeVertex(AtlasVertex instanceVertex, DataTypes.TypeCategory typeCategory, boolean force) throws AtlasException { - switch (typeCategory) { - case STRUCT: - case TRAIT: - deleteTypeVertex(instanceVertex, force); - break; - - case CLASS: - deleteEntities(Collections.singletonList(instanceVertex)); - break; - - default: - throw new IllegalStateException("Type category " + typeCategory + " not handled"); - } - } - - /** - * Deleting any type vertex. Goes over the complex attributes and removes the references - * @param instanceVertex - * @throws AtlasException - */ - protected void deleteTypeVertex(AtlasVertex instanceVertex, boolean force) throws AtlasException { - if (LOG.isDebugEnabled()) { - LOG.debug("Deleting {}", string(instanceVertex)); - } - - String typeName = GraphHelper.getTypeName(instanceVertex); - IDataType type = typeSystem.getDataType(IDataType.class, typeName); - FieldMapping fieldMapping = getFieldMapping(type); - - for (AttributeInfo attributeInfo : fieldMapping.fields.values()) { - if (LOG.isDebugEnabled()) { - LOG.debug("Deleting attribute {} for {}", attributeInfo.name, string(instanceVertex)); - } - - String edgeLabel = GraphHelper.getEdgeLabel(type, attributeInfo); - - switch (attributeInfo.dataType().getTypeCategory()) { - case CLASS: - //If its class attribute, delete the reference - deleteEdgeReference(instanceVertex, edgeLabel, DataTypes.TypeCategory.CLASS, attributeInfo.isComposite); - break; - - case STRUCT: - //If its struct attribute, delete the reference - deleteEdgeReference(instanceVertex, edgeLabel, DataTypes.TypeCategory.STRUCT, false); - break; - - case ARRAY: - //For array attribute, if the element is struct/class, delete all the references - IDataType elementType = ((DataTypes.ArrayType) attributeInfo.dataType()).getElemType(); - DataTypes.TypeCategory elementTypeCategory = elementType.getTypeCategory(); - if (elementTypeCategory == DataTypes.TypeCategory.STRUCT || - elementTypeCategory == DataTypes.TypeCategory.CLASS) { - Iterator<AtlasEdge> edges = graphHelper.getOutGoingEdgesByLabel(instanceVertex, edgeLabel); - if (edges != null) { - while (edges.hasNext()) { - AtlasEdge edge = edges.next(); - deleteEdgeReference(edge, elementType.getTypeCategory(), attributeInfo.isComposite, false); - } - } - } - break; - - case MAP: - //For map attribute, if the value type is struct/class, delete all the references - DataTypes.MapType mapType = (DataTypes.MapType) attributeInfo.dataType(); - DataTypes.TypeCategory valueTypeCategory = mapType.getValueType().getTypeCategory(); - String propertyName = GraphHelper.getQualifiedFieldName(type, attributeInfo.name); - - if (valueTypeCategory == DataTypes.TypeCategory.STRUCT || - valueTypeCategory == DataTypes.TypeCategory.CLASS) { - List<String> keys = GraphHelper.getListProperty(instanceVertex, propertyName); - if (keys != null) { - for (String key : keys) { - String mapEdgeLabel = GraphHelper.getQualifiedNameForMapKey(edgeLabel, key); - deleteEdgeReference(instanceVertex, mapEdgeLabel, valueTypeCategory, attributeInfo.isComposite); - } - } - } - } - } - - deleteVertex(instanceVertex, force); - } - - /** - * Force delete is used to remove struct/trait in case of entity updates - * @param edge - * @param typeCategory - * @param isComposite - * @param forceDeleteStructTrait - * @return returns true if the edge reference is hard deleted - * @throws AtlasException - */ - public boolean deleteEdgeReference(AtlasEdge edge, DataTypes.TypeCategory typeCategory, boolean isComposite, - boolean forceDeleteStructTrait) throws AtlasException { - if (LOG.isDebugEnabled()) { - LOG.debug("Deleting {}", string(edge)); - } - - boolean forceDelete = - (typeCategory == DataTypes.TypeCategory.STRUCT || typeCategory == DataTypes.TypeCategory.TRAIT) && forceDeleteStructTrait; - if (typeCategory == DataTypes.TypeCategory.STRUCT || typeCategory == DataTypes.TypeCategory.TRAIT - || (typeCategory == DataTypes.TypeCategory.CLASS && isComposite)) { - //If the vertex is of type struct/trait, delete the edge and then the reference vertex as the vertex is not shared by any other entities. - //If the vertex is of type class, and its composite attribute, this reference vertex' lifecycle is controlled - //through this delete, hence delete the edge and the reference vertex. - AtlasVertex vertexForDelete = edge.getInVertex(); - - //If deleting the edge and then the in vertex, reverse attribute shouldn't be updated - deleteEdge(edge, false, forceDelete); - deleteTypeVertex(vertexForDelete, typeCategory, forceDelete); - } else { - //If the vertex is of type class, and its not a composite attributes, the reference AtlasVertex' lifecycle is not controlled - //through this delete. Hence just remove the reference edge. Leave the reference AtlasVertex as is - - //If deleting just the edge, reverse attribute should be updated for any references - //For example, for the department type system, if the person's manager edge is deleted, subordinates of manager should be updated - deleteEdge(edge, true, false); - } - return !softDelete || forceDelete; - } - - public void deleteEdgeReference(AtlasVertex outVertex, String edgeLabel, DataTypes.TypeCategory typeCategory, - boolean isComposite) throws AtlasException { - AtlasEdge edge = graphHelper.getEdgeForLabel(outVertex, edgeLabel); - if (edge != null) { - deleteEdgeReference(edge, typeCategory, isComposite, false); - } - } - - protected void deleteEdge(AtlasEdge edge, boolean updateReverseAttribute, boolean force) throws AtlasException { - //update reverse attribute - if (updateReverseAttribute) { - AttributeInfo attributeInfo = getAttributeForEdge(edge.getLabel()); - if (attributeInfo.reverseAttributeName != null) { - deleteEdgeBetweenVertices(edge.getInVertex(), edge.getOutVertex(), - attributeInfo.reverseAttributeName); - } - } - - deleteEdge(edge, force); - } - - protected void deleteVertex(AtlasVertex instanceVertex, boolean force) throws AtlasException { - //Update external references(incoming edges) to this vertex - if (LOG.isDebugEnabled()) { - LOG.debug("Setting the external references to {} to null(removing edges)", string(instanceVertex)); - } - - for (AtlasEdge edge : (Iterable<AtlasEdge>) instanceVertex.getEdges(AtlasEdgeDirection.IN)) { - Id.EntityState edgeState = GraphHelper.getState(edge); - if (edgeState == Id.EntityState.ACTIVE) { - //Delete only the active edge references - AttributeInfo attribute = getAttributeForEdge(edge.getLabel()); - //TODO use delete edge instead?? - deleteEdgeBetweenVertices(edge.getOutVertex(), edge.getInVertex(), attribute.name); - } - } - _deleteVertex(instanceVertex, force); - } - - protected abstract void _deleteVertex(AtlasVertex instanceVertex, boolean force); - - /** - * Deletes the edge between outvertex and inVertex. The edge is for attribute attributeName of outVertex - * @param outVertex - * @param inVertex - * @param attributeName - * @throws AtlasException - */ - protected void deleteEdgeBetweenVertices(AtlasVertex outVertex, AtlasVertex inVertex, String attributeName) throws AtlasException { - if (LOG.isDebugEnabled()) { - LOG.debug("Removing edge from {} to {} with attribute name {}", string(outVertex), string(inVertex), - attributeName); - } - - String typeName = GraphHelper.getTypeName(outVertex); - String outId = GraphHelper.getGuid(outVertex); - Id.EntityState state = GraphHelper.getState(outVertex); - if ((outId != null && RequestContext.get().isDeletedEntity(outId)) || state == Id.EntityState.DELETED) { - //If the reference vertex is marked for deletion, skip updating the reference - return; - } - - IDataType type = typeSystem.getDataType(IDataType.class, typeName); - AttributeInfo attributeInfo = getFieldMapping(type).fields.get(attributeName); - String propertyName = GraphHelper.getQualifiedFieldName(type, attributeName); - String edgeLabel = EDGE_LABEL_PREFIX + propertyName; - AtlasEdge edge = null; - - switch (attributeInfo.dataType().getTypeCategory()) { - case CLASS: - //If its class attribute, its the only edge between two vertices - if (attributeInfo.multiplicity.nullAllowed()) { - edge = graphHelper.getEdgeForLabel(outVertex, edgeLabel); - if (shouldUpdateReverseAttribute) { - GraphHelper.setProperty(outVertex, propertyName, null); - } - } else { - // Cannot unset a required attribute. - throw new NullRequiredAttributeException("Cannot unset required attribute " + GraphHelper.getQualifiedFieldName(type, attributeName) + - " on " + GraphHelper.getVertexDetails(outVertex) + " edge = " + edgeLabel); - } - break; - - case ARRAY: - //If its array attribute, find the right edge between the two vertices and update array property - List<String> elements = GraphHelper.getListProperty(outVertex, propertyName); - if (elements != null) { - elements = new ArrayList<>(elements); //Make a copy, else list.remove reflects on titan.getProperty() - for (String elementEdgeId : elements) { - AtlasEdge elementEdge = graphHelper.getEdgeByEdgeId(outVertex, edgeLabel, elementEdgeId); - if (elementEdge == null) { - continue; - } - - AtlasVertex elementVertex = elementEdge.getInVertex(); - if (elementVertex.equals(inVertex)) { - edge = elementEdge; - - //TODO element.size includes deleted items as well. should exclude - if (!attributeInfo.multiplicity.nullAllowed() - && elements.size() <= attributeInfo.multiplicity.lower) { - // Deleting this edge would violate the attribute's lower bound. - throw new NullRequiredAttributeException( - "Cannot remove array element from required attribute " + - GraphHelper.getQualifiedFieldName(type, attributeName) + " on " - + GraphHelper.getVertexDetails(outVertex) + " " + GraphHelper.getEdgeDetails(elementEdge)); - } - - if (shouldUpdateReverseAttribute) { - //if composite attribute, remove the reference as well. else, just remove the edge - //for example, when table is deleted, process still references the table - //but when column is deleted, table will not reference the deleted column - if (LOG.isDebugEnabled()) { - LOG.debug("Removing edge {} from the array attribute {}", string(elementEdge), - attributeName); - } - - // Remove all occurrences of the edge ID from the list. - // This prevents dangling edge IDs (i.e. edge IDs for deleted edges) - // from the remaining in the list if there are duplicates. - elements.removeAll(Collections.singletonList(elementEdge.getId().toString())); - GraphHelper.setProperty(outVertex, propertyName, elements); - break; - - } - } - } - } - break; - - case MAP: - //If its map attribute, find the right edge between two vertices and update map property - List<String> keys = GraphHelper.getListProperty(outVertex, propertyName); - if (keys != null) { - keys = new ArrayList<>(keys); //Make a copy, else list.remove reflects on titan.getProperty() - for (String key : keys) { - String keyPropertyName = GraphHelper.getQualifiedNameForMapKey(propertyName, key); - String mapEdgeId = GraphHelper.getSingleValuedProperty(outVertex, keyPropertyName, String.class); - AtlasEdge mapEdge = graphHelper.getEdgeByEdgeId(outVertex, keyPropertyName, mapEdgeId); - if(mapEdge != null) { - AtlasVertex mapVertex = mapEdge.getInVertex(); - if (mapVertex.getId().toString().equals(inVertex.getId().toString())) { - //TODO keys.size includes deleted items as well. should exclude - if (attributeInfo.multiplicity.nullAllowed() || keys.size() > attributeInfo.multiplicity.lower) { - edge = mapEdge; - } else { - // Deleting this entry would violate the attribute's lower bound. - throw new NullRequiredAttributeException( - "Cannot remove map entry " + keyPropertyName + " from required attribute " + - GraphHelper.getQualifiedFieldName(type, attributeName) + " on " + GraphHelper.getVertexDetails(outVertex) + " " + GraphHelper.getEdgeDetails(mapEdge)); - } - - if (shouldUpdateReverseAttribute) { - //remove this key - if (LOG.isDebugEnabled()) { - LOG.debug("Removing edge {}, key {} from the map attribute {}", string(mapEdge), key, - attributeName); - } - - keys.remove(key); - GraphHelper.setProperty(outVertex, propertyName, keys); - GraphHelper.setProperty(outVertex, keyPropertyName, null); - } - break; - } - } - } - } - break; - - case STRUCT: - case TRAIT: - break; - - default: - throw new IllegalStateException("There can't be an edge from " + GraphHelper.getVertexDetails(outVertex) + " to " - + GraphHelper.getVertexDetails(inVertex) + " with attribute name " + attributeName + " which is not class/array/map attribute"); - } - - if (edge != null) { - deleteEdge(edge, false); - RequestContext requestContext = RequestContext.get(); - GraphHelper.setProperty(outVertex, Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY, - requestContext.getRequestTime()); - GraphHelper.setProperty(outVertex, Constants.MODIFIED_BY_KEY, requestContext.getUser()); - requestContext.recordEntityUpdate(outId); - } - } - - protected AttributeInfo getAttributeForEdge(String edgLabel) throws AtlasException { - AtlasEdgeLabel atlasEdgeLabel = new AtlasEdgeLabel(edgLabel); - IDataType referenceType = typeSystem.getDataType(IDataType.class, atlasEdgeLabel.getTypeName()); - return getFieldMapping(referenceType).fields.get(atlasEdgeLabel.getAttributeName()); - } - - protected FieldMapping getFieldMapping(IDataType type) { - switch (type.getTypeCategory()) { - case CLASS: - case TRAIT: - return ((HierarchicalType)type).fieldMapping(); - - case STRUCT: - return ((StructType)type).fieldMapping(); - - default: - throw new IllegalStateException("Type " + type + " doesn't have any fields!"); - } - } - - /** - * Delete all traits from the specified vertex. - * @param instanceVertex - * @throws AtlasException - */ - private void deleteAllTraits(AtlasVertex instanceVertex) throws AtlasException { - List<String> traitNames = GraphHelper.getTraitNames(instanceVertex); - - if (LOG.isDebugEnabled()) { - LOG.debug("Deleting traits {} for {}", traitNames, string(instanceVertex)); - } - - String typeName = GraphHelper.getTypeName(instanceVertex); - - for (String traitNameToBeDeleted : traitNames) { - String relationshipLabel = GraphHelper.getTraitLabel(typeName, traitNameToBeDeleted); - deleteEdgeReference(instanceVertex, relationshipLabel, DataTypes.TypeCategory.TRAIT, false); - } - } -} http://git-wip-us.apache.org/repos/asf/atlas/blob/0877e47c/repository/src/main/java/org/apache/atlas/repository/graph/EntityProcessor.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/graph/EntityProcessor.java b/repository/src/main/java/org/apache/atlas/repository/graph/EntityProcessor.java deleted file mode 100644 index 892b36d..0000000 --- a/repository/src/main/java/org/apache/atlas/repository/graph/EntityProcessor.java +++ /dev/null @@ -1,82 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.atlas.repository.graph; - -import org.apache.atlas.AtlasException; -import org.apache.atlas.repository.RepositoryException; -import org.apache.atlas.typesystem.IReferenceableInstance; -import org.apache.atlas.typesystem.ITypedReferenceableInstance; -import org.apache.atlas.typesystem.persistence.Id; -import org.apache.atlas.typesystem.types.DataTypes; -import org.apache.atlas.typesystem.types.ObjectGraphWalker; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.Map; - -@Deprecated -public final class EntityProcessor implements ObjectGraphWalker.NodeProcessor { - - private final Map<Id, IReferenceableInstance> idToInstanceMap; - - public EntityProcessor() { - idToInstanceMap = new LinkedHashMap<>(); - } - - public Collection<IReferenceableInstance> getInstances() { - ArrayList<IReferenceableInstance> instances = new ArrayList<>(idToInstanceMap.values()); - Collections.reverse(instances); - return instances; - } - - @Override - public void processNode(ObjectGraphWalker.Node nd) throws AtlasException { - IReferenceableInstance ref = null; - Id id = null; - - if (nd.attributeName == null) { - ref = (IReferenceableInstance) nd.instance; - id = ref.getId(); - } else if (nd.aInfo.dataType().getTypeCategory() == DataTypes.TypeCategory.CLASS) { - if (nd.value != null && (nd.value instanceof Id)) { - id = (Id) nd.value; - } - } - - if (id != null) { - if (id.isUnassigned()) { - if (ref != null) { - if (idToInstanceMap.containsKey(id)) { // Oops - throw new RepositoryException( - String.format("Unexpected internal error: Id %s processed again", id)); - } - - idToInstanceMap.put(id, ref); - } - } - } - } - - public void addInstanceIfNotExists(ITypedReferenceableInstance ref) { - if(!idToInstanceMap.containsKey(ref.getId())) { - idToInstanceMap.put(ref.getId(), ref); - } - } -} http://git-wip-us.apache.org/repos/asf/atlas/blob/0877e47c/repository/src/main/java/org/apache/atlas/repository/graph/FullTextMapper.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/graph/FullTextMapper.java b/repository/src/main/java/org/apache/atlas/repository/graph/FullTextMapper.java deleted file mode 100644 index 2e8ae0c..0000000 --- a/repository/src/main/java/org/apache/atlas/repository/graph/FullTextMapper.java +++ /dev/null @@ -1,174 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.atlas.repository.graph; - -import org.apache.atlas.AtlasException; -import org.apache.atlas.RequestContext; -import org.apache.atlas.repository.graphdb.AtlasVertex; -import org.apache.atlas.typesystem.ITypedInstance; -import org.apache.atlas.typesystem.ITypedReferenceableInstance; -import org.apache.atlas.typesystem.persistence.Id; -import org.apache.atlas.typesystem.types.AttributeInfo; -import org.apache.atlas.typesystem.types.DataTypes; -import org.apache.atlas.typesystem.types.EnumValue; -import org.apache.atlas.typesystem.types.IDataType; -import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.List; -import java.util.Map; - -@Deprecated -public class FullTextMapper { - - private static final Logger LOG = LoggerFactory.getLogger(FullTextMapper.class); - - private final GraphToTypedInstanceMapper graphToTypedInstanceMapper; - private final TypedInstanceToGraphMapper typedInstanceToGraphMapper; - - private static final GraphHelper graphHelper = GraphHelper.getInstance(); - - private static final String FULL_TEXT_DELIMITER = " "; - - public FullTextMapper(TypedInstanceToGraphMapper typedInstanceToGraphMapper, - GraphToTypedInstanceMapper graphToTypedInstanceMapper) { - this.graphToTypedInstanceMapper = graphToTypedInstanceMapper; - this.typedInstanceToGraphMapper = typedInstanceToGraphMapper; - } - - public String mapRecursive(AtlasVertex instanceVertex, boolean followReferences) throws AtlasException { - String guid = GraphHelper.getGuid(instanceVertex); - ITypedReferenceableInstance typedReference; - RequestContext context = RequestContext.get(); - typedReference = context.getInstanceV1(guid); - if (typedReference != null) { - - if (LOG.isDebugEnabled()) { - LOG.debug("Cache hit: guid = {}, entityId = {}", guid, typedReference.getId()._getId()); - } - } else { - typedReference = - graphToTypedInstanceMapper.mapGraphToTypedInstance(guid, instanceVertex); - context.cache(typedReference); - - if (LOG.isDebugEnabled()) { - LOG.debug("Cache miss: guid = {}, entityId = {}", guid, typedReference.getId().getId()); - } - } - String fullText = forInstance(typedReference, followReferences); - StringBuilder fullTextBuilder = - new StringBuilder(typedReference.getTypeName()).append(FULL_TEXT_DELIMITER).append(fullText); - - List<String> traits = typedReference.getTraits(); - for (String traitName : traits) { - String traitText = forInstance((ITypedInstance) typedReference.getTrait(traitName), false); - fullTextBuilder.append(FULL_TEXT_DELIMITER).append(traitName).append(FULL_TEXT_DELIMITER) - .append(traitText); - } - return fullTextBuilder.toString(); - } - - private String forAttribute(IDataType type, Object value, boolean followReferences) - throws AtlasException { - if (value == null) { - return null; - } - switch (type.getTypeCategory()) { - case PRIMITIVE: - return String.valueOf(value); - case ENUM: - - return ((EnumValue) value).value; - - case ARRAY: - StringBuilder fullText = new StringBuilder(); - IDataType elemType = ((DataTypes.ArrayType) type).getElemType(); - List list = (List) value; - - for (Object element : list) { - String elemFullText = forAttribute(elemType, element, false); - if (StringUtils.isNotEmpty(elemFullText)) { - fullText = fullText.append(FULL_TEXT_DELIMITER).append(elemFullText); - } - } - return fullText.toString(); - - case MAP: - fullText = new StringBuilder(); - IDataType keyType = ((DataTypes.MapType) type).getKeyType(); - IDataType valueType = ((DataTypes.MapType) type).getValueType(); - Map map = (Map) value; - - for (Object entryObj : map.entrySet()) { - Map.Entry entry = (Map.Entry) entryObj; - String keyFullText = forAttribute(keyType, entry.getKey(), false); - if (StringUtils.isNotEmpty(keyFullText)) { - fullText = fullText.append(FULL_TEXT_DELIMITER).append(keyFullText); - } - String valueFullText = forAttribute(valueType, entry.getValue(), false); - if (StringUtils.isNotEmpty(valueFullText)) { - fullText = fullText.append(FULL_TEXT_DELIMITER).append(valueFullText); - } - } - return fullText.toString(); - - case CLASS: - if (followReferences) { - Id refId = ((ITypedReferenceableInstance) value).getId(); - String refGuid = refId._getId(); - AtlasVertex refVertex = typedInstanceToGraphMapper.lookupVertex(refId); - if(refVertex == null) { - refVertex = graphHelper.getVertexForGUID(refGuid); - } - return mapRecursive(refVertex, false); - } - break; - - case STRUCT: - if (followReferences) { - return forInstance((ITypedInstance) value, true); - } - break; - - default: - throw new IllegalStateException("Unhandled type category " + type.getTypeCategory()); - - } - return null; - } - - private String forInstance(ITypedInstance typedInstance, boolean followReferences) - throws AtlasException { - StringBuilder fullText = new StringBuilder(); - for (AttributeInfo attributeInfo : typedInstance.fieldMapping().fields.values()) { - Object attrValue = typedInstance.get(attributeInfo.name); - if (attrValue == null) { - continue; - } - - String attrFullText = forAttribute(attributeInfo.dataType(), attrValue, followReferences); - if (StringUtils.isNotEmpty(attrFullText)) { - fullText = - fullText.append(FULL_TEXT_DELIMITER).append(attributeInfo.name).append(FULL_TEXT_DELIMITER) - .append(attrFullText); - } - } - return fullText.toString(); - } -} http://git-wip-us.apache.org/repos/asf/atlas/blob/0877e47c/repository/src/main/java/org/apache/atlas/repository/graph/FullTextMapperV2.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/graph/FullTextMapperV2.java b/repository/src/main/java/org/apache/atlas/repository/graph/FullTextMapperV2.java index 76acf8c..c42aa15 100644 --- a/repository/src/main/java/org/apache/atlas/repository/graph/FullTextMapperV2.java +++ b/repository/src/main/java/org/apache/atlas/repository/graph/FullTextMapperV2.java @@ -17,7 +17,7 @@ */ package org.apache.atlas.repository.graph; -import org.apache.atlas.RequestContext; +import org.apache.atlas.RequestContextV1; import org.apache.atlas.exception.AtlasBaseException; import org.apache.atlas.model.instance.AtlasClassification; import org.apache.atlas.model.instance.AtlasEntity; @@ -203,7 +203,7 @@ public class FullTextMapperV2 { } private AtlasEntityWithExtInfo getAndCacheEntity(String guid) throws AtlasBaseException { - RequestContext context = RequestContext.get(); + RequestContextV1 context = RequestContextV1.get(); AtlasEntityWithExtInfo entityWithExtInfo = context.getInstanceV2(guid); if (entityWithExtInfo == null) {