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 std::shared_ptr
<Cache
>* cache
) {
20 RocksDBOptionsParser parser
;
21 Status s
= parser
.Parse(file_name
, env
, ignore_unknown_options
);
25 *db_options
= *parser
.db_opt();
26 const std::vector
<std::string
>& cf_names
= *parser
.cf_names();
27 const std::vector
<ColumnFamilyOptions
>& cf_opts
= *parser
.cf_opts();
29 for (size_t i
= 0; i
< cf_opts
.size(); ++i
) {
30 cf_descs
->push_back({cf_names
[i
], cf_opts
[i
]});
31 if (cache
!= nullptr) {
32 TableFactory
* tf
= cf_opts
[i
].table_factory
.get();
33 if (tf
!= nullptr && tf
->GetOptions() != nullptr &&
34 tf
->Name() == BlockBasedTableFactory().Name()) {
35 auto* loaded_bbt_opt
=
36 reinterpret_cast<BlockBasedTableOptions
*>(tf
->GetOptions());
37 loaded_bbt_opt
->block_cache
= *cache
;
44 Status
GetLatestOptionsFileName(const std::string
& dbpath
,
45 Env
* env
, std::string
* options_file_name
) {
47 std::string latest_file_name
;
48 uint64_t latest_time_stamp
= 0;
49 std::vector
<std::string
> file_names
;
50 s
= env
->GetChildren(dbpath
, &file_names
);
54 for (auto& file_name
: file_names
) {
57 if (ParseFileName(file_name
, &time_stamp
, &type
) && type
== kOptionsFile
) {
58 if (time_stamp
> latest_time_stamp
) {
59 latest_time_stamp
= time_stamp
;
60 latest_file_name
= file_name
;
64 if (latest_file_name
.size() == 0) {
65 return Status::NotFound("No options files found in the DB directory.");
67 *options_file_name
= latest_file_name
;
71 Status
LoadLatestOptions(const std::string
& dbpath
, Env
* env
,
72 DBOptions
* db_options
,
73 std::vector
<ColumnFamilyDescriptor
>* cf_descs
,
74 bool ignore_unknown_options
,
75 std::shared_ptr
<Cache
>* cache
) {
76 std::string options_file_name
;
77 Status s
= GetLatestOptionsFileName(dbpath
, env
, &options_file_name
);
81 return LoadOptionsFromFile(dbpath
+ "/" + options_file_name
, env
, db_options
,
82 cf_descs
, ignore_unknown_options
, cache
);
85 Status
CheckOptionsCompatibility(
86 const std::string
& dbpath
, Env
* env
, const DBOptions
& db_options
,
87 const std::vector
<ColumnFamilyDescriptor
>& cf_descs
,
88 bool ignore_unknown_options
) {
89 std::string options_file_name
;
90 Status s
= GetLatestOptionsFileName(dbpath
, env
, &options_file_name
);
95 std::vector
<std::string
> cf_names
;
96 std::vector
<ColumnFamilyOptions
> cf_opts
;
97 for (const auto& cf_desc
: cf_descs
) {
98 cf_names
.push_back(cf_desc
.name
);
99 cf_opts
.push_back(cf_desc
.options
);
102 const OptionsSanityCheckLevel kDefaultLevel
= kSanityLevelLooselyCompatible
;
104 return RocksDBOptionsParser::VerifyRocksDBOptionsFromFile(
105 db_options
, cf_names
, cf_opts
, dbpath
+ "/" + options_file_name
, env
,
106 kDefaultLevel
, ignore_unknown_options
);
109 } // namespace rocksdb
110 #endif // !ROCKSDB_LITE