/src/rocksdb/utilities/merge_operators/bytesxor.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 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 | | |
6 | | #include "utilities/merge_operators/bytesxor.h" |
7 | | |
8 | | #include <algorithm> |
9 | | #include <string> |
10 | | |
11 | | namespace ROCKSDB_NAMESPACE { |
12 | | |
13 | 0 | std::shared_ptr<MergeOperator> MergeOperators::CreateBytesXOROperator() { |
14 | 0 | return std::make_shared<BytesXOROperator>(); |
15 | 0 | } |
16 | | |
17 | | bool BytesXOROperator::Merge(const Slice& /*key*/, const Slice* existing_value, |
18 | | const Slice& value, std::string* new_value, |
19 | 0 | Logger* /*logger*/) const { |
20 | 0 | XOR(existing_value, value, new_value); |
21 | 0 | return true; |
22 | 0 | } |
23 | | |
24 | | void BytesXOROperator::XOR(const Slice* existing_value, const Slice& value, |
25 | 0 | std::string* new_value) const { |
26 | 0 | if (!existing_value) { |
27 | 0 | new_value->clear(); |
28 | 0 | new_value->assign(value.data(), value.size()); |
29 | 0 | return; |
30 | 0 | } |
31 | | |
32 | 0 | size_t min_size = std::min(existing_value->size(), value.size()); |
33 | 0 | size_t max_size = std::max(existing_value->size(), value.size()); |
34 | |
|
35 | 0 | new_value->clear(); |
36 | 0 | new_value->reserve(max_size); |
37 | |
|
38 | 0 | const char* existing_value_data = existing_value->data(); |
39 | 0 | const char* value_data = value.data(); |
40 | |
|
41 | 0 | for (size_t i = 0; i < min_size; i++) { |
42 | 0 | new_value->push_back(existing_value_data[i] ^ value_data[i]); |
43 | 0 | } |
44 | |
|
45 | 0 | if (existing_value->size() == max_size) { |
46 | 0 | for (size_t i = min_size; i < max_size; i++) { |
47 | 0 | new_value->push_back(existing_value_data[i]); |
48 | 0 | } |
49 | 0 | } else { |
50 | 0 | assert(value.size() == max_size); |
51 | 0 | for (size_t i = min_size; i < max_size; i++) { |
52 | 0 | new_value->push_back(value_data[i]); |
53 | 0 | } |
54 | 0 | } |
55 | 0 | } |
56 | | |
57 | | } // namespace ROCKSDB_NAMESPACE |