Search in sources :

Example 6 with GraphTransaction

use of org.apache.atlas.annotation.GraphTransaction in project incubator-atlas by apache.

the class AtlasEntityStoreV1 method getById.

@Override
@GraphTransaction
public AtlasEntityWithExtInfo getById(String guid) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> getById({})", guid);
    }
    EntityGraphRetriever entityRetriever = new EntityGraphRetriever(typeRegistry);
    AtlasEntityWithExtInfo ret = entityRetriever.toAtlasEntityWithExtInfo(guid);
    if (ret == null) {
        throw new AtlasBaseException(AtlasErrorCode.INSTANCE_GUID_NOT_FOUND, guid);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== getById({}): {}", guid, ret);
    }
    return ret;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasEntityWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Example 7 with GraphTransaction

use of org.apache.atlas.annotation.GraphTransaction 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 8 with GraphTransaction

use of org.apache.atlas.annotation.GraphTransaction 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 9 with GraphTransaction

use of org.apache.atlas.annotation.GraphTransaction in project incubator-atlas by apache.

the class GraphBackedMetadataRepository method updatePartial.

@Override
@GraphTransaction
public CreateUpdateEntitiesResult updatePartial(ITypedReferenceableInstance entity) throws RepositoryException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("updating entity {}", entity);
    }
    try {
        TypedInstanceToGraphMapper instanceToGraphMapper = new TypedInstanceToGraphMapper(graphToInstanceMapper, deleteHandler);
        instanceToGraphMapper.mapTypedInstanceToGraph(TypedInstanceToGraphMapper.Operation.UPDATE_PARTIAL, entity);
        RequestContext requestContext = RequestContext.get();
        CreateUpdateEntitiesResult result = new CreateUpdateEntitiesResult();
        GuidMapping mapping = instanceToGraphMapper.createGuidMapping();
        result.setEntityResult(createEntityResultFromContext(requestContext));
        result.setGuidMapping(mapping);
        return result;
    } catch (AtlasException e) {
        throw new RepositoryException(e);
    }
}
Also used : CreateUpdateEntitiesResult(org.apache.atlas.CreateUpdateEntitiesResult) RepositoryException(org.apache.atlas.repository.RepositoryException) RequestContext(org.apache.atlas.RequestContext) AtlasException(org.apache.atlas.AtlasException) GuidMapping(org.apache.atlas.model.instance.GuidMapping) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Example 10 with GraphTransaction

use of org.apache.atlas.annotation.GraphTransaction in project incubator-atlas by apache.

the class GraphBackedMetadataRepository method updateEntities.

@Override
@GraphTransaction
public CreateUpdateEntitiesResult updateEntities(ITypedReferenceableInstance... entitiesUpdated) throws RepositoryException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("updating entity {}", entitiesUpdated);
    }
    try {
        TypedInstanceToGraphMapper instanceToGraphMapper = new TypedInstanceToGraphMapper(graphToInstanceMapper, deleteHandler);
        instanceToGraphMapper.mapTypedInstanceToGraph(TypedInstanceToGraphMapper.Operation.UPDATE_FULL, entitiesUpdated);
        CreateUpdateEntitiesResult result = new CreateUpdateEntitiesResult();
        RequestContext requestContext = RequestContext.get();
        result.setEntityResult(createEntityResultFromContext(requestContext));
        GuidMapping mapping = instanceToGraphMapper.createGuidMapping();
        result.setGuidMapping(mapping);
        return result;
    } catch (AtlasException e) {
        throw new RepositoryException(e);
    }
}
Also used : CreateUpdateEntitiesResult(org.apache.atlas.CreateUpdateEntitiesResult) RepositoryException(org.apache.atlas.repository.RepositoryException) RequestContext(org.apache.atlas.RequestContext) AtlasException(org.apache.atlas.AtlasException) GuidMapping(org.apache.atlas.model.instance.GuidMapping) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Aggregations

GraphTransaction (org.apache.atlas.annotation.GraphTransaction)34 AtlasTransientTypeRegistry (org.apache.atlas.type.AtlasTypeRegistry.AtlasTransientTypeRegistry)12 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)11 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)11 AtlasException (org.apache.atlas.AtlasException)6 RepositoryException (org.apache.atlas.repository.RepositoryException)6 ArrayList (java.util.ArrayList)5 RequestContext (org.apache.atlas.RequestContext)4 AtlasEntityWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo)4 EntityMutationResponse (org.apache.atlas.model.instance.EntityMutationResponse)4 CreateUpdateEntitiesResult (org.apache.atlas.CreateUpdateEntitiesResult)3 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)3 GuidMapping (org.apache.atlas.model.instance.GuidMapping)3 AtlasTypesDef (org.apache.atlas.model.typedef.AtlasTypesDef)3 AtlasClassification (org.apache.atlas.model.instance.AtlasClassification)2 EntityResult (org.apache.atlas.model.legacy.EntityResult)2 AtlasClassificationDef (org.apache.atlas.model.typedef.AtlasClassificationDef)2 AtlasEntityDef (org.apache.atlas.model.typedef.AtlasEntityDef)2 AtlasEnumDef (org.apache.atlas.model.typedef.AtlasEnumDef)2 AtlasStructDef (org.apache.atlas.model.typedef.AtlasStructDef)2