netudima commented on code in PR #3764:
URL: https://github.com/apache/cassandra/pull/3764#discussion_r1903250280


##########
src/java/org/apache/cassandra/db/NativeClustering.java:
##########
@@ -93,10 +100,172 @@ public int size()
         return MemoryUtil.getShort(peer);
     }
 
-    public ByteBuffer get(int i)
+    private static class NativeClusteringValue implements NativeData {
+        private final long peer;
+        private final int i;
+
+        private NativeClusteringValue(long peer, int i)
+        {
+            this.peer = peer;
+            this.i = i;
+        }
+
+        @Override
+        public int nativeDataSize()
+        {
+            int size = parentSize();
+            return NativeClustering.nativeDataSize(peer, size, i);
+        }
+
+        @Override
+        public ByteBuffer asByteBuffer()
+        {
+            return getByteBuffer((address, length) -> 
MemoryUtil.getByteBuffer(address, length, ByteOrder.BIG_ENDIAN));
+        }
+
+        @Override
+        public NativeData slice(int offset, int length)
+        {
+            ByteBuffer byteBuffer = asByteBuffer(); // we get a new buffer 
here each time, so duplicate() is not needed
+            byteBuffer.position(byteBuffer.position() + offset);
+            byteBuffer.limit(byteBuffer.position() + length);
+            return new ByteBufferSliceNativeData(byteBuffer);
+        }
+
+        private int parentSize() {
+            return MemoryUtil.getShort(peer);
+        }
+
+        private static final FastThreadLocal<ByteBuffer> 
REUSABLE_WRITE_BUFFER_1 = new FastThreadLocal<ByteBuffer>()
+        {
+            @Override
+            protected ByteBuffer initialValue()
+            {
+                return 
MemoryUtil.getHollowDirectByteBuffer(ByteOrder.BIG_ENDIAN);
+            }
+        };
+
+        /**
+         * we need two re-usable buffers to be able to do compare operation 
without memory allocations
+         */
+        private static final FastThreadLocal<ByteBuffer> 
REUSABLE_WRITE_BUFFER_2 = new FastThreadLocal<ByteBuffer>()
+        {
+            @Override
+            protected ByteBuffer initialValue()
+            {
+                return 
MemoryUtil.getHollowDirectByteBuffer(ByteOrder.BIG_ENDIAN);
+            }
+        };
+
+        @Override
+        public void writeTo(DataOutputPlus out) throws IOException
+        {
+            ByteBuffer byteBuffer = getByteBuffer((address, length) -> {
+                ByteBuffer result = REUSABLE_WRITE_BUFFER_1.get();
+                MemoryUtil.setDirectByteBuffer(result, address, length);
+                return result;
+            });
+            out.write(byteBuffer);
+        }
+
+        private ByteBuffer getByteBuffer(ByteBufferProvider provider)
+        {
+            int size = parentSize();
+            if (i >= size)
+                throw new IndexOutOfBoundsException();
+
+            return NativeClustering.getByteBuffer(peer, i, size, provider);
+        }
+
+        private interface ByteBufferProvider
+        {
+            ByteBuffer get(long address, int length);
+        }
+
+        @Override
+        public int compareTo(NativeData right)
+        {
+            ByteBuffer leftByteBuffer = getByteBuffer((address, length) -> {

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to