options_parser.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "rocksdb/env.h"
  10. #include "rocksdb/options.h"
  11. namespace ROCKSDB_NAMESPACE {
  12. struct ConfigOptions;
  13. class OptionTypeInfo;
  14. class TableFactory;
  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 WriteOptions& write_options,
  27. const DBOptions& db_opt,
  28. const std::vector<std::string>& cf_names,
  29. const std::vector<ColumnFamilyOptions>& cf_opts,
  30. const std::string& file_name, FileSystem* fs);
  31. Status PersistRocksDBOptions(const WriteOptions& write_options,
  32. const ConfigOptions& config_options,
  33. const DBOptions& db_opt,
  34. const std::vector<std::string>& cf_names,
  35. const std::vector<ColumnFamilyOptions>& cf_opts,
  36. const std::string& file_name, FileSystem* fs);
  37. class RocksDBOptionsParser {
  38. public:
  39. explicit RocksDBOptionsParser();
  40. ~RocksDBOptionsParser() {}
  41. void Reset();
  42. // `file_readahead_size` is used for readahead for the option file.
  43. // If 0 is given, a default value will be used.
  44. Status Parse(const std::string& file_name, FileSystem* fs,
  45. bool ignore_unknown_options, size_t file_readahead_size);
  46. Status Parse(const ConfigOptions& config_options,
  47. const std::string& file_name, FileSystem* fs);
  48. static std::string TrimAndRemoveComment(const std::string& line,
  49. const bool trim_only = false);
  50. const DBOptions* db_opt() const { return &db_opt_; }
  51. const std::unordered_map<std::string, std::string>* db_opt_map() const {
  52. return &db_opt_map_;
  53. }
  54. const std::vector<ColumnFamilyOptions>* cf_opts() const { return &cf_opts_; }
  55. const std::vector<std::string>* cf_names() const { return &cf_names_; }
  56. const std::vector<std::unordered_map<std::string, std::string>>* cf_opt_maps()
  57. const {
  58. return &cf_opt_maps_;
  59. }
  60. const ColumnFamilyOptions* GetCFOptions(const std::string& name) {
  61. return GetCFOptionsImpl(name);
  62. }
  63. size_t NumColumnFamilies() { return cf_opts_.size(); }
  64. static Status VerifyRocksDBOptionsFromFile(
  65. const ConfigOptions& config_options, const DBOptions& db_opt,
  66. const std::vector<std::string>& cf_names,
  67. const std::vector<ColumnFamilyOptions>& cf_opts,
  68. const std::string& file_name, FileSystem* fs);
  69. static Status VerifyDBOptions(
  70. const ConfigOptions& config_options, const DBOptions& base_opt,
  71. const DBOptions& new_opt,
  72. const std::unordered_map<std::string, std::string>* new_opt_map =
  73. nullptr);
  74. static Status VerifyCFOptions(
  75. const ConfigOptions& config_options, const ColumnFamilyOptions& base_opt,
  76. const ColumnFamilyOptions& new_opt,
  77. const std::unordered_map<std::string, std::string>* new_opt_map =
  78. nullptr);
  79. static Status VerifyTableFactory(const ConfigOptions& config_options,
  80. const TableFactory* base_tf,
  81. const TableFactory* file_tf);
  82. static Status ExtraParserCheck(const RocksDBOptionsParser& input_parser);
  83. static Status ParseStatement(std::string* name, std::string* value,
  84. const std::string& line, const int line_num);
  85. protected:
  86. bool IsSection(const std::string& line);
  87. Status ParseSection(OptionSection* section, std::string* title,
  88. std::string* argument, const std::string& line,
  89. const int line_num);
  90. Status CheckSection(const OptionSection section,
  91. const std::string& section_arg, const int line_num);
  92. Status EndSection(
  93. const ConfigOptions& config_options, const OptionSection section,
  94. const std::string& title, const std::string& section_arg,
  95. const std::unordered_map<std::string, std::string>& opt_map);
  96. Status ValidityCheck();
  97. static Status InvalidArgument(const int line_num, const std::string& message);
  98. Status ParseVersionNumber(const std::string& ver_name,
  99. const std::string& ver_string, const int max_count,
  100. int* version);
  101. ColumnFamilyOptions* GetCFOptionsImpl(const std::string& name) {
  102. assert(cf_names_.size() == cf_opts_.size());
  103. for (size_t i = 0; i < cf_names_.size(); ++i) {
  104. if (cf_names_[i] == name) {
  105. return &cf_opts_[i];
  106. }
  107. }
  108. return nullptr;
  109. }
  110. private:
  111. DBOptions db_opt_;
  112. std::unordered_map<std::string, std::string> db_opt_map_;
  113. std::vector<std::string> cf_names_;
  114. std::vector<ColumnFamilyOptions> cf_opts_;
  115. std::vector<std::unordered_map<std::string, std::string>> cf_opt_maps_;
  116. bool has_version_section_;
  117. bool has_db_options_;
  118. bool has_default_cf_options_;
  119. int db_version[3];
  120. int opt_file_version[3];
  121. };
  122. } // namespace ROCKSDB_NAMESPACE