Search in sources :

Example 21 with AtlasVertex

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

the class DeleteHandler method deleteEdgeReference.

/**
     * Force delete is used to remove struct/trait in case of entity updates
     * @param edge
     * @param typeCategory
     * @param isComposite
     * @param forceDeleteStructTrait
     * @return returns true if the edge reference is hard deleted
     * @throws AtlasException
     */
public boolean deleteEdgeReference(AtlasEdge edge, DataTypes.TypeCategory typeCategory, boolean isComposite, boolean forceDeleteStructTrait) throws AtlasException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Deleting {}", string(edge));
    }
    boolean forceDelete = (typeCategory == DataTypes.TypeCategory.STRUCT || typeCategory == DataTypes.TypeCategory.TRAIT) && forceDeleteStructTrait;
    if (typeCategory == DataTypes.TypeCategory.STRUCT || typeCategory == DataTypes.TypeCategory.TRAIT || (typeCategory == DataTypes.TypeCategory.CLASS && isComposite)) {
        //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 {
        //If the vertex is of type class, and its not a composite attributes, the reference AtlasVertex' lifecycle is not controlled
        //through this delete. Hence just remove the reference edge. Leave the reference AtlasVertex as is
        //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)

Example 22 with AtlasVertex

use of org.apache.atlas.repository.graphdb.AtlasVertex 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 23 with AtlasVertex

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

the class GraphBackedMetadataRepository method getEntityDefinitions.

@Override
@GraphTransaction
public List<ITypedReferenceableInstance> getEntityDefinitions(String... guids) throws RepositoryException, EntityNotFoundException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Retrieving entities with guids={}", Arrays.toString(guids));
    }
    RequestContext context = RequestContext.get();
    ITypedReferenceableInstance[] result = new ITypedReferenceableInstance[guids.length];
    // Map of the guids of instances not in the cache to their index(es) in the result.
    // This is used to put the loaded instances into the location(s) corresponding
    // to their guid in the result.  Note that a set is needed since guids can
    // appear more than once in the list.
    Map<String, Set<Integer>> uncachedGuids = new HashMap<>();
    for (int i = 0; i < guids.length; i++) {
        String guid = guids[i];
        // First, check the cache.
        ITypedReferenceableInstance cached = context.getInstanceV1(guid);
        if (cached != null) {
            result[i] = cached;
        } else {
            Set<Integer> indices = uncachedGuids.get(guid);
            if (indices == null) {
                indices = new HashSet<>(1);
                uncachedGuids.put(guid, indices);
            }
            indices.add(i);
        }
    }
    List<String> guidsToFetch = new ArrayList<>(uncachedGuids.keySet());
    Map<String, AtlasVertex> instanceVertices = graphHelper.getVerticesForGUIDs(guidsToFetch);
    // search for missing entities
    if (instanceVertices.size() != guidsToFetch.size()) {
        Set<String> missingGuids = new HashSet<String>(guidsToFetch);
        missingGuids.removeAll(instanceVertices.keySet());
        if (!missingGuids.isEmpty()) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Failed to find guids={}", missingGuids);
            }
            throw new EntityNotFoundException("Could not find entities in the repository with guids: " + missingGuids.toString());
        }
    }
    for (String guid : guidsToFetch) {
        try {
            ITypedReferenceableInstance entity = graphToInstanceMapper.mapGraphToTypedInstance(guid, instanceVertices.get(guid));
            for (int index : uncachedGuids.get(guid)) {
                result[index] = entity;
            }
        } catch (AtlasException e) {
            throw new RepositoryException(e);
        }
    }
    return Arrays.asList(result);
}
Also used : ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) RepositoryException(org.apache.atlas.repository.RepositoryException) EntityNotFoundException(org.apache.atlas.typesystem.exception.EntityNotFoundException) AtlasException(org.apache.atlas.AtlasException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) RequestContext(org.apache.atlas.RequestContext) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Example 24 with AtlasVertex

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

the class GraphBackedMetadataRepository method getEntityList.

@Override
@GraphTransaction
public List<String> getEntityList(String entityType) throws RepositoryException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Retrieving entity list for type={}", entityType);
    }
    AtlasGraphQuery query = getGraph().query().has(Constants.ENTITY_TYPE_PROPERTY_KEY, entityType);
    Iterator<AtlasVertex> results = query.vertices().iterator();
    if (!results.hasNext()) {
        return Collections.emptyList();
    }
    ArrayList<String> entityList = new ArrayList<>();
    while (results.hasNext()) {
        AtlasVertex vertex = results.next();
        entityList.add(GraphHelper.getGuid(vertex));
    }
    return entityList;
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasGraphQuery(org.apache.atlas.repository.graphdb.AtlasGraphQuery) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Example 25 with AtlasVertex

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

the class FullTextMapper method forAttribute.

private String forAttribute(IDataType type, Object value, boolean followReferences) throws AtlasException {
    if (value == null) {
        return null;
    }
    switch(type.getTypeCategory()) {
        case PRIMITIVE:
            return String.valueOf(value);
        case ENUM:
            return ((EnumValue) value).value;
        case ARRAY:
            StringBuilder fullText = new StringBuilder();
            IDataType elemType = ((DataTypes.ArrayType) type).getElemType();
            List list = (List) value;
            for (Object element : list) {
                String elemFullText = forAttribute(elemType, element, false);
                if (StringUtils.isNotEmpty(elemFullText)) {
                    fullText = fullText.append(FULL_TEXT_DELIMITER).append(elemFullText);
                }
            }
            return fullText.toString();
        case MAP:
            fullText = new StringBuilder();
            IDataType keyType = ((DataTypes.MapType) type).getKeyType();
            IDataType valueType = ((DataTypes.MapType) type).getValueType();
            Map map = (Map) value;
            for (Object entryObj : map.entrySet()) {
                Map.Entry entry = (Map.Entry) entryObj;
                String keyFullText = forAttribute(keyType, entry.getKey(), false);
                if (StringUtils.isNotEmpty(keyFullText)) {
                    fullText = fullText.append(FULL_TEXT_DELIMITER).append(keyFullText);
                }
                String valueFullText = forAttribute(valueType, entry.getValue(), false);
                if (StringUtils.isNotEmpty(valueFullText)) {
                    fullText = fullText.append(FULL_TEXT_DELIMITER).append(valueFullText);
                }
            }
            return fullText.toString();
        case CLASS:
            if (followReferences) {
                Id refId = ((ITypedReferenceableInstance) value).getId();
                String refGuid = refId._getId();
                AtlasVertex refVertex = typedInstanceToGraphMapper.lookupVertex(refId);
                if (refVertex == null) {
                    refVertex = graphHelper.getVertexForGUID(refGuid);
                }
                return mapRecursive(refVertex, false);
            }
            break;
        case STRUCT:
            if (followReferences) {
                return forInstance((ITypedInstance) value, true);
            }
            break;
        default:
            throw new IllegalStateException("Unhandled type category " + type.getTypeCategory());
    }
    return null;
}
Also used : EnumValue(org.apache.atlas.typesystem.types.EnumValue) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) IDataType(org.apache.atlas.typesystem.types.IDataType) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) List(java.util.List) Id(org.apache.atlas.typesystem.persistence.Id) Map(java.util.Map)

Aggregations

AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)164 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)53 AtlasEdge (org.apache.atlas.repository.graphdb.AtlasEdge)26 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)21 ArrayList (java.util.ArrayList)20 Test (org.testng.annotations.Test)19 HashMap (java.util.HashMap)16 Id (org.apache.atlas.typesystem.persistence.Id)14 Map (java.util.Map)13 List (java.util.List)12 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)12 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)12 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)12 AtlasException (org.apache.atlas.AtlasException)11 GraphTransaction (org.apache.atlas.annotation.GraphTransaction)11 RepositoryException (org.apache.atlas.repository.RepositoryException)11 AtlasGraphQuery (org.apache.atlas.repository.graphdb.AtlasGraphQuery)11 AtlasType (org.apache.atlas.type.AtlasType)11 HashSet (java.util.HashSet)8 AtlasEntityHeader (org.apache.atlas.model.instance.AtlasEntityHeader)8