kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | ea1eb34ee4403b839af9c403c37266b3f9af98515a9da121121efc4634379bc0 |
|---|---|
| Author | Norman Maurer <norman_maurer@apple.com> · 2016-07-12 16:38 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-09-20 16:19 -07:00 |
| Parents | b37b671958a5 |
1 file changed
modified driver-core/src/main/java/com/datastax/driver/core/FrameCompressor.java +37 −24
| @@ -207,44 +207,57 @@ | ||
| 207 | 207 | |
| 208 | 208 | @Override |
| 209 | 209 | public Frame compress(Frame frame) throws IOException { |
| 210 | - byte[] input = CBUtil.readRawBytes(frame.body); | |
| 211 | - | |
| 212 | - int maxCompressedLength = compressor.maxCompressedLength(input.length); | |
| 213 | - byte[] output = new byte[INTEGER_BYTES + maxCompressedLength]; | |
| 214 | - | |
| 215 | - output[0] = (byte) (input.length >>> 24); | |
| 216 | - output[1] = (byte) (input.length >>> 16); | |
| 217 | - output[2] = (byte) (input.length >>> 8); | |
| 218 | - output[3] = (byte) (input.length); | |
| 219 | - | |
| 210 | + ByteBuf input = frame.body; | |
| 211 | + // Using internalNioBuffer(...) as we only hold the reference in this method and so can | |
| 212 | + // reduce Object allocations. | |
| 213 | + ByteBuffer in = inputNioBuffer(input, input.readerIndex(), input.readableBytes()); | |
| 214 | + | |
| 215 | + int maxCompressedLength = compressor.maxCompressedLength(in.remaining()); | |
| 216 | + ByteBuf output = input.alloc().directBuffer(INTEGER_BYTES + maxCompressedLength); | |
| 220 | 217 | try { |
| 221 | - int written = compressor.compress(input, 0, input.length, output, INTEGER_BYTES, maxCompressedLength); | |
| 222 | - return frame.with(Unpooled.wrappedBuffer(output, 0, INTEGER_BYTES + written)); | |
| 218 | + output.writeInt(in.remaining()); | |
| 219 | + // Using internalNioBuffer(...) as we only hold the reference in this method and so can | |
| 220 | + // reduce Object allocations. | |
| 221 | + ByteBuffer out = output.internalNioBuffer(output.writerIndex(), output.writableBytes()); | |
| 222 | + int written = compressor.compress(in, in.position(), in.remaining(), out, out.position(), out.remaining()); | |
| 223 | + // Set the writer index so the amount of written bytes is reflected | |
| 224 | + output.writerIndex(output.writerIndex() + written); | |
| 225 | + return frame.with(output); | |
| 223 | 226 | } catch (Exception e) { |
| 227 | + // release output buffer so we not leak and rethrow exception. | |
| 228 | + output.release(); | |
| 224 | 229 | throw new IOException(e); |
| 225 | 230 | } |
| 226 | 231 | } |
| 227 | 232 | |
| 228 | 233 | @Override |
| 229 | 234 | public Frame decompress(Frame frame) throws IOException { |
| 230 | - byte[] input = CBUtil.readRawBytes(frame.body); | |
| 231 | - | |
| 232 | - int uncompressedLength = ((input[0] & 0xFF) << 24) | |
| 233 | - | ((input[1] & 0xFF) << 16) | |
| 234 | - | ((input[2] & 0xFF) << 8) | |
| 235 | - | ((input[3] & 0xFF)); | |
| 236 | - | |
| 237 | - byte[] output = new byte[uncompressedLength]; | |
| 238 | - | |
| 235 | + ByteBuf input = frame.body; | |
| 236 | + int readable = input.readableBytes(); | |
| 237 | + int uncompressedLength = input.readInt(); | |
| 238 | + ByteBuffer in = inputNioBuffer(input, input.readerIndex(), input.readableBytes()); | |
| 239 | + input.readerIndex(input.writerIndex()); | |
| 240 | + ByteBuf output = input.alloc().directBuffer(uncompressedLength); | |
| 239 | 241 | try { |
| 240 | - int read = decompressor.decompress(input, INTEGER_BYTES, output, 0, uncompressedLength); | |
| 241 | - if (read != input.length - INTEGER_BYTES) | |
| 242 | + ByteBuffer out = output.internalNioBuffer(output.writerIndex(), output.writableBytes()); | |
| 243 | + int read = decompressor.decompress(in, in.position(), out, out.position(), out.remaining()); | |
| 244 | + if (read != readable - INTEGER_BYTES) | |
| 242 | 245 | throw new IOException("Compressed lengths mismatch"); |
| 243 | 246 | |
| 244 | - return frame.with(Unpooled.wrappedBuffer(output)); | |
| 247 | + // Set the writer index so the amount of written bytes is reflected | |
| 248 | + output.writerIndex(output.writerIndex() + uncompressedLength); | |
| 249 | + return frame.with(output); | |
| 245 | 250 | } catch (Exception e) { |
| 251 | + // release output buffer so we not leak and rethrow exception. | |
| 252 | + output.release(); | |
| 246 | 253 | throw new IOException(e); |
| 247 | 254 | } |
| 248 | 255 | } |
| 256 | + | |
| 257 | + private static ByteBuffer inputNioBuffer(ByteBuf buf, int index, int len) { | |
| 258 | + // Using internalNioBuffer(...) as we only hold the reference in this method and so can | |
| 259 | + // reduce Object allocations. | |
| 260 | + return buf.nioBufferCount() == 1 ? buf.internalNioBuffer(index, len) : buf.nioBuffer(index, len); | |
| 261 | + } | |
| 249 | 262 | } |
| 250 | 263 | } |