From d5dd29e23f9c9a05127a381aeb0b473532afe5a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xavier=20L=C3=A9aut=C3=A9?= Date: Tue, 28 May 2013 14:50:26 -0700 Subject: [PATCH] enable snappy compression for memcached results --- client/pom.xml | 4 ++ .../druid/client/cache/MemcachedCache.java | 7 +-- .../druid/client/cache/SnappyTranscoder.java | 50 +++++++++++++++++++ pom.xml | 6 +++ 4 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 client/src/main/java/com/metamx/druid/client/cache/SnappyTranscoder.java diff --git a/client/pom.xml b/client/pom.xml index d0ae4006937..b17cdbc4017 100644 --- a/client/pom.xml +++ b/client/pom.xml @@ -181,6 +181,10 @@ com.metamx bytebuffer-collections + + org.xerial.snappy + snappy-java + diff --git a/client/src/main/java/com/metamx/druid/client/cache/MemcachedCache.java b/client/src/main/java/com/metamx/druid/client/cache/MemcachedCache.java index 9dca20d9fb8..e5d478e341e 100644 --- a/client/src/main/java/com/metamx/druid/client/cache/MemcachedCache.java +++ b/client/src/main/java/com/metamx/druid/client/cache/MemcachedCache.java @@ -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( diff --git a/client/src/main/java/com/metamx/druid/client/cache/SnappyTranscoder.java b/client/src/main/java/com/metamx/druid/client/cache/SnappyTranscoder.java new file mode 100644 index 00000000000..d83e740f810 --- /dev/null +++ b/client/src/main/java/com/metamx/druid/client/cache/SnappyTranscoder.java @@ -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; + } +} diff --git a/pom.xml b/pom.xml index dce1e771eef..0c97dd87ed7 100644 --- a/pom.xml +++ b/pom.xml @@ -320,6 +320,12 @@ jackson-mapper-asl 1.9.11 + + org.xerial.snappy + snappy-java + 1.0.5 + +