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

The following examples show how to use org.apache.hadoop.hbase.util.Bytes#putByte() . 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: canal-1.1.3   File: PhTypeUtil.java    License: Apache License 2.0 5 votes vote down vote up
private static int encodeUnsignedByte(byte v, byte[] b, int o) {
    if (v < 0) {
        throw new RuntimeException();
    }
    Bytes.putByte(b, o, v);
    return Bytes.SIZEOF_BYTE;
}
 
Example 2
Source Project: phoenix   File: PUnsignedTinyint.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public int encodeByte(byte v, byte[] b, int o) {
  if (v < 0) {
    throw newIllegalDataException();
  }
  Bytes.putByte(b, o, v);
  return Bytes.SIZEOF_BYTE;
}
 
Example 3
Source Project: hbase   File: HFileBlock.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Put the header into the given byte array at the given offset.
 * @param onDiskSize size of the block on disk header + data + checksum
 * @param uncompressedSize size of the block after decompression (but
 *          before optional data block decoding) including header
 * @param onDiskDataSize size of the block on disk with header
 *        and data but not including the checksums
 */
private void putHeader(byte[] dest, int offset, int onDiskSize,
    int uncompressedSize, int onDiskDataSize) {
  offset = blockType.put(dest, offset);
  offset = Bytes.putInt(dest, offset, onDiskSize - HConstants.HFILEBLOCK_HEADER_SIZE);
  offset = Bytes.putInt(dest, offset, uncompressedSize - HConstants.HFILEBLOCK_HEADER_SIZE);
  offset = Bytes.putLong(dest, offset, prevOffset);
  offset = Bytes.putByte(dest, offset, fileContext.getChecksumType().getCode());
  offset = Bytes.putInt(dest, offset, fileContext.getBytesPerChecksum());
  Bytes.putInt(dest, offset, onDiskDataSize);
}
 
Example 4
Source Project: hbase   File: ZKMetadata.java    License: Apache License 2.0 5 votes vote down vote up
public static byte[] appendMetaData(byte[] id, byte[] data) {
  if (data == null || data.length == 0) {
    return data;
  }
  byte[] salt = Bytes.toBytes(ThreadLocalRandom.current().nextLong());
  int idLength = id.length + salt.length;
  byte[] newData = new byte[MAGIC_SIZE + ID_LENGTH_SIZE + idLength + data.length];
  int pos = 0;
  pos = Bytes.putByte(newData, pos, MAGIC);
  pos = Bytes.putInt(newData, pos, idLength);
  pos = Bytes.putBytes(newData, pos, id, 0, id.length);
  pos = Bytes.putBytes(newData, pos, salt, 0, salt.length);
  pos = Bytes.putBytes(newData, pos, data, 0, data.length);
  return newData;
}
 
Example 5
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 6
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 7
Source Project: hbase   File: ArrayBackedTag.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Format for a tag :
 * {@code <length of tag - 2 bytes><type code - 1 byte><tag>} tag length is serialized
 * using 2 bytes only but as this will be unsigned, we can have max tag length of
 * (Short.MAX_SIZE * 2) +1. It includes 1 byte type length and actual tag bytes length.
 */
public ArrayBackedTag(byte tagType, byte[] tag) {
  int tagLength = tag.length + TYPE_LENGTH_SIZE;
  if (tagLength > MAX_TAG_LENGTH) {
    throw new IllegalArgumentException(
        "Invalid tag data being passed. Its length can not exceed " + MAX_TAG_LENGTH);
  }
  length = TAG_LENGTH_SIZE + tagLength;
  bytes = new byte[length];
  int pos = Bytes.putAsShort(bytes, 0, tagLength);
  pos = Bytes.putByte(bytes, pos, tagType);
  Bytes.putBytes(bytes, pos, tag, 0, tag.length);
  this.type = tagType;
}
 
Example 8
Source Project: phoenix   File: PArrayDataType.java    License: Apache License 2.0 5 votes vote down vote up
private static void writeEndBytes(byte[] array, int newOffsetArrayPosition, int offsetArrayLength, int arrayLength,
        byte header, boolean useInt) {
    int byteSize = useInt ? Bytes.SIZEOF_INT : Bytes.SIZEOF_SHORT;

    Bytes.putInt(array, newOffsetArrayPosition + offsetArrayLength + byteSize, newOffsetArrayPosition);
    Bytes.putInt(array, newOffsetArrayPosition + offsetArrayLength + byteSize + Bytes.SIZEOF_INT, arrayLength);
    Bytes.putByte(array, newOffsetArrayPosition + offsetArrayLength + byteSize + 2 * Bytes.SIZEOF_INT, header);
}
 
Example 9
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 10
Source Project: phoenix   File: PUnsignedTinyint.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public int encodeByte(byte v, byte[] b, int o) {
  if (v < 0) {
    throw newIllegalDataException();
  }
  Bytes.putByte(b, o, v);
  return Bytes.SIZEOF_BYTE;
}
 
Example 11
Source Project: canal   File: PhTypeUtil.java    License: Apache License 2.0 5 votes vote down vote up
private static int encodeUnsignedByte(byte v, byte[] b, int o) {
    if (v < 0) {
        throw new RuntimeException();
    }
    Bytes.putByte(b, o, v);
    return Bytes.SIZEOF_BYTE;
}
 
Example 12
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 13
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 14
Source Project: hbase   File: KeyValue.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * This is a HFile block index key optimization.
 * @param leftKey
 * @param rightKey
 * @return 0 if equal, &lt;0 if left smaller, &gt;0 if right smaller
 * @deprecated Since 0.99.2;
 */
@Deprecated
public byte[] getShortMidpointKey(final byte[] leftKey, final byte[] rightKey) {
  if (rightKey == null) {
    throw new IllegalArgumentException("rightKey can not be null");
  }
  if (leftKey == null) {
    return Arrays.copyOf(rightKey, rightKey.length);
  }
  if (compareFlatKey(leftKey, rightKey) >= 0) {
    throw new IllegalArgumentException("Unexpected input, leftKey:" + Bytes.toString(leftKey)
      + ", rightKey:" + Bytes.toString(rightKey));
  }

  short leftRowLength = Bytes.toShort(leftKey, 0);
  short rightRowLength = Bytes.toShort(rightKey, 0);
  int leftCommonLength = ROW_LENGTH_SIZE + FAMILY_LENGTH_SIZE + leftRowLength;
  int rightCommonLength = ROW_LENGTH_SIZE + FAMILY_LENGTH_SIZE + rightRowLength;
  int leftCommonLengthWithTSAndType = TIMESTAMP_TYPE_SIZE + leftCommonLength;
  int rightCommonLengthWithTSAndType = TIMESTAMP_TYPE_SIZE + rightCommonLength;
  int leftColumnLength = leftKey.length - leftCommonLengthWithTSAndType;
  int rightColumnLength = rightKey.length - rightCommonLengthWithTSAndType;
  // rows are equal
  if (leftRowLength == rightRowLength && compareRows(leftKey, ROW_LENGTH_SIZE, leftRowLength,
    rightKey, ROW_LENGTH_SIZE, rightRowLength) == 0) {
    // Compare family & qualifier together.
    int comparison = Bytes.compareTo(leftKey, leftCommonLength, leftColumnLength, rightKey,
      rightCommonLength, rightColumnLength);
    // same with "row + family + qualifier", return rightKey directly
    if (comparison == 0) {
      return Arrays.copyOf(rightKey, rightKey.length);
    }
    // "family + qualifier" are different, generate a faked key per rightKey
    byte[] newKey = Arrays.copyOf(rightKey, rightKey.length);
    Bytes.putLong(newKey, rightKey.length - TIMESTAMP_TYPE_SIZE, HConstants.LATEST_TIMESTAMP);
    Bytes.putByte(newKey, rightKey.length - TYPE_SIZE, Type.Maximum.getCode());
    return newKey;
  }
  // rows are different
  short minLength = leftRowLength < rightRowLength ? leftRowLength : rightRowLength;
  short diffIdx = 0;
  while (diffIdx < minLength
      && leftKey[ROW_LENGTH_SIZE + diffIdx] == rightKey[ROW_LENGTH_SIZE + diffIdx]) {
    diffIdx++;
  }
  byte[] newRowKey = null;
  if (diffIdx >= minLength) {
    // leftKey's row is prefix of rightKey's.
    newRowKey = new byte[diffIdx + 1];
    System.arraycopy(rightKey, ROW_LENGTH_SIZE, newRowKey, 0, diffIdx + 1);
  } else {
    int diffByte = leftKey[ROW_LENGTH_SIZE + diffIdx];
    if ((0xff & diffByte) < 0xff && (diffByte + 1) <
        (rightKey[ROW_LENGTH_SIZE + diffIdx] & 0xff)) {
      newRowKey = new byte[diffIdx + 1];
      System.arraycopy(leftKey, ROW_LENGTH_SIZE, newRowKey, 0, diffIdx);
      newRowKey[diffIdx] = (byte) (diffByte + 1);
    } else {
      newRowKey = new byte[diffIdx + 1];
      System.arraycopy(rightKey, ROW_LENGTH_SIZE, newRowKey, 0, diffIdx + 1);
    }
  }
  return new KeyValue(newRowKey, null, null, HConstants.LATEST_TIMESTAMP,
    Type.Maximum).getKey();
}
 
Example 15
Source Project: hbase   File: RawByte.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public int encode(PositionedByteRange dst, Byte val) {
  Bytes.putByte(dst.getBytes(), dst.getOffset() + dst.getPosition(), val);
  return skip(dst);
}
 
Example 16
Source Project: hbase   File: RawByte.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Write instance {@code val} into buffer {@code buff}.
 */
public int encodeByte(byte[] buff, int offset, byte val) {
  return Bytes.putByte(buff, offset, val);
}