Search in sources :

Example 1 with SortOrder

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

the class GlossaryServiceTest method testGetGlossaryTerms.

@Test(dataProvider = "getGlossaryTermsProvider", groups = "Glossary.GET.postUpdate", dependsOnGroups = "Glossary.UPDATE")
public void testGetGlossaryTerms(int offset, int limit, int expected) {
    String guid = bankGlossary.getGuid();
    SortOrder sortOrder = SortOrder.ASCENDING;
    try {
        List<AtlasRelatedTermHeader> glossaryTerms = glossaryService.getGlossaryTermsHeaders(guid, offset, limit, sortOrder);
        assertNotNull(glossaryTerms);
        assertEquals(glossaryTerms.size(), expected);
    } catch (AtlasBaseException e) {
        fail("Glossary term fetching should've succeeded", e);
    }
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasRelatedTermHeader(org.apache.atlas.model.glossary.relations.AtlasRelatedTermHeader) SortOrder(org.apache.atlas.SortOrder) Test(org.testng.annotations.Test)

Example 2 with SortOrder

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

the class GlossaryServiceTest method testGetGlossaryCategories.

@Test(dataProvider = "getGlossaryCategoriesProvider", groups = "Glossary.GET.postUpdate", dependsOnGroups = "Glossary.UPDATE")
public void testGetGlossaryCategories(int offset, int limit, int expected) {
    String guid = bankGlossary.getGuid();
    SortOrder sortOrder = SortOrder.ASCENDING;
    try {
        List<AtlasRelatedCategoryHeader> glossaryCategories = glossaryService.getGlossaryCategoriesHeaders(guid, offset, limit, sortOrder);
        assertNotNull(glossaryCategories);
        assertEquals(glossaryCategories.size(), expected);
    } catch (AtlasBaseException e) {
        fail("Glossary term fetching should've succeeded");
    }
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) SortOrder(org.apache.atlas.SortOrder) AtlasRelatedCategoryHeader(org.apache.atlas.model.glossary.relations.AtlasRelatedCategoryHeader) Test(org.testng.annotations.Test)

Example 3 with SortOrder

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

the class EntityDiscoveryService method searchRelatedEntities.

@Override
@GraphTransaction
public AtlasSearchResult searchRelatedEntities(String guid, String relation, boolean getApproximateCount, SearchParameters searchParameters) throws AtlasBaseException {
    AtlasSearchResult ret = new AtlasSearchResult(AtlasQueryType.RELATIONSHIP);
    if (StringUtils.isEmpty(guid) || StringUtils.isEmpty(relation)) {
        throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS, "guid: '" + guid + "', relation: '" + relation + "'");
    }
    // validate entity
    AtlasVertex entityVertex = entityRetriever.getEntityVertex(guid);
    String entityTypeName = GraphHelper.getTypeName(entityVertex);
    AtlasEntityType entityType = typeRegistry.getEntityTypeByName(entityTypeName);
    if (entityType == null) {
        throw new AtlasBaseException(AtlasErrorCode.INVALID_RELATIONSHIP_TYPE, entityTypeName, guid);
    }
    // validate relation
    AtlasEntityType endEntityType = null;
    AtlasAttribute attribute = entityType.getAttribute(relation);
    if (attribute == null) {
        attribute = entityType.getRelationshipAttribute(relation, null);
    }
    if (attribute != null) {
        // get end entity type through relationship attribute
        endEntityType = attribute.getReferencedEntityType(typeRegistry);
        if (endEntityType == null) {
            throw new AtlasBaseException(AtlasErrorCode.INVALID_RELATIONSHIP_ATTRIBUTE, relation, attribute.getTypeName());
        }
        relation = attribute.getRelationshipEdgeLabel();
    } else {
        // get end entity type through label
        String endEntityTypeName = GraphHelper.getReferencedEntityTypeName(entityVertex, relation);
        if (StringUtils.isNotEmpty(endEntityTypeName)) {
            endEntityType = typeRegistry.getEntityTypeByName(endEntityTypeName);
        }
    }
    // validate sortBy attribute
    String sortBy = searchParameters.getSortBy();
    SortOrder sortOrder = searchParameters.getSortOrder();
    int offset = searchParameters.getOffset();
    int limit = searchParameters.getLimit();
    String sortByAttributeName = DEFAULT_SORT_ATTRIBUTE_NAME;
    if (StringUtils.isNotEmpty(sortBy)) {
        sortByAttributeName = sortBy;
    }
    if (endEntityType != null) {
        AtlasAttribute sortByAttribute = endEntityType.getAttribute(sortByAttributeName);
        if (sortByAttribute == null) {
            sortByAttributeName = null;
            sortOrder = null;
            if (StringUtils.isNotEmpty(sortBy)) {
                LOG.info("Invalid sortBy Attribute {} for entityType {}, Ignoring Sorting", sortBy, endEntityType.getTypeName());
            } else {
                LOG.info("Invalid Default sortBy Attribute {} for entityType {}, Ignoring Sorting", DEFAULT_SORT_ATTRIBUTE_NAME, endEntityType.getTypeName());
            }
        } else {
            sortByAttributeName = sortByAttribute.getVertexPropertyName();
            if (sortOrder == null) {
                sortOrder = ASCENDING;
            }
        }
    } else {
        sortOrder = null;
        if (StringUtils.isNotEmpty(sortBy)) {
            LOG.info("Invalid sortBy Attribute {}, Ignoring Sorting", sortBy);
        }
    }
    // get relationship(end vertices) vertices
    GraphTraversal gt = graph.V(entityVertex.getId()).bothE(relation).otherV();
    if (searchParameters.getExcludeDeletedEntities()) {
        gt.has(Constants.STATE_PROPERTY_KEY, AtlasEntity.Status.ACTIVE.name());
    }
    if (sortOrder != null) {
        if (sortOrder == ASCENDING) {
            gt.order().by(sortByAttributeName, Order.asc);
        } else {
            gt.order().by(sortByAttributeName, Order.desc);
        }
    }
    gt.range(offset, offset + limit);
    List<AtlasEntityHeader> resultList = new ArrayList<>();
    while (gt.hasNext()) {
        Vertex v = (Vertex) gt.next();
        if (v != null && v.property(Constants.GUID_PROPERTY_KEY).isPresent()) {
            String endVertexGuid = v.property(Constants.GUID_PROPERTY_KEY).value().toString();
            AtlasVertex vertex = entityRetriever.getEntityVertex(endVertexGuid);
            AtlasEntityHeader entity = entityRetriever.toAtlasEntityHeader(vertex, searchParameters.getAttributes());
            if (searchParameters.getIncludeClassificationAttributes()) {
                entity.setClassifications(entityRetriever.getAllClassifications(vertex));
            }
            resultList.add(entity);
        }
    }
    ret.setEntities(resultList);
    if (ret.getEntities() == null) {
        ret.setEntities(new ArrayList<>());
    }
    // state of the edge and endVertex will be same
    if (getApproximateCount) {
        Iterator<AtlasEdge> edges = GraphHelper.getAdjacentEdgesByLabel(entityVertex, AtlasEdgeDirection.BOTH, relation);
        if (searchParameters.getExcludeDeletedEntities()) {
            List<AtlasEdge> edgeList = new ArrayList<>();
            edges.forEachRemaining(edgeList::add);
            Predicate activePredicate = SearchPredicateUtil.getEQPredicateGenerator().generatePredicate(Constants.STATE_PROPERTY_KEY, AtlasEntity.Status.ACTIVE.name(), String.class);
            CollectionUtils.filter(edgeList, activePredicate);
            ret.setApproximateCount(edgeList.size());
        } else {
            ret.setApproximateCount(IteratorUtils.size(edges));
        }
    }
    scrubSearchResults(ret);
    return ret;
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) ArrayList(java.util.ArrayList) SortOrder(org.apache.atlas.SortOrder) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) AtlasSearchResult(org.apache.atlas.model.discovery.AtlasSearchResult) Predicate(org.apache.commons.collections.Predicate) AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) AtlasEntityType(org.apache.atlas.type.AtlasEntityType) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Example 4 with SortOrder

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

the class ClassificationSearchProcessor method execute.

@Override
public List<AtlasVertex> execute() {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> ClassificationSearchProcessor.execute({})", context);
    }
    List<AtlasVertex> ret = new ArrayList<>();
    AtlasPerfTracer perf = null;
    if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
        perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "ClassificationSearchProcessor.execute(" + context + ")");
    }
    try {
        final int limit = context.getSearchParameters().getLimit();
        Integer marker = context.getMarker();
        // marker functionality will not work when there is need to fetch classificationVertices and get entities from it
        if (indexQuery == null) {
            marker = null;
        }
        // if marker is provided, start query with marker offset
        int startIdx = marker != null ? marker : context.getSearchParameters().getOffset();
        int qryOffset = marker != null ? marker : 0;
        int resultIdx = qryOffset;
        final Set<String> processedGuids = new HashSet<>();
        LinkedHashMap<Integer, AtlasVertex> offsetEntityVertexMap = new LinkedHashMap<>();
        final List<AtlasVertex> classificationVertices = new ArrayList<>();
        final String sortBy = context.getSearchParameters().getSortBy();
        final SortOrder sortOrder = context.getSearchParameters().getSortOrder();
        for (; ret.size() < limit; qryOffset += limit) {
            offsetEntityVertexMap.clear();
            classificationVertices.clear();
            if (context.terminateSearch()) {
                LOG.warn("query terminated: {}", context.getSearchParameters());
                break;
            }
            boolean isLastResultPage = true;
            if (indexQuery != null) {
                Iterator<AtlasIndexQuery.Result> queryResult;
                if (StringUtils.isNotEmpty(sortBy)) {
                    Order qrySortOrder = sortOrder == SortOrder.ASCENDING ? Order.asc : Order.desc;
                    queryResult = indexQuery.vertices(qryOffset, limit, sortBy, qrySortOrder);
                } else {
                    queryResult = indexQuery.vertices(qryOffset, limit);
                }
                offsetEntityVertexMap = getVerticesFromIndexQueryResult(queryResult, offsetEntityVertexMap, qryOffset);
                isLastResultPage = offsetEntityVertexMap.size() < limit;
                // Do in-memory filtering
                offsetEntityVertexMap = super.filter(offsetEntityVertexMap, traitPredicate);
                offsetEntityVertexMap = super.filter(offsetEntityVertexMap, isEntityPredicate);
            } else {
                if (classificationIndexQuery != null) {
                    Iterator<AtlasIndexQuery.Result> queryResult = classificationIndexQuery.vertices(qryOffset, limit);
                    getVerticesFromIndexQueryResult(queryResult, classificationVertices);
                    isLastResultPage = classificationVertices.size() < limit;
                    CollectionUtils.filter(classificationVertices, inMemoryPredicate);
                } else if (tagGraphQueryWithAttributes != null) {
                    Iterator<AtlasVertex> queryResult = tagGraphQueryWithAttributes.vertices(qryOffset, limit).iterator();
                    getVertices(queryResult, classificationVertices);
                    isLastResultPage = classificationVertices.size() < limit;
                    CollectionUtils.filter(classificationVertices, inMemoryPredicate);
                }
            }
            // vertex results (as these might be lower in number)
            if (CollectionUtils.isNotEmpty(classificationVertices)) {
                int resultCount = 0;
                for (AtlasVertex classificationVertex : classificationVertices) {
                    Iterable<AtlasEdge> edges = classificationVertex.getEdges(AtlasEdgeDirection.IN, Constants.CLASSIFICATION_LABEL);
                    for (AtlasEdge edge : edges) {
                        AtlasVertex entityVertex = edge.getOutVertex();
                        resultCount++;
                        String guid = AtlasGraphUtilsV2.getIdFromVertex(entityVertex);
                        if (processedGuids.contains(guid)) {
                            continue;
                        }
                        offsetEntityVertexMap.put((qryOffset + resultCount) - 1, entityVertex);
                        processedGuids.add(guid);
                    }
                }
            }
            if (whiteSpaceFilter) {
                offsetEntityVertexMap = filterWhiteSpaceClassification(offsetEntityVertexMap);
            }
            // Do in-memory filtering
            offsetEntityVertexMap = super.filter(offsetEntityVertexMap, isEntityPredicate);
            if (activePredicate != null) {
                offsetEntityVertexMap = super.filter(offsetEntityVertexMap, activePredicate);
            }
            offsetEntityVertexMap = super.filter(offsetEntityVertexMap);
            resultIdx = collectResultVertices(ret, startIdx, limit, resultIdx, offsetEntityVertexMap, marker);
            if (isLastResultPage) {
                resultIdx = SearchContext.MarkerUtil.MARKER_END - 1;
                break;
            }
        }
        if (marker != null) {
            nextOffset = resultIdx + 1;
        }
    } finally {
        AtlasPerfTracer.log(perf);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== ClassificationSearchProcessor.execute({}): ret.size()={}", context, ret.size());
    }
    return ret;
}
Also used : SortOrder(org.apache.atlas.SortOrder) Order(org.apache.tinkerpop.gremlin.process.traversal.Order) AtlasPerfTracer(org.apache.atlas.utils.AtlasPerfTracer) SortOrder(org.apache.atlas.SortOrder)

Aggregations

SortOrder (org.apache.atlas.SortOrder)4 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)3 Test (org.testng.annotations.Test)2 ArrayList (java.util.ArrayList)1 GraphTransaction (org.apache.atlas.annotation.GraphTransaction)1 AtlasSearchResult (org.apache.atlas.model.discovery.AtlasSearchResult)1 AtlasRelatedCategoryHeader (org.apache.atlas.model.glossary.relations.AtlasRelatedCategoryHeader)1 AtlasRelatedTermHeader (org.apache.atlas.model.glossary.relations.AtlasRelatedTermHeader)1 AtlasEntityHeader (org.apache.atlas.model.instance.AtlasEntityHeader)1 AtlasEdge (org.apache.atlas.repository.graphdb.AtlasEdge)1 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)1 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)1 AtlasAttribute (org.apache.atlas.type.AtlasStructType.AtlasAttribute)1 AtlasPerfTracer (org.apache.atlas.utils.AtlasPerfTracer)1 Predicate (org.apache.commons.collections.Predicate)1 Order (org.apache.tinkerpop.gremlin.process.traversal.Order)1 GraphTraversal (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal)1 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)1