From dc8f814accab73f3b8fa318d8592d5c3e5b76bbf Mon Sep 17 00:00:00 2001 From: Roman Leventov Date: Wed, 7 Dec 2016 14:54:09 -0600 Subject: [PATCH] Optimize Iterator implementation inside Filters.matchPredicate() so that it doesn't emit empty bitmap in the end of the iteration, and make it to follow Iterator contract, that is throw NoSuchElementException from next() if there are no more bitmaps (#3754) --- .../java/io/druid/segment/filter/Filters.java | 37 ++++++++++++++----- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/processing/src/main/java/io/druid/segment/filter/Filters.java b/processing/src/main/java/io/druid/segment/filter/Filters.java index fb26bef42b4..eea3d1cf089 100644 --- a/processing/src/main/java/io/druid/segment/filter/Filters.java +++ b/processing/src/main/java/io/druid/segment/filter/Filters.java @@ -43,6 +43,7 @@ import io.druid.segment.data.Indexed; import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.NoSuchElementException; /** */ @@ -141,26 +142,42 @@ public class Filters { return new Iterator() { - int currIndex = 0; + private final int bitmapIndexCardinality = bitmapIndex.getCardinality(); + private int nextIndex = 0; + private ImmutableBitmap nextBitmap; + + { + findNextBitmap(); + } + + private void findNextBitmap() + { + while (nextIndex < bitmapIndexCardinality) { + if (predicate.apply(dimValues.get(nextIndex))) { + nextBitmap = bitmapIndex.getBitmap(nextIndex); + nextIndex++; + return; + } + nextIndex++; + } + nextBitmap = null; + } @Override public boolean hasNext() { - return currIndex < bitmapIndex.getCardinality(); + return nextBitmap != null; } @Override public ImmutableBitmap next() { - while (currIndex < bitmapIndex.getCardinality() && !predicate.apply(dimValues.get(currIndex))) { - currIndex++; + ImmutableBitmap bitmap = nextBitmap; + if (bitmap == null) { + throw new NoSuchElementException(); } - - if (currIndex == bitmapIndex.getCardinality()) { - return bitmapIndex.getBitmapFactory().makeEmptyImmutableBitmap(); - } - - return bitmapIndex.getBitmap(currIndex++); + findNextBitmap(); + return bitmap; } @Override