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();
}
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;
}
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);
}
Aggregations