point_lock_tracker.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #pragma once
  6. #include <memory>
  7. #include <string>
  8. #include <unordered_map>
  9. #include "utilities/transactions/lock/lock_tracker.h"
  10. namespace ROCKSDB_NAMESPACE {
  11. struct TrackedKeyInfo {
  12. // Earliest sequence number that is relevant to this transaction for this key
  13. SequenceNumber seq;
  14. uint32_t num_writes;
  15. uint32_t num_reads;
  16. bool exclusive;
  17. explicit TrackedKeyInfo(SequenceNumber seq_no)
  18. : seq(seq_no), num_writes(0), num_reads(0), exclusive(false) {}
  19. void Merge(const TrackedKeyInfo& info) {
  20. assert(seq <= info.seq);
  21. num_reads += info.num_reads;
  22. num_writes += info.num_writes;
  23. exclusive = exclusive || info.exclusive;
  24. }
  25. };
  26. using TrackedKeyInfos = std::unordered_map<std::string, TrackedKeyInfo>;
  27. using TrackedKeys = std::unordered_map<ColumnFamilyId, TrackedKeyInfos>;
  28. // Tracks point locks on single keys.
  29. class PointLockTracker : public LockTracker {
  30. public:
  31. PointLockTracker() = default;
  32. PointLockTracker(const PointLockTracker&) = delete;
  33. PointLockTracker& operator=(const PointLockTracker&) = delete;
  34. bool IsPointLockSupported() const override { return true; }
  35. bool IsRangeLockSupported() const override { return false; }
  36. void Track(const PointLockRequest& lock_request) override;
  37. UntrackStatus Untrack(const PointLockRequest& lock_request) override;
  38. void Track(const RangeLockRequest& /*lock_request*/) override {}
  39. UntrackStatus Untrack(const RangeLockRequest& /*lock_request*/) override {
  40. return UntrackStatus::NOT_TRACKED;
  41. }
  42. void Merge(const LockTracker& tracker) override;
  43. void Subtract(const LockTracker& tracker) override;
  44. void Clear() override;
  45. LockTracker* GetTrackedLocksSinceSavePoint(
  46. const LockTracker& save_point_tracker) const override;
  47. PointLockStatus GetPointLockStatus(ColumnFamilyId column_family_id,
  48. const std::string& key) const override;
  49. uint64_t GetNumPointLocks() const override;
  50. ColumnFamilyIterator* GetColumnFamilyIterator() const override;
  51. KeyIterator* GetKeyIterator(ColumnFamilyId column_family_id) const override;
  52. private:
  53. TrackedKeys tracked_keys_;
  54. };
  55. class PointLockTrackerFactory : public LockTrackerFactory {
  56. public:
  57. static const PointLockTrackerFactory& Get() {
  58. static const PointLockTrackerFactory instance;
  59. return instance;
  60. }
  61. LockTracker* Create() const override { return new PointLockTracker(); }
  62. private:
  63. PointLockTrackerFactory() {}
  64. };
  65. } // namespace ROCKSDB_NAMESPACE