]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/utilities/options/options_util.cc
import quincy beta 17.1.0
[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 "env/composite_env_wrapper.h"
11 #include "file/filename.h"
12 #include "options/options_parser.h"
13 #include "rocksdb/convenience.h"
14 #include "rocksdb/options.h"
15 #include "table/block_based/block_based_table_factory.h"
16
17 namespace ROCKSDB_NAMESPACE {
18 Status LoadOptionsFromFile(const std::string& file_name, Env* env,
19 DBOptions* db_options,
20 std::vector<ColumnFamilyDescriptor>* cf_descs,
21 bool ignore_unknown_options,
22 std::shared_ptr<Cache>* cache) {
23 ConfigOptions config_options;
24 config_options.ignore_unknown_options = ignore_unknown_options;
25 config_options.input_strings_escaped = true;
26 config_options.env = env;
27
28 return LoadOptionsFromFile(config_options, file_name, db_options, cf_descs,
29 cache);
30 }
31
32 Status LoadOptionsFromFile(const ConfigOptions& config_options,
33 const std::string& file_name, DBOptions* db_options,
34 std::vector<ColumnFamilyDescriptor>* cf_descs,
35 std::shared_ptr<Cache>* cache) {
36 RocksDBOptionsParser parser;
37 LegacyFileSystemWrapper fs(config_options.env);
38 Status s = parser.Parse(config_options, file_name, &fs);
39 if (!s.ok()) {
40 return s;
41 }
42 *db_options = *parser.db_opt();
43 const std::vector<std::string>& cf_names = *parser.cf_names();
44 const std::vector<ColumnFamilyOptions>& cf_opts = *parser.cf_opts();
45 cf_descs->clear();
46 for (size_t i = 0; i < cf_opts.size(); ++i) {
47 cf_descs->push_back({cf_names[i], cf_opts[i]});
48 if (cache != nullptr) {
49 TableFactory* tf = cf_opts[i].table_factory.get();
50 if (tf != nullptr) {
51 auto* opts = tf->GetOptions<BlockBasedTableOptions>();
52 if (opts != nullptr) {
53 opts->block_cache = *cache;
54 }
55 }
56 }
57 }
58 return Status::OK();
59 }
60
61 Status GetLatestOptionsFileName(const std::string& dbpath,
62 Env* env, std::string* options_file_name) {
63 Status s;
64 std::string latest_file_name;
65 uint64_t latest_time_stamp = 0;
66 std::vector<std::string> file_names;
67 s = env->GetChildren(dbpath, &file_names);
68 if (s.IsNotFound()) {
69 return Status::NotFound(Status::kPathNotFound,
70 "No options files found in the DB directory.",
71 dbpath);
72 } else if (!s.ok()) {
73 return s;
74 }
75 for (auto& file_name : file_names) {
76 uint64_t time_stamp;
77 FileType type;
78 if (ParseFileName(file_name, &time_stamp, &type) && type == kOptionsFile) {
79 if (time_stamp > latest_time_stamp) {
80 latest_time_stamp = time_stamp;
81 latest_file_name = file_name;
82 }
83 }
84 }
85 if (latest_file_name.size() == 0) {
86 return Status::NotFound(Status::kPathNotFound,
87 "No options files found in the DB directory.",
88 dbpath);
89 }
90 *options_file_name = latest_file_name;
91 return Status::OK();
92 }
93
94 Status LoadLatestOptions(const std::string& dbpath, Env* env,
95 DBOptions* db_options,
96 std::vector<ColumnFamilyDescriptor>* cf_descs,
97 bool ignore_unknown_options,
98 std::shared_ptr<Cache>* cache) {
99 ConfigOptions config_options;
100 config_options.ignore_unknown_options = ignore_unknown_options;
101 config_options.input_strings_escaped = true;
102 config_options.env = env;
103
104 return LoadLatestOptions(config_options, dbpath, db_options, cf_descs, cache);
105 }
106
107 Status LoadLatestOptions(const ConfigOptions& config_options,
108 const std::string& dbpath, DBOptions* db_options,
109 std::vector<ColumnFamilyDescriptor>* cf_descs,
110 std::shared_ptr<Cache>* cache) {
111 std::string options_file_name;
112 Status s =
113 GetLatestOptionsFileName(dbpath, config_options.env, &options_file_name);
114 if (!s.ok()) {
115 return s;
116 }
117 return LoadOptionsFromFile(config_options, dbpath + "/" + options_file_name,
118 db_options, cf_descs, cache);
119 }
120
121 Status CheckOptionsCompatibility(
122 const std::string& dbpath, Env* env, const DBOptions& db_options,
123 const std::vector<ColumnFamilyDescriptor>& cf_descs,
124 bool ignore_unknown_options) {
125 ConfigOptions config_options;
126 config_options.sanity_level = ConfigOptions::kSanityLevelLooselyCompatible;
127 config_options.ignore_unknown_options = ignore_unknown_options;
128 config_options.input_strings_escaped = true;
129 config_options.env = env;
130 return CheckOptionsCompatibility(config_options, dbpath, db_options,
131 cf_descs);
132 }
133
134 Status CheckOptionsCompatibility(
135 const ConfigOptions& config_options, const std::string& dbpath,
136 const DBOptions& db_options,
137 const std::vector<ColumnFamilyDescriptor>& cf_descs) {
138 std::string options_file_name;
139 Status s =
140 GetLatestOptionsFileName(dbpath, config_options.env, &options_file_name);
141 if (!s.ok()) {
142 return s;
143 }
144
145 std::vector<std::string> cf_names;
146 std::vector<ColumnFamilyOptions> cf_opts;
147 for (const auto& cf_desc : cf_descs) {
148 cf_names.push_back(cf_desc.name);
149 cf_opts.push_back(cf_desc.options);
150 }
151
152 LegacyFileSystemWrapper fs(config_options.env);
153
154 return RocksDBOptionsParser::VerifyRocksDBOptionsFromFile(
155
156 config_options, db_options, cf_names, cf_opts,
157 dbpath + "/" + options_file_name, &fs);
158 }
159
160 } // namespace ROCKSDB_NAMESPACE
161 #endif // !ROCKSDB_LITE