http://git-wip-us.apache.org/repos/asf/atlas/blob/435fe3fb/repository/src/main/java/org/apache/atlas/repository/graph/GraphHelper.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/graph/GraphHelper.java b/repository/src/main/java/org/apache/atlas/repository/graph/GraphHelper.java index ffe859b..3e60243 100755 --- a/repository/src/main/java/org/apache/atlas/repository/graph/GraphHelper.java +++ b/repository/src/main/java/org/apache/atlas/repository/graph/GraphHelper.java @@ -24,12 +24,15 @@ import com.google.common.collect.HashBiMap; import org.apache.atlas.ApplicationProperties; import org.apache.atlas.AtlasErrorCode; import org.apache.atlas.AtlasException; -import org.apache.atlas.RequestContext; +import org.apache.atlas.RequestContextV1; import org.apache.atlas.exception.AtlasBaseException; import org.apache.atlas.model.instance.AtlasEntity.Status; import org.apache.atlas.model.instance.AtlasObjectId; import org.apache.atlas.model.instance.AtlasRelationship; import org.apache.atlas.model.typedef.AtlasRelationshipDef; +import org.apache.atlas.v1.model.instance.Id; +import org.apache.atlas.v1.model.instance.Referenceable; +import org.apache.atlas.type.AtlasStructType.AtlasAttribute; import org.apache.atlas.type.AtlasStructType.AtlasAttribute.AtlasRelationshipEdgeDirection; import org.apache.atlas.repository.Constants; import org.apache.atlas.repository.RepositoryException; @@ -43,31 +46,11 @@ import org.apache.atlas.repository.store.graph.v1.AtlasGraphUtilsV1; import org.apache.atlas.type.AtlasEntityType; import org.apache.atlas.type.AtlasRelationshipType; import org.apache.atlas.type.AtlasType; -import org.apache.atlas.typesystem.IReferenceableInstance; -import org.apache.atlas.typesystem.ITypedInstance; -import org.apache.atlas.typesystem.ITypedReferenceableInstance; -import org.apache.atlas.typesystem.Referenceable; -import org.apache.atlas.typesystem.exception.EntityNotFoundException; -import org.apache.atlas.typesystem.exception.TypeNotFoundException; -import org.apache.atlas.typesystem.json.InstanceSerialization; -import org.apache.atlas.typesystem.persistence.Id; -import org.apache.atlas.typesystem.persistence.ReferenceableInstance; -import org.apache.atlas.typesystem.types.AttributeInfo; -import org.apache.atlas.typesystem.types.ClassType; -import org.apache.atlas.typesystem.types.DataTypes; -import org.apache.atlas.typesystem.types.DataTypes.TypeCategory; -import org.apache.atlas.typesystem.types.HierarchicalType; -import org.apache.atlas.typesystem.types.IDataType; -import org.apache.atlas.typesystem.types.Multiplicity; -import org.apache.atlas.typesystem.types.TypeSystem; -import org.apache.atlas.typesystem.types.ValueConversionException; -import org.apache.atlas.typesystem.types.utils.TypesUtil; +import org.apache.atlas.exception.EntityNotFoundException; import org.apache.atlas.util.AttributeValueMap; import org.apache.atlas.util.IndexedInstance; -import org.apache.atlas.utils.ParamChecker; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang.StringUtils; -import org.codehaus.jettison.json.JSONArray; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -82,7 +65,6 @@ import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Set; -import java.util.Stack; import java.util.UUID; import static org.apache.atlas.type.AtlasStructType.AtlasAttribute.AtlasRelationshipEdgeDirection.BOTH; @@ -97,8 +79,6 @@ public final class GraphHelper { private static final Logger LOG = LoggerFactory.getLogger(GraphHelper.class); public static final String EDGE_LABEL_PREFIX = "__"; - private static final TypeSystem typeSystem = TypeSystem.getInstance(); - public static final String RETRY_COUNT = "atlas.graph.storage.num.retries"; public static final String RETRY_DELAY = "atlas.graph.storage.retry.sleeptime.ms"; @@ -143,7 +123,7 @@ public final class GraphHelper { } - public AtlasVertex createVertexWithIdentity(ITypedReferenceableInstance typedInstance, Set<String> superTypeNames) { + public AtlasVertex createVertexWithIdentity(Referenceable typedInstance, Set<String> superTypeNames) { final String guid = UUID.randomUUID().toString(); final AtlasVertex vertexWithIdentity = createVertexWithoutIdentity(typedInstance.getTypeName(), @@ -153,7 +133,7 @@ public final class GraphHelper { setProperty(vertexWithIdentity, Constants.GUID_PROPERTY_KEY, guid); // add version information - setProperty(vertexWithIdentity, Constants.VERSION_PROPERTY_KEY, Long.valueOf(typedInstance.getId().version)); + setProperty(vertexWithIdentity, Constants.VERSION_PROPERTY_KEY, Long.valueOf(typedInstance.getId().getVersion())); return vertexWithIdentity; } @@ -179,12 +159,12 @@ public final class GraphHelper { setProperty(vertexWithoutIdentity, Constants.STATE_PROPERTY_KEY, Id.EntityState.ACTIVE.name()); // add timestamp information - setProperty(vertexWithoutIdentity, Constants.TIMESTAMP_PROPERTY_KEY, RequestContext.get().getRequestTime()); + setProperty(vertexWithoutIdentity, Constants.TIMESTAMP_PROPERTY_KEY, RequestContextV1.get().getRequestTime()); setProperty(vertexWithoutIdentity, Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY, - RequestContext.get().getRequestTime()); + RequestContextV1.get().getRequestTime()); - setProperty(vertexWithoutIdentity, Constants.CREATED_BY_KEY, RequestContext.get().getUser()); - setProperty(vertexWithoutIdentity, Constants.MODIFIED_BY_KEY, RequestContext.get().getUser()); + setProperty(vertexWithoutIdentity, Constants.CREATED_BY_KEY, RequestContextV1.get().getUser()); + setProperty(vertexWithoutIdentity, Constants.MODIFIED_BY_KEY, RequestContextV1.get().getUser()); return vertexWithoutIdentity; } @@ -196,10 +176,10 @@ public final class GraphHelper { AtlasEdge edge = graph.addEdge(fromVertex, toVertex, edgeLabel); setProperty(edge, Constants.STATE_PROPERTY_KEY, Id.EntityState.ACTIVE.name()); - setProperty(edge, Constants.TIMESTAMP_PROPERTY_KEY, RequestContext.get().getRequestTime()); - setProperty(edge, Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY, RequestContext.get().getRequestTime()); - setProperty(edge, Constants.CREATED_BY_KEY, RequestContext.get().getUser()); - setProperty(edge, Constants.MODIFIED_BY_KEY, RequestContext.get().getUser()); + setProperty(edge, Constants.TIMESTAMP_PROPERTY_KEY, RequestContextV1.get().getRequestTime()); + setProperty(edge, Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY, RequestContextV1.get().getRequestTime()); + setProperty(edge, Constants.CREATED_BY_KEY, RequestContextV1.get().getUser()); + setProperty(edge, Constants.MODIFIED_BY_KEY, RequestContextV1.get().getUser()); if (LOG.isDebugEnabled()) { LOG.debug("Added {}", string(edge)); @@ -673,17 +653,6 @@ public final class GraphHelper { return prefix + "." + key; } - public static String getQualifiedFieldName(ITypedInstance typedInstance, AttributeInfo attributeInfo) throws AtlasException { - IDataType dataType = typeSystem.getDataType(IDataType.class, typedInstance.getTypeName()); - return getQualifiedFieldName(dataType, attributeInfo.name); - } - - public static String getQualifiedFieldName(IDataType dataType, String attributeName) throws AtlasException { - return dataType.getTypeCategory() == DataTypes.TypeCategory.STRUCT ? dataType.getName() + "." + attributeName - // else class or trait - : ((HierarchicalType) dataType).getQualifiedName(attributeName); - } - public static String getTraitLabel(String typeName, String attrName) { return attrName; } @@ -710,13 +679,8 @@ public final class GraphHelper { return superTypes; } - public static String getEdgeLabel(ITypedInstance typedInstance, AttributeInfo aInfo) throws AtlasException { - IDataType dataType = typeSystem.getDataType(IDataType.class, typedInstance.getTypeName()); - return getEdgeLabel(dataType, aInfo); - } - - public static String getEdgeLabel(IDataType dataType, AttributeInfo aInfo) throws AtlasException { - return GraphHelper.EDGE_LABEL_PREFIX + getQualifiedFieldName(dataType, aInfo.name); + public static String getEdgeLabel(AtlasAttribute aInfo) throws AtlasException { + return GraphHelper.EDGE_LABEL_PREFIX + aInfo.getQualifiedName(); } public static Id getIdFromVertex(String dataTypeName, AtlasVertex vertex) { @@ -784,26 +748,24 @@ public final class GraphHelper { * @return * @throws AtlasException */ - public AtlasVertex getVertexForInstanceByUniqueAttribute(ClassType classType, IReferenceableInstance instance) + public AtlasVertex getVertexForInstanceByUniqueAttribute(AtlasEntityType classType, Referenceable instance) throws AtlasException { if (LOG.isDebugEnabled()) { LOG.debug("Checking if there is an instance with the same unique attributes for instance {}", instance.toShortString()); } AtlasVertex result = null; - for (AttributeInfo attributeInfo : classType.fieldMapping().fields.values()) { - if (attributeInfo.isUnique) { - String propertyKey = getQualifiedFieldName(classType, attributeInfo.name); - try { - result = findVertex(propertyKey, instance.get(attributeInfo.name), - Constants.ENTITY_TYPE_PROPERTY_KEY, classType.getName(), - Constants.STATE_PROPERTY_KEY, Id.EntityState.ACTIVE.name()); - if (LOG.isDebugEnabled()) { - LOG.debug("Found vertex by unique attribute : {}={}", propertyKey, instance.get(attributeInfo.name)); - } - } catch (EntityNotFoundException e) { - //Its ok if there is no entity with the same unique value + for (AtlasAttribute attributeInfo : classType.getUniqAttributes().values()) { + String propertyKey = attributeInfo.getQualifiedName(); + try { + result = findVertex(propertyKey, instance.get(attributeInfo.getName()), + Constants.ENTITY_TYPE_PROPERTY_KEY, classType.getTypeName(), + Constants.STATE_PROPERTY_KEY, Id.EntityState.ACTIVE.name()); + if (LOG.isDebugEnabled()) { + LOG.debug("Found vertex by unique attribute : {}={}", propertyKey, instance.get(attributeInfo.getName())); } + } catch (EntityNotFoundException e) { + //Its ok if there is no entity with the same unique value } } @@ -821,23 +783,21 @@ public final class GraphHelper { * @return * @throws AtlasException */ - public List<AtlasVertex> getVerticesForInstancesByUniqueAttribute(ClassType classType, List<? extends IReferenceableInstance> instancesForClass) throws AtlasException { + public List<AtlasVertex> getVerticesForInstancesByUniqueAttribute(AtlasEntityType classType, List<? extends Referenceable> instancesForClass) throws AtlasException { //For each attribute, need to figure out what values to search for and which instance(s) //those values correspond to. Map<String, AttributeValueMap> map = new HashMap<String, AttributeValueMap>(); - for (AttributeInfo attributeInfo : classType.fieldMapping().fields.values()) { - if (attributeInfo.isUnique) { - String propertyKey = getQualifiedFieldName(classType, attributeInfo.name); - AttributeValueMap mapForAttribute = new AttributeValueMap(); - for(int idx = 0; idx < instancesForClass.size(); idx++) { - IReferenceableInstance instance = instancesForClass.get(idx); - Object value = instance.get(attributeInfo.name); - mapForAttribute.put(value, instance, idx); - } - map.put(propertyKey, mapForAttribute); + for (AtlasAttribute attributeInfo : classType.getUniqAttributes().values()) { + String propertyKey = attributeInfo.getQualifiedName(); + AttributeValueMap mapForAttribute = new AttributeValueMap(); + for(int idx = 0; idx < instancesForClass.size(); idx++) { + Referenceable instance = instancesForClass.get(idx); + Object value = instance.get(attributeInfo.getName()); + mapForAttribute.put(value, instance, idx); } + map.put(propertyKey, mapForAttribute); } AtlasVertex[] result = new AtlasVertex[instancesForClass.size()]; @@ -849,7 +809,7 @@ public final class GraphHelper { //construct gremlin query AtlasGraphQuery query = graph.query(); - query.has(Constants.ENTITY_TYPE_PROPERTY_KEY, classType.getName()); + query.has(Constants.ENTITY_TYPE_PROPERTY_KEY, classType.getTypeName()); query.has(Constants.STATE_PROPERTY_KEY,Id.EntityState.ACTIVE.name()); List<AtlasGraphQuery> orChildren = new ArrayList<AtlasGraphQuery>(); @@ -965,6 +925,7 @@ public final class GraphHelper { * @return set of VertexInfo for all composite entities * @throws AtlasException */ + /* public Set<VertexInfo> getCompositeVertices(AtlasVertex entityVertex) throws AtlasException { Set<VertexInfo> result = new HashSet<>(); Stack<AtlasVertex> vertices = new Stack<>(); @@ -1035,20 +996,22 @@ public final class GraphHelper { } return result; } + */ - public static ITypedReferenceableInstance[] deserializeClassInstances(TypeSystem typeSystem, String entityInstanceDefinition) + /* + public static Referenceable[] deserializeClassInstances(AtlasTypeRegistry typeRegistry, String entityInstanceDefinition) throws AtlasException { try { JSONArray referableInstances = new JSONArray(entityInstanceDefinition); - ITypedReferenceableInstance[] instances = new ITypedReferenceableInstance[referableInstances.length()]; + Referenceable[] instances = new Referenceable[referableInstances.length()]; for (int index = 0; index < referableInstances.length(); index++) { Referenceable entityInstance = - InstanceSerialization.fromJsonReferenceable(referableInstances.getString(index), true); - ITypedReferenceableInstance typedInstrance = getTypedReferenceableInstance(typeSystem, entityInstance); + AtlasType.fromV1Json(referableInstances.getString(index), Referenceable.class); + Referenceable typedInstrance = getTypedReferenceableInstance(typeRegistry, entityInstance); instances[index] = typedInstrance; } return instances; - } catch(ValueConversionException | TypeNotFoundException e) { + } catch(TypeNotFoundException e) { throw e; } catch (Exception e) { // exception from deserializer LOG.error("Unable to deserialize json={}", entityInstanceDefinition, e); @@ -1056,28 +1019,22 @@ public final class GraphHelper { } } - public static ITypedReferenceableInstance getTypedReferenceableInstance(TypeSystem typeSystem, Referenceable entityInstance) + public static Referenceable getTypedReferenceableInstance(AtlasTypeRegistry typeRegistry, Referenceable entityInstance) throws AtlasException { final String entityTypeName = ParamChecker.notEmpty(entityInstance.getTypeName(), "Entity type cannot be null"); - ClassType entityType = typeSystem.getDataType(ClassType.class, entityTypeName); + AtlasEntityType entityType = typeRegistry.getEntityTypeByName(entityTypeName); //Both assigned id and values are required for full update //classtype.convert() will remove values if id is assigned. So, set temp id, convert and // then replace with original id Id origId = entityInstance.getId(); - entityInstance.replaceWithNewId(new Id(entityInstance.getTypeName())); - ITypedReferenceableInstance typedInstrance = entityType.convert(entityInstance, Multiplicity.REQUIRED); - ((ReferenceableInstance)typedInstrance).replaceWithNewId(origId); + entityInstance.setId(new Id(entityInstance.getTypeName())); + Referenceable typedInstrance = new Referenceable(entityInstance); + typedInstrance.setId(origId); return typedInstrance; } - - public static boolean isReference(IDataType type) { - - return type.getTypeCategory() == DataTypes.TypeCategory.STRUCT || - type.getTypeCategory() == DataTypes.TypeCategory.CLASS; - - } + */ public static boolean isInternalType(AtlasVertex vertex) { return vertex != null && isInternalType(getTypeName(vertex)); @@ -1087,9 +1044,9 @@ public final class GraphHelper { return typeName != null && typeName.startsWith(Constants.INTERNAL_PROPERTY_KEY_PREFIX); } - public static void setArrayElementsProperty(IDataType elementType, AtlasVertex instanceVertex, String propertyName, List<Object> values) { + public static void setArrayElementsProperty(AtlasType elementType, AtlasVertex instanceVertex, String propertyName, List<Object> values) { String actualPropertyName = GraphHelper.encodePropertyKey(propertyName); - if(GraphHelper.isReference(elementType)) { + if(AtlasGraphUtilsV1.isReference(elementType)) { setListPropertyFromElementIds(instanceVertex, actualPropertyName, (List)values); } else { @@ -1097,9 +1054,9 @@ public final class GraphHelper { } } - public static void setMapValueProperty(IDataType elementType, AtlasVertex instanceVertex, String propertyName, Object value) { + public static void setMapValueProperty(AtlasType elementType, AtlasVertex instanceVertex, String propertyName, Object value) { String actualPropertyName = GraphHelper.encodePropertyKey(propertyName); - if(GraphHelper.isReference(elementType)) { + if(AtlasGraphUtilsV1.isReference(elementType)) { instanceVertex.setPropertyFromElementId(actualPropertyName, (AtlasEdge)value); } else { @@ -1107,16 +1064,6 @@ public final class GraphHelper { } } - public static Object getMapValueProperty(IDataType elementType, AtlasVertex instanceVertex, String propertyName) { - String actualPropertyName = GraphHelper.encodePropertyKey(propertyName); - if(GraphHelper.isReference(elementType)) { - return instanceVertex.getProperty(actualPropertyName, AtlasEdge.class); - } - else { - return instanceVertex.getProperty(actualPropertyName, Object.class).toString(); - } - } - public static Object getMapValueProperty(AtlasType elementType, AtlasVertex instanceVertex, String propertyName) { String vertexPropertyName = GraphHelper.encodePropertyKey(propertyName); @@ -1138,16 +1085,6 @@ public final class GraphHelper { } } - public static List<Object> getArrayElementsProperty(IDataType elementType, AtlasVertex instanceVertex, String propertyName) { - String actualPropertyName = GraphHelper.encodePropertyKey(propertyName); - if(GraphHelper.isReference(elementType)) { - return (List)instanceVertex.getListProperty(actualPropertyName, AtlasEdge.class); - } - else { - return (List)instanceVertex.getListProperty(actualPropertyName); - } - } - public static void dumpToLog(final AtlasGraph<?,?> graph) { LOG.debug("*******************Graph Dump****************************"); LOG.debug("Vertices of {}", graph); @@ -1162,7 +1099,7 @@ public final class GraphHelper { LOG.debug("*******************Graph Dump****************************"); } - public static String string(ITypedReferenceableInstance instance) { + public static String string(Referenceable instance) { return String.format("entity[type=%s guid=%s]", instance.getTypeName(), instance.getId()._getId()); } @@ -1245,6 +1182,7 @@ public final class GraphHelper { return instanceVertexId; } + /* public static AttributeInfo getAttributeInfoForSystemAttributes(String field) { switch (field) { case Constants.STATE_PROPERTY_KEY: @@ -1259,6 +1197,7 @@ public final class GraphHelper { } return null; } + */ public static boolean elementExists(AtlasElement v) { return v != null && v.exists();
http://git-wip-us.apache.org/repos/asf/atlas/blob/435fe3fb/repository/src/main/java/org/apache/atlas/repository/graph/GraphSchemaInitializer.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/graph/GraphSchemaInitializer.java b/repository/src/main/java/org/apache/atlas/repository/graph/GraphSchemaInitializer.java deleted file mode 100644 index e877680..0000000 --- a/repository/src/main/java/org/apache/atlas/repository/graph/GraphSchemaInitializer.java +++ /dev/null @@ -1,50 +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.setup.SetupException; -import org.apache.atlas.setup.SetupStep; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Component; - -/** - * A {@link SetupStep} that initializes the Graph backend for Atlas. - * - * This class will initialize the specific backend implementation specified in - * the Atlas configuration for the key atlas.graph.storage.backend. - */ -@Component -public class GraphSchemaInitializer implements SetupStep { - - private static final Logger LOG = LoggerFactory.getLogger(GraphSchemaInitializer.class); - - @Override - public void run() throws SetupException { - LOG.info("Initializing graph schema backend."); - try { - // The implementation of this method internally creates the schema. - AtlasGraphProvider.getGraphInstance(); - LOG.info("Completed initializing graph schema backend."); - } catch (Exception e) { - LOG.error("Could not initialize graph schema backend due to exception, {}", e.getMessage(), e); - throw new SetupException("Could not initialize graph schema due to exception", e); - } - } -} http://git-wip-us.apache.org/repos/asf/atlas/blob/435fe3fb/repository/src/main/java/org/apache/atlas/repository/graph/GraphToTypedInstanceMapper.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/graph/GraphToTypedInstanceMapper.java b/repository/src/main/java/org/apache/atlas/repository/graph/GraphToTypedInstanceMapper.java deleted file mode 100644 index d7a8fa9..0000000 --- a/repository/src/main/java/org/apache/atlas/repository/graph/GraphToTypedInstanceMapper.java +++ /dev/null @@ -1,452 +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.Constants; -import org.apache.atlas.repository.RepositoryException; -import org.apache.atlas.repository.graphdb.AtlasEdge; -import org.apache.atlas.repository.graphdb.AtlasEdgeDirection; -import org.apache.atlas.repository.graphdb.AtlasGraph; -import org.apache.atlas.repository.graphdb.AtlasVertex; -import org.apache.atlas.typesystem.ITypedInstance; -import org.apache.atlas.typesystem.ITypedReferenceableInstance; -import org.apache.atlas.typesystem.ITypedStruct; -import org.apache.atlas.typesystem.persistence.AtlasSystemAttributes; -import org.apache.atlas.typesystem.persistence.Id; -import org.apache.atlas.typesystem.types.AttributeInfo; -import org.apache.atlas.typesystem.types.ClassType; -import org.apache.atlas.typesystem.types.DataTypes; -import org.apache.atlas.typesystem.types.IDataType; -import org.apache.atlas.typesystem.types.Multiplicity; -import org.apache.atlas.typesystem.types.StructType; -import org.apache.atlas.typesystem.types.TraitType; -import org.apache.atlas.typesystem.types.TypeSystem; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Component; - -import javax.inject.Inject; -import java.math.BigDecimal; -import java.math.BigInteger; -import java.util.ArrayList; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import static org.apache.atlas.repository.graph.GraphHelper.string; - -@Component -@Deprecated -public final class GraphToTypedInstanceMapper { - - private static final Logger LOG = LoggerFactory.getLogger(GraphToTypedInstanceMapper.class); - private static TypeSystem typeSystem = TypeSystem.getInstance(); - private static final GraphHelper graphHelper = GraphHelper.getInstance(); - - private final AtlasGraph atlasGraph; - - @Inject - public GraphToTypedInstanceMapper(AtlasGraph atlasGraph) { - this.atlasGraph = atlasGraph; - } - - public ITypedReferenceableInstance mapGraphToTypedInstance(String guid, AtlasVertex instanceVertex) - throws AtlasException { - - if(LOG.isDebugEnabled()) { - //We don't do a cache check here since we want that to be at a higher level - //where the vertex lookup can also be avoided. However, this is a convenient - //place to add a check to see if there are any places that were missed. - if(RequestContext.get().getInstanceV1(guid) != null) { - LOG.warn("Looking up previously cached guid at: ", new Exception()); - } - - LOG.debug("Mapping graph root vertex {} to typed instance for guid {}", instanceVertex, guid); - } - - String typeName = GraphHelper.getSingleValuedProperty(instanceVertex, Constants.ENTITY_TYPE_PROPERTY_KEY, String.class); - List<String> traits = GraphHelper.getTraitNames(instanceVertex); - String state = GraphHelper.getStateAsString(instanceVertex); - String createdBy = GraphHelper.getCreatedByAsString(instanceVertex); - String modifiedBy = GraphHelper.getModifiedByAsString(instanceVertex); - Date createdTime = new Date(GraphHelper.getCreatedTime(instanceVertex)); - Date modifiedTime = new Date(GraphHelper.getModifiedTime(instanceVertex)); - AtlasSystemAttributes systemAttributes = new AtlasSystemAttributes(createdBy, modifiedBy, createdTime, modifiedTime); - - if (LOG.isDebugEnabled()) { - LOG.debug("Found createdBy : {} modifiedBy : {} createdTime: {} modifedTime: {}", createdBy, modifiedBy, createdTime, modifiedTime); - } - - Id id = new Id(guid, Integer.parseInt(String.valueOf(GraphHelper.getProperty(instanceVertex, Constants.VERSION_PROPERTY_KEY))), typeName, state); - - if (LOG.isDebugEnabled()) { - LOG.debug("Created id {} for instance type {}", id, typeName); - } - - ClassType classType = typeSystem.getDataType(ClassType.class, typeName); - ITypedReferenceableInstance typedInstance = - classType.createInstance(id, systemAttributes, traits.toArray(new String[traits.size()])); - - mapVertexToInstance(instanceVertex, typedInstance, classType.fieldMapping().fields); - mapVertexToInstanceTraits(instanceVertex, typedInstance, traits); - RequestContext.get().cache(typedInstance); - return typedInstance; - } - - private void mapVertexToInstanceTraits(AtlasVertex instanceVertex, ITypedReferenceableInstance typedInstance, - List<String> traits) throws AtlasException { - for (String traitName : traits) { - if (LOG.isDebugEnabled()) { - LOG.debug("mapping trait {} to instance", traitName); - } - - TraitType traitType = typeSystem.getDataType(TraitType.class, traitName); - mapVertexToTraitInstance(instanceVertex, typedInstance, traitName, traitType); - } - } - - public void mapVertexToInstance(AtlasVertex instanceVertex, ITypedInstance typedInstance, - Map<String, AttributeInfo> fields) throws AtlasException { - - if (LOG.isDebugEnabled()) { - LOG.debug("Mapping vertex {} to instance {} for fields", instanceVertex, typedInstance.getTypeName(), - fields); - } - - for (AttributeInfo attributeInfo : fields.values()) { - mapVertexToAttribute(instanceVertex, typedInstance, attributeInfo); - } - } - - private void mapVertexToAttribute(AtlasVertex instanceVertex, ITypedInstance typedInstance, - AttributeInfo attributeInfo) throws AtlasException { - - if (LOG.isDebugEnabled()) { - LOG.debug("Mapping attributeInfo {}", attributeInfo.name); - } - - final IDataType dataType = attributeInfo.dataType(); - final String vertexPropertyName = GraphHelper.getQualifiedFieldName(typedInstance, attributeInfo); - String relationshipLabel = GraphHelper.getEdgeLabel(typedInstance, attributeInfo); - - switch (dataType.getTypeCategory()) { - case PRIMITIVE: - mapVertexToPrimitive(instanceVertex, typedInstance, attributeInfo); - break; // add only if vertex has this attribute - - case ENUM: - Object propertyValue = GraphHelper.getProperty(instanceVertex, vertexPropertyName); - if (propertyValue == null) { - return; - } - - typedInstance.set(attributeInfo.name, dataType.convert(propertyValue, Multiplicity.REQUIRED)); - break; - - case ARRAY: - mapVertexToArrayInstance(instanceVertex, typedInstance, attributeInfo, vertexPropertyName); - break; - - case MAP: - mapVertexToMapInstance(instanceVertex, typedInstance, attributeInfo, vertexPropertyName); - break; - - case STRUCT: - ITypedStruct structInstance = mapVertexToStructInstance(instanceVertex, - (StructType) attributeInfo.dataType(), relationshipLabel, null); - typedInstance.set(attributeInfo.name, structInstance); - break; - - case TRAIT: - // do NOTHING - handled in class - break; - - case CLASS: - AtlasEdge nullEdge = null; - Object idOrInstance = mapVertexToClassReference(instanceVertex, attributeInfo, relationshipLabel, - attributeInfo.dataType(), nullEdge); - if (idOrInstance != null) { - typedInstance.set(attributeInfo.name, idOrInstance); - } - break; - - default: - break; - } - } - - private Object mapVertexToClassReference(AtlasVertex instanceVertex, AttributeInfo attributeInfo, - String relationshipLabel, IDataType dataType, AtlasEdge optionalEdge) throws AtlasException { - if (LOG.isDebugEnabled()) { - LOG.debug("Finding edge for {} -> label {} ", instanceVertex, relationshipLabel); - } - - AtlasEdge edge = null; - if (optionalEdge == null) { - edge = graphHelper.getEdgeForLabel(instanceVertex, relationshipLabel); - } else { - edge = optionalEdge; - } - - if (GraphHelper.elementExists(edge)) { - final AtlasVertex referenceVertex = edge.getInVertex(); - final String guid = GraphHelper.getSingleValuedProperty(referenceVertex, Constants.GUID_PROPERTY_KEY, String.class); - - if (LOG.isDebugEnabled()) { - LOG.debug("Found vertex {} for label {} with guid {}", referenceVertex, relationshipLabel, guid); - } - - if (attributeInfo.isComposite) { - //Also, when you retrieve a type's instance, you get the complete object graph of the composites - LOG.debug("Found composite, mapping vertex to instance"); - ITypedReferenceableInstance cached = RequestContext.get().getInstanceV1(guid); - if(cached != null) { - return cached; - } - return mapGraphToTypedInstance(guid, referenceVertex); - } else { - String state = GraphHelper.getStateAsString(referenceVertex); - Id referenceId = new Id(guid, GraphHelper.getVersion(referenceVertex).intValue(), dataType.getName(), state); - - if (LOG.isDebugEnabled()) { - LOG.debug("Found non-composite, adding id {} ", referenceId); - } - - return referenceId; - } - } - - return null; - } - - @SuppressWarnings("unchecked") - private void mapVertexToArrayInstance(AtlasVertex<?,?> instanceVertex, ITypedInstance typedInstance, - AttributeInfo attributeInfo, String propertyName) throws AtlasException { - if (LOG.isDebugEnabled()) { - LOG.debug("mapping vertex {} to array {}", instanceVertex, attributeInfo.name); - } - - final DataTypes.ArrayType arrayType = (DataTypes.ArrayType) attributeInfo.dataType(); - final IDataType elementType = arrayType.getElemType(); - - List<Object> list = GraphHelper.getArrayElementsProperty(elementType, instanceVertex, propertyName); - - if (list == null || list.size() == 0) { - return; - } - - String edgeLabel = GraphHelper.EDGE_LABEL_PREFIX + propertyName; - ArrayList values = new ArrayList(); - for (Object aList : list) { - values.add(mapVertexToCollectionEntry(instanceVertex, attributeInfo, elementType, aList, - edgeLabel)); - } - - if (values.size() > 0) { - typedInstance.set(attributeInfo.name, values); - } - } - - private Object mapVertexToCollectionEntry(AtlasVertex instanceVertex, AttributeInfo attributeInfo, - IDataType elementType, Object value, String edgeLabel) throws AtlasException { - switch (elementType.getTypeCategory()) { - case PRIMITIVE: - case ENUM: - return value; - - case ARRAY: - case MAP: - case TRAIT: - // do nothing - break; - - case STRUCT: - return mapVertexToStructInstance(instanceVertex, (StructType) elementType, edgeLabel, (AtlasEdge) value); - - case CLASS: - return mapVertexToClassReference(instanceVertex, attributeInfo, edgeLabel, elementType, (AtlasEdge) value); - - default: - break; - } - - throw new IllegalArgumentException(); - } - - @SuppressWarnings("unchecked") - private void mapVertexToMapInstance(AtlasVertex<?,?> instanceVertex, ITypedInstance typedInstance, - AttributeInfo attributeInfo, final String propertyName) throws AtlasException { - if (LOG.isDebugEnabled()) { - LOG.debug("mapping vertex {} to array {}", instanceVertex, attributeInfo.name); - } - - List<String> keys = GraphHelper.getListProperty(instanceVertex, propertyName); - if (keys == null || keys.size() == 0) { - return; - } - DataTypes.MapType mapType = (DataTypes.MapType) attributeInfo.dataType(); - final IDataType valueType = mapType.getValueType(); - - HashMap<String,Object> values = new HashMap<>(); - for (String key : keys) { - final String keyPropertyName = propertyName + "." + key; - final String edgeLabel = GraphHelper.EDGE_LABEL_PREFIX + keyPropertyName; - final Object keyValue = GraphHelper.getMapValueProperty(valueType, instanceVertex, keyPropertyName); - Object mapValue = mapVertexToCollectionEntry(instanceVertex, attributeInfo, valueType, keyValue, edgeLabel); - if (mapValue != null) { - values.put(key, mapValue); - } - } - - if (!values.isEmpty()) { - typedInstance.set(attributeInfo.name, values); - } - } - - private ITypedStruct mapVertexToStructInstance(AtlasVertex instanceVertex, StructType structType, - String relationshipLabel, AtlasEdge optionalEdge) throws AtlasException { - if (LOG.isDebugEnabled()) { - LOG.debug("mapping {} to struct {}", string(instanceVertex), relationshipLabel); - } - - ITypedStruct structInstance = null; - - AtlasEdge edge; - if (optionalEdge == null) { - edge = graphHelper.getEdgeForLabel(instanceVertex, relationshipLabel); - } else { - edge = optionalEdge; - } - - if (GraphHelper.elementExists(edge)) { - structInstance = structType.createInstance(); - AtlasVertex structInstanceVertex = edge.getInVertex(); - if (LOG.isDebugEnabled()) { - LOG.debug("Found struct instance {}, mapping to instance {} ", string(structInstanceVertex), - structInstance.getTypeName()); - } - - mapVertexToInstance(structInstanceVertex, structInstance, structType.fieldMapping().fields); - } - return structInstance; - } - - private void mapVertexToTraitInstance(AtlasVertex instanceVertex, ITypedReferenceableInstance typedInstance, - String traitName, TraitType traitType) throws AtlasException { - ITypedStruct traitInstance = (ITypedStruct) typedInstance.getTrait(traitName); - - mapVertexToTraitInstance(instanceVertex, typedInstance.getTypeName(), traitName, traitType, traitInstance); - } - - private void mapVertexToTraitInstance(AtlasVertex<?,?> instanceVertex, String typedInstanceTypeName, String traitName, - TraitType traitType, ITypedStruct traitInstance) throws AtlasException { - String relationshipLabel = GraphHelper.getTraitLabel(typedInstanceTypeName, traitName); - - if (LOG.isDebugEnabled()) { - LOG.debug("Finding edge for {} -> label {} ", instanceVertex, relationshipLabel); - } - - for (AtlasEdge<?,?> edge : instanceVertex.getEdges(AtlasEdgeDirection.OUT, relationshipLabel)) { - final AtlasVertex<?,?> traitInstanceVertex = edge.getInVertex(); - if (traitInstanceVertex != null) { - if (LOG.isDebugEnabled()) { - LOG.debug("Found trait instance vertex {}, mapping to instance {} ", traitInstanceVertex, - traitInstance.getTypeName()); - } - - mapVertexToInstance(traitInstanceVertex, traitInstance, traitType.fieldMapping().fields); - break; - } - } - } - - private void mapVertexToPrimitive(AtlasVertex<?,?> instanceVertex, ITypedInstance typedInstance, - AttributeInfo attributeInfo) throws AtlasException { - if (LOG.isDebugEnabled()) { - LOG.debug("Adding primitive {} from vertex {}", attributeInfo, instanceVertex); - } - - final String vertexPropertyName = GraphHelper.getQualifiedFieldName(typedInstance, attributeInfo); - if (GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, Object.class) == null) { - return; - } - - if (attributeInfo.dataType() == DataTypes.STRING_TYPE) { - typedInstance.setString(attributeInfo.name, GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, String.class)); - } else if (attributeInfo.dataType() == DataTypes.SHORT_TYPE) { - typedInstance.setShort(attributeInfo.name, GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, Short.class)); - } else if (attributeInfo.dataType() == DataTypes.INT_TYPE) { - typedInstance.setInt(attributeInfo.name, GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, Integer.class)); - } else if (attributeInfo.dataType() == DataTypes.BIGINTEGER_TYPE) { - typedInstance.setBigInt(attributeInfo.name, GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, BigInteger.class)); - } else if (attributeInfo.dataType() == DataTypes.BOOLEAN_TYPE) { - typedInstance.setBoolean(attributeInfo.name, GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, Boolean.class)); - } else if (attributeInfo.dataType() == DataTypes.BYTE_TYPE) { - typedInstance.setByte(attributeInfo.name, GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, Byte.class)); - } else if (attributeInfo.dataType() == DataTypes.LONG_TYPE) { - typedInstance.setLong(attributeInfo.name, GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, Long.class)); - } else if (attributeInfo.dataType() == DataTypes.FLOAT_TYPE) { - typedInstance.setFloat(attributeInfo.name, GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, Float.class)); - } else if (attributeInfo.dataType() == DataTypes.DOUBLE_TYPE) { - typedInstance.setDouble(attributeInfo.name, GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, Double.class)); - } else if (attributeInfo.dataType() == DataTypes.BIGDECIMAL_TYPE) { - typedInstance - .setBigDecimal(attributeInfo.name, GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, BigDecimal.class)); - } else if (attributeInfo.dataType() == DataTypes.DATE_TYPE) { - final Long dateVal = GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, Long.class); - typedInstance.setDate(attributeInfo.name, new Date(dateVal)); - } - } - - - public ITypedInstance getReferredEntity(String edgeId, IDataType<?> referredType) throws AtlasException { - final AtlasEdge edge = getGraph().getEdge(edgeId); - if (edge != null) { - final AtlasVertex referredVertex = edge.getInVertex(); - if (referredVertex != null) { - switch (referredType.getTypeCategory()) { - case STRUCT: - if (LOG.isDebugEnabled()) { - LOG.debug("Found struct instance vertex {}, mapping to instance {} ", referredVertex, - referredType.getName()); - } - - StructType structType = (StructType) referredType; - ITypedStruct instance = structType.createInstance(); - Map<String, AttributeInfo> fields = structType.fieldMapping().fields; - mapVertexToInstance(referredVertex, instance, fields); - return instance; - case CLASS: - //TODO isComposite handling for class loads - return GraphHelper.getIdFromVertex(referredType.getName(), referredVertex); - default: - throw new UnsupportedOperationException("Loading " + referredType.getTypeCategory() + " is not supported"); - } - } - } - return null; - } - - private AtlasGraph getGraph() throws RepositoryException { - return atlasGraph; - } -} - http://git-wip-us.apache.org/repos/asf/atlas/blob/435fe3fb/repository/src/main/java/org/apache/atlas/repository/graph/HardDeleteHandler.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/graph/HardDeleteHandler.java b/repository/src/main/java/org/apache/atlas/repository/graph/HardDeleteHandler.java deleted file mode 100644 index e00ef96..0000000 --- a/repository/src/main/java/org/apache/atlas/repository/graph/HardDeleteHandler.java +++ /dev/null @@ -1,48 +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 org.apache.atlas.AtlasException; -import org.apache.atlas.annotation.ConditionalOnAtlasProperty; -import org.apache.atlas.repository.graphdb.AtlasEdge; -import org.apache.atlas.repository.graphdb.AtlasVertex; -import org.apache.atlas.typesystem.types.TypeSystem; -import org.springframework.stereotype.Component; - -import javax.inject.Inject; - -@Component -@ConditionalOnAtlasProperty(property = "atlas.DeleteHandler.impl") -public class HardDeleteHandler extends DeleteHandler { - - @Inject - public HardDeleteHandler(TypeSystem typeSystem) { - super(typeSystem, true, false); - } - - @Override - protected void _deleteVertex(AtlasVertex instanceVertex, boolean force) { - graphHelper.removeVertex(instanceVertex); - } - - @Override - protected void deleteEdge(AtlasEdge edge, boolean force) throws AtlasException { - graphHelper.removeEdge(edge); - } -} http://git-wip-us.apache.org/repos/asf/atlas/blob/435fe3fb/repository/src/main/java/org/apache/atlas/repository/graph/SoftDeleteHandler.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/graph/SoftDeleteHandler.java b/repository/src/main/java/org/apache/atlas/repository/graph/SoftDeleteHandler.java deleted file mode 100644 index b37fe75..0000000 --- a/repository/src/main/java/org/apache/atlas/repository/graph/SoftDeleteHandler.java +++ /dev/null @@ -1,74 +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 org.apache.atlas.AtlasException; -import org.apache.atlas.RequestContext; -import org.apache.atlas.annotation.ConditionalOnAtlasProperty; -import org.apache.atlas.repository.graphdb.AtlasEdge; -import org.apache.atlas.repository.graphdb.AtlasVertex; -import org.apache.atlas.typesystem.persistence.Id; -import org.apache.atlas.typesystem.types.TypeSystem; -import org.springframework.stereotype.Component; - -import javax.inject.Inject; - -import static org.apache.atlas.repository.Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY; -import static org.apache.atlas.repository.Constants.MODIFIED_BY_KEY; -import static org.apache.atlas.repository.Constants.STATE_PROPERTY_KEY; - -@Component -@ConditionalOnAtlasProperty(property = "atlas.DeleteHandler.impl", isDefault = true) -public class SoftDeleteHandler extends DeleteHandler { - - @Inject - public SoftDeleteHandler(TypeSystem typeSystem) { - super(typeSystem, false, true); - } - - @Override - protected void _deleteVertex(AtlasVertex instanceVertex, boolean force) { - if (force) { - graphHelper.removeVertex(instanceVertex); - } else { - Id.EntityState state = GraphHelper.getState(instanceVertex); - if (state != Id.EntityState.DELETED) { - GraphHelper.setProperty(instanceVertex, STATE_PROPERTY_KEY, Id.EntityState.DELETED.name()); - GraphHelper.setProperty(instanceVertex, MODIFICATION_TIMESTAMP_PROPERTY_KEY, - RequestContext.get().getRequestTime()); - GraphHelper.setProperty(instanceVertex, MODIFIED_BY_KEY, RequestContext.get().getUser()); - } - } - } - - @Override - protected void deleteEdge(AtlasEdge edge, boolean force) throws AtlasException { - if (force) { - graphHelper.removeEdge(edge); - } else { - Id.EntityState state = GraphHelper.getState(edge); - if (state != Id.EntityState.DELETED) { - GraphHelper.setProperty(edge, STATE_PROPERTY_KEY, Id.EntityState.DELETED.name()); - GraphHelper - .setProperty(edge, MODIFICATION_TIMESTAMP_PROPERTY_KEY, RequestContext.get().getRequestTime()); - GraphHelper.setProperty(edge, MODIFIED_BY_KEY, RequestContext.get().getUser()); - } - } - } -}