MySQL 9.4.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
kv_store.h
Go to the documentation of this file.
1/* Copyright (c) 2020, 2025, Oracle and/or its affiliates.
2
3This program is free software; you can redistribute it and/or modify it under
4the terms of the GNU General Public License, version 2.0, as published by the
5Free Software Foundation.
6
7This program is designed to work with certain software (including
8but not limited to OpenSSL) that is licensed under separate terms,
9as designated in a particular file or component or in included license
10documentation. The authors of MySQL hereby grant you an additional
11permission to link the program and your derivative works with the
12separately licensed software that they have either included with
13the program or referenced in the documentation.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
18for more details.
19
20You should have received a copy of the GNU General Public License along with
21this program; if not, write to the Free Software Foundation, Inc.,
2251 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24/** @file storage/temptable/include/temptable/kv_store.h
25TempTable key-value store implementation. */
26
27#ifndef TEMPTABLE_KV_STORE_H
28#define TEMPTABLE_KV_STORE_H
29
30#include <shared_mutex>
31#include <string>
32#include <unordered_map>
33
34#include "my_thread_local.h" // my_thread_id
37
38namespace temptable {
39
40/** Forward-declarations. */
41class Table;
42
43/** Key-value store, a convenience wrapper class which models a thread-safe
44 * dictionary type.
45 *
46 * Thread-safety is accomplished by using a `Lock` which can be any of the usual
47 * mutual exclusive algorithms from C++ thread-support library or any other
48 * which satisfy C++ concurrency named requirements. E.g. mutex, timed_mutex,
49 * recursive_mutex, recursive_timed_mutex, shared_mutex, shared_timed_mutex, ...
50 * User code can opt-in for any of those. Also, whether the actual
51 * implementation will use shared-locks or exclusive-locks (for read-only
52 * operations) is handled automagically by this class.
53 *
54 * Furthermore, user code can similarly opt-in for different key-value
55 * implementation but whose interface is compatible with the one of
56 * std::unordered_map.
57 * */
58template <typename Lock,
59 template <typename...> class KeyValueImpl = std::unordered_map>
61 : public Key_value_store_logger<Key_value_store<Lock, KeyValueImpl>,
62 DEBUG_BUILD> {
63 /** Do not break encapsulation when using CRTP. */
64 friend class Key_value_store_logger<Key_value_store<Lock, KeyValueImpl>,
66
67 /** Help ADL to bring debugging/logging symbols into the scope. */
72
73 /** Check whether we can use shared locks (which enable multiple concurrent
74 * readers) or must we rather fallback to exclusive locks. Shared-locks will
75 * be used only during read-only operations.
76 * */
79 std::shared_lock<Lock>, std::lock_guard<Lock>>;
80
81 /** Alias for our key-value store implementation. */
82 using Key_value_store_impl = KeyValueImpl<std::string, Table>;
83
84 /** Container holding (table-name, Table) tuples. */
86
87 /** Lock type. */
88 Lock m_lock;
89
90 public:
91 /** Inserts a new table into the container constructed in-place with the
92 * given args if there is no table with the key in the container.
93 *
94 * [in] args Arguments to forward to the constructor of the table.
95 * @return Returns a pair consisting of an iterator to the inserted table,
96 * or the already-existing table if no insertion happened, and a bool
97 * denoting whether the insertion took place (true if insertion happened,
98 * false if it did not).
99 * */
100 template <class... Args>
101 std::pair<typename Key_value_store_impl::iterator, bool> emplace(
102 Args &&...args) {
103 std::lock_guard<Lock> lock(m_lock);
104 dbug_print();
106 return m_kv_store.emplace(args...);
107 }
108
109 /** Searches for a table with given name (key).
110 *
111 * [in] key Name of a table to search for.
112 * @return Pointer to table if found, nullptr otherwise.
113 * */
114 Table *find(const std::string &key) {
116 auto iter = m_kv_store.find(key);
117 if (iter != m_kv_store.end()) {
118 return &iter->second;
119 } else {
120 return nullptr;
121 }
122 }
123
124 /** Searches for all tables tagged with the given owner ID.
125 *
126 * [in] id ID of the thread whose temptables we're looking for.
127 * @return Vector containing the names of all temptables owned by this thread.
128 * */
129 std::vector<std::string> find_all(my_thread_id id) {
131 std::vector<std::string> tables;
132 for (auto &[name, table] : m_kv_store) {
133 if (table.owner_id() == id) {
134 tables.push_back(name);
135 }
136 }
137 return tables;
138 }
139
140 /** Removes the table (if one exists) with the given name (key).
141 *
142 * [in] key Name of a table to remove..
143 * @return Number of elements removed.
144 * */
145 typename Key_value_store_impl::size_type erase(const std::string &key) {
146 std::lock_guard<Lock> lock(m_lock);
147 dbug_print();
149 return m_kv_store.erase(key);
150 }
151};
152
153} // namespace temptable
154
155#endif /* TEMPTABLE_KV_STORE_H */
Default Key_value_store logging facility which turns to no-op in non-debug builds.
Definition: kv_store_logger.h:42
void log(Key_value_store_stats::Event)
Definition: kv_store_logger.h:44
Key-value store, a convenience wrapper class which models a thread-safe dictionary type.
Definition: kv_store.h:62
std::pair< typename Key_value_store_impl::iterator, bool > emplace(Args &&...args)
Inserts a new table into the container constructed in-place with the given args if there is no table ...
Definition: kv_store.h:101
std::conditional_t< std::is_same< Lock, std::shared_timed_mutex >::value, std::shared_lock< Lock >, std::lock_guard< Lock > > Exclusive_or_shared_lock
Check whether we can use shared locks (which enable multiple concurrent readers) or must we rather fa...
Definition: kv_store.h:79
Key_value_store_impl m_kv_store
Container holding (table-name, Table) tuples.
Definition: kv_store.h:85
KeyValueImpl< std::string, Table > Key_value_store_impl
Alias for our key-value store implementation.
Definition: kv_store.h:82
std::vector< std::string > find_all(my_thread_id id)
Searches for all tables tagged with the given owner ID.
Definition: kv_store.h:129
Key_value_store_impl::size_type erase(const std::string &key)
Removes the table (if one exists) with the given name (key).
Definition: kv_store.h:145
Table * find(const std::string &key)
Searches for a table with given name (key).
Definition: kv_store.h:114
Lock m_lock
Lock type.
Definition: kv_store.h:88
Definition: table.h:47
TempTable key-value store logger implementation.
uint32 my_thread_id
Definition: my_thread_local.h:34
static PFS_engine_table_share_proxy table
Definition: pfs.cc:61
ValueType value(const std::optional< ValueType > &v)
Definition: gtid.h:83
Definition: allocator.h:48
constexpr bool DEBUG_BUILD
Store build-type information into the constexpr expression.
Definition: constants.h:76
std::unordered_map< Key, Value, Hash, Key_equal, ut::allocator< std::pair< const Key, Value > > > unordered_map
Definition: ut0new.h:2900
static std::mutex lock
Definition: net_ns.cc:56
required string key
Definition: replication_asynchronous_connection_failover.proto:60
case opt name
Definition: sslopt-case.h:29
TempTable constants.