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

The following are Jave code examples for showing how to use toShort() 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: HFileReaderV3.java   View Source Code Vote up 7 votes
protected final void readKeyValueLen() {
  // TODO: METHOD (mostly) DUPLICATED IN V2!!!! FIXED in master branch by collapsing v3 and v2.
  // This is a hot method. We go out of our way to make this method short so it can be
  // inlined and is not too big to compile. We also manage position in ByteBuffer ourselves
  // because it is faster than going via range-checked ByteBuffer methods or going through a
  // byte buffer array a byte at a time.
  int p = blockBuffer.position() + blockBuffer.arrayOffset();
  // Get a long at a time rather than read two individual ints. In micro-benchmarking, even
  // with the extra bit-fiddling, this is order-of-magnitude faster than getting two ints.
  long ll = Bytes.toLong(blockBuffer.array(), p);
  // Read top half as an int of key length and bottom int as value length
  this.currKeyLen = (int)(ll >> Integer.SIZE);
  this.currValueLen = (int)(Bytes.MASK_FOR_LOWER_INT_IN_LONG ^ ll);
  checkKeyValueLen();
  // Move position past the key and value lengths and then beyond the key and value
  p += (Bytes.SIZEOF_LONG + currKeyLen + currValueLen);
  if (reader.hfileContext.isIncludesTags()) {
    // Tags length is a short.
    this.currTagsLen = Bytes.toShort(blockBuffer.array(), p);
    checkTagsLen();
    p += (Bytes.SIZEOF_SHORT + currTagsLen);
  }
  readMvccVersion(p);
}
 
Example 2
Project: ditb   File: KeyValue.java   View Source Code Vote up 7 votes
/**
 * Use for logging.
 * @param b Key portion of a KeyValue.
 * @param o Offset to start of key
 * @param l Length of key.
 * @return Key as a String.
 */
public static String keyToString(final byte [] b, final int o, final int l) {
  if (b == null) return "";
  int rowlength = Bytes.toShort(b, o);
  String row = Bytes.toStringBinary(b, o + Bytes.SIZEOF_SHORT, rowlength);
  int columnoffset = o + Bytes.SIZEOF_SHORT + 1 + rowlength;
  int familylength = b[columnoffset - 1];
  int columnlength = l - ((columnoffset - o) + TIMESTAMP_TYPE_SIZE);
  String family = familylength == 0? "":
    Bytes.toStringBinary(b, columnoffset, familylength);
  String qualifier = columnlength == 0? "":
    Bytes.toStringBinary(b, columnoffset + familylength,
    columnlength - familylength);
  long timestamp = Bytes.toLong(b, o + (l - TIMESTAMP_TYPE_SIZE));
  String timestampStr = humanReadableTimestamp(timestamp);
  byte type = b[o + l - 1];
  return row + "/" + family +
    (family != null && family.length() > 0? ":" :"") +
    qualifier + "/" + timestampStr + "/" + Type.codeToType(type);
}
 
Example 3
Project: ditb   File: NamespaceUpgrade.java   View Source Code Vote up 6 votes
private static HTableDescriptor readTableDescriptor(FileSystem fs,
                                                    FileStatus status) throws IOException {
  int len = Ints.checkedCast(status.getLen());
  byte [] content = new byte[len];
  FSDataInputStream fsDataInputStream = fs.open(status.getPath());
  try {
    fsDataInputStream.readFully(content);
  } finally {
    fsDataInputStream.close();
  }
  HTableDescriptor htd = null;
  try {
    htd = HTableDescriptor.parseFrom(content);
  } catch (DeserializationException e) {
    throw new IOException("content=" + Bytes.toShort(content), e);
  }
  return htd;
}
 
Example 4
Project: ditb   File: KeyValue.java   View Source Code Vote up 6 votes
/**
 * Compares left to right assuming that left,loffset,llength and right,roffset,rlength are
 * full KVs laid out in a flat byte[]s.
 * @param left
 * @param loffset
 * @param llength
 * @param right
 * @param roffset
 * @param rlength
 * @return  0 if equal, <0 if left smaller, >0 if right smaller
 */
public int compareFlatKey(byte[] left, int loffset, int llength,
    byte[] right, int roffset, int rlength) {
  // Compare row
  short lrowlength = Bytes.toShort(left, loffset);
  short rrowlength = Bytes.toShort(right, roffset);
  int compare = compareRows(left, loffset + Bytes.SIZEOF_SHORT,
      lrowlength, right, roffset + Bytes.SIZEOF_SHORT, rrowlength);
  if (compare != 0) {
    return compare;
  }

  // Compare the rest of the two KVs without making any assumptions about
  // the common prefix. This function will not compare rows anyway, so we
  // don't need to tell it that the common prefix includes the row.
  return compareWithoutRow(0, left, loffset, llength, right, roffset,
      rlength, rrowlength);
}
 
Example 5
Project: ditb   File: ServerName.java   View Source Code Vote up 5 votes
/**
 * Use this method instantiating a {@link ServerName} from bytes
 * gotten from a call to {@link #getVersionedBytes()}.  Will take care of the
 * case where bytes were written by an earlier version of hbase.
 * @param versionedBytes Pass bytes gotten from a call to {@link #getVersionedBytes()}
 * @return A ServerName instance.
 * @see #getVersionedBytes()
 */
public static ServerName parseVersionedServerName(final byte [] versionedBytes) {
  // Version is a short.
  short version = Bytes.toShort(versionedBytes);
  if (version == VERSION) {
    int length = versionedBytes.length - Bytes.SIZEOF_SHORT;
    return valueOf(Bytes.toString(versionedBytes, Bytes.SIZEOF_SHORT, length));
  }
  // Presume the bytes were written with an old version of hbase and that the
  // bytes are actually a String of the form "'<hostname>' ':' '<port>'".
  return valueOf(Bytes.toString(versionedBytes), NON_STARTCODE);
}
 
Example 6
Project: ditb   File: KeyValue.java   View Source Code Vote up 4 votes
/**
 * @return Row length
 */
@Override
public short getRowLength() {
  return Bytes.toShort(this.bytes, getKeyOffset());
}
 
Example 7
Project: ditb   File: KeyValue.java   View Source Code Vote up 4 votes
/**
 * 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 8
Project: ditb   File: KeyValue.java   View Source Code Vote up 4 votes
@Override
public short getRowLength() {
  return Bytes.toShort(this.bytes, getKeyOffset());
}
 
Example 9
Project: ditb   File: RawShort.java   View Source Code Vote up 4 votes
@Override
public Short decode(PositionedByteRange src) {
  short val = Bytes.toShort(src.getBytes(), src.getOffset() + src.getPosition());
  skip(src);
  return val;
}
 
Example 10
Project: ditb   File: RawShort.java   View Source Code Vote up 4 votes
/**
 * Read a {@code short} value from the buffer {@code buff}.
 */
public short decodeShort(byte[] buff, int offset) {
  return Bytes.toShort(buff, offset);
}
 
Example 11
Project: ditb   File: TestKeyValue.java   View Source Code Vote up 4 votes
/**
 * See HBASE-7845
 */
public void testGetShortMidpointKey() {
  final KVComparator keyComparator = KeyValue.COMPARATOR;
  //verify that faked shorter rowkey could be generated
  long ts = 5;
  KeyValue kv1 = new KeyValue(Bytes.toBytes("the quick brown fox"), family, qualA, ts, Type.Put);
  KeyValue kv2 = new KeyValue(Bytes.toBytes("the who test text"), family, qualA, ts, Type.Put);
  byte[] newKey = keyComparator.getShortMidpointKey(kv1.getKey(), kv2.getKey());
  assertTrue(keyComparator.compareFlatKey(kv1.getKey(), newKey) < 0);
  assertTrue(keyComparator.compareFlatKey(newKey, kv2.getKey()) < 0);
  short newRowLength = Bytes.toShort(newKey, 0);
  byte[] expectedArray = Bytes.toBytes("the r");
  Bytes.equals(newKey, KeyValue.ROW_LENGTH_SIZE, newRowLength, expectedArray, 0,
    expectedArray.length);

  //verify: same with "row + family + qualifier", return rightKey directly
  kv1 = new KeyValue(Bytes.toBytes("ilovehbase"), family, qualA, 5, Type.Put);
  kv2 = new KeyValue(Bytes.toBytes("ilovehbase"), family, qualA, 0, Type.Put);
  assertTrue(keyComparator.compareFlatKey(kv1.getKey(), kv2.getKey()) < 0);
  newKey = keyComparator.getShortMidpointKey(kv1.getKey(), kv2.getKey());
  assertTrue(keyComparator.compareFlatKey(kv1.getKey(), newKey) < 0);
  assertTrue(keyComparator.compareFlatKey(newKey, kv2.getKey()) == 0);
  kv1 = new KeyValue(Bytes.toBytes("ilovehbase"), family, qualA, -5, Type.Put);
  kv2 = new KeyValue(Bytes.toBytes("ilovehbase"), family, qualA, -10, Type.Put);
  assertTrue(keyComparator.compareFlatKey(kv1.getKey(), kv2.getKey()) < 0);
  newKey = keyComparator.getShortMidpointKey(kv1.getKey(), kv2.getKey());
  assertTrue(keyComparator.compareFlatKey(kv1.getKey(), newKey) < 0);
  assertTrue(keyComparator.compareFlatKey(newKey, kv2.getKey()) == 0);

  // verify: same with row, different with qualifier
  kv1 = new KeyValue(Bytes.toBytes("ilovehbase"), family, qualA, 5, Type.Put);
  kv2 = new KeyValue(Bytes.toBytes("ilovehbase"), family, qualB, 5, Type.Put);
  assertTrue(keyComparator.compareFlatKey(kv1.getKey(), kv2.getKey()) < 0);
  newKey = keyComparator.getShortMidpointKey(kv1.getKey(), kv2.getKey());
  assertTrue(keyComparator.compareFlatKey(kv1.getKey(), newKey) < 0);
  assertTrue(keyComparator.compareFlatKey(newKey, kv2.getKey()) < 0);
  KeyValue newKeyValue = KeyValue.createKeyValueFromKey(newKey);
  assertTrue(Arrays.equals(newKeyValue.getFamily(),family));
  assertTrue(Arrays.equals(newKeyValue.getQualifier(),qualB));
  assertTrue(newKeyValue.getTimestamp() == HConstants.LATEST_TIMESTAMP);
  assertTrue(newKeyValue.getTypeByte() == Type.Maximum.getCode());

  //verify metaKeyComparator's getShortMidpointKey output
  final KVComparator metaKeyComparator = KeyValue.META_COMPARATOR;
  kv1 = new KeyValue(Bytes.toBytes("ilovehbase123"), family, qualA, 5, Type.Put);
  kv2 = new KeyValue(Bytes.toBytes("ilovehbase234"), family, qualA, 0, Type.Put);
  newKey = metaKeyComparator.getShortMidpointKey(kv1.getKey(), kv2.getKey());
  assertTrue(metaKeyComparator.compareFlatKey(kv1.getKey(), newKey) < 0);
  assertTrue(metaKeyComparator.compareFlatKey(newKey, kv2.getKey()) == 0);

  //verify common fix scenario
  kv1 = new KeyValue(Bytes.toBytes("ilovehbase"), family, qualA, ts, Type.Put);
  kv2 = new KeyValue(Bytes.toBytes("ilovehbaseandhdfs"), family, qualA, ts, Type.Put);
  assertTrue(keyComparator.compareFlatKey(kv1.getKey(), kv2.getKey()) < 0);
  newKey = keyComparator.getShortMidpointKey(kv1.getKey(), kv2.getKey());
  assertTrue(keyComparator.compareFlatKey(kv1.getKey(), newKey) < 0);
  assertTrue(keyComparator.compareFlatKey(newKey, kv2.getKey()) < 0);
  newRowLength = Bytes.toShort(newKey, 0);
  expectedArray = Bytes.toBytes("ilovehbasea");
  Bytes.equals(newKey, KeyValue.ROW_LENGTH_SIZE, newRowLength, expectedArray, 0,
    expectedArray.length);
  //verify only 1 offset scenario
  kv1 = new KeyValue(Bytes.toBytes("100abcdefg"), family, qualA, ts, Type.Put);
  kv2 = new KeyValue(Bytes.toBytes("101abcdefg"), family, qualA, ts, Type.Put);
  assertTrue(keyComparator.compareFlatKey(kv1.getKey(), kv2.getKey()) < 0);
  newKey = keyComparator.getShortMidpointKey(kv1.getKey(), kv2.getKey());
  assertTrue(keyComparator.compareFlatKey(kv1.getKey(), newKey) < 0);
  assertTrue(keyComparator.compareFlatKey(newKey, kv2.getKey()) < 0);
  newRowLength = Bytes.toShort(newKey, 0);
  expectedArray = Bytes.toBytes("101");
  Bytes.equals(newKey, KeyValue.ROW_LENGTH_SIZE, newRowLength, expectedArray, 0,
    expectedArray.length);
}
 
Example 12
Project: stroom-stats   File: RollUpBitMask.java   View Source Code Vote up 2 votes
/**
 * Constructor
 *
 * @param bytes
 *            The byte array to convert from
 * @return A {@link RollUpBitMask} object built from the byte array
 */
public static RollUpBitMask fromBytes(final byte[] bytes) {
    return new RollUpBitMask(Bytes.toShort(bytes));
}
 
Example 13
Project: stroom-stats   File: RollUpBitMask.java   View Source Code Vote up 2 votes
/**
 * Constructor
 *
 * @param bytes
 *            The byte array containing the mask
 * @param offset
 *              The mask's position in the array
 * @return A {@link RollUpBitMask} object built from the byte array
 */
public static RollUpBitMask fromBytes(final byte[] bytes, final int offset) {
    return new RollUpBitMask(Bytes.toShort(bytes, offset));
}
 
Example 14
Project: ditb   File: IntegrationTestBigLinkedList.java   View Source Code Vote up 2 votes
/**
 * @param bs
 * @return Type from the Counts enum of this row. Reads prefix added by
 * {@link #addPrefixFlag(int, byte[])}
 */
public static Counts whichType(final byte [] bs) {
  int ordinal = Bytes.toShort(bs, 0, Bytes.SIZEOF_SHORT);
  return Counts.values()[ordinal];
}