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 
6 #pragma once
7 #ifndef ROCKSDB_LITE
8 
9 #include "rocksdb/types.h"
10 #include "rocksdb/utilities/transaction.h"
11 #include "rocksdb/utilities/transaction_db.h"
12 #include "utilities/transactions/lock/lock_tracker.h"
13 #include "utilities/transactions/pessimistic_transaction.h"
14 
15 namespace ROCKSDB_NAMESPACE {
16 
17 class PessimisticTransactionDB;
18 
19 class LockManager {
20  public:
~LockManager()21   virtual ~LockManager() {}
22 
23   // Whether supports locking a specific key.
24   virtual bool IsPointLockSupported() const = 0;
25 
26   // Whether supports locking a range of keys.
27   virtual bool IsRangeLockSupported() const = 0;
28 
29   // Locks acquired through this LockManager should be tracked by
30   // the LockTrackers created through the returned factory.
31   virtual const LockTrackerFactory& GetLockTrackerFactory() const = 0;
32 
33   // Enable locking for the specified column family.
34   // Caller should guarantee that this column family is not already enabled.
35   virtual void AddColumnFamily(const ColumnFamilyHandle* cf) = 0;
36 
37   // Disable locking for the specified column family.
38   // Caller should guarantee that this column family is no longer used.
39   virtual void RemoveColumnFamily(const ColumnFamilyHandle* cf) = 0;
40 
41   // Attempt to lock a key or a key range.  If OK status is returned, the caller
42   // is responsible for calling UnLock() on this key.
43   virtual Status TryLock(PessimisticTransaction* txn,
44                          ColumnFamilyId column_family_id,
45                          const std::string& key, Env* env, bool exclusive) = 0;
46   // The range [start, end] are inclusive at both sides.
47   virtual Status TryLock(PessimisticTransaction* txn,
48                          ColumnFamilyId column_family_id, const Endpoint& start,
49                          const Endpoint& end, Env* env, bool exclusive) = 0;
50 
51   // Unlock a key or a range locked by TryLock().  txn must be the same
52   // Transaction that locked this key.
53   virtual void UnLock(PessimisticTransaction* txn, const LockTracker& tracker,
54                       Env* env) = 0;
55   virtual void UnLock(PessimisticTransaction* txn,
56                       ColumnFamilyId column_family_id, const std::string& key,
57                       Env* env) = 0;
58   virtual void UnLock(PessimisticTransaction* txn,
59                       ColumnFamilyId column_family_id, const Endpoint& start,
60                       const Endpoint& end, Env* env) = 0;
61 
62   using PointLockStatus = std::unordered_multimap<ColumnFamilyId, KeyLockInfo>;
63   virtual PointLockStatus GetPointLockStatus() = 0;
64 
65   using RangeLockStatus =
66       std::unordered_multimap<ColumnFamilyId, RangeLockInfo>;
67   virtual RangeLockStatus GetRangeLockStatus() = 0;
68 
69   virtual std::vector<DeadlockPath> GetDeadlockInfoBuffer() = 0;
70 
71   virtual void Resize(uint32_t new_size) = 0;
72 };
73 
74 // LockManager should always be constructed through this factory method,
75 // instead of constructing through concrete implementations' constructor.
76 // Caller owns the returned pointer.
77 std::shared_ptr<LockManager> NewLockManager(PessimisticTransactionDB* db,
78                                             const TransactionDBOptions& opt);
79 
80 }  // namespace ROCKSDB_NAMESPACE
81 
82 #endif  // ROCKSDB_LITE
83