From 84992d6475e990f2b5a5cd0dd11d9ec4b2239917 Mon Sep 17 00:00:00 2001 From: SherlockNoMad Date: Fri, 30 Oct 2015 15:58:46 -0700 Subject: [PATCH] Option Helper Refactoring --- table/block_based_table_factory.cc | 2 +- table/block_based_table_factory.h | 2 +- table/plain_table_factory.cc | 2 +- table/plain_table_factory.h | 2 +- util/options_helper.cc | 212 +++++++---------------------- util/options_helper.h | 30 ++++ util/options_parser.cc | 4 +- util/options_test.cc | 4 +- 8 files changed, 84 insertions(+), 174 deletions(-) diff --git a/table/block_based_table_factory.cc b/table/block_based_table_factory.cc index 1631652dd..7c9498367 100644 --- a/table/block_based_table_factory.cc +++ b/table/block_based_table_factory.cc @@ -158,7 +158,7 @@ std::string BlockBasedTableFactory::GetPrintableTableOptions() const { return ret; } -const BlockBasedTableOptions& BlockBasedTableFactory::GetTableOptions() const { +const BlockBasedTableOptions& BlockBasedTableFactory::table_options() const { return table_options_; } diff --git a/table/block_based_table_factory.h b/table/block_based_table_factory.h index 522e31c22..da108e210 100644 --- a/table/block_based_table_factory.h +++ b/table/block_based_table_factory.h @@ -56,7 +56,7 @@ class BlockBasedTableFactory : public TableFactory { std::string GetPrintableTableOptions() const override; - const BlockBasedTableOptions& GetTableOptions() const; + const BlockBasedTableOptions& table_options() const; private: BlockBasedTableOptions table_options_; diff --git a/table/plain_table_factory.cc b/table/plain_table_factory.cc index f5f3a7692..db1d8bd6f 100644 --- a/table/plain_table_factory.cc +++ b/table/plain_table_factory.cc @@ -75,7 +75,7 @@ std::string PlainTableFactory::GetPrintableTableOptions() const { return ret; } -const PlainTableOptions& PlainTableFactory::GetTableOptions() const { +const PlainTableOptions& PlainTableFactory::table_options() const { return table_options_; } diff --git a/table/plain_table_factory.h b/table/plain_table_factory.h index d21c9fd1e..41cc1a4cb 100644 --- a/table/plain_table_factory.h +++ b/table/plain_table_factory.h @@ -158,7 +158,7 @@ class PlainTableFactory : public TableFactory { std::string GetPrintableTableOptions() const override; - const PlainTableOptions& GetTableOptions() const; + const PlainTableOptions& table_options() const; static const char kValueTypeSeqId0 = 0xFF; diff --git a/util/options_helper.cc b/util/options_helper.cc index f1f04c481..36af0b90e 100644 --- a/util/options_helper.cc +++ b/util/options_helper.cc @@ -106,32 +106,28 @@ std::string trim(const std::string& str) { return std::string(); } -bool SerializeCompressionType(const CompressionType& type, std::string* value) { - switch (type) { - case kNoCompression: - *value = "kNoCompression"; - return true; - case kSnappyCompression: - *value = "kSnappyCompression"; - return true; - case kZlibCompression: - *value = "kZlibCompression"; - return true; - case kBZip2Compression: - *value = "kBZip2Compression"; - return true; - case kLZ4Compression: - *value = "kLZ4Compression"; - return true; - case kLZ4HCCompression: - *value = "kLZ4HCCompression"; - return true; - case kZSTDNotFinalCompression: - *value = "kZSTDNotFinalCompression"; - return true; - default: - return false; +template +bool ParseEnum(const std::unordered_map& type_map, + const std::string& type, T* value) { + + auto iter = type_map.find(type); + if (iter != type_map.end()) { + *value = iter->second; + return true; } + return false; +} + +template +bool SerializeEnum(const std::unordered_map& type_map, + const T& type, std::string* value) { + for (const auto& pair : type_map) { + if (pair.second == type) { + *value = pair.first; + return true; + } + } + return false; } bool SerializeVectorCompressionType(const std::vector& types, @@ -143,7 +139,8 @@ bool SerializeVectorCompressionType(const std::vector& types, ss << ':'; } std::string string_type; - result = SerializeCompressionType(types[i], &string_type); + result = SerializeEnum(compression_type_string_map, + types[i], &string_type); if (result == false) { return result; } @@ -153,101 +150,6 @@ bool SerializeVectorCompressionType(const std::vector& types, return true; } -bool ParseCompressionType(const std::string& string_value, - CompressionType* type) { - if (string_value == "kNoCompression") { - *type = kNoCompression; - } else if (string_value == "kSnappyCompression") { - *type = kSnappyCompression; - } else if (string_value == "kZlibCompression") { - *type = kZlibCompression; - } else if (string_value == "kBZip2Compression") { - *type = kBZip2Compression; - } else if (string_value == "kLZ4Compression") { - *type = kLZ4Compression; - } else if (string_value == "kLZ4HCCompression") { - *type = kLZ4HCCompression; - } else if (string_value == "kZSTDNotFinalCompression") { - *type = kZSTDNotFinalCompression; - } else { - return false; - } - return true; -} - -bool SerializeBlockBasedTableIndexType( - const BlockBasedTableOptions::IndexType& type, std::string* value) { - switch (type) { - case BlockBasedTableOptions::kBinarySearch: - *value = "kBinarySearch"; - return true; - case BlockBasedTableOptions::kHashSearch: - *value = "kHashSearch"; - return true; - default: - return false; - } -} - -bool ParseBlockBasedTableIndexType(const std::string& type, - BlockBasedTableOptions::IndexType* value) { - if (type == "kBinarySearch") { - *value = BlockBasedTableOptions::kBinarySearch; - } else if (type == "kHashSearch") { - *value = BlockBasedTableOptions::kHashSearch; - } else { - return false; - } - return true; -} - -bool SerializeEncodingType( - const EncodingType& type, std::string* value) { - switch (type) { - case EncodingType::kPlain: - *value = "kPlain"; - return true; - case EncodingType::kPrefix: - *value = "kPrefix"; - return true; - default: - return false; - } -} - -bool PraseEncodingType(const std::string& type, EncodingType* value) { - if (type == "kPlain") { - *value = EncodingType::kPlain; - } else if (type == "kPrefix") { - *value = EncodingType::kPrefix; - } else { - return false; - } - return true; -} - -static std::unordered_map checksum_type_map = { - {"kNoChecksum", kNoChecksum}, {"kCRC32c", kCRC32c}, {"kxxHash", kxxHash}}; - -bool ParseChecksumType(const std::string& type, ChecksumType* value) { - auto iter = checksum_type_map.find(type); - if (iter != checksum_type_map.end()) { - *value = iter->second; - return true; - } - return false; -} - -bool SerializeChecksumType(const ChecksumType& type, std::string* value) { - for (const auto& pair : checksum_type_map) { - if (pair.second == type) { - *value = pair.first; - return true; - } - } - return false; -} - bool ParseBoolean(const std::string& type, const std::string& value) { if (value == "true" || value == "1") { return true; @@ -325,28 +227,6 @@ double ParseDouble(const std::string& value) { return std::strtod(value.c_str(), 0); #endif } -static const std::unordered_map - compaction_style_to_string_map = { - {kCompactionStyleLevel, "kCompactionStyleLevel"}, - {kCompactionStyleUniversal, "kCompactionStyleUniversal"}, - {kCompactionStyleFIFO, "kCompactionStyleFIFO"}, - {kCompactionStyleNone, "kCompactionStyleNone"}}; - -CompactionStyle ParseCompactionStyle(const std::string& type) { - for (auto const& entry : compaction_style_to_string_map) { - if (entry.second == type) { - return static_cast(entry.first); - } - } - throw std::invalid_argument("unknown compaction style: " + type); - return kCompactionStyleLevel; -} - -std::string CompactionStyleToString(const CompactionStyle style) { - auto iter = compaction_style_to_string_map.find(style); - assert(iter != compaction_style_to_string_map.end()); - return iter->second; -} bool ParseVectorCompressionType( const std::string& value, @@ -358,14 +238,16 @@ bool ParseVectorCompressionType( bool is_ok; CompressionType type; if (end == std::string::npos) { - is_ok = ParseCompressionType(value.substr(start), &type); + is_ok = ParseEnum(compression_type_string_map, + value.substr(start), &type); if (!is_ok) { return false; } compression_per_level->emplace_back(type); break; } else { - is_ok = ParseCompressionType(value.substr(start, end - start), &type); + is_ok = ParseEnum(compression_type_string_map, + value.substr(start, end - start), &type); if (!is_ok) { return false; } @@ -454,11 +336,10 @@ bool ParseOptionHelper(char* opt_address, const OptionType& opt_type, *reinterpret_cast(opt_address) = ParseDouble(value); break; case OptionType::kCompactionStyle: - *reinterpret_cast(opt_address) = - ParseCompactionStyle(value); - break; + return ParseEnum(compaction_style_string_map, + value, reinterpret_cast(opt_address)); case OptionType::kCompressionType: - return ParseCompressionType( + return ParseEnum(compression_type_string_map, value, reinterpret_cast(opt_address)); case OptionType::kVectorCompressionType: return ParseVectorCompressionType( @@ -468,16 +349,15 @@ bool ParseOptionHelper(char* opt_address, const OptionType& opt_type, value, reinterpret_cast*>( opt_address)); case OptionType::kChecksumType: - return ParseChecksumType(value, + return ParseEnum(checksum_type_string_map, value, reinterpret_cast(opt_address)); case OptionType::kBlockBasedTableIndexType: - return ParseBlockBasedTableIndexType( - value, - reinterpret_cast(opt_address)); + return ParseEnum( + block_base_table_index_type_string_map, value, + reinterpret_cast(opt_address)); case OptionType::kEncodingType: - return PraseEncodingType( - value, - reinterpret_cast(opt_address)); + return ParseEnum(encoding_type_string_map, value, + reinterpret_cast(opt_address)); default: return false; } @@ -518,11 +398,10 @@ bool SerializeSingleOptionHelper(const char* opt_address, *(reinterpret_cast(opt_address))); break; case OptionType::kCompactionStyle: - *value = CompactionStyleToString( - *(reinterpret_cast(opt_address))); - break; + return SerializeEnum(compaction_style_string_map, + *(reinterpret_cast(opt_address)), value); case OptionType::kCompressionType: - return SerializeCompressionType( + return SerializeEnum(compression_type_string_map, *(reinterpret_cast(opt_address)), value); case OptionType::kVectorCompressionType: return SerializeVectorCompressionType( @@ -585,10 +464,11 @@ bool SerializeSingleOptionHelper(const char* opt_address, break; } case OptionType::kChecksumType: - return SerializeChecksumType( + return SerializeEnum(checksum_type_string_map, *reinterpret_cast(opt_address), value); case OptionType::kBlockBasedTableIndexType: - return SerializeBlockBasedTableIndexType( + return SerializeEnum( + block_base_table_index_type_string_map, *reinterpret_cast( opt_address), value); @@ -600,7 +480,7 @@ bool SerializeSingleOptionHelper(const char* opt_address, break; } case OptionType::kEncodingType: - return SerializeEncodingType( + return SerializeEnum(encoding_type_string_map, *reinterpret_cast(opt_address), value); default: return false; @@ -832,7 +712,7 @@ bool ParseColumnFamilyOption(const std::string& name, auto block_based_table_factory = dynamic_cast( new_options->table_factory.get()); if (block_based_table_factory != nullptr) { - base_table_options = block_based_table_factory->GetTableOptions(); + base_table_options = block_based_table_factory->table_options(); } Status table_opt_s = GetBlockBasedTableOptionsFromString( base_table_options, value, &table_opt); @@ -846,7 +726,7 @@ bool ParseColumnFamilyOption(const std::string& name, auto plain_table_factory = dynamic_cast( new_options->table_factory.get()); if (plain_table_factory != nullptr) { - base_table_options = plain_table_factory->GetTableOptions(); + base_table_options = plain_table_factory->table_options(); } Status table_opt_s = GetPlainTableOptionsFromString( base_table_options, value, &table_opt); @@ -1031,7 +911,7 @@ Status GetStringFromTableFactory(std::string* opts_str, const TableFactory* tf, opts_str->clear(); if (bbtf != nullptr) { return GetStringFromBlockBasedTableOptions( - opts_str, bbtf->GetTableOptions(), delimiter); + opts_str, bbtf->table_options(), delimiter); } return Status::OK(); diff --git a/util/options_helper.h b/util/options_helper.h index 5c7277f46..ee3f82a30 100644 --- a/util/options_helper.h +++ b/util/options_helper.h @@ -491,6 +491,36 @@ static std::unordered_map + compression_type_string_map = { + {"kNoCompression", kNoCompression}, + {"kSnappyCompression", kSnappyCompression}, + {"kZlibCompression", kZlibCompression}, + {"kBZip2Compression", kBZip2Compression}, + {"kLZ4Compression", kLZ4Compression}, + {"kLZ4HCCompression", kLZ4HCCompression}, + {"kZSTDNotFinalCompression", kZSTDNotFinalCompression} +}; + +static std::unordered_map + block_base_table_index_type_string_map = { + {"kBinarySearch", BlockBasedTableOptions::IndexType::kBinarySearch}, + {"kHashSearch", BlockBasedTableOptions::IndexType::kHashSearch}}; + +static std::unordered_map + encoding_type_string_map = {{"kPlain", kPlain}, {"kPrefix", kPrefix}}; + +static std::unordered_map + checksum_type_string_map = { + {"kNoChecksum", kNoChecksum}, {"kCRC32c", kCRC32c}, {"kxxHash", kxxHash}}; + +static std::unordered_map + compaction_style_string_map = { + {"kCompactionStyleLevel", kCompactionStyleLevel}, + {"kCompactionStyleUniversal", kCompactionStyleUniversal}, + {"kCompactionStyleFIFO", kCompactionStyleFIFO}, + {"kCompactionStyleNone", kCompactionStyleNone}}; + } // namespace rocksdb #endif // !ROCKSDB_LITE diff --git a/util/options_parser.cc b/util/options_parser.cc index c8439c6fd..2af24533e 100644 --- a/util/options_parser.cc +++ b/util/options_parser.cc @@ -685,8 +685,8 @@ Status RocksDBOptionsParser::VerifyBlockBasedTableFactory( return Status::OK(); } - const auto& base_opt = base_tf->GetTableOptions(); - const auto& file_opt = file_tf->GetTableOptions(); + const auto& base_opt = base_tf->table_options(); + const auto& file_opt = file_tf->table_options(); for (auto& pair : block_based_table_type_info) { if (pair.second.verification == OptionVerificationType::kDeprecated) { diff --git a/util/options_test.cc b/util/options_test.cc index 1cabce6c1..1fcb1e74e 100644 --- a/util/options_test.cc +++ b/util/options_test.cc @@ -720,7 +720,7 @@ TEST_F(OptionsTest, GetOptionsFromStringTest) { ASSERT_EQ(new_options.max_write_buffer_number, 16); BlockBasedTableOptions new_block_based_table_options = dynamic_cast(new_options.table_factory.get()) - ->GetTableOptions(); + ->table_options(); ASSERT_EQ(new_block_based_table_options.block_cache->GetCapacity(), 1U << 20); ASSERT_EQ(new_block_based_table_options.block_size, 4U); // don't overwrite block based table options @@ -1221,7 +1221,7 @@ TEST_F(OptionsTest, ConvertOptionsTest) { ASSERT_TRUE(table_factory.get() != nullptr); - const BlockBasedTableOptions table_opt = table_factory->GetTableOptions(); + const BlockBasedTableOptions table_opt = table_factory->table_options(); ASSERT_EQ(table_opt.block_cache->GetCapacity(), 8UL << 20); ASSERT_EQ(table_opt.block_size, leveldb_opt.block_size);