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