Merge pull request #805 from SherlockNoMad/OptionHelperFix

Option Helper Refactoring
main
Siying Dong 9 years ago
commit 3a6643c2fd
  1. 2
      table/block_based_table_factory.cc
  2. 2
      table/block_based_table_factory.h
  3. 2
      table/plain_table_factory.cc
  4. 2
      table/plain_table_factory.h
  5. 210
      util/options_helper.cc
  6. 30
      util/options_helper.h
  7. 4
      util/options_parser.cc
  8. 4
      util/options_test.cc

@ -162,7 +162,7 @@ std::string BlockBasedTableFactory::GetPrintableTableOptions() const {
return ret;
}
const BlockBasedTableOptions& BlockBasedTableFactory::GetTableOptions() const {
const BlockBasedTableOptions& BlockBasedTableFactory::table_options() const {
return table_options_;
}

@ -56,7 +56,7 @@ class BlockBasedTableFactory : public TableFactory {
std::string GetPrintableTableOptions() const override;
const BlockBasedTableOptions& GetTableOptions() const;
const BlockBasedTableOptions& table_options() const;
void* GetOptions() override { return &table_options_; }

@ -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_;
}

@ -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 = char(0xFF);

@ -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";
template<typename T>
bool ParseEnum(const std::unordered_map<std::string,T>& 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<typename T>
bool SerializeEnum(const std::unordered_map<std::string,T>& type_map,
const T& type, std::string* value) {
for (const auto& pair : type_map) {
if (pair.second == type) {
*value = pair.first;
return true;
default:
return false;
}
}
return false;
}
bool SerializeVectorCompressionType(const std::vector<CompressionType>& types,
@ -143,7 +139,8 @@ bool SerializeVectorCompressionType(const std::vector<CompressionType>& types,
ss << ':';
}
std::string string_type;
result = SerializeCompressionType(types[i], &string_type);
result = SerializeEnum<CompressionType>(compression_type_string_map,
types[i], &string_type);
if (result == false) {
return result;
}
@ -153,101 +150,6 @@ bool SerializeVectorCompressionType(const std::vector<CompressionType>& 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<std::string, ChecksumType> 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<char, std::string>
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<CompactionStyle>(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<CompressionType>(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<CompressionType>(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<double*>(opt_address) = ParseDouble(value);
break;
case OptionType::kCompactionStyle:
*reinterpret_cast<CompactionStyle*>(opt_address) =
ParseCompactionStyle(value);
break;
return ParseEnum<CompactionStyle>(compaction_style_string_map,
value, reinterpret_cast<CompactionStyle*>(opt_address));
case OptionType::kCompressionType:
return ParseCompressionType(
return ParseEnum<CompressionType>(compression_type_string_map,
value, reinterpret_cast<CompressionType*>(opt_address));
case OptionType::kVectorCompressionType:
return ParseVectorCompressionType(
@ -468,16 +349,15 @@ bool ParseOptionHelper(char* opt_address, const OptionType& opt_type,
value, reinterpret_cast<std::shared_ptr<const SliceTransform>*>(
opt_address));
case OptionType::kChecksumType:
return ParseChecksumType(value,
return ParseEnum<ChecksumType>(checksum_type_string_map, value,
reinterpret_cast<ChecksumType*>(opt_address));
case OptionType::kBlockBasedTableIndexType:
return ParseBlockBasedTableIndexType(
value,
reinterpret_cast<BlockBasedTableOptions::IndexType*>(opt_address));
return ParseEnum<BlockBasedTableOptions::IndexType>(
block_base_table_index_type_string_map, value,
reinterpret_cast<BlockBasedTableOptions::IndexType*>(opt_address));
case OptionType::kEncodingType:
return PraseEncodingType(
value,
reinterpret_cast<EncodingType*>(opt_address));
return ParseEnum<EncodingType>(encoding_type_string_map, value,
reinterpret_cast<EncodingType*>(opt_address));
default:
return false;
}
@ -518,11 +398,10 @@ bool SerializeSingleOptionHelper(const char* opt_address,
*(reinterpret_cast<const std::string*>(opt_address)));
break;
case OptionType::kCompactionStyle:
*value = CompactionStyleToString(
*(reinterpret_cast<const CompactionStyle*>(opt_address)));
break;
return SerializeEnum<CompactionStyle>(compaction_style_string_map,
*(reinterpret_cast<const CompactionStyle*>(opt_address)), value);
case OptionType::kCompressionType:
return SerializeCompressionType(
return SerializeEnum<CompressionType>(compression_type_string_map,
*(reinterpret_cast<const CompressionType*>(opt_address)), value);
case OptionType::kVectorCompressionType:
return SerializeVectorCompressionType(
@ -594,10 +473,11 @@ bool SerializeSingleOptionHelper(const char* opt_address,
break;
}
case OptionType::kChecksumType:
return SerializeChecksumType(
return SerializeEnum<ChecksumType>(checksum_type_string_map,
*reinterpret_cast<const ChecksumType*>(opt_address), value);
case OptionType::kBlockBasedTableIndexType:
return SerializeBlockBasedTableIndexType(
return SerializeEnum<BlockBasedTableOptions::IndexType>(
block_base_table_index_type_string_map,
*reinterpret_cast<const BlockBasedTableOptions::IndexType*>(
opt_address),
value);
@ -609,7 +489,7 @@ bool SerializeSingleOptionHelper(const char* opt_address,
break;
}
case OptionType::kEncodingType:
return SerializeEncodingType(
return SerializeEnum<EncodingType>(encoding_type_string_map,
*reinterpret_cast<const EncodingType*>(opt_address), value);
default:
return false;
@ -841,7 +721,7 @@ Status ParseColumnFamilyOption(const std::string& name,
auto block_based_table_factory = dynamic_cast<BlockBasedTableFactory*>(
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);
@ -856,7 +736,7 @@ Status ParseColumnFamilyOption(const std::string& name,
auto plain_table_factory = dynamic_cast<PlainTableFactory*>(
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);
@ -1057,7 +937,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();

@ -520,6 +520,36 @@ static std::unordered_map<std::string,
{offsetof(struct PlainTableOptions, store_index_in_file),
OptionType::kBoolean, OptionVerificationType::kNormal}}};
static std::unordered_map<std::string, CompressionType>
compression_type_string_map = {
{"kNoCompression", kNoCompression},
{"kSnappyCompression", kSnappyCompression},
{"kZlibCompression", kZlibCompression},
{"kBZip2Compression", kBZip2Compression},
{"kLZ4Compression", kLZ4Compression},
{"kLZ4HCCompression", kLZ4HCCompression},
{"kZSTDNotFinalCompression", kZSTDNotFinalCompression}
};
static std::unordered_map<std::string, BlockBasedTableOptions::IndexType>
block_base_table_index_type_string_map = {
{"kBinarySearch", BlockBasedTableOptions::IndexType::kBinarySearch},
{"kHashSearch", BlockBasedTableOptions::IndexType::kHashSearch}};
static std::unordered_map<std::string, EncodingType>
encoding_type_string_map = {{"kPlain", kPlain}, {"kPrefix", kPrefix}};
static std::unordered_map<std::string, ChecksumType>
checksum_type_string_map = {
{"kNoChecksum", kNoChecksum}, {"kCRC32c", kCRC32c}, {"kxxHash", kxxHash}};
static std::unordered_map<std::string, CompactionStyle>
compaction_style_string_map = {
{"kCompactionStyleLevel", kCompactionStyleLevel},
{"kCompactionStyleUniversal", kCompactionStyleUniversal},
{"kCompactionStyleFIFO", kCompactionStyleFIFO},
{"kCompactionStyleNone", kCompactionStyleNone}};
} // namespace rocksdb
#endif // !ROCKSDB_LITE

@ -738,8 +738,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) {

@ -563,7 +563,7 @@ TEST_F(OptionsTest, GetOptionsFromStringTest) {
ASSERT_EQ(new_options.max_write_buffer_number, 16);
BlockBasedTableOptions new_block_based_table_options =
dynamic_cast<BlockBasedTableFactory*>(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
@ -805,7 +805,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);

Loading…
Cancel
Save