org.apache.hadoop.hbase.util.Bytes.binarySearch()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(11.8k)|赞(0)|评价(0)|浏览(88)

本文整理了Java中org.apache.hadoop.hbase.util.Bytes.binarySearch()方法的一些代码示例,展示了Bytes.binarySearch()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Bytes.binarySearch()方法的具体详情如下:
包路径:org.apache.hadoop.hbase.util.Bytes
类名称:Bytes
方法名:binarySearch

Bytes.binarySearch介绍

[英]Binary search for keys in indexes.
[中]在索引中对键进行二进制搜索。

代码示例

代码示例来源:origin: apache/hbase

@Override
public int rootBlockContainingKey(byte[] key, int offset, int length, CellComparator comp) {
 int pos = Bytes.binarySearch(blockKeys, key, offset, length);
 // pos is between -(blockKeys.length + 1) to blockKeys.length - 1, see
 // binarySearch's javadoc.
 if (pos >= 0) {
  // This means this is an exact match with an element of blockKeys.
  assert pos < blockKeys.length;
  return pos;
 }
 // Otherwise, pos = -(i + 1), where blockKeys[i - 1] < key < blockKeys[i],
 // and i is in [0, blockKeys.length]. We are returning j = i - 1 such that
 // blockKeys[j] <= key < blockKeys[j + 1]. In particular, j = -1 if
 // key < blockKeys[0], meaning the file does not contain the given key.
 int i = -pos - 1;
 assert 0 <= i && i <= blockKeys.length;
 return i - 1;
}

代码示例来源:origin: apache/hbase

@Override
public int rootBlockContainingKey(Cell key) {
 // Here the comparator should not be null as this happens for the root-level block
 int pos = Bytes.binarySearch(blockKeys, key, comparator);
 // pos is between -(blockKeys.length + 1) to blockKeys.length - 1, see
 // binarySearch's javadoc.
 if (pos >= 0) {
  // This means this is an exact match with an element of blockKeys.
  assert pos < blockKeys.length;
  return pos;
 }
 // Otherwise, pos = -(i + 1), where blockKeys[i - 1] < key < blockKeys[i],
 // and i is in [0, blockKeys.length]. We are returning j = i - 1 such that
 // blockKeys[j] <= key < blockKeys[j + 1]. In particular, j = -1 if
 // key < blockKeys[0], meaning the file does not contain the given key.
 int i = -pos - 1;
 assert 0 <= i && i <= blockKeys.length;
 return i - 1;
}

代码示例来源:origin: apache/hbase

/**
 * Binary search for keys in indexes.
 *
 * @param arr array of byte arrays to search for
 * @param key the key you want to find
 * @param offset the offset in the key you want to find
 * @param length the length of the key
 * @param comparator a comparator to compare.
 * @return zero-based index of the key, if the key is present in the array.
 *         Otherwise, a value -(i + 1) such that the key is between arr[i -
 *         1] and arr[i] non-inclusively, where i is in [0, i], if we define
 *         arr[-1] = -Inf and arr[N] = Inf for an N-element array. The above
 *         means that this function can return 2N + 1 different values
 *         ranging from -(N + 1) to N - 1.
 * @deprecated {@link Bytes#binarySearch(byte[][], byte[], int, int)}
 */
@Deprecated
public static int binarySearch(byte [][]arr, byte []key, int offset,
  int length, RawComparator<?> comparator) {
 return binarySearch(arr, key, offset, length);
}

代码示例来源:origin: apache/hbase

byte [] key5 = {2};
assertEquals(1, Bytes.binarySearch(arr, key1, 0, 1));
assertEquals(0, Bytes.binarySearch(arr, key1, 1, 1));
assertEquals(-(2+1), Arrays.binarySearch(arr, key2_2,
 Bytes.BYTES_COMPARATOR));
assertEquals(-(2+1), Bytes.binarySearch(arr, key2, 0, 1));
assertEquals(4, Bytes.binarySearch(arr, key2, 1, 1));
assertEquals(2, Bytes.binarySearch(arr, key3, 0, 1));
assertEquals(5, Bytes.binarySearch(arr, key3, 1, 1));
assertEquals(-1,
 Bytes.binarySearch(arr, key4, 0, 1));
assertEquals(-2,
 Bytes.binarySearch(arr, key5, 0, 1));
 assertEquals(-(i + 1), Bytes.binarySearch(arr,
   new byte[] { (byte) (arr[i][0] - 1) }, 0, 1));
 assertEquals(-(i + 2), Bytes.binarySearch(arr,
   new byte[] { (byte) (arr[i][0] + 1) }, 0, 1));

代码示例来源:origin: apache/hbase

@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());
 // 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;
}

代码示例来源:origin: co.cask.hbase/hbase

/**
 * Finds the root-level index block containing the given key.
 *
 * @param key
 *          Key to find
 * @return Offset of block containing <code>key</code> (between 0 and the
 *         number of blocks - 1) or -1 if this file does not contain the
 *         request.
 */
public int rootBlockContainingKey(final byte[] key, int offset,
  int length) {
 int pos = Bytes.binarySearch(blockKeys, key, offset, length,
   comparator);
 // pos is between -(blockKeys.length + 1) to blockKeys.length - 1, see
 // binarySearch's javadoc.
 if (pos >= 0) {
  // This means this is an exact match with an element of blockKeys.
  assert pos < blockKeys.length;
  return pos;
 }
 // Otherwise, pos = -(i + 1), where blockKeys[i - 1] < key < blockKeys[i],
 // and i is in [0, blockKeys.length]. We are returning j = i - 1 such that
 // blockKeys[j] <= key < blockKeys[j + 1]. In particular, j = -1 if
 // key < blockKeys[0], meaning the file does not contain the given key.
 int i = -pos - 1;
 assert 0 <= i && i <= blockKeys.length;
 return i - 1;
}

代码示例来源:origin: harbby/presto-connectors

/**
 * Finds the root-level index block containing the given key.
 *
 * @param key
 *          Key to find
 */
public int rootBlockContainingKey(final Cell key) {
 int pos = Bytes.binarySearch(blockKeys, key, comparator);
 // pos is between -(blockKeys.length + 1) to blockKeys.length - 1, see
 // binarySearch's javadoc.
 if (pos >= 0) {
  // This means this is an exact match with an element of blockKeys.
  assert pos < blockKeys.length;
  return pos;
 }
 // Otherwise, pos = -(i + 1), where blockKeys[i - 1] < key < blockKeys[i],
 // and i is in [0, blockKeys.length]. We are returning j = i - 1 such that
 // blockKeys[j] <= key < blockKeys[j + 1]. In particular, j = -1 if
 // key < blockKeys[0], meaning the file does not contain the given key.
 int i = -pos - 1;
 assert 0 <= i && i <= blockKeys.length;
 return i - 1;
}

代码示例来源:origin: harbby/presto-connectors

/**
 * Finds the root-level index block containing the given key.
 *
 * @param key
 *          Key to find
 * @return Offset of block containing <code>key</code> (between 0 and the
 *         number of blocks - 1) or -1 if this file does not contain the
 *         request.
 */
public int rootBlockContainingKey(final byte[] key, int offset, int length) {
 int pos = Bytes.binarySearch(blockKeys, key, offset, length, comparator);
 // pos is between -(blockKeys.length + 1) to blockKeys.length - 1, see
 // binarySearch's javadoc.
 if (pos >= 0) {
  // This means this is an exact match with an element of blockKeys.
  assert pos < blockKeys.length;
  return pos;
 }
 // Otherwise, pos = -(i + 1), where blockKeys[i - 1] < key < blockKeys[i],
 // and i is in [0, blockKeys.length]. We are returning j = i - 1 such that
 // blockKeys[j] <= key < blockKeys[j + 1]. In particular, j = -1 if
 // key < blockKeys[0], meaning the file does not contain the given key.
 int i = -pos - 1;
 assert 0 <= i && i <= blockKeys.length;
 return i - 1;
}

代码示例来源:origin: org.apache.hbase/hbase-common

/**
 * Binary search for keys in indexes.
 *
 * @param arr array of byte arrays to search for
 * @param key the key you want to find
 * @param offset the offset in the key you want to find
 * @param length the length of the key
 * @param comparator a comparator to compare.
 * @return zero-based index of the key, if the key is present in the array.
 *         Otherwise, a value -(i + 1) such that the key is between arr[i -
 *         1] and arr[i] non-inclusively, where i is in [0, i], if we define
 *         arr[-1] = -Inf and arr[N] = Inf for an N-element array. The above
 *         means that this function can return 2N + 1 different values
 *         ranging from -(N + 1) to N - 1.
 * @deprecated {@link Bytes#binarySearch(byte[][], byte[], int, int)}
 */
@Deprecated
public static int binarySearch(byte [][]arr, byte []key, int offset,
  int length, RawComparator<?> comparator) {
 return binarySearch(arr, key, offset, length);
}

代码示例来源:origin: com.aliyun.hbase/alihbase-common

/**
 * Binary search for keys in indexes.
 *
 * @param arr array of byte arrays to search for
 * @param key the key you want to find
 * @param offset the offset in the key you want to find
 * @param length the length of the key
 * @param comparator a comparator to compare.
 * @return zero-based index of the key, if the key is present in the array.
 *         Otherwise, a value -(i + 1) such that the key is between arr[i -
 *         1] and arr[i] non-inclusively, where i is in [0, i], if we define
 *         arr[-1] = -Inf and arr[N] = Inf for an N-element array. The above
 *         means that this function can return 2N + 1 different values
 *         ranging from -(N + 1) to N - 1.
 * @deprecated {@link Bytes#binarySearch(byte[][], byte[], int, int)}
 */
@Deprecated
public static int binarySearch(byte [][]arr, byte []key, int offset,
  int length, RawComparator<?> comparator) {
 return binarySearch(arr, key, offset, length);
}

代码示例来源:origin: com.aliyun.hbase/alihbase-mapreduce

@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());
 // 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;
}

代码示例来源:origin: co.cask.hbase/hbase

@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]));
  }
 }
 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;
}

代码示例来源:origin: org.apache.hbase/hbase-mapreduce

@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());
 // 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;
}

代码示例来源:origin: com.aliyun.hbase/alihbase-common

byte [] key5 = {2};
assertEquals(1, Bytes.binarySearch(arr, key1, 0, 1));
assertEquals(0, Bytes.binarySearch(arr, key1, 1, 1));
assertEquals(-(2+1), Arrays.binarySearch(arr, key2_2,
 Bytes.BYTES_COMPARATOR));
assertEquals(-(2+1), Bytes.binarySearch(arr, key2, 0, 1));
assertEquals(4, Bytes.binarySearch(arr, key2, 1, 1));
assertEquals(2, Bytes.binarySearch(arr, key3, 0, 1));
assertEquals(5, Bytes.binarySearch(arr, key3, 1, 1));
assertEquals(-1,
 Bytes.binarySearch(arr, key4, 0, 1));
assertEquals(-2,
 Bytes.binarySearch(arr, key5, 0, 1));
 assertEquals(-(i + 1), Bytes.binarySearch(arr,
   new byte[] { (byte) (arr[i][0] - 1) }, 0, 1));
 assertEquals(-(i + 2), Bytes.binarySearch(arr,
   new byte[] { (byte) (arr[i][0] + 1) }, 0, 1));

代码示例来源:origin: org.apache.hbase/hbase-common

byte [] key5 = {2};
assertEquals(1, Bytes.binarySearch(arr, key1, 0, 1));
assertEquals(0, Bytes.binarySearch(arr, key1, 1, 1));
assertEquals(-(2+1), Arrays.binarySearch(arr, key2_2,
 Bytes.BYTES_COMPARATOR));
assertEquals(-(2+1), Bytes.binarySearch(arr, key2, 0, 1));
assertEquals(4, Bytes.binarySearch(arr, key2, 1, 1));
assertEquals(2, Bytes.binarySearch(arr, key3, 0, 1));
assertEquals(5, Bytes.binarySearch(arr, key3, 1, 1));
assertEquals(-1,
 Bytes.binarySearch(arr, key4, 0, 1));
assertEquals(-2,
 Bytes.binarySearch(arr, key5, 0, 1));
 assertEquals(-(i + 1), Bytes.binarySearch(arr,
   new byte[] { (byte) (arr[i][0] - 1) }, 0, 1));
 assertEquals(-(i + 2), Bytes.binarySearch(arr,
   new byte[] { (byte) (arr[i][0] + 1) }, 0, 1));

代码示例来源:origin: harbby/presto-connectors

@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;
}

相关文章

微信公众号

最新文章

更多