Search in sources :

Example 36 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)

Example 37 with AtlasVertex

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

the class GraphHelper method getVerticesForPropertyValues.

/**
     * Finds the Vertices that correspond to the given property values.  Property
     * values that are not found in the graph will not be in the map.
     *
     *  @return propertyValue to AtlasVertex map with the result.
     */
public Map<String, AtlasVertex> getVerticesForPropertyValues(String property, List<String> values) {
    if (values.isEmpty()) {
        return Collections.emptyMap();
    }
    Collection<String> nonNullValues = new HashSet<>(values.size());
    for (String value : values) {
        if (value != null) {
            nonNullValues.add(value);
        }
    }
    //create graph query that finds vertices with the guids
    AtlasGraphQuery query = graph.query();
    query.in(property, nonNullValues);
    Iterable<AtlasVertex> results = query.vertices();
    Map<String, AtlasVertex> result = new HashMap<>(values.size());
    //each vertex should go in the result list.
    for (AtlasVertex vertex : results) {
        if (vertex.exists()) {
            String propertyValue = vertex.getProperty(property, String.class);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Found a vertex {} with {} =  {}", string(vertex), property, propertyValue);
            }
            result.put(propertyValue, vertex);
        }
    }
    return result;
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) HashMap(java.util.HashMap) AtlasGraphQuery(org.apache.atlas.repository.graphdb.AtlasGraphQuery) HashSet(java.util.HashSet)

Example 38 with AtlasVertex

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

the class DeleteHandler method deleteEntities.

/**
     * Deletes the specified entity vertices.
     * Deletes any traits, composite entities, and structs owned by each entity.
     * Also deletes all the references from/to the entity.
     *
     * @param instanceVertices
     * @throws AtlasException
     */
public void deleteEntities(Collection<AtlasVertex> instanceVertices) throws AtlasException {
    RequestContext requestContext = RequestContext.get();
    Set<AtlasVertex> deletionCandidateVertices = new HashSet<>();
    for (AtlasVertex instanceVertex : instanceVertices) {
        String guid = GraphHelper.getGuid(instanceVertex);
        Id.EntityState state = GraphHelper.getState(instanceVertex);
        if (requestContext.getDeletedEntityIds().contains(guid) || state == Id.EntityState.DELETED) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Skipping deletion of {} as it is already deleted", guid);
            }
            continue;
        }
        // Get GUIDs and vertices for all deletion candidates.
        Set<VertexInfo> compositeVertices = graphHelper.getCompositeVertices(instanceVertex);
        // and gather deletion candidate vertices.
        for (VertexInfo vertexInfo : compositeVertices) {
            requestContext.recordEntityDelete(vertexInfo.getGuid(), vertexInfo.getTypeName());
            deletionCandidateVertices.add(vertexInfo.getVertex());
        }
    }
    // Delete traits and vertices.
    for (AtlasVertex deletionCandidateVertex : deletionCandidateVertices) {
        deleteAllTraits(deletionCandidateVertex);
        deleteTypeVertex(deletionCandidateVertex, false);
    }
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) VertexInfo(org.apache.atlas.repository.graph.GraphHelper.VertexInfo) RequestContext(org.apache.atlas.RequestContext) Id(org.apache.atlas.typesystem.persistence.Id) HashSet(java.util.HashSet)

Example 39 with AtlasVertex

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

the class GraphBackedMetadataRepository method addTraitImpl.

private void addTraitImpl(String guid, ITypedStruct traitInstance) throws RepositoryException {
    final String traitName = traitInstance.getTypeName();
    if (LOG.isDebugEnabled()) {
        LOG.debug("Adding a new trait={} for entity={}", traitName, guid);
    }
    try {
        AtlasVertex instanceVertex = graphHelper.getVertexForGUID(guid);
        // add the trait instance as a new vertex
        final String typeName = GraphHelper.getTypeName(instanceVertex);
        TypedInstanceToGraphMapper instanceToGraphMapper = new TypedInstanceToGraphMapper(graphToInstanceMapper, deleteHandler);
        instanceToGraphMapper.mapTraitInstanceToVertex(traitInstance, typeSystem.getDataType(ClassType.class, typeName), instanceVertex);
        // update the traits in entity once adding trait instance is successful
        GraphHelper.addProperty(instanceVertex, Constants.TRAIT_NAMES_PROPERTY_KEY, traitName);
        GraphHelper.setProperty(instanceVertex, Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY, RequestContext.get().getRequestTime());
        GraphHelper.setProperty(instanceVertex, Constants.MODIFIED_BY_KEY, RequestContext.get().getUser());
    } catch (RepositoryException e) {
        throw e;
    } catch (Exception e) {
        throw new RepositoryException(e);
    }
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) RepositoryException(org.apache.atlas.repository.RepositoryException) ClassType(org.apache.atlas.typesystem.types.ClassType) RepositoryException(org.apache.atlas.repository.RepositoryException) TraitNotFoundException(org.apache.atlas.typesystem.exception.TraitNotFoundException) EntityExistsException(org.apache.atlas.typesystem.exception.EntityExistsException) EntityNotFoundException(org.apache.atlas.typesystem.exception.EntityNotFoundException) AtlasException(org.apache.atlas.AtlasException)

Example 40 with AtlasVertex

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

the class GraphBackedMetadataRepositoryTest method testGetIdFromVertex.

@Test(dependsOnMethods = "testCreateEntity")
public void testGetIdFromVertex() throws Exception {
    AtlasVertex tableVertex = getTableEntityVertex();
    String guid = GraphHelper.getSingleValuedProperty(tableVertex, Constants.GUID_PROPERTY_KEY, String.class);
    if (guid == null) {
        Assert.fail();
    }
    Id expected = new Id(guid, GraphHelper.getSingleValuedProperty(tableVertex, Constants.VERSION_PROPERTY_KEY, Integer.class), TestUtils.TABLE_TYPE);
    Assert.assertEquals(GraphHelper.getIdFromVertex(TestUtils.TABLE_TYPE, tableVertex), expected);
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) Id(org.apache.atlas.typesystem.persistence.Id) Test(org.testng.annotations.Test)

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