MySQL 8.3.0
Source Code Documentation
kv_store.h
Go to the documentation of this file.
1/* Copyright (c) 2020, 2023, 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 also distributed with certain software (including but not
8limited to OpenSSL) that is licensed under separate terms, as designated in a
9particular file or component or in included license documentation. The authors
10of MySQL hereby grant you an additional permission to link the program and
11your derivative works with the separately licensed software that they have
12included with MySQL.
13
14This program is distributed in the hope that it will be useful, but WITHOUT
15ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
17for more details.
18
19You should have received a copy of the GNU General Public License along with
20this program; if not, write to the Free Software Foundation, Inc.,
2151 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23/** @file storage/temptable/include/temptable/kv_store.h
24TempTable key-value store implementation. */
25
26#ifndef TEMPTABLE_KV_STORE_H
27#define TEMPTABLE_KV_STORE_H
28
29#include <shared_mutex>
30#include <string>
31#include <unordered_map>
32
35
36namespace temptable {
37
38/** Forward-declarations. */
39class Table;
40
41/** Key-value store, a convenience wrapper class which models a thread-safe
42 * dictionary type.
43 *
44 * Thread-safety is accomplished by using a `Lock` which can be any of the usual
45 * mutual exclusive algorithms from C++ thread-support library or any other
46 * which satisfy C++ concurrency named requirements. E.g. mutex, timed_mutex,
47 * recursive_mutex, recursive_timed_mutex, shared_mutex, shared_timed_mutex, ...
48 * User code can opt-in for any of those. Also, whether the actual
49 * implementation will use shared-locks or exclusive-locks (for read-only
50 * operations) is handled automagically by this class.
51 *
52 * Furthermore, user code can similarly opt-in for different key-value
53 * implementation but whose interface is compatible with the one of
54 * std::unordered_map.
55 * */
56template <typename Lock,
57 template <typename...> class KeyValueImpl = std::unordered_map>
59 : public Key_value_store_logger<Key_value_store<Lock, KeyValueImpl>,
60 DEBUG_BUILD> {
61 /** Do not break encapsulation when using CRTP. */
62 friend class Key_value_store_logger<Key_value_store<Lock, KeyValueImpl>,
64
65 /** Help ADL to bring debugging/logging symbols into the scope. */
70
71 /** Check whether we can use shared locks (which enable multiple concurrent
72 * readers) or must we rather fallback to exclusive locks. Shared-locks will
73 * be used only during read-only operations.
74 * */
76 std::conditional_t<std::is_same<Lock, std::shared_timed_mutex>::value,
77 std::shared_lock<Lock>, std::lock_guard<Lock>>;
78
79 /** Alias for our key-value store implementation. */
80 using Key_value_store_impl = KeyValueImpl<std::string, Table>;
81
82 /** Container holding (table-name, Table) tuples. */
84
85 /** Lock type. */
86 Lock m_lock;
87
88 public:
89 /** Inserts a new table into the container constructed in-place with the
90 * given args if there is no table with the key in the container.
91 *
92 * [in] args Arguments to forward to the constructor of the table.
93 * @return Returns a pair consisting of an iterator to the inserted table,
94 * or the already-existing table if no insertion happened, and a bool
95 * denoting whether the insertion took place (true if insertion happened,
96 * false if it did not).
97 * */
98 template <class... Args>
99 std::pair<typename Key_value_store_impl::iterator, bool> emplace(
100 Args &&... args) {
101 std::lock_guard<Lock> lock(m_lock);
102 dbug_print();
104 return m_kv_store.emplace(args...);
105 }
106
107 /** Searches for a table with given name (key).
108 *
109 * [in] key Name of a table to search for.
110 * @return Pointer to table if found, nullptr otherwise.
111 * */
112 Table *find(const std::string &key) {
114 auto iter = m_kv_store.find(key);
115 if (iter != m_kv_store.end()) {
116 return &iter->second;
117 } else {
118 return nullptr;
119 }
120 }
121
122 /** Removes the table (if one exists) with the given name (key).
123 *
124 * [in] key Name of a table to remove..
125 * @return Number of elements removed.
126 * */
127 typename Key_value_store_impl::size_type erase(const std::string &key) {
128 std::lock_guard<Lock> lock(m_lock);
129 dbug_print();
131 return m_kv_store.erase(key);
132 }
133};
134
135} // namespace temptable
136
137#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:41
void log(Key_value_store_stats::Event)
Definition: kv_store_logger.h:43
Key-value store, a convenience wrapper class which models a thread-safe dictionary type.
Definition: kv_store.h:60
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:77
Key_value_store_impl m_kv_store
Container holding (table-name, Table) tuples.
Definition: kv_store.h:83
KeyValueImpl< std::string, Table > Key_value_store_impl
Alias for our key-value store implementation.
Definition: kv_store.h:80
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:127
Table * find(const std::string &key)
Searches for a table with given name (key).
Definition: kv_store.h:112
Lock m_lock
Lock type.
Definition: kv_store.h:86
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:99
Definition: table.h:46
TempTable key-value store logger implementation.
Definition: allocator.h:44
constexpr bool DEBUG_BUILD
Store build-type information into the constexpr expression.
Definition: constants.h:75
std::unordered_map< Key, Value, Hash, Key_equal, ut::allocator< std::pair< const Key, Value > > > unordered_map
Definition: ut0new.h:2897
static std::mutex lock
Definition: net_ns.cc:55
required string key
Definition: replication_asynchronous_connection_failover.proto:59
TempTable constants.