configurable_helper.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #pragma once
  6. #include <map>
  7. #include <stdexcept>
  8. #include <string>
  9. #include <vector>
  10. #include "rocksdb/configurable.h"
  11. #include "rocksdb/convenience.h"
  12. namespace ROCKSDB_NAMESPACE {
  13. // Helper class defining static methods for supporting the Configurable
  14. // class. The purpose of this class is to keep the Configurable class
  15. // as tight as possible and provide methods for doing the actual work
  16. // of configuring the objects.
  17. class ConfigurableHelper {
  18. public:
  19. // Configures the input Configurable object based on the parameters.
  20. // On successful completion, the Configurable is updated with the settings
  21. // from the opt_map.
  22. //
  23. // The acceptable values of the name/value pairs are documented with the
  24. // specific class/instance.
  25. //
  26. // @param config_options Controls how the arguments are processed.
  27. // @param opt_map Name/value pairs of the options to update
  28. // @param unused If specified, this value will return the name/value
  29. // pairs from opt_map that were NotFound for this object.
  30. // @return OK If all values in the map were successfully updated
  31. // @return NotFound If any of the names in the opt_map were not valid
  32. // for this object. If unused is specified, it will contain the
  33. // collection of NotFound entries
  34. // @return NotSupported If any of the names are valid but the object does
  35. // not know how to convert the value. This can happen if, for example,
  36. // there is some nested Configurable that cannot be created.
  37. // @return InvalidArgument If any of the values cannot be successfully
  38. // parsed. This can also be returned if PrepareOptions encounters an
  39. // error.
  40. static Status ConfigureOptions(
  41. const ConfigOptions& config_options, Configurable& configurable,
  42. const std::unordered_map<std::string, std::string>& options,
  43. std::unordered_map<std::string, std::string>* unused);
  44. // Internal method to configure a set of options for this object.
  45. // Classes may override this value to change its behavior.
  46. // @param config_options Controls how the options are being configured
  47. // @param type_name The name that was registered for this set of options
  48. // @param type_map The map of options for this name
  49. // @param opt_ptr Pointer to the object being configured for this option set.
  50. // @param options The option name/values being updated. On return, any
  51. // option that was found is removed from the list.
  52. // @return OK If all of the options were successfully updated.
  53. // @return InvalidArgument If an option was found but the value could not
  54. // be updated.
  55. // @return NotFound If an option name was not found in type_mape
  56. // @return NotSupported If the option was found but no rule for converting
  57. // the value could be found.
  58. static Status ConfigureSomeOptions(
  59. const ConfigOptions& config_options, Configurable& configurable,
  60. const std::unordered_map<std::string, OptionTypeInfo>& type_map,
  61. std::unordered_map<std::string, std::string>* options, void* opt_ptr);
  62. // Configures a single option in the input Configurable.
  63. // This method will look through the set of option names for this
  64. // Configurable searching for one with the input name. If such an option
  65. // is found, it will be configured via the input value.
  66. //
  67. // @param config_options Controls how the option is being configured
  68. // @param configurable The object to configure
  69. // @param name For options with sub-options (like Structs or
  70. // Configurables),
  71. // this value may be the name of the sub-field of the option being
  72. // updated. For example, if the option is
  73. // "compaction_options_fifo.allow_compaction", then field name would be
  74. // "allow_compaction". For most options, field_name and opt_name will be
  75. // equivalent.
  76. // @param value The new value for this option.
  77. // @param See ConfigureOptions for the possible return values
  78. static Status ConfigureSingleOption(const ConfigOptions& config_options,
  79. Configurable& configurable,
  80. const std::string& name,
  81. const std::string& value);
  82. // Configures the option referenced by opt_info for this configurable
  83. // This method configures the option based on opt_info for the input
  84. // configurable.
  85. // @param config_options Controls how the option is being configured
  86. // @param configurable The object to configure
  87. // @param opt_name The full option name
  88. // @param name For options with sub-options (like Structs or
  89. // Configurables),
  90. // this value may be the name of the sub-field of the option being
  91. // updated. For example, if the option is
  92. // "compaction_options_fifo.allow_compaction", then field name would be
  93. // "allow_compaction". For most options, field_name and opt_name will be
  94. // equivalent.
  95. // @param value The new value for this option.
  96. // @param See ConfigureOptions for the possible return values
  97. static Status ConfigureOption(const ConfigOptions& config_options,
  98. Configurable& configurable,
  99. const OptionTypeInfo& opt_info,
  100. const std::string& opt_name,
  101. const std::string& name,
  102. const std::string& value, void* opt_ptr);
  103. // Returns the value of the option associated with the input name
  104. // This method is the functional inverse of ConfigureOption
  105. // @param config_options Controls how the value is returned
  106. // @param configurable The object from which to get the option.
  107. // @param name The name of the option to return a value for.
  108. // @param value The returned value associated with the named option.
  109. // Note that value will be only the serialized version
  110. // of the option and not "name=value"
  111. // @return OK If the named field was successfully updated to value.
  112. // @return NotFound If the name is not valid for this object.
  113. // @param InvalidArgument If the name is valid for this object but
  114. // its value cannot be serialized.
  115. static Status GetOption(const ConfigOptions& config_options,
  116. const Configurable& configurable,
  117. const std::string& name, std::string* value);
  118. // Serializes the input Configurable into the output result.
  119. // This is the inverse of ConfigureOptions
  120. // @param config_options Controls how serialization happens.
  121. // @param configurable The object to serialize
  122. // @param prefix A prefix to add to the each option as it is serialized.
  123. // @param result The string representation of the configurable.
  124. // @return OK If the options for this object wer successfully serialized.
  125. // @return InvalidArgument If one or more of the options could not be
  126. // serialized.
  127. static Status SerializeOptions(const ConfigOptions& config_options,
  128. const Configurable& configurable,
  129. const std::string& prefix,
  130. std::string* result);
  131. // Internal method to list the option names for this object.
  132. // Classes may override this value to change its behavior.
  133. // @see ListOptions for more details
  134. static Status ListOptions(const ConfigOptions& config_options,
  135. const Configurable& configurable,
  136. const std::string& prefix,
  137. std::unordered_set<std::string>* result);
  138. // Checks to see if the two configurables are equivalent to one other.
  139. // This method assumes that the two objects are of the same class.
  140. // @param config_options Controls how the options are compared.
  141. // @param this_one The object to compare to.
  142. // @param that_one The other object being compared.
  143. // @param mismatch If the objects do not match, this parameter contains
  144. // the name of the option that triggered the match failure.
  145. // @param True if the objects match, false otherwise.
  146. static bool AreEquivalent(const ConfigOptions& config_options,
  147. const Configurable& this_one,
  148. const Configurable& that_one,
  149. std::string* mismatch);
  150. private:
  151. // Looks for the option specified by name in the RegisteredOptions of a
  152. // configurable. If an entry matching name is found, that entry, opt_name,
  153. // and pointer are returned.
  154. // @param options The vector of options to search through
  155. // @param name The name of the option to search for in the OptionType map
  156. // @param opt_name If the name was found, this value is set to the option name
  157. // associated with the input name/type.
  158. // @param opt_ptr If the name was found, this value is set to the option
  159. // pointer
  160. // in the RegisteredOptions vector associated with this entry
  161. // @return A pointer to the OptionTypeInfo from the options if found,
  162. // nullptr if the name was not found in the input options
  163. static const OptionTypeInfo* FindOption(const Configurable& configurable,
  164. const std::string& name,
  165. std::string* opt_name,
  166. void** opt_ptr);
  167. static Status ConfigureCustomizableOption(
  168. const ConfigOptions& config_options, Configurable& configurable,
  169. const OptionTypeInfo& opt_info, const std::string& opt_name,
  170. const std::string& name, const std::string& value, void* opt_ptr);
  171. };
  172. } // namespace ROCKSDB_NAMESPACE