Coverage Report

Created: 2025-07-23 07:17

/src/rocksdb/options/options_parser.h
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 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).
5
6
#pragma once
7
8
#include <map>
9
#include <string>
10
#include <vector>
11
12
#include "rocksdb/env.h"
13
#include "rocksdb/options.h"
14
15
namespace ROCKSDB_NAMESPACE {
16
17
struct ConfigOptions;
18
class OptionTypeInfo;
19
class TableFactory;
20
21
102k
#define ROCKSDB_OPTION_FILE_MAJOR 1
22
102k
#define ROCKSDB_OPTION_FILE_MINOR 1
23
24
enum OptionSection : char {
25
  kOptionSectionVersion = 0,
26
  kOptionSectionDBOptions,
27
  kOptionSectionCFOptions,
28
  kOptionSectionTableOptions,
29
  kOptionSectionUnknown
30
};
31
32
static const std::string opt_section_titles[] = {
33
    "Version", "DBOptions", "CFOptions", "TableOptions/", "Unknown"};
34
35
Status PersistRocksDBOptions(const WriteOptions& write_options,
36
                             const DBOptions& db_opt,
37
                             const std::vector<std::string>& cf_names,
38
                             const std::vector<ColumnFamilyOptions>& cf_opts,
39
                             const std::string& file_name, FileSystem* fs);
40
Status PersistRocksDBOptions(const WriteOptions& write_options,
41
                             const ConfigOptions& config_options,
42
                             const DBOptions& db_opt,
43
                             const std::vector<std::string>& cf_names,
44
                             const std::vector<ColumnFamilyOptions>& cf_opts,
45
                             const std::string& file_name, FileSystem* fs);
46
47
class RocksDBOptionsParser {
48
 public:
49
  explicit RocksDBOptionsParser();
50
102k
  ~RocksDBOptionsParser() {}
51
  void Reset();
52
53
  // `file_readahead_size` is used for readahead for the option file.
54
  // If 0 is given, a default value will be used.
55
  Status Parse(const std::string& file_name, FileSystem* fs,
56
               bool ignore_unknown_options, size_t file_readahead_size);
57
58
  Status Parse(const ConfigOptions& config_options,
59
               const std::string& file_name, FileSystem* fs);
60
61
  static std::string TrimAndRemoveComment(const std::string& line,
62
                                          const bool trim_only = false);
63
64
102k
  const DBOptions* db_opt() const { return &db_opt_; }
65
102k
  const std::unordered_map<std::string, std::string>* db_opt_map() const {
66
102k
    return &db_opt_map_;
67
102k
  }
68
394k
  const std::vector<ColumnFamilyOptions>* cf_opts() const { return &cf_opts_; }
69
248k
  const std::vector<std::string>* cf_names() const { return &cf_names_; }
70
  const std::vector<std::unordered_map<std::string, std::string>>* cf_opt_maps()
71
146k
      const {
72
146k
    return &cf_opt_maps_;
73
146k
  }
74
75
292k
  const ColumnFamilyOptions* GetCFOptions(const std::string& name) {
76
292k
    return GetCFOptionsImpl(name);
77
292k
  }
78
0
  size_t NumColumnFamilies() { return cf_opts_.size(); }
79
  static Status VerifyRocksDBOptionsFromFile(
80
      const ConfigOptions& config_options, const DBOptions& db_opt,
81
      const std::vector<std::string>& cf_names,
82
      const std::vector<ColumnFamilyOptions>& cf_opts,
83
      const std::string& file_name, FileSystem* fs);
84
  static Status VerifyDBOptions(
85
      const ConfigOptions& config_options, const DBOptions& base_opt,
86
      const DBOptions& new_opt,
87
      const std::unordered_map<std::string, std::string>* new_opt_map =
88
          nullptr);
89
90
  static Status VerifyCFOptions(
91
      const ConfigOptions& config_options, const ColumnFamilyOptions& base_opt,
92
      const ColumnFamilyOptions& new_opt,
93
      const std::unordered_map<std::string, std::string>* new_opt_map =
94
          nullptr);
95
96
  static Status VerifyTableFactory(const ConfigOptions& config_options,
97
                                   const TableFactory* base_tf,
98
                                   const TableFactory* file_tf);
99
100
  static Status ExtraParserCheck(const RocksDBOptionsParser& input_parser);
101
102
  static Status ParseStatement(std::string* name, std::string* value,
103
                               const std::string& line, const int line_num);
104
105
 protected:
106
  bool IsSection(const std::string& line);
107
  Status ParseSection(OptionSection* section, std::string* title,
108
                      std::string* argument, const std::string& line,
109
                      const int line_num);
110
111
  Status CheckSection(const OptionSection section,
112
                      const std::string& section_arg, const int line_num);
113
114
  Status EndSection(
115
      const ConfigOptions& config_options, const OptionSection section,
116
      const std::string& title, const std::string& section_arg,
117
      const std::unordered_map<std::string, std::string>& opt_map);
118
119
  Status ValidityCheck();
120
121
  static Status InvalidArgument(const int line_num, const std::string& message);
122
123
  Status ParseVersionNumber(const std::string& ver_name,
124
                            const std::string& ver_string, const int max_count,
125
                            int* version);
126
127
438k
  ColumnFamilyOptions* GetCFOptionsImpl(const std::string& name) {
128
438k
    assert(cf_names_.size() == cf_opts_.size());
129
570k
    for (size_t i = 0; i < cf_names_.size(); ++i) {
130
424k
      if (cf_names_[i] == name) {
131
292k
        return &cf_opts_[i];
132
292k
      }
133
424k
    }
134
146k
    return nullptr;
135
438k
  }
136
137
 private:
138
  DBOptions db_opt_;
139
  std::unordered_map<std::string, std::string> db_opt_map_;
140
  std::vector<std::string> cf_names_;
141
  std::vector<ColumnFamilyOptions> cf_opts_;
142
  std::vector<std::unordered_map<std::string, std::string>> cf_opt_maps_;
143
  bool has_version_section_;
144
  bool has_db_options_;
145
  bool has_default_cf_options_;
146
  int db_version[3];
147
  int opt_file_version[3];
148
};
149
150
}  // namespace ROCKSDB_NAMESPACE