From 91308f65cbd8506c4e6dc3a627e78c307c89f74c Mon Sep 17 00:00:00 2001 From: Pavel Ershov Date: Thu, 15 Apr 2021 17:53:35 +0300 Subject: [PATCH] Do not store distinct elements when single iterator processed Signed-off-by: Pavel Ershov --- .../graphdb/util/MultiDistinctOrderedIterator.java | 6 +++++- .../graphdb/util/MultiDistinctUnorderedIterator.java | 4 +++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/janusgraph-core/src/main/java/org/janusgraph/graphdb/util/MultiDistinctOrderedIterator.java b/janusgraph-core/src/main/java/org/janusgraph/graphdb/util/MultiDistinctOrderedIterator.java index af2e0dede0..f3b9d00f24 100644 --- a/janusgraph-core/src/main/java/org/janusgraph/graphdb/util/MultiDistinctOrderedIterator.java +++ b/janusgraph-core/src/main/java/org/janusgraph/graphdb/util/MultiDistinctOrderedIterator.java @@ -38,10 +38,12 @@ public class MultiDistinctOrderedIterator implements Closeabl private final TreeMap currentElements; private final Set allElements = new HashSet<>(); private final Integer limit; + private final boolean singleIterator; private long count = 0; public MultiDistinctOrderedIterator(final Integer lowLimit, final Integer highLimit, final List> iterators, final List orders) { this.limit = highLimit; + this.singleIterator = iterators.size() == 1; final List> comp = new ArrayList<>(); orders.forEach(o -> comp.add(new ElementValueComparator(o.key, o.order))); Comparator comparator = new MultiComparator<>(comp); @@ -73,7 +75,9 @@ public boolean hasNext() { if (element != null) { values.put(i, element); currentElements.put(element, i); - allElements.add(element.id()); + if (!singleIterator) { + allElements.add(element.id()); + } } } } diff --git a/janusgraph-core/src/main/java/org/janusgraph/graphdb/util/MultiDistinctUnorderedIterator.java b/janusgraph-core/src/main/java/org/janusgraph/graphdb/util/MultiDistinctUnorderedIterator.java index f9d0f28a0d..e58079cbf5 100644 --- a/janusgraph-core/src/main/java/org/janusgraph/graphdb/util/MultiDistinctUnorderedIterator.java +++ b/janusgraph-core/src/main/java/org/janusgraph/graphdb/util/MultiDistinctUnorderedIterator.java @@ -28,12 +28,14 @@ public class MultiDistinctUnorderedIterator extends Closeable private final Set allElements = new HashSet<>(); private final CloseableIterator iterator; private final int limit; + private final boolean singleIterator; private long count; public MultiDistinctUnorderedIterator(final int lowLimit, final int highLimit, final List> iterators) { Objects.requireNonNull(iterators); iterator = CloseableIteratorUtils.concat(iterators); limit = highLimit; + singleIterator = iterators.size() == 1; long i = 0; while (i < lowLimit && hasNext()) { @@ -47,7 +49,7 @@ protected E computeNext() { if (count < limit) { while (iterator.hasNext()) { E elem = iterator.next(); - if (allElements.add(elem.id())) { + if (singleIterator || allElements.add(elem.id())) { count++; return elem; }