Java Code Examples for org.apache.hadoop.hbase.util.Bytes#MASK_FOR_LOWER_INT_IN_LONG

The following examples show how to use org.apache.hadoop.hbase.util.Bytes#MASK_FOR_LOWER_INT_IN_LONG . 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: HFileReaderImpl.java    From hbase with Apache License 2.0 6 votes vote down vote up
protected void readKeyValueLen() {
  // 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.
  // 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.
  // Trying to imitate what was done - need to profile if this is better or
  // earlier way is better by doing mark and reset?
  // But ensure that you read long instead of two ints
  long ll = blockBuffer.getLongAfterPosition(0);
  // 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
  int p = (Bytes.SIZEOF_LONG + currKeyLen + currValueLen);
  if (reader.getFileContext().isIncludesTags()) {
    // Tags length is a short.
    this.currTagsLen = blockBuffer.getShortAfterPosition(p);
    checkTagsLen();
    p += (Bytes.SIZEOF_SHORT + currTagsLen);
  }
  readMvccVersion(p);
}
 
Example 2
Source File: RowIndexSeekerV1.java    From hbase with Apache License 2.0 5 votes vote down vote up
protected void decodeNext() {
  current.startOffset = currentBuffer.position();
  long ll = currentBuffer.getLongAfterPosition(0);
  // Read top half as an int of key length and bottom int as value length
  current.keyLength = (int) (ll >> Integer.SIZE);
  current.valueLength = (int) (Bytes.MASK_FOR_LOWER_INT_IN_LONG ^ ll);
  currentBuffer.skip(Bytes.SIZEOF_LONG);
  // key part
  currentBuffer.asSubByteBuffer(currentBuffer.position(), current.keyLength,
      tmpPair);
  ByteBuffer key = tmpPair.getFirst().duplicate();
  key.position(tmpPair.getSecond()).limit(
      tmpPair.getSecond() + current.keyLength);
  current.keyBuffer = key;
  currentBuffer.skip(current.keyLength);
  // value part
  current.valueOffset = currentBuffer.position();
  currentBuffer.skip(current.valueLength);
  if (includesTags()) {
    decodeTags();
  }
  if (includesMvcc()) {
    current.memstoreTS = ByteBufferUtils.readVLong(currentBuffer);
  } else {
    current.memstoreTS = 0;
  }
  current.nextKvOffset = currentBuffer.position();
  current.currentKey.setKey(current.keyBuffer, tmpPair.getSecond(),
      current.keyLength);
}
 
Example 3
Source File: HFileReaderImpl.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * Within a loaded block, seek looking for the last key that is smaller than
 * (or equal to?) the key we are interested in.
 * A note on the seekBefore: if you have seekBefore = true, AND the first
 * key in the block = key, then you'll get thrown exceptions. The caller has
 * to check for that case and load the previous block as appropriate.
 * @param key
 *          the key to find
 * @param seekBefore
 *          find the key before the given key in case of exact match.
 * @return 0 in case of an exact key match, 1 in case of an inexact match,
 *         -2 in case of an inexact match and furthermore, the input key
 *         less than the first key of current block(e.g. using a faked index
 *         key)
 */
protected int blockSeek(Cell key, boolean seekBefore) {
  int klen, vlen, tlen = 0;
  int lastKeyValueSize = -1;
  int offsetFromPos;
  do {
    offsetFromPos = 0;
    // Better to ensure that we use the BB Utils here
    long ll = blockBuffer.getLongAfterPosition(offsetFromPos);
    klen = (int)(ll >> Integer.SIZE);
    vlen = (int)(Bytes.MASK_FOR_LOWER_INT_IN_LONG ^ ll);
    if (checkKeyLen(klen) || checkLen(vlen)) {
      throw new IllegalStateException("Invalid klen " + klen + " or vlen "
          + vlen + ". Block offset: "
          + curBlock.getOffset() + ", block length: " + blockBuffer.limit() + ", position: "
          + blockBuffer.position() + " (without header)."
          + " path=" + reader.getPath());
    }
    offsetFromPos += Bytes.SIZEOF_LONG;
    blockBuffer.asSubByteBuffer(blockBuffer.position() + offsetFromPos, klen, pair);
    bufBackedKeyOnlyKv.setKey(pair.getFirst(), pair.getSecond(), klen);
    int comp =
        PrivateCellUtil.compareKeyIgnoresMvcc(reader.getComparator(), key, bufBackedKeyOnlyKv);
    offsetFromPos += klen + vlen;
    if (this.reader.getFileContext().isIncludesTags()) {
      // Read short as unsigned, high byte first
      tlen = ((blockBuffer.getByteAfterPosition(offsetFromPos) & 0xff) << 8)
          ^ (blockBuffer.getByteAfterPosition(offsetFromPos + 1) & 0xff);
      if (checkLen(tlen)) {
        throw new IllegalStateException("Invalid tlen " + tlen + ". Block offset: "
            + curBlock.getOffset() + ", block length: " + blockBuffer.limit() + ", position: "
            + blockBuffer.position() + " (without header)."
            + " path=" + reader.getPath());
      }
      // add the two bytes read for the tags.
      offsetFromPos += tlen + (Bytes.SIZEOF_SHORT);
    }
    if (this.reader.getHFileInfo().shouldIncludeMemStoreTS()) {
      // Directly read the mvcc based on current position
      readMvccVersion(offsetFromPos);
    }
    if (comp == 0) {
      if (seekBefore) {
        if (lastKeyValueSize < 0) {
          throw new IllegalStateException("blockSeek with seekBefore "
              + "at the first key of the block: key=" + CellUtil.getCellKeyAsString(key)
              + ", blockOffset=" + curBlock.getOffset() + ", onDiskSize="
              + curBlock.getOnDiskSizeWithHeader()
              + ", path=" + reader.getPath());
        }
        blockBuffer.moveBack(lastKeyValueSize);
        readKeyValueLen();
        return 1; // non exact match.
      }
      currKeyLen = klen;
      currValueLen = vlen;
      currTagsLen = tlen;
      return 0; // indicate exact match
    } else if (comp < 0) {
      if (lastKeyValueSize > 0) {
        blockBuffer.moveBack(lastKeyValueSize);
      }
      readKeyValueLen();
      if (lastKeyValueSize == -1 && blockBuffer.position() == 0) {
        return HConstants.INDEX_KEY_MAGIC;
      }
      return 1;
    }
    // The size of this key/value tuple, including key/value length fields.
    lastKeyValueSize = klen + vlen + currMemstoreTSLen + KEY_VALUE_LEN_SIZE;
    // include tag length also if tags included with KV
    if (reader.getFileContext().isIncludesTags()) {
      lastKeyValueSize += tlen + Bytes.SIZEOF_SHORT;
    }
    blockBuffer.skip(lastKeyValueSize);
  } while (blockBuffer.hasRemaining());

  // Seek to the last key we successfully read. This will happen if this is
  // the last key/value pair in the file, in which case the following call
  // to next() has to return false.
  blockBuffer.moveBack(lastKeyValueSize);
  readKeyValueLen();
  return 1; // didn't exactly find it.
}