Пример #1
0
  public Slice finish() {
    if (!finished) {
      finished = true;

      if (entryCount > 0) {
        restartPositions.write(block);
        block.writeInt(restartPositions.size());
      } else {
        block.writeInt(0);
      }
    }
    return block.slice();
  }
Пример #2
0
  public int currentSizeEstimate() {
    // no need to estimate if closed
    if (finished) {
      return block.size();
    }

    // no records is just a single int
    if (block.size() == 0) {
      return SIZE_OF_INT;
    }

    return block.size()
        + // raw data buffer
        restartPositions.size() * SIZE_OF_INT
        + // restart positions
        SIZE_OF_INT; // restart position size
  }
Пример #3
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;
 }
Пример #4
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++;
  }