merge_operators.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  2. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  3. // Use of this source code is governed by a BSD-style license that can be
  4. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  5. #include "utilities/merge_operators.h"
  6. #include <memory>
  7. #include "rocksdb/merge_operator.h"
  8. #include "rocksdb/options.h"
  9. #include "rocksdb/utilities/customizable_util.h"
  10. #include "rocksdb/utilities/object_registry.h"
  11. #include "utilities/merge_operators/bytesxor.h"
  12. #include "utilities/merge_operators/max_operator.h"
  13. #include "utilities/merge_operators/put_operator.h"
  14. #include "utilities/merge_operators/sortlist.h"
  15. #include "utilities/merge_operators/string_append/stringappend.h"
  16. #include "utilities/merge_operators/string_append/stringappend2.h"
  17. #include "utilities/merge_operators/uint64add.h"
  18. namespace ROCKSDB_NAMESPACE {
  19. static int RegisterBuiltinMergeOperators(ObjectLibrary& library,
  20. const std::string& /*arg*/) {
  21. size_t num_types;
  22. library.AddFactory<MergeOperator>(
  23. ObjectLibrary::PatternEntry(StringAppendOperator::kClassName())
  24. .AnotherName(StringAppendOperator::kNickName()),
  25. [](const std::string& /*uri*/, std::unique_ptr<MergeOperator>* guard,
  26. std::string* /*errmsg*/) {
  27. guard->reset(new StringAppendOperator(","));
  28. return guard->get();
  29. });
  30. library.AddFactory<MergeOperator>(
  31. ObjectLibrary::PatternEntry(StringAppendTESTOperator::kClassName())
  32. .AnotherName(StringAppendTESTOperator::kNickName()),
  33. [](const std::string& /*uri*/, std::unique_ptr<MergeOperator>* guard,
  34. std::string* /*errmsg*/) {
  35. guard->reset(new StringAppendTESTOperator(","));
  36. return guard->get();
  37. });
  38. library.AddFactory<MergeOperator>(
  39. ObjectLibrary::PatternEntry(SortList::kClassName())
  40. .AnotherName(SortList::kNickName()),
  41. [](const std::string& /*uri*/, std::unique_ptr<MergeOperator>* guard,
  42. std::string* /*errmsg*/) {
  43. guard->reset(new SortList());
  44. return guard->get();
  45. });
  46. library.AddFactory<MergeOperator>(
  47. ObjectLibrary::PatternEntry(BytesXOROperator::kClassName())
  48. .AnotherName(BytesXOROperator::kNickName()),
  49. [](const std::string& /*uri*/, std::unique_ptr<MergeOperator>* guard,
  50. std::string* /*errmsg*/) {
  51. guard->reset(new BytesXOROperator());
  52. return guard->get();
  53. });
  54. library.AddFactory<MergeOperator>(
  55. ObjectLibrary::PatternEntry(UInt64AddOperator::kClassName())
  56. .AnotherName(UInt64AddOperator::kNickName()),
  57. [](const std::string& /*uri*/, std::unique_ptr<MergeOperator>* guard,
  58. std::string* /*errmsg*/) {
  59. guard->reset(new UInt64AddOperator());
  60. return guard->get();
  61. });
  62. library.AddFactory<MergeOperator>(
  63. ObjectLibrary::PatternEntry(MaxOperator::kClassName())
  64. .AnotherName(MaxOperator::kNickName()),
  65. [](const std::string& /*uri*/, std::unique_ptr<MergeOperator>* guard,
  66. std::string* /*errmsg*/) {
  67. guard->reset(new MaxOperator());
  68. return guard->get();
  69. });
  70. library.AddFactory<MergeOperator>(
  71. ObjectLibrary::PatternEntry(PutOperatorV2::kClassName())
  72. .AnotherName(PutOperatorV2::kNickName()),
  73. [](const std::string& /*uri*/, std::unique_ptr<MergeOperator>* guard,
  74. std::string* /*errmsg*/) {
  75. guard->reset(new PutOperatorV2());
  76. return guard->get();
  77. });
  78. library.AddFactory<MergeOperator>(
  79. ObjectLibrary::PatternEntry(PutOperator::kNickName()),
  80. [](const std::string& /*uri*/, std::unique_ptr<MergeOperator>* guard,
  81. std::string* /*errmsg*/) {
  82. guard->reset(new PutOperator());
  83. return guard->get();
  84. });
  85. return static_cast<int>(library.GetFactoryCount(&num_types));
  86. }
  87. Status MergeOperator::CreateFromString(const ConfigOptions& config_options,
  88. const std::string& value,
  89. std::shared_ptr<MergeOperator>* result) {
  90. static std::once_flag once;
  91. std::call_once(once, [&]() {
  92. RegisterBuiltinMergeOperators(*(ObjectLibrary::Default().get()), "");
  93. });
  94. return LoadSharedObject<MergeOperator>(config_options, value, result);
  95. }
  96. std::shared_ptr<MergeOperator> MergeOperators::CreateFromStringId(
  97. const std::string& id) {
  98. std::shared_ptr<MergeOperator> result;
  99. Status s = MergeOperator::CreateFromString(ConfigOptions(), id, &result);
  100. if (s.ok()) {
  101. return result;
  102. } else {
  103. // Empty or unknown, just return nullptr
  104. return nullptr;
  105. }
  106. }
  107. } // namespace ROCKSDB_NAMESPACE