Search in sources :

Example 66 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 67 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 68 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 69 with AtlasEdge

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

the class EntityGraphMapper method addInverseReference.

private void addInverseReference(AttributeMutationContext ctx, AtlasAttribute inverseAttribute, AtlasEdge edge) throws AtlasBaseException {
    AtlasStructType inverseType = inverseAttribute.getDefinedInType();
    String propertyName = AtlasGraphUtilsV1.getQualifiedAttributePropertyKey(inverseType, inverseAttribute.getName());
    AtlasVertex vertex = edge.getOutVertex();
    AtlasVertex inverseVertex = edge.getInVertex();
    String inverseEdgeLabel = AtlasGraphUtilsV1.getEdgeLabel(propertyName);
    AtlasEdge inverseEdge = graphHelper.getEdgeForLabel(inverseVertex, inverseEdgeLabel);
    AtlasEdge newEdge;
    try {
        newEdge = graphHelper.getOrCreateEdge(inverseVertex, vertex, inverseEdgeLabel);
    } catch (RepositoryException e) {
        throw new AtlasBaseException(AtlasErrorCode.INTERNAL_ERROR, e);
    }
    boolean inverseUpdated = true;
    switch(inverseAttribute.getAttributeType().getTypeCategory()) {
        case OBJECT_ID_TYPE:
            if (inverseEdge != null) {
                if (!inverseEdge.equals(newEdge)) {
                    // Disconnect old reference
                    deleteHandler.deleteEdgeReference(inverseEdge, inverseAttribute.getAttributeType().getTypeCategory(), inverseAttribute.isOwnedRef(), true);
                } else {
                    // Edge already exists for this attribute between these vertices.
                    inverseUpdated = false;
                }
            }
            break;
        case ARRAY:
            // Add edge ID to property value
            List<String> elements = inverseVertex.getProperty(propertyName, List.class);
            if (elements == null) {
                elements = new ArrayList<>();
                elements.add(newEdge.getId().toString());
                inverseVertex.setProperty(propertyName, elements);
            } else {
                if (!elements.contains(newEdge.getId().toString())) {
                    elements.add(newEdge.getId().toString());
                    inverseVertex.setProperty(propertyName, elements);
                } else {
                    // Property value list already contains the edge ID.
                    inverseUpdated = false;
                }
            }
            break;
        default:
            break;
    }
    if (inverseUpdated) {
        updateModificationMetadata(inverseVertex);
        AtlasObjectId inverseEntityId = new AtlasObjectId(AtlasGraphUtilsV1.getIdFromVertex(inverseVertex), inverseType.getTypeName());
        RequestContextV1.get().recordEntityUpdate(inverseEntityId);
    }
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasStructType(org.apache.atlas.type.AtlasStructType) RepositoryException(org.apache.atlas.repository.RepositoryException) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Example 70 with AtlasEdge

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

the class DeleteHandlerV1 method deleteVertex.

protected void deleteVertex(AtlasVertex instanceVertex, boolean force) throws AtlasBaseException {
    // 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)) {
        AtlasEntity.Status edgeState = getState(edge);
        if (edgeState == AtlasEntity.Status.ACTIVE) {
            // Delete only the active edge references
            AtlasAttribute attribute = getAttributeForEdge(edge.getLabel());
            // TODO use delete edge instead??
            deleteEdgeBetweenVertices(edge.getOutVertex(), edge.getInVertex(), attribute);
        }
    }
    _deleteVertex(instanceVertex, force);
}
Also used : AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) 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