Search in sources :

Example 1 with AtlasEdge

use of org.apache.atlas.repository.graphdb.AtlasEdge in project incubator-atlas by apache.

the class GraphHelper method getCompositeVertices.

/**
     * Get the GUIDs and vertices for all composite entities owned/contained by the specified root entity AtlasVertex.
     * The graph is traversed from the root entity through to the leaf nodes of the containment graph.
     *
     * @param entityVertex the root entity vertex
     * @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<>();
    vertices.push(entityVertex);
    while (vertices.size() > 0) {
        AtlasVertex vertex = vertices.pop();
        String typeName = GraphHelper.getTypeName(vertex);
        String guid = GraphHelper.getGuid(vertex);
        Id.EntityState state = GraphHelper.getState(vertex);
        if (state == Id.EntityState.DELETED) {
            //If the reference vertex is marked for deletion, skip it
            continue;
        }
        result.add(new VertexInfo(guid, vertex, typeName));
        ClassType classType = typeSystem.getDataType(ClassType.class, typeName);
        for (AttributeInfo attributeInfo : classType.fieldMapping().fields.values()) {
            if (!attributeInfo.isComposite) {
                continue;
            }
            String edgeLabel = GraphHelper.getEdgeLabel(classType, attributeInfo);
            switch(attributeInfo.dataType().getTypeCategory()) {
                case CLASS:
                    AtlasEdge edge = getEdgeForLabel(vertex, edgeLabel);
                    if (edge != null && GraphHelper.getState(edge) == Id.EntityState.ACTIVE) {
                        AtlasVertex compositeVertex = edge.getInVertex();
                        vertices.push(compositeVertex);
                    }
                    break;
                case ARRAY:
                    IDataType elementType = ((DataTypes.ArrayType) attributeInfo.dataType()).getElemType();
                    DataTypes.TypeCategory elementTypeCategory = elementType.getTypeCategory();
                    if (elementTypeCategory != TypeCategory.CLASS) {
                        continue;
                    }
                    Iterator<AtlasEdge> edges = getOutGoingEdgesByLabel(vertex, edgeLabel);
                    if (edges != null) {
                        while (edges.hasNext()) {
                            edge = edges.next();
                            if (edge != null && GraphHelper.getState(edge) == Id.EntityState.ACTIVE) {
                                AtlasVertex compositeVertex = edge.getInVertex();
                                vertices.push(compositeVertex);
                            }
                        }
                    }
                    break;
                case MAP:
                    DataTypes.MapType mapType = (DataTypes.MapType) attributeInfo.dataType();
                    DataTypes.TypeCategory valueTypeCategory = mapType.getValueType().getTypeCategory();
                    if (valueTypeCategory != TypeCategory.CLASS) {
                        continue;
                    }
                    String propertyName = GraphHelper.getQualifiedFieldName(classType, attributeInfo.name);
                    List<String> keys = vertex.getProperty(propertyName, List.class);
                    if (keys != null) {
                        for (String key : keys) {
                            String mapEdgeLabel = GraphHelper.getQualifiedNameForMapKey(edgeLabel, key);
                            edge = getEdgeForLabel(vertex, mapEdgeLabel);
                            if (edge != null && GraphHelper.getState(edge) == Id.EntityState.ACTIVE) {
                                AtlasVertex compositeVertex = edge.getInVertex();
                                vertices.push(compositeVertex);
                            }
                        }
                    }
                    break;
                default:
            }
        }
    }
    return result;
}
Also used : TypeCategory(org.apache.atlas.typesystem.types.DataTypes.TypeCategory) ClassType(org.apache.atlas.typesystem.types.ClassType) IDataType(org.apache.atlas.typesystem.types.IDataType) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) Stack(java.util.Stack) AttributeInfo(org.apache.atlas.typesystem.types.AttributeInfo) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) Id(org.apache.atlas.typesystem.persistence.Id) HashSet(java.util.HashSet) DataTypes(org.apache.atlas.typesystem.types.DataTypes)

Example 2 with AtlasEdge

use of org.apache.atlas.repository.graphdb.AtlasEdge in project incubator-atlas by apache.

the class GraphToTypedInstanceMapper method getReferredEntity.

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;
}
Also used : AttributeInfo(org.apache.atlas.typesystem.types.AttributeInfo) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) StructType(org.apache.atlas.typesystem.types.StructType) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Example 3 with AtlasEdge

use of org.apache.atlas.repository.graphdb.AtlasEdge in project incubator-atlas by apache.

the class GraphToTypedInstanceMapper method mapVertexToClassReference.

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.getSingleValuedProperty(referenceVertex, Constants.VERSION_PROPERTY_KEY, Integer.class), dataType.getName(), state);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Found non-composite, adding id {} ", referenceId);
            }
            return referenceId;
        }
    }
    return null;
}
Also used : BigInteger(java.math.BigInteger) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) Id(org.apache.atlas.typesystem.persistence.Id) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Example 4 with AtlasEdge

use of org.apache.atlas.repository.graphdb.AtlasEdge in project incubator-atlas by apache.

the class GraphToTypedInstanceMapper method mapVertexToAttribute.

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);
            // add only if vertex has this attribute
            break;
        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;
    }
}
Also used : IDataType(org.apache.atlas.typesystem.types.IDataType) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Example 5 with AtlasEdge

use of org.apache.atlas.repository.graphdb.AtlasEdge in project incubator-atlas by apache.

the class TypedInstanceToGraphMapper method removeUnusedEntries.

//Removes unused edges from the old collection, compared to the new collection
private List<AtlasEdge> removeUnusedEntries(AtlasVertex instanceVertex, String edgeLabel, Collection<AtlasEdge> currentEntries, Collection<AtlasEdge> newEntries, IDataType entryType, AttributeInfo attributeInfo) throws AtlasException {
    if (currentEntries != null && !currentEntries.isEmpty()) {
        LOG.debug("Removing unused entries from the old collection");
        if (entryType.getTypeCategory() == TypeCategory.STRUCT || entryType.getTypeCategory() == TypeCategory.CLASS) {
            //Remove the edges for (current edges - new edges)
            List<AtlasEdge> cloneElements = new ArrayList<>(currentEntries);
            cloneElements.removeAll(newEntries);
            List<AtlasEdge> additionalElements = new ArrayList<>();
            if (LOG.isDebugEnabled()) {
                LOG.debug("Removing unused entries from the old collection - {}", cloneElements);
            }
            if (!cloneElements.isEmpty()) {
                for (AtlasEdge edge : cloneElements) {
                    boolean deleted = deleteHandler.deleteEdgeReference(edge, entryType.getTypeCategory(), attributeInfo.isComposite, true);
                    if (!deleted) {
                        additionalElements.add(edge);
                    }
                }
            }
            return additionalElements;
        }
    }
    return new ArrayList<>();
}
Also used : ArrayList(java.util.ArrayList) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Aggregations

AtlasEdge (org.apache.atlas.repository.graphdb.AtlasEdge)120 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)53 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)23 ArrayList (java.util.ArrayList)12 AtlasType (org.apache.atlas.type.AtlasType)12 AtlasAttribute (org.apache.atlas.type.AtlasStructType.AtlasAttribute)11 AtlasStructType (org.apache.atlas.type.AtlasStructType)10 Edge (org.apache.tinkerpop.gremlin.structure.Edge)10 RepositoryException (org.apache.atlas.repository.RepositoryException)9 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)9 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)8 AtlasMapType (org.apache.atlas.type.AtlasMapType)8 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)7 AtlasArrayType (org.apache.atlas.type.AtlasArrayType)7 HashSet (java.util.HashSet)6 AtlasException (org.apache.atlas.AtlasException)5 GraphTransaction (org.apache.atlas.annotation.GraphTransaction)5 Iterator (java.util.Iterator)4 List (java.util.List)4 TypeCategory (org.apache.atlas.model.TypeCategory)4