options_util.cc 4.0 KB

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