Search in sources :

Example 11 with RequestContextV1

use of org.apache.atlas.RequestContextV1 in project atlas by apache.

the class DeleteHandlerV1 method deleteEdgeReference.

public boolean deleteEdgeReference(AtlasEdge edge, TypeCategory typeCategory, boolean isOwned, boolean forceDeleteStructTrait, AtlasRelationshipEdgeDirection relationshipDirection, AtlasVertex entityVertex) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Deleting {}", string(edge));
    }
    boolean forceDelete = (typeCategory == TypeCategory.STRUCT || typeCategory == TypeCategory.CLASSIFICATION) && forceDeleteStructTrait;
    if (typeCategory == TypeCategory.STRUCT || typeCategory == TypeCategory.CLASSIFICATION || (typeCategory == TypeCategory.OBJECT_ID_TYPE && isOwned)) {
        // If the vertex is of type struct/trait, delete the edge and then the reference vertex as the vertex is not shared by any other entities.
        // If the vertex is of type class, and its composite attribute, this reference vertex' lifecycle is controlled
        // through this delete, hence delete the edge and the reference vertex.
        AtlasVertex vertexForDelete = edge.getInVertex();
        // If deleting the edge and then the in vertex, reverse attribute shouldn't be updated
        deleteEdge(edge, false, forceDelete);
        deleteTypeVertex(vertexForDelete, typeCategory, forceDelete);
    } else {
        // only delete the reference relationship edge
        if (isRelationshipEdge(edge)) {
            deleteEdge(edge, false);
            AtlasVertex referencedVertex = entityRetriever.getReferencedEntityVertex(edge, relationshipDirection, entityVertex);
            if (referencedVertex != null) {
                RequestContextV1 requestContext = RequestContextV1.get();
                if (!requestContext.isUpdatedEntity(GraphHelper.getGuid(referencedVertex))) {
                    GraphHelper.setProperty(referencedVertex, Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY, requestContext.getRequestTime());
                    GraphHelper.setProperty(referencedVertex, Constants.MODIFIED_BY_KEY, requestContext.getUser());
                    requestContext.recordEntityUpdate(entityRetriever.toAtlasObjectId(referencedVertex));
                }
            }
        } else {
            // legacy case - not a relationship edge
            // If deleting just the edge, reverse attribute should be updated for any references
            // For example, for the department type system, if the person's manager edge is deleted, subordinates of manager should be updated
            deleteEdge(edge, true, false);
        }
    }
    return !softDelete || forceDelete;
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) RequestContextV1(org.apache.atlas.RequestContextV1)

Example 12 with RequestContextV1

use of org.apache.atlas.RequestContextV1 in project incubator-atlas by apache.

the class DeleteHandlerV1 method deleteEdgeBetweenVertices.

/**
 * Deletes the edge between outvertex and inVertex. The edge is for attribute attributeName of outVertex
 * @param outVertex
 * @param inVertex
 * @param attribute
 * @throws AtlasException
 */
protected void deleteEdgeBetweenVertices(AtlasVertex outVertex, AtlasVertex inVertex, AtlasAttribute attribute) throws AtlasBaseException {
    LOG.debug("Removing edge from {} to {} with attribute name {}", string(outVertex), string(inVertex), attribute.getName());
    String typeName = GraphHelper.getTypeName(outVertex);
    String outId = GraphHelper.getGuid(outVertex);
    AtlasObjectId objId = new AtlasObjectId(outId, typeName);
    AtlasEntity.Status state = AtlasGraphUtilsV1.getState(outVertex);
    if (state == AtlasEntity.Status.DELETED || (outId != null && RequestContextV1.get().isDeletedEntity(objId))) {
        // If the reference vertex is marked for deletion, skip updating the reference
        return;
    }
    AtlasStructType parentType = (AtlasStructType) typeRegistry.getType(typeName);
    String propertyName = AtlasGraphUtilsV1.getQualifiedAttributePropertyKey(parentType, attribute.getName());
    String edgeLabel = EDGE_LABEL_PREFIX + propertyName;
    AtlasEdge edge = null;
    AtlasAttributeDef attrDef = attribute.getAttributeDef();
    AtlasType attrType = attribute.getAttributeType();
    switch(attrType.getTypeCategory()) {
        case OBJECT_ID_TYPE:
            // If its class attribute, its the only edge between two vertices
            if (attrDef.getIsOptional()) {
                edge = graphHelper.getEdgeForLabel(outVertex, edgeLabel);
                if (shouldUpdateInverseReferences) {
                    GraphHelper.setProperty(outVertex, propertyName, null);
                }
            } else {
                // Cannot unset a required attribute.
                throw new AtlasBaseException("Cannot unset required attribute " + propertyName + " on " + GraphHelper.vertexString(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 (!attrDef.getIsOptional() && elements.size() <= attrDef.getValuesMinCount()) {
                            // Deleting this edge would violate the attribute's lower bound.
                            throw new AtlasBaseException("Cannot remove array element from required attribute " + propertyName + " on " + GraphHelper.getVertexDetails(outVertex) + " " + GraphHelper.getEdgeDetails(elementEdge));
                        }
                        if (shouldUpdateInverseReferences) {
                            // if composite attribute, remove the reference as well. else, just remove the edge
                            // for example, when table is deleted, process still references the table
                            // but when column is deleted, table will not reference the deleted column
                            LOG.debug("Removing edge {} from the array attribute {}", string(elementEdge), attribute.getName());
                            // 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 (attrDef.getIsOptional() || keys.size() > attrDef.getValuesMinCount()) {
                                edge = mapEdge;
                            } else {
                                // Deleting this entry would violate the attribute's lower bound.
                                throw new AtlasBaseException("Cannot remove map entry " + keyPropertyName + " from required attribute " + propertyName + " on " + GraphHelper.getVertexDetails(outVertex) + " " + GraphHelper.getEdgeDetails(mapEdge));
                            }
                            if (shouldUpdateInverseReferences) {
                                // remove this key
                                LOG.debug("Removing edge {}, key {} from the map attribute {}", string(mapEdge), key, attribute.getName());
                                keys.remove(key);
                                GraphHelper.setProperty(outVertex, propertyName, keys);
                                GraphHelper.setProperty(outVertex, keyPropertyName, null);
                            }
                            break;
                        }
                    }
                }
            }
            break;
        case STRUCT:
        case CLASSIFICATION:
            break;
        default:
            throw new IllegalStateException("There can't be an edge from " + GraphHelper.getVertexDetails(outVertex) + " to " + GraphHelper.getVertexDetails(inVertex) + " with attribute name " + attribute.getName() + " which is not class/array/map attribute. found " + attrType.getTypeCategory().name());
    }
    if (edge != null) {
        deleteEdge(edge, false);
        RequestContextV1 requestContext = RequestContextV1.get();
        GraphHelper.setProperty(outVertex, Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY, requestContext.getRequestTime());
        GraphHelper.setProperty(outVertex, Constants.MODIFIED_BY_KEY, requestContext.getUser());
        requestContext.recordEntityUpdate(new AtlasObjectId(outId, typeName));
    }
}
Also used : RequestContextV1(org.apache.atlas.RequestContextV1) AtlasStructType(org.apache.atlas.type.AtlasStructType) AtlasType(org.apache.atlas.type.AtlasType) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasAttributeDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef)

Example 13 with RequestContextV1

use of org.apache.atlas.RequestContextV1 in project incubator-atlas by apache.

the class EntityGraphMapper method mapAttributesAndClassifications.

public EntityMutationResponse mapAttributesAndClassifications(EntityMutationContext context, final boolean isPartialUpdate, final boolean replaceClassifications) throws AtlasBaseException {
    EntityMutationResponse resp = new EntityMutationResponse();
    Collection<AtlasEntity> createdEntities = context.getCreatedEntities();
    Collection<AtlasEntity> updatedEntities = context.getUpdatedEntities();
    if (CollectionUtils.isNotEmpty(createdEntities)) {
        for (AtlasEntity createdEntity : createdEntities) {
            String guid = createdEntity.getGuid();
            AtlasVertex vertex = context.getVertex(guid);
            AtlasEntityType entityType = context.getType(guid);
            mapAttributes(createdEntity, vertex, CREATE, context);
            mapRelationshipAttributes(createdEntity, vertex, CREATE, context);
            resp.addEntity(CREATE, constructHeader(createdEntity, entityType, vertex));
            addClassifications(context, guid, createdEntity.getClassifications());
        }
    }
    if (CollectionUtils.isNotEmpty(updatedEntities)) {
        for (AtlasEntity updatedEntity : updatedEntities) {
            String guid = updatedEntity.getGuid();
            AtlasVertex vertex = context.getVertex(guid);
            AtlasEntityType entityType = context.getType(guid);
            mapAttributes(updatedEntity, vertex, UPDATE, context);
            mapRelationshipAttributes(updatedEntity, vertex, UPDATE, context);
            if (isPartialUpdate) {
                resp.addEntity(PARTIAL_UPDATE, constructHeader(updatedEntity, entityType, vertex));
            } else {
                resp.addEntity(UPDATE, constructHeader(updatedEntity, entityType, vertex));
            }
            if (replaceClassifications) {
                deleteClassifications(guid);
                addClassifications(context, guid, updatedEntity.getClassifications());
            }
        }
    }
    RequestContextV1 req = RequestContextV1.get();
    for (AtlasObjectId id : req.getDeletedEntityIds()) {
        resp.addEntity(DELETE, constructHeader(id));
    }
    for (AtlasObjectId id : req.getUpdatedEntityIds()) {
        if (isPartialUpdate) {
            resp.addEntity(PARTIAL_UPDATE, constructHeader(id));
        } else {
            resp.addEntity(UPDATE, constructHeader(id));
        }
    }
    return resp;
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) RequestContextV1(org.apache.atlas.RequestContextV1) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) AtlasEntityType(org.apache.atlas.type.AtlasEntityType)

Example 14 with RequestContextV1

use of org.apache.atlas.RequestContextV1 in project incubator-atlas by apache.

the class AtlasEntityStoreV1 method deleteVertices.

private EntityMutationResponse deleteVertices(Collection<AtlasVertex> deletionCandidates) throws AtlasBaseException {
    EntityMutationResponse response = new EntityMutationResponse();
    deleteHandler.deleteEntities(deletionCandidates);
    RequestContextV1 req = RequestContextV1.get();
    for (AtlasObjectId id : req.getDeletedEntityIds()) {
        response.addEntity(DELETE, EntityGraphMapper.constructHeader(id));
    }
    for (AtlasObjectId id : req.getUpdatedEntityIds()) {
        response.addEntity(UPDATE, EntityGraphMapper.constructHeader(id));
    }
    return response;
}
Also used : RequestContextV1(org.apache.atlas.RequestContextV1) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId)

Example 15 with RequestContextV1

use of org.apache.atlas.RequestContextV1 in project incubator-atlas by apache.

the class EntityGraphMapper method recordEntityUpdate.

private void recordEntityUpdate(AtlasVertex vertex) {
    AtlasObjectId objectId = new AtlasObjectId(GraphHelper.getGuid(vertex), GraphHelper.getTypeName(vertex));
    RequestContextV1 req = RequestContextV1.get();
    if (!objectIdsContain(req.getUpdatedEntityIds(), objectId)) {
        req.recordEntityUpdate(objectId);
    }
}
Also used : RequestContextV1(org.apache.atlas.RequestContextV1) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId)

Aggregations

RequestContextV1 (org.apache.atlas.RequestContextV1)15 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)8 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)7 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)6 EntityMutationResponse (org.apache.atlas.model.instance.EntityMutationResponse)3 AtlasEdge (org.apache.atlas.repository.graphdb.AtlasEdge)3 AtlasStructType (org.apache.atlas.type.AtlasStructType)3 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)2 AtlasAttributeDef (org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef)2 GraphHelper (org.apache.atlas.repository.graph.GraphHelper)2 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)2 AtlasType (org.apache.atlas.type.AtlasType)2 Date (java.util.Date)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 AtlasEntityWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo)1