Search in sources :

Example 1 with LimitIterator

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

the class LimitIteratorTest 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 LimitIterator<>(vals, val -> {
        callbackCount.incrementAndGet();
        return false;
    });
    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) LimitIterator(com.baidu.hugegraph.iterator.LimitIterator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 2 with LimitIterator

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

the class LimitIteratorTest method testLimit.

@Test
public void testLimit() {
    AtomicInteger callbackCount = new AtomicInteger(0);
    Iterator<Integer> values = DATA.iterator();
    int limit = 2;
    Function<Integer, Boolean> filter = value -> {
        return callbackCount.incrementAndGet() > limit;
    };
    Iterator<Integer> results = new LimitIterator<>(values, filter);
    List<Integer> actual = new ArrayList<>();
    while (results.hasNext()) {
        actual.add(results.next());
    }
    Assert.assertEquals(3, callbackCount.get());
    Assert.assertEquals(ImmutableList.of(1, 2), actual);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List) Iterator(java.util.Iterator) 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) LimitIterator(com.baidu.hugegraph.iterator.LimitIterator) ArrayList(java.util.ArrayList) LimitIterator(com.baidu.hugegraph.iterator.LimitIterator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 3 with LimitIterator

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

the class HugeTraverser method edgesOfVertex.

@Watched
protected Iterator<Edge> edgesOfVertex(Id source, Directions dir, Map<Id, String> labels, long limit) {
    if (labels == null || labels.isEmpty()) {
        return this.edgesOfVertex(source, dir, (Id) null, limit);
    }
    ExtendableIterator<Edge> results = new ExtendableIterator<>();
    for (Id label : labels.keySet()) {
        E.checkNotNull(label, "edge label");
        results.extend(this.edgesOfVertex(source, dir, label, limit));
    }
    if (limit == NO_LIMIT) {
        return results;
    }
    long[] count = new long[1];
    return new LimitIterator<>(results, e -> {
        return count[0]++ >= limit;
    });
}
Also used : LimitIterator(com.baidu.hugegraph.iterator.LimitIterator) ExtendableIterator(com.baidu.hugegraph.iterator.ExtendableIterator) Id(com.baidu.hugegraph.backend.id.Id) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Aggregations

LimitIterator (com.baidu.hugegraph.iterator.LimitIterator)3 BaseUnitTest (com.baidu.hugegraph.unit.BaseUnitTest)2 ArrayList (java.util.ArrayList)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Test (org.junit.Test)2 Id (com.baidu.hugegraph.backend.id.Id)1 ExtendableIterator (com.baidu.hugegraph.iterator.ExtendableIterator)1 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)1 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)1 Assert (com.baidu.hugegraph.testutil.Assert)1 CloseableItor (com.baidu.hugegraph.unit.iterator.ExtendableIteratorTest.CloseableItor)1 ImmutableList (com.google.common.collect.ImmutableList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 NoSuchElementException (java.util.NoSuchElementException)1 Function (java.util.function.Function)1 Edge (org.apache.tinkerpop.gremlin.structure.Edge)1