kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | b37b671958a5a8c2f65f5cf4991a076dcee6648fcbeb5e84762f2e2698f16584 |
|---|---|
| Author | Norman Maurer <norman_maurer@apple.com> · 2016-07-12 07:04 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-09-20 16:19 -07:00 |
| Parents | 9945f5df57d4 |
1 file changed
modified driver-core/src/main/java/com/datastax/driver/core/FrameCompressor.java +109 −11
| @@ -16,6 +16,7 @@ | ||
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | 18 | import com.datastax.driver.core.exceptions.DriverInternalError; |
| 19 | +import io.netty.buffer.ByteBuf; | |
| 19 | 20 | import io.netty.buffer.Unpooled; |
| 20 | 21 | import net.jpountz.lz4.LZ4Factory; |
| 21 | 22 | import org.slf4j.Logger; |
| @@ -23,6 +24,7 @@ | ||
| 23 | 24 | import org.xerial.snappy.Snappy; |
| 24 | 25 | |
| 25 | 26 | import java.io.IOException; |
| 27 | +import java.nio.ByteBuffer; | |
| 26 | 28 | |
| 27 | 29 | abstract class FrameCompressor { |
| 28 | 30 | |
| @@ -58,23 +60,119 @@ | ||
| 58 | 60 | |
| 59 | 61 | @Override |
| 60 | 62 | public Frame compress(Frame frame) throws IOException { |
| 61 | - byte[] input = CBUtil.readRawBytes(frame.body); | |
| 62 | - byte[] output = new byte[Snappy.maxCompressedLength(input.length)]; | |
| 63 | - | |
| 64 | - int written = Snappy.compress(input, 0, input.length, output, 0); | |
| 65 | - return frame.with(Unpooled.wrappedBuffer(output, 0, written)); | |
| 63 | + ByteBuf input = frame.body; | |
| 64 | + int maxCompressedLength = Snappy.maxCompressedLength(input.readableBytes()); | |
| 65 | + | |
| 66 | + final ByteBuf frameBody; | |
| 67 | + if (input.isDirect()) { | |
| 68 | + // If the input is direct we will allocate a direct output buffer as well as this will allow us to use | |
| 69 | + // Snappy.compress(ByteBuffer, ByteBuffer) and so eliminate memory copies. | |
| 70 | + ByteBuf output = input.alloc().directBuffer(maxCompressedLength); | |
| 71 | + try { | |
| 72 | + // Using internalNioBuffer(...) as we only hold the reference in this method and so can | |
| 73 | + // reduce Object allocations. | |
| 74 | + ByteBuffer in = input.internalNioBuffer(input.readerIndex(), input.readableBytes()); | |
| 75 | + // Increase reader index. | |
| 76 | + input.readerIndex(input.writerIndex()); | |
| 77 | + | |
| 78 | + ByteBuffer out = output.internalNioBuffer(output.writerIndex(), output.writableBytes()); | |
| 79 | + int written = Snappy.compress(in, out); | |
| 80 | + // Set the writer index so the amount of written bytes is reflected | |
| 81 | + output.writerIndex(output.writerIndex() + written); | |
| 82 | + frameBody = output; | |
| 83 | + } catch (IOException e) { | |
| 84 | + // release output buffer so we not leak and rethrow exception. | |
| 85 | + output.release(); | |
| 86 | + throw e; | |
| 87 | + } | |
| 88 | + } else { | |
| 89 | + int inOffset = input.arrayOffset() + input.readerIndex(); | |
| 90 | + byte[] in = input.array(); | |
| 91 | + int len = input.readableBytes(); | |
| 92 | + // Increase reader index. | |
| 93 | + input.readerIndex(input.writerIndex()); | |
| 94 | + | |
| 95 | + // Allocate a heap buffer from the ByteBufAllocator as we may use a PooledByteBufAllocator and so | |
| 96 | + // can eliminate the overhead of allocate a new byte[]. | |
| 97 | + ByteBuf output = input.alloc().heapBuffer(maxCompressedLength); | |
| 98 | + try { | |
| 99 | + // Calculate the correct offset. | |
| 100 | + int offset = output.arrayOffset() + output.writerIndex(); | |
| 101 | + byte[] out = output.array(); | |
| 102 | + int written = Snappy.compress(in, inOffset, len, out, offset); | |
| 103 | + | |
| 104 | + // Increase the writerIndex with the written bytes. | |
| 105 | + output.writerIndex(output.writerIndex() + written); | |
| 106 | + frameBody = output; | |
| 107 | + } catch (IOException e) { | |
| 108 | + // release output buffer so we not leak and rethrow exception. | |
| 109 | + output.release(); | |
| 110 | + throw e; | |
| 111 | + } | |
| 112 | + } | |
| 113 | + return frame.with(frameBody); | |
| 66 | 114 | } |
| 67 | 115 | |
| 68 | 116 | @Override |
| 69 | 117 | public Frame decompress(Frame frame) throws IOException { |
| 70 | - byte[] input = CBUtil.readRawBytes(frame.body); | |
| 118 | + ByteBuf input = frame.body; | |
| 119 | + final ByteBuf frameBody; | |
| 71 | 120 | |
| 72 | - if (!Snappy.isValidCompressedBuffer(input, 0, input.length)) | |
| 73 | - throw new DriverInternalError("Provided frame does not appear to be Snappy compressed"); | |
| 121 | + if (input.isDirect()) { | |
| 122 | + // Using internalNioBuffer(...) as we only hold the reference in this method and so can | |
| 123 | + // reduce Object allocations. | |
| 124 | + ByteBuffer in = input.internalNioBuffer(input.readerIndex(), input.readableBytes()); | |
| 125 | + // Increase reader index. | |
| 126 | + input.readerIndex(input.writerIndex()); | |
| 74 | 127 | |
| 75 | - byte[] output = new byte[Snappy.uncompressedLength(input)]; | |
| 76 | - int size = Snappy.uncompress(input, 0, input.length, output, 0); | |
| 77 | - return frame.with(Unpooled.wrappedBuffer(output, 0, size)); | |
| 128 | + if (!Snappy.isValidCompressedBuffer(in)) | |
| 129 | + throw new DriverInternalError("Provided frame does not appear to be Snappy compressed"); | |
| 130 | + | |
| 131 | + // If the input is direct we will allocate a direct output buffer as well as this will allow us to use | |
| 132 | + // Snappy.compress(ByteBuffer, ByteBuffer) and so eliminate memory copies. | |
| 133 | + ByteBuf output = frame.body.alloc().directBuffer(Snappy.uncompressedLength(in)); | |
| 134 | + try { | |
| 135 | + ByteBuffer out = output.internalNioBuffer(output.writerIndex(), output.writableBytes()); | |
| 136 | + | |
| 137 | + int size = Snappy.uncompress(in, out); | |
| 138 | + // Set the writer index so the amount of written bytes is reflected | |
| 139 | + output.writerIndex(output.writerIndex() + size); | |
| 140 | + frameBody = output; | |
| 141 | + } catch (IOException e) { | |
| 142 | + // release output buffer so we not leak and rethrow exception. | |
| 143 | + output.release(); | |
| 144 | + throw e; | |
| 145 | + } | |
| 146 | + } else { | |
| 147 | + // Not a direct buffer so use byte arrays... | |
| 148 | + int inOffset = input.arrayOffset() + input.readerIndex(); | |
| 149 | + byte[] in = input.array(); | |
| 150 | + int len = input.readableBytes(); | |
| 151 | + // Increase reader index. | |
| 152 | + input.readerIndex(input.writerIndex()); | |
| 153 | + | |
| 154 | + if (!Snappy.isValidCompressedBuffer(in, inOffset, len)) | |
| 155 | + throw new DriverInternalError("Provided frame does not appear to be Snappy compressed"); | |
| 156 | + | |
| 157 | + // Allocate a heap buffer from the ByteBufAllocator as we may use a PooledByteBufAllocator and so | |
| 158 | + // can eliminate the overhead of allocate a new byte[]. | |
| 159 | + ByteBuf output = input.alloc().heapBuffer(Snappy.uncompressedLength(in, inOffset, len)); | |
| 160 | + try { | |
| 161 | + // Calculate the correct offset. | |
| 162 | + int offset = output.arrayOffset() + output.writerIndex(); | |
| 163 | + byte[] out = output.array(); | |
| 164 | + int written = Snappy.uncompress(in, inOffset, len, out, offset); | |
| 165 | + | |
| 166 | + // Increase the writerIndex with the written bytes. | |
| 167 | + output.writerIndex(output.writerIndex() + written); | |
| 168 | + frameBody = output; | |
| 169 | + } catch (IOException e) { | |
| 170 | + // release output buffer so we not leak and rethrow exception. | |
| 171 | + output.release(); | |
| 172 | + throw e; | |
| 173 | + } | |
| 174 | + } | |
| 175 | + return frame.with(frameBody); | |
| 78 | 176 | } |
| 79 | 177 | } |
| 80 | 178 |