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

The following are Jave code examples for showing how to use putInt() of the org.apache.hadoop.hbase.util.Bytes class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: ditb   File: BalancerTestBase.java   View Source Code Vote up 6 votes
protected List<HRegionInfo> randomRegions(int numRegions, int numTables) {
  List<HRegionInfo> regions = new ArrayList<HRegionInfo>(numRegions);
  byte[] start = new byte[16];
  byte[] end = new byte[16];
  rand.nextBytes(start);
  rand.nextBytes(end);
  for (int i = 0; i < numRegions; i++) {
    if (!regionQueue.isEmpty()) {
      regions.add(regionQueue.poll());
      continue;
    }
    Bytes.putInt(start, 0, numRegions << 1);
    Bytes.putInt(end, 0, (numRegions << 1) + 1);
    TableName tableName =
        TableName.valueOf("table" + (numTables > 0 ? rand.nextInt(numTables) : i));
    HRegionInfo hri = new HRegionInfo(tableName, start, end, false, regionId++);
    regions.add(hri);
  }
  return regions;
}
 
Example 2
Project: ditb   File: RecoverableZooKeeper.java   View Source Code Vote up 6 votes
private byte[] appendMetaData(byte[] data) {
  if(data == null || data.length == 0){
    return data;
  }
  byte[] salt = Bytes.toBytes(salter.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 3
Project: ditb   File: KeyOnlyFilter.java   View Source Code Vote up 6 votes
private Cell createKeyOnlyCell(Cell c) {
  // KV format: <keylen:4><valuelen:4><key:keylen><value:valuelen>
  // Rebuild as: <keylen:4><0:4><key:keylen>
  int dataLen = lenAsVal ? Bytes.SIZEOF_INT : 0;
  int keyOffset = (2 * Bytes.SIZEOF_INT);
  int keyLen = KeyValueUtil.keyLength(c);
  byte[] newBuffer = new byte[keyLen + keyOffset + dataLen];
  Bytes.putInt(newBuffer, 0, keyLen);
  Bytes.putInt(newBuffer, Bytes.SIZEOF_INT, dataLen);
  KeyValueUtil.appendKeyTo(c, newBuffer, keyOffset);
  if (lenAsVal) {
    Bytes.putInt(newBuffer, newBuffer.length - dataLen, c.getValueLength());
  }
  return new KeyValue(newBuffer);
}
 
Example 4
Project: ditb   File: KeyValue.java   View Source Code Vote up 6 votes
/**
 * 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);
  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 5
Project: ditb   File: KeyValue.java   View Source Code Vote up 6 votes
/**
 * Creates a new KeyValue that only contains the key portion (the value is
 * set to be null).
 *
 * TODO only used by KeyOnlyFilter -- move there.
 * @param lenAsVal replace value with the actual value length (false=empty)
 */
public KeyValue createKeyOnly(boolean lenAsVal) {
  // KV format:  <keylen:4><valuelen:4><key:keylen><value:valuelen>
  // Rebuild as: <keylen:4><0:4><key:keylen>
  int dataLen = lenAsVal? Bytes.SIZEOF_INT : 0;
  byte [] newBuffer = new byte[getKeyLength() + ROW_OFFSET + dataLen];
  System.arraycopy(this.bytes, this.offset, newBuffer, 0,
      Math.min(newBuffer.length,this.length));
  Bytes.putInt(newBuffer, Bytes.SIZEOF_INT, dataLen);
  if (lenAsVal) {
    Bytes.putInt(newBuffer, newBuffer.length - dataLen, this.getValueLength());
  }
  return new KeyValue(newBuffer);
}
 
Example 6
Project: ditb   File: KeyValueUtil.java   View Source Code Vote up 6 votes
/**************** copy key and value *********************/

  public static int appendToByteArray(final Cell cell, final byte[] output, final int offset) {
    // TODO when cell instance of KV we can bypass all steps and just do backing single array
    // copy(?)
    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 ((cell.getTagsLength() > 0)) {
      pos = Bytes.putAsShort(output, pos, cell.getTagsLength());
      pos = CellUtil.copyTagTo(cell, output, pos);
    }
    return pos;
  }
 
Example 7
Project: ditb   File: KeyValueCompression.java   View Source Code Vote up 5 votes
/**
 * Uncompresses a KeyValue from a DataInput and returns it.
 * 
 * @param in the DataInput
 * @param readContext the compressionContext to use.
 * @return an uncompressed KeyValue
 * @throws IOException
 */

public static KeyValue readKV(DataInput in, CompressionContext readContext)
    throws IOException {
  int keylength = WritableUtils.readVInt(in);
  int vlength = WritableUtils.readVInt(in);
  int tagsLength = WritableUtils.readVInt(in);
  int length = (int) KeyValue.getKeyValueDataStructureSize(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 = Compressor.uncompressIntoArray(backingArray,
      pos + Bytes.SIZEOF_SHORT, in, readContext.rowDict);
  checkLength(elemLen, Short.MAX_VALUE);
  pos = Bytes.putShort(backingArray, pos, (short)elemLen);
  pos += elemLen;

  // family
  elemLen = Compressor.uncompressIntoArray(backingArray,
      pos + Bytes.SIZEOF_BYTE, in, readContext.familyDict);
  checkLength(elemLen, Byte.MAX_VALUE);
  pos = Bytes.putByte(backingArray, pos, (byte)elemLen);
  pos += elemLen;

  // qualifier
  elemLen = Compressor.uncompressIntoArray(backingArray, pos, in,
      readContext.qualifierDict);
  pos += elemLen;

  // the rest
  in.readFully(backingArray, pos, length - pos);

  return new KeyValue(backingArray, 0, length);
}
 
Example 8
Project: ditb   File: IndexKeyValue.java   View Source Code Vote up 5 votes
/**
 * Write KeyValue format into a byte array.
 * @param row
 * @param roffset
 * @param rlength
 * @param qualifier
 * @param qoffset
 * @param qlength
 * @param value
 * @param voffset
 * @param vlength
 * @return
 */
static byte[] createByteArray(byte[] row, int roffset, int rlength, byte[] qualifier,
    int qoffset, int qlength, byte[] value, int voffset, int vlength) {
  rlength = row == null ? 0 : rlength;
  if (rlength > Short.MAX_VALUE) {
    throw new IllegalArgumentException("Row > " + Short.MAX_VALUE);
  }

  // Qualifier length
  qlength = qualifier == null ? 0 : qlength;
  // Value length
  vlength = value == null ? 0 : vlength;
  if (qlength + vlength > Integer.MAX_VALUE) {
    throw new IllegalArgumentException("Qualifier + Value > " + Integer.MAX_VALUE);
  }

  // Key length
  long longkeylength = KEY_INFRASTRUCTURE_SIZE + qlength + vlength;
  if (longkeylength > Integer.MAX_VALUE) {
    throw new IllegalArgumentException("keylength " + longkeylength + " > " + Integer.MAX_VALUE);
  }
  int keylength = (int) longkeylength;

  // Allocate right-sized byte array.
  byte[] bytes = new byte[KEYVALUE_INFRASTRUCTURE_SIZE + keylength + rlength];
  // write key and row length.
  int pos = 0;
  pos = Bytes.putInt(bytes, pos, keylength);
  pos = Bytes.putInt(bytes, pos, rlength);

  // write qualifier length
  pos = Bytes.putInt(bytes, pos, qlength);
  // write qualifier and qualifier value
  if (qlength != 0) {
    pos = Bytes.putBytes(bytes, pos, qualifier, qoffset, qlength);
  }
  if (vlength != 0) {
    pos = Bytes.putBytes(bytes, pos, value, voffset, vlength);
  }

  if (rlength != 0) {
    pos = Bytes.putBytes(bytes, pos, row, roffset, rlength);
  }
  return bytes;
}
 
Example 9
Project: ditb   File: IndexKeyValue.java   View Source Code Vote up 5 votes
/**
 * @param b
 * @param o
 * @param l
 * @return A IndexKeyValue made of a byte array that holds the key-only part. Needed to convert
 *         IndexFile index members to KeyValues.
 */
public static IndexKeyValue createIndexKeyValueFromKey(final byte[] b, final int o, final int l) {
  byte[] newb = new byte[b.length + QUALIFIER_OFFSET];
  System.arraycopy(b, o, newb, QUALIFIER_OFFSET, l);
  Bytes.putInt(newb, 0, b.length);
  Bytes.putInt(newb, Bytes.SIZEOF_INT, 0);
  return new IndexKeyValue(newb);
}
 
Example 10
Project: ditb   File: HFileBlock.java   View Source Code Vote up 5 votes
/**
 * 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 11
Project: ditb   File: TestHFileBlockCompatibility.java   View Source Code Vote up 5 votes
/**
 * Put the header into the given byte array at the given offset.
 * @param onDiskSize size of the block on disk
 * @param uncompressedSize size of the block after decompression (but
 *          before optional data block decoding)
 */
private void putHeader(byte[] dest, int offset, int onDiskSize,
    int uncompressedSize) {
  offset = blockType.put(dest, offset);
  offset = Bytes.putInt(dest, offset, onDiskSize - HEADER_SIZE);
  offset = Bytes.putInt(dest, offset, uncompressedSize - HEADER_SIZE);
  Bytes.putLong(dest, offset, prevOffset);
}
 
Example 12
Project: ditb   File: KeyValue.java   View Source Code Vote up 5 votes
/**
 * @param b
 * @param o
 * @param l
 * @return A KeyValue made of a byte array that holds the key-only part.
 * Needed to convert hfile index members to KeyValues.
 */
public static KeyValue createKeyValueFromKey(final byte [] b, final int o,
    final int l) {
  byte [] newb = new byte[l + ROW_OFFSET];
  System.arraycopy(b, o, newb, ROW_OFFSET, l);
  Bytes.putInt(newb, 0, l);
  Bytes.putInt(newb, Bytes.SIZEOF_INT, 0);
  return new KeyValue(newb);
}
 
Example 13
Project: ditb   File: BufferedDataBlockEncoder.java   View Source Code Vote up 5 votes
@Override
public void endBlockEncoding(HFileBlockEncodingContext encodingCtx, DataOutputStream out,
    byte[] uncompressedBytesWithHeader) throws IOException {
  BufferedDataBlockEncodingState state = (BufferedDataBlockEncodingState) encodingCtx
      .getEncodingState();
  // Write the unencodedDataSizeWritten (with header size)
  Bytes.putInt(uncompressedBytesWithHeader, HConstants.HFILEBLOCK_HEADER_SIZE
      + DataBlockEncoding.ID_SIZE, state.unencodedDataSizeWritten
      );
  if (encodingCtx.getDataBlockEncoding() != DataBlockEncoding.NONE) {
    encodingCtx.postEncoding(BlockType.ENCODED_DATA);
  } else {
    encodingCtx.postEncoding(BlockType.DATA);
  }
}
 
Example 14
Project: ditb   File: WALCellCodec.java   View Source Code Vote up 4 votes
@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.rowDict);
  checkLength(elemLen, Short.MAX_VALUE);
  pos = Bytes.putShort(backingArray, pos, (short)elemLen);
  pos += elemLen;

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

  // qualifier
  elemLen = readIntoArray(backingArray, pos, compression.qualifierDict);
  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 15
Project: ditb   File: KeyValue.java   View Source Code Vote up 4 votes
/**
 * 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);
  }
  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 16
Project: ditb   File: RawInteger.java   View Source Code Vote up 4 votes
@Override
public int encode(PositionedByteRange dst, Integer val) {
  Bytes.putInt(dst.getBytes(), dst.getOffset() + dst.getPosition(), val);
  return skip(dst);
}
 
Example 17
Project: ditb   File: RawInteger.java   View Source Code Vote up 4 votes
/**
 * Write instance {@code val} into buffer {@code buff}.
 */
public int encodeInt(byte[] buff, int offset, int val) {
  return Bytes.putInt(buff, offset, val);
}