Search in sources :

Example 1 with FilterIterator

use of com.baidu.hugegraph.iterator.FilterIterator in project incubator-hugegraph by apache.

the class HugeTraverser method edgesOfVertex.

private Iterator<Edge> edgesOfVertex(Id source, EdgeStep edgeStep, boolean mustAllSK) {
    Id[] edgeLabels = edgeStep.edgeLabels();
    Query query = GraphTransaction.constructEdgesQuery(source, edgeStep.direction(), edgeLabels);
    ConditionQuery filter = null;
    if (mustAllSK) {
        this.fillFilterBySortKeys(query, edgeLabels, edgeStep.properties());
    } else {
        filter = (ConditionQuery) query.copy();
        this.fillFilterByProperties(filter, edgeStep.properties());
    }
    query.capacity(Query.NO_CAPACITY);
    if (edgeStep.limit() != NO_LIMIT) {
        query.limit(edgeStep.limit());
    }
    Iterator<Edge> edges = this.graph().edges(query);
    if (filter != null) {
        ConditionQuery finalFilter = filter;
        edges = new FilterIterator<>(edges, (e) -> {
            return finalFilter.test((HugeEdge) e);
        });
    }
    return edgeStep.skipSuperNodeIfNeeded(edges);
}
Also used : QueryResults(com.baidu.hugegraph.backend.query.QueryResults) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) NotFoundException(com.baidu.hugegraph.exception.NotFoundException) CollectionUtil(com.baidu.hugegraph.util.CollectionUtil) MultivaluedMap(jakarta.ws.rs.core.MultivaluedMap) FilterIterator(com.baidu.hugegraph.iterator.FilterIterator) ImmutableList(com.google.common.collect.ImmutableList) CollectionUtils(org.apache.commons.collections.CollectionUtils) HugeGraph(com.baidu.hugegraph.HugeGraph) Map(java.util.Map) Query(com.baidu.hugegraph.backend.query.Query) CollectionFactory(com.baidu.hugegraph.util.collection.CollectionFactory) E(com.baidu.hugegraph.util.E) CoreOptions(com.baidu.hugegraph.config.CoreOptions) Edge(org.apache.tinkerpop.gremlin.structure.Edge) MapperIterator(com.baidu.hugegraph.iterator.MapperIterator) CollectionType(com.baidu.hugegraph.type.define.CollectionType) Logger(org.slf4j.Logger) ImmutableSet(com.google.common.collect.ImmutableSet) Iterator(java.util.Iterator) ExtendableIterator(com.baidu.hugegraph.iterator.ExtendableIterator) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) EdgeStep(com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) InsertionOrderUtil(com.baidu.hugegraph.util.InsertionOrderUtil) HugeKeys(com.baidu.hugegraph.type.define.HugeKeys) GraphTransaction(com.baidu.hugegraph.backend.tx.GraphTransaction) LimitIterator(com.baidu.hugegraph.iterator.LimitIterator) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched) Objects(java.util.Objects) MultivaluedHashMap(jakarta.ws.rs.core.MultivaluedHashMap) SchemaLabel(com.baidu.hugegraph.schema.SchemaLabel) HugeException(com.baidu.hugegraph.HugeException) List(java.util.List) Log(com.baidu.hugegraph.util.Log) Directions(com.baidu.hugegraph.type.define.Directions) Aggregate(com.baidu.hugegraph.backend.query.Aggregate) Id(com.baidu.hugegraph.backend.id.Id) TraversalUtil(com.baidu.hugegraph.traversal.optimize.TraversalUtil) Collections(java.util.Collections) HugeType(com.baidu.hugegraph.type.HugeType) Query(com.baidu.hugegraph.backend.query.Query) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) Id(com.baidu.hugegraph.backend.id.Id) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge)

Example 2 with FilterIterator

use of com.baidu.hugegraph.iterator.FilterIterator in project incubator-hugegraph by apache.

the class GraphTransaction method joinTxEdges.

private Iterator<?> joinTxEdges(Query query, Iterator<HugeEdge> edges, Map<Id, HugeVertex> removingVertices) {
    assert query.resultType().isEdge();
    BiFunction<Query, HugeEdge, HugeEdge> matchTxFunc = (q, e) -> {
        assert q.resultType() == HugeType.EDGE;
        if (e.expired() && !q.showExpired()) {
            // Filter expired edges with TTL
            return null;
        }
        // Filter edges matched conditions
        return q.test(e) ? e : q.test(e = e.switchOwner()) ? e : null;
    };
    edges = this.joinTxRecords(query, edges, matchTxFunc, this.addedEdges, this.removedEdges, this.updatedEdges);
    if (removingVertices.isEmpty()) {
        return edges;
    }
    // Filter edges that belong to deleted vertex
    return new FilterIterator<HugeEdge>(edges, edge -> {
        for (HugeVertex v : removingVertices.values()) {
            if (edge.belongToVertex(v)) {
                return false;
            }
        }
        return true;
    });
}
Also used : Arrays(java.util.Arrays) QueryResults(com.baidu.hugegraph.backend.query.QueryResults) ListIterator(com.baidu.hugegraph.iterator.ListIterator) BiFunction(java.util.function.BiFunction) Graph(org.apache.tinkerpop.gremlin.structure.Graph) BackendException(com.baidu.hugegraph.backend.BackendException) HugeProperty(com.baidu.hugegraph.structure.HugeProperty) HugeElement(com.baidu.hugegraph.structure.HugeElement) ElementHelper(org.apache.tinkerpop.gremlin.structure.util.ElementHelper) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) SplicingIdGenerator(com.baidu.hugegraph.backend.id.SplicingIdGenerator) LimitExceedException(com.baidu.hugegraph.exception.LimitExceedException) DeleteExpiredJob(com.baidu.hugegraph.job.system.DeleteExpiredJob) LockUtil(com.baidu.hugegraph.util.LockUtil) Map(java.util.Map) Query(com.baidu.hugegraph.backend.query.Query) EdgeLabel(com.baidu.hugegraph.schema.EdgeLabel) IteratorUtils(org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils) MapperIterator(com.baidu.hugegraph.iterator.MapperIterator) BackendStore(com.baidu.hugegraph.backend.store.BackendStore) ExtendableIterator(com.baidu.hugegraph.iterator.ExtendableIterator) FlatMapperIterator(com.baidu.hugegraph.iterator.FlatMapperIterator) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) Collection(java.util.Collection) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) Set(java.util.Set) QueryList(com.baidu.hugegraph.backend.page.QueryList) InsertionOrderUtil(com.baidu.hugegraph.util.InsertionOrderUtil) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) LimitIterator(com.baidu.hugegraph.iterator.LimitIterator) ForbiddenException(jakarta.ws.rs.ForbiddenException) List(java.util.List) Element(org.apache.tinkerpop.gremlin.structure.Element) CloseableIterator(org.apache.tinkerpop.gremlin.structure.util.CloseableIterator) BatchMapperIterator(com.baidu.hugegraph.iterator.BatchMapperIterator) IdStrategy(com.baidu.hugegraph.type.define.IdStrategy) Id(com.baidu.hugegraph.backend.id.Id) AggregateFunc(com.baidu.hugegraph.backend.query.Aggregate.AggregateFunc) HugeVertexFeatures(com.baidu.hugegraph.structure.HugeFeatures.HugeVertexFeatures) HugeGraphParams(com.baidu.hugegraph.HugeGraphParams) BackendEntry(com.baidu.hugegraph.backend.store.BackendEntry) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) NotFoundException(com.baidu.hugegraph.exception.NotFoundException) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ConditionQueryFlatten(com.baidu.hugegraph.backend.query.ConditionQueryFlatten) FilterIterator(com.baidu.hugegraph.iterator.FilterIterator) PropertyKey(com.baidu.hugegraph.schema.PropertyKey) ImmutableList(com.google.common.collect.ImmutableList) CollectionUtils(org.apache.commons.collections.CollectionUtils) HugeGraph(com.baidu.hugegraph.HugeGraph) OptimizedType(com.baidu.hugegraph.backend.query.ConditionQuery.OptimizedType) E(com.baidu.hugegraph.util.E) CoreOptions(com.baidu.hugegraph.config.CoreOptions) Edge(org.apache.tinkerpop.gremlin.structure.Edge) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) PageInfo(com.baidu.hugegraph.backend.page.PageInfo) Iterator(java.util.Iterator) HugeEdgeProperty(com.baidu.hugegraph.structure.HugeEdgeProperty) Condition(com.baidu.hugegraph.backend.query.Condition) EdgeId(com.baidu.hugegraph.backend.id.EdgeId) BackendMutation(com.baidu.hugegraph.backend.store.BackendMutation) HugeKeys(com.baidu.hugegraph.type.define.HugeKeys) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched) Consumer(java.util.function.Consumer) SchemaLabel(com.baidu.hugegraph.schema.SchemaLabel) Action(com.baidu.hugegraph.type.define.Action) HugeException(com.baidu.hugegraph.HugeException) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) Directions(com.baidu.hugegraph.type.define.Directions) Aggregate(com.baidu.hugegraph.backend.query.Aggregate) HugeVertexProperty(com.baidu.hugegraph.structure.HugeVertexProperty) HugeIndex(com.baidu.hugegraph.structure.HugeIndex) HugeConfig(com.baidu.hugegraph.config.HugeConfig) IdHolderList(com.baidu.hugegraph.backend.page.IdHolderList) HugeType(com.baidu.hugegraph.type.HugeType) Query(com.baidu.hugegraph.backend.query.Query) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) FilterIterator(com.baidu.hugegraph.iterator.FilterIterator) HugeVertex(com.baidu.hugegraph.structure.HugeVertex)

Example 3 with FilterIterator

use of com.baidu.hugegraph.iterator.FilterIterator in project hugegraph-common by hugegraph.

the class FilterIteratorTest method testFilter.

@Test
public void testFilter() {
    AtomicInteger callbackCount = new AtomicInteger(0);
    Iterator<Integer> values = DATA.iterator();
    Function<Integer, Boolean> filter = value -> {
        callbackCount.incrementAndGet();
        return (value % 2 == 0);
    };
    Iterator<Integer> results = new FilterIterator<>(values, filter);
    List<Integer> actual = new ArrayList<>();
    while (results.hasNext()) {
        actual.add(results.next());
    }
    Assert.assertEquals(4, callbackCount.get());
    Assert.assertEquals(ImmutableList.of(2, 4), actual);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List) Iterator(java.util.Iterator) FilterIterator(com.baidu.hugegraph.iterator.FilterIterator) ImmutableList(com.google.common.collect.ImmutableList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) CloseableItor(com.baidu.hugegraph.unit.iterator.ExtendableIteratorTest.CloseableItor) Test(org.junit.Test) NoSuchElementException(java.util.NoSuchElementException) Assert(com.baidu.hugegraph.testutil.Assert) Function(java.util.function.Function) ArrayList(java.util.ArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) FilterIterator(com.baidu.hugegraph.iterator.FilterIterator) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 4 with FilterIterator

use of com.baidu.hugegraph.iterator.FilterIterator in project hugegraph-common by hugegraph.

the class FilterIteratorTest method testNextWithOriginIteratorReturnNullElem.

@Test
public void testNextWithOriginIteratorReturnNullElem() {
    List<Integer> list = new ArrayList<>();
    list.add(1);
    list.add(null);
    list.add(3);
    Iterator<Integer> vals = list.iterator();
    AtomicInteger callbackCount = new AtomicInteger(0);
    Iterator<Integer> results = new FilterIterator<>(vals, val -> {
        callbackCount.incrementAndGet();
        return true;
    });
    Assert.assertTrue(results.hasNext());
    for (int i = 0; i < 2; i++) {
        results.next();
    }
    Assert.assertFalse(results.hasNext());
    Assert.assertEquals(2, callbackCount.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) FilterIterator(com.baidu.hugegraph.iterator.FilterIterator) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 5 with FilterIterator

use of com.baidu.hugegraph.iterator.FilterIterator in project incubator-hugegraph by apache.

the class GraphTransaction method joinTxRecords.

private <V extends HugeElement> Iterator<V> joinTxRecords(Query query, Iterator<V> records, BiFunction<Query, V, V> matchFunc, Map<Id, V> addedTxRecords, Map<Id, V> removedTxRecords, Map<Id, V> updatedTxRecords) {
    this.checkOwnerThread();
    // Return the origin results if there is no change in tx
    if (addedTxRecords.isEmpty() && removedTxRecords.isEmpty() && updatedTxRecords.isEmpty()) {
        return records;
    }
    Set<V> txResults = InsertionOrderUtil.newSet();
    /*
         * Collect added/updated records
         * Records in memory have higher priority than query from backend store
         */
    for (V elem : addedTxRecords.values()) {
        if (query.reachLimit(txResults.size())) {
            break;
        }
        if ((elem = matchFunc.apply(query, elem)) != null) {
            txResults.add(elem);
        }
    }
    for (V elem : updatedTxRecords.values()) {
        if (query.reachLimit(txResults.size())) {
            break;
        }
        if ((elem = matchFunc.apply(query, elem)) != null) {
            txResults.add(elem);
        }
    }
    // Filter backend record if it's updated in memory
    Iterator<V> backendResults = new FilterIterator<>(records, elem -> {
        Id id = elem.id();
        return !addedTxRecords.containsKey(id) && !updatedTxRecords.containsKey(id) && !removedTxRecords.containsKey(id);
    });
    return new ExtendableIterator<V>(txResults.iterator(), backendResults);
}
Also used : ExtendableIterator(com.baidu.hugegraph.iterator.ExtendableIterator) FilterIterator(com.baidu.hugegraph.iterator.FilterIterator) Id(com.baidu.hugegraph.backend.id.Id) EdgeId(com.baidu.hugegraph.backend.id.EdgeId)

Aggregations

FilterIterator (com.baidu.hugegraph.iterator.FilterIterator)5 Id (com.baidu.hugegraph.backend.id.Id)3 ExtendableIterator (com.baidu.hugegraph.iterator.ExtendableIterator)3 ImmutableList (com.google.common.collect.ImmutableList)3 Iterator (java.util.Iterator)3 List (java.util.List)3 HugeException (com.baidu.hugegraph.HugeException)2 HugeGraph (com.baidu.hugegraph.HugeGraph)2 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)2 Aggregate (com.baidu.hugegraph.backend.query.Aggregate)2 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)2 Query (com.baidu.hugegraph.backend.query.Query)2 QueryResults (com.baidu.hugegraph.backend.query.QueryResults)2 CoreOptions (com.baidu.hugegraph.config.CoreOptions)2 NotFoundException (com.baidu.hugegraph.exception.NotFoundException)2 LimitIterator (com.baidu.hugegraph.iterator.LimitIterator)2 MapperIterator (com.baidu.hugegraph.iterator.MapperIterator)2 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)2 SchemaLabel (com.baidu.hugegraph.schema.SchemaLabel)2 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)2