Exemplo n.º 1
0
  public long getApproximateSizes(Range range) {
    Version v = versions.getCurrent();

    InternalKey startKey =
        new InternalKey(
            Slices.wrappedBuffer(range.start()),
            SequenceNumber.MAX_SEQUENCE_NUMBER,
            ValueType.VALUE);
    InternalKey limitKey =
        new InternalKey(
            Slices.wrappedBuffer(range.limit()),
            SequenceNumber.MAX_SEQUENCE_NUMBER,
            ValueType.VALUE);
    long startOffset = v.getApproximateOffsetOf(startKey);
    long limitOffset = v.getApproximateOffsetOf(limitKey);

    return (limitOffset >= startOffset ? limitOffset - startOffset : 0);
  }
Exemplo n.º 2
0
  @Override
  public byte[] get(byte[] key, ReadOptions options) throws DBException {
    checkBackgroundException();
    LookupKey lookupKey;
    mutex.lock();
    try {
      SnapshotImpl snapshot = getSnapshot(options);
      lookupKey = new LookupKey(Slices.wrappedBuffer(key), snapshot.getLastSequence());

      // First look in the memtable, then in the immutable memtable (if any).
      LookupResult lookupResult = memTable.get(lookupKey);
      if (lookupResult != null) {
        Slice value = lookupResult.getValue();
        if (value == null) {
          return null;
        }
        return value.getBytes();
      }
      if (immutableMemTable != null) {
        lookupResult = immutableMemTable.get(lookupKey);
        if (lookupResult != null) {
          Slice value = lookupResult.getValue();
          if (value == null) {
            return null;
          }
          return value.getBytes();
        }
      }
    } finally {
      mutex.unlock();
    }

    // Not in memTables; try live files in level order
    LookupResult lookupResult = versions.get(lookupKey);

    // schedule compaction if necessary
    mutex.lock();
    try {
      if (versions.needsCompaction()) {
        maybeScheduleCompaction();
      }
    } finally {
      mutex.unlock();
    }

    if (lookupResult != null) {
      Slice value = lookupResult.getValue();
      if (value != null) {
        return value.getBytes();
      }
    }
    return null;
  }
Exemplo n.º 3
0
  private Slice writeWriteBatch(WriteBatchImpl updates, long sequenceBegin) {
    Slice record = Slices.allocate(SIZE_OF_LONG + SIZE_OF_INT + updates.getApproximateSize());
    final SliceOutput sliceOutput = record.output();
    sliceOutput.writeLong(sequenceBegin);
    sliceOutput.writeInt(updates.size());
    updates.forEach(
        new Handler() {
          @Override
          public void put(Slice key, Slice value) {
            sliceOutput.writeByte(VALUE.getPersistentId());
            writeLengthPrefixedBytes(sliceOutput, key);
            writeLengthPrefixedBytes(sliceOutput, value);
          }

          @Override
          public void delete(Slice key) {
            sliceOutput.writeByte(DELETION.getPersistentId());
            writeLengthPrefixedBytes(sliceOutput, key);
          }
        });
    return record.slice(0, sliceOutput.size());
  }