options_util.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #ifndef ROCKSDB_LITE
  6. #include "rocksdb/utilities/options_util.h"
  7. #include "env/composite_env_wrapper.h"
  8. #include "file/filename.h"
  9. #include "options/options_parser.h"
  10. #include "rocksdb/options.h"
  11. namespace ROCKSDB_NAMESPACE {
  12. Status LoadOptionsFromFile(const std::string& file_name, Env* env,
  13. DBOptions* db_options,
  14. std::vector<ColumnFamilyDescriptor>* cf_descs,
  15. bool ignore_unknown_options,
  16. std::shared_ptr<Cache>* cache) {
  17. RocksDBOptionsParser parser;
  18. LegacyFileSystemWrapper fs(env);
  19. Status s = parser.Parse(file_name, &fs, ignore_unknown_options,
  20. 0 /* file_readahead_size */);
  21. if (!s.ok()) {
  22. return s;
  23. }
  24. *db_options = *parser.db_opt();
  25. const std::vector<std::string>& cf_names = *parser.cf_names();
  26. const std::vector<ColumnFamilyOptions>& cf_opts = *parser.cf_opts();
  27. cf_descs->clear();
  28. for (size_t i = 0; i < cf_opts.size(); ++i) {
  29. cf_descs->push_back({cf_names[i], cf_opts[i]});
  30. if (cache != nullptr) {
  31. TableFactory* tf = cf_opts[i].table_factory.get();
  32. if (tf != nullptr && tf->GetOptions() != nullptr &&
  33. tf->Name() == BlockBasedTableFactory().Name()) {
  34. auto* loaded_bbt_opt =
  35. reinterpret_cast<BlockBasedTableOptions*>(tf->GetOptions());
  36. loaded_bbt_opt->block_cache = *cache;
  37. }
  38. }
  39. }
  40. return Status::OK();
  41. }
  42. Status GetLatestOptionsFileName(const std::string& dbpath,
  43. Env* env, std::string* options_file_name) {
  44. Status s;
  45. std::string latest_file_name;
  46. uint64_t latest_time_stamp = 0;
  47. std::vector<std::string> file_names;
  48. s = env->GetChildren(dbpath, &file_names);
  49. if (!s.ok()) {
  50. return s;
  51. }
  52. for (auto& file_name : file_names) {
  53. uint64_t time_stamp;
  54. FileType type;
  55. if (ParseFileName(file_name, &time_stamp, &type) && type == kOptionsFile) {
  56. if (time_stamp > latest_time_stamp) {
  57. latest_time_stamp = time_stamp;
  58. latest_file_name = file_name;
  59. }
  60. }
  61. }
  62. if (latest_file_name.size() == 0) {
  63. return Status::NotFound("No options files found in the DB directory.");
  64. }
  65. *options_file_name = latest_file_name;
  66. return Status::OK();
  67. }
  68. Status LoadLatestOptions(const std::string& dbpath, Env* env,
  69. DBOptions* db_options,
  70. std::vector<ColumnFamilyDescriptor>* cf_descs,
  71. bool ignore_unknown_options,
  72. std::shared_ptr<Cache>* cache) {
  73. std::string options_file_name;
  74. Status s = GetLatestOptionsFileName(dbpath, env, &options_file_name);
  75. if (!s.ok()) {
  76. return s;
  77. }
  78. return LoadOptionsFromFile(dbpath + "/" + options_file_name, env, db_options,
  79. cf_descs, ignore_unknown_options, cache);
  80. }
  81. Status CheckOptionsCompatibility(
  82. const std::string& dbpath, Env* env, const DBOptions& db_options,
  83. const std::vector<ColumnFamilyDescriptor>& cf_descs,
  84. bool ignore_unknown_options) {
  85. std::string options_file_name;
  86. Status s = GetLatestOptionsFileName(dbpath, env, &options_file_name);
  87. if (!s.ok()) {
  88. return s;
  89. }
  90. std::vector<std::string> cf_names;
  91. std::vector<ColumnFamilyOptions> cf_opts;
  92. for (const auto& cf_desc : cf_descs) {
  93. cf_names.push_back(cf_desc.name);
  94. cf_opts.push_back(cf_desc.options);
  95. }
  96. const OptionsSanityCheckLevel kDefaultLevel = kSanityLevelLooselyCompatible;
  97. LegacyFileSystemWrapper fs(env);
  98. return RocksDBOptionsParser::VerifyRocksDBOptionsFromFile(
  99. db_options, cf_names, cf_opts, dbpath + "/" + options_file_name, &fs,
  100. kDefaultLevel, ignore_unknown_options);
  101. }
  102. } // namespace ROCKSDB_NAMESPACE
  103. #endif // !ROCKSDB_LITE