blambov commented on code in PR #4117:
URL: https://github.com/apache/cassandra/pull/4117#discussion_r2093184435


##########
src/java/org/apache/cassandra/utils/memory/BigEndianMemoryUtil.java:
##########
@@ -0,0 +1,146 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.cassandra.utils.memory;
+
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+import com.google.common.annotations.VisibleForTesting;
+
+import org.apache.cassandra.utils.Architecture;
+
+public class BigEndianMemoryUtil extends MemoryUtil
+{
+    public static int getUnsignedShort(long address)
+    {
+        if (Architecture.IS_UNALIGNED || (address & 0b1) == 0L)
+            return (Architecture.BIG_ENDIAN ? unsafe.getShort(address) : 
Short.reverseBytes(unsafe.getShort(address))) & 0xffff;
+        else
+            return getShortByByte(address) & 0xffff;
+    }
+
+    public static int getInt(long address)
+    {
+        if (Architecture.IS_UNALIGNED || (address & 0b11) == 0L)
+            return Architecture.BIG_ENDIAN ? unsafe.getInt(address) : 
Integer.reverseBytes(unsafe.getInt(address));
+        else
+            return getIntByByte(address);
+    }
+
+    public static long getLong(long address)
+    {
+        if (Architecture.IS_UNALIGNED || (address & 0b111) == 0L)
+            return Architecture.BIG_ENDIAN ? unsafe.getLong(address) : 
Long.reverseBytes(unsafe.getLong(address));
+        else
+            return getLongByByte(address);
+    }
+
+    public static void setShort(long address, short s)
+    {
+        if (Architecture.IS_UNALIGNED || (address & 0b1) == 0L)
+            unsafe.putShort(address, Architecture.BIG_ENDIAN ? s : 
Short.reverseBytes(s));
+        else
+            putShortByByte(address, s);
+    }
+
+    public static void setInt(long address, int l)
+    {
+        if (Architecture.IS_UNALIGNED || (address & 0b11) == 0L)
+            unsafe.putInt(address, Architecture.BIG_ENDIAN ? l : 
Integer.reverseBytes(l));
+        else
+            putIntByByte(address, l);
+    }
+
+    public static void setLong(long address, long l)
+    {
+        if (Architecture.IS_UNALIGNED || (address & 0b111) == 0L)
+            unsafe.putLong(address, Architecture.BIG_ENDIAN ? l : 
Long.reverseBytes(l));
+        else
+            putLongByByte(address, l);
+    }
+
+    @VisibleForTesting
+    static long getLongByByte(long address)
+    {
+        return  (((long) unsafe.getByte(address    )       ) << 56) |

Review Comment:
   If we ever find ourselves actually using unaligned-access architectures, we 
should implement this by reading two longs and shifting appropriately.
   
   No point to do anything in this patch, though.



##########
src/java/org/apache/cassandra/db/marshal/AddressBasedNativeData.java:
##########
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.cassandra.db.marshal;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+import org.apache.cassandra.utils.memory.MemoryUtil;
+
+public class AddressBasedNativeData implements NativeData
+{
+    // use a real address, just in case
+    private static final ByteBuffer EMPTY_NATIVE_BUFFER = 
ByteBuffer.allocateDirect(1);
+    private static final long EMPTY_VALUE_ADDRESS = 
MemoryUtil.getAddress(EMPTY_NATIVE_BUFFER);
+    public static final AddressBasedNativeData EMPTY = new 
AddressBasedNativeData(EMPTY_VALUE_ADDRESS, 0);
+
+    private final long address;
+    private final int length;
+
+    public AddressBasedNativeData(long address, int length)
+    {
+        this.address = address;
+        this.length = length;
+    }
+
+
+    @Override
+    public int nativeDataSize()
+    {
+        return length;
+    }
+
+    @Override
+    public ByteBuffer asByteBuffer()
+    {
+        return MemoryUtil.getByteBuffer(address, length, ByteOrder.BIG_ENDIAN);
+    }
+
+    @Override
+    public NativeData slice(int offset, int length)
+    {
+        if (offset < 0 || offset > this.length)
+            throw new IllegalArgumentException("offset must but be >= 0 and < 
parent length");

Review Comment:
   Nit: It would help to see the values of `this.length`, `offset` and `length` 
in these errors.



##########
src/java/org/apache/cassandra/utils/FastByteOperations.java:
##########
@@ -262,7 +321,7 @@ public void copy(byte[] src, int srcPosition, ByteBuffer 
trg, int trgPosition, i
             if (trg.hasArray())
                 System.arraycopy(src, srcPosition, trg.array(), 
trg.arrayOffset() + trgPosition, length);
             else
-                copy(null, srcPosition + theUnsafe.getLong(src, 
Unsafe.ARRAY_BYTE_BASE_OFFSET), trg, trgPosition, length);
+                copy(src, (long) srcPosition + Unsafe.ARRAY_BYTE_BASE_OFFSET, 
trg, trgPosition, length);

Review Comment:
   Just to make it extra visible which method is called, I would cast `src` to 
`Object` as well.



##########
test/unit/org/apache/cassandra/db/ClusteringHeapSizeTest.java:
##########
@@ -64,6 +65,8 @@ public void unsharedHeapSizeExcludingDataLTEUnsharedHeapSize()
     @Test
     public void testSingletonClusteringHeapSize()
     {
+        if (this.clustering.accessor() == NativeAccessor.instance)

Review Comment:
   Nit: change this to `Assume(this.clustering.accessor() != 
NativeAccessor.instance)`.



-- 
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: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org

Reply via email to