2017-03-04 02:09:43 +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).
|
2017-03-04 02:09:43 +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.
|
|
|
|
|
|
|
|
#include "table/index_builder.h"
|
|
|
|
#include <assert.h>
|
|
|
|
#include <inttypes.h>
|
2017-03-07 21:48:02 +00:00
|
|
|
|
2017-03-04 02:09:43 +00:00
|
|
|
#include <list>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "rocksdb/comparator.h"
|
2017-03-28 18:56:56 +00:00
|
|
|
#include "rocksdb/flush_block_policy.h"
|
2017-03-04 02:09:43 +00:00
|
|
|
#include "table/format.h"
|
|
|
|
#include "table/partitioned_filter_block.h"
|
|
|
|
|
|
|
|
// Without anonymous namespace here, we fail the warning -Wmissing-prototypes
|
|
|
|
namespace rocksdb {
|
|
|
|
// using namespace rocksdb;
|
|
|
|
// Create a index builder based on its type.
|
|
|
|
IndexBuilder* IndexBuilder::CreateIndexBuilder(
|
|
|
|
BlockBasedTableOptions::IndexType index_type,
|
|
|
|
const InternalKeyComparator* comparator,
|
2017-03-07 21:48:02 +00:00
|
|
|
const InternalKeySliceTransform* int_key_slice_transform,
|
2018-08-09 23:49:45 +00:00
|
|
|
const bool use_value_delta_encoding,
|
2017-03-07 21:48:02 +00:00
|
|
|
const BlockBasedTableOptions& table_opt) {
|
2017-10-19 17:48:47 +00:00
|
|
|
IndexBuilder* result = nullptr;
|
2017-03-04 02:09:43 +00:00
|
|
|
switch (index_type) {
|
|
|
|
case BlockBasedTableOptions::kBinarySearch: {
|
2018-08-09 23:49:45 +00:00
|
|
|
result = new ShortenedIndexBuilder(
|
|
|
|
comparator, table_opt.index_block_restart_interval,
|
|
|
|
table_opt.format_version, use_value_delta_encoding);
|
2017-03-04 02:09:43 +00:00
|
|
|
}
|
2017-10-19 17:48:47 +00:00
|
|
|
break;
|
2017-03-04 02:09:43 +00:00
|
|
|
case BlockBasedTableOptions::kHashSearch: {
|
2017-10-19 17:48:47 +00:00
|
|
|
result = new HashIndexBuilder(comparator, int_key_slice_transform,
|
2018-05-26 01:41:31 +00:00
|
|
|
table_opt.index_block_restart_interval,
|
2018-08-09 23:49:45 +00:00
|
|
|
table_opt.format_version,
|
|
|
|
use_value_delta_encoding);
|
2017-03-04 02:09:43 +00:00
|
|
|
}
|
2017-10-19 17:48:47 +00:00
|
|
|
break;
|
2017-03-04 02:09:43 +00:00
|
|
|
case BlockBasedTableOptions::kTwoLevelIndexSearch: {
|
2018-08-09 23:49:45 +00:00
|
|
|
result = PartitionedIndexBuilder::CreateIndexBuilder(
|
|
|
|
comparator, use_value_delta_encoding, table_opt);
|
2017-03-04 02:09:43 +00:00
|
|
|
}
|
2017-10-19 17:48:47 +00:00
|
|
|
break;
|
2017-03-04 02:09:43 +00:00
|
|
|
default: {
|
|
|
|
assert(!"Do not recognize the index type ");
|
|
|
|
}
|
2017-10-19 17:48:47 +00:00
|
|
|
break;
|
2017-03-04 02:09:43 +00:00
|
|
|
}
|
2017-10-19 17:48:47 +00:00
|
|
|
return result;
|
2017-03-04 02:09:43 +00:00
|
|
|
}
|
2017-03-07 21:48:02 +00:00
|
|
|
|
|
|
|
PartitionedIndexBuilder* PartitionedIndexBuilder::CreateIndexBuilder(
|
|
|
|
const InternalKeyComparator* comparator,
|
2018-08-09 23:49:45 +00:00
|
|
|
const bool use_value_delta_encoding,
|
2017-03-07 21:48:02 +00:00
|
|
|
const BlockBasedTableOptions& table_opt) {
|
2018-08-09 23:49:45 +00:00
|
|
|
return new PartitionedIndexBuilder(comparator, table_opt,
|
|
|
|
use_value_delta_encoding);
|
2017-03-07 21:48:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PartitionedIndexBuilder::PartitionedIndexBuilder(
|
|
|
|
const InternalKeyComparator* comparator,
|
2018-08-09 23:49:45 +00:00
|
|
|
const BlockBasedTableOptions& table_opt,
|
|
|
|
const bool use_value_delta_encoding)
|
2017-03-07 21:48:02 +00:00
|
|
|
: IndexBuilder(comparator),
|
2018-05-26 01:41:31 +00:00
|
|
|
index_block_builder_(table_opt.index_block_restart_interval,
|
2018-08-09 23:49:45 +00:00
|
|
|
true /*use_delta_encoding*/,
|
|
|
|
use_value_delta_encoding),
|
2018-06-06 23:44:52 +00:00
|
|
|
index_block_builder_without_seq_(table_opt.index_block_restart_interval,
|
2018-08-09 23:49:45 +00:00
|
|
|
true /*use_delta_encoding*/,
|
|
|
|
use_value_delta_encoding),
|
2017-03-28 18:56:56 +00:00
|
|
|
sub_index_builder_(nullptr),
|
2018-05-26 01:41:31 +00:00
|
|
|
table_opt_(table_opt),
|
2018-08-09 23:49:45 +00:00
|
|
|
seperator_is_key_plus_seq_(false),
|
|
|
|
use_value_delta_encoding_(use_value_delta_encoding) {}
|
2017-03-07 21:48:02 +00:00
|
|
|
|
|
|
|
PartitionedIndexBuilder::~PartitionedIndexBuilder() {
|
|
|
|
delete sub_index_builder_;
|
|
|
|
}
|
|
|
|
|
2017-03-28 18:56:56 +00:00
|
|
|
void PartitionedIndexBuilder::MakeNewSubIndexBuilder() {
|
|
|
|
assert(sub_index_builder_ == nullptr);
|
|
|
|
sub_index_builder_ = new ShortenedIndexBuilder(
|
2018-05-26 01:41:31 +00:00
|
|
|
comparator_, table_opt_.index_block_restart_interval,
|
2018-08-09 23:49:45 +00:00
|
|
|
table_opt_.format_version, use_value_delta_encoding_);
|
2017-03-28 18:56:56 +00:00
|
|
|
flush_policy_.reset(FlushBlockBySizePolicyFactory::NewFlushBlockPolicy(
|
|
|
|
table_opt_.metadata_block_size, table_opt_.block_size_deviation,
|
|
|
|
sub_index_builder_->index_block_builder_));
|
2017-07-02 17:36:10 +00:00
|
|
|
partition_cut_requested_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PartitionedIndexBuilder::RequestPartitionCut() {
|
|
|
|
partition_cut_requested_ = true;
|
2017-03-28 18:56:56 +00:00
|
|
|
}
|
|
|
|
|
2017-03-07 21:48:02 +00:00
|
|
|
void PartitionedIndexBuilder::AddIndexEntry(
|
|
|
|
std::string* last_key_in_current_block,
|
|
|
|
const Slice* first_key_in_next_block, const BlockHandle& block_handle) {
|
2017-03-28 18:56:56 +00:00
|
|
|
// Note: to avoid two consecuitive flush in the same method call, we do not
|
|
|
|
// check flush policy when adding the last key
|
2017-03-07 21:48:02 +00:00
|
|
|
if (UNLIKELY(first_key_in_next_block == nullptr)) { // no more keys
|
2017-03-28 18:56:56 +00:00
|
|
|
if (sub_index_builder_ == nullptr) {
|
|
|
|
MakeNewSubIndexBuilder();
|
|
|
|
}
|
|
|
|
sub_index_builder_->AddIndexEntry(last_key_in_current_block,
|
|
|
|
first_key_in_next_block, block_handle);
|
2018-05-26 01:41:31 +00:00
|
|
|
if (sub_index_builder_->seperator_is_key_plus_seq_) {
|
|
|
|
// then we need to apply it to all sub-index builders
|
|
|
|
seperator_is_key_plus_seq_ = true;
|
|
|
|
}
|
2017-03-28 18:56:56 +00:00
|
|
|
sub_index_last_key_ = std::string(*last_key_in_current_block);
|
|
|
|
entries_.push_back(
|
|
|
|
{sub_index_last_key_,
|
|
|
|
std::unique_ptr<ShortenedIndexBuilder>(sub_index_builder_)});
|
2017-03-07 21:48:02 +00:00
|
|
|
sub_index_builder_ = nullptr;
|
|
|
|
cut_filter_block = true;
|
2017-03-28 18:56:56 +00:00
|
|
|
} else {
|
|
|
|
// apply flush policy only to non-empty sub_index_builder_
|
|
|
|
if (sub_index_builder_ != nullptr) {
|
|
|
|
std::string handle_encoding;
|
|
|
|
block_handle.EncodeTo(&handle_encoding);
|
|
|
|
bool do_flush =
|
2017-07-02 17:36:10 +00:00
|
|
|
partition_cut_requested_ ||
|
2017-03-28 18:56:56 +00:00
|
|
|
flush_policy_->Update(*last_key_in_current_block, handle_encoding);
|
|
|
|
if (do_flush) {
|
|
|
|
entries_.push_back(
|
|
|
|
{sub_index_last_key_,
|
|
|
|
std::unique_ptr<ShortenedIndexBuilder>(sub_index_builder_)});
|
|
|
|
cut_filter_block = true;
|
|
|
|
sub_index_builder_ = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sub_index_builder_ == nullptr) {
|
|
|
|
MakeNewSubIndexBuilder();
|
|
|
|
}
|
|
|
|
sub_index_builder_->AddIndexEntry(last_key_in_current_block,
|
|
|
|
first_key_in_next_block, block_handle);
|
|
|
|
sub_index_last_key_ = std::string(*last_key_in_current_block);
|
2018-05-26 01:41:31 +00:00
|
|
|
if (sub_index_builder_->seperator_is_key_plus_seq_) {
|
|
|
|
// then we need to apply it to all sub-index builders
|
|
|
|
seperator_is_key_plus_seq_ = true;
|
|
|
|
}
|
2017-03-07 21:48:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Status PartitionedIndexBuilder::Finish(
|
|
|
|
IndexBlocks* index_blocks, const BlockHandle& last_partition_block_handle) {
|
|
|
|
// It must be set to null after last key is added
|
|
|
|
assert(sub_index_builder_ == nullptr);
|
|
|
|
if (finishing_indexes == true) {
|
|
|
|
Entry& last_entry = entries_.front();
|
|
|
|
std::string handle_encoding;
|
|
|
|
last_partition_block_handle.EncodeTo(&handle_encoding);
|
2018-08-09 23:49:45 +00:00
|
|
|
std::string handle_delta_encoding;
|
|
|
|
PutVarsignedint64(
|
|
|
|
&handle_delta_encoding,
|
|
|
|
last_partition_block_handle.size() - last_encoded_handle_.size());
|
|
|
|
last_encoded_handle_ = last_partition_block_handle;
|
|
|
|
const Slice handle_delta_encoding_slice(handle_delta_encoding);
|
|
|
|
index_block_builder_.Add(last_entry.key, handle_encoding,
|
|
|
|
&handle_delta_encoding_slice);
|
2018-06-06 23:44:52 +00:00
|
|
|
if (!seperator_is_key_plus_seq_) {
|
|
|
|
index_block_builder_without_seq_.Add(ExtractUserKey(last_entry.key),
|
2018-08-09 23:49:45 +00:00
|
|
|
handle_encoding,
|
|
|
|
&handle_delta_encoding_slice);
|
2018-06-06 23:44:52 +00:00
|
|
|
}
|
2017-03-07 21:48:02 +00:00
|
|
|
entries_.pop_front();
|
|
|
|
}
|
|
|
|
// If there is no sub_index left, then return the 2nd level index.
|
|
|
|
if (UNLIKELY(entries_.empty())) {
|
2018-06-06 23:44:52 +00:00
|
|
|
if (seperator_is_key_plus_seq_) {
|
|
|
|
index_blocks->index_block_contents = index_block_builder_.Finish();
|
|
|
|
} else {
|
|
|
|
index_blocks->index_block_contents =
|
|
|
|
index_block_builder_without_seq_.Finish();
|
|
|
|
}
|
2017-03-07 21:48:02 +00:00
|
|
|
return Status::OK();
|
|
|
|
} else {
|
|
|
|
// Finish the next partition index in line and Incomplete() to indicate we
|
|
|
|
// expect more calls to Finish
|
|
|
|
Entry& entry = entries_.front();
|
2018-05-26 01:41:31 +00:00
|
|
|
// Apply the policy to all sub-indexes
|
|
|
|
entry.value->seperator_is_key_plus_seq_ = seperator_is_key_plus_seq_;
|
2017-03-07 21:48:02 +00:00
|
|
|
auto s = entry.value->Finish(index_blocks);
|
|
|
|
finishing_indexes = true;
|
|
|
|
return s.ok() ? Status::Incomplete() : s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-13 17:59:22 +00:00
|
|
|
// Estimate size excluding the top-level index
|
|
|
|
// It is assumed that this method is called before writing index partition
|
|
|
|
// starts
|
2017-03-07 21:48:02 +00:00
|
|
|
size_t PartitionedIndexBuilder::EstimatedSize() const {
|
|
|
|
size_t total = 0;
|
|
|
|
for (auto it = entries_.begin(); it != entries_.end(); ++it) {
|
|
|
|
total += it->value->EstimatedSize();
|
|
|
|
}
|
|
|
|
total +=
|
|
|
|
sub_index_builder_ == nullptr ? 0 : sub_index_builder_->EstimatedSize();
|
|
|
|
return total;
|
|
|
|
}
|
2017-06-13 17:59:22 +00:00
|
|
|
|
|
|
|
// Since when this method is called we do not know the index block offsets yet,
|
|
|
|
// the top-level index does not exist. Hence we estimate the block offsets and
|
|
|
|
// create a temporary top-level index.
|
|
|
|
size_t PartitionedIndexBuilder::EstimateTopLevelIndexSize(
|
|
|
|
uint64_t offset) const {
|
|
|
|
BlockBuilder tmp_builder(
|
|
|
|
table_opt_.index_block_restart_interval); // tmp top-level index builder
|
|
|
|
for (auto it = entries_.begin(); it != entries_.end(); ++it) {
|
|
|
|
std::string tmp_handle_encoding;
|
|
|
|
uint64_t size = it->value->EstimatedSize();
|
|
|
|
BlockHandle tmp_block_handle(offset, size);
|
|
|
|
tmp_block_handle.EncodeTo(&tmp_handle_encoding);
|
2018-08-09 23:49:45 +00:00
|
|
|
std::string handle_delta_encoding;
|
|
|
|
tmp_block_handle.EncodeSizeTo(&handle_delta_encoding);
|
|
|
|
const Slice handle_delta_encoding_slice(handle_delta_encoding);
|
2018-06-06 23:44:52 +00:00
|
|
|
tmp_builder.Add(
|
|
|
|
seperator_is_key_plus_seq_ ? it->key : ExtractUserKey(it->key),
|
2018-08-09 23:49:45 +00:00
|
|
|
tmp_handle_encoding, &handle_delta_encoding_slice);
|
2017-06-13 17:59:22 +00:00
|
|
|
offset += size;
|
|
|
|
}
|
|
|
|
return tmp_builder.CurrentSizeEstimate();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t PartitionedIndexBuilder::NumPartitions() const {
|
|
|
|
return entries_.size();
|
|
|
|
}
|
2017-03-04 02:09:43 +00:00
|
|
|
} // namespace rocksdb
|