Search in sources :

Example 56 with AtlasEdge

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

the class EntityGraphRetriever method mapRelationshipArrayAttribute.

private List<AtlasObjectId> mapRelationshipArrayAttribute(AtlasVertex entityVertex, AtlasAttribute attribute) throws AtlasBaseException {
    List<AtlasObjectId> ret = new ArrayList<>();
    Iterator<AtlasEdge> edges = null;
    if (attribute.getRelationshipEdgeDirection() == AtlasRelationshipEdgeDirection.IN) {
        edges = graphHelper.getIncomingEdgesByLabel(entityVertex, attribute.getRelationshipEdgeLabel());
    } else if (attribute.getRelationshipEdgeDirection() == AtlasRelationshipEdgeDirection.OUT) {
        edges = graphHelper.getOutGoingEdgesByLabel(entityVertex, attribute.getRelationshipEdgeLabel());
    }
    if (edges != null) {
        while (edges.hasNext()) {
            AtlasEdge relationshipEdge = edges.next();
            AtlasObjectId objectId = mapRelatedVertexToObjectId(entityVertex, relationshipEdge);
            ret.add(objectId);
        }
    }
    return ret;
}
Also used : ArrayList(java.util.ArrayList) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Example 57 with AtlasEdge

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

the class TypedInstanceToGraphMapper method mapAttributeToVertex.

void mapAttributeToVertex(ITypedInstance typedInstance, AtlasVertex instanceVertex, AttributeInfo attributeInfo, Operation operation) throws AtlasException {
    if (typedInstance.isValueSet(attributeInfo.name) || operation == Operation.CREATE) {
        Object attrValue = typedInstance.get(attributeInfo.name);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Mapping attribute {} = {}", attributeInfo.name, attrValue);
        }
        switch(attributeInfo.dataType().getTypeCategory()) {
            case PRIMITIVE:
            case ENUM:
                mapPrimitiveOrEnumToVertex(typedInstance, instanceVertex, attributeInfo);
                break;
            case ARRAY:
                mapArrayCollectionToVertex(typedInstance, instanceVertex, attributeInfo, operation);
                break;
            case MAP:
                mapMapCollectionToVertex(typedInstance, instanceVertex, attributeInfo, operation);
                break;
            case STRUCT:
            case CLASS:
                String edgeLabel = graphHelper.getEdgeLabel(typedInstance, attributeInfo);
                AtlasEdge currentEdge = graphHelper.getEdgeForLabel(instanceVertex, edgeLabel);
                AtlasEdge newEdge = addOrUpdateReference(instanceVertex, attributeInfo, attributeInfo.dataType(), attrValue, currentEdge, edgeLabel, operation);
                if (currentEdge != null && !currentEdge.equals(newEdge)) {
                    deleteHandler.deleteEdgeReference(currentEdge, attributeInfo.dataType().getTypeCategory(), attributeInfo.isComposite, true);
                }
                if (attributeInfo.reverseAttributeName != null && newEdge != null) {
                    addReverseReference(instanceVertex, attributeInfo.reverseAttributeName, newEdge);
                }
                break;
            case TRAIT:
                // do NOTHING - this is taken care of earlier
                break;
            default:
                throw new IllegalArgumentException("Unknown type category: " + attributeInfo.dataType().getTypeCategory());
        }
    }
}
Also used : AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Example 58 with AtlasEdge

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

the class TypedInstanceToGraphMapper method addOrUpdateClassVertex.

/**
 ****************************************** CLASS *************************************************
 */
private AtlasEdge addOrUpdateClassVertex(AtlasVertex instanceVertex, AtlasEdge currentEdge, ITypedReferenceableInstance newAttributeValue, AttributeInfo attributeInfo, String edgeLabel) throws AtlasException {
    AtlasVertex newReferenceVertex = getClassVertex(newAttributeValue);
    if (!GraphHelper.elementExists(newReferenceVertex) && newAttributeValue != null) {
        LOG.error("Could not find vertex for Class Reference {}", newAttributeValue);
        throw new EntityNotFoundException("Could not find vertex for Class Reference " + newAttributeValue);
    }
    AtlasEdge newEdge = null;
    if (GraphHelper.elementExists(currentEdge) && newAttributeValue != null) {
        newEdge = updateClassEdge(instanceVertex, currentEdge, newAttributeValue, newReferenceVertex, attributeInfo, edgeLabel);
    } else if (!GraphHelper.elementExists(currentEdge) && newAttributeValue != null) {
        newEdge = addClassEdge(instanceVertex, newReferenceVertex, edgeLabel);
    }
    return newEdge;
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) EntityNotFoundException(org.apache.atlas.typesystem.exception.EntityNotFoundException) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Example 59 with AtlasEdge

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

the class TypedInstanceToGraphMapper method addOrUpdateStruct.

/**
 ****************************************** STRUCT *************************************************
 */
private AtlasEdge addOrUpdateStruct(AtlasVertex instanceVertex, AttributeInfo attributeInfo, ITypedStruct newAttributeValue, AtlasEdge currentEdge, String edgeLabel, Operation operation) throws AtlasException {
    AtlasEdge newEdge = null;
    if (GraphHelper.elementExists(currentEdge) && newAttributeValue != null) {
        // update
        updateStructVertex(newAttributeValue, currentEdge, operation);
        newEdge = currentEdge;
    } else if (!GraphHelper.elementExists(currentEdge) && newAttributeValue != null) {
        // add
        newEdge = addStructVertex(newAttributeValue, instanceVertex, attributeInfo, edgeLabel);
    }
    return newEdge;
}
Also used : AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Example 60 with AtlasEdge

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

the class TypedInstanceToGraphMapper method mapArrayCollectionToVertex.

/**
 ****************************************** ARRAY *************************************************
 */
private void mapArrayCollectionToVertex(ITypedInstance typedInstance, AtlasVertex instanceVertex, AttributeInfo attributeInfo, Operation operation) throws AtlasException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Mapping instance {} for array attribute {} vertex {}", typedInstance.toShortString(), attributeInfo.name, string(instanceVertex));
    }
    List newElements = (List) typedInstance.get(attributeInfo.name);
    boolean newAttributeEmpty = (newElements == null || newElements.isEmpty());
    IDataType elementType = ((DataTypes.ArrayType) attributeInfo.dataType()).getElemType();
    String propertyName = GraphHelper.getQualifiedFieldName(typedInstance, attributeInfo);
    List<Object> currentElements = GraphHelper.getArrayElementsProperty(elementType, instanceVertex, propertyName);
    List<Object> newElementsCreated = new ArrayList<>();
    if (!newAttributeEmpty) {
        int index = 0;
        for (; index < newElements.size(); index++) {
            Object currentElement = (currentElements != null && index < currentElements.size()) ? currentElements.get(index) : null;
            if (LOG.isDebugEnabled()) {
                LOG.debug("Adding/updating element at position {}, current element {}, new element {}", index, currentElement, newElements.get(index));
            }
            Object newEntry = addOrUpdateCollectionEntry(instanceVertex, attributeInfo, elementType, newElements.get(index), currentElement, propertyName, operation);
            newElementsCreated.add(newEntry);
        }
    }
    if (GraphHelper.isReference(elementType)) {
        if (attributeInfo.reverseAttributeName != null && newElementsCreated.size() > 0) {
            // Set/add the new reference value(s) on the reverse reference.
            for (Object newElement : newElementsCreated) {
                if ((newElement instanceof AtlasEdge)) {
                    AtlasEdge newEdge = (AtlasEdge) newElement;
                    addReverseReference(instanceVertex, attributeInfo.reverseAttributeName, newEdge);
                } else {
                    throw new AtlasException("Invalid array element type " + newElement.getClass().getName() + " - expected " + AtlasEdge.class.getName() + " for reference " + GraphHelper.getQualifiedFieldName(typedInstance, attributeInfo) + " on vertex " + GraphHelper.getVertexDetails(instanceVertex));
                }
            }
        }
        List<AtlasEdge> additionalEdges = removeUnusedEntries(instanceVertex, propertyName, (List) currentElements, (List) newElementsCreated, elementType, attributeInfo);
        newElementsCreated.addAll(additionalEdges);
    }
    // for dereference on way out
    GraphHelper.setArrayElementsProperty(elementType, instanceVertex, propertyName, newElementsCreated);
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) AtlasException(org.apache.atlas.AtlasException) 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