]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/utilities/merge_operators/uint64add.cc
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / rocksdb / utilities / merge_operators / uint64add.cc
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 #include <memory>
7
8 #include "rocksdb/env.h"
9 #include "rocksdb/merge_operator.h"
10 #include "rocksdb/slice.h"
11 #include "util/coding.h"
12 #include "util/logging.h"
13 #include "utilities/merge_operators.h"
14
15 using namespace rocksdb;
16
17 namespace { // anonymous namespace
18
19 // A 'model' merge operator with uint64 addition semantics
20 // Implemented as an AssociativeMergeOperator for simplicity and example.
21 class UInt64AddOperator : public AssociativeMergeOperator {
22 public:
23 virtual bool Merge(const Slice& key,
24 const Slice* existing_value,
25 const Slice& value,
26 std::string* new_value,
27 Logger* logger) const override {
28 uint64_t orig_value = 0;
29 if (existing_value){
30 orig_value = DecodeInteger(*existing_value, logger);
31 }
32 uint64_t operand = DecodeInteger(value, logger);
33
34 assert(new_value);
35 new_value->clear();
36 PutFixed64(new_value, orig_value + operand);
37
38 return true; // Return true always since corruption will be treated as 0
39 }
40
41 virtual const char* Name() const override {
42 return "UInt64AddOperator";
43 }
44
45 private:
46 // Takes the string and decodes it into a uint64_t
47 // On error, prints a message and returns 0
48 uint64_t DecodeInteger(const Slice& value, Logger* logger) const {
49 uint64_t result = 0;
50
51 if (value.size() == sizeof(uint64_t)) {
52 result = DecodeFixed64(value.data());
53 } else if (logger != nullptr) {
54 // If value is corrupted, treat it as 0
55 ROCKS_LOG_ERROR(logger, "uint64 value corruption, size: %" ROCKSDB_PRIszt
56 " > %" ROCKSDB_PRIszt,
57 value.size(), sizeof(uint64_t));
58 }
59
60 return result;
61 }
62
63 };
64
65 }
66
67 namespace rocksdb {
68
69 std::shared_ptr<MergeOperator> MergeOperators::CreateUInt64AddOperator() {
70 return std::make_shared<UInt64AddOperator>();
71 }
72
73 }