mirror of https://github.com/apache/druid.git
avoid typecasts
This commit is contained in:
parent
10db94101f
commit
56912adccb
|
@ -22,6 +22,7 @@ package io.druid.query.aggregation;
|
|||
import com.google.common.base.Predicate;
|
||||
import com.google.common.collect.Iterables;
|
||||
import io.druid.segment.DimensionSelector;
|
||||
import io.druid.segment.data.IndexedInts;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
|
@ -30,9 +31,9 @@ public class FilteredAggregator implements Aggregator
|
|||
private final String name;
|
||||
private final DimensionSelector dimSelector;
|
||||
private final Aggregator delegate;
|
||||
private final Predicate<Integer> predicate;
|
||||
private final IntPredicate predicate;
|
||||
|
||||
public FilteredAggregator(String name, DimensionSelector dimSelector, Predicate<Integer> predicate, Aggregator delegate)
|
||||
public FilteredAggregator(String name, DimensionSelector dimSelector, IntPredicate predicate, Aggregator delegate)
|
||||
{
|
||||
this.name = name;
|
||||
this.dimSelector = dimSelector;
|
||||
|
@ -43,12 +44,13 @@ public class FilteredAggregator implements Aggregator
|
|||
@Override
|
||||
public void aggregate()
|
||||
{
|
||||
if (
|
||||
Iterables.any(
|
||||
dimSelector.getRow(), predicate
|
||||
)
|
||||
) {
|
||||
delegate.aggregate();
|
||||
final IndexedInts row = dimSelector.getRow();
|
||||
final int size = row.size();
|
||||
for (int i = 0; i < size; ++i) {
|
||||
if (predicate.apply(row.get(i))) {
|
||||
delegate.aggregate();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -21,8 +21,6 @@ package io.druid.query.aggregation;
|
|||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.base.Predicates;
|
||||
import io.druid.query.filter.DimFilter;
|
||||
import io.druid.query.filter.SelectorDimFilter;
|
||||
import io.druid.segment.ColumnSelectorFactory;
|
||||
|
@ -59,8 +57,16 @@ public class FilteredAggregatorFactory implements AggregatorFactory
|
|||
public Aggregator factorize(ColumnSelectorFactory metricFactory)
|
||||
{
|
||||
final Aggregator aggregator = delegate.factorize(metricFactory);
|
||||
final DimensionSelector dimSelector = metricFactory.makeDimensionSelector(((SelectorDimFilter)filter).getDimension());
|
||||
Predicate<Integer> predicate = Predicates.equalTo(dimSelector.lookupId(((SelectorDimFilter)filter).getValue()));
|
||||
final DimensionSelector dimSelector = metricFactory.makeDimensionSelector(((SelectorDimFilter) filter).getDimension());
|
||||
final int lookupId = dimSelector.lookupId(((SelectorDimFilter) filter).getValue());
|
||||
final IntPredicate predicate = new IntPredicate()
|
||||
{
|
||||
@Override
|
||||
public boolean apply(int value)
|
||||
{
|
||||
return lookupId == value;
|
||||
}
|
||||
};
|
||||
return new FilteredAggregator(name, dimSelector, predicate, aggregator);
|
||||
}
|
||||
|
||||
|
@ -68,8 +74,16 @@ public class FilteredAggregatorFactory implements AggregatorFactory
|
|||
public BufferAggregator factorizeBuffered(ColumnSelectorFactory metricFactory)
|
||||
{
|
||||
final BufferAggregator aggregator = delegate.factorizeBuffered(metricFactory);
|
||||
final DimensionSelector dimSelector = metricFactory.makeDimensionSelector(((SelectorDimFilter)filter).getDimension());
|
||||
Predicate<Integer> predicate = Predicates.equalTo(dimSelector.lookupId(((SelectorDimFilter)filter).getValue()));
|
||||
final DimensionSelector dimSelector = metricFactory.makeDimensionSelector(((SelectorDimFilter) filter).getDimension());
|
||||
final int lookupId = dimSelector.lookupId(((SelectorDimFilter) filter).getValue());
|
||||
final IntPredicate predicate = new IntPredicate()
|
||||
{
|
||||
@Override
|
||||
public boolean apply(int value)
|
||||
{
|
||||
return lookupId == value;
|
||||
}
|
||||
};
|
||||
return new FilteredBufferAggregator(dimSelector, predicate, aggregator);
|
||||
}
|
||||
|
||||
|
@ -122,10 +136,10 @@ public class FilteredAggregatorFactory implements AggregatorFactory
|
|||
byte[] filterCacheKey = filter.getCacheKey();
|
||||
byte[] aggregatorCacheKey = delegate.getCacheKey();
|
||||
return ByteBuffer.allocate(1 + filterCacheKey.length + aggregatorCacheKey.length)
|
||||
.put(CACHE_TYPE_ID)
|
||||
.put(filterCacheKey)
|
||||
.put(aggregatorCacheKey)
|
||||
.array();
|
||||
.put(CACHE_TYPE_ID)
|
||||
.put(filterCacheKey)
|
||||
.put(aggregatorCacheKey)
|
||||
.array();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -22,6 +22,7 @@ package io.druid.query.aggregation;
|
|||
import com.google.common.base.Predicate;
|
||||
import com.google.common.collect.Iterables;
|
||||
import io.druid.segment.DimensionSelector;
|
||||
import io.druid.segment.data.IndexedInts;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.nio.ByteBuffer;
|
||||
|
@ -29,10 +30,10 @@ import java.nio.ByteBuffer;
|
|||
public class FilteredBufferAggregator implements BufferAggregator
|
||||
{
|
||||
private final DimensionSelector dimSelector;
|
||||
private final Predicate<Integer> predicate;
|
||||
private final IntPredicate predicate;
|
||||
private final BufferAggregator delegate;
|
||||
|
||||
public FilteredBufferAggregator(DimensionSelector dimSelector, Predicate<Integer> predicate, BufferAggregator delegate)
|
||||
public FilteredBufferAggregator(DimensionSelector dimSelector, IntPredicate predicate, BufferAggregator delegate)
|
||||
{
|
||||
this.dimSelector = dimSelector;
|
||||
this.predicate = predicate;
|
||||
|
@ -48,19 +49,13 @@ public class FilteredBufferAggregator implements BufferAggregator
|
|||
@Override
|
||||
public void aggregate(ByteBuffer buf, int position)
|
||||
{
|
||||
if (
|
||||
Iterables.any(
|
||||
dimSelector.getRow(), new Predicate<Integer>()
|
||||
{
|
||||
@Override
|
||||
public boolean apply(@Nullable Integer input)
|
||||
{
|
||||
return predicate.apply(input);
|
||||
}
|
||||
}
|
||||
)
|
||||
) {
|
||||
delegate.aggregate(buf, position);
|
||||
final IndexedInts row = dimSelector.getRow();
|
||||
final int size = row.size();
|
||||
for (int i = 0; i < size; ++i) {
|
||||
if (predicate.apply(row.get(i))) {
|
||||
delegate.aggregate(buf, position);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* Druid - a distributed column store.
|
||||
* Copyright (C) 2012, 2013, 2014 Metamarkets Group Inc.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
package io.druid.query.aggregation;
|
||||
|
||||
/**
|
||||
* can be replaced with http://docs.oracle.com/javase/8/docs/api/java/util/function/IntPredicate.html
|
||||
* when druid moves to java 8.
|
||||
*/
|
||||
public interface IntPredicate
|
||||
{
|
||||
boolean apply(int value);
|
||||
}
|
Loading…
Reference in New Issue