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

The following are Jave code examples for showing how to use binarySearch() 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: SimpleTotalOrderPartitioner.java   View Source Code Vote up 6 votes
@Override
public int getPartition(final ImmutableBytesWritable key, final VALUE value,
    final int reduces) {
  if (reduces == 1) return 0;
  if (this.lastReduces != reduces) {
    this.splits = Bytes.split(this.startkey, this.endkey, reduces - 1);
    for (int i = 0; i < splits.length; i++) {
      LOG.info(Bytes.toStringBinary(splits[i]));
    }
    this.lastReduces = reduces;
  }
  int pos = Bytes.binarySearch(this.splits, key.get(), key.getOffset(),
    key.getLength(), Bytes.BYTES_RAWCOMPARATOR);
  // Below code is from hfile index search.
  if (pos < 0) {
    pos++;
    pos *= -1;
    if (pos == 0) {
      // falls before the beginning of the file.
      throw new RuntimeException("Key outside start/stop range: " +
        key.toString());
    }
    pos--;
  }
  return pos;
}
 
Example 2
Project: ditb   File: IndexFile.java   View Source Code Vote up 6 votes
/**
 * @param key Key to find
 * @return Offset of block containing <code>key</code> or -1 if this file
 * does not contain the request.
 */
int blockContainingKey(final byte[] key, int offset, int length) {
  int pos = Bytes.binarySearch(blockKeys, key, offset, length, this.comparator);
  if (pos < 0) {
    pos ++;
    pos *= -1;
    if (pos == 0) {
      // falls before the beginning of the file.
      return -1;
    }
    // When switched to "first key in block" index, binarySearch now returns
    // the block with a firstKey < key.  This means the value we want is potentially
    // in the next block.
    pos --; // in previous block.

    return pos;
  }
  // wow, a perfect hit, how unlikely?
  return pos;
}