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).
8 #include "rocksdb/utilities/options_util.h"
10 #include "options/options_parser.h"
11 #include "rocksdb/options.h"
12 #include "util/filename.h"
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 RocksDBOptionsParser parser
;
20 Status s
= parser
.Parse(file_name
, env
, ignore_unknown_options
);
25 *db_options
= *parser
.db_opt();
27 const std::vector
<std::string
>& cf_names
= *parser
.cf_names();
28 const std::vector
<ColumnFamilyOptions
>& cf_opts
= *parser
.cf_opts();
30 for (size_t i
= 0; i
< cf_opts
.size(); ++i
) {
31 cf_descs
->push_back({cf_names
[i
], cf_opts
[i
]});
36 Status
GetLatestOptionsFileName(const std::string
& dbpath
,
37 Env
* env
, std::string
* options_file_name
) {
39 std::string latest_file_name
;
40 uint64_t latest_time_stamp
= 0;
41 std::vector
<std::string
> file_names
;
42 s
= env
->GetChildren(dbpath
, &file_names
);
46 for (auto& file_name
: file_names
) {
49 if (ParseFileName(file_name
, &time_stamp
, &type
) && type
== kOptionsFile
) {
50 if (time_stamp
> latest_time_stamp
) {
51 latest_time_stamp
= time_stamp
;
52 latest_file_name
= file_name
;
56 if (latest_file_name
.size() == 0) {
57 return Status::NotFound("No options files found in the DB directory.");
59 *options_file_name
= latest_file_name
;
63 Status
LoadLatestOptions(const std::string
& dbpath
, Env
* env
,
64 DBOptions
* db_options
,
65 std::vector
<ColumnFamilyDescriptor
>* cf_descs
,
66 bool ignore_unknown_options
) {
67 std::string options_file_name
;
68 Status s
= GetLatestOptionsFileName(dbpath
, env
, &options_file_name
);
73 return LoadOptionsFromFile(dbpath
+ "/" + options_file_name
, env
, db_options
,
74 cf_descs
, ignore_unknown_options
);
77 Status
CheckOptionsCompatibility(
78 const std::string
& dbpath
, Env
* env
, const DBOptions
& db_options
,
79 const std::vector
<ColumnFamilyDescriptor
>& cf_descs
,
80 bool ignore_unknown_options
) {
81 std::string options_file_name
;
82 Status s
= GetLatestOptionsFileName(dbpath
, env
, &options_file_name
);
87 std::vector
<std::string
> cf_names
;
88 std::vector
<ColumnFamilyOptions
> cf_opts
;
89 for (const auto& cf_desc
: cf_descs
) {
90 cf_names
.push_back(cf_desc
.name
);
91 cf_opts
.push_back(cf_desc
.options
);
94 const OptionsSanityCheckLevel kDefaultLevel
= kSanityLevelLooselyCompatible
;
96 return RocksDBOptionsParser::VerifyRocksDBOptionsFromFile(
97 db_options
, cf_names
, cf_opts
, dbpath
+ "/" + options_file_name
, env
,
98 kDefaultLevel
, ignore_unknown_options
);
101 } // namespace rocksdb
102 #endif // !ROCKSDB_LITE