util_merge_operators_test.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #include "test_util/testharness.h"
  6. #include "test_util/testutil.h"
  7. #include "utilities/merge_operators.h"
  8. namespace ROCKSDB_NAMESPACE {
  9. class UtilMergeOperatorTest : public testing::Test {
  10. public:
  11. UtilMergeOperatorTest() {}
  12. std::string FullMergeV2(std::string existing_value,
  13. std::vector<std::string> operands,
  14. std::string key = "") {
  15. std::string result;
  16. Slice result_operand(nullptr, 0);
  17. Slice existing_value_slice(existing_value);
  18. std::vector<Slice> operands_slice(operands.begin(), operands.end());
  19. const MergeOperator::MergeOperationInput merge_in(
  20. key, &existing_value_slice, operands_slice, nullptr);
  21. MergeOperator::MergeOperationOutput merge_out(result, result_operand);
  22. merge_operator_->FullMergeV2(merge_in, &merge_out);
  23. if (result_operand.data()) {
  24. result.assign(result_operand.data(), result_operand.size());
  25. }
  26. return result;
  27. }
  28. std::string FullMergeV2(std::vector<std::string> operands,
  29. std::string key = "") {
  30. std::string result;
  31. Slice result_operand(nullptr, 0);
  32. std::vector<Slice> operands_slice(operands.begin(), operands.end());
  33. const MergeOperator::MergeOperationInput merge_in(key, nullptr,
  34. operands_slice, nullptr);
  35. MergeOperator::MergeOperationOutput merge_out(result, result_operand);
  36. merge_operator_->FullMergeV2(merge_in, &merge_out);
  37. if (result_operand.data()) {
  38. result.assign(result_operand.data(), result_operand.size());
  39. }
  40. return result;
  41. }
  42. std::string PartialMerge(std::string left, std::string right,
  43. std::string key = "") {
  44. std::string result;
  45. merge_operator_->PartialMerge(key, left, right, &result, nullptr);
  46. return result;
  47. }
  48. std::string PartialMergeMulti(std::deque<std::string> operands,
  49. std::string key = "") {
  50. std::string result;
  51. std::deque<Slice> operands_slice(operands.begin(), operands.end());
  52. merge_operator_->PartialMergeMulti(key, operands_slice, &result, nullptr);
  53. return result;
  54. }
  55. protected:
  56. std::shared_ptr<MergeOperator> merge_operator_;
  57. };
  58. TEST_F(UtilMergeOperatorTest, MaxMergeOperator) {
  59. merge_operator_ = MergeOperators::CreateMaxOperator();
  60. EXPECT_EQ("B", FullMergeV2("B", {"A"}));
  61. EXPECT_EQ("B", FullMergeV2("A", {"B"}));
  62. EXPECT_EQ("", FullMergeV2({"", "", ""}));
  63. EXPECT_EQ("A", FullMergeV2({"A"}));
  64. EXPECT_EQ("ABC", FullMergeV2({"ABC"}));
  65. EXPECT_EQ("Z", FullMergeV2({"ABC", "Z", "C", "AXX"}));
  66. EXPECT_EQ("ZZZ", FullMergeV2({"ABC", "CC", "Z", "ZZZ"}));
  67. EXPECT_EQ("a", FullMergeV2("a", {"ABC", "CC", "Z", "ZZZ"}));
  68. EXPECT_EQ("z", PartialMergeMulti({"a", "z", "efqfqwgwew", "aaz", "hhhhh"}));
  69. EXPECT_EQ("b", PartialMerge("a", "b"));
  70. EXPECT_EQ("z", PartialMerge("z", "azzz"));
  71. EXPECT_EQ("a", PartialMerge("a", ""));
  72. }
  73. } // namespace ROCKSDB_NAMESPACE
  74. int main(int argc, char** argv) {
  75. ::testing::InitGoogleTest(&argc, argv);
  76. return RUN_ALL_TESTS();
  77. }