Skip to content

Commit

Permalink
avoid typecasts
Browse files Browse the repository at this point in the history
  • Loading branch information
nishantmonu51 authored and xvrl committed Oct 21, 2014
1 parent 10db941 commit 56912ad
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;

Expand All @@ -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;
Expand All @@ -43,12 +44,13 @@ public FilteredAggregator(String name, DimensionSelector dimSelector, Predicate<
@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;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

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;
Expand Down Expand Up @@ -59,17 +57,33 @@ public FilteredAggregatorFactory(
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);
}

@Override
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);
}

Expand Down Expand Up @@ -122,10 +136,10 @@ public byte[] getCacheKey()
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@
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;

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;
Expand All @@ -48,19 +49,13 @@ public void init(ByteBuffer buf, int position)
@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;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 56912ad

Please sign in to comment.