fork of https://github.com/oxigraph/rocksdb and https://github.com/facebook/rocksdb for nextgraph and oxigraph
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
197 lines
7.1 KiB
197 lines
7.1 KiB
9 years ago
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||
8 years ago
|
// This source code is licensed under both the GPLv2 (found in the
|
||
|
// COPYING file in the root directory) and Apache 2.0 License
|
||
|
// (found in the LICENSE.Apache file in the root directory).
|
||
11 years ago
|
//
|
||
14 years ago
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a BSD-style license that can be
|
||
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
||
|
//
|
||
|
// BlockBuilder generates blocks where keys are prefix-compressed:
|
||
|
//
|
||
|
// When we store a key, we drop the prefix shared with the previous
|
||
|
// string. This helps reduce the space requirement significantly.
|
||
|
// Furthermore, once every K keys, we do not apply the prefix
|
||
|
// compression and store the entire key. We call this a "restart
|
||
|
// point". The tail end of the block stores the offsets of all of the
|
||
|
// restart points, and can be used to do a binary search when looking
|
||
|
// for a particular key. Values are stored as-is (without compression)
|
||
|
// immediately following the corresponding key.
|
||
|
//
|
||
|
// An entry for a particular key-value pair has the form:
|
||
|
// shared_bytes: varint32
|
||
|
// unshared_bytes: varint32
|
||
|
// value_length: varint32
|
||
|
// key_delta: char[unshared_bytes]
|
||
|
// value: char[value_length]
|
||
|
// shared_bytes == 0 for restart points.
|
||
|
//
|
||
|
// The trailer of the block has the form:
|
||
|
// restarts: uint32[num_restarts]
|
||
|
// num_restarts: uint32
|
||
|
// restarts[i] contains the offset within the block of the ith restart point.
|
||
|
|
||
6 years ago
|
#include "table/block_based/block_builder.h"
|
||
14 years ago
|
|
||
|
#include <assert.h>
|
||
6 years ago
|
#include <algorithm>
|
||
11 years ago
|
#include "db/dbformat.h"
|
||
6 years ago
|
#include "rocksdb/comparator.h"
|
||
6 years ago
|
#include "table/block_based/data_block_footer.h"
|
||
14 years ago
|
#include "util/coding.h"
|
||
|
|
||
11 years ago
|
namespace rocksdb {
|
||
14 years ago
|
|
||
6 years ago
|
BlockBuilder::BlockBuilder(
|
||
|
int block_restart_interval, bool use_delta_encoding,
|
||
|
bool use_value_delta_encoding,
|
||
|
BlockBasedTableOptions::DataBlockIndexType index_type,
|
||
|
double data_block_hash_table_util_ratio)
|
||
11 years ago
|
: block_restart_interval_(block_restart_interval),
|
||
9 years ago
|
use_delta_encoding_(use_delta_encoding),
|
||
6 years ago
|
use_value_delta_encoding_(use_value_delta_encoding),
|
||
14 years ago
|
restarts_(),
|
||
|
counter_(0),
|
||
|
finished_(false) {
|
||
6 years ago
|
switch (index_type) {
|
||
|
case BlockBasedTableOptions::kDataBlockBinarySearch:
|
||
|
break;
|
||
|
case BlockBasedTableOptions::kDataBlockBinaryAndHash:
|
||
|
data_block_hash_index_builder_.Initialize(
|
||
|
data_block_hash_table_util_ratio);
|
||
|
break;
|
||
|
default:
|
||
|
assert(0);
|
||
|
}
|
||
11 years ago
|
assert(block_restart_interval_ >= 1);
|
||
6 years ago
|
restarts_.push_back(0); // First restart point is at offset 0
|
||
9 years ago
|
estimate_ = sizeof(uint32_t) + sizeof(uint32_t);
|
||
14 years ago
|
}
|
||
|
|
||
|
void BlockBuilder::Reset() {
|
||
|
buffer_.clear();
|
||
|
restarts_.clear();
|
||
6 years ago
|
restarts_.push_back(0); // First restart point is at offset 0
|
||
9 years ago
|
estimate_ = sizeof(uint32_t) + sizeof(uint32_t);
|
||
14 years ago
|
counter_ = 0;
|
||
|
finished_ = false;
|
||
|
last_key_.clear();
|
||
6 years ago
|
if (data_block_hash_index_builder_.Valid()) {
|
||
|
data_block_hash_index_builder_.Reset();
|
||
|
}
|
||
14 years ago
|
}
|
||
|
|
||
6 years ago
|
size_t BlockBuilder::EstimateSizeAfterKV(const Slice& key,
|
||
|
const Slice& value) const {
|
||
12 years ago
|
size_t estimate = CurrentSizeEstimate();
|
||
6 years ago
|
// Note: this is an imprecise estimate as it accounts for the whole key size
|
||
|
// instead of non-shared key size.
|
||
|
estimate += key.size();
|
||
|
// In value delta encoding we estimate the value delta size as half the full
|
||
|
// value size since only the size field of block handle is encoded.
|
||
|
estimate +=
|
||
|
!use_value_delta_encoding_ || (counter_ >= block_restart_interval_)
|
||
|
? value.size()
|
||
|
: value.size() / 2;
|
||
|
|
||
11 years ago
|
if (counter_ >= block_restart_interval_) {
|
||
6 years ago
|
estimate += sizeof(uint32_t); // a new restart entry.
|
||
12 years ago
|
}
|
||
|
|
||
6 years ago
|
estimate += sizeof(int32_t); // varint for shared prefix length.
|
||
6 years ago
|
// Note: this is an imprecise estimate as we will have to encoded size, one
|
||
|
// for shared key and one for non-shared key.
|
||
6 years ago
|
estimate += VarintLength(key.size()); // varint for key length.
|
||
6 years ago
|
if (!use_value_delta_encoding_ || (counter_ >= block_restart_interval_)) {
|
||
|
estimate += VarintLength(value.size()); // varint for value length.
|
||
|
}
|
||
12 years ago
|
|
||
|
return estimate;
|
||
|
}
|
||
|
|
||
14 years ago
|
Slice BlockBuilder::Finish() {
|
||
|
// Append restart array
|
||
14 years ago
|
for (size_t i = 0; i < restarts_.size(); i++) {
|
||
14 years ago
|
PutFixed32(&buffer_, restarts_[i]);
|
||
|
}
|
||
6 years ago
|
|
||
|
uint32_t num_restarts = static_cast<uint32_t>(restarts_.size());
|
||
|
BlockBasedTableOptions::DataBlockIndexType index_type =
|
||
6 years ago
|
BlockBasedTableOptions::kDataBlockBinarySearch;
|
||
6 years ago
|
if (data_block_hash_index_builder_.Valid() &&
|
||
|
CurrentSizeEstimate() <= kMaxBlockSizeSupportedByHashIndex) {
|
||
|
data_block_hash_index_builder_.Finish(buffer_);
|
||
|
index_type = BlockBasedTableOptions::kDataBlockBinaryAndHash;
|
||
|
}
|
||
|
|
||
|
// footer is a packed format of data_block_index_type and num_restarts
|
||
6 years ago
|
uint32_t block_footer = PackIndexTypeAndNumRestarts(index_type, num_restarts);
|
||
6 years ago
|
|
||
|
PutFixed32(&buffer_, block_footer);
|
||
14 years ago
|
finished_ = true;
|
||
|
return Slice(buffer_);
|
||
|
}
|
||
|
|
||
6 years ago
|
void BlockBuilder::Add(const Slice& key, const Slice& value,
|
||
|
const Slice* const delta_value) {
|
||
14 years ago
|
assert(!finished_);
|
||
11 years ago
|
assert(counter_ <= block_restart_interval_);
|
||
6 years ago
|
assert(!use_value_delta_encoding_ || delta_value);
|
||
9 years ago
|
size_t shared = 0; // number of bytes shared with prev key
|
||
|
if (counter_ >= block_restart_interval_) {
|
||
|
// Restart compression
|
||
|
restarts_.push_back(static_cast<uint32_t>(buffer_.size()));
|
||
9 years ago
|
estimate_ += sizeof(uint32_t);
|
||
9 years ago
|
counter_ = 0;
|
||
9 years ago
|
|
||
|
if (use_delta_encoding_) {
|
||
|
// Update state
|
||
|
last_key_.assign(key.data(), key.size());
|
||
|
}
|
||
9 years ago
|
} else if (use_delta_encoding_) {
|
||
9 years ago
|
Slice last_key_piece(last_key_);
|
||
14 years ago
|
// See how much sharing to do with previous string
|
||
9 years ago
|
shared = key.difference_offset(last_key_piece);
|
||
|
|
||
|
// Update state
|
||
|
// We used to just copy the changed data here, but it appears to be
|
||
|
// faster to just copy the whole thing.
|
||
|
last_key_.assign(key.data(), key.size());
|
||
14 years ago
|
}
|
||
9 years ago
|
|
||
14 years ago
|
const size_t non_shared = key.size() - shared;
|
||
9 years ago
|
const size_t curr_size = buffer_.size();
|
||
14 years ago
|
|
||
6 years ago
|
if (use_value_delta_encoding_) {
|
||
|
// Add "<shared><non_shared>" to buffer_
|
||
|
PutVarint32Varint32(&buffer_, static_cast<uint32_t>(shared),
|
||
|
static_cast<uint32_t>(non_shared));
|
||
|
} else {
|
||
|
// Add "<shared><non_shared><value_size>" to buffer_
|
||
|
PutVarint32Varint32Varint32(&buffer_, static_cast<uint32_t>(shared),
|
||
|
static_cast<uint32_t>(non_shared),
|
||
|
static_cast<uint32_t>(value.size()));
|
||
|
}
|
||
14 years ago
|
|
||
|
// Add string delta to buffer_ followed by value
|
||
|
buffer_.append(key.data() + shared, non_shared);
|
||
6 years ago
|
// Use value delta encoding only when the key has shared bytes. This would
|
||
|
// simplify the decoding, where it can figure which decoding to use simply by
|
||
|
// looking at the shared bytes size.
|
||
|
if (shared != 0 && use_value_delta_encoding_) {
|
||
|
buffer_.append(delta_value->data(), delta_value->size());
|
||
|
} else {
|
||
|
buffer_.append(value.data(), value.size());
|
||
|
}
|
||
14 years ago
|
|
||
6 years ago
|
if (data_block_hash_index_builder_.Valid()) {
|
||
|
data_block_hash_index_builder_.Add(ExtractUserKey(key),
|
||
|
restarts_.size() - 1);
|
||
|
}
|
||
|
|
||
14 years ago
|
counter_++;
|
||
9 years ago
|
estimate_ += buffer_.size() - curr_size;
|
||
14 years ago
|
}
|
||
|
|
||
11 years ago
|
} // namespace rocksdb
|