|
| 1 | +/* |
| 2 | + * Copyright (C) 2017-2017 DataStax Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.datastax.oss.driver.internal.core.protocol; |
| 17 | + |
| 18 | +import com.datastax.oss.driver.api.core.config.DriverOption; |
| 19 | +import com.datastax.oss.driver.api.core.context.DriverContext; |
| 20 | +import com.datastax.oss.protocol.internal.Compressor; |
| 21 | +import io.netty.buffer.ByteBuf; |
| 22 | +import java.nio.ByteBuffer; |
| 23 | +import net.jpountz.lz4.LZ4Compressor; |
| 24 | +import net.jpountz.lz4.LZ4Factory; |
| 25 | +import net.jpountz.lz4.LZ4FastDecompressor; |
| 26 | +import org.slf4j.Logger; |
| 27 | +import org.slf4j.LoggerFactory; |
| 28 | + |
| 29 | +public class Lz4Compressor extends ByteBufCompressor { |
| 30 | + |
| 31 | + private static final Logger LOG = LoggerFactory.getLogger(Lz4Compressor.class); |
| 32 | + |
| 33 | + private final LZ4Compressor compressor; |
| 34 | + private final LZ4FastDecompressor decompressor; |
| 35 | + |
| 36 | + public Lz4Compressor(DriverContext context, @SuppressWarnings("unused") DriverOption configRoot) { |
| 37 | + try { |
| 38 | + LZ4Factory lz4Factory = LZ4Factory.fastestInstance(); |
| 39 | + LOG.info("[{}] Using {}", context.clusterName(), lz4Factory.toString()); |
| 40 | + this.compressor = lz4Factory.fastCompressor(); |
| 41 | + this.decompressor = lz4Factory.fastDecompressor(); |
| 42 | + } catch (NoClassDefFoundError e) { |
| 43 | + throw new IllegalStateException( |
| 44 | + "Error initializing compressor, make sure that the LZ4 library is in the classpath " |
| 45 | + + "(the driver declares it as an optional dependency, " |
| 46 | + + "so you need to declare it explicitly)", |
| 47 | + e); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public String algorithm() { |
| 53 | + return "lz4"; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + protected ByteBuf compressDirect(ByteBuf input) { |
| 58 | + int maxCompressedLength = compressor.maxCompressedLength(input.readableBytes()); |
| 59 | + // If the input is direct we will allocate a direct output buffer as well as this will allow us |
| 60 | + // to use LZ4Compressor.compress and so eliminate memory copies. |
| 61 | + ByteBuf output = input.alloc().directBuffer(4 + maxCompressedLength); |
| 62 | + try { |
| 63 | + ByteBuffer in = inputNioBuffer(input); |
| 64 | + // Increase reader index. |
| 65 | + input.readerIndex(input.writerIndex()); |
| 66 | + |
| 67 | + output.writeInt(in.remaining()); |
| 68 | + |
| 69 | + ByteBuffer out = outputNioBuffer(output); |
| 70 | + int written = |
| 71 | + compressor.compress( |
| 72 | + in, in.position(), in.remaining(), out, out.position(), out.remaining()); |
| 73 | + // Set the writer index so the amount of written bytes is reflected |
| 74 | + output.writerIndex(output.writerIndex() + written); |
| 75 | + } catch (Exception e) { |
| 76 | + // release output buffer so we not leak and rethrow exception. |
| 77 | + output.release(); |
| 78 | + throw e; |
| 79 | + } |
| 80 | + return output; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + protected ByteBuf compressHeap(ByteBuf input) { |
| 85 | + int maxCompressedLength = compressor.maxCompressedLength(input.readableBytes()); |
| 86 | + |
| 87 | + // Not a direct buffer so use byte arrays... |
| 88 | + int inOffset = input.arrayOffset() + input.readerIndex(); |
| 89 | + byte[] in = input.array(); |
| 90 | + int len = input.readableBytes(); |
| 91 | + // Increase reader index. |
| 92 | + input.readerIndex(input.writerIndex()); |
| 93 | + |
| 94 | + // Allocate a heap buffer from the ByteBufAllocator as we may use a PooledByteBufAllocator and |
| 95 | + // so can eliminate the overhead of allocate a new byte[]. |
| 96 | + ByteBuf output = input.alloc().heapBuffer(4 + maxCompressedLength); |
| 97 | + try { |
| 98 | + output.writeInt(len); |
| 99 | + // calculate the correct offset. |
| 100 | + int offset = output.arrayOffset() + output.writerIndex(); |
| 101 | + byte[] out = output.array(); |
| 102 | + int written = compressor.compress(in, inOffset, len, out, offset); |
| 103 | + |
| 104 | + // Set the writer index so the amount of written bytes is reflected |
| 105 | + output.writerIndex(output.writerIndex() + written); |
| 106 | + } catch (Exception e) { |
| 107 | + // release output buffer so we not leak and rethrow exception. |
| 108 | + output.release(); |
| 109 | + throw e; |
| 110 | + } |
| 111 | + return output; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + protected ByteBuf decompressDirect(ByteBuf input) { |
| 116 | + // If the input is direct we will allocate a direct output buffer as well as this will allow us |
| 117 | + // to use LZ4Compressor.decompress and so eliminate memory copies. |
| 118 | + int readable = input.readableBytes(); |
| 119 | + int uncompressedLength = input.readInt(); |
| 120 | + ByteBuffer in = inputNioBuffer(input); |
| 121 | + // Increase reader index. |
| 122 | + input.readerIndex(input.writerIndex()); |
| 123 | + ByteBuf output = input.alloc().directBuffer(uncompressedLength); |
| 124 | + try { |
| 125 | + ByteBuffer out = outputNioBuffer(output); |
| 126 | + int read = decompressor.decompress(in, in.position(), out, out.position(), out.remaining()); |
| 127 | + if (read != readable - 4) { |
| 128 | + throw new IllegalArgumentException("Compressed lengths mismatch"); |
| 129 | + } |
| 130 | + |
| 131 | + // Set the writer index so the amount of written bytes is reflected |
| 132 | + output.writerIndex(output.writerIndex() + uncompressedLength); |
| 133 | + } catch (Exception e) { |
| 134 | + // release output buffer so we not leak and rethrow exception. |
| 135 | + output.release(); |
| 136 | + throw e; |
| 137 | + } |
| 138 | + return output; |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + protected ByteBuf decompressHeap(ByteBuf input) { |
| 143 | + // Not a direct buffer so use byte arrays... |
| 144 | + byte[] in = input.array(); |
| 145 | + int len = input.readableBytes(); |
| 146 | + int uncompressedLength = input.readInt(); |
| 147 | + int inOffset = input.arrayOffset() + input.readerIndex(); |
| 148 | + // Increase reader index. |
| 149 | + input.readerIndex(input.writerIndex()); |
| 150 | + |
| 151 | + // Allocate a heap buffer from the ByteBufAllocator as we may use a PooledByteBufAllocator and |
| 152 | + // so can eliminate the overhead of allocate a new byte[]. |
| 153 | + ByteBuf output = input.alloc().heapBuffer(uncompressedLength); |
| 154 | + try { |
| 155 | + int offset = output.arrayOffset() + output.writerIndex(); |
| 156 | + byte out[] = output.array(); |
| 157 | + int read = decompressor.decompress(in, inOffset, out, offset, uncompressedLength); |
| 158 | + if (read != len - 4) { |
| 159 | + throw new IllegalArgumentException("Compressed lengths mismatch"); |
| 160 | + } |
| 161 | + |
| 162 | + // Set the writer index so the amount of written bytes is reflected |
| 163 | + output.writerIndex(output.writerIndex() + uncompressedLength); |
| 164 | + } catch (Exception e) { |
| 165 | + // release output buffer so we not leak and rethrow exception. |
| 166 | + output.release(); |
| 167 | + throw e; |
| 168 | + } |
| 169 | + return output; |
| 170 | + } |
| 171 | +} |
0 commit comments