/Users/deen/code/yugabyte-db/src/yb/docdb/lock_batch.h
Line | Count | Source |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | // |
13 | | |
14 | | #ifndef YB_DOCDB_LOCK_BATCH_H |
15 | | #define YB_DOCDB_LOCK_BATCH_H |
16 | | |
17 | | #include <string> |
18 | | |
19 | | #include <glog/logging.h> |
20 | | |
21 | | #include "yb/docdb/docdb_fwd.h" |
22 | | #include "yb/docdb/intent.h" |
23 | | |
24 | | #include "yb/util/monotime.h" |
25 | | #include "yb/util/ref_cnt_buffer.h" |
26 | | #include "yb/util/status.h" |
27 | | |
28 | | namespace yb { |
29 | | |
30 | | class RefCntPrefix; |
31 | | |
32 | | namespace docdb { |
33 | | |
34 | | class SharedLockManager; |
35 | | |
36 | | // We don't care about actual content of this struct here, since it is an implementation detail |
37 | | // of SharedLockManager. |
38 | | struct LockedBatchEntry; |
39 | | |
40 | | struct LockBatchEntry { |
41 | | RefCntPrefix key; |
42 | | IntentTypeSet intent_types; |
43 | | |
44 | | // Memory is owned by SharedLockManager. |
45 | | LockedBatchEntry* locked = nullptr; |
46 | | |
47 | | std::string ToString() const; |
48 | | }; |
49 | | |
50 | | // A LockBatch encapsulates a mapping from lock keys to lock types (intent types) to be acquired |
51 | | // for each key. It also keeps track of a lock manager when locked, and auto-releases the locks |
52 | | // in the destructor. |
53 | | class LockBatch { |
54 | | public: |
55 | 9.77M | LockBatch() {} |
56 | | LockBatch(SharedLockManager* lock_manager, LockBatchEntries&& key_to_intent_type, |
57 | | CoarseTimePoint deadline); |
58 | 6.49M | LockBatch(LockBatch&& other) { MoveFrom(&other); } |
59 | 9.61M | LockBatch& operator=(LockBatch&& other) { MoveFrom(&other); return *this; } |
60 | | ~LockBatch(); |
61 | | |
62 | | // This class is move-only. |
63 | | LockBatch(const LockBatch&) = delete; |
64 | | LockBatch& operator=(const LockBatch&) = delete; |
65 | | |
66 | | // @return the number of keys in this batch |
67 | 8 | size_t size() const { return data_.key_to_type.size(); } |
68 | | |
69 | | // @return whether the batch is empty. This is also used for checking if the batch is locked. |
70 | 51.4M | bool empty() const { return data_.key_to_type.empty(); } |
71 | | |
72 | 3.24M | const Status& status() const { return data_.status; } |
73 | | |
74 | | // Unlocks this batch if it is non-empty. |
75 | | void Reset(); |
76 | | |
77 | | private: |
78 | | void MoveFrom(LockBatch* other); |
79 | | |
80 | | struct Data { |
81 | 16.2M | Data() = default; |
82 | | Data(LockBatchEntries&& key_to_type_, SharedLockManager* shared_lock_manager_) : |
83 | 7.93M | key_to_type(std::move(key_to_type_)), shared_lock_manager(shared_lock_manager_) {} |
84 | | |
85 | | Data(Data&&) = default; |
86 | 16.0M | Data& operator=(Data&& other) = default; |
87 | | |
88 | | Data(const Data&) = delete; |
89 | | Data& operator=(const Data&) = delete; |
90 | | |
91 | | LockBatchEntries key_to_type; |
92 | | |
93 | | SharedLockManager* shared_lock_manager = nullptr; |
94 | | |
95 | | Status status; |
96 | | }; |
97 | | |
98 | | Data data_; |
99 | | }; |
100 | | |
101 | | } // namespace docdb |
102 | | } // namespace yb |
103 | | |
104 | | #endif // YB_DOCDB_LOCK_BATCH_H |