/Users/deen/code/yugabyte-db/src/yb/rocksdb/db/merge_operator.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | | // This source code is licensed under the BSD-style license found in the |
3 | | // LICENSE file in the root directory of this source tree. An additional grant |
4 | | // of patent rights can be found in the PATENTS file in the same directory. |
5 | | // |
6 | | // The following only applies to changes made to this file as part of YugaByte development. |
7 | | // |
8 | | // Portions Copyright (c) YugaByte, Inc. |
9 | | // |
10 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
11 | | // in compliance with the License. You may obtain a copy of the License at |
12 | | // |
13 | | // http://www.apache.org/licenses/LICENSE-2.0 |
14 | | // |
15 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
16 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
17 | | // or implied. See the License for the specific language governing permissions and limitations |
18 | | // under the License. |
19 | | // |
20 | | /** |
21 | | * Back-end implementation details specific to the Merge Operator. |
22 | | */ |
23 | | |
24 | | #include "yb/rocksdb/merge_operator.h" |
25 | | |
26 | | namespace rocksdb { |
27 | | |
28 | | // The default implementation of PartialMergeMulti, which invokes |
29 | | // PartialMerge multiple times internally and merges two operands at |
30 | | // a time. |
31 | | bool MergeOperator::PartialMergeMulti(const Slice& key, |
32 | | const std::deque<Slice>& operand_list, |
33 | | std::string* new_value, |
34 | 1.88k | Logger* logger) const { |
35 | 1.88k | assert(operand_list.size() >= 2); |
36 | | // Simply loop through the operands |
37 | 1.88k | Slice temp_slice(operand_list[0]); |
38 | | |
39 | 1.95k | for (size_t i = 1; i < operand_list.size(); ++i) { |
40 | 1.88k | auto& operand = operand_list[i]; |
41 | 1.88k | std::string temp_value; |
42 | 1.88k | if (!PartialMerge(key, temp_slice, operand, &temp_value, logger)) { |
43 | 1.80k | return false; |
44 | 1.80k | } |
45 | 77 | swap(temp_value, *new_value); |
46 | 77 | temp_slice = Slice(*new_value); |
47 | 77 | } |
48 | | |
49 | | // The result will be in *new_value. All merges succeeded. |
50 | 74 | return true; |
51 | 1.88k | } |
52 | | |
53 | | // Given a "real" merge from the library, call the user's |
54 | | // associative merge function one-by-one on each of the operands. |
55 | | // NOTE: It is assumed that the client's merge-operator will handle any errors. |
56 | | bool AssociativeMergeOperator::FullMerge( |
57 | | const Slice& key, |
58 | | const Slice* existing_value, |
59 | | const std::deque<std::string>& operand_list, |
60 | | std::string* new_value, |
61 | 3.69k | Logger* logger) const { |
62 | | |
63 | | // Simply loop through the operands |
64 | 3.69k | Slice temp_existing; |
65 | 45.9k | for (const auto& operand : operand_list) { |
66 | 45.9k | Slice value(operand); |
67 | 45.9k | std::string temp_value; |
68 | 45.9k | if (!Merge(key, existing_value, value, &temp_value, logger)) { |
69 | 0 | return false; |
70 | 0 | } |
71 | 45.9k | swap(temp_value, *new_value); |
72 | 45.9k | temp_existing = Slice(*new_value); |
73 | 45.9k | existing_value = &temp_existing; |
74 | 45.9k | } |
75 | | |
76 | | // The result will be in *new_value. All merges succeeded. |
77 | 3.69k | return true; |
78 | 3.69k | } |
79 | | |
80 | | // Call the user defined simple merge on the operands; |
81 | | // NOTE: It is assumed that the client's merge-operator will handle any errors. |
82 | | bool AssociativeMergeOperator::PartialMerge( |
83 | | const Slice& key, |
84 | | const Slice& left_operand, |
85 | | const Slice& right_operand, |
86 | | std::string* new_value, |
87 | 441 | Logger* logger) const { |
88 | 441 | return Merge(key, &left_operand, right_operand, new_value, logger); |
89 | 441 | } |
90 | | |
91 | | } // namespace rocksdb |