Java Code Examples for org.apache.hadoop.hbase.HConstants#INDEX_KEY_MAGIC

The following examples show how to use org.apache.hadoop.hbase.HConstants#INDEX_KEY_MAGIC . 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: StoreFileScanner.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param s
 * @param k
 * @return false if not found or if k is after the end.
 * @throws IOException
 */
public static boolean seekAtOrAfter(HFileScanner s, Cell k)
throws IOException {
  int result = s.seekTo(k);
  if(result < 0) {
    if (result == HConstants.INDEX_KEY_MAGIC) {
      // using faked key
      return true;
    }
    // Passed KV is smaller than first KV in file, work from start of file
    return s.seekTo();
  } else if(result > 0) {
    // Passed KV is larger than current KV in file, if there is a next
    // it is the "after", if not then this scanner is done.
    return s.next();
  }
  // Seeked to the exact key
  return true;
}
 
Example 2
Source File: StoreFileScanner.java    From hbase with Apache License 2.0 6 votes vote down vote up
static boolean reseekAtOrAfter(HFileScanner s, Cell k)
throws IOException {
  //This function is similar to seekAtOrAfter function
  int result = s.reseekTo(k);
  if (result <= 0) {
    if (result == HConstants.INDEX_KEY_MAGIC) {
      // using faked key
      return true;
    }
    // If up to now scanner is not seeked yet, this means passed KV is smaller
    // than first KV in file, and it is the first time we seek on this file.
    // So we also need to work from the start of file.
    if (!s.isSeeked()) {
      return  s.seekTo();
    }
    return true;
  }
  // passed KV is larger than current KV in file, if there is a next
  // it is after, if not then this scanner is done.
  return s.next();
}
 
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.
}
 
Example 4
Source File: BufferedDataBlockEncoder.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Override
public int seekToKeyInBlock(Cell seekCell, boolean seekBefore) {
  int rowCommonPrefix = 0;
  int familyCommonPrefix = 0;
  int qualCommonPrefix = 0;
  previous.invalidate();
  do {
    int comp;
    keyOnlyKV.setKey(current.keyBuffer, 0, current.keyLength);
    if (current.lastCommonPrefix != 0) {
      // The KV format has row key length also in the byte array. The
      // common prefix
      // includes it. So we need to subtract to find out the common prefix
      // in the
      // row part alone
      rowCommonPrefix = Math.min(rowCommonPrefix, current.lastCommonPrefix - 2);
    }
    if (current.lastCommonPrefix <= 2) {
      rowCommonPrefix = 0;
    }
    rowCommonPrefix += findCommonPrefixInRowPart(seekCell, keyOnlyKV, rowCommonPrefix);
    comp = compareCommonRowPrefix(seekCell, keyOnlyKV, rowCommonPrefix);
    if (comp == 0) {
      comp = compareTypeBytes(seekCell, keyOnlyKV);
      if (comp == 0) {
        // Subtract the fixed row key length and the family key fixed length
        familyCommonPrefix = Math.max(
            0,
            Math.min(familyCommonPrefix,
                current.lastCommonPrefix - (3 + keyOnlyKV.getRowLength())));
        familyCommonPrefix += findCommonPrefixInFamilyPart(seekCell, keyOnlyKV,
            familyCommonPrefix);
        comp = compareCommonFamilyPrefix(seekCell, keyOnlyKV, familyCommonPrefix);
        if (comp == 0) {
          // subtract the rowkey fixed length and the family key fixed
          // length
          qualCommonPrefix = Math.max(
              0,
              Math.min(
                  qualCommonPrefix,
                  current.lastCommonPrefix
                      - (3 + keyOnlyKV.getRowLength() + keyOnlyKV.getFamilyLength())));
          qualCommonPrefix += findCommonPrefixInQualifierPart(seekCell, keyOnlyKV,
              qualCommonPrefix);
          comp = compareCommonQualifierPrefix(seekCell, keyOnlyKV, qualCommonPrefix);
          if (comp == 0) {
            comp = CellComparator.getInstance().compareTimestamps(seekCell, keyOnlyKV);
            if (comp == 0) {
              // Compare types. Let the delete types sort ahead of puts;
              // i.e. types
              // of higher numbers sort before those of lesser numbers.
              // Maximum
              // (255)
              // appears ahead of everything, and minimum (0) appears
              // after
              // everything.
              comp = (0xff & keyOnlyKV.getTypeByte()) - (0xff & seekCell.getTypeByte());
            }
          }
        }
      }
    }
    if (comp == 0) { // exact match
      if (seekBefore) {
        if (!previous.isValid()) {
          // The caller (seekBefore) has to ensure that we are not at the
          // first key in the block.
          throw new IllegalStateException("Cannot seekBefore if "
              + "positioned at the first key in the block: key="
              + Bytes.toStringBinary(seekCell.getRowArray()));
        }
        moveToPrevious();
        return 1;
      }
      return 0;
    }

    if (comp < 0) { // already too large, check previous
      if (previous.isValid()) {
        moveToPrevious();
      } else {
        return HConstants.INDEX_KEY_MAGIC; // using optimized index key
      }
      return 1;
    }

    // move to next, if more data is available
    if (currentBuffer.hasRemaining()) {
      previous.copyFromNext(current);
      decodeNext();
      current.setKey(current.keyBuffer, current.memstoreTS);
    } else {
      break;
    }
  } while (true);

  // we hit the end of the block, not an exact match
  return 1;
}
 
Example 5
Source File: RowIndexSeekerV1.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Override
public int seekToKeyInBlock(Cell seekCell, boolean seekBefore) {
  previous.invalidate();
  int index = binarySearch(seekCell, seekBefore);
  if (index < 0) {
    return HConstants.INDEX_KEY_MAGIC; // using optimized index key
  } else {
    int offset = rowOffsets.getIntAfterPosition(index * Bytes.SIZEOF_INT);
    if (offset != 0) {
      decodeAtPosition(offset);
    }
  }
  do {
    int comp =
      PrivateCellUtil.compareKeyIgnoresMvcc(this.cellComparator, seekCell, current.currentKey);
    if (comp == 0) { // exact match
      if (seekBefore) {
        if (!previous.isValid()) {
          // The caller (seekBefore) has to ensure that we are not at the
          // first key in the block.
          throw new IllegalStateException("Cannot seekBefore if "
              + "positioned at the first key in the block: key="
              + Bytes.toStringBinary(seekCell.getRowArray()));
        }
        moveToPrevious();
        return 1;
      }
      return 0;
    }

    if (comp < 0) { // already too large, check previous
      if (previous.isValid()) {
        moveToPrevious();
      } else {
        return HConstants.INDEX_KEY_MAGIC; // using optimized index key
      }
      return 1;
    }

    // move to next, if more data is available
    if (currentBuffer.hasRemaining()) {
      previous.copyFromNext(current);
      decodeNext();
    } else {
      break;
    }
  } while (true);

  // we hit the end of the block, not an exact match
  return 1;
}