fix histogram aggregator cache key

This commit is contained in:
Xavier Léauté 2015-07-15 13:02:42 -07:00
parent 850460446d
commit a15a2c4047
2 changed files with 26 additions and 1 deletions

View File

@ -153,7 +153,14 @@ public class HistogramAggregatorFactory implements AggregatorFactory
public byte[] getCacheKey()
{
byte[] fieldNameBytes = StringUtils.toUtf8(fieldName);
return ByteBuffer.allocate(1 + fieldNameBytes.length).put(CACHE_TYPE_ID).put(fieldNameBytes).array();
ByteBuffer buf = ByteBuffer
.allocate(1 + fieldNameBytes.length + Floats.BYTES * breaks.length)
.put(CACHE_TYPE_ID)
.put(fieldNameBytes)
.put((byte)0xFF);
buf.asFloatBuffer().put(breaks);
return buf.array();
}
@Override

View File

@ -17,12 +17,15 @@
package io.druid.query.aggregation;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import io.druid.jackson.DefaultObjectMapper;
import org.junit.Assert;
import org.junit.Test;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
public class HistogramAggregatorTest
{
@ -32,6 +35,21 @@ public class HistogramAggregatorTest
selector.increment();
}
@Test
public void testSerde() throws Exception
{
final DefaultObjectMapper objectMapper = new DefaultObjectMapper();
String json0 = "{\"type\": \"histogram\", \"name\": \"billy\", \"fieldName\": \"nilly\"}";
HistogramAggregatorFactory agg0 = objectMapper.readValue(json0, HistogramAggregatorFactory.class);
Assert.assertEquals(ImmutableList.of(), agg0.getBreaks());
String aggSpecJson = "{\"type\": \"histogram\", \"name\": \"billy\", \"fieldName\": \"nilly\", \"breaks\": [ -1, 2, 3.0 ]}";
HistogramAggregatorFactory agg = objectMapper.readValue(aggSpecJson, HistogramAggregatorFactory.class);
Assert.assertEquals(new HistogramAggregatorFactory("billy", "nilly", Arrays.asList(-1f, 2f, 3.0f)), agg);
Assert.assertEquals(agg, objectMapper.readValue(objectMapper.writeValueAsBytes(agg), HistogramAggregatorFactory.class));
}
@Test
public void testAggregate() throws Exception {
final float[] values = {0.55f, 0.27f, -0.3f, -.1f, -0.8f, -.7f, -.5f, 0.25f, 0.1f, 2f, -3f};