This is an automated email from the ASF dual-hosted git repository.

sijie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/bookkeeper.git


The following commit(s) were added to refs/heads/master by this push:
     new ff4932b  DoubleByteBuf fix for Netty > 4.1.12
ff4932b is described below

commit ff4932b37f68303793acce91840a55fddfda231f
Author: Enrico Olivelli <[email protected]>
AuthorDate: Wed Jan 17 10:34:53 2018 -0800

    DoubleByteBuf fix for Netty > 4.1.12
    
    This is a port from Pulsar, DoubleByteBuf has problems with Netty Native 
Transport.
    Original author is  sschepens
    
    see https://github.com/apache/incubator-pulsar/pull/1056
    
    Author: Enrico Olivelli <[email protected]>
    
    Reviewers: Ivan Kelly <[email protected]>, Jia Zhai <None>, Sijie Guo 
<[email protected]>
    
    This closes #996 from eolivelli/fix/doublebytebuf
---
 .../org/apache/bookkeeper/util/DoubleByteBuf.java  |  8 +++-
 .../apache/bookkeeper/util/DoubleByteBufTest.java  | 50 ++++++++++++++++++++++
 2 files changed, 56 insertions(+), 2 deletions(-)

diff --git 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/DoubleByteBuf.java 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/DoubleByteBuf.java
index e55dca2..9c51601 100644
--- 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/DoubleByteBuf.java
+++ 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/DoubleByteBuf.java
@@ -17,6 +17,7 @@
 */
 package org.apache.bookkeeper.util;
 
+import com.google.common.collect.ObjectArrays;
 import io.netty.buffer.AbstractReferenceCountedByteBuf;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.ByteBufAllocator;
@@ -361,7 +362,7 @@ public final class DoubleByteBuf extends 
AbstractReferenceCountedByteBuf {
 
     @Override
     public ByteBuffer nioBuffer(int index, int length) {
-        ByteBuffer dst = ByteBuffer.allocate(length);
+        ByteBuffer dst = isDirect() ? ByteBuffer.allocateDirect(length) : 
ByteBuffer.allocate(length);
         ByteBuf b = Unpooled.wrappedBuffer(dst);
         b.writerIndex(0);
         getBytes(index, b, length);
@@ -387,7 +388,10 @@ public final class DoubleByteBuf extends 
AbstractReferenceCountedByteBuf {
 
     @Override
     public ByteBuffer[] nioBuffers() {
-        return nioBuffers(readerIndex(), readableBytes());
+        if (b1.nioBufferCount() == 1 && b2.nioBufferCount() == 1) {
+            return new ByteBuffer[] { b1.nioBuffer(), b2.nioBuffer() };
+        }
+        return ObjectArrays.concat(b1.nioBuffers(), b2.nioBuffers(), 
ByteBuffer.class);
     }
 
     @Override
diff --git 
a/bookkeeper-server/src/test/java/org/apache/bookkeeper/util/DoubleByteBufTest.java
 
b/bookkeeper-server/src/test/java/org/apache/bookkeeper/util/DoubleByteBufTest.java
index 270a60f..0bb1825 100644
--- 
a/bookkeeper-server/src/test/java/org/apache/bookkeeper/util/DoubleByteBufTest.java
+++ 
b/bookkeeper-server/src/test/java/org/apache/bookkeeper/util/DoubleByteBufTest.java
@@ -19,6 +19,8 @@ package org.apache.bookkeeper.util;
 
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
 
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.PooledByteBufAllocator;
@@ -123,6 +125,54 @@ public class DoubleByteBufTest {
         assertEquals(ByteBuffer.wrap(new byte[] { 1, 2, 3, 4 }), 
b.nioBuffer());
     }
 
+    @Test
+    public void testNonDirectNioBuffer() {
+        ByteBuf b1 = Unpooled.wrappedBuffer(new byte[] { 1, 2 });
+        ByteBuf b2 = Unpooled.wrappedBuffer(new byte[] { 3, 4 });
+        ByteBuf b = DoubleByteBuf.get(b1, b2);
+        assertFalse(b1.isDirect());
+        assertFalse(b2.isDirect());
+        assertFalse(b.isDirect());
+        ByteBuffer nioBuffer = b.nioBuffer();
+        assertFalse(nioBuffer.isDirect());
+    }
+
+    @Test
+    public void testNonDirectPlusDirectNioBuffer() {
+        ByteBuf b1 = Unpooled.wrappedBuffer(new byte[] { 1, 2 });
+        ByteBuf b2 = Unpooled.directBuffer(2);
+        ByteBuf b = DoubleByteBuf.get(b1, b2);
+        assertFalse(b1.isDirect());
+        assertTrue(b2.isDirect());
+        assertFalse(b.isDirect());
+        ByteBuffer nioBuffer = b.nioBuffer();
+        assertFalse(nioBuffer.isDirect());
+    }
+
+    @Test
+    public void testDirectPlusNonDirectNioBuffer() {
+        ByteBuf b1 = Unpooled.directBuffer(2);
+        ByteBuf b2 = Unpooled.wrappedBuffer(new byte[] { 1, 2 });
+        ByteBuf b = DoubleByteBuf.get(b1, b2);
+        assertTrue(b1.isDirect());
+        assertFalse(b2.isDirect());
+        assertFalse(b.isDirect());
+        ByteBuffer nioBuffer = b.nioBuffer();
+        assertFalse(nioBuffer.isDirect());
+    }
+
+    @Test
+    public void testDirectNioBuffer() {
+        ByteBuf b1 = Unpooled.directBuffer(2);
+        ByteBuf b2 = Unpooled.directBuffer(2);
+        ByteBuf b = DoubleByteBuf.get(b1, b2);
+        assertTrue(b1.isDirect());
+        assertTrue(b2.isDirect());
+        assertTrue(b.isDirect());
+        ByteBuffer nioBuffer = b.nioBuffer();
+        assertTrue(nioBuffer.isDirect());
+    }
+
     /**
      * Verify that readableBytes() returns writerIndex - readerIndex. In this 
case writerIndex is the end of the buffer
      * and readerIndex is increased by 64.

-- 
To stop receiving notification emails like this one, please contact
['"[email protected]" <[email protected]>'].

Reply via email to