mirror of https://github.com/apache/druid.git
enable snappy compression for memcached results
This commit is contained in:
parent
67be515db6
commit
d5dd29e23f
|
@ -181,6 +181,10 @@
|
|||
<groupId>com.metamx</groupId>
|
||||
<artifactId>bytebuffer-collections</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.xerial.snappy</groupId>
|
||||
<artifactId>snappy-java</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Tests -->
|
||||
<dependency>
|
||||
|
|
|
@ -53,9 +53,10 @@ public class MemcachedCache implements Cache
|
|||
public static MemcachedCache create(final MemcachedCacheConfig config)
|
||||
{
|
||||
try {
|
||||
SerializingTranscoder transcoder = new SerializingTranscoder(config.getMaxObjectSize());
|
||||
// disable compression
|
||||
transcoder.setCompressionThreshold(Integer.MAX_VALUE);
|
||||
SnappyTranscoder transcoder = new SnappyTranscoder(config.getMaxObjectSize());
|
||||
|
||||
// always use compression
|
||||
transcoder.setCompressionThreshold(0);
|
||||
|
||||
return new MemcachedCache(
|
||||
new MemcachedClient(
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue