Avoid creating multiple NoneShardSpec objects (#2855)

* Avoid creating multiple NoneShardSpec objects
* deprecate NoneShardSpec constructor
This commit is contained in:
Nishant 2016-04-19 23:00:14 +05:30 committed by Xavier Léauté
parent dbf63f738f
commit f80a5dc4ef
3 changed files with 30 additions and 7 deletions

View File

@ -126,7 +126,7 @@ public class DataSegment implements Comparable<DataSegment>
this.metrics = metrics == null
? ImmutableList.<String>of()
: ImmutableList.copyOf(Iterables.transform(Iterables.filter(metrics, nonEmpty), internFun));
this.shardSpec = (shardSpec == null) ? new NoneShardSpec() : shardSpec;
this.shardSpec = (shardSpec == null) ? NoneShardSpec.instance() : shardSpec;
this.binaryVersion = binaryVersion;
this.size = size;
@ -328,7 +328,7 @@ public class DataSegment implements Comparable<DataSegment>
this.loadSpec = ImmutableMap.of();
this.dimensions = ImmutableList.of();
this.metrics = ImmutableList.of();
this.shardSpec = new NoneShardSpec();
this.shardSpec = NoneShardSpec.instance();
this.size = -1;
}

View File

@ -19,19 +19,26 @@
package io.druid.timeline.partition;
import com.google.common.base.Preconditions;
import com.google.common.collect.Iterables;
import com.metamx.common.ISE;
import com.fasterxml.jackson.annotation.JsonCreator;
import io.druid.data.input.InputRow;
import java.util.Collections;
import java.util.List;
import java.util.Set;
/**
*/
public class NoneShardSpec implements ShardSpec
{
private final static NoneShardSpec INSTANCE = new NoneShardSpec();
@JsonCreator
public static NoneShardSpec instance() { return INSTANCE; }
@Deprecated
// Use NoneShardSpec.instance() instead
public NoneShardSpec(){
}
@Override
public <T> PartitionChunk<T> createChunk(T obj)
{

View File

@ -1,5 +1,8 @@
package io.druid.timeline.partition;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.druid.TestObjectMapper;
import org.junit.Assert;
import org.junit.Test;
@ -13,4 +16,17 @@ public class NoneShardSpecTest
Assert.assertEquals(one, two);
Assert.assertEquals(one.hashCode(), two.hashCode());
}
@Test
public void testSerde() throws Exception
{
final NoneShardSpec one = NoneShardSpec.instance();
ObjectMapper mapper = new TestObjectMapper();
NoneShardSpec serde1 = mapper.readValue(mapper.writeValueAsString(one), NoneShardSpec.class);
NoneShardSpec serde2 = mapper.readValue(mapper.writeValueAsString(one), NoneShardSpec.class);
// Serde should return same object instead of creating new one every time.
Assert.assertTrue(serde1 == serde2);
Assert.assertTrue(one == serde1);
}
}