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.
214 lines
8.7 KiB
214 lines
8.7 KiB
![]()
8 years ago
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||
![]()
7 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).
|
||
![]()
8 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.
|
||
|
|
||
![]()
5 years ago
|
#include "table/block_based/index_builder.h"
|
||
|
|
||
![]()
8 years ago
|
#include <assert.h>
|
||
|
#include <inttypes.h>
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
#include <list>
|
||
|
#include <string>
|
||
|
|
||
|
#include "rocksdb/comparator.h"
|
||
![]()
8 years ago
|
#include "rocksdb/flush_block_policy.h"
|
||
![]()
8 years ago
|
#include "table/format.h"
|
||
![]()
5 years ago
|
#include "table/block_based/partitioned_filter_block.h"
|
||
![]()
8 years ago
|
|
||
|
// 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,
|
||
![]()
8 years ago
|
const InternalKeySliceTransform* int_key_slice_transform,
|
||
![]()
6 years ago
|
const bool use_value_delta_encoding,
|
||
![]()
8 years ago
|
const BlockBasedTableOptions& table_opt) {
|
||
![]()
7 years ago
|
IndexBuilder* result = nullptr;
|
||
![]()
8 years ago
|
switch (index_type) {
|
||
|
case BlockBasedTableOptions::kBinarySearch: {
|
||
![]()
6 years ago
|
result = new ShortenedIndexBuilder(
|
||
|
comparator, table_opt.index_block_restart_interval,
|
||
![]()
5 years ago
|
table_opt.format_version, use_value_delta_encoding,
|
||
|
table_opt.index_shortening);
|
||
|
} break;
|
||
![]()
8 years ago
|
case BlockBasedTableOptions::kHashSearch: {
|
||
![]()
5 years ago
|
result = new HashIndexBuilder(
|
||
|
comparator, int_key_slice_transform,
|
||
|
table_opt.index_block_restart_interval, table_opt.format_version,
|
||
|
use_value_delta_encoding, table_opt.index_shortening);
|
||
|
} break;
|
||
![]()
8 years ago
|
case BlockBasedTableOptions::kTwoLevelIndexSearch: {
|
||
![]()
6 years ago
|
result = PartitionedIndexBuilder::CreateIndexBuilder(
|
||
|
comparator, use_value_delta_encoding, table_opt);
|
||
![]()
5 years ago
|
} break;
|
||
![]()
8 years ago
|
default: {
|
||
|
assert(!"Do not recognize the index type ");
|
||
![]()
5 years ago
|
} break;
|
||
![]()
8 years ago
|
}
|
||
![]()
7 years ago
|
return result;
|
||
![]()
8 years ago
|
}
|
||
![]()
8 years ago
|
|
||
|
PartitionedIndexBuilder* PartitionedIndexBuilder::CreateIndexBuilder(
|
||
|
const InternalKeyComparator* comparator,
|
||
![]()
6 years ago
|
const bool use_value_delta_encoding,
|
||
![]()
8 years ago
|
const BlockBasedTableOptions& table_opt) {
|
||
![]()
6 years ago
|
return new PartitionedIndexBuilder(comparator, table_opt,
|
||
|
use_value_delta_encoding);
|
||
![]()
8 years ago
|
}
|
||
|
|
||
|
PartitionedIndexBuilder::PartitionedIndexBuilder(
|
||
|
const InternalKeyComparator* comparator,
|
||
![]()
6 years ago
|
const BlockBasedTableOptions& table_opt,
|
||
|
const bool use_value_delta_encoding)
|
||
![]()
8 years ago
|
: IndexBuilder(comparator),
|
||
![]()
6 years ago
|
index_block_builder_(table_opt.index_block_restart_interval,
|
||
![]()
6 years ago
|
true /*use_delta_encoding*/,
|
||
|
use_value_delta_encoding),
|
||
![]()
6 years ago
|
index_block_builder_without_seq_(table_opt.index_block_restart_interval,
|
||
![]()
6 years ago
|
true /*use_delta_encoding*/,
|
||
|
use_value_delta_encoding),
|
||
![]()
8 years ago
|
sub_index_builder_(nullptr),
|
||
![]()
6 years ago
|
table_opt_(table_opt),
|
||
![]()
6 years ago
|
// We start by false. After each partition we revise the value based on
|
||
|
// what the sub_index_builder has decided. If the feature is disabled
|
||
|
// entirely, this will be set to true after switching the first
|
||
|
// sub_index_builder. Otherwise, it could be set to true even one of the
|
||
|
// sub_index_builders could not safely exclude seq from the keys, then it
|
||
|
// wil be enforced on all sub_index_builders on ::Finish.
|
||
![]()
6 years ago
|
seperator_is_key_plus_seq_(false),
|
||
|
use_value_delta_encoding_(use_value_delta_encoding) {}
|
||
![]()
8 years ago
|
|
||
|
PartitionedIndexBuilder::~PartitionedIndexBuilder() {
|
||
|
delete sub_index_builder_;
|
||
|
}
|
||
|
|
||
![]()
8 years ago
|
void PartitionedIndexBuilder::MakeNewSubIndexBuilder() {
|
||
|
assert(sub_index_builder_ == nullptr);
|
||
|
sub_index_builder_ = new ShortenedIndexBuilder(
|
||
![]()
6 years ago
|
comparator_, table_opt_.index_block_restart_interval,
|
||
![]()
5 years ago
|
table_opt_.format_version, use_value_delta_encoding_,
|
||
|
table_opt_.index_shortening);
|
||
![]()
8 years ago
|
flush_policy_.reset(FlushBlockBySizePolicyFactory::NewFlushBlockPolicy(
|
||
|
table_opt_.metadata_block_size, table_opt_.block_size_deviation,
|
||
![]()
6 years ago
|
// Note: this is sub-optimal since sub_index_builder_ could later reset
|
||
|
// seperator_is_key_plus_seq_ but the probability of that is low.
|
||
|
sub_index_builder_->seperator_is_key_plus_seq_
|
||
|
? sub_index_builder_->index_block_builder_
|
||
|
: sub_index_builder_->index_block_builder_without_seq_));
|
||
![]()
7 years ago
|
partition_cut_requested_ = false;
|
||
|
}
|
||
|
|
||
|
void PartitionedIndexBuilder::RequestPartitionCut() {
|
||
|
partition_cut_requested_ = true;
|
||
![]()
8 years ago
|
}
|
||
|
|
||
![]()
8 years ago
|
void PartitionedIndexBuilder::AddIndexEntry(
|
||
|
std::string* last_key_in_current_block,
|
||
|
const Slice* first_key_in_next_block, const BlockHandle& block_handle) {
|
||
![]()
8 years ago
|
// Note: to avoid two consecuitive flush in the same method call, we do not
|
||
|
// check flush policy when adding the last key
|
||
![]()
8 years ago
|
if (UNLIKELY(first_key_in_next_block == nullptr)) { // no more keys
|
||
![]()
8 years ago
|
if (sub_index_builder_ == nullptr) {
|
||
|
MakeNewSubIndexBuilder();
|
||
|
}
|
||
|
sub_index_builder_->AddIndexEntry(last_key_in_current_block,
|
||
|
first_key_in_next_block, block_handle);
|
||
![]()
6 years ago
|
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;
|
||
|
}
|
||
![]()
8 years ago
|
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_)});
|
||
![]()
8 years ago
|
sub_index_builder_ = nullptr;
|
||
|
cut_filter_block = true;
|
||
![]()
8 years ago
|
} 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 =
|
||
![]()
7 years ago
|
partition_cut_requested_ ||
|
||
![]()
8 years ago
|
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);
|
||
![]()
6 years ago
|
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;
|
||
|
}
|
||
![]()
8 years ago
|
}
|
||
|
}
|
||
|
|
||
|
Status PartitionedIndexBuilder::Finish(
|
||
|
IndexBlocks* index_blocks, const BlockHandle& last_partition_block_handle) {
|
||
![]()
6 years ago
|
if (partition_cnt_ == 0) {
|
||
|
partition_cnt_ = entries_.size();
|
||
|
}
|
||
![]()
8 years ago
|
// 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);
|
||
![]()
6 years ago
|
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);
|
||
![]()
6 years ago
|
if (!seperator_is_key_plus_seq_) {
|
||
|
index_block_builder_without_seq_.Add(ExtractUserKey(last_entry.key),
|
||
![]()
6 years ago
|
handle_encoding,
|
||
|
&handle_delta_encoding_slice);
|
||
![]()
6 years ago
|
}
|
||
![]()
8 years ago
|
entries_.pop_front();
|
||
|
}
|
||
|
// If there is no sub_index left, then return the 2nd level index.
|
||
|
if (UNLIKELY(entries_.empty())) {
|
||
![]()
6 years ago
|
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();
|
||
|
}
|
||
![]()
6 years ago
|
top_level_index_size_ = index_blocks->index_block_contents.size();
|
||
|
index_size_ += top_level_index_size_;
|
||
![]()
8 years ago
|
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();
|
||
![]()
6 years ago
|
// Apply the policy to all sub-indexes
|
||
|
entry.value->seperator_is_key_plus_seq_ = seperator_is_key_plus_seq_;
|
||
![]()
8 years ago
|
auto s = entry.value->Finish(index_blocks);
|
||
![]()
6 years ago
|
index_size_ += index_blocks->index_block_contents.size();
|
||
![]()
8 years ago
|
finishing_indexes = true;
|
||
|
return s.ok() ? Status::Incomplete() : s;
|
||
|
}
|
||
|
}
|
||
|
|
||
![]()
6 years ago
|
size_t PartitionedIndexBuilder::NumPartitions() const { return partition_cnt_; }
|
||
![]()
8 years ago
|
} // namespace rocksdb
|