From b1ff9208316b070205999066529a6ebd88bf7031 Mon Sep 17 00:00:00 2001 From: "navis.ryu" Date: Wed, 17 Feb 2016 22:50:26 +0900 Subject: [PATCH] Lazily initialize predicate for bound filter --- .../io/druid/segment/filter/BoundFilter.java | 64 ++++++++++++------- 1 file changed, 40 insertions(+), 24 deletions(-) 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; } } );