]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/utilities/options/options_util.cc
import 14.2.4 nautilus point release
[ceph.git] / ceph / src / rocksdb / utilities / options / options_util.cc
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
6 #ifndef ROCKSDB_LITE
7
8 #include "rocksdb/utilities/options_util.h"
9
10 #include "options/options_parser.h"
11 #include "rocksdb/options.h"
12 #include "util/filename.h"
13
14 namespace rocksdb {
15 Status LoadOptionsFromFile(const std::string& file_name, Env* env,
16 DBOptions* db_options,
17 std::vector<ColumnFamilyDescriptor>* cf_descs,
18 bool ignore_unknown_options,
19 std::shared_ptr<Cache>* cache) {
20 RocksDBOptionsParser parser;
21 Status s = parser.Parse(file_name, env, ignore_unknown_options);
22 if (!s.ok()) {
23 return s;
24 }
25 *db_options = *parser.db_opt();
26 const std::vector<std::string>& cf_names = *parser.cf_names();
27 const std::vector<ColumnFamilyOptions>& cf_opts = *parser.cf_opts();
28 cf_descs->clear();
29 for (size_t i = 0; i < cf_opts.size(); ++i) {
30 cf_descs->push_back({cf_names[i], cf_opts[i]});
31 if (cache != nullptr) {
32 TableFactory* tf = cf_opts[i].table_factory.get();
33 if (tf != nullptr && tf->GetOptions() != nullptr &&
34 tf->Name() == BlockBasedTableFactory().Name()) {
35 auto* loaded_bbt_opt =
36 reinterpret_cast<BlockBasedTableOptions*>(tf->GetOptions());
37 loaded_bbt_opt->block_cache = *cache;
38 }
39 }
40 }
41 return Status::OK();
42 }
43
44 Status GetLatestOptionsFileName(const std::string& dbpath,
45 Env* env, std::string* options_file_name) {
46 Status s;
47 std::string latest_file_name;
48 uint64_t latest_time_stamp = 0;
49 std::vector<std::string> file_names;
50 s = env->GetChildren(dbpath, &file_names);
51 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("No options files found in the DB directory.");
66 }
67 *options_file_name = latest_file_name;
68 return Status::OK();
69 }
70
71 Status LoadLatestOptions(const std::string& dbpath, Env* env,
72 DBOptions* db_options,
73 std::vector<ColumnFamilyDescriptor>* cf_descs,
74 bool ignore_unknown_options,
75 std::shared_ptr<Cache>* cache) {
76 std::string options_file_name;
77 Status s = GetLatestOptionsFileName(dbpath, env, &options_file_name);
78 if (!s.ok()) {
79 return s;
80 }
81 return LoadOptionsFromFile(dbpath + "/" + options_file_name, env, db_options,
82 cf_descs, ignore_unknown_options, cache);
83 }
84
85 Status CheckOptionsCompatibility(
86 const std::string& dbpath, Env* env, const DBOptions& db_options,
87 const std::vector<ColumnFamilyDescriptor>& cf_descs,
88 bool ignore_unknown_options) {
89 std::string options_file_name;
90 Status s = GetLatestOptionsFileName(dbpath, env, &options_file_name);
91 if (!s.ok()) {
92 return s;
93 }
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
102 const OptionsSanityCheckLevel kDefaultLevel = kSanityLevelLooselyCompatible;
103
104 return RocksDBOptionsParser::VerifyRocksDBOptionsFromFile(
105 db_options, cf_names, cf_opts, dbpath + "/" + options_file_name, env,
106 kDefaultLevel, ignore_unknown_options);
107 }
108
109 } // namespace rocksdb
110 #endif // !ROCKSDB_LITE