/Users/deen/code/yugabyte-db/src/yb/rocksdb/utilities/options/options_util.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | | // This source code is licensed under the BSD-style license found in the |
3 | | // LICENSE file in the root directory of this source tree. An additional grant |
4 | | // of patent rights can be found in the PATENTS file in the same directory. |
5 | | // |
6 | | // The following only applies to changes made to this file as part of YugaByte development. |
7 | | // |
8 | | // Portions Copyright (c) YugaByte, Inc. |
9 | | // |
10 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
11 | | // in compliance with the License. You may obtain a copy of the License at |
12 | | // |
13 | | // http://www.apache.org/licenses/LICENSE-2.0 |
14 | | // |
15 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
16 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
17 | | // or implied. See the License for the specific language governing permissions and limitations |
18 | | // under the License. |
19 | | // |
20 | | |
21 | | #ifndef ROCKSDB_LITE |
22 | | |
23 | | #include "yb/rocksdb/db.h" |
24 | | #include "yb/rocksdb/db/filename.h" |
25 | | #include "yb/rocksdb/options.h" |
26 | | #include "yb/rocksdb/util/options_parser.h" |
27 | | |
28 | | namespace rocksdb { |
29 | | Status LoadOptionsFromFile(const std::string& file_name, Env* env, |
30 | | DBOptions* db_options, |
31 | 5 | std::vector<ColumnFamilyDescriptor>* cf_descs) { |
32 | 5 | RocksDBOptionsParser parser; |
33 | 5 | Status s = parser.Parse(file_name, env); |
34 | 5 | if (!s.ok()) { |
35 | 0 | return s; |
36 | 0 | } |
37 | | |
38 | 5 | *db_options = *parser.db_opt(); |
39 | | |
40 | 5 | const std::vector<std::string>& cf_names = *parser.cf_names(); |
41 | 5 | const std::vector<ColumnFamilyOptions>& cf_opts = *parser.cf_opts(); |
42 | 5 | cf_descs->clear(); |
43 | 30 | for (size_t i = 0; i < cf_opts.size(); ++i) { |
44 | 25 | cf_descs->push_back({cf_names[i], cf_opts[i]}); |
45 | 25 | } |
46 | 5 | return Status::OK(); |
47 | 5 | } |
48 | | |
49 | | Status GetLatestOptionsFileName(const std::string& dbpath, |
50 | 14 | Env* env, std::string* options_file_name) { |
51 | 14 | Status s; |
52 | 14 | std::string latest_file_name; |
53 | 14 | uint64_t latest_time_stamp = 0; |
54 | 14 | std::vector<std::string> file_names; |
55 | 14 | s = env->GetChildren(dbpath, &file_names); |
56 | 14 | if (!s.ok()) { |
57 | 0 | return s; |
58 | 0 | } |
59 | 140 | for (auto& file_name : file_names) { |
60 | 140 | uint64_t time_stamp; |
61 | 140 | FileType type; |
62 | 140 | if (ParseFileName(file_name, &time_stamp, &type) && type == kOptionsFile) { |
63 | 28 | if (time_stamp > latest_time_stamp) { |
64 | 28 | latest_time_stamp = time_stamp; |
65 | 28 | latest_file_name = file_name; |
66 | 28 | } |
67 | 28 | } |
68 | 140 | } |
69 | 14 | if (latest_file_name.size() == 0) { |
70 | 0 | return STATUS(NotFound, "No options files found in the DB directory."); |
71 | 0 | } |
72 | 14 | *options_file_name = latest_file_name; |
73 | 14 | return Status::OK(); |
74 | 14 | } |
75 | | |
76 | | Status LoadLatestOptions(const std::string& dbpath, Env* env, |
77 | | DBOptions* db_options, |
78 | 0 | std::vector<ColumnFamilyDescriptor>* cf_descs) { |
79 | 0 | std::string options_file_name; |
80 | 0 | Status s = GetLatestOptionsFileName(dbpath, env, &options_file_name); |
81 | 0 | if (!s.ok()) { |
82 | 0 | return s; |
83 | 0 | } |
84 | | |
85 | 0 | return LoadOptionsFromFile(dbpath + "/" + options_file_name, env, |
86 | 0 | db_options, cf_descs); |
87 | 0 | } |
88 | | |
89 | | Status CheckOptionsCompatibility( |
90 | | const std::string& dbpath, Env* env, const DBOptions& db_options, |
91 | 14 | const std::vector<ColumnFamilyDescriptor>& cf_descs) { |
92 | 14 | std::string options_file_name; |
93 | 14 | Status s = GetLatestOptionsFileName(dbpath, env, &options_file_name); |
94 | 14 | if (!s.ok()) { |
95 | 0 | return s; |
96 | 0 | } |
97 | | |
98 | 14 | std::vector<std::string> cf_names; |
99 | 14 | std::vector<ColumnFamilyOptions> cf_opts; |
100 | 70 | for (const auto& cf_desc : cf_descs) { |
101 | 70 | cf_names.push_back(cf_desc.name); |
102 | 70 | cf_opts.push_back(cf_desc.options); |
103 | 70 | } |
104 | | |
105 | 14 | const OptionsSanityCheckLevel kDefaultLevel = kSanityLevelLooselyCompatible; |
106 | | |
107 | 14 | return RocksDBOptionsParser::VerifyRocksDBOptionsFromFile( |
108 | 14 | db_options, cf_names, cf_opts, dbpath + "/" + options_file_name, env, |
109 | 14 | kDefaultLevel); |
110 | 14 | } |
111 | | |
112 | | } // namespace rocksdb |
113 | | #endif // !ROCKSDB_LITE |