Search in sources :

Example 1 with FlatMapperIterator

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

the class CountTraverser method count.

public long count(Id source, List<EdgeStep> steps, boolean containsTraversed, long dedupSize) {
    E.checkNotNull(source, "source vertex id");
    this.checkVertexExist(source, "source vertex");
    E.checkArgument(steps != null && !steps.isEmpty(), "The steps can't be empty");
    checkDedupSize(dedupSize);
    this.containsTraversed = containsTraversed;
    this.dedupSize = dedupSize;
    if (this.containsTraversed) {
        this.count.increment();
    }
    int stepNum = steps.size();
    EdgeStep firstStep = steps.get(0);
    if (stepNum == 1) {
        // Just one step, query count and return
        long edgesCount = this.edgesCount(source, firstStep);
        this.count.add(edgesCount);
        return this.count.longValue();
    }
    // Multiple steps, construct first step to iterator
    Iterator<Edge> edges = this.edgesOfVertexWithCount(source, firstStep);
    // Wrap steps to Iterator except last step
    for (int i = 1; i < stepNum - 1; i++) {
        EdgeStep currentStep = steps.get(i);
        edges = new FlatMapperIterator<>(edges, (edge) -> {
            Id target = ((HugeEdge) edge).id().otherVertexId();
            return this.edgesOfVertexWithCount(target, currentStep);
        });
    }
    // The last step, just query count
    EdgeStep lastStep = steps.get(stepNum - 1);
    while (edges.hasNext()) {
        Id target = ((HugeEdge) edges.next()).id().otherVertexId();
        if (this.dedup(target)) {
            continue;
        }
        // Count last layer vertices(without dedup size)
        long edgesCount = this.edgesCount(target, lastStep);
        this.count.add(edgesCount);
    }
    return this.count.longValue();
}
Also used : Iterator(java.util.Iterator) QueryResults(com.baidu.hugegraph.backend.query.QueryResults) FlatMapperIterator(com.baidu.hugegraph.iterator.FlatMapperIterator) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) EdgeStep(com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep) Set(java.util.Set) List(java.util.List) FilterIterator(com.baidu.hugegraph.iterator.FilterIterator) HugeGraph(com.baidu.hugegraph.HugeGraph) Id(com.baidu.hugegraph.backend.id.Id) E(com.baidu.hugegraph.util.E) MutableLong(org.apache.commons.lang.mutable.MutableLong) Edge(org.apache.tinkerpop.gremlin.structure.Edge) EdgeStep(com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep) 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 FlatMapperIterator

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

the class GraphTransaction method queryEdgesFromBackend.

protected Iterator<HugeEdge> queryEdgesFromBackend(Query query) {
    assert query.resultType().isEdge();
    QueryResults<BackendEntry> results = this.query(query);
    Iterator<BackendEntry> entries = results.iterator();
    Iterator<HugeEdge> edges = new FlatMapperIterator<>(entries, entry -> {
        // Edges are in a vertex
        HugeVertex vertex = this.parseEntry(entry);
        if (vertex == null) {
            return null;
        }
        if (query.idsSize() == 1) {
            assert vertex.getEdges().size() == 1;
        }
        /*
             * Copy to avoid ConcurrentModificationException when removing edge
             * because HugeEdge.remove() will update edges in owner vertex
             */
        return new ListIterator<>(ImmutableList.copyOf(vertex.getEdges()));
    });
    edges = this.filterExpiredResultFromFromBackend(query, edges);
    if (!this.store().features().supportsQuerySortByInputIds()) {
        // There is no id in BackendEntry, so sort after deserialization
        edges = results.keepInputOrderIfNeeded(edges);
    }
    return edges;
}
Also used : BackendEntry(com.baidu.hugegraph.backend.store.BackendEntry) FlatMapperIterator(com.baidu.hugegraph.iterator.FlatMapperIterator) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) ListIterator(com.baidu.hugegraph.iterator.ListIterator) HugeVertex(com.baidu.hugegraph.structure.HugeVertex)

Example 3 with FlatMapperIterator

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

the class FlatMapperIteratorTest method testFlatMapper.

@Test
public void testFlatMapper() {
    AtomicInteger keysCount = new AtomicInteger(0);
    Iterator<String> keys = DATA.keySet().iterator();
    Function<String, Iterator<Integer>> mapper = key -> {
        keysCount.incrementAndGet();
        return DATA.get(key).iterator();
    };
    Iterator<Integer> results = new FlatMapperIterator<>(keys, mapper);
    List<Integer> actual = new ArrayList<>();
    while (results.hasNext()) {
        actual.add(results.next());
    }
    List<Integer> expected = ImmutableList.of(11, 21, 22, 31, 32, 33, 41, 42, 43, 44);
    Assert.assertEquals(4, keysCount.get());
    Assert.assertEquals(expected, actual);
}
Also used : Iterator(java.util.Iterator) FlatMapperIterator(com.baidu.hugegraph.iterator.FlatMapperIterator) ImmutableMap(com.google.common.collect.ImmutableMap) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) CloseableItor(com.baidu.hugegraph.unit.iterator.ExtendableIteratorTest.CloseableItor) Test(org.junit.Test) Function(java.util.function.Function) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) NoSuchElementException(java.util.NoSuchElementException) Assert(com.baidu.hugegraph.testutil.Assert) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FlatMapperIterator(com.baidu.hugegraph.iterator.FlatMapperIterator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Iterator(java.util.Iterator) FlatMapperIterator(com.baidu.hugegraph.iterator.FlatMapperIterator) ArrayList(java.util.ArrayList) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Aggregations

FlatMapperIterator (com.baidu.hugegraph.iterator.FlatMapperIterator)3 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)2 Iterator (java.util.Iterator)2 List (java.util.List)2 HugeGraph (com.baidu.hugegraph.HugeGraph)1 Id (com.baidu.hugegraph.backend.id.Id)1 QueryResults (com.baidu.hugegraph.backend.query.QueryResults)1 BackendEntry (com.baidu.hugegraph.backend.store.BackendEntry)1 FilterIterator (com.baidu.hugegraph.iterator.FilterIterator)1 ListIterator (com.baidu.hugegraph.iterator.ListIterator)1 HugeVertex (com.baidu.hugegraph.structure.HugeVertex)1 Assert (com.baidu.hugegraph.testutil.Assert)1 EdgeStep (com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep)1 BaseUnitTest (com.baidu.hugegraph.unit.BaseUnitTest)1 CloseableItor (com.baidu.hugegraph.unit.iterator.ExtendableIteratorTest.CloseableItor)1 E (com.baidu.hugegraph.util.E)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1