From 0107d1d43ad5c5756cd03ea82a1aa5b6535ebc1e Mon Sep 17 00:00:00 2001 From: leventov Date: Wed, 7 Dec 2016 01:31:37 -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 --- .../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 fb26bef42b41..eea3d1cf089c 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 java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.NoSuchElementException; /** */ @@ -141,26 +142,42 @@ public Iterator iterator() { 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