user_comparator_wrapper.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 {
  15. public:
  16. // `UserComparatorWrapper`s constructed with the default constructor are not
  17. // usable and will segfault on any attempt to use them for comparisons.
  18. UserComparatorWrapper() : user_comparator_(nullptr) {}
  19. explicit UserComparatorWrapper(const Comparator* const user_cmp)
  20. : user_comparator_(user_cmp) {}
  21. ~UserComparatorWrapper() = default;
  22. const Comparator* user_comparator() const { return user_comparator_; }
  23. int Compare(const Slice& a, const Slice& b) const {
  24. PERF_COUNTER_ADD(user_key_comparison_count, 1);
  25. return user_comparator_->Compare(a, b);
  26. }
  27. bool Equal(const Slice& a, const Slice& b) const {
  28. PERF_COUNTER_ADD(user_key_comparison_count, 1);
  29. return user_comparator_->Equal(a, b);
  30. }
  31. int CompareTimestamp(const Slice& ts1, const Slice& ts2) const {
  32. return user_comparator_->CompareTimestamp(ts1, ts2);
  33. }
  34. int CompareWithoutTimestamp(const Slice& a, const Slice& b) const {
  35. PERF_COUNTER_ADD(user_key_comparison_count, 1);
  36. return user_comparator_->CompareWithoutTimestamp(a, b);
  37. }
  38. int CompareWithoutTimestamp(const Slice& a, bool a_has_ts, const Slice& b,
  39. bool b_has_ts) const {
  40. PERF_COUNTER_ADD(user_key_comparison_count, 1);
  41. return user_comparator_->CompareWithoutTimestamp(a, a_has_ts, b, b_has_ts);
  42. }
  43. bool EqualWithoutTimestamp(const Slice& a, const Slice& b) const {
  44. return user_comparator_->EqualWithoutTimestamp(a, b);
  45. }
  46. private:
  47. const Comparator* user_comparator_;
  48. };
  49. } // namespace ROCKSDB_NAMESPACE