]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
11fdf7f2 TL |
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). | |
7c673cae FG |
5 | |
6 | #include <memory> | |
7 | ||
8 | #include "rocksdb/merge_operator.h" | |
9 | #include "rocksdb/slice.h" | |
10 | #include "utilities/merge_operators.h" | |
11 | ||
12 | using rocksdb::Slice; | |
13 | using rocksdb::Logger; | |
14 | using rocksdb::MergeOperator; | |
15 | ||
16 | namespace { // anonymous namespace | |
17 | ||
18 | // Merge operator that picks the maximum operand, Comparison is based on | |
19 | // Slice::compare | |
20 | class MaxOperator : public MergeOperator { | |
21 | public: | |
22 | virtual bool FullMergeV2(const MergeOperationInput& merge_in, | |
23 | MergeOperationOutput* merge_out) const override { | |
24 | Slice& max = merge_out->existing_operand; | |
25 | if (merge_in.existing_value) { | |
26 | max = Slice(merge_in.existing_value->data(), | |
27 | merge_in.existing_value->size()); | |
11fdf7f2 TL |
28 | } else if (max.data() == nullptr) { |
29 | max = Slice(); | |
7c673cae FG |
30 | } |
31 | ||
32 | for (const auto& op : merge_in.operand_list) { | |
33 | if (max.compare(op) < 0) { | |
34 | max = op; | |
35 | } | |
36 | } | |
37 | ||
38 | return true; | |
39 | } | |
40 | ||
11fdf7f2 | 41 | virtual bool PartialMerge(const Slice& /*key*/, const Slice& left_operand, |
7c673cae | 42 | const Slice& right_operand, std::string* new_value, |
11fdf7f2 | 43 | Logger* /*logger*/) const override { |
7c673cae FG |
44 | if (left_operand.compare(right_operand) >= 0) { |
45 | new_value->assign(left_operand.data(), left_operand.size()); | |
46 | } else { | |
47 | new_value->assign(right_operand.data(), right_operand.size()); | |
48 | } | |
49 | return true; | |
50 | } | |
51 | ||
11fdf7f2 | 52 | virtual bool PartialMergeMulti(const Slice& /*key*/, |
7c673cae FG |
53 | const std::deque<Slice>& operand_list, |
54 | std::string* new_value, | |
11fdf7f2 | 55 | Logger* /*logger*/) const override { |
7c673cae FG |
56 | Slice max; |
57 | for (const auto& operand : operand_list) { | |
58 | if (max.compare(operand) < 0) { | |
59 | max = operand; | |
60 | } | |
61 | } | |
62 | ||
63 | new_value->assign(max.data(), max.size()); | |
64 | return true; | |
65 | } | |
66 | ||
67 | virtual const char* Name() const override { return "MaxOperator"; } | |
68 | }; | |
69 | ||
70 | } // end of anonymous namespace | |
71 | ||
72 | namespace rocksdb { | |
73 | ||
74 | std::shared_ptr<MergeOperator> MergeOperators::CreateMaxOperator() { | |
75 | return std::make_shared<MaxOperator>(); | |
76 | } | |
77 | } |