vldpyatkov commented on code in PR #5895: URL: https://github.com/apache/ignite-3/pull/5895#discussion_r2109233439
########## modules/binary-tuple/src/main/java/org/apache/ignite/internal/binarytuple/BinaryTupleParser.java: ########## @@ -632,4 +653,199 @@ private LocalTime getTime(int offset, int length) { return LocalTime.of(hour, minute, second, nanos); } + + /** + * An interface for accessing a byte buffer with methods to read various data types + * at specific positions. This interface abstracts the underlying implementation + * for handling data in either direct or heap-based byte buffers. + */ + public interface ByteBufferAccessor { + /** + * Retrieves the byte value from the underlying byte buffer at the specified index. + * + * @param p the index in the underlying byte buffer to retrieve the byte from. + * @return the byte value located at the specified index in the byte buffer. + */ + byte get(int p); + + /** + * Reads a 32-bit integer value from the underlying byte buffer at the specified index. + * + * @param p the index in the underlying byte buffer to start reading the 32-bit integer value from. + * @return the 32-bit integer value located at the specified index in the byte buffer. + */ + int getInt(int p); + + /** + * Reads a 64-bit long value from the underlying byte buffer at the specified index. + * + * @param p the index in the underlying byte buffer to start reading the 64-bit long value from. + * @return the 64-bit long value located at the specified index in the byte buffer. + */ + long getLong(int p); + + /** + * Reads a 16-bit short value from the underlying byte buffer at the specified index. + * + * @param p the index in the underlying byte buffer to start reading the 16-bit short value from. + * @return the 16-bit short value located at the specified index in the byte buffer. + */ + short getShort(int p); + + /** + * Reads a 32-bit floating-point value from the underlying byte buffer at the specified index. + * + * @param p the index in the underlying byte buffer to start reading the 32-bit floating-point value from. + * @return the 32-bit floating-point value located at the specified index in the byte buffer. + */ + float getFloat(int p); + + /** + * Reads a 64-bit double-precision floating-point value from the underlying + * byte buffer at the specified index. + * + * @param p the index in the underlying byte buffer to start reading the + * 64-bit double-precision floating-point value from. + * @return the 64-bit double-precision floating-point value located at + * the specified index in the byte buffer. + */ + double getDouble(int p); + + /** + * Retrieves a 64-bit long value from the underlying byte buffer at the specified index + * using little-endian byte order. The method interprets the specified position as the starting + * index of an 8-byte region and reads the bytes in little-endian order to construct the long value. + * + * @param p the index in the underlying byte buffer to start reading the 64-bit long value from. + * @return the 64-bit long value interpreted from the 8 bytes starting at the specified index in little-endian byte order. + */ + long getLongLittleEndian(int p); + + /** + * Returns the capacity of the underlying byte buffer, representing the total number of bytes it can hold. + * + * @return the total capacity of the byte buffer. + */ + int capacity(); + } + + /** + * Provides direct and efficient access to the off-heap memory of a direct {@link ByteBuffer}. + * This class implements {@link ByteBufferAccessor} to read various data types directly + * from memory using low-level unsafe operations. + * The accessor relies on the address of the buffer in off-heap memory and is specifically + * designed to work with direct buffers. + */ + private static class OffHeapByteBufferAccessor implements ByteBufferAccessor { + private final long addr; + private final int capacity; + + OffHeapByteBufferAccessor(ByteBuffer buff) { + assert buff.isDirect(); + addr = GridUnsafe.bufferAddress(buff); + capacity = buff.capacity(); + } + + @Override + public byte get(int p) { + return GridUnsafe.getByte(addr + p); + } + + @Override + public int getInt(int p) { + return GridUnsafe.getInt(addr + p); Review Comment: Done. ########## modules/binary-tuple/src/main/java/org/apache/ignite/internal/binarytuple/BinaryTupleParser.java: ########## @@ -632,4 +653,199 @@ private LocalTime getTime(int offset, int length) { return LocalTime.of(hour, minute, second, nanos); } + + /** + * An interface for accessing a byte buffer with methods to read various data types + * at specific positions. This interface abstracts the underlying implementation + * for handling data in either direct or heap-based byte buffers. + */ + public interface ByteBufferAccessor { + /** + * Retrieves the byte value from the underlying byte buffer at the specified index. + * + * @param p the index in the underlying byte buffer to retrieve the byte from. + * @return the byte value located at the specified index in the byte buffer. + */ + byte get(int p); + + /** + * Reads a 32-bit integer value from the underlying byte buffer at the specified index. + * + * @param p the index in the underlying byte buffer to start reading the 32-bit integer value from. + * @return the 32-bit integer value located at the specified index in the byte buffer. + */ + int getInt(int p); + + /** + * Reads a 64-bit long value from the underlying byte buffer at the specified index. + * + * @param p the index in the underlying byte buffer to start reading the 64-bit long value from. + * @return the 64-bit long value located at the specified index in the byte buffer. + */ + long getLong(int p); + + /** + * Reads a 16-bit short value from the underlying byte buffer at the specified index. + * + * @param p the index in the underlying byte buffer to start reading the 16-bit short value from. + * @return the 16-bit short value located at the specified index in the byte buffer. + */ + short getShort(int p); + + /** + * Reads a 32-bit floating-point value from the underlying byte buffer at the specified index. + * + * @param p the index in the underlying byte buffer to start reading the 32-bit floating-point value from. + * @return the 32-bit floating-point value located at the specified index in the byte buffer. + */ + float getFloat(int p); + + /** + * Reads a 64-bit double-precision floating-point value from the underlying + * byte buffer at the specified index. + * + * @param p the index in the underlying byte buffer to start reading the + * 64-bit double-precision floating-point value from. + * @return the 64-bit double-precision floating-point value located at + * the specified index in the byte buffer. + */ + double getDouble(int p); + + /** + * Retrieves a 64-bit long value from the underlying byte buffer at the specified index + * using little-endian byte order. The method interprets the specified position as the starting + * index of an 8-byte region and reads the bytes in little-endian order to construct the long value. + * + * @param p the index in the underlying byte buffer to start reading the 64-bit long value from. + * @return the 64-bit long value interpreted from the 8 bytes starting at the specified index in little-endian byte order. + */ + long getLongLittleEndian(int p); + + /** + * Returns the capacity of the underlying byte buffer, representing the total number of bytes it can hold. + * + * @return the total capacity of the byte buffer. + */ + int capacity(); + } + + /** + * Provides direct and efficient access to the off-heap memory of a direct {@link ByteBuffer}. + * This class implements {@link ByteBufferAccessor} to read various data types directly + * from memory using low-level unsafe operations. + * The accessor relies on the address of the buffer in off-heap memory and is specifically + * designed to work with direct buffers. + */ + private static class OffHeapByteBufferAccessor implements ByteBufferAccessor { + private final long addr; + private final int capacity; + + OffHeapByteBufferAccessor(ByteBuffer buff) { + assert buff.isDirect(); + addr = GridUnsafe.bufferAddress(buff); + capacity = buff.capacity(); + } + + @Override + public byte get(int p) { + return GridUnsafe.getByte(addr + p); + } + + @Override + public int getInt(int p) { + return GridUnsafe.getInt(addr + p); + } + + @Override + public long getLong(int p) { + return GridUnsafe.getLong(addr + p); + } + + @Override + public short getShort(int p) { + return GridUnsafe.getShort(addr + p); + } + + @Override + public float getFloat(int p) { + return GridUnsafe.getFloat(addr + p); + } + + @Override + public double getDouble(int p) { + return GridUnsafe.getDouble(addr + p); + } + + @Override + public long getLongLittleEndian(int p) { + return GridUnsafe.getLongLittleEndian(addr + p); + } + + @Override + public int capacity() { + return capacity; + } + } + + /** + * A utility class for accessing data stored in a heap-based {@link ByteBuffer}. + * This class implements the {@link ByteBufferAccessor} interface and provides methods + * to retrieve various types of data (e.g., byte, int, long, etc.) from a byte buffer + * backed by a byte array. + * This accessor uses the {@link GridUnsafe} utility to read data from the byte array + * with an offset calculated from the {@link ByteBuffer} properties, enabling efficient + * low-level operations. + */ + private static class HeapByteBufferAccessor implements ByteBufferAccessor { + private final byte[] bytes; + private final long offset; + private final int capacity; + + HeapByteBufferAccessor(ByteBuffer buff) { + assert !buff.isDirect(); + bytes = buff.array(); + offset = GridUnsafe.BYTE_ARR_OFF + buff.arrayOffset(); + capacity = buff.capacity(); + } + + @Override + public byte get(int p) { + return GridUnsafe.getByte(bytes, offset + p); + } + + @Override + public int getInt(int p) { + return GridUnsafe.getInt(bytes, offset + p); Review Comment: Fixed. -- 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: notifications-unsubscr...@ignite.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org