2016-02-09 23:12:00 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00:00
|
|
|
// 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).
|
2013-10-16 21:59:46 +00:00
|
|
|
//
|
2011-03-18 22:37:00 +00:00
|
|
|
// 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.
|
|
|
|
|
2019-05-30 21:47:29 +00:00
|
|
|
#include "table/block_based/block_builder.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
#include <assert.h>
|
2022-10-25 18:50:38 +00:00
|
|
|
|
2018-08-17 01:29:13 +00:00
|
|
|
#include <algorithm>
|
2022-10-25 18:50:38 +00:00
|
|
|
|
2014-01-27 21:53:22 +00:00
|
|
|
#include "db/dbformat.h"
|
2018-08-15 21:27:47 +00:00
|
|
|
#include "rocksdb/comparator.h"
|
2019-05-30 21:47:29 +00:00
|
|
|
#include "table/block_based/data_block_footer.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
#include "util/coding.h"
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2018-08-15 21:27:47 +00:00
|
|
|
BlockBuilder::BlockBuilder(
|
|
|
|
int block_restart_interval, bool use_delta_encoding,
|
|
|
|
bool use_value_delta_encoding,
|
|
|
|
BlockBasedTableOptions::DataBlockIndexType index_type,
|
2023-05-25 22:41:32 +00:00
|
|
|
double data_block_hash_table_util_ratio, size_t ts_sz,
|
|
|
|
bool persist_user_defined_timestamps, bool is_user_key)
|
2013-10-08 01:33:49 +00:00
|
|
|
: block_restart_interval_(block_restart_interval),
|
2015-12-16 20:08:30 +00:00
|
|
|
use_delta_encoding_(use_delta_encoding),
|
2018-08-09 23:49:45 +00:00
|
|
|
use_value_delta_encoding_(use_value_delta_encoding),
|
2023-05-25 22:41:32 +00:00
|
|
|
ts_sz_(ts_sz),
|
|
|
|
persist_user_defined_timestamps_(persist_user_defined_timestamps),
|
|
|
|
is_user_key_(is_user_key),
|
Two performance improvements in BlockBuilder (#9039)
Summary:
Primarily, this change reserves space in the std::string for building
the next block once a block is finished, using `block_size` as
reservation size. Note: also tried reusing same std::string in the
common "unbuffered" path but that showed no benefit or regression.
Secondarily, this slightly reduces the work in resetting `restarts_`.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/9039
Test Plan:
TEST_TMPDIR=/dev/shm/rocksdb1 ./db_bench -benchmarks=fillseq -memtablerep=vector -allow_concurrent_memtable_write=false -num=50000000
Compiled with DEBUG_LEVEL=0
Test vs. control runs simulaneous for better accuracy, units = ops/sec
Run 1, Primary change only: 292697 vs. 280267 (+4.4%)
Run 2, Primary change only: 288763 vs. 279621 (+3.3%)
Run 1, Secondary change only: 260065 vs. 254232 (+2.3%)
Run 2, Secondary change only: 275925 vs. 272248 (+1.4%)
Run 1, Both changes: 284890 vs. 270372 (+5.3%)
Run 2, Both changes: 263511 vs. 258188 (+2.0%)
Reviewed By: zhichao-cao
Differential Revision: D31701253
Pulled By: pdillinger
fbshipit-source-id: 7e40810afbb98e6b6446955e77bda59e69b19ffd
2021-10-18 15:34:45 +00:00
|
|
|
restarts_(1, 0), // First restart point is at offset 0
|
2011-03-18 22:37:00 +00:00
|
|
|
counter_(0),
|
|
|
|
finished_(false) {
|
2018-08-15 21:27:47 +00:00
|
|
|
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);
|
|
|
|
}
|
2013-10-08 01:33:49 +00:00
|
|
|
assert(block_restart_interval_ >= 1);
|
2016-06-13 16:57:43 +00:00
|
|
|
estimate_ = sizeof(uint32_t) + sizeof(uint32_t);
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BlockBuilder::Reset() {
|
|
|
|
buffer_.clear();
|
Two performance improvements in BlockBuilder (#9039)
Summary:
Primarily, this change reserves space in the std::string for building
the next block once a block is finished, using `block_size` as
reservation size. Note: also tried reusing same std::string in the
common "unbuffered" path but that showed no benefit or regression.
Secondarily, this slightly reduces the work in resetting `restarts_`.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/9039
Test Plan:
TEST_TMPDIR=/dev/shm/rocksdb1 ./db_bench -benchmarks=fillseq -memtablerep=vector -allow_concurrent_memtable_write=false -num=50000000
Compiled with DEBUG_LEVEL=0
Test vs. control runs simulaneous for better accuracy, units = ops/sec
Run 1, Primary change only: 292697 vs. 280267 (+4.4%)
Run 2, Primary change only: 288763 vs. 279621 (+3.3%)
Run 1, Secondary change only: 260065 vs. 254232 (+2.3%)
Run 2, Secondary change only: 275925 vs. 272248 (+1.4%)
Run 1, Both changes: 284890 vs. 270372 (+5.3%)
Run 2, Both changes: 263511 vs. 258188 (+2.0%)
Reviewed By: zhichao-cao
Differential Revision: D31701253
Pulled By: pdillinger
fbshipit-source-id: 7e40810afbb98e6b6446955e77bda59e69b19ffd
2021-10-18 15:34:45 +00:00
|
|
|
restarts_.resize(1); // First restart point is at offset 0
|
|
|
|
assert(restarts_[0] == 0);
|
2016-06-13 16:57:43 +00:00
|
|
|
estimate_ = sizeof(uint32_t) + sizeof(uint32_t);
|
2011-03-18 22:37:00 +00:00
|
|
|
counter_ = 0;
|
|
|
|
finished_ = false;
|
|
|
|
last_key_.clear();
|
2018-08-15 21:27:47 +00:00
|
|
|
if (data_block_hash_index_builder_.Valid()) {
|
|
|
|
data_block_hash_index_builder_.Reset();
|
|
|
|
}
|
2021-10-19 19:35:17 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
add_with_last_key_called_ = false;
|
|
|
|
#endif
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 23:37:54 +00:00
|
|
|
void BlockBuilder::SwapAndReset(std::string& buffer) {
|
|
|
|
std::swap(buffer_, buffer);
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
2019-03-27 23:13:08 +00:00
|
|
|
size_t BlockBuilder::EstimateSizeAfterKV(const Slice& key,
|
|
|
|
const Slice& value) const {
|
2013-05-15 17:34:02 +00:00
|
|
|
size_t estimate = CurrentSizeEstimate();
|
2018-08-09 23:49:45 +00:00
|
|
|
// Note: this is an imprecise estimate as it accounts for the whole key size
|
|
|
|
// instead of non-shared key size.
|
|
|
|
estimate += key.size();
|
2023-05-25 22:41:32 +00:00
|
|
|
if (ts_sz_ > 0 && !persist_user_defined_timestamps_) {
|
|
|
|
estimate -= ts_sz_;
|
|
|
|
}
|
2018-08-09 23:49:45 +00:00
|
|
|
// 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;
|
|
|
|
|
2013-10-08 01:33:49 +00:00
|
|
|
if (counter_ >= block_restart_interval_) {
|
2019-03-27 23:13:08 +00:00
|
|
|
estimate += sizeof(uint32_t); // a new restart entry.
|
2013-05-15 17:34:02 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 23:13:08 +00:00
|
|
|
estimate += sizeof(int32_t); // varint for shared prefix length.
|
2018-08-09 23:49:45 +00:00
|
|
|
// Note: this is an imprecise estimate as we will have to encoded size, one
|
|
|
|
// for shared key and one for non-shared key.
|
2019-03-27 23:13:08 +00:00
|
|
|
estimate += VarintLength(key.size()); // varint for key length.
|
2018-08-09 23:49:45 +00:00
|
|
|
if (!use_value_delta_encoding_ || (counter_ >= block_restart_interval_)) {
|
|
|
|
estimate += VarintLength(value.size()); // varint for value length.
|
|
|
|
}
|
2013-05-15 17:34:02 +00:00
|
|
|
|
|
|
|
return estimate;
|
|
|
|
}
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
Slice BlockBuilder::Finish() {
|
|
|
|
// Append restart array
|
2011-04-20 22:48:11 +00:00
|
|
|
for (size_t i = 0; i < restarts_.size(); i++) {
|
2011-03-18 22:37:00 +00:00
|
|
|
PutFixed32(&buffer_, restarts_[i]);
|
|
|
|
}
|
2018-08-15 21:27:47 +00:00
|
|
|
|
|
|
|
uint32_t num_restarts = static_cast<uint32_t>(restarts_.size());
|
|
|
|
BlockBasedTableOptions::DataBlockIndexType index_type =
|
2018-08-17 01:29:13 +00:00
|
|
|
BlockBasedTableOptions::kDataBlockBinarySearch;
|
2018-08-15 21:27:47 +00:00
|
|
|
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
|
2018-08-17 01:29:13 +00:00
|
|
|
uint32_t block_footer = PackIndexTypeAndNumRestarts(index_type, num_restarts);
|
2018-08-15 21:27:47 +00:00
|
|
|
|
|
|
|
PutFixed32(&buffer_, block_footer);
|
2011-03-18 22:37:00 +00:00
|
|
|
finished_ = true;
|
|
|
|
return Slice(buffer_);
|
|
|
|
}
|
|
|
|
|
2018-08-09 23:49:45 +00:00
|
|
|
void BlockBuilder::Add(const Slice& key, const Slice& value,
|
|
|
|
const Slice* const delta_value) {
|
2021-10-19 19:35:17 +00:00
|
|
|
// Ensure no unsafe mixing of Add and AddWithLastKey
|
|
|
|
assert(!add_with_last_key_called_);
|
|
|
|
|
|
|
|
AddWithLastKeyImpl(key, value, last_key_, delta_value, buffer_.size());
|
|
|
|
if (use_delta_encoding_) {
|
|
|
|
// Update state
|
|
|
|
// We used to just copy the changed data, but it appears to be
|
|
|
|
// faster to just copy the whole thing.
|
|
|
|
last_key_.assign(key.data(), key.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlockBuilder::AddWithLastKey(const Slice& key, const Slice& value,
|
|
|
|
const Slice& last_key_param,
|
|
|
|
const Slice* const delta_value) {
|
|
|
|
// Ensure no unsafe mixing of Add and AddWithLastKey
|
|
|
|
assert(last_key_.empty());
|
|
|
|
#ifndef NDEBUG
|
|
|
|
add_with_last_key_called_ = false;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Here we make sure to use an empty `last_key` on first call after creation
|
|
|
|
// or Reset. This is more convenient for the caller and we can be more
|
|
|
|
// clever inside BlockBuilder. On this hot code path, we want to avoid
|
|
|
|
// conditional jumps like `buffer_.empty() ? ... : ...` so we can use a
|
|
|
|
// fast min operation instead, with an assertion to be sure our logic is
|
|
|
|
// sound.
|
|
|
|
size_t buffer_size = buffer_.size();
|
|
|
|
size_t last_key_size = last_key_param.size();
|
|
|
|
assert(buffer_size == 0 || buffer_size >= last_key_size);
|
|
|
|
|
|
|
|
Slice last_key(last_key_param.data(), std::min(buffer_size, last_key_size));
|
|
|
|
|
|
|
|
AddWithLastKeyImpl(key, value, last_key, delta_value, buffer_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void BlockBuilder::AddWithLastKeyImpl(const Slice& key,
|
|
|
|
const Slice& value,
|
|
|
|
const Slice& last_key,
|
|
|
|
const Slice* const delta_value,
|
|
|
|
size_t buffer_size) {
|
2011-03-18 22:37:00 +00:00
|
|
|
assert(!finished_);
|
2013-10-08 01:33:49 +00:00
|
|
|
assert(counter_ <= block_restart_interval_);
|
2018-08-09 23:49:45 +00:00
|
|
|
assert(!use_value_delta_encoding_ || delta_value);
|
2023-05-25 22:41:32 +00:00
|
|
|
std::string key_buf;
|
|
|
|
std::string last_key_buf;
|
|
|
|
const Slice key_to_persist = MaybeStripTimestampFromKey(&key_buf, key);
|
|
|
|
// For delta key encoding, the first key in each restart interval doesn't have
|
|
|
|
// a last key to share bytes with.
|
|
|
|
const Slice last_key_persisted =
|
|
|
|
last_key.size() == 0
|
|
|
|
? last_key
|
|
|
|
: MaybeStripTimestampFromKey(&last_key_buf, last_key);
|
2015-12-16 20:08:30 +00:00
|
|
|
size_t shared = 0; // number of bytes shared with prev key
|
|
|
|
if (counter_ >= block_restart_interval_) {
|
|
|
|
// Restart compression
|
2021-10-19 19:35:17 +00:00
|
|
|
restarts_.push_back(static_cast<uint32_t>(buffer_size));
|
2016-06-13 16:57:43 +00:00
|
|
|
estimate_ += sizeof(uint32_t);
|
2015-12-16 20:08:30 +00:00
|
|
|
counter_ = 0;
|
|
|
|
} else if (use_delta_encoding_) {
|
2011-03-18 22:37:00 +00:00
|
|
|
// See how much sharing to do with previous string
|
2023-05-25 22:41:32 +00:00
|
|
|
shared = key_to_persist.difference_offset(last_key_persisted);
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
2016-06-13 16:57:43 +00:00
|
|
|
|
2023-05-25 22:41:32 +00:00
|
|
|
const size_t non_shared = key_to_persist.size() - shared;
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2018-08-09 23:49:45 +00:00
|
|
|
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()));
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
// Add string delta to buffer_ followed by value
|
2023-05-25 22:41:32 +00:00
|
|
|
buffer_.append(key_to_persist.data() + shared, non_shared);
|
2018-08-09 23:49:45 +00:00
|
|
|
// 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());
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2023-05-25 22:41:32 +00:00
|
|
|
// TODO(yuzhangyu): make user defined timestamp work with block hash index.
|
2018-08-15 21:27:47 +00:00
|
|
|
if (data_block_hash_index_builder_.Valid()) {
|
|
|
|
data_block_hash_index_builder_.Add(ExtractUserKey(key),
|
|
|
|
restarts_.size() - 1);
|
|
|
|
}
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
counter_++;
|
2021-10-19 19:35:17 +00:00
|
|
|
estimate_ += buffer_.size() - buffer_size;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2023-05-25 22:41:32 +00:00
|
|
|
const Slice BlockBuilder::MaybeStripTimestampFromKey(std::string* key_buf,
|
|
|
|
const Slice& key) {
|
|
|
|
Slice stripped_key = key;
|
|
|
|
if (ts_sz_ > 0 && !persist_user_defined_timestamps_) {
|
|
|
|
if (is_user_key_) {
|
|
|
|
stripped_key.remove_suffix(ts_sz_);
|
|
|
|
} else {
|
|
|
|
StripTimestampFromInternalKey(key_buf, key, ts_sz_);
|
|
|
|
stripped_key = *key_buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return stripped_key;
|
|
|
|
}
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|