mirror of https://github.com/apache/druid.git
Merge pull request #155 from metamx/snappy-cache-compression
enable cache compression for memcached
This commit is contained in:
commit
809a958513
|
@ -181,6 +181,10 @@
|
|||
<groupId>com.metamx</groupId>
|
||||
<artifactId>bytebuffer-collections</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.jpountz.lz4</groupId>
|
||||
<artifactId>lz4</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Tests -->
|
||||
<dependency>
|
||||
|
|
|
@ -0,0 +1,81 @@
|
|||
/*
|
||||
* Druid - a distributed column store.
|
||||
* Copyright (C) 2013 Metamarkets Group Inc.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
package com.metamx.druid.client.cache;
|
||||
|
||||
import com.google.common.primitives.Ints;
|
||||
import net.jpountz.lz4.LZ4Compressor;
|
||||
import net.jpountz.lz4.LZ4Decompressor;
|
||||
import net.jpountz.lz4.LZ4Factory;
|
||||
import net.spy.memcached.transcoders.SerializingTranscoder;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class LZ4Transcoder extends SerializingTranscoder
|
||||
{
|
||||
|
||||
private final LZ4Factory lz4Factory;
|
||||
|
||||
public LZ4Transcoder()
|
||||
{
|
||||
super();
|
||||
lz4Factory = LZ4Factory.fastestJavaInstance();
|
||||
}
|
||||
|
||||
public LZ4Transcoder(int max)
|
||||
{
|
||||
super(max);
|
||||
lz4Factory = LZ4Factory.fastestJavaInstance();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected byte[] compress(byte[] in)
|
||||
{
|
||||
if (in == null) {
|
||||
throw new NullPointerException("Can't compress null");
|
||||
}
|
||||
|
||||
LZ4Compressor compressor = lz4Factory.fastCompressor();
|
||||
|
||||
byte[] out = new byte[compressor.maxCompressedLength(in.length)];
|
||||
int compressedLength = compressor.compress(in, 0, in.length, out, 0);
|
||||
|
||||
getLogger().debug("Compressed %d bytes to %d", in.length, compressedLength);
|
||||
|
||||
return ByteBuffer.allocate(Ints.BYTES + compressedLength)
|
||||
.putInt(in.length)
|
||||
.put(out, 0, compressedLength)
|
||||
.array();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected byte[] decompress(byte[] in)
|
||||
{
|
||||
byte[] out = null;
|
||||
if(in != null) {
|
||||
LZ4Decompressor decompressor = lz4Factory.decompressor();
|
||||
|
||||
int size = ByteBuffer.wrap(in).getInt();
|
||||
|
||||
out = new byte[size];
|
||||
decompressor.decompress(in, Ints.BYTES, out, 0, out.length);
|
||||
}
|
||||
return out == null ? null : out;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
LZ4Transcoder transcoder = new LZ4Transcoder(config.getMaxObjectSize());
|
||||
|
||||
// always use compression
|
||||
transcoder.setCompressionThreshold(0);
|
||||
|
||||
return new MemcachedCache(
|
||||
new MemcachedClient(
|
||||
|
|
|
@ -52,8 +52,8 @@ import java.util.concurrent.TimeoutException;
|
|||
*/
|
||||
public class MemcachedCacheTest
|
||||
{
|
||||
private static final byte[] HI = "hi".getBytes();
|
||||
private static final byte[] HO = "ho".getBytes();
|
||||
private static final byte[] HI = "hiiiiiiiiiiiiiiiiiii".getBytes();
|
||||
private static final byte[] HO = "hooooooooooooooooooo".getBytes();
|
||||
private MemcachedCache cache;
|
||||
|
||||
@Before
|
||||
|
@ -124,7 +124,13 @@ public class MemcachedCacheTest
|
|||
class MockMemcachedClient implements MemcachedClientIF
|
||||
{
|
||||
private final ConcurrentMap<String, CachedData> theMap = new ConcurrentHashMap<String, CachedData>();
|
||||
private final Transcoder<Object> transcoder = new SerializingTranscoder();
|
||||
private final SerializingTranscoder transcoder;
|
||||
|
||||
public MockMemcachedClient()
|
||||
{
|
||||
transcoder = new LZ4Transcoder();
|
||||
transcoder.setCompressionThreshold(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<SocketAddress> getAvailableServers()
|
||||
|
|
Loading…
Reference in New Issue