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 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
6 // Use of this source code is governed by a BSD-style license that can be
7 // found in the LICENSE file. See the AUTHORS file for names of contributors.
8 
9 #pragma once
10 
11 #include "monitoring/perf_context_imp.h"
12 #include "rocksdb/comparator.h"
13 
14 namespace ROCKSDB_NAMESPACE {
15 
16 // Wrapper of user comparator, with auto increment to
17 // perf_context.user_key_comparison_count.
18 class UserComparatorWrapper final : public Comparator {
19  public:
UserComparatorWrapper(const Comparator * const user_cmp)20   explicit UserComparatorWrapper(const Comparator* const user_cmp)
21       : user_comparator_(user_cmp) {}
22 
23   ~UserComparatorWrapper() = default;
24 
user_comparator()25   const Comparator* user_comparator() const { return user_comparator_; }
26 
Compare(const Slice & a,const Slice & b)27   int Compare(const Slice& a, const Slice& b) const override {
28     PERF_COUNTER_ADD(user_key_comparison_count, 1);
29     return user_comparator_->Compare(a, b);
30   }
31 
Equal(const Slice & a,const Slice & b)32   bool Equal(const Slice& a, const Slice& b) const override {
33     PERF_COUNTER_ADD(user_key_comparison_count, 1);
34     return user_comparator_->Equal(a, b);
35   }
36 
Name()37   const char* Name() const override { return user_comparator_->Name(); }
38 
FindShortestSeparator(std::string * start,const Slice & limit)39   void FindShortestSeparator(std::string* start,
40                              const Slice& limit) const override {
41     return user_comparator_->FindShortestSeparator(start, limit);
42   }
43 
FindShortSuccessor(std::string * key)44   void FindShortSuccessor(std::string* key) const override {
45     return user_comparator_->FindShortSuccessor(key);
46   }
47 
GetRootComparator()48   const Comparator* GetRootComparator() const override {
49     return user_comparator_->GetRootComparator();
50   }
51 
IsSameLengthImmediateSuccessor(const Slice & s,const Slice & t)52   bool IsSameLengthImmediateSuccessor(const Slice& s,
53                                       const Slice& t) const override {
54     return user_comparator_->IsSameLengthImmediateSuccessor(s, t);
55   }
56 
CanKeysWithDifferentByteContentsBeEqual()57   bool CanKeysWithDifferentByteContentsBeEqual() const override {
58     return user_comparator_->CanKeysWithDifferentByteContentsBeEqual();
59   }
60 
61  private:
62   const Comparator* user_comparator_;
63 };
64 
65 }  // namespace ROCKSDB_NAMESPACE
66