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 "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"
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
;
28 return LoadOptionsFromFile(config_options
, file_name
, db_options
, cf_descs
,
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
);
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();
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();
51 auto* opts
= tf
->GetOptions
<BlockBasedTableOptions
>();
52 if (opts
!= nullptr) {
53 opts
->block_cache
= *cache
;
61 Status
GetLatestOptionsFileName(const std::string
& dbpath
,
62 Env
* env
, std::string
* options_file_name
) {
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
);
69 return Status::NotFound(Status::kPathNotFound
,
70 "No options files found in the DB directory.",
75 for (auto& file_name
: file_names
) {
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
;
85 if (latest_file_name
.size() == 0) {
86 return Status::NotFound(Status::kPathNotFound
,
87 "No options files found in the DB directory.",
90 *options_file_name
= latest_file_name
;
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
;
104 return LoadLatestOptions(config_options
, dbpath
, db_options
, cf_descs
, cache
);
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
;
113 GetLatestOptionsFileName(dbpath
, config_options
.env
, &options_file_name
);
117 return LoadOptionsFromFile(config_options
, dbpath
+ "/" + options_file_name
,
118 db_options
, cf_descs
, cache
);
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
,
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
;
140 GetLatestOptionsFileName(dbpath
, config_options
.env
, &options_file_name
);
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
);
152 LegacyFileSystemWrapper
fs(config_options
.env
);
154 return RocksDBOptionsParser::VerifyRocksDBOptionsFromFile(
156 config_options
, db_options
, cf_names
, cf_opts
,
157 dbpath
+ "/" + options_file_name
, &fs
);
160 } // namespace ROCKSDB_NAMESPACE
161 #endif // !ROCKSDB_LITE