MySQL 9.0.0
Source Code Documentation
kv_store.h
Go to the documentation of this file.
1/* Copyright (c) 2020, 2024, 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
36
37namespace temptable {
38
39/** Forward-declarations. */
40class Table;
41
42/** Key-value store, a convenience wrapper class which models a thread-safe
43 * dictionary type.
44 *
45 * Thread-safety is accomplished by using a `Lock` which can be any of the usual
46 * mutual exclusive algorithms from C++ thread-support library or any other
47 * which satisfy C++ concurrency named requirements. E.g. mutex, timed_mutex,
48 * recursive_mutex, recursive_timed_mutex, shared_mutex, shared_timed_mutex, ...
49 * User code can opt-in for any of those. Also, whether the actual
50 * implementation will use shared-locks or exclusive-locks (for read-only
51 * operations) is handled automagically by this class.
52 *
53 * Furthermore, user code can similarly opt-in for different key-value
54 * implementation but whose interface is compatible with the one of
55 * std::unordered_map.
56 * */
57template <typename Lock,
58 template <typename...> class KeyValueImpl = std::unordered_map>
60 : public Key_value_store_logger<Key_value_store<Lock, KeyValueImpl>,
61 DEBUG_BUILD> {
62 /** Do not break encapsulation when using CRTP. */
63 friend class Key_value_store_logger<Key_value_store<Lock, KeyValueImpl>,
65
66 /** Help ADL to bring debugging/logging symbols into the scope. */
71
72 /** Check whether we can use shared locks (which enable multiple concurrent
73 * readers) or must we rather fallback to exclusive locks. Shared-locks will
74 * be used only during read-only operations.
75 * */
77 std::conditional_t<std::is_same<Lock, std::shared_timed_mutex>::value,
78 std::shared_lock<Lock>, std::lock_guard<Lock>>;
79
80 /** Alias for our key-value store implementation. */
81 using Key_value_store_impl = KeyValueImpl<std::string, Table>;
82
83 /** Container holding (table-name, Table) tuples. */
85
86 /** Lock type. */
87 Lock m_lock;
88
89 public:
90 /** Inserts a new table into the container constructed in-place with the
91 * given args if there is no table with the key in the container.
92 *
93 * [in] args Arguments to forward to the constructor of the table.
94 * @return Returns a pair consisting of an iterator to the inserted table,
95 * or the already-existing table if no insertion happened, and a bool
96 * denoting whether the insertion took place (true if insertion happened,
97 * false if it did not).
98 * */
99 template <class... Args>
100 std::pair<typename Key_value_store_impl::iterator, bool> emplace(
101 Args &&... args) {
102 std::lock_guard<Lock> lock(m_lock);
103 dbug_print();
105 return m_kv_store.emplace(args...);
106 }
107
108 /** Searches for a table with given name (key).
109 *
110 * [in] key Name of a table to search for.
111 * @return Pointer to table if found, nullptr otherwise.
112 * */
113 Table *find(const std::string &key) {
115 auto iter = m_kv_store.find(key);
116 if (iter != m_kv_store.end()) {
117 return &iter->second;
118 } else {
119 return nullptr;
120 }
121 }
122
123 /** Removes the table (if one exists) with the given name (key).
124 *
125 * [in] key Name of a table to remove..
126 * @return Number of elements removed.
127 * */
128 typename Key_value_store_impl::size_type erase(const std::string &key) {
129 std::lock_guard<Lock> lock(m_lock);
130 dbug_print();
132 return m_kv_store.erase(key);
133 }
134};
135
136} // namespace temptable
137
138#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:61
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:78
Key_value_store_impl m_kv_store
Container holding (table-name, Table) tuples.
Definition: kv_store.h:84
KeyValueImpl< std::string, Table > Key_value_store_impl
Alias for our key-value store implementation.
Definition: kv_store.h:81
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:128
Table * find(const std::string &key)
Searches for a table with given name (key).
Definition: kv_store.h:113
Lock m_lock
Lock type.
Definition: kv_store.h:87
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:100
Definition: table.h:47
TempTable key-value store logger implementation.
Definition: allocator.h:45
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:2899
static std::mutex lock
Definition: net_ns.cc:56
required string key
Definition: replication_asynchronous_connection_failover.proto:60
TempTable constants.