package org.apache.cassandra.transport;
import java.io.IOException;
import io.netty.buffer.ByteBuf;
import org.xerial.snappy.Snappy;
import org.xerial.snappy.SnappyError;
import net.jpountz.lz4.LZ4Factory;
import org.apache.cassandra.utils.JVMStabilityInspector;
public interface FrameCompressor
{
public Frame compress(Frame frame) throws IOException;
public Frame decompress(Frame frame) throws IOException;
public static class SnappyCompressor implements FrameCompressor
{
public static final SnappyCompressor instance;
static
{
SnappyCompressor i;
try
{
i = new SnappyCompressor();
}
catch (Exception e)
{
JVMStabilityInspector.inspectThrowable(e);
i = null;
}
catch (NoClassDefFoundError | SnappyError | UnsatisfiedLinkError e)
{
i = null;
}
instance = i;
}
private SnappyCompressor()
{
Snappy.getNativeLibraryVersion();
}
public Frame compress(Frame frame) throws IOException
{
byte[] input = CBUtil.readRawBytes(frame.body);
ByteBuf output = CBUtil.allocator.heapBuffer(Snappy.maxCompressedLength(input.length));
try
{
int written = Snappy.compress(input, 0, input.length, output.array(), output.arrayOffset());
output.writerIndex(written);
}
catch (final Throwable e)
{
output.release();
throw e;
}
finally
{
frame.release();
}
return frame.with(output);
}
public Frame decompress(Frame frame) throws IOException
{
byte[] input = CBUtil.readRawBytes(frame.body);
if (!Snappy.isValidCompressedBuffer(input, 0, input.length))
throw new ProtocolException("Provided frame does not appear to be Snappy compressed");
ByteBuf output = CBUtil.allocator.heapBuffer(Snappy.uncompressedLength(input));
try
{
int size = Snappy.uncompress(input, 0, input.length, output.array(), output.arrayOffset());
output.writerIndex(size);
}
catch (final Throwable e)
{
output.release();
throw e;
}
finally
{
frame.release();
}
return frame.with(output);
}
}
public static class LZ4Compressor implements FrameCompressor
{
public static final LZ4Compressor instance = new LZ4Compressor();
private static final int INTEGER_BYTES = 4;
private final net.jpountz.lz4.LZ4Compressor compressor;
private final net.jpountz.lz4.LZ4Decompressor decompressor;
private LZ4Compressor()
{
final LZ4Factory lz4Factory = LZ4Factory.fastestInstance();
compressor = lz4Factory.fastCompressor();
decompressor = lz4Factory.decompressor();
}
public Frame compress(Frame frame) throws IOException
{
byte[] input = CBUtil.readRawBytes(frame.body);
int maxCompressedLength = compressor.maxCompressedLength(input.length);
ByteBuf outputBuf = CBUtil.allocator.heapBuffer(INTEGER_BYTES + maxCompressedLength);
byte[] output = outputBuf.array();
int outputOffset = outputBuf.arrayOffset();
output[outputOffset + 0] = (byte) (input.length >>> 24);
output[outputOffset + 1] = (byte) (input.length >>> 16);
output[outputOffset + 2] = (byte) (input.length >>> 8);
output[outputOffset + 3] = (byte) (input.length);
try
{
int written = compressor.compress(input, 0, input.length, output, outputOffset + INTEGER_BYTES, maxCompressedLength);
outputBuf.writerIndex(INTEGER_BYTES + written);
return frame.with(outputBuf);
}
catch (final Throwable e)
{
outputBuf.release();
throw e;
}
finally
{
frame.release();
}
}
public Frame decompress(Frame frame) throws IOException
{
byte[] input = CBUtil.readRawBytes(frame.body);
int uncompressedLength = ((input[0] & 0xFF) << 24)
| ((input[1] & 0xFF) << 16)
| ((input[2] & 0xFF) << 8)
| ((input[3] & 0xFF));
ByteBuf output = CBUtil.allocator.heapBuffer(uncompressedLength);
try
{
int read = decompressor.decompress(input, INTEGER_BYTES, output.array(), output.arrayOffset(), uncompressedLength);
if (read != input.length - INTEGER_BYTES)
throw new IOException("Compressed lengths mismatch");
output.writerIndex(uncompressedLength);
return frame.with(output);
}
catch (final Throwable e)
{
output.release();
throw e;
}
finally
{
frame.release();
}
}
}
}