]>
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 | #include "rocksdb/slice.h" | |
8 | #include "rocksdb/merge_operator.h" | |
9 | #include "utilities/merge_operators.h" | |
10 | ||
11 | using namespace rocksdb; | |
12 | ||
13 | namespace { // anonymous namespace | |
14 | ||
15 | // A merge operator that mimics Put semantics | |
16 | // Since this merge-operator will not be used in production, | |
17 | // it is implemented as a non-associative merge operator to illustrate the | |
18 | // new interface and for testing purposes. (That is, we inherit from | |
19 | // the MergeOperator class rather than the AssociativeMergeOperator | |
20 | // which would be simpler in this case). | |
21 | // | |
22 | // From the client-perspective, semantics are the same. | |
23 | class PutOperator : public MergeOperator { | |
24 | public: | |
11fdf7f2 | 25 | virtual bool FullMerge(const Slice& /*key*/, const Slice* /*existing_value*/, |
7c673cae FG |
26 | const std::deque<std::string>& operand_sequence, |
27 | std::string* new_value, | |
11fdf7f2 | 28 | Logger* /*logger*/) const override { |
7c673cae FG |
29 | // Put basically only looks at the current/latest value |
30 | assert(!operand_sequence.empty()); | |
31 | assert(new_value != nullptr); | |
32 | new_value->assign(operand_sequence.back()); | |
33 | return true; | |
34 | } | |
35 | ||
11fdf7f2 TL |
36 | virtual bool PartialMerge(const Slice& /*key*/, const Slice& /*left_operand*/, |
37 | const Slice& right_operand, std::string* new_value, | |
38 | Logger* /*logger*/) const override { | |
7c673cae FG |
39 | new_value->assign(right_operand.data(), right_operand.size()); |
40 | return true; | |
41 | } | |
42 | ||
43 | using MergeOperator::PartialMergeMulti; | |
11fdf7f2 | 44 | virtual bool PartialMergeMulti(const Slice& /*key*/, |
7c673cae | 45 | const std::deque<Slice>& operand_list, |
11fdf7f2 TL |
46 | std::string* new_value, |
47 | Logger* /*logger*/) const override { | |
7c673cae FG |
48 | new_value->assign(operand_list.back().data(), operand_list.back().size()); |
49 | return true; | |
50 | } | |
51 | ||
52 | virtual const char* Name() const override { | |
53 | return "PutOperator"; | |
54 | } | |
55 | }; | |
56 | ||
57 | class PutOperatorV2 : public PutOperator { | |
11fdf7f2 TL |
58 | virtual bool FullMerge(const Slice& /*key*/, const Slice* /*existing_value*/, |
59 | const std::deque<std::string>& /*operand_sequence*/, | |
60 | std::string* /*new_value*/, | |
61 | Logger* /*logger*/) const override { | |
7c673cae FG |
62 | assert(false); |
63 | return false; | |
64 | } | |
65 | ||
66 | virtual bool FullMergeV2(const MergeOperationInput& merge_in, | |
67 | MergeOperationOutput* merge_out) const override { | |
68 | // Put basically only looks at the current/latest value | |
69 | assert(!merge_in.operand_list.empty()); | |
70 | merge_out->existing_operand = merge_in.operand_list.back(); | |
71 | return true; | |
72 | } | |
73 | }; | |
74 | ||
75 | } // end of anonymous namespace | |
76 | ||
77 | namespace rocksdb { | |
78 | ||
79 | std::shared_ptr<MergeOperator> MergeOperators::CreateDeprecatedPutOperator() { | |
80 | return std::make_shared<PutOperator>(); | |
81 | } | |
82 | ||
83 | std::shared_ptr<MergeOperator> MergeOperators::CreatePutOperator() { | |
84 | return std::make_shared<PutOperatorV2>(); | |
85 | } | |
86 | } |