下面列出了io.netty.util.internal.PlatformDependent#putShort ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public ByteBuf writeShort(int value) {
BoundsChecking.ensureWritable(this, 2);
PlatformDependent.putShort(addr(writerIndex), (short) value);
writerIndex += 2;
return this;
}
@Override
public ByteBuf writeChar(int value) {
BoundsChecking.ensureWritable(this, 2);
PlatformDependent.putShort(addr(writerIndex), (short) value);
writerIndex += 2;
return this;
}
@Test
public void bigintRoundtrip(){
final int count = 1024;
try(
BigIntVector in = new BigIntVector("in", allocator);
BigIntVector out = new BigIntVector("out", allocator);
){
in.allocateNew(count);
for(int i = 0; i < count; i++){
if(i % 5 == 0){
in.setSafe(i, i);
}
}
in.setValueCount(count);
List<FieldBufferCopier> copiers = FieldBufferCopier.getCopiers(ImmutableList.<FieldVector>of(in), ImmutableList.<FieldVector>of(out));
try(
final SelectionVector2 sv2 = new SelectionVector2(allocator);
){
sv2.allocateNew(count);
// create full sv2.
int x = 0;
for(long mem = sv2.memoryAddress(); mem < sv2.memoryAddress() + count * 2; mem+=2){
PlatformDependent.putShort(mem, (short) (char) x);
x++;
}
sv2.setRecordCount(count);
copy(copiers, sv2);
out.setValueCount(count);
for(int i =0; i < count; i++){
assertEquals(in.getObject(i), out.getObject(i));
}
}
}
}
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 setMedium(long address, int value) {
PlatformDependent.putByte(address, (byte) (value >>> 16));
if (UNALIGNED) {
PlatformDependent.putShort(address + 1, BIG_ENDIAN_NATIVE_ORDER ? (short) value
: Short.reverseBytes((short) value));
} else {
PlatformDependent.putByte(address + 1, (byte) (value >>> 8));
PlatformDependent.putByte(address + 2, (byte) value);
}
}
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));
}
}
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 setShortLE(byte[] array, int index, int value) {
if (UNALIGNED) {
PlatformDependent.putShort(array, index,
BIG_ENDIAN_NATIVE_ORDER ? Short.reverseBytes((short) value) : (short) value);
} else {
PlatformDependent.putByte(array, index, (byte) value);
PlatformDependent.putByte(array, index + 1, (byte) (value >>> 8));
}
}
@Override
public void endRow() {
long rowHeaderPos = recordStartAddr();
// curOffset is equivalent to a byte length of this row.
PlatformDependent.putInt(rowHeaderPos, curOffset);
rowHeaderPos += SizeOf.SIZE_OF_INT;
//set null flags
byte [] flags = nullFlags.toArray();
PlatformDependent.putShort(rowHeaderPos, (short) flags.length);
rowHeaderPos += SizeOf.SIZE_OF_SHORT;
PlatformDependent.copyMemory(flags, 0, rowHeaderPos, flags.length);
rowBlock.setRows(rowBlock.rows() + 1);
}
static void setMediumLE(byte[] array, int index, int value) {
PlatformDependent.putByte(array, index, (byte) value);
if (UNALIGNED) {
PlatformDependent.putShort(array, index + 1,
BIG_ENDIAN_NATIVE_ORDER ? Short.reverseBytes((short) (value >>> 8))
: (short) (value >>> 8));
} else {
PlatformDependent.putByte(array, index + 1, (byte) (value >>> 8));
PlatformDependent.putByte(array, index + 2, (byte) (value >>> 16));
}
}
@Test
public void bigintAppend(){
final int count = 1024;
try(
BigIntVector in = new BigIntVector("in", allocator);
BigIntVector out = new BigIntVector("out", allocator);
){
in.allocateNew(count);
for(int i = 0; i < count; i++){
if(i % 5 == 0){
in.setSafe(i, i);
}
}
in.setValueCount(count);
List<FieldBufferCopier> copiers = FieldBufferCopier.getCopiers(ImmutableList.<FieldVector>of(in), ImmutableList.<FieldVector>of(out));
try(
final SelectionVector2 sv2 = new SelectionVector2(allocator);
){
sv2.allocateNew(count / 2);
// set alternate elements.
int x = 0;
for(long mem = sv2.memoryAddress(); mem < sv2.memoryAddress() + count; mem+=2){
PlatformDependent.putShort(mem, (short) (char) x);
x += 2;
}
sv2.setRecordCount(count/2);
append(copiers, sv2);
out.setValueCount(count / 2);
for(int i =0; i < count / 2; i++){
assertEquals(in.getObject(i * 2), out.getObject(i));
}
}
}
}
@Test
public void intAppend(){
final int count = 1024;
try(
IntVector in = new IntVector("in", allocator);
IntVector out = new IntVector("out", allocator);
){
in.allocateNew(count);
for(int i = 0; i < count; i++){
if(i % 5 == 0){
in.setSafe(i, i);
}
}
in.setValueCount(count);
List<FieldBufferCopier> copiers = FieldBufferCopier.getCopiers(ImmutableList.<FieldVector>of(in), ImmutableList.<FieldVector>of(out));
try(
final SelectionVector2 sv2 = new SelectionVector2(allocator);
){
sv2.allocateNew(count / 2);
// set alternate elements.
int x = 0;
for(long mem = sv2.memoryAddress(); mem < sv2.memoryAddress() + count; mem+=2){
PlatformDependent.putShort(mem, (short) (char) x);
x += 2;
}
sv2.setRecordCount(count/2);
append(copiers, sv2);
out.setValueCount(count / 2);
for(int i =0; i < count / 2; i++){
assertEquals(in.getObject(i * 2), out.getObject(i));
}
}
}
}
@Override
public ByteBuf setShort(int index, int value) {
chk(index, 2);
PlatformDependent.putShort(addr(index), (short) value);
return this;
}
@Override
public ByteBuf setChar(int index, int value) {
chk(index, 2);
PlatformDependent.putShort(addr(index), (short) value);
return this;
}
private void _setShort(int index, int value) {
PlatformDependent.putShort(addr(index), (short) value);
}
@Override
protected void _setShort(AbstractByteBuf wrapped, int index, short value) {
PlatformDependent.putShort(addr(wrapped, index), value);
}
@Override
protected void _setShort(AbstractByteBuf wrapped, int index, short value) {
PlatformDependent.putShort(wrapped.array(), idx(wrapped, index), value);
}
@Test
public void varcharRoundtrip(){
final int count = 1024;
try(
VarCharVector in = new VarCharVector("in", allocator);
VarCharVector out = new VarCharVector("out", allocator);
){
in.allocateNew(count * 8, count);
for(int i = 0; i < count; i++){
if(i % 5 == 0){
byte[] data = ("hello-" + i).getBytes(Charsets.UTF_8);
in.setSafe(i, data, 0, data.length);
}
}
in.setValueCount(count);
List<FieldBufferCopier> copiers = FieldBufferCopier.getCopiers(ImmutableList.<FieldVector>of(in), ImmutableList.<FieldVector>of(out));
try(
final SelectionVector2 sv2 = new SelectionVector2(allocator);
){
sv2.allocateNew(count);
// create full sv2.
int x = 0;
for(long mem = sv2.memoryAddress(); mem < sv2.memoryAddress() + count * 2; mem+=2){
PlatformDependent.putShort(mem, (short) (char) x);
x++;
}
sv2.setRecordCount(count);
copy(copiers, sv2);
out.setValueCount(count);
for(int i =0; i < count; i++){
assertEquals(in.getObject(i), out.getObject(i));
}
}
}
}
private void setLinks(long indexAddr, final int buildBatch, final int records){
for(int incomingRecordIndex = 0; incomingRecordIndex < records; incomingRecordIndex++, indexAddr+=4){
final int hashTableIndex = PlatformDependent.getInt(indexAddr);
if(hashTableIndex == -1){
continue;
}
if (hashTableIndex > maxHashTableIndex) {
maxHashTableIndex = hashTableIndex;
}
/* Use the global index returned by the hash table, to store
* the current record index and batch index. This will be used
* later when we probe and find a match.
*/
/* set the current record batch index and the index
* within the batch at the specified keyIndex. The keyIndex
* denotes the global index where the key for this record is
* stored in the hash table
*/
int hashTableBatch = hashTableIndex >>> 16;
int hashTableOffset = hashTableIndex & BATCH_MASK;
ArrowBuf startIndex;
try {
startIndex = startIndices.get(hashTableBatch);
} catch (IndexOutOfBoundsException e){
UserException.Builder b = UserException.functionError()
.message("Index out of bounds in VectorizedHashJoin. Index = %d, size = %d", hashTableBatch, startIndices.size())
.addContext("incomingRecordIndex=%d, hashTableIndex=%d", incomingRecordIndex, hashTableIndex);
if (debugInsertion) {
b.addContext(table.traceReport());
}
throw b.build(logger);
}
final long startIndexMemStart = startIndex.memoryAddress() + hashTableOffset * HashTable.BUILD_RECORD_LINK_SIZE;
// If head of the list is empty, insert current index at this position
final int linkBatch = PlatformDependent.getInt(startIndexMemStart);
if (linkBatch == INDEX_EMPTY) {
PlatformDependent.putInt(startIndexMemStart, buildBatch);
PlatformDependent.putShort(startIndexMemStart + 4, (short) incomingRecordIndex);
} else {
/* Head of this list is not empty, if the first link
* is empty insert there
*/
hashTableBatch = linkBatch;
hashTableOffset = Short.toUnsignedInt(PlatformDependent.getShort(startIndexMemStart + 4));
final ArrowBuf firstLink = buildInfoList.get(hashTableBatch).getLinks();
final long firstLinkMemStart = firstLink.memoryAddress() + hashTableOffset * HashTable.BUILD_RECORD_LINK_SIZE;
final int firstLinkBatch = PlatformDependent.getInt(firstLinkMemStart);
if (firstLinkBatch == INDEX_EMPTY) {
PlatformDependent.putInt(firstLinkMemStart, buildBatch);
PlatformDependent.putShort(firstLinkMemStart + 4, (short) incomingRecordIndex);
} else {
/* Insert the current value as the first link and
* make the current first link as its next
*/
final int firstLinkOffset = Short.toUnsignedInt(PlatformDependent.getShort(firstLinkMemStart + 4));
final ArrowBuf nextLink = buildInfoList.get(buildBatch).getLinks();
final long nextLinkMemStart = nextLink.memoryAddress() + incomingRecordIndex * HashTable.BUILD_RECORD_LINK_SIZE;
PlatformDependent.putInt(nextLinkMemStart, firstLinkBatch);
PlatformDependent.putShort(nextLinkMemStart + 4, (short) firstLinkOffset);
// As the existing (batch, offset) pair is moved out of firstLink into nextLink,
// now put the new (batch, offset) in the firstLink
PlatformDependent.putInt(firstLinkMemStart, buildBatch);
PlatformDependent.putShort(firstLinkMemStart + 4, (short) incomingRecordIndex);
}
}
}
}
/**
* Copy a short value to the dest+destIndex
*
* @param dest destination byte array
* @param destIndex destination index
* @param value a short value
*/
public static void putShort(byte[] dest, int destIndex, short value) {
check(destIndex, SHORT_NUM_BYTES, dest.length);
PlatformDependent.putShort(dest, destIndex, value);
}