]>
git.proxmox.com Git - ceph.git/blob - 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).
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"
15 namespace { // anonymous namespace
17 using ROCKSDB_NAMESPACE::AssociativeMergeOperator
;
18 using ROCKSDB_NAMESPACE::InfoLogLevel
;
19 using ROCKSDB_NAMESPACE::Logger
;
20 using ROCKSDB_NAMESPACE::Slice
;
22 // A 'model' merge operator with uint64 addition semantics
23 // Implemented as an AssociativeMergeOperator for simplicity and example.
24 class UInt64AddOperator
: public AssociativeMergeOperator
{
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;
31 orig_value
= DecodeInteger(*existing_value
, logger
);
33 uint64_t operand
= DecodeInteger(value
, logger
);
37 ROCKSDB_NAMESPACE::PutFixed64(new_value
, orig_value
+ operand
);
39 return true; // Return true always since corruption will be treated as 0
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(); }
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 {
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));
67 } // anonymous namespace
69 namespace ROCKSDB_NAMESPACE
{
71 std::shared_ptr
<MergeOperator
> MergeOperators::CreateUInt64AddOperator() {
72 return std::make_shared
<UInt64AddOperator
>();
75 } // namespace ROCKSDB_NAMESPACE