netudima commented on code in PR #3764: URL: https://github.com/apache/cassandra/pull/3764#discussion_r1903380866
########## src/java/org/apache/cassandra/db/marshal/NativeAccessor.java: ########## @@ -0,0 +1,447 @@ +/* + * 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.io.IOException; +import java.nio.ByteBuffer; +import java.nio.FloatBuffer; +import java.nio.charset.CharacterCodingException; +import java.nio.charset.Charset; +import java.util.UUID; + +import org.apache.cassandra.db.Digest; +import org.apache.cassandra.db.TypeSizes; +import org.apache.cassandra.io.util.DataInputPlus; +import org.apache.cassandra.io.util.DataOutputPlus; +import org.apache.cassandra.service.paxos.Ballot; +import org.apache.cassandra.utils.ByteBufferUtil; +import org.apache.cassandra.utils.FastByteOperations; +import org.apache.cassandra.utils.TimeUUID; +import org.apache.cassandra.utils.UUIDGen; + +/** + * ValueAccessor has a lot of different methods are grouped together in a single interface. + * Technically the methods can be classfied to 4 categories: + * 1) basic methods to deal with the existing data as an abstract read-only container of bytes + * 2) deserialization methods to decode the data into different data types + * 3) serialization methods to encode and write different data types into the value entity + * 4) Value object creation methods + * + * NativeAccessor provides a support for real NativeData objects (on top of off-heap memory) for 1-3 categories + * with a focus on 1) category and only emulates 4th category using ByteBufferSliceNativeData on top of heap ByteBuffers. + * We expect NativeData is used only to store data in Memtables with an explicit allocator and memory regions lifecycle + * and not used to create short-living Mutation requests and transfer them between nodes. + */ +public class NativeAccessor implements ValueAccessor<NativeData> +{ + public static final ValueAccessor<NativeData> instance = new NativeAccessor(); + + // ----------------------------------------------------------------------------- + // basic methods to deal with data as a read-only container of bytes + + @Override + public int size(NativeData value) + { + return value.nativeDataSize(); + } + + @Override + public void write(NativeData sourceValue, DataOutputPlus out) throws IOException + { + sourceValue.writeTo(out); + } + + @Override + public ByteBuffer toBuffer(NativeData value) + { + if (value == null) + return null; + return value.asByteBuffer(); + } + + @Override + public void write(NativeData value, ByteBuffer out) + { + out.put(value.asByteBuffer().duplicate()); // ByteBufferSliceNativeDataasByteBuffer() returns a re-usable byte buffer + } + + @Override + public <V2> int copyTo(NativeData src, int srcOffset, V2 dst, ValueAccessor<V2> dstAccessor, int dstOffset, int size) + { + dstAccessor.copyByteBufferTo(src.asByteBuffer(), srcOffset, dst, dstOffset, size); + return size; + } + + @Override + public int copyByteArrayTo(byte[] src, int srcOffset, NativeData dstNative, int dstOffset, int size) + { + ByteBuffer dst = dstNative.asByteBuffer(); + FastByteOperations.copy(src, srcOffset, dst, dst.position() + dstOffset, size); + return size; + } + + @Override + public int copyByteBufferTo(ByteBuffer src, int srcOffset, NativeData dstNative, int dstOffset, int size) + { + ByteBuffer dst = dstNative.asByteBuffer(); + FastByteOperations.copy(src, src.position() + srcOffset, dst, dst.position() + dstOffset, size); + return size; + } + + @Override + public void digest(NativeData value, int offset, int size, Digest digest) + { + ByteBuffer byteBuffer = value.asByteBuffer(); + digest.update(byteBuffer, byteBuffer.position() + offset, size); + } + + @Override + public NativeData slice(NativeData input, int offset, int length) + { + return input.slice(offset, length); + } + + @Override + public <VR> int compare(NativeData left, VR right, ValueAccessor<VR> accessorR) + { + if (right instanceof NativeData) + { + return left.compareTo((NativeData) right); + } + return left.compareTo(accessorR.toBuffer(right)); + } + + @Override + public int compareByteArrayTo(byte[] left, NativeData right) + { + return ByteBufferUtil.compare(left, right.asByteBuffer()); + } + + @Override + public int compareByteBufferTo(ByteBuffer left, NativeData right) + { + return -right.compareTo(left); // we want to avoid ByteBuffer retrieval from NativeData + } + + // ----------------------------------------------------------------------------- + // Data deserialization methods + + @Override + public byte[] toArray(NativeData value) + { + if (value == null) + return null; + return ByteBufferUtil.getArray(value.asByteBuffer()); + } + + @Override + public byte[] toArray(NativeData value, int offset, int length) + { + if (value == null) + return null; + ByteBuffer byteBuffer = value.asByteBuffer(); + return ByteBufferUtil.getArray(byteBuffer, byteBuffer.position() + offset, length); + } + + @Override + public String toString(NativeData value, Charset charset) throws CharacterCodingException + { + return ByteBufferUtil.string(value.asByteBuffer(), charset); + } + + @Override + public String toHex(NativeData value) + { + return ByteBufferUtil.bytesToHex(value.asByteBuffer()); + } + + @Override + public byte toByte(NativeData value) + { + return ByteBufferUtil.toByte(value.asByteBuffer()); + } + + @Override + public byte getByte(NativeData value, int offset) Review Comment: working on a test coverage -- 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