2019-04-18 17:51:19 +00:00
|
|
|
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
2013-10-29 03:34:02 +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.
|
|
|
|
|
2019-05-30 21:47:29 +00:00
|
|
|
#include "table/plain/plain_table_factory.h"
|
2013-10-29 03:34:02 +00:00
|
|
|
|
2023-12-01 19:15:17 +00:00
|
|
|
#include <cstdint>
|
2017-07-28 23:23:50 +00:00
|
|
|
#include <memory>
|
2020-04-22 00:35:28 +00:00
|
|
|
|
2014-01-27 21:53:22 +00:00
|
|
|
#include "db/dbformat.h"
|
2017-07-28 23:23:50 +00:00
|
|
|
#include "port/port.h"
|
|
|
|
#include "rocksdb/convenience.h"
|
2021-09-08 14:45:59 +00:00
|
|
|
#include "rocksdb/utilities/customizable_util.h"
|
|
|
|
#include "rocksdb/utilities/object_registry.h"
|
2020-09-14 23:59:00 +00:00
|
|
|
#include "rocksdb/utilities/options_type.h"
|
2019-05-30 21:47:29 +00:00
|
|
|
#include "table/plain/plain_table_builder.h"
|
|
|
|
#include "table/plain/plain_table_reader.h"
|
2017-07-28 23:23:50 +00:00
|
|
|
#include "util/string_util.h"
|
2013-10-29 03:34:02 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2020-04-03 17:48:46 +00:00
|
|
|
static std::unordered_map<std::string, OptionTypeInfo> plain_table_type_info = {
|
|
|
|
{"user_key_len",
|
|
|
|
{offsetof(struct PlainTableOptions, user_key_len), OptionType::kUInt32T,
|
2020-09-14 23:59:00 +00:00
|
|
|
OptionVerificationType::kNormal, OptionTypeFlags::kNone}},
|
2020-04-03 17:48:46 +00:00
|
|
|
{"bloom_bits_per_key",
|
|
|
|
{offsetof(struct PlainTableOptions, bloom_bits_per_key), OptionType::kInt,
|
2020-09-14 23:59:00 +00:00
|
|
|
OptionVerificationType::kNormal, OptionTypeFlags::kNone}},
|
2020-04-03 17:48:46 +00:00
|
|
|
{"hash_table_ratio",
|
|
|
|
{offsetof(struct PlainTableOptions, hash_table_ratio), OptionType::kDouble,
|
2020-09-14 23:59:00 +00:00
|
|
|
OptionVerificationType::kNormal, OptionTypeFlags::kNone}},
|
2020-04-03 17:48:46 +00:00
|
|
|
{"index_sparseness",
|
|
|
|
{offsetof(struct PlainTableOptions, index_sparseness), OptionType::kSizeT,
|
2020-09-14 23:59:00 +00:00
|
|
|
OptionVerificationType::kNormal, OptionTypeFlags::kNone}},
|
2020-04-03 17:48:46 +00:00
|
|
|
{"huge_page_tlb_size",
|
|
|
|
{offsetof(struct PlainTableOptions, huge_page_tlb_size),
|
|
|
|
OptionType::kSizeT, OptionVerificationType::kNormal,
|
2020-09-14 23:59:00 +00:00
|
|
|
OptionTypeFlags::kNone}},
|
2020-04-03 17:48:46 +00:00
|
|
|
{"encoding_type",
|
|
|
|
{offsetof(struct PlainTableOptions, encoding_type),
|
2020-09-14 23:59:00 +00:00
|
|
|
OptionType::kEncodingType, OptionVerificationType::kNormal,
|
|
|
|
OptionTypeFlags::kNone}},
|
2020-04-03 17:48:46 +00:00
|
|
|
{"full_scan_mode",
|
|
|
|
{offsetof(struct PlainTableOptions, full_scan_mode), OptionType::kBoolean,
|
2020-09-14 23:59:00 +00:00
|
|
|
OptionVerificationType::kNormal, OptionTypeFlags::kNone}},
|
2020-04-03 17:48:46 +00:00
|
|
|
{"store_index_in_file",
|
|
|
|
{offsetof(struct PlainTableOptions, store_index_in_file),
|
|
|
|
OptionType::kBoolean, OptionVerificationType::kNormal,
|
2020-09-14 23:59:00 +00:00
|
|
|
OptionTypeFlags::kNone}},
|
|
|
|
};
|
|
|
|
|
|
|
|
PlainTableFactory::PlainTableFactory(const PlainTableOptions& options)
|
|
|
|
: table_options_(options) {
|
2021-04-26 10:12:35 +00:00
|
|
|
RegisterOptions(&table_options_, &plain_table_type_info);
|
2020-09-14 23:59:00 +00:00
|
|
|
}
|
2013-10-29 03:34:02 +00:00
|
|
|
|
Move rate_limiter, write buffering, most perf context instrumentation and most random kill out of Env
Summary: We want to keep Env a think layer for better portability. Less platform dependent codes should be moved out of Env. In this patch, I create a wrapper of file readers and writers, and put rate limiting, write buffering, as well as most perf context instrumentation and random kill out of Env. It will make it easier to maintain multiple Env in the future.
Test Plan: Run all existing unit tests.
Reviewers: anthony, kradhakrishnan, IslamAbdelRahman, yhchiang, igor
Reviewed By: igor
Subscribers: leveldb, dhruba
Differential Revision: https://reviews.facebook.net/D42321
2015-07-17 23:16:11 +00:00
|
|
|
Status PlainTableFactory::NewTableReader(
|
2020-06-29 21:51:57 +00:00
|
|
|
const ReadOptions& /*ro*/, const TableReaderOptions& table_reader_options,
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<RandomAccessFileReader>&& file, uint64_t file_size,
|
|
|
|
std::unique_ptr<TableReader>* table,
|
2018-03-05 21:08:17 +00:00
|
|
|
bool /*prefetch_index_and_filter_in_cache*/) const {
|
2015-09-11 18:36:33 +00:00
|
|
|
return PlainTableReader::Open(
|
|
|
|
table_reader_options.ioptions, table_reader_options.env_options,
|
|
|
|
table_reader_options.internal_comparator, std::move(file), file_size,
|
2015-11-16 20:56:21 +00:00
|
|
|
table, table_options_.bloom_bits_per_key, table_options_.hash_table_ratio,
|
|
|
|
table_options_.index_sparseness, table_options_.huge_page_tlb_size,
|
2019-01-26 01:07:00 +00:00
|
|
|
table_options_.full_scan_mode, table_reader_options.immortal,
|
2022-01-21 19:36:36 +00:00
|
|
|
table_reader_options.prefix_extractor.get());
|
2013-10-29 03:34:02 +00:00
|
|
|
}
|
|
|
|
|
2014-01-28 05:58:46 +00:00
|
|
|
TableBuilder* PlainTableFactory::NewTableBuilder(
|
2021-04-29 13:59:53 +00:00
|
|
|
const TableBuilderOptions& table_builder_options,
|
Move rate_limiter, write buffering, most perf context instrumentation and most random kill out of Env
Summary: We want to keep Env a think layer for better portability. Less platform dependent codes should be moved out of Env. In this patch, I create a wrapper of file readers and writers, and put rate limiting, write buffering, as well as most perf context instrumentation and random kill out of Env. It will make it easier to maintain multiple Env in the future.
Test Plan: Run all existing unit tests.
Reviewers: anthony, kradhakrishnan, IslamAbdelRahman, yhchiang, igor
Reviewed By: igor
Subscribers: leveldb, dhruba
Differential Revision: https://reviews.facebook.net/D42321
2015-07-17 23:16:11 +00:00
|
|
|
WritableFileWriter* file) const {
|
2015-02-17 16:03:45 +00:00
|
|
|
// Ignore the skip_filters flag. PlainTable format is optimized for small
|
|
|
|
// in-memory dbs. The skip_filters optimization is not useful for plain
|
|
|
|
// tables
|
|
|
|
//
|
2015-07-08 14:35:56 +00:00
|
|
|
return new PlainTableBuilder(
|
2018-05-21 21:33:55 +00:00
|
|
|
table_builder_options.ioptions, table_builder_options.moptions,
|
2024-02-02 22:14:43 +00:00
|
|
|
table_builder_options.internal_tbl_prop_coll_factories,
|
2021-09-28 19:33:03 +00:00
|
|
|
table_builder_options.column_family_id,
|
|
|
|
table_builder_options.level_at_creation, file,
|
|
|
|
table_options_.user_key_len, table_options_.encoding_type,
|
|
|
|
table_options_.index_sparseness, table_options_.bloom_bits_per_key,
|
2019-03-01 23:41:55 +00:00
|
|
|
table_builder_options.column_family_name, 6,
|
|
|
|
table_options_.huge_page_tlb_size, table_options_.hash_table_ratio,
|
2020-06-17 17:55:42 +00:00
|
|
|
table_options_.store_index_in_file, table_builder_options.db_id,
|
2021-08-21 03:39:52 +00:00
|
|
|
table_builder_options.db_session_id, table_builder_options.cur_file_num);
|
2013-10-29 03:34:02 +00:00
|
|
|
}
|
2014-01-28 05:58:46 +00:00
|
|
|
|
2020-09-14 23:59:00 +00:00
|
|
|
std::string PlainTableFactory::GetPrintableOptions() const {
|
2014-08-25 21:24:09 +00:00
|
|
|
std::string ret;
|
|
|
|
ret.reserve(20000);
|
|
|
|
const int kBufferSize = 200;
|
|
|
|
char buffer[kBufferSize];
|
|
|
|
|
|
|
|
snprintf(buffer, kBufferSize, " user_key_len: %u\n",
|
2015-10-28 17:46:01 +00:00
|
|
|
table_options_.user_key_len);
|
2014-08-25 21:24:09 +00:00
|
|
|
ret.append(buffer);
|
2019-03-01 23:41:55 +00:00
|
|
|
snprintf(buffer, kBufferSize, " bloom_bits_per_key: %d\n",
|
|
|
|
table_options_.bloom_bits_per_key);
|
|
|
|
ret.append(buffer);
|
|
|
|
snprintf(buffer, kBufferSize, " hash_table_ratio: %lf\n",
|
|
|
|
table_options_.hash_table_ratio);
|
|
|
|
ret.append(buffer);
|
2015-07-01 23:13:49 +00:00
|
|
|
snprintf(buffer, kBufferSize, " index_sparseness: %" ROCKSDB_PRIszt "\n",
|
2015-10-28 17:46:01 +00:00
|
|
|
table_options_.index_sparseness);
|
2014-08-25 21:24:09 +00:00
|
|
|
ret.append(buffer);
|
2019-03-01 23:41:55 +00:00
|
|
|
snprintf(buffer, kBufferSize, " huge_page_tlb_size: %" ROCKSDB_PRIszt "\n",
|
|
|
|
table_options_.huge_page_tlb_size);
|
|
|
|
ret.append(buffer);
|
2014-08-25 21:24:09 +00:00
|
|
|
snprintf(buffer, kBufferSize, " encoding_type: %d\n",
|
2015-10-28 17:46:01 +00:00
|
|
|
table_options_.encoding_type);
|
2014-08-25 21:24:09 +00:00
|
|
|
ret.append(buffer);
|
|
|
|
snprintf(buffer, kBufferSize, " full_scan_mode: %d\n",
|
2015-10-28 17:46:01 +00:00
|
|
|
table_options_.full_scan_mode);
|
2014-08-25 21:24:09 +00:00
|
|
|
ret.append(buffer);
|
2019-03-01 23:41:55 +00:00
|
|
|
snprintf(buffer, kBufferSize, " store_index_in_file: %d\n",
|
|
|
|
table_options_.store_index_in_file);
|
|
|
|
ret.append(buffer);
|
2014-08-25 21:24:09 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-04-22 00:35:28 +00:00
|
|
|
Status GetPlainTableOptionsFromString(const ConfigOptions& config_options,
|
|
|
|
const PlainTableOptions& table_options,
|
|
|
|
const std::string& opts_str,
|
|
|
|
PlainTableOptions* new_table_options) {
|
2017-07-28 23:23:50 +00:00
|
|
|
std::unordered_map<std::string, std::string> opts_map;
|
|
|
|
Status s = StringToMap(opts_str, &opts_map);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
2020-04-22 00:35:28 +00:00
|
|
|
|
2020-10-20 18:51:51 +00:00
|
|
|
s = GetPlainTableOptionsFromMap(config_options, table_options, opts_map,
|
|
|
|
new_table_options);
|
|
|
|
// Translate any errors (NotFound, NotSupported, to InvalidArgument
|
|
|
|
if (s.ok() || s.IsInvalidArgument()) {
|
|
|
|
return s;
|
|
|
|
} else {
|
|
|
|
return Status::InvalidArgument(s.getState());
|
|
|
|
}
|
2017-07-28 23:23:50 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 14:45:59 +00:00
|
|
|
static int RegisterBuiltinMemTableRepFactory(ObjectLibrary& library,
|
|
|
|
const std::string& /*arg*/) {
|
|
|
|
// The MemTableRepFactory built-in classes will be either a class
|
|
|
|
// (VectorRepFactory) or a nickname (vector), followed optionally by ":#",
|
|
|
|
// where # is the "size" of the factory.
|
2021-12-29 15:55:17 +00:00
|
|
|
auto AsPattern = [](const std::string& name, const std::string& alt) {
|
|
|
|
auto pattern = ObjectLibrary::PatternEntry(name, true);
|
|
|
|
pattern.AnotherName(alt);
|
|
|
|
pattern.AddNumber(":");
|
|
|
|
return pattern;
|
2021-09-08 14:45:59 +00:00
|
|
|
};
|
2022-01-11 14:32:42 +00:00
|
|
|
library.AddFactory<MemTableRepFactory>(
|
2021-12-29 15:55:17 +00:00
|
|
|
AsPattern(VectorRepFactory::kClassName(), VectorRepFactory::kNickName()),
|
2021-09-08 14:45:59 +00:00
|
|
|
[](const std::string& uri, std::unique_ptr<MemTableRepFactory>* guard,
|
|
|
|
std::string* /*errmsg*/) {
|
2023-12-01 19:15:17 +00:00
|
|
|
auto colon = uri.find(':');
|
2021-09-08 14:45:59 +00:00
|
|
|
if (colon != std::string::npos) {
|
|
|
|
size_t count = ParseSizeT(uri.substr(colon + 1));
|
|
|
|
guard->reset(new VectorRepFactory(count));
|
|
|
|
} else {
|
|
|
|
guard->reset(new VectorRepFactory());
|
|
|
|
}
|
|
|
|
return guard->get();
|
|
|
|
});
|
2022-01-11 14:32:42 +00:00
|
|
|
library.AddFactory<MemTableRepFactory>(
|
2021-12-29 15:55:17 +00:00
|
|
|
AsPattern(SkipListFactory::kClassName(), SkipListFactory::kNickName()),
|
2021-09-08 14:45:59 +00:00
|
|
|
[](const std::string& uri, std::unique_ptr<MemTableRepFactory>* guard,
|
|
|
|
std::string* /*errmsg*/) {
|
2023-12-01 19:15:17 +00:00
|
|
|
auto colon = uri.find(':');
|
2021-09-08 14:45:59 +00:00
|
|
|
if (colon != std::string::npos) {
|
|
|
|
size_t lookahead = ParseSizeT(uri.substr(colon + 1));
|
|
|
|
guard->reset(new SkipListFactory(lookahead));
|
|
|
|
} else {
|
|
|
|
guard->reset(new SkipListFactory());
|
|
|
|
}
|
|
|
|
return guard->get();
|
|
|
|
});
|
2022-01-11 14:32:42 +00:00
|
|
|
library.AddFactory<MemTableRepFactory>(
|
2021-12-29 15:55:17 +00:00
|
|
|
AsPattern("HashLinkListRepFactory", "hash_linkedlist"),
|
2021-09-08 14:45:59 +00:00
|
|
|
[](const std::string& uri, std::unique_ptr<MemTableRepFactory>* guard,
|
|
|
|
std::string* /*errmsg*/) {
|
|
|
|
// Expecting format: hash_linkedlist:<hash_bucket_count>
|
2023-12-01 19:15:17 +00:00
|
|
|
auto colon = uri.find(':');
|
2021-09-08 14:45:59 +00:00
|
|
|
if (colon != std::string::npos) {
|
|
|
|
size_t hash_bucket_count = ParseSizeT(uri.substr(colon + 1));
|
|
|
|
guard->reset(NewHashLinkListRepFactory(hash_bucket_count));
|
|
|
|
} else {
|
|
|
|
guard->reset(NewHashLinkListRepFactory());
|
|
|
|
}
|
|
|
|
return guard->get();
|
|
|
|
});
|
2022-01-11 14:32:42 +00:00
|
|
|
library.AddFactory<MemTableRepFactory>(
|
2021-12-29 15:55:17 +00:00
|
|
|
AsPattern("HashSkipListRepFactory", "prefix_hash"),
|
2021-09-08 14:45:59 +00:00
|
|
|
[](const std::string& uri, std::unique_ptr<MemTableRepFactory>* guard,
|
|
|
|
std::string* /*errmsg*/) {
|
|
|
|
// Expecting format: prefix_hash:<hash_bucket_count>
|
2023-12-01 19:15:17 +00:00
|
|
|
auto colon = uri.find(':');
|
2021-09-08 14:45:59 +00:00
|
|
|
if (colon != std::string::npos) {
|
|
|
|
size_t hash_bucket_count = ParseSizeT(uri.substr(colon + 1));
|
|
|
|
guard->reset(NewHashSkipListRepFactory(hash_bucket_count));
|
|
|
|
} else {
|
|
|
|
guard->reset(NewHashSkipListRepFactory());
|
|
|
|
}
|
|
|
|
return guard->get();
|
|
|
|
});
|
2022-01-11 14:32:42 +00:00
|
|
|
library.AddFactory<MemTableRepFactory>(
|
2021-09-08 14:45:59 +00:00
|
|
|
"cuckoo",
|
|
|
|
[](const std::string& /*uri*/,
|
|
|
|
std::unique_ptr<MemTableRepFactory>* /*guard*/, std::string* errmsg) {
|
|
|
|
*errmsg = "cuckoo hash memtable is not supported anymore.";
|
|
|
|
return nullptr;
|
|
|
|
});
|
2017-07-28 23:23:50 +00:00
|
|
|
|
2021-12-29 15:55:17 +00:00
|
|
|
size_t num_types;
|
|
|
|
return static_cast<int>(library.GetFactoryCount(&num_types));
|
2021-09-08 14:45:59 +00:00
|
|
|
}
|
2021-12-29 15:55:17 +00:00
|
|
|
|
2021-09-08 14:45:59 +00:00
|
|
|
Status GetMemTableRepFactoryFromString(
|
|
|
|
const std::string& opts_str, std::unique_ptr<MemTableRepFactory>* result) {
|
|
|
|
ConfigOptions config_options;
|
|
|
|
config_options.ignore_unsupported_options = false;
|
|
|
|
config_options.ignore_unknown_options = false;
|
|
|
|
return MemTableRepFactory::CreateFromString(config_options, opts_str, result);
|
|
|
|
}
|
2017-07-28 23:23:50 +00:00
|
|
|
|
2021-09-08 14:45:59 +00:00
|
|
|
Status MemTableRepFactory::CreateFromString(
|
|
|
|
const ConfigOptions& config_options, const std::string& value,
|
|
|
|
std::unique_ptr<MemTableRepFactory>* result) {
|
|
|
|
static std::once_flag once;
|
|
|
|
std::call_once(once, [&]() {
|
|
|
|
RegisterBuiltinMemTableRepFactory(*(ObjectLibrary::Default().get()), "");
|
|
|
|
});
|
|
|
|
std::string id;
|
|
|
|
std::unordered_map<std::string, std::string> opt_map;
|
|
|
|
Status status = Customizable::GetOptionsMap(config_options, result->get(),
|
|
|
|
value, &id, &opt_map);
|
|
|
|
if (!status.ok()) { // GetOptionsMap failed
|
|
|
|
return status;
|
|
|
|
} else if (value.empty()) {
|
|
|
|
// No Id and no options. Clear the object
|
|
|
|
result->reset();
|
|
|
|
return Status::OK();
|
|
|
|
} else if (id.empty()) { // We have no Id but have options. Not good
|
|
|
|
return Status::NotSupported("Cannot reset object ", id);
|
2017-07-28 23:23:50 +00:00
|
|
|
} else {
|
2021-09-08 14:45:59 +00:00
|
|
|
status = NewUniqueObject<MemTableRepFactory>(config_options, id, opt_map,
|
|
|
|
result);
|
2017-07-28 23:23:50 +00:00
|
|
|
}
|
2021-09-08 14:45:59 +00:00
|
|
|
return status;
|
2017-07-28 23:23:50 +00:00
|
|
|
}
|
|
|
|
|
2022-05-16 16:44:43 +00:00
|
|
|
Status MemTableRepFactory::CreateFromString(
|
|
|
|
const ConfigOptions& config_options, const std::string& value,
|
|
|
|
std::shared_ptr<MemTableRepFactory>* result) {
|
|
|
|
std::unique_ptr<MemTableRepFactory> factory;
|
|
|
|
Status s = CreateFromString(config_options, value, &factory);
|
|
|
|
if (factory && s.ok()) {
|
|
|
|
result->reset(factory.release());
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2020-04-22 00:35:28 +00:00
|
|
|
Status GetPlainTableOptionsFromMap(
|
|
|
|
const ConfigOptions& config_options, const PlainTableOptions& table_options,
|
|
|
|
const std::unordered_map<std::string, std::string>& opts_map,
|
|
|
|
PlainTableOptions* new_table_options) {
|
2017-07-28 23:23:50 +00:00
|
|
|
assert(new_table_options);
|
2020-09-14 23:59:00 +00:00
|
|
|
PlainTableFactory ptf(table_options);
|
|
|
|
Status s = ptf.ConfigureFromMap(config_options, opts_map);
|
|
|
|
if (s.ok()) {
|
|
|
|
*new_table_options = *(ptf.GetOptions<PlainTableOptions>());
|
|
|
|
} else {
|
|
|
|
// Restore "new_options" to the default "base_options".
|
|
|
|
*new_table_options = table_options;
|
2017-07-28 23:23:50 +00:00
|
|
|
}
|
2020-09-14 23:59:00 +00:00
|
|
|
return s;
|
2017-07-28 23:23:50 +00:00
|
|
|
}
|
|
|
|
|
2024-01-29 18:38:08 +00:00
|
|
|
TableFactory* NewPlainTableFactory(const PlainTableOptions& options) {
|
2014-07-18 07:08:38 +00:00
|
|
|
return new PlainTableFactory(options);
|
2014-01-28 05:58:46 +00:00
|
|
|
}
|
|
|
|
|
2014-06-18 23:36:48 +00:00
|
|
|
const std::string PlainTablePropertyNames::kEncodingType =
|
|
|
|
"rocksdb.plain.table.encoding.type";
|
|
|
|
|
2014-07-18 23:58:13 +00:00
|
|
|
const std::string PlainTablePropertyNames::kBloomVersion =
|
|
|
|
"rocksdb.plain.table.bloom.version";
|
|
|
|
|
|
|
|
const std::string PlainTablePropertyNames::kNumBloomBlocks =
|
|
|
|
"rocksdb.plain.table.bloom.numblocks";
|
|
|
|
|
2021-09-08 14:45:59 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|