Search in sources :

Example 61 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)

Example 62 with AtlasEdge

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

the class DeleteHandler method deleteEdgeBetweenVertices.

/**
 * 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) {
                // Make a copy, else list.remove reflects on titan.getProperty()
                elements = new ArrayList<>(elements);
                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) {
                            // 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) {
                // Make a copy, else list.remove reflects on titan.getProperty()
                keys = new ArrayList<>(keys);
                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);
    }
}
Also used : AttributeInfo(org.apache.atlas.typesystem.types.AttributeInfo) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) Id(org.apache.atlas.typesystem.persistence.Id) RequestContext(org.apache.atlas.RequestContext) IDataType(org.apache.atlas.typesystem.types.IDataType) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) NullRequiredAttributeException(org.apache.atlas.typesystem.exception.NullRequiredAttributeException)

Example 63 with AtlasEdge

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

the class DeleteHandler method deleteTypeVertex.

/**
 * 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);
}
Also used : AttributeInfo(org.apache.atlas.typesystem.types.AttributeInfo) FieldMapping(org.apache.atlas.typesystem.types.FieldMapping) IDataType(org.apache.atlas.typesystem.types.IDataType) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) DataTypes(org.apache.atlas.typesystem.types.DataTypes)

Example 64 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) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) Id(org.apache.atlas.typesystem.persistence.Id) HashSet(java.util.HashSet) DataTypes(org.apache.atlas.typesystem.types.DataTypes)

Example 65 with AtlasEdge

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

the class GraphHelper method dumpToLog.

public static void dumpToLog(final AtlasGraph<?, ?> graph) {
    LOG.debug("*******************Graph Dump****************************");
    LOG.debug("Vertices of {}", graph);
    for (AtlasVertex vertex : graph.getVertices()) {
        LOG.debug(vertexString(vertex));
    }
    LOG.debug("Edges of {}", graph);
    for (AtlasEdge edge : graph.getEdges()) {
        LOG.debug(edgeString(edge));
    }
    LOG.debug("*******************Graph Dump****************************");
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Aggregations

AtlasEdge (org.apache.atlas.repository.graphdb.AtlasEdge)138 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)60 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)28 AtlasType (org.apache.atlas.type.AtlasType)15 ArrayList (java.util.ArrayList)13 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)13 AtlasStructType (org.apache.atlas.type.AtlasStructType)12 AtlasAttribute (org.apache.atlas.type.AtlasStructType.AtlasAttribute)12 RepositoryException (org.apache.atlas.repository.RepositoryException)11 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)11 Edge (org.apache.tinkerpop.gremlin.structure.Edge)10 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)8 AtlasRelationship (org.apache.atlas.model.instance.AtlasRelationship)8 AtlasMapType (org.apache.atlas.type.AtlasMapType)8 AtlasArrayType (org.apache.atlas.type.AtlasArrayType)7 HashSet (java.util.HashSet)6 GraphTransaction (org.apache.atlas.annotation.GraphTransaction)6 Id (org.apache.atlas.typesystem.persistence.Id)6 AtlasException (org.apache.atlas.AtlasException)5 Iterator (java.util.Iterator)4