下面列出了io.netty.util.internal.PlatformDependent#putByte ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static int unsafeOnHeapWriteUTF(final CharSequence str, final byte[] bytes, final int index, final int length) {
int charCount = index;
for (int i = 0; i < length; i++) {
char charAtPos = str.charAt(i);
if (charAtPos <= 0x7f) {
PlatformDependent.putByte(bytes, charCount++, (byte) charAtPos);
} else if (charAtPos >= 0x800) {
PlatformDependent.putByte(bytes, charCount++, (byte) (0xE0 | charAtPos >> 12 & 0x0F));
PlatformDependent.putByte(bytes, charCount++, (byte) (0x80 | charAtPos >> 6 & 0x3F));
PlatformDependent.putByte(bytes, charCount++, (byte) (0x80 | charAtPos >> 0 & 0x3F));
} else {
PlatformDependent.putByte(bytes, charCount++, (byte) (0xC0 | charAtPos >> 6 & 0x1F));
PlatformDependent.putByte(bytes, charCount++, (byte) (0x80 | charAtPos >> 0 & 0x3F));
}
}
final int writtenBytes = (charCount - index);
return writtenBytes;
}
public static void writeWordwise(long addr, long length, long value) {
if (length == 0) {
return;
}
long nLong = length >>> 3;
int nBytes = (int) length & 7;
for (long i = nLong; i > 0; i--) {
PlatformDependent.putLong(addr, value);
addr += 8;
}
if (nBytes == 4) {
PlatformDependent.putInt(addr, (int) value);
addr += 4;
} else if (nBytes < 4) {
for (int i = nBytes; i > 0; i--) {
PlatformDependent.putByte(addr, (byte) value);
addr++;
}
} else {
PlatformDependent.putInt(addr, (int) value);
addr += 4;
for (int i = nBytes - 4; i > 0; i--) {
PlatformDependent.putByte(addr, (byte) value);
addr++;
}
}
}
public static void writeWordwise(long addr, long length, long value) {
if (length == 0) {
return;
}
long nLong = length >>> 3;
int nBytes = (int) length & 7;
for (long i = nLong; i > 0; i--) {
PlatformDependent.putLong(addr, value);
addr += 8;
}
if (nBytes == 4) {
PlatformDependent.putInt(addr, (int) value);
addr += 4;
} else if (nBytes < 4) {
for (int i = nBytes; i > 0; i--) {
PlatformDependent.putByte(addr, (byte) value);
addr++;
}
} else {
PlatformDependent.putInt(addr, (int) value);
addr += 4;
for (int i = nBytes - 4; i > 0; i--) {
PlatformDependent.putByte(addr, (byte) value);
addr++;
}
}
}
@Override
public void putByte(byte val) {
ensureSize(SizeOf.SIZE_OF_BYTE);
long addr = currentAddr();
PlatformDependent.putByte(addr, val);
curFieldIdx++;
forwardField(SizeOf.SIZE_OF_BYTE);
}
static void setShort(long address, int value) {
if (UNALIGNED) {
PlatformDependent.putShort(
address, BIG_ENDIAN_NATIVE_ORDER ? (short) value : Short.reverseBytes((short) value));
} else {
PlatformDependent.putByte(address, (byte) (value >>> 8));
PlatformDependent.putByte(address + 1, (byte) value);
}
}
static void setShortLE(long address, int value) {
if (UNALIGNED) {
PlatformDependent.putShort(
address, BIG_ENDIAN_NATIVE_ORDER ? Short.reverseBytes((short) value) : (short) value);
} else {
PlatformDependent.putByte(address, (byte) value);
PlatformDependent.putByte(address + 1, (byte) (value >>> 8));
}
}
@Override
public void copy(long offsetAddr, int count) {
if(allocateAsFixed){
targetAlt.allocateNew(count);
}
final long[] srcAddr = this.srcAddrs;
final long dstAddr;
switch (bufferOrdinal) {
case NULL_BUFFER_ORDINAL:
dstAddr = target.getValidityBufferAddress();
break;
case VALUE_BUFFER_ORDINAL:
dstAddr = target.getDataBufferAddress();
break;
default:
throw new UnsupportedOperationException("unexpected buffer offset");
}
final long maxAddr = offsetAddr + count * BUILD_RECORD_LINK_SIZE;
int targetIndex = 0;
for(; offsetAddr < maxAddr; offsetAddr += BUILD_RECORD_LINK_SIZE, targetIndex++){
final int batchIndex = PlatformDependent.getInt(offsetAddr);
final int batchOffset = Short.toUnsignedInt(PlatformDependent.getShort(offsetAddr + 4));
final int byteValue = PlatformDependent.getByte(srcAddr[batchIndex] + (batchOffset >>> 3));
final int bitVal = ((byteValue >>> (batchOffset & 7)) & 1) << (targetIndex & 7);
final long addr = dstAddr + (targetIndex >>> 3);
PlatformDependent.putByte(addr, (byte) (PlatformDependent.getByte(addr) | bitVal));
}
}
static void setMediumLE(long address, int value) {
PlatformDependent.putByte(address, (byte) value);
if (UNALIGNED) {
PlatformDependent.putShort(address + 1, BIG_ENDIAN_NATIVE_ORDER ? Short.reverseBytes((short) (value >>> 8))
: (short) (value >>> 8));
} else {
PlatformDependent.putByte(address + 1, (byte) (value >>> 8));
PlatformDependent.putByte(address + 2, (byte) (value >>> 16));
}
}
private void fillZeros(long startAddr, int length) {
long c = startAddr;
final long endAddr = startAddr + length;
while(c < endAddr) {
PlatformDependent.putLong(c, 0);
c += 8;
}
int remain = length % 8;
if(remain != 0){
for(int i = 0; i < remain ; i++) {
PlatformDependent.putByte(endAddr - i, (byte)0);
}
}
}
@Override
public void copy(long compoundAddr, int srcStart, final int count) {
copyWatch.start();
final long[] dstAddrs = this.dstAddrs;
// skip bytes, but make sure to account for the remaining bits too
final long srcAddr;
switch (bufferOrdinal) {
case NULL_BUFFER_ORDINAL:
srcAddr = source.getValidityBufferAddress();
break;
case VALUE_BUFFER_ORDINAL:
srcAddr = source.getDataBufferAddress();
break;
default:
throw new UnsupportedOperationException("unexpected buffer offset");
}
final long max = compoundAddr + count * OFFSET_SIZE;
for(; compoundAddr < max; compoundAddr +=OFFSET_SIZE, srcStart++){
final int compoundIdx = PlatformDependent.getInt(compoundAddr);
final int batchIdx = compoundIdx >>> 16;
final int rowIdx = compoundIdx & 65535;
final int byteValue = PlatformDependent.getByte(srcAddr + (srcStart >>> 3));
final int bitVal = ((byteValue >>> (srcStart & 7)) & 1) << (rowIdx & 7);
final long dstAddr = dstAddrs[batchIdx] + (rowIdx >>> 3);
PlatformDependent.putByte(dstAddr, (byte) (PlatformDependent.getByte(dstAddr) | bitVal));
}
copyWatch.stop();
}
@Override
public void putBool(boolean val) {
ensureSize(SizeOf.SIZE_OF_BOOL);
long addr = currentAddr();
PlatformDependent.putByte(addr, (byte) (val ? 0x01 : 0x00));
putFieldHeader(addr, curOffset);
forwardField(SizeOf.SIZE_OF_BOOL);
}
static void setShort(byte[] array, int index, int value) {
if (UNALIGNED) {
PlatformDependent.putShort(array, index,
BIG_ENDIAN_NATIVE_ORDER ? (short) value : Short.reverseBytes((short) value));
} else {
PlatformDependent.putByte(array, index, (byte) (value >>> 8));
PlatformDependent.putByte(array, index + 1, (byte) value);
}
}
static void setLong(byte[] array, int index, long value) {
if (UNALIGNED) {
PlatformDependent.putLong(array, index, BIG_ENDIAN_NATIVE_ORDER ? value : Long.reverseBytes(value));
} else {
PlatformDependent.putByte(array, index, (byte) (value >>> 56));
PlatformDependent.putByte(array, index + 1, (byte) (value >>> 48));
PlatformDependent.putByte(array, index + 2, (byte) (value >>> 40));
PlatformDependent.putByte(array, index + 3, (byte) (value >>> 32));
PlatformDependent.putByte(array, index + 4, (byte) (value >>> 24));
PlatformDependent.putByte(array, index + 5, (byte) (value >>> 16));
PlatformDependent.putByte(array, index + 6, (byte) (value >>> 8));
PlatformDependent.putByte(array, index + 7, (byte) value);
}
}
@Override
protected void _setMedium(int index, int value) {
long addr = addr(index);
PlatformDependent.putByte(addr, (byte) (value >>> 16));
PlatformDependent.putByte(addr + 1, (byte) (value >>> 8));
PlatformDependent.putByte(addr + 2, (byte) value);
}
static void setInt(byte[] array, int index, int value) {
if (UNALIGNED) {
PlatformDependent.putInt(array, index, BIG_ENDIAN_NATIVE_ORDER ? value : Integer.reverseBytes(value));
} else {
PlatformDependent.putByte(array, index, (byte) (value >>> 24));
PlatformDependent.putByte(array, index + 1, (byte) (value >>> 16));
PlatformDependent.putByte(array, index + 2, (byte) (value >>> 8));
PlatformDependent.putByte(array, index + 3, (byte) value);
}
}
private void insertAndAccumulateForAllPartitions(final BufferAllocator allocator,
final int records,
final PivotDef pivot,
final VectorizedHashAggPartition[] partitions,
final int[] expectedOrdinals) {
try (final FixedBlockVector fbv = new FixedBlockVector(allocator, pivot.getBlockWidth());
final VariableBlockVector var = new VariableBlockVector(allocator, pivot.getVariableCount());
final ArrowBuf offsets = allocator.buffer(records * VectorizedHashAggOperator.PARTITIONINDEX_HTORDINAL_WIDTH);
final SimpleBigIntVector hashValues = new SimpleBigIntVector("hashvalues", allocator)) {
/* pivot the data into temporary space */
Pivots.pivot(pivot, records, fbv, var);
final long keyFixedVectorAddr = fbv.getMemoryAddress();
final long keyVarVectorAddr = var.getMemoryAddress();
int[] actualOrdinals = new int[expectedOrdinals.length];
final boolean fixedOnly = pivot.getVariableCount() == 0;
/* compute hash on the pivoted data */
hashValues.allocateNew(records);
final BlockChunk blockChunk = new BlockChunk(keyFixedVectorAddr, keyVarVectorAddr, fixedOnly,
pivot.getBlockWidth(), records, hashValues.getBufferAddress(), 0);
HashComputation.computeHash(blockChunk);
/* since we are mocking partitions to test spill handler,
* just insert same data into all partitions
*/
int hashPartitionIndex = 0;
for (int i = 0; i < partitions.length; i++) {
/* insert */
long offsetAddr = offsets.memoryAddress();
LBlockHashTable hashTable = null;
for (int keyIndex = 0; keyIndex < records; keyIndex++, offsetAddr += VectorizedHashAggOperator.PARTITIONINDEX_HTORDINAL_WIDTH) {
final int keyHash = (int)hashValues.get(keyIndex);
hashTable = partitions[i].getHashTable();
actualOrdinals[keyIndex] = hashTable.add(keyFixedVectorAddr, keyVarVectorAddr, keyIndex, keyHash);
PlatformDependent.putByte(offsetAddr, (byte)hashPartitionIndex);
PlatformDependent.putInt(offsetAddr + VectorizedHashAggOperator.HTORDINAL_OFFSET, actualOrdinals[keyIndex]);
PlatformDependent.putInt(offsetAddr + VectorizedHashAggOperator.KEYINDEX_OFFSET, keyIndex);
}
/* accumulate */
partitions[i].getAccumulator().accumulate(offsets.memoryAddress(), records,
partitions[0].getHashTable().getBitsInChunk(),
partitions[0].getHashTable().getChunkOffsetMask());
/* check hashtable */
assertArrayEquals(expectedOrdinals, actualOrdinals);
assertEquals(1, hashTable.getFixedBlockBuffers().size());
assertEquals(1, hashTable.getFixedBlockBuffers().size());
}
}
}
public void writeByteUnsafe(byte b) {
PlatformDependent.putByte(addr(readerIndex), b);
readerIndex++;
}
@Override
public ByteBuf setByte(int index, int value) {
PlatformDependent.putByte(addr(index), (byte) value);
return this;
}
@Override
protected void _setByte(int index, int value) {
PlatformDependent.putByte(addr(index), (byte) value);
}
private void insertAndAccumulateForAllPartitions(final BufferAllocator allocator,
final int records,
final PivotDef pivot,
final LBlockHashTable hashTable,
final AccumulatorSet accumulator,
final int[] expectedOrdinals) {
try (final FixedBlockVector fbv = new FixedBlockVector(allocator, pivot.getBlockWidth());
final VariableBlockVector var = new VariableBlockVector(allocator, pivot.getVariableCount());
final ArrowBuf offsets = allocator.buffer(records * VectorizedHashAggOperator.PARTITIONINDEX_HTORDINAL_WIDTH);
final SimpleBigIntVector hashValues = new SimpleBigIntVector("hashvalues", allocator)) {
/* pivot the data into temporary space */
Pivots.pivot(pivot, records, fbv, var);
final long keyFixedVectorAddr = fbv.getMemoryAddress();
final long keyVarVectorAddr = var.getMemoryAddress();
int[] actualOrdinals = new int[expectedOrdinals.length];
final boolean fixedOnly = pivot.getVariableCount() == 0;
/* compute hash on the pivoted data */
hashValues.allocateNew(records);
final BlockChunk blockChunk = new BlockChunk(keyFixedVectorAddr, keyVarVectorAddr, fixedOnly,
pivot.getBlockWidth(), records, hashValues.getBufferAddress(), 0);
HashComputation.computeHash(blockChunk);
/* insert */
long offsetAddr = offsets.memoryAddress();
for (int keyIndex = 0; keyIndex < records; keyIndex++, offsetAddr += VectorizedHashAggOperator.PARTITIONINDEX_HTORDINAL_WIDTH) {
final int keyHash = (int)hashValues.get(keyIndex);
actualOrdinals[keyIndex] = hashTable.add(keyFixedVectorAddr, keyVarVectorAddr, keyIndex, keyHash);
PlatformDependent.putByte(offsetAddr, (byte)0);
PlatformDependent.putInt(offsetAddr + VectorizedHashAggOperator.HTORDINAL_OFFSET, actualOrdinals[keyIndex]);
PlatformDependent.putInt(offsetAddr + VectorizedHashAggOperator.KEYINDEX_OFFSET, keyIndex);
}
assertArrayEquals(expectedOrdinals, actualOrdinals);
/* accumulate */
accumulator.accumulate(offsets.memoryAddress(), records, hashTable.getBitsInChunk(), hashTable.getChunkOffsetMask());
}
}