YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/rocksdb/utilities/merge_operators/uint64add.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 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
// The following only applies to changes made to this file as part of YugaByte development.
7
//
8
// Portions Copyright (c) YugaByte, Inc.
9
//
10
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
11
// in compliance with the License.  You may obtain a copy of the License at
12
//
13
// http://www.apache.org/licenses/LICENSE-2.0
14
//
15
// Unless required by applicable law or agreed to in writing, software distributed under the License
16
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
17
// or implied.  See the License for the specific language governing permissions and limitations
18
// under the License.
19
//
20
21
#include <memory>
22
23
#include "yb/rocksdb/env.h"
24
#include "yb/rocksdb/merge_operator.h"
25
#include "yb/util/slice.h"
26
#include "yb/rocksdb/util/coding.h"
27
#include "yb/rocksdb/utilities/merge_operators.h"
28
29
using namespace rocksdb;
30
31
namespace { // anonymous namespace
32
33
// A 'model' merge operator with uint64 addition semantics
34
// Implemented as an AssociativeMergeOperator for simplicity and example.
35
class UInt64AddOperator : public AssociativeMergeOperator {
36
 public:
37
  virtual bool Merge(const Slice& key,
38
                     const Slice* existing_value,
39
                     const Slice& value,
40
                     std::string* new_value,
41
2.33k
                     Logger* logger) const override {
42
2.33k
    uint64_t orig_value = 0;
43
2.33k
    if (existing_value){
44
1.13k
      orig_value = DecodeInteger(*existing_value, logger);
45
1.13k
    }
46
2.33k
    uint64_t operand = DecodeInteger(value, logger);
47
48
2.33k
    assert(new_value);
49
2.33k
    new_value->clear();
50
2.33k
    PutFixed64(new_value, orig_value + operand);
51
52
2.33k
    return true;  // Return true always since corruption will be treated as 0
53
2.33k
  }
54
55
524
  const char* Name() const override {
56
524
    return "UInt64AddOperator";
57
524
  }
58
59
 private:
60
  // Takes the string and decodes it into a uint64_t
61
  // On error, prints a message and returns 0
62
3.46k
  uint64_t DecodeInteger(const Slice& value, Logger* logger) const {
63
3.46k
    uint64_t result = 0;
64
65
3.46k
    if (value.size() == sizeof(uint64_t)) {
66
3.46k
      result = DecodeFixed64(value.data());
67
0
    } else if (logger != nullptr) {
68
      // If value is corrupted, treat it as 0
69
0
      RLOG(InfoLogLevel::ERROR_LEVEL, logger,
70
0
          "uint64 value corruption, size: %" ROCKSDB_PRIszt
71
0
          " > %" ROCKSDB_PRIszt,
72
0
          value.size(), sizeof(uint64_t));
73
0
    }
74
75
3.46k
    return result;
76
3.46k
  }
77
78
};
79
80
}
81
82
namespace rocksdb {
83
84
41
std::shared_ptr<MergeOperator> MergeOperators::CreateUInt64AddOperator() {
85
41
  return std::make_shared<UInt64AddOperator>();
86
41
}
87
88
}