Last active
September 16, 2020 09:21
-
-
Save markusdybeck/d7434d195987fea985c8603c337f3af7 to your computer and use it in GitHub Desktop.
Spliterator that handles EsIterators.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EsSpliterator<T> implements Spliterator<Collection<T>> { | |
private final List<EsIterator<T>> iterators = new ArrayList<>(); | |
// constructor omitted | |
private static <T> EsSpliterator<T> from(String index, QueryBuilder query, int batchSize, Class<T> documentClass, int maxSlices) { | |
List<EsIterator<T>> iterators = IntStream.range(0, maxSlices) | |
.mapToObj(i -> new EsIterator<>(index, query, batchSize, documentClass, i, maxSlices)) | |
.collect(toList()); | |
return new EsSpliterator<>(iterators); | |
} | |
@Override | |
public boolean tryAdvance(Consumer<? super Collection<T>> action) { | |
List<EsIterator<T>> hasNextIterators = iterators.stream() | |
.filter(EsIterator::hasNext) | |
.collect(toList()); | |
if (!hasNextIterators.isEmpty()) { | |
hasNextIterators.forEach(it -> action.accept(it.next())); | |
return true; | |
} | |
return false; | |
} | |
@Override | |
public Spliterator<Collection<T>> trySplit() { | |
if (iterators.size() >= 2) { | |
List<EsIterator<T>> sublist = iterators.stream() | |
.limit(iterators.size() / 2) | |
.collect(toList()); | |
iterators.removeAll(sublist); | |
return new EsSpliterator<>(sublist); | |
} | |
return null; | |
} | |
@Override | |
public long estimateSize() { | |
return iterators.stream().map(EsIterator::getTotalHits).mapToLong(l -> l).sum(); | |
} | |
@Override | |
public int characteristics() { | |
return SIZED; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment