mirror of
https://github.com/usatiuk/dhfs.git
synced 2025-10-29 04:57:48 +01:00
Objects: simplify merging iterator even more^2
initialMaxValue streams can be simplified too
This commit is contained in:
@@ -4,7 +4,6 @@ import io.quarkus.logging.Log;
|
|||||||
import org.apache.commons.lang3.tuple.Pair;
|
import org.apache.commons.lang3.tuple.Pair;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
import java.util.stream.IntStream;
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
public class MergingKvIterator<K extends Comparable<K>, V> extends ReversibleKvIterator<K, V> {
|
public class MergingKvIterator<K extends Comparable<K>, V> extends ReversibleKvIterator<K, V> {
|
||||||
@@ -27,18 +26,34 @@ public class MergingKvIterator<K extends Comparable<K>, V> extends ReversibleKvI
|
|||||||
// We have a bunch of iterators that have given us theirs "greatest LT/LE key"
|
// We have a bunch of iterators that have given us theirs "greatest LT/LE key"
|
||||||
// now we need to pick the greatest of those to start with
|
// now we need to pick the greatest of those to start with
|
||||||
// But if some of them don't have a lesser key, we need to pick the smallest of those
|
// But if some of them don't have a lesser key, we need to pick the smallest of those
|
||||||
var found = _iterators.keySet().stream()
|
|
||||||
.filter(CloseableKvIterator::hasNext)
|
K greatestLess = null;
|
||||||
.map((i) -> {
|
K smallestMore = null;
|
||||||
var peeked = i.peekNextKey();
|
|
||||||
// Log.warnv("peeked: {0}, from {1}", peeked, i.getClass());
|
for (var it : _iterators.keySet()) {
|
||||||
return peeked;
|
if (it.hasNext()) {
|
||||||
}).distinct().collect(Collectors.partitioningBy(e -> startType == IteratorStart.LE ? e.compareTo(startKey) <= 0 : e.compareTo(startKey) < 0));
|
var peeked = it.peekNextKey();
|
||||||
|
if (startType == IteratorStart.LE ? peeked.compareTo(startKey) <= 0 : peeked.compareTo(startKey) < 0) {
|
||||||
|
if (greatestLess == null || peeked.compareTo(greatestLess) > 0) {
|
||||||
|
greatestLess = peeked;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (smallestMore == null || peeked.compareTo(smallestMore) < 0) {
|
||||||
|
smallestMore = peeked;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
K initialMaxValue;
|
K initialMaxValue;
|
||||||
if (!found.get(true).isEmpty())
|
if (greatestLess != null)
|
||||||
initialMaxValue = found.get(true).stream().max(Comparator.naturalOrder()).orElse(null);
|
initialMaxValue = greatestLess;
|
||||||
else
|
else
|
||||||
initialMaxValue = found.get(false).stream().min(Comparator.naturalOrder()).orElse(null);
|
initialMaxValue = smallestMore;
|
||||||
|
|
||||||
|
if (initialMaxValue == null) {
|
||||||
|
// Empty iterators
|
||||||
|
}
|
||||||
|
|
||||||
for (var iterator : _iterators.keySet()) {
|
for (var iterator : _iterators.keySet()) {
|
||||||
while (iterator.hasNext() && iterator.peekNextKey().compareTo(initialMaxValue) < 0) {
|
while (iterator.hasNext() && iterator.peekNextKey().compareTo(initialMaxValue) < 0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user