enable snappy compression for memcached results

This commit is contained in:
Xavier Léauté 2013-05-28 14:50:26 -07:00
parent 67be515db6
commit d5dd29e23f
4 changed files with 64 additions and 3 deletions

View File

@ -181,6 +181,10 @@
<groupId>com.metamx</groupId> <groupId>com.metamx</groupId>
<artifactId>bytebuffer-collections</artifactId> <artifactId>bytebuffer-collections</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.xerial.snappy</groupId>
<artifactId>snappy-java</artifactId>
</dependency>
<!-- Tests --> <!-- Tests -->
<dependency> <dependency>

View File

@ -53,9 +53,10 @@ public class MemcachedCache implements Cache
public static MemcachedCache create(final MemcachedCacheConfig config) public static MemcachedCache create(final MemcachedCacheConfig config)
{ {
try { try {
SerializingTranscoder transcoder = new SerializingTranscoder(config.getMaxObjectSize()); SnappyTranscoder transcoder = new SnappyTranscoder(config.getMaxObjectSize());
// disable compression
transcoder.setCompressionThreshold(Integer.MAX_VALUE); // always use compression
transcoder.setCompressionThreshold(0);
return new MemcachedCache( return new MemcachedCache(
new MemcachedClient( new MemcachedClient(

View File

@ -0,0 +1,50 @@
package com.metamx.druid.client.cache;
import net.spy.memcached.transcoders.SerializingTranscoder;
import org.xerial.snappy.Snappy;
import java.io.IOException;
public class SnappyTranscoder extends SerializingTranscoder
{
public SnappyTranscoder()
{
super();
}
public SnappyTranscoder(int max)
{
super(max);
}
@Override
protected byte[] compress(byte[] in)
{
if (in == null) {
throw new NullPointerException("Can't compress null");
}
byte[] out;
try {
out = Snappy.compress(in);
} catch(IOException e) {
throw new RuntimeException("IO exception compressing data", e);
}
getLogger().debug("Compressed %d bytes to %d", in.length, out.length);
return out;
}
@Override
protected byte[] decompress(byte[] in)
{
byte[] out = null;
if(in != null) {
try {
out = Snappy.uncompress(in);
} catch (IOException e) {
getLogger().warn("Failed to decompress data", e);
}
}
return out == null ? null : out;
}
}

View File

@ -320,6 +320,12 @@
<artifactId>jackson-mapper-asl</artifactId> <artifactId>jackson-mapper-asl</artifactId>
<version>1.9.11</version> <version>1.9.11</version>
</dependency> </dependency>
<dependency>
<groupId>org.xerial.snappy</groupId>
<artifactId>snappy-java</artifactId>
<version>1.0.5</version>
</dependency>
<!-- Test Scope --> <!-- Test Scope -->
<dependency> <dependency>