public class MemorySegmentUtils extends Object
MemorySegment
.Modifier and Type | Field and Description |
---|---|
static int |
BIT_BYTE_INDEX_MASK |
Constructor and Description |
---|
MemorySegmentUtils() |
Modifier and Type | Method and Description |
---|---|
static byte[] |
allocateReuseBytes(int length)
Allocate bytes that is only for temporary usage, it should not be stored in somewhere else.
|
static char[] |
allocateReuseChars(int length) |
static boolean |
bitGet(MemorySegment[] segments,
int baseOffset,
int index)
read bit from segments.
|
static boolean |
bitGet(MemorySegment segment,
int baseOffset,
int index)
read bit.
|
static void |
bitSet(MemorySegment[] segments,
int baseOffset,
int index)
set bit from segments.
|
static void |
bitSet(MemorySegment segment,
int baseOffset,
int index)
set bit.
|
static void |
bitUnSet(MemorySegment[] segments,
int baseOffset,
int index)
unset bit from segments.
|
static void |
bitUnSet(MemorySegment segment,
int baseOffset,
int index)
unset bit.
|
static int |
byteIndex(int bitIndex)
Given a bit index, return the byte index containing it.
|
static void |
copyFromBytes(MemorySegment[] segments,
int offset,
byte[] bytes,
int bytesOffset,
int numBytes)
Copy target segments from source byte[].
|
static void |
copyMultiSegmentsToBytes(MemorySegment[] segments,
int offset,
byte[] bytes,
int bytesOffset,
int numBytes) |
static byte[] |
copyToBytes(MemorySegment[] segments,
int offset,
byte[] bytes,
int bytesOffset,
int numBytes)
Copy segments to target byte[].
|
static byte[] |
copyToBytes(MemorySegment[] segments,
int offset,
int numBytes)
Copy segments to a new byte[].
|
static void |
copyToUnsafe(MemorySegment[] segments,
int offset,
Object target,
int pointer,
int numBytes)
Copy segments to target unsafe pointer.
|
static void |
copyToView(MemorySegment[] segments,
int offset,
int sizeInBytes,
DataOutputView target)
Copy bytes of segments to output view.
|
static boolean |
equals(MemorySegment[] segments1,
int offset1,
MemorySegment[] segments2,
int offset2,
int len)
Equals two memory segments regions.
|
static int |
find(MemorySegment[] segments1,
int offset1,
int numBytes1,
MemorySegment[] segments2,
int offset2,
int numBytes2)
Find equal segments2 in segments1.
|
static boolean |
getBoolean(MemorySegment[] segments,
int offset)
get boolean from segments.
|
static byte |
getByte(MemorySegment[] segments,
int offset)
get byte from segments.
|
static byte[] |
getBytes(MemorySegment[] segments,
int baseOffset,
int sizeInBytes)
Maybe not copied, if want copy, please use copyTo.
|
static double |
getDouble(MemorySegment[] segments,
int offset)
get double from segments.
|
static float |
getFloat(MemorySegment[] segments,
int offset)
get float from segments.
|
static int |
getInt(MemorySegment[] segments,
int offset)
get int from segments.
|
static long |
getLong(MemorySegment[] segments,
int offset)
get long from segments.
|
static short |
getShort(MemorySegment[] segments,
int offset)
get short from segments.
|
static int |
hash(MemorySegment[] segments,
int offset,
int numBytes)
hash segments to int.
|
static int |
hashByWords(MemorySegment[] segments,
int offset,
int numBytes)
hash segments to int, numBytes must be aligned to 4 bytes.
|
static InternalArray |
readArrayData(MemorySegment[] segments,
int baseOffset,
long offsetAndSize)
Gets an instance of
InternalArray from underlying MemorySegment . |
static byte[] |
readBinary(MemorySegment[] segments,
int baseOffset,
int fieldOffset,
long variablePartOffsetAndLen)
Get binary, if len less than 8, will be include in variablePartOffsetAndLen.
|
static BinaryString |
readBinaryString(MemorySegment[] segments,
int baseOffset,
int fieldOffset,
long variablePartOffsetAndLen)
Get binary string, if len less than 8, will be include in variablePartOffsetAndLen.
|
static Decimal |
readDecimal(MemorySegment[] segments,
int baseOffset,
long offsetAndSize,
int precision,
int scale)
Gets an instance of
Decimal from underlying MemorySegment . |
static InternalMap |
readMapData(MemorySegment[] segments,
int baseOffset,
long offsetAndSize)
Gets an instance of
InternalMap from underlying MemorySegment . |
static InternalRow |
readRowData(MemorySegment[] segments,
int numFields,
int baseOffset,
long offsetAndSize)
Gets an instance of
InternalRow from underlying MemorySegment . |
static Timestamp |
readTimestampData(MemorySegment[] segments,
int baseOffset,
long offsetAndNanos)
Gets an instance of
Timestamp from underlying MemorySegment . |
static void |
setBoolean(MemorySegment[] segments,
int offset,
boolean value)
set boolean from segments.
|
static void |
setByte(MemorySegment[] segments,
int offset,
byte value)
set byte from segments.
|
static void |
setDouble(MemorySegment[] segments,
int offset,
double value)
set double from segments.
|
static void |
setFloat(MemorySegment[] segments,
int offset,
float value)
set float from segments.
|
static void |
setInt(MemorySegment[] segments,
int offset,
int value)
set int from segments.
|
static void |
setLong(MemorySegment[] segments,
int offset,
long value)
set long from segments.
|
static void |
setShort(MemorySegment[] segments,
int offset,
short value)
set short from segments.
|
public static final int BIT_BYTE_INDEX_MASK
public static byte[] allocateReuseBytes(int length)
ThreadLocal
to reuse bytes to avoid overhead of byte[] new and gc.
If there are methods that can only accept a byte[], instead of a MemorySegment[] parameter, we can allocate a reuse bytes and copy the MemorySegment data to byte[], then call the method. Such as String deserialization.
public static char[] allocateReuseChars(int length)
public static void copyToView(MemorySegment[] segments, int offset, int sizeInBytes, DataOutputView target) throws IOException
Note: It just copies the data in, not include the length.
segments
- source segmentsoffset
- offset for segmentssizeInBytes
- size in bytestarget
- target output viewIOException
public static void copyFromBytes(MemorySegment[] segments, int offset, byte[] bytes, int bytesOffset, int numBytes)
segments
- target segments.offset
- target segments offset.bytes
- source byte[].bytesOffset
- source byte[] offset.numBytes
- the number bytes to copy.public static byte[] getBytes(MemorySegment[] segments, int baseOffset, int sizeInBytes)
public static boolean equals(MemorySegment[] segments1, int offset1, MemorySegment[] segments2, int offset2, int len)
segments1
- Segments 1offset1
- Offset of segments1 to start equalingsegments2
- Segments 2offset2
- Offset of segments2 to start equalinglen
- Length of the equaled memory regionpublic static byte[] copyToBytes(MemorySegment[] segments, int offset, int numBytes)
segments
- Source segments.offset
- Source segments offset.numBytes
- the number bytes to copy.public static byte[] copyToBytes(MemorySegment[] segments, int offset, byte[] bytes, int bytesOffset, int numBytes)
segments
- Source segments.offset
- Source segments offset.bytes
- target byte[].bytesOffset
- target byte[] offset.numBytes
- the number bytes to copy.public static void copyMultiSegmentsToBytes(MemorySegment[] segments, int offset, byte[] bytes, int bytesOffset, int numBytes)
public static void copyToUnsafe(MemorySegment[] segments, int offset, Object target, int pointer, int numBytes)
segments
- Source segments.offset
- The position where the bytes are started to be read from these memory segments.target
- The unsafe memory to copy the bytes to.pointer
- The position in the target unsafe memory to copy the chunk to.numBytes
- the number bytes to copy.public static int hashByWords(MemorySegment[] segments, int offset, int numBytes)
segments
- Source segments.offset
- Source segments offset.numBytes
- the number bytes to hash.public static int hash(MemorySegment[] segments, int offset, int numBytes)
segments
- Source segments.offset
- Source segments offset.numBytes
- the number bytes to hash.public static int find(MemorySegment[] segments1, int offset1, int numBytes1, MemorySegment[] segments2, int offset2, int numBytes2)
segments1
- segs to find.segments2
- sub segs.public static int byteIndex(int bitIndex)
bitIndex
- the bit index.public static void bitUnSet(MemorySegment segment, int baseOffset, int index)
segment
- target segment.baseOffset
- bits base offset.index
- bit index from base offset.public static void bitSet(MemorySegment segment, int baseOffset, int index)
segment
- target segment.baseOffset
- bits base offset.index
- bit index from base offset.public static boolean bitGet(MemorySegment segment, int baseOffset, int index)
segment
- target segment.baseOffset
- bits base offset.index
- bit index from base offset.public static void bitUnSet(MemorySegment[] segments, int baseOffset, int index)
segments
- target segments.baseOffset
- bits base offset.index
- bit index from base offset.public static void bitSet(MemorySegment[] segments, int baseOffset, int index)
segments
- target segments.baseOffset
- bits base offset.index
- bit index from base offset.public static boolean bitGet(MemorySegment[] segments, int baseOffset, int index)
segments
- target segments.baseOffset
- bits base offset.index
- bit index from base offset.public static boolean getBoolean(MemorySegment[] segments, int offset)
segments
- target segments.offset
- value offset.public static void setBoolean(MemorySegment[] segments, int offset, boolean value)
segments
- target segments.offset
- value offset.public static byte getByte(MemorySegment[] segments, int offset)
segments
- target segments.offset
- value offset.public static void setByte(MemorySegment[] segments, int offset, byte value)
segments
- target segments.offset
- value offset.public static int getInt(MemorySegment[] segments, int offset)
segments
- target segments.offset
- value offset.public static void setInt(MemorySegment[] segments, int offset, int value)
segments
- target segments.offset
- value offset.public static long getLong(MemorySegment[] segments, int offset)
segments
- target segments.offset
- value offset.public static void setLong(MemorySegment[] segments, int offset, long value)
segments
- target segments.offset
- value offset.public static short getShort(MemorySegment[] segments, int offset)
segments
- target segments.offset
- value offset.public static void setShort(MemorySegment[] segments, int offset, short value)
segments
- target segments.offset
- value offset.public static float getFloat(MemorySegment[] segments, int offset)
segments
- target segments.offset
- value offset.public static void setFloat(MemorySegment[] segments, int offset, float value)
segments
- target segments.offset
- value offset.public static double getDouble(MemorySegment[] segments, int offset)
segments
- target segments.offset
- value offset.public static void setDouble(MemorySegment[] segments, int offset, double value)
segments
- target segments.offset
- value offset.public static Decimal readDecimal(MemorySegment[] segments, int baseOffset, long offsetAndSize, int precision, int scale)
Decimal
from underlying MemorySegment
.public static Timestamp readTimestampData(MemorySegment[] segments, int baseOffset, long offsetAndNanos)
Timestamp
from underlying MemorySegment
.segments
- the underlying MemorySegmentsbaseOffset
- the base offset of current instance of TimestampData
offsetAndNanos
- the offset of milli-seconds part and nanosecondsTimestamp
public static byte[] readBinary(MemorySegment[] segments, int baseOffset, int fieldOffset, long variablePartOffsetAndLen)
Note: Need to consider the ByteOrder.
baseOffset
- base offset of composite binary format.fieldOffset
- absolute start offset of 'variablePartOffsetAndLen'.variablePartOffsetAndLen
- a long value, real data or offset and len.public static BinaryString readBinaryString(MemorySegment[] segments, int baseOffset, int fieldOffset, long variablePartOffsetAndLen)
Note: Need to consider the ByteOrder.
baseOffset
- base offset of composite binary format.fieldOffset
- absolute start offset of 'variablePartOffsetAndLen'.variablePartOffsetAndLen
- a long value, real data or offset and len.public static InternalMap readMapData(MemorySegment[] segments, int baseOffset, long offsetAndSize)
InternalMap
from underlying MemorySegment
.public static InternalArray readArrayData(MemorySegment[] segments, int baseOffset, long offsetAndSize)
InternalArray
from underlying MemorySegment
.public static InternalRow readRowData(MemorySegment[] segments, int numFields, int baseOffset, long offsetAndSize)
InternalRow
from underlying MemorySegment
.Copyright © 2023–2024 The Apache Software Foundation. All rights reserved.