user_comparator_wrapper.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. #pragma once
  9. #include "monitoring/perf_context_imp.h"
  10. #include "rocksdb/comparator.h"
  11. namespace ROCKSDB_NAMESPACE {
  12. // Wrapper of user comparator, with auto increment to
  13. // perf_context.user_key_comparison_count.
  14. class UserComparatorWrapper final : public Comparator {
  15. public:
  16. explicit UserComparatorWrapper(const Comparator* const user_cmp)
  17. : user_comparator_(user_cmp) {}
  18. ~UserComparatorWrapper() = default;
  19. const Comparator* user_comparator() const { return user_comparator_; }
  20. int Compare(const Slice& a, const Slice& b) const override {
  21. PERF_COUNTER_ADD(user_key_comparison_count, 1);
  22. return user_comparator_->Compare(a, b);
  23. }
  24. bool Equal(const Slice& a, const Slice& b) const override {
  25. PERF_COUNTER_ADD(user_key_comparison_count, 1);
  26. return user_comparator_->Equal(a, b);
  27. }
  28. const char* Name() const override { return user_comparator_->Name(); }
  29. void FindShortestSeparator(std::string* start,
  30. const Slice& limit) const override {
  31. return user_comparator_->FindShortestSeparator(start, limit);
  32. }
  33. void FindShortSuccessor(std::string* key) const override {
  34. return user_comparator_->FindShortSuccessor(key);
  35. }
  36. const Comparator* GetRootComparator() const override {
  37. return user_comparator_->GetRootComparator();
  38. }
  39. bool IsSameLengthImmediateSuccessor(const Slice& s,
  40. const Slice& t) const override {
  41. return user_comparator_->IsSameLengthImmediateSuccessor(s, t);
  42. }
  43. bool CanKeysWithDifferentByteContentsBeEqual() const override {
  44. return user_comparator_->CanKeysWithDifferentByteContentsBeEqual();
  45. }
  46. private:
  47. const Comparator* user_comparator_;
  48. };
  49. } // namespace ROCKSDB_NAMESPACE