28
28
import java .time .LocalTime ;
29
29
import java .time .Period ;
30
30
import java .util .UUID ;
31
+ import java .util .function .Function ;
31
32
import org .apache .ignite .internal .util .ByteUtils ;
32
- import org .apache .ignite .internal .util .GridUnsafe ;
33
33
34
34
/**
35
35
* Binary tuple parser allows to get bytes of individual elements from entirety of tuple bytes.
@@ -52,9 +52,6 @@ public interface Sink {
52
52
/** Byte order of ByteBuffers that contain the tuple. */
53
53
public static final ByteOrder ORDER = ByteOrder .LITTLE_ENDIAN ;
54
54
55
- /** Whether the byte order of the underlying buffer is reversed compared to the native byte order. */
56
- private static final boolean REVERSE_BYTE_ORDER = GridUnsafe .NATIVE_BYTE_ORDER != ORDER ;
57
-
58
55
/** UUID size in bytes. */
59
56
private static final int UUID_SIZE = 16 ;
60
57
@@ -75,19 +72,29 @@ public interface Sink {
75
72
/** Binary tuple. */
76
73
protected final ByteBuffer buffer ;
77
74
75
+ /**
76
+ * This constructor uses a default `PlainByteBufferAccessor` for accessing the buffer.
77
+ *
78
+ * @param numElements The number of elements in the binary tuple.
79
+ * @param buffer The `ByteBuffer` containing the binary tuple data.
80
+ */
81
+ public BinaryTupleParser (int numElements , ByteBuffer buffer ) {
82
+ this (numElements , buffer , PlainByteBufferAccessor ::new );
83
+ }
84
+
78
85
/**
79
86
* Constructor.
80
87
*
81
88
* @param numElements Number of tuple elements.
82
89
* @param buffer Buffer with a binary tuple.
83
90
*/
84
- public BinaryTupleParser (int numElements , ByteBuffer buffer ) {
91
+ public BinaryTupleParser (int numElements , ByteBuffer buffer , Function < ByteBuffer , ByteBufferAccessor > byteBufferAccessorFactory ) {
85
92
this .numElements = numElements ;
86
93
87
94
assert buffer .order () == ORDER : "Buffer order must be LITTLE_ENDIAN, actual: " + buffer .order ();
88
95
assert buffer .position () == 0 : "Buffer position must be 0, actual: " + buffer .position ();
89
96
this .buffer = buffer ;
90
- byteBufferAccessor = new ByteBufferAccessor (buffer );
97
+ byteBufferAccessor = byteBufferAccessorFactory . apply (buffer );
91
98
92
99
byte flags = byteBufferAccessor .get (0 );
93
100
@@ -658,107 +665,49 @@ private LocalTime getTime(int offset, int length) {
658
665
}
659
666
660
667
/**
661
- * An interface for accessing a byte buffer with methods to read various data types
662
- * at specific positions. This interface abstracts the underlying implementation
663
- * for handling data in either direct or heap-based byte buffers.
668
+ * A plain implementation of the `ByteBufferAccessor` interface.
669
+ * This class provides methods to access various data types from a `ByteBuffer`.
664
670
*/
665
- public static class ByteBufferAccessor {
666
- private final byte [] bytes ;
667
- private final long addr ;
668
- private final int capacity ;
669
-
670
- ByteBufferAccessor (ByteBuffer buff ) {
671
- if (buff .isDirect ()) {
672
- bytes = null ;
673
- addr = GridUnsafe .bufferAddress (buff );
674
- } else {
675
- bytes = buff .array ();
676
- addr = GridUnsafe .BYTE_ARR_OFF + buff .arrayOffset ();
677
- }
671
+ private static class PlainByteBufferAccessor implements ByteBufferAccessor {
672
+ private final ByteBuffer buffer ;
678
673
679
- capacity = buff .capacity ();
674
+ PlainByteBufferAccessor (ByteBuffer buffer ) {
675
+ this .buffer = buffer ;
680
676
}
681
677
682
- /**
683
- * Retrieves the byte value from the underlying byte buffer at the specified index.
684
- *
685
- * @param p the index in the underlying byte buffer to retrieve the byte from.
686
- * @return the byte value located at the specified index in the byte buffer.
687
- */
688
- public byte get (int p ) {
689
- return GridUnsafe .getByte (bytes , addr + p );
678
+ @ Override
679
+ public byte get (int index ) {
680
+ return buffer .get (index );
690
681
}
691
682
692
- /**
693
- * Reads a 32-bit integer value from the underlying byte buffer at the specified index.
694
- *
695
- * @param p the index in the underlying byte buffer to start reading the 32-bit integer value from.
696
- * @return the 32-bit integer value located at the specified index in the byte buffer.
697
- */
698
- public int getInt (int p ) {
699
- int value = GridUnsafe .getInt (bytes , addr + p );
700
-
701
- return REVERSE_BYTE_ORDER ? Integer .reverseBytes (value ) : value ;
683
+ @ Override
684
+ public short getShort (int index ) {
685
+ return buffer .getShort (index );
702
686
}
703
687
704
- /**
705
- * Reads a 64-bit long value from the underlying byte buffer at the specified index.
706
- *
707
- * @param p the index in the underlying byte buffer to start reading the 64-bit long value from.
708
- * @return the 64-bit long value located at the specified index in the byte buffer.
709
- */
710
- public long getLong (int p ) {
711
- long value = GridUnsafe .getLong (bytes , addr + p );
712
-
713
- return REVERSE_BYTE_ORDER ? Long .reverseBytes (value ) : value ;
688
+ @ Override
689
+ public int getInt (int index ) {
690
+ return buffer .getInt (index );
714
691
}
715
692
716
- /**
717
- * Reads a 16-bit short value from the underlying byte buffer at the specified index.
718
- *
719
- * @param p the index in the underlying byte buffer to start reading the 16-bit short value from.
720
- * @return the 16-bit short value located at the specified index in the byte buffer.
721
- */
722
- public short getShort (int p ) {
723
- short value = GridUnsafe .getShort (bytes , addr + p );
724
-
725
- return REVERSE_BYTE_ORDER ? Short .reverseBytes (value ) : value ;
693
+ @ Override
694
+ public long getLong (int index ) {
695
+ return buffer .getLong (index );
726
696
}
727
697
728
- /**
729
- * Reads a 32-bit floating-point value from the underlying byte buffer at the specified index.
730
- *
731
- * @param p the index in the underlying byte buffer to start reading the 32-bit floating-point value from.
732
- * @return the 32-bit floating-point value located at the specified index in the byte buffer.
733
- */
734
- public float getFloat (int p ) {
735
- float value = GridUnsafe .getFloat (bytes , addr + p );
736
-
737
- return REVERSE_BYTE_ORDER ? Float .intBitsToFloat (Integer .reverseBytes (Float .floatToIntBits (value ))) : value ;
698
+ @ Override
699
+ public float getFloat (int index ) {
700
+ return buffer .getFloat (index );
738
701
}
739
702
740
- /**
741
- * Reads a 64-bit double-precision floating-point value from the underlying
742
- * byte buffer at the specified index.
743
- *
744
- * @param p the index in the underlying byte buffer to start reading the
745
- * 64-bit double-precision floating-point value from.
746
- * @return the 64-bit double-precision floating-point value located at
747
- * the specified index in the byte buffer.
748
- */
749
- public double getDouble (int p ) {
750
- double value = GridUnsafe .getDouble (bytes , addr + p );
751
-
752
- return REVERSE_BYTE_ORDER ? Double .longBitsToDouble (Long .reverseBytes (Double .doubleToLongBits (value ))) : value ;
703
+ @ Override
704
+ public double getDouble (int index ) {
705
+ return buffer .getDouble (index );
753
706
}
754
707
755
- /**
756
- * Returns the capacity of the underlying byte buffer, representing the total number of bytes it can hold.
757
- *
758
- * @return the total capacity of the byte buffer.
759
- */
708
+ @ Override
760
709
public int capacity () {
761
- return capacity ;
710
+ return buffer . capacity () ;
762
711
}
763
712
}
764
713
}
0 commit comments