diff --git a/processing/src/main/java/io/druid/segment/filter/BoundFilter.java b/processing/src/main/java/io/druid/segment/filter/BoundFilter.java index b91806b684e..2d2e01e8909 100644 --- a/processing/src/main/java/io/druid/segment/filter/BoundFilter.java +++ b/processing/src/main/java/io/druid/segment/filter/BoundFilter.java @@ -34,35 +34,51 @@ public class BoundFilter extends DimensionPredicateFilter super( boundDimFilter.getDimension(), new Predicate() { + private volatile Predicate predicate; + @Override public boolean apply(String input) { - if (input == null) { - return false; - } - Comparator comparator; - if (boundDimFilter.isAlphaNumeric()) { - comparator = new AlphaNumericTopNMetricSpec(null).getComparator(null, null); - } else { - comparator = new LexicographicTopNMetricSpec(null).getComparator(null, null); - } + return function().apply(input); + } - int lowerComparing = 1; - int upperComparing = 1; - if (boundDimFilter.getLower() != null) { - lowerComparing = comparator.compare(input, boundDimFilter.getLower()); + private Predicate function() + { + if (predicate == null) { + final Comparator comparator; + if (boundDimFilter.isAlphaNumeric()) { + comparator = new AlphaNumericTopNMetricSpec(null).getComparator(null, null); + } else { + comparator = new LexicographicTopNMetricSpec(null).getComparator(null, null); + } + predicate = new Predicate() + { + @Override + public boolean apply(String input) + { + if (input == null) { + return false; + } + int lowerComparing = 1; + int upperComparing = 1; + if (boundDimFilter.getLower() != null) { + lowerComparing = comparator.compare(input, boundDimFilter.getLower()); + } + if (boundDimFilter.getUpper() != null) { + upperComparing = comparator.compare(boundDimFilter.getUpper(), input); + } + if (boundDimFilter.isLowerStrict() && boundDimFilter.isUpperStrict()) { + return ((lowerComparing > 0)) && (upperComparing > 0); + } else if (boundDimFilter.isLowerStrict()) { + return (lowerComparing > 0) && (upperComparing >= 0); + } else if (boundDimFilter.isUpperStrict()) { + return (lowerComparing >= 0) && (upperComparing > 0); + } + return (lowerComparing >= 0) && (upperComparing >= 0); + } + }; } - if (boundDimFilter.getUpper() != null) { - upperComparing = comparator.compare(boundDimFilter.getUpper(), input); - } - if (boundDimFilter.isLowerStrict() && boundDimFilter.isUpperStrict()) { - return ((lowerComparing > 0)) && ( upperComparing > 0); - } else if (boundDimFilter.isLowerStrict()) { - return (lowerComparing > 0) && (upperComparing >= 0); - } else if (boundDimFilter.isUpperStrict()) { - return (lowerComparing >= 0) && (upperComparing > 0); - } - return (lowerComparing >= 0) && (upperComparing >= 0); + return predicate; } } );