Beispiel #1
0
 public void reset() {
   block.reset();
   entryCount = 0;
   restartPositions.clear();
   restartPositions.add(0); // first restart point must be 0
   restartBlockEntryCount = 0;
   lastKey = null;
   finished = false;
 }
Beispiel #2
0
  public BlockBuilder(int estimatedSize, int blockRestartInterval, Comparator<Slice> comparator) {
    Preconditions.checkArgument(estimatedSize >= 0, "estimatedSize is negative");
    Preconditions.checkArgument(blockRestartInterval >= 0, "blockRestartInterval is negative");
    Preconditions.checkNotNull(comparator, "comparator is null");

    this.block = new DynamicSliceOutput(estimatedSize);
    this.blockRestartInterval = blockRestartInterval;
    this.comparator = comparator;

    restartPositions = new IntVector(32);
    restartPositions.add(0); // first restart point must be 0
  }
Beispiel #3
0
  public void add(Slice key, Slice value) {
    Preconditions.checkNotNull(key, "key is null");
    Preconditions.checkNotNull(value, "value is null");
    Preconditions.checkState(!finished, "block is finished");
    Preconditions.checkPositionIndex(restartBlockEntryCount, blockRestartInterval);

    Preconditions.checkArgument(
        lastKey == null || comparator.compare(key, lastKey) > 0,
        "key %s must be greater than last key %s",
        key,
        lastKey);

    int sharedKeyBytes = 0;
    if (restartBlockEntryCount < blockRestartInterval) {
      sharedKeyBytes = calculateSharedBytes(key, lastKey);
    } else {
      // restart prefix compression
      restartPositions.add(block.size());
      restartBlockEntryCount = 0;
    }

    int nonSharedKeyBytes = key.length() - sharedKeyBytes;

    // write "<shared><non_shared><value_size>"
    VariableLengthQuantity.writeVariableLengthInt(sharedKeyBytes, block);
    VariableLengthQuantity.writeVariableLengthInt(nonSharedKeyBytes, block);
    VariableLengthQuantity.writeVariableLengthInt(value.length(), block);

    // write non-shared key bytes
    block.writeBytes(key, sharedKeyBytes, nonSharedKeyBytes);

    // write value bytes
    block.writeBytes(value, 0, value.length());

    // update last key
    lastKey = key;

    // update state
    entryCount++;
    restartBlockEntryCount++;
  }