Search in sources :

Example 36 with AtlasEdge

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

the class EntityGraphMapper method updateEdge.

private AtlasEdge updateEdge(AtlasAttributeDef attributeDef, Object value, AtlasEdge currentEdge, final AtlasVertex entityVertex) throws AtlasBaseException {
    LOG.debug("Updating entity reference {} for reference attribute {}", attributeDef.getName());
    // Update edge if it exists
    AtlasVertex currentVertex = currentEdge.getInVertex();
    String currentEntityId = getIdFromVertex(currentVertex);
    String newEntityId = getIdFromVertex(entityVertex);
    AtlasEdge newEdge = currentEdge;
    if (!currentEntityId.equals(newEntityId)) {
        // add an edge to the class vertex from the instance
        if (entityVertex != null) {
            try {
                newEdge = graphHelper.getOrCreateEdge(currentEdge.getOutVertex(), entityVertex, currentEdge.getLabel());
            } catch (RepositoryException e) {
                throw new AtlasBaseException(AtlasErrorCode.INTERNAL_ERROR, e);
            }
        }
    }
    return newEdge;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) RepositoryException(org.apache.atlas.repository.RepositoryException) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Example 37 with AtlasEdge

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

the class EntityGraphMapper method removeUnusedMapEntries.

// Remove unused entries from map
private Map<String, Object> removeUnusedMapEntries(AtlasAttribute attribute, AtlasVertex vertex, String propertyName, Map<String, Object> currentMap, Map<String, Object> newMap) throws AtlasException, AtlasBaseException {
    AtlasMapType mapType = (AtlasMapType) attribute.getAttributeType();
    Map<String, Object> additionalMap = new HashMap<>();
    for (String currentKey : currentMap.keySet()) {
        boolean shouldDeleteKey = !newMap.containsKey(currentKey);
        if (AtlasGraphUtilsV1.isReference(mapType.getValueType())) {
            // Delete the edge reference if its not part of new edges created/updated
            AtlasEdge currentEdge = (AtlasEdge) currentMap.get(currentKey);
            if (!newMap.values().contains(currentEdge)) {
                boolean deleted = deleteHandler.deleteEdgeReference(currentEdge, mapType.getValueType().getTypeCategory(), attribute.isOwnedRef(), true);
                if (!deleted) {
                    additionalMap.put(currentKey, currentEdge);
                    shouldDeleteKey = false;
                }
            }
        }
        if (shouldDeleteKey) {
            String propertyNameForKey = GraphHelper.getQualifiedNameForMapKey(propertyName, GraphHelper.encodePropertyKey(currentKey));
            GraphHelper.setProperty(vertex, propertyNameForKey, null);
        }
    }
    return additionalMap;
}
Also used : AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) AtlasMapType(org.apache.atlas.type.AtlasMapType)

Example 38 with AtlasEdge

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

the class EntityGraphMapper method mapObjectIdValueUsingRelationship.

private AtlasEdge mapObjectIdValueUsingRelationship(AttributeMutationContext ctx, EntityMutationContext context) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> mapObjectIdValueUsingRelationship({})", ctx);
    }
    AtlasVertex attributeVertex = context.getDiscoveryContext().getResolvedEntityVertex(getGuid(ctx.getValue()));
    AtlasVertex entityVertex = ctx.getReferringVertex();
    AtlasEdge ret;
    if (attributeVertex == null) {
        AtlasObjectId objectId = getObjectId(ctx.getValue());
        attributeVertex = (objectId != null) ? context.getDiscoveryContext().getResolvedEntityVertex(objectId) : null;
    }
    if (attributeVertex == null) {
        throw new AtlasBaseException(AtlasErrorCode.INVALID_OBJECT_ID, (ctx.getValue() == null ? null : ctx.getValue().toString()));
    }
    String attributeName = ctx.getAttribute().getName();
    AtlasType type = typeRegistry.getType(AtlasGraphUtilsV1.getTypeName(entityVertex));
    AtlasRelationshipEdgeDirection edgeDirection = ctx.getAttribute().getRelationshipEdgeDirection();
    String edgeLabel = ctx.getAttribute().getRelationshipEdgeLabel();
    if (type instanceof AtlasEntityType) {
        AtlasEntityType entityType = (AtlasEntityType) type;
        // use relationship to create/update edges
        if (entityType.hasRelationshipAttribute(attributeName)) {
            if (ctx.getCurrentEdge() != null) {
                ret = updateRelationship(ctx.getCurrentEdge(), attributeVertex, edgeDirection, ctx.getAttribute());
                recordEntityUpdate(attributeVertex);
            } else {
                String relationshipName = graphHelper.getRelationshipDefName(entityVertex, entityType, attributeName);
                AtlasVertex fromVertex;
                AtlasVertex toVertex;
                if (edgeDirection == IN) {
                    fromVertex = attributeVertex;
                    toVertex = entityVertex;
                } else {
                    fromVertex = entityVertex;
                    toVertex = attributeVertex;
                }
                boolean relationshipExists = isRelationshipExists(fromVertex, toVertex, edgeLabel);
                ret = getOrCreateRelationship(fromVertex, toVertex, relationshipName, ctx.getAttribute());
                // record entity update on both relationship vertices
                if (!relationshipExists) {
                    recordEntityUpdate(attributeVertex);
                }
            }
        } else {
            // use legacy way to create/update edges
            if (LOG.isDebugEnabled()) {
                LOG.debug("No RelationshipDef defined between {} and {} on attribute: {}", getTypeName(entityVertex), getTypeName(attributeVertex), attributeName);
            }
            ret = mapObjectIdValue(ctx, context);
        }
    } else {
        // if type is StructType having objectid as attribute
        ret = mapObjectIdValue(ctx, context);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== mapObjectIdValueUsingRelationship({})", ctx);
    }
    return ret;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasRelationshipEdgeDirection(org.apache.atlas.type.AtlasStructType.AtlasAttribute.AtlasRelationshipEdgeDirection) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasType(org.apache.atlas.type.AtlasType) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) AtlasEntityType(org.apache.atlas.type.AtlasEntityType)

Example 39 with AtlasEdge

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

the class EntityGraphMapper method updateRelationship.

private AtlasEdge updateRelationship(AtlasEdge currentEdge, final AtlasVertex newEntityVertex, AtlasRelationshipEdgeDirection edgeDirection, AtlasAttribute attribute) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Updating entity reference using relationship {} for reference attribute {}", getTypeName(newEntityVertex));
    }
    // Max's manager updated from Jane to Julius (Max.manager --> Jane.subordinates)
    // manager attribute (OUT direction), current manager vertex (Jane) (IN vertex)
    // Max's mentor updated from John to Jane (John.mentee --> Max.mentor)
    // mentor attribute (IN direction), current mentee vertex (John) (OUT vertex)
    String currentEntityId = (edgeDirection == IN) ? getIdFromVertex(currentEdge.getOutVertex()) : getIdFromVertex(currentEdge.getInVertex());
    String newEntityId = getIdFromVertex(newEntityVertex);
    AtlasEdge ret = currentEdge;
    if (!currentEntityId.equals(newEntityId)) {
        // create a new relationship edge to the new attribute vertex from the instance
        String relationshipName = AtlasGraphUtilsV1.getTypeName(currentEdge);
        if (relationshipName == null) {
            relationshipName = currentEdge.getLabel();
        }
        ret = (edgeDirection == IN) ? getOrCreateRelationship(newEntityVertex, currentEdge.getInVertex(), relationshipName, attribute) : getOrCreateRelationship(currentEdge.getOutVertex(), newEntityVertex, relationshipName, attribute);
    }
    return ret;
}
Also used : AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Example 40 with AtlasEdge

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

the class EntityGraphMapper method getOrCreateRelationship.

private AtlasEdge getOrCreateRelationship(AtlasVertex end1Vertex, AtlasVertex end2Vertex, String relationshipName, AtlasAttribute attribute) throws AtlasBaseException {
    AtlasEdge ret = null;
    AtlasObjectId end1 = new AtlasObjectId(getIdFromVertex(end1Vertex), AtlasGraphUtilsV1.getTypeName(end1Vertex));
    AtlasObjectId end2 = new AtlasObjectId(getIdFromVertex(end2Vertex), AtlasGraphUtilsV1.getTypeName(end2Vertex));
    AtlasRelationship relationship = relationshipStore.getOrCreate(new AtlasRelationship(relationshipName, end1, end2));
    // return newly created AtlasEdge
    // if multiple edges are returned, compare using guid to pick the right one
    Iterator<AtlasEdge> outEdges = graphHelper.getOutGoingEdgesByLabel(end1Vertex, relationship.getLabel());
    while (outEdges.hasNext()) {
        AtlasEdge edge = outEdges.next();
        if (getIdFromVertex(end2Vertex).equals(getIdFromVertex(edge.getInVertex()))) {
            ret = edge;
            break;
        }
    }
    return ret;
}
Also used : AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) AtlasRelationship(org.apache.atlas.model.instance.AtlasRelationship) 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