Coverage Report

Created: 2026-08-14 08:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/rocksdb/options/customizable.cc
Line
Count
Source
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
#include "rocksdb/customizable.h"
7
8
#include <sstream>
9
10
#include "options/options_helper.h"
11
#include "port/port.h"
12
#include "rocksdb/convenience.h"
13
#include "rocksdb/status.h"
14
#include "rocksdb/utilities/options_type.h"
15
#include "util/string_util.h"
16
17
namespace ROCKSDB_NAMESPACE {
18
19
5.39M
std::string Customizable::GetOptionName(const std::string& long_name) const {
20
5.39M
  const std::string& name = Name();
21
5.39M
  size_t name_len = name.size();
22
5.39M
  if (long_name.size() > name_len + 1 &&
23
4.34M
      long_name.compare(0, name_len, name) == 0 &&
24
0
      long_name.at(name_len) == '.') {
25
0
    return long_name.substr(name_len + 1);
26
5.39M
  } else {
27
5.39M
    return Configurable::GetOptionName(long_name);
28
5.39M
  }
29
5.39M
}
30
31
0
std::string Customizable::GenerateIndividualId() const {
32
0
  std::ostringstream ostr;
33
0
  ostr << Name() << "@" << static_cast<const void*>(this) << "#"
34
0
       << port::GetProcessID();
35
0
  return ostr.str();
36
0
}
37
38
Status Customizable::GetOption(const ConfigOptions& config_options,
39
                               const std::string& opt_name,
40
0
                               std::string* value) const {
41
0
  if (opt_name == OptionTypeInfo::kIdPropName()) {
42
0
    *value = GetId();
43
0
    return Status::OK();
44
0
  } else {
45
0
    return Configurable::GetOption(config_options, opt_name, value);
46
0
  }
47
0
}
48
49
std::string Customizable::SerializeOptions(const ConfigOptions& config_options,
50
1.05M
                                           const std::string& prefix) const {
51
1.05M
  std::string result;
52
1.05M
  std::string parent;
53
1.05M
  std::string id = GetId();
54
1.05M
  if (!config_options.IsShallow() && !id.empty()) {
55
1.05M
    parent = Configurable::SerializeOptions(config_options, "");
56
1.05M
  }
57
1.05M
  if (parent.empty()) {
58
921k
    result = id;
59
921k
  } else {
60
131k
    result.append(prefix);
61
131k
    result.append(OptionTypeInfo::kIdPropName());
62
131k
    result.append("=");
63
131k
    result.append(id);
64
131k
    result.append(config_options.delimiter);
65
131k
    result.append(parent);
66
131k
  }
67
1.05M
  return result;
68
1.05M
}
69
70
bool Customizable::AreEquivalent(const ConfigOptions& config_options,
71
                                 const Configurable* other,
72
394k
                                 std::string* mismatch) const {
73
394k
  if (config_options.sanity_level > ConfigOptions::kSanityLevelNone &&
74
394k
      this != other) {
75
394k
    const Customizable* custom = static_cast<const Customizable*>(other);
76
394k
    if (custom == nullptr) {  // Cast failed
77
0
      return false;
78
394k
    } else if (GetId() != custom->GetId()) {
79
0
      *mismatch = OptionTypeInfo::kIdPropName();
80
0
      return false;
81
394k
    } else if (config_options.sanity_level >
82
394k
               ConfigOptions::kSanityLevelLooselyCompatible) {
83
263k
      bool matches =
84
263k
          Configurable::AreEquivalent(config_options, other, mismatch);
85
263k
      return matches;
86
263k
    }
87
394k
  }
88
131k
  return true;
89
394k
}
90
91
Status Customizable::GetOptionsMap(
92
    const ConfigOptions& config_options, const Customizable* customizable,
93
    const std::string& value, std::string* id,
94
1.88M
    std::unordered_map<std::string, std::string>* props) {
95
1.88M
  Status status;
96
1.88M
  if (value.empty() || value == kNullptrString) {
97
1.22M
    *id = "";
98
1.22M
    props->clear();
99
1.22M
  } else if (customizable != nullptr) {
100
263k
    status =
101
263k
        Configurable::GetOptionsMap(value, customizable->GetId(), id, props);
102
263k
    if (status.ok() && customizable->IsInstanceOf(*id)) {
103
      // The new ID and the old ID match, so the objects are the same type.
104
      // Try to get the existing options, ignoring any errors
105
263k
      ConfigOptions embedded = config_options;
106
263k
      embedded.delimiter = ";";
107
263k
      std::string curr_opts;
108
263k
      if (customizable->GetOptionString(embedded, &curr_opts).ok()) {
109
263k
        std::unordered_map<std::string, std::string> curr_props;
110
263k
        if (StringToMap(curr_opts, &curr_props).ok()) {
111
263k
          props->insert(curr_props.begin(), curr_props.end());
112
263k
        }
113
263k
      }
114
263k
    }
115
394k
  } else {
116
394k
    status = Configurable::GetOptionsMap(value, "", id, props);
117
394k
  }
118
1.88M
  return status;
119
1.88M
}
120
121
Status Customizable::ConfigureNewObject(
122
    const ConfigOptions& config_options, Customizable* object,
123
526k
    const std::unordered_map<std::string, std::string>& opt_map) {
124
526k
  Status status;
125
526k
  if (object != nullptr) {
126
526k
    status = object->ConfigureFromMap(config_options, opt_map);
127
526k
  } else if (!opt_map.empty()) {
128
0
    status = Status::InvalidArgument("Cannot configure null object ");
129
0
  }
130
526k
  return status;
131
526k
}
132
}  // namespace ROCKSDB_NAMESPACE