Search in sources :

Example 76 with AtlasVertex

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

the class GraphBackedMetadataRepositoryDeleteTestBase method testDeleteEntitiesWithCompositeMapReference.

@Test
public void testDeleteEntitiesWithCompositeMapReference() throws Exception {
    // Create instances of MapOwner and MapValue.
    // Set MapOwner.map with one entry that references MapValue instance.
    ITypedReferenceableInstance entityDefinition = createMapOwnerAndValueEntities();
    String mapOwnerGuid = entityDefinition.getId()._getId();
    // Verify MapOwner.map attribute has expected value.
    ITypedReferenceableInstance mapOwnerInstance = repositoryService.getEntityDefinition(mapOwnerGuid);
    Object object = mapOwnerInstance.get("map");
    Assert.assertNotNull(object);
    Assert.assertTrue(object instanceof Map);
    Map<String, ITypedReferenceableInstance> map = (Map<String, ITypedReferenceableInstance>) object;
    Assert.assertEquals(map.size(), 1);
    ITypedReferenceableInstance mapValueInstance = map.get("value1");
    Assert.assertNotNull(mapValueInstance);
    String mapValueGuid = mapValueInstance.getId()._getId();
    String edgeLabel = GraphHelper.getEdgeLabel(compositeMapOwnerType, compositeMapOwnerType.fieldMapping.fields.get("map"));
    String mapEntryLabel = edgeLabel + "." + "value1";
    AtlasEdgeLabel atlasEdgeLabel = new AtlasEdgeLabel(mapEntryLabel);
    AtlasVertex mapOwnerVertex = GraphHelper.getInstance().getVertexForGUID(mapOwnerGuid);
    object = mapOwnerVertex.getProperty(atlasEdgeLabel.getQualifiedMapKey(), Object.class);
    Assert.assertNotNull(object);
    List<String> deletedEntities = deleteEntities(mapOwnerGuid).getDeletedEntities();
    Assert.assertEquals(deletedEntities.size(), 2);
    Assert.assertTrue(deletedEntities.contains(mapOwnerGuid));
    Assert.assertTrue(deletedEntities.contains(mapValueGuid));
    assertEntityDeleted(mapOwnerGuid);
    assertEntityDeleted(mapValueGuid);
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 77 with AtlasVertex

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

the class EntityDiscoveryService method searchUsingDslQuery.

@Override
public AtlasSearchResult searchUsingDslQuery(String dslQuery, int limit, int offset) throws AtlasBaseException {
    AtlasSearchResult ret = new AtlasSearchResult(dslQuery, AtlasQueryType.DSL);
    GremlinQuery gremlinQuery = toGremlinQuery(dslQuery, limit, offset);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Executing DSL query: {}", dslQuery);
    }
    Object result = graph.executeGremlinScript(gremlinQuery.queryStr(), false);
    if (result instanceof List && CollectionUtils.isNotEmpty((List) result)) {
        List queryResult = (List) result;
        Object firstElement = queryResult.get(0);
        if (firstElement instanceof AtlasVertex) {
            for (Object element : queryResult) {
                if (element instanceof AtlasVertex) {
                    ret.addEntity(entityRetriever.toAtlasEntityHeader((AtlasVertex) element));
                } else {
                    LOG.warn("searchUsingDslQuery({}): expected an AtlasVertex; found unexpected entry in result {}", dslQuery, element);
                }
            }
        } else if (firstElement instanceof Map && (((Map) firstElement).containsKey("theInstance") || ((Map) firstElement).containsKey("theTrait"))) {
            for (Object element : queryResult) {
                if (element instanceof Map) {
                    Map map = (Map) element;
                    if (map.containsKey("theInstance")) {
                        Object value = map.get("theInstance");
                        if (value instanceof List && CollectionUtils.isNotEmpty((List) value)) {
                            Object entry = ((List) value).get(0);
                            if (entry instanceof AtlasVertex) {
                                ret.addEntity(entityRetriever.toAtlasEntityHeader((AtlasVertex) entry));
                            }
                        }
                    }
                } else {
                    LOG.warn("searchUsingDslQuery({}): expected a trait result; found unexpected entry in result {}", dslQuery, element);
                }
            }
        } else if (gremlinQuery.hasSelectList()) {
            ret.setAttributes(toAttributesResult(queryResult, gremlinQuery));
        }
    }
    return ret;
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) List(java.util.List) ArrayList(java.util.ArrayList) GremlinQuery(org.apache.atlas.query.GremlinQuery) AtlasGremlinQuery(org.apache.atlas.util.AtlasGremlinQueryProvider.AtlasGremlinQuery) Map(java.util.Map) HashMap(java.util.HashMap) AtlasSearchResult(org.apache.atlas.model.discovery.AtlasSearchResult)

Example 78 with AtlasVertex

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

the class DefaultGraphPersistenceStrategy method constructClassInstanceId.

@Override
public ITypedReferenceableInstance constructClassInstanceId(ClassType classType, Object value) {
    try {
        AtlasVertex classVertex = (AtlasVertex) value;
        ITypedReferenceableInstance classInstance = classType.createInstance(GraphHelper.getIdFromVertex(classVertex), new String[0]);
        return classType.convert(classInstance, Multiplicity.OPTIONAL);
    } catch (AtlasException e) {
        LOG.error("error while constructing an instance", e);
    }
    return null;
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) AtlasException(org.apache.atlas.AtlasException)

Example 79 with AtlasVertex

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

the class UniqAttrBasedEntityResolver method resolveEntityReferences.

@Override
public EntityGraphDiscoveryContext resolveEntityReferences(EntityGraphDiscoveryContext context) throws AtlasBaseException {
    if (context == null) {
        throw new AtlasBaseException(AtlasErrorCode.INTERNAL_ERROR, "UniqAttrBasedEntityResolver.resolveEntityReferences(): context is null");
    }
    //Resolve attribute references
    List<AtlasObjectId> resolvedReferences = new ArrayList<>();
    for (AtlasObjectId objId : context.getReferencedByUniqAttribs()) {
        //query in graph repo that given unique attribute - check for deleted also?
        AtlasEntityType entityType = typeRegistry.getEntityTypeByName(objId.getTypeName());
        if (entityType == null) {
            throw new AtlasBaseException(AtlasErrorCode.TYPE_NAME_INVALID, TypeCategory.ENTITY.name(), objId.getTypeName());
        }
        AtlasVertex vertex = AtlasGraphUtilsV1.findByUniqueAttributes(entityType, objId.getUniqueAttributes());
        if (vertex != null) {
            context.addResolvedIdByUniqAttribs(objId, vertex);
            resolvedReferences.add(objId);
        } else {
            throw new AtlasBaseException(AtlasErrorCode.REFERENCED_ENTITY_NOT_FOUND, objId.toString());
        }
    }
    return context;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) ArrayList(java.util.ArrayList) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) AtlasEntityType(org.apache.atlas.type.AtlasEntityType)

Example 80 with AtlasVertex

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

the class GraphBackedTypeStore method getOrCreateTypeVertices.

private Map<String, AtlasVertex> getOrCreateTypeVertices(TypeSystem typeSystem, ImmutableList<String> typeNames) throws AtlasException {
    //examine the types to determine what type vertices are needed
    TypeVertexFinder vertexFinder = new TypeVertexFinder(typeSystem);
    processTypes(typeNames, typeSystem, vertexFinder);
    List<TypeVertexInfo> typeVerticesNeeded = vertexFinder.getVerticesToCreate();
    //find or create the type vertices
    List<AtlasVertex> vertices = createVertices(typeVerticesNeeded);
    //Create a type name->AtlasVertex map with the result
    Map<String, AtlasVertex> result = new HashMap<>(typeVerticesNeeded.size());
    for (int i = 0; i < typeVerticesNeeded.size(); i++) {
        TypeVertexInfo createdVertexInfo = typeVerticesNeeded.get(i);
        AtlasVertex createdVertex = vertices.get(i);
        result.put(createdVertexInfo.getTypeName(), createdVertex);
    }
    return result;
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) HashMap(java.util.HashMap)

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