configurable_test.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style license that can be
  8. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  9. #pragma once
  10. #include <algorithm>
  11. #include <memory>
  12. #include <unordered_map>
  13. #include "options/configurable_helper.h"
  14. #include "rocksdb/configurable.h"
  15. #include "rocksdb/utilities/options_type.h"
  16. namespace ROCKSDB_NAMESPACE {
  17. struct ColumnFamilyOptions;
  18. struct DBOptions;
  19. namespace test {
  20. enum TestEnum { kTestA, kTestB };
  21. static const std::unordered_map<std::string, int> test_enum_map = {
  22. {"A", TestEnum::kTestA},
  23. {"B", TestEnum::kTestB},
  24. };
  25. struct TestOptions {
  26. int i = 0;
  27. bool b = false;
  28. bool d = true;
  29. TestEnum e = TestEnum::kTestA;
  30. std::string s;
  31. std::string u;
  32. };
  33. static std::unordered_map<std::string, OptionTypeInfo> simple_option_info = {
  34. {"int",
  35. {offsetof(struct TestOptions, i), OptionType::kInt,
  36. OptionVerificationType::kNormal, OptionTypeFlags::kMutable}},
  37. {"bool",
  38. {offsetof(struct TestOptions, b), OptionType::kBoolean,
  39. OptionVerificationType::kNormal, OptionTypeFlags::kNone}},
  40. {"string",
  41. {offsetof(struct TestOptions, s), OptionType::kString,
  42. OptionVerificationType::kNormal, OptionTypeFlags::kNone}},
  43. };
  44. static std::unordered_map<std::string, OptionTypeInfo> enum_option_info = {
  45. {"enum",
  46. OptionTypeInfo::Enum(offsetof(struct TestOptions, e), &test_enum_map)}};
  47. static std::unordered_map<std::string, OptionTypeInfo> unique_option_info = {
  48. {"unique",
  49. {0, OptionType::kConfigurable, OptionVerificationType::kNormal,
  50. (OptionTypeFlags::kUnique | OptionTypeFlags::kMutable)}},
  51. };
  52. static std::unordered_map<std::string, OptionTypeInfo> shared_option_info = {
  53. {"shared",
  54. {0, OptionType::kConfigurable, OptionVerificationType::kNormal,
  55. (OptionTypeFlags::kShared)}},
  56. };
  57. static std::unordered_map<std::string, OptionTypeInfo> pointer_option_info = {
  58. {"pointer",
  59. {0, OptionType::kConfigurable, OptionVerificationType::kNormal,
  60. OptionTypeFlags::kRawPointer}},
  61. };
  62. enum TestConfigMode {
  63. kEmptyMode = 0x0, // Don't register anything
  64. kMutableMode = 0x01, // Configuration is mutable
  65. kSimpleMode = 0x02, // Use the simple options
  66. kEnumMode = 0x04, // Use the enum options
  67. kDefaultMode = kSimpleMode, // Use no inner nested configurations
  68. kSharedMode = 0x10, // Use shared configuration
  69. kUniqueMode = 0x20, // Use unique configuration
  70. kRawPtrMode = 0x40, // Use pointer configuration
  71. kNestedMode = (kSharedMode | kUniqueMode | kRawPtrMode),
  72. kAllOptMode = (kNestedMode | kEnumMode | kSimpleMode),
  73. };
  74. template <typename T>
  75. class TestConfigurable : public Configurable {
  76. protected:
  77. std::string name_;
  78. std::string prefix_;
  79. TestOptions options_;
  80. public:
  81. std::unique_ptr<T> unique_;
  82. std::shared_ptr<T> shared_;
  83. T* pointer_;
  84. TestConfigurable(const std::string& name, int mode,
  85. const std::unordered_map<std::string, OptionTypeInfo>* map =
  86. &simple_option_info)
  87. : name_(name), pointer_(nullptr) {
  88. prefix_ = "test." + name + ".";
  89. if ((mode & TestConfigMode::kSimpleMode) != 0) {
  90. RegisterOptions(name_, &options_, map);
  91. }
  92. if ((mode & TestConfigMode::kEnumMode) != 0) {
  93. RegisterOptions(name_ + "Enum", &options_, &enum_option_info);
  94. }
  95. }
  96. ~TestConfigurable() override { delete pointer_; }
  97. };
  98. } // namespace test
  99. } // namespace ROCKSDB_NAMESPACE