Java Code Examples for io.netty.util.internal.PlatformDependent#putShort()

The following examples show how to use io.netty.util.internal.PlatformDependent#putShort() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestCopierRoundTrip.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@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));
      }
    }
  }
}
 
Example 2
Source File: DrillBuf.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuf writeShort(int value) {
  BoundsChecking.ensureWritable(this, 2);
  PlatformDependent.putShort(addr(writerIndex), (short) value);
  writerIndex += 2;
  return this;
}
 
Example 3
Source File: DrillBuf.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuf writeChar(int value) {
  BoundsChecking.ensureWritable(this, 2);
  PlatformDependent.putShort(addr(writerIndex), (short) value);
  writerIndex += 2;
  return this;
}
 
Example 4
Source File: TestCopierRoundTrip.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@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));
      }
    }
  }
}
 
Example 5
Source File: TestCopierRoundTrip.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@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));
      }
    }
  }
}
 
Example 6
Source File: UnsafeByteBufUtil.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
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));
    }
}
 
Example 7
Source File: CompactRowBlockWriter.java    From tajo with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 8
Source File: UnsafeByteBufUtil.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 9
Source File: UnsafeByteBufUtil.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
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));
    }
}
 
Example 10
Source File: UnsafeByteBufUtil.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 11
Source File: UnsafeByteBufUtil.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
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));
    }
}
 
Example 12
Source File: UnsafeByteBufUtil.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 13
Source File: TestCopierRoundTrip.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@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));
      }
    }
  }
}
 
Example 14
Source File: VectorizedHashJoinOperator.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
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);
      }
    }
  }
}
 
Example 15
Source File: DrillBuf.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public ByteBuf setShort(int index, int value) {
  chk(index, 2);
  PlatformDependent.putShort(addr(index), (short) value);
  return this;
}
 
Example 16
Source File: UnsafeHeapSwappedByteBuf.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
protected void _setShort(AbstractByteBuf wrapped, int index, short value) {
    PlatformDependent.putShort(wrapped.array(), idx(wrapped, index), value);
}
 
Example 17
Source File: UnsafeDirectSwappedByteBuf.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
protected void _setShort(AbstractByteBuf wrapped, int index, short value) {
    PlatformDependent.putShort(addr(wrapped, index), value);
}
 
Example 18
Source File: UnsafeDirectLittleEndian.java    From Bats with Apache License 2.0 4 votes vote down vote up
private void _setShort(int index, int value) {
  PlatformDependent.putShort(addr(index), (short) value);
}
 
Example 19
Source File: DrillBuf.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public ByteBuf setChar(int index, int value) {
  chk(index, 2);
  PlatformDependent.putShort(addr(index), (short) value);
  return this;
}
 
Example 20
Source File: DrillBuf.java    From Bats with Apache License 2.0 2 votes vote down vote up
/**
 * 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);
}