Java Code Examples for org.apache.hadoop.hbase.util.Bytes#putShort()

The following examples show how to use org.apache.hadoop.hbase.util.Bytes#putShort() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: phoenix   File: ValueBitSet.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Serialize the value bit set into a byte array. The byte array
 * is expected to have enough room (use {@link #getEstimatedLength()}
 * to ensure enough room exists.
 * @param b the byte array into which to put the serialized bit set
 * @param offset the offset into the byte array
 * @return the incremented offset
 */
public int toBytes(byte[] b, int offset) {
    if (schema == null) {
        return offset;
    }
    // If the total number of possible values is bigger than 16 bits (the
    // size of a short), then serialize the long array followed by the
    // array length.
    if (isVarLength()) {
        short nLongs = (short)((maxSetBit + BITS_PER_LONG) / BITS_PER_LONG);
        for (int i = 0; i < nLongs; i++) {
            offset = Bytes.putLong(b, offset, bits[i]);
        }
        offset = Bytes.putShort(b, offset, nLongs);
    } else { 
        // Else if the number of values is less than or equal to 16,
        // serialize the bits directly into a short.
        offset = Bytes.putShort(b, offset, (short)bits[0]);            
    }
    return offset;
}
 
Example 2
Source Project: phoenix   File: ValueBitSet.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Serialize the value bit set into a byte array. The byte array
 * is expected to have enough room (use {@link #getEstimatedLength()}
 * to ensure enough room exists.
 * @param b the byte array into which to put the serialized bit set
 * @param offset the offset into the byte array
 * @return the incremented offset
 */
public int toBytes(byte[] b, int offset) {
    if (schema == null) {
        return offset;
    }
    // If the total number of possible values is bigger than 16 bits (the
    // size of a short), then serialize the long array followed by the
    // array length.
    if (isVarLength()) {
        short nLongs = (short)((maxSetBit + BITS_PER_LONG) / BITS_PER_LONG);
        for (int i = 0; i < nLongs; i++) {
            offset = Bytes.putLong(b, offset, bits[i]);
        }
        offset = Bytes.putShort(b, offset, nLongs);
    } else { 
        // Else if the number of values is less than or equal to 16,
        // serialize the bits directly into a short.
        offset = Bytes.putShort(b, offset, (short)bits[0]);            
    }
    return offset;
}
 
Example 3
/**
 * Serialize the value bit set into a byte array. The byte array
 * is expected to have enough room (use {@link #getEstimatedLength()}
 * to ensure enough room exists.
 * @param b the byte array into which to put the serialized bit set
 * @param offset the offset into the byte array
 * @return the incremented offset
 */
public int toBytes(byte[] b, int offset) {
    if (schema == null) {
        return offset;
    }
    // If the total number of possible values is bigger than 16 bits (the
    // size of a short), then serialize the long array followed by the
    // array length.
    if (isVarLength()) {
        short nLongs = (short)((maxSetBit + BITS_PER_LONG - 1) / BITS_PER_LONG);
        for (int i = 0; i < nLongs; i++) {
            offset = Bytes.putLong(b, offset, bits[i]);
        }
        offset = Bytes.putShort(b, offset, nLongs);
    } else { 
        // Else if the number of values is less than or equal to 16,
        // serialize the bits directly into a short.
        offset = Bytes.putShort(b, offset, (short)bits[0]);            
    }
    return offset;
}
 
Example 4
Source Project: canal-1.1.3   File: PhTypeUtil.java    License: Apache License 2.0 5 votes vote down vote up
private static int encodeUnsignedShort(short v, byte[] b, int o) {
    checkForSufficientLength(b, o, Bytes.SIZEOF_SHORT);
    if (v < 0) {
        throw new RuntimeException();
    }
    Bytes.putShort(b, o, v);
    return Bytes.SIZEOF_SHORT;
}
 
Example 5
Source Project: phoenix   File: PUnsignedSmallint.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public int encodeShort(short v, byte[] b, int o) {
  checkForSufficientLength(b, o, Bytes.SIZEOF_SHORT);
  if (v < 0) {
    throw newIllegalDataException();
  }
  Bytes.putShort(b, o, v);
  return Bytes.SIZEOF_SHORT;
}
 
Example 6
Source Project: hbase   File: KeyValue.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Write KeyValue format into a byte array.
 * @param row row key
 * @param roffset row offset
 * @param rlength row length
 * @param family family name
 * @param foffset family offset
 * @param flength family length
 * @param qualifier column qualifier
 * @param qoffset qualifier offset
 * @param qlength qualifier length
 * @param timestamp version timestamp
 * @param type key type
 * @param value column value
 * @param voffset value offset
 * @param vlength value length
 * @return The newly created byte array.
 */
private static byte [] createByteArray(final byte [] row, final int roffset,
    final int rlength, final byte [] family, final int foffset, int flength,
    final byte [] qualifier, final int qoffset, int qlength,
    final long timestamp, final Type type,
    final byte [] value, final int voffset,
    int vlength, byte[] tags, int tagsOffset, int tagsLength) {

  checkParameters(row, rlength, family, flength, qlength, vlength);
  RawCell.checkForTagsLength(tagsLength);
  // Allocate right-sized byte array.
  int keyLength = (int) getKeyDataStructureSize(rlength, flength, qlength);
  byte[] bytes = new byte[(int) getKeyValueDataStructureSize(rlength, flength, qlength, vlength,
    tagsLength)];
  // Write key, value and key row length.
  int pos = 0;
  pos = Bytes.putInt(bytes, pos, keyLength);
  pos = Bytes.putInt(bytes, pos, vlength);
  pos = Bytes.putShort(bytes, pos, (short)(rlength & 0x0000ffff));
  pos = Bytes.putBytes(bytes, pos, row, roffset, rlength);
  pos = Bytes.putByte(bytes, pos, (byte)(flength & 0x0000ff));
  if(flength != 0) {
    pos = Bytes.putBytes(bytes, pos, family, foffset, flength);
  }
  if(qlength != 0) {
    pos = Bytes.putBytes(bytes, pos, qualifier, qoffset, qlength);
  }
  pos = Bytes.putLong(bytes, pos, timestamp);
  pos = Bytes.putByte(bytes, pos, type.getCode());
  if (value != null && value.length > 0) {
    pos = Bytes.putBytes(bytes, pos, value, voffset, vlength);
  }
  // Add the tags after the value part
  if (tagsLength > 0) {
    pos = Bytes.putAsShort(bytes, pos, tagsLength);
    pos = Bytes.putBytes(bytes, pos, tags, tagsOffset, tagsLength);
  }
  return bytes;
}
 
Example 7
Source Project: hbase   File: KeyValueUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static int appendKeyTo(final Cell cell, final byte[] output,
    final int offset) {
  int nextOffset = offset;
  nextOffset = Bytes.putShort(output, nextOffset, cell.getRowLength());
  nextOffset = CellUtil.copyRowTo(cell, output, nextOffset);
  nextOffset = Bytes.putByte(output, nextOffset, cell.getFamilyLength());
  nextOffset = CellUtil.copyFamilyTo(cell, output, nextOffset);
  nextOffset = CellUtil.copyQualifierTo(cell, output, nextOffset);
  nextOffset = Bytes.putLong(output, nextOffset, cell.getTimestamp());
  nextOffset = Bytes.putByte(output, nextOffset, cell.getTypeByte());
  return nextOffset;
}
 
Example 8
Source Project: phoenix   File: PArrayDataType.java    License: Apache License 2.0 5 votes vote down vote up
private static void writeNewOffsets(byte[] arrayBytes, byte[] newArray, boolean useShortNew,
        boolean useShortPrevious, int newOffsetArrayPosition, int arrayLength, int offsetArrayPosition, int offset,
        int offsetShift, int length) {
    int currentPosition = newOffsetArrayPosition;
    int offsetArrayElementSize = useShortNew ? Bytes.SIZEOF_SHORT : Bytes.SIZEOF_INT;
    if (useShortNew) {
        Bytes.putShort(newArray, currentPosition, (short)(0 - Short.MAX_VALUE));
    } else {
        Bytes.putInt(newArray, currentPosition, 0);
    }

    currentPosition += offsetArrayElementSize;
    boolean nullsAtBeginning = true;
    byte serializationVersion = arrayBytes[offset + length - Bytes.SIZEOF_BYTE];
    for (int arrayIndex = 0; arrayIndex < arrayLength - 1; arrayIndex++) {
        int oldOffset = getOffset(arrayBytes, arrayIndex, useShortPrevious, offsetArrayPosition + offset, serializationVersion);
        if (arrayBytes[offset + oldOffset] == QueryConstants.SEPARATOR_BYTE && nullsAtBeginning) {
            if (useShortNew) {
                Bytes.putShort(newArray, currentPosition, (short)(oldOffset - Short.MAX_VALUE));
            } else {
                Bytes.putInt(newArray, currentPosition, oldOffset);
            }
        } else {
            if (useShortNew) {
                Bytes.putShort(newArray, currentPosition, (short)(oldOffset + offsetShift - Short.MAX_VALUE));
            } else {
                Bytes.putInt(newArray, currentPosition, oldOffset + offsetShift);
            }
            nullsAtBeginning = false;
        }
        currentPosition += offsetArrayElementSize;
    }

    Bytes.putInt(newArray, currentPosition, newOffsetArrayPosition);
    currentPosition += Bytes.SIZEOF_INT;
    Bytes.putInt(newArray, currentPosition, useShortNew ? arrayLength : -arrayLength);
    currentPosition += Bytes.SIZEOF_INT;
    Bytes.putByte(newArray, currentPosition, arrayBytes[offset + length - 1]);
}
 
Example 9
Source Project: phoenix   File: PUnsignedSmallint.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public int encodeShort(short v, byte[] b, int o) {
  checkForSufficientLength(b, o, Bytes.SIZEOF_SHORT);
  if (v < 0) {
    throw newIllegalDataException();
  }
  Bytes.putShort(b, o, v);
  return Bytes.SIZEOF_SHORT;
}
 
Example 10
Source Project: canal   File: PhTypeUtil.java    License: Apache License 2.0 5 votes vote down vote up
private static int encodeUnsignedShort(short v, byte[] b, int o) {
    checkForSufficientLength(b, o, Bytes.SIZEOF_SHORT);
    if (v < 0) {
        throw new RuntimeException();
    }
    Bytes.putShort(b, o, v);
    return Bytes.SIZEOF_SHORT;
}
 
Example 11
Source Project: hbase   File: WALCellCodec.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected Cell parseCell() throws IOException {
  int keylength = StreamUtils.readRawVarint32(in);
  int vlength = StreamUtils.readRawVarint32(in);

  int tagsLength = StreamUtils.readRawVarint32(in);
  int length = 0;
  if(tagsLength == 0) {
    length = KeyValue.KEYVALUE_INFRASTRUCTURE_SIZE + keylength + vlength;
  } else {
    length = KeyValue.KEYVALUE_WITH_TAGS_INFRASTRUCTURE_SIZE + keylength + vlength + tagsLength;
  }

  byte[] backingArray = new byte[length];
  int pos = 0;
  pos = Bytes.putInt(backingArray, pos, keylength);
  pos = Bytes.putInt(backingArray, pos, vlength);

  // the row
  int elemLen = readIntoArray(backingArray, pos + Bytes.SIZEOF_SHORT,
    compression.getDictionary(CompressionContext.DictionaryIndex.ROW));
  checkLength(elemLen, Short.MAX_VALUE);
  pos = Bytes.putShort(backingArray, pos, (short)elemLen);
  pos += elemLen;

  // family
  elemLen = readIntoArray(backingArray, pos + Bytes.SIZEOF_BYTE,
    compression.getDictionary(CompressionContext.DictionaryIndex.FAMILY));
  checkLength(elemLen, Byte.MAX_VALUE);
  pos = Bytes.putByte(backingArray, pos, (byte)elemLen);
  pos += elemLen;

  // qualifier
  elemLen = readIntoArray(backingArray, pos,
    compression.getDictionary(CompressionContext.DictionaryIndex.QUALIFIER));
  pos += elemLen;

  // timestamp, type and value
  int tsTypeValLen = length - pos;
  if (tagsLength > 0) {
    tsTypeValLen = tsTypeValLen - tagsLength - KeyValue.TAGS_LENGTH_SIZE;
  }
  IOUtils.readFully(in, backingArray, pos, tsTypeValLen);
  pos += tsTypeValLen;

  // tags
  if (tagsLength > 0) {
    pos = Bytes.putAsShort(backingArray, pos, tagsLength);
    if (compression.tagCompressionContext != null) {
      compression.tagCompressionContext.uncompressTags(in, backingArray, pos, tagsLength);
    } else {
      IOUtils.readFully(in, backingArray, pos, tagsLength);
    }
  }
  return new KeyValue(backingArray, 0, length);
}
 
Example 12
Source Project: hbase   File: KeyValue.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Create an empty byte[] representing a KeyValue
 * All lengths are preset and can be filled in later.
 * @param rlength
 * @param flength
 * @param qlength
 * @param timestamp
 * @param type
 * @param vlength
 * @return The newly created byte array.
 */
private static byte[] createEmptyByteArray(final int rlength, int flength,
    int qlength, final long timestamp, final Type type, int vlength, int tagsLength) {
  if (rlength > Short.MAX_VALUE) {
    throw new IllegalArgumentException("Row > " + Short.MAX_VALUE);
  }
  if (flength > Byte.MAX_VALUE) {
    throw new IllegalArgumentException("Family > " + Byte.MAX_VALUE);
  }
  // Qualifier length
  if (qlength > Integer.MAX_VALUE - rlength - flength) {
    throw new IllegalArgumentException("Qualifier > " + Integer.MAX_VALUE);
  }
  RawCell.checkForTagsLength(tagsLength);
  // Key length
  long longkeylength = getKeyDataStructureSize(rlength, flength, qlength);
  if (longkeylength > Integer.MAX_VALUE) {
    throw new IllegalArgumentException("keylength " + longkeylength + " > " +
      Integer.MAX_VALUE);
  }
  int keylength = (int)longkeylength;
  // Value length
  if (vlength > HConstants.MAXIMUM_VALUE_LENGTH) { // FindBugs INT_VACUOUS_COMPARISON
    throw new IllegalArgumentException("Valuer > " +
        HConstants.MAXIMUM_VALUE_LENGTH);
  }

  // Allocate right-sized byte array.
  byte[] bytes= new byte[(int) getKeyValueDataStructureSize(rlength, flength, qlength, vlength,
      tagsLength)];
  // Write the correct size markers
  int pos = 0;
  pos = Bytes.putInt(bytes, pos, keylength);
  pos = Bytes.putInt(bytes, pos, vlength);
  pos = Bytes.putShort(bytes, pos, (short)(rlength & 0x0000ffff));
  pos += rlength;
  pos = Bytes.putByte(bytes, pos, (byte)(flength & 0x0000ff));
  pos += flength + qlength;
  pos = Bytes.putLong(bytes, pos, timestamp);
  pos = Bytes.putByte(bytes, pos, type.getCode());
  pos += vlength;
  if (tagsLength > 0) {
    pos = Bytes.putAsShort(bytes, pos, tagsLength);
  }
  return bytes;
}
 
Example 13
Source Project: hbase   File: RawShort.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public int encode(PositionedByteRange dst, Short val) {
  Bytes.putShort(dst.getBytes(), dst.getOffset() + dst.getPosition(), val);
  return skip(dst);
}
 
Example 14
Source Project: hbase   File: RawShort.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Write instance {@code val} into buffer {@code buff}.
 */
public int encodeShort(byte[] buff, int offset, short val) {
  return Bytes.putShort(buff, offset, val);
}