]>
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 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.
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"
15 using namespace rocksdb
;
17 namespace { // anonymous namespace
19 // A 'model' merge operator with uint64 addition semantics
20 // Implemented as an AssociativeMergeOperator for simplicity and example.
21 class UInt64AddOperator
: public AssociativeMergeOperator
{
23 virtual bool Merge(const Slice
& key
,
24 const Slice
* existing_value
,
26 std::string
* new_value
,
27 Logger
* logger
) const override
{
28 uint64_t orig_value
= 0;
30 orig_value
= DecodeInteger(*existing_value
, logger
);
32 uint64_t operand
= DecodeInteger(value
, logger
);
36 PutFixed64(new_value
, orig_value
+ operand
);
38 return true; // Return true always since corruption will be treated as 0
41 virtual const char* Name() const override
{
42 return "UInt64AddOperator";
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 {
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));
69 std::shared_ptr
<MergeOperator
> MergeOperators::CreateUInt64AddOperator() {
70 return std::make_shared
<UInt64AddOperator
>();