Skip to content

Commit

Permalink
Lazily initialize predicate for bound filter
Browse files Browse the repository at this point in the history
  • Loading branch information
navis committed Feb 29, 2016
1 parent 8f97b1e commit b1ff920
Showing 1 changed file with 40 additions and 24 deletions.
64 changes: 40 additions & 24 deletions processing/src/main/java/io/druid/segment/filter/BoundFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,35 +34,51 @@ public BoundFilter(final BoundDimFilter boundDimFilter)
super(
boundDimFilter.getDimension(), new Predicate<String>()
{
private volatile Predicate<String> predicate;

@Override
public boolean apply(String input)
{
if (input == null) {
return false;
}
Comparator<String> 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());
}
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);
private Predicate<String> function()
{
if (predicate == null) {
final Comparator<String> comparator;
if (boundDimFilter.isAlphaNumeric()) {
comparator = new AlphaNumericTopNMetricSpec(null).getComparator(null, null);
} else {
comparator = new LexicographicTopNMetricSpec(null).getComparator(null, null);
}
predicate = new Predicate<String>()
{
@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);
}
};
}
return (lowerComparing >= 0) && (upperComparing >= 0);
return predicate;
}
}
);
Expand Down

0 comments on commit b1ff920

Please sign in to comment.