options_parser.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #pragma once
  6. #include <map>
  7. #include <string>
  8. #include <vector>
  9. #include "options/options_sanity_check.h"
  10. #include "rocksdb/env.h"
  11. #include "rocksdb/options.h"
  12. #include "table/block_based/block_based_table_factory.h"
  13. namespace ROCKSDB_NAMESPACE {
  14. #ifndef ROCKSDB_LITE
  15. #define ROCKSDB_OPTION_FILE_MAJOR 1
  16. #define ROCKSDB_OPTION_FILE_MINOR 1
  17. enum OptionSection : char {
  18. kOptionSectionVersion = 0,
  19. kOptionSectionDBOptions,
  20. kOptionSectionCFOptions,
  21. kOptionSectionTableOptions,
  22. kOptionSectionUnknown
  23. };
  24. static const std::string opt_section_titles[] = {
  25. "Version", "DBOptions", "CFOptions", "TableOptions/", "Unknown"};
  26. Status PersistRocksDBOptions(const DBOptions& db_opt,
  27. const std::vector<std::string>& cf_names,
  28. const std::vector<ColumnFamilyOptions>& cf_opts,
  29. const std::string& file_name, FileSystem* fs);
  30. extern bool AreEqualOptions(
  31. const char* opt1, const char* opt2, const OptionTypeInfo& type_info,
  32. const std::string& opt_name,
  33. const std::unordered_map<std::string, std::string>* opt_map);
  34. class RocksDBOptionsParser {
  35. public:
  36. explicit RocksDBOptionsParser();
  37. ~RocksDBOptionsParser() {}
  38. void Reset();
  39. // `file_readahead_size` is used for readahead for the option file.
  40. // If 0 is given, a default value will be used.
  41. Status Parse(const std::string& file_name, FileSystem* fs,
  42. bool ignore_unknown_options, size_t file_readahead_size);
  43. static std::string TrimAndRemoveComment(const std::string& line,
  44. const bool trim_only = false);
  45. const DBOptions* db_opt() const { return &db_opt_; }
  46. const std::unordered_map<std::string, std::string>* db_opt_map() const {
  47. return &db_opt_map_;
  48. }
  49. const std::vector<ColumnFamilyOptions>* cf_opts() const { return &cf_opts_; }
  50. const std::vector<std::string>* cf_names() const { return &cf_names_; }
  51. const std::vector<std::unordered_map<std::string, std::string>>* cf_opt_maps()
  52. const {
  53. return &cf_opt_maps_;
  54. }
  55. const ColumnFamilyOptions* GetCFOptions(const std::string& name) {
  56. return GetCFOptionsImpl(name);
  57. }
  58. size_t NumColumnFamilies() { return cf_opts_.size(); }
  59. static Status VerifyRocksDBOptionsFromFile(
  60. const DBOptions& db_opt, const std::vector<std::string>& cf_names,
  61. const std::vector<ColumnFamilyOptions>& cf_opts,
  62. const std::string& file_name, FileSystem* fs,
  63. OptionsSanityCheckLevel sanity_check_level = kSanityLevelExactMatch,
  64. bool ignore_unknown_options = false);
  65. static Status VerifyDBOptions(
  66. const DBOptions& base_opt, const DBOptions& new_opt,
  67. const std::unordered_map<std::string, std::string>* new_opt_map = nullptr,
  68. OptionsSanityCheckLevel sanity_check_level = kSanityLevelExactMatch);
  69. static Status VerifyCFOptions(
  70. const ColumnFamilyOptions& base_opt, const ColumnFamilyOptions& new_opt,
  71. const std::unordered_map<std::string, std::string>* new_opt_map = nullptr,
  72. OptionsSanityCheckLevel sanity_check_level = kSanityLevelExactMatch);
  73. static Status VerifyTableFactory(
  74. const TableFactory* base_tf, const TableFactory* file_tf,
  75. OptionsSanityCheckLevel sanity_check_level = kSanityLevelExactMatch);
  76. static Status ExtraParserCheck(const RocksDBOptionsParser& input_parser);
  77. protected:
  78. bool IsSection(const std::string& line);
  79. Status ParseSection(OptionSection* section, std::string* title,
  80. std::string* argument, const std::string& line,
  81. const int line_num);
  82. Status CheckSection(const OptionSection section,
  83. const std::string& section_arg, const int line_num);
  84. Status ParseStatement(std::string* name, std::string* value,
  85. const std::string& line, const int line_num);
  86. Status EndSection(const OptionSection section, const std::string& title,
  87. const std::string& section_arg,
  88. const std::unordered_map<std::string, std::string>& opt_map,
  89. bool ignore_unknown_options);
  90. Status ValidityCheck();
  91. Status InvalidArgument(const int line_num, const std::string& message);
  92. Status ParseVersionNumber(const std::string& ver_name,
  93. const std::string& ver_string, const int max_count,
  94. int* version);
  95. ColumnFamilyOptions* GetCFOptionsImpl(const std::string& name) {
  96. assert(cf_names_.size() == cf_opts_.size());
  97. for (size_t i = 0; i < cf_names_.size(); ++i) {
  98. if (cf_names_[i] == name) {
  99. return &cf_opts_[i];
  100. }
  101. }
  102. return nullptr;
  103. }
  104. private:
  105. DBOptions db_opt_;
  106. std::unordered_map<std::string, std::string> db_opt_map_;
  107. std::vector<std::string> cf_names_;
  108. std::vector<ColumnFamilyOptions> cf_opts_;
  109. std::vector<std::unordered_map<std::string, std::string>> cf_opt_maps_;
  110. bool has_version_section_;
  111. bool has_db_options_;
  112. bool has_default_cf_options_;
  113. int db_version[3];
  114. int opt_file_version[3];
  115. };
  116. #endif // !ROCKSDB_LITE
  117. } // namespace ROCKSDB_NAMESPACE