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

The following examples show how to use org.apache.hadoop.hbase.util.Bytes#putAsShort() . 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: PrivateCellUtil.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Writes the cell to the given OutputStream
 * @param cell the cell to be written
 * @param out the outputstream
 * @param withTags if tags are to be written or not
 * @return the total bytes written
 * @throws IOException
 */
public static int writeCell(Cell cell, OutputStream out, boolean withTags) throws IOException {
  if (cell instanceof ExtendedCell) {
    return ((ExtendedCell) cell).write(out, withTags);
  } else {
    ByteBufferUtils.putInt(out, estimatedSerializedSizeOfKey(cell));
    ByteBufferUtils.putInt(out, cell.getValueLength());
    writeFlatKey(cell, out);
    writeValue(out, cell, cell.getValueLength());
    int tagsLength = cell.getTagsLength();
    if (withTags) {
      byte[] len = new byte[Bytes.SIZEOF_SHORT];
      Bytes.putAsShort(len, 0, tagsLength);
      out.write(len);
      if (tagsLength > 0) {
        writeTags(out, cell, tagsLength);
      }
    }
    int lenWritten = (2 * Bytes.SIZEOF_INT) + estimatedSerializedSizeOfKey(cell)
        + cell.getValueLength();
    if (withTags) {
      lenWritten += Bytes.SIZEOF_SHORT + tagsLength;
    }
    return lenWritten;
  }
}
 
Example 2
Source File: TagCompressionContext.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Uncompress tags from the InputStream and writes to the destination array.
 * @param src Stream where the compressed tags are available
 * @param dest Destination array where to write the uncompressed tags
 * @param offset Offset in destination where tags to be written
 * @param length Length of all tag bytes
 * @throws IOException
 */
public void uncompressTags(InputStream src, byte[] dest, int offset, int length)
    throws IOException {
  int endOffset = offset + length;
  while (offset < endOffset) {
    byte status = (byte) src.read();
    if (status == Dictionary.NOT_IN_DICTIONARY) {
      int tagLen = StreamUtils.readRawVarint32(src);
      offset = Bytes.putAsShort(dest, offset, tagLen);
      IOUtils.readFully(src, dest, offset, tagLen);
      tagDict.addEntry(dest, offset, tagLen);
      offset += tagLen;
    } else {
      short dictIdx = StreamUtils.toShort(status, (byte) src.read());
      byte[] entry = tagDict.getEntry(dictIdx);
      if (entry == null) {
        throw new IOException("Missing dictionary entry for index " + dictIdx);
      }
      offset = Bytes.putAsShort(dest, offset, entry.length);
      System.arraycopy(entry, 0, dest, offset, entry.length);
      offset += entry.length;
    }
  }
}
 
Example 3
Source File: KeyValue.java    From hbase with 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 4
Source File: KeyValueUtil.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**************** copy key and value *********************/

  public static int appendToByteArray(Cell cell, byte[] output, int offset, boolean withTags) {
    int pos = offset;
    pos = Bytes.putInt(output, pos, keyLength(cell));
    pos = Bytes.putInt(output, pos, cell.getValueLength());
    pos = appendKeyTo(cell, output, pos);
    pos = CellUtil.copyValueTo(cell, output, pos);
    if (withTags && (cell.getTagsLength() > 0)) {
      pos = Bytes.putAsShort(output, pos, cell.getTagsLength());
      pos = PrivateCellUtil.copyTagsTo(cell, output, pos);
    }
    return pos;
  }
 
Example 5
Source File: ArrayBackedTag.java    From hbase with 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 6
Source File: TagCompressionContext.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Uncompress tags from the input ByteBuffer and writes to the destination array.
 * @param src Buffer where the compressed tags are available
 * @param dest Destination array where to write the uncompressed tags
 * @param offset Offset in destination where tags to be written
 * @param length Length of all tag bytes
 * @return bytes count read from source to uncompress all tags.
 * @throws IOException
 */
public int uncompressTags(ByteBuff src, byte[] dest, int offset, int length)
    throws IOException {
  int srcBeginPos = src.position();
  int endOffset = offset + length;
  while (offset < endOffset) {
    byte status = src.get();
    int tagLen;
    if (status == Dictionary.NOT_IN_DICTIONARY) {
      tagLen = StreamUtils.readRawVarint32(src);
      offset = Bytes.putAsShort(dest, offset, tagLen);
      src.get(dest, offset, tagLen);
      tagDict.addEntry(dest, offset, tagLen);
      offset += tagLen;
    } else {
      short dictIdx = StreamUtils.toShort(status, src.get());
      byte[] entry = tagDict.getEntry(dictIdx);
      if (entry == null) {
        throw new IOException("Missing dictionary entry for index " + dictIdx);
      }
      tagLen = entry.length;
      offset = Bytes.putAsShort(dest, offset, tagLen);
      System.arraycopy(entry, 0, dest, offset, tagLen);
      offset += tagLen;
    }
  }
  return src.position() - srcBeginPos;
}
 
Example 7
Source File: WALCellCodec.java    From hbase with 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 8
Source File: KeyValue.java    From hbase with 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;
}