]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
11fdf7f2 TL |
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). | |
7c673cae FG |
5 | |
6 | #ifndef ROCKSDB_LITE | |
7 | ||
8 | #include "rocksdb/utilities/options_util.h" | |
9 | ||
f67539c2 TL |
10 | #include "env/composite_env_wrapper.h" |
11 | #include "file/filename.h" | |
7c673cae | 12 | #include "options/options_parser.h" |
20effc67 | 13 | #include "rocksdb/convenience.h" |
7c673cae | 14 | #include "rocksdb/options.h" |
20effc67 | 15 | #include "table/block_based/block_based_table_factory.h" |
7c673cae | 16 | |
f67539c2 | 17 | namespace ROCKSDB_NAMESPACE { |
7c673cae FG |
18 | Status LoadOptionsFromFile(const std::string& file_name, Env* env, |
19 | DBOptions* db_options, | |
11fdf7f2 | 20 | std::vector<ColumnFamilyDescriptor>* cf_descs, |
494da23a TL |
21 | bool ignore_unknown_options, |
22 | std::shared_ptr<Cache>* cache) { | |
20effc67 TL |
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) { | |
7c673cae | 36 | RocksDBOptionsParser parser; |
20effc67 TL |
37 | LegacyFileSystemWrapper fs(config_options.env); |
38 | Status s = parser.Parse(config_options, file_name, &fs); | |
7c673cae FG |
39 | if (!s.ok()) { |
40 | return s; | |
41 | } | |
7c673cae | 42 | *db_options = *parser.db_opt(); |
7c673cae FG |
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]}); | |
494da23a TL |
48 | if (cache != nullptr) { |
49 | TableFactory* tf = cf_opts[i].table_factory.get(); | |
20effc67 TL |
50 | if (tf != nullptr) { |
51 | auto* opts = tf->GetOptions<BlockBasedTableOptions>(); | |
52 | if (opts != nullptr) { | |
53 | opts->block_cache = *cache; | |
54 | } | |
494da23a TL |
55 | } |
56 | } | |
7c673cae FG |
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); | |
20effc67 TL |
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()) { | |
7c673cae FG |
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) { | |
20effc67 TL |
86 | return Status::NotFound(Status::kPathNotFound, |
87 | "No options files found in the DB directory.", | |
88 | dbpath); | |
7c673cae FG |
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, | |
11fdf7f2 | 96 | std::vector<ColumnFamilyDescriptor>* cf_descs, |
494da23a TL |
97 | bool ignore_unknown_options, |
98 | std::shared_ptr<Cache>* cache) { | |
20effc67 TL |
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) { | |
7c673cae | 111 | std::string options_file_name; |
20effc67 TL |
112 | Status s = |
113 | GetLatestOptionsFileName(dbpath, config_options.env, &options_file_name); | |
7c673cae FG |
114 | if (!s.ok()) { |
115 | return s; | |
116 | } | |
20effc67 TL |
117 | return LoadOptionsFromFile(config_options, dbpath + "/" + options_file_name, |
118 | db_options, cf_descs, cache); | |
7c673cae FG |
119 | } |
120 | ||
121 | Status CheckOptionsCompatibility( | |
122 | const std::string& dbpath, Env* env, const DBOptions& db_options, | |
11fdf7f2 TL |
123 | const std::vector<ColumnFamilyDescriptor>& cf_descs, |
124 | bool ignore_unknown_options) { | |
20effc67 TL |
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) { | |
7c673cae | 138 | std::string options_file_name; |
20effc67 TL |
139 | Status s = |
140 | GetLatestOptionsFileName(dbpath, config_options.env, &options_file_name); | |
7c673cae FG |
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 | ||
20effc67 | 152 | LegacyFileSystemWrapper fs(config_options.env); |
7c673cae FG |
153 | |
154 | return RocksDBOptionsParser::VerifyRocksDBOptionsFromFile( | |
20effc67 TL |
155 | |
156 | config_options, db_options, cf_names, cf_opts, | |
157 | dbpath + "/" + options_file_name, &fs); | |
7c673cae FG |
158 | } |
159 | ||
f67539c2 | 160 | } // namespace ROCKSDB_NAMESPACE |
7c673cae | 161 | #endif // !ROCKSDB_LITE |