MySQL 8.0.39
Source Code Documentation
auth_utility.h
Go to the documentation of this file.
1/* Copyright (c) 2018, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23/* Internals */
24
25#ifndef AUTH_UTILITY_INCLUDED
26#define AUTH_UTILITY_INCLUDED
27
29#include "rwlock_scoped_lock.h"
30
31#include <map>
32
33/**
34 Map with RWLock protections
35*/
36template <typename K, typename V>
38 public:
45 m_map.clear();
47 }
48
49 /**
50 Search an entry
51
52 @param [in] key Key
53 @param [out] value Value
54
55 @returns status of search
56 @retval false Entry not found
57 @retval true Entry found. Check value.
58 */
59 bool find(const K &key, V &value) {
60 rwlock_scoped_lock rdlock(&m_lock, false, __FILE__, __LINE__);
61 const auto search_itr = m_map.find(key);
62 if (search_itr != m_map.end()) {
63 value = search_itr->second;
64 return true;
65 }
66 return false;
67 }
68
69 /**
70 Insert an entry
71
72 @param [in] key Key
73 @param [in] value Value
74
75 @returns status of insertion
76 @retval false Error
77 @retval true Success
78 */
79 bool insert(K key, V value) {
80 rwlock_scoped_lock wrlock(&m_lock, true, __FILE__, __LINE__);
81 auto returns = m_map.insert(std::make_pair(key, value));
82 return returns.second;
83 }
84
85 /**
86 Delete an entry
87
88 @param [in] key Key to the element to be erased
89 */
90 void erase(K key) {
91 rwlock_scoped_lock wrlock(&m_lock, true, __FILE__, __LINE__);
92 m_map.erase(key);
93 }
94
95 /**
96 Check limit and clear if needed
97
98 @param [in] size Limit to check against
99 */
100 void clear_if_greater(size_t size) {
101 rwlock_scoped_lock wrlock(&m_lock, true, __FILE__, __LINE__);
102 if (m_map.size() >= size) m_map.clear();
103 }
104
105 /** Get the size of the map */
106 size_t size() {
107 rwlock_scoped_lock rdlock(&m_lock, false, __FILE__, __LINE__);
108 return m_map.size();
109 }
110
111 private:
112 /** Map */
113 std::map<K, V> m_map;
114 /** Lock to protect m_map */
116};
117
118#endif /* AUTH_UTILITY_INCLUDED */
Map with RWLock protections.
Definition: auth_utility.h:37
size_t size()
Get the size of the map.
Definition: auth_utility.h:106
Map_with_rw_lock(const Map_with_rw_lock &)=delete
~Map_with_rw_lock()
Definition: auth_utility.h:44
void clear_if_greater(size_t size)
Check limit and clear if needed.
Definition: auth_utility.h:100
void erase(K key)
Delete an entry.
Definition: auth_utility.h:90
mysql_rwlock_t m_lock
Lock to protect m_map.
Definition: auth_utility.h:115
Map_with_rw_lock & operator=(Map_with_rw_lock &&)=delete
std::map< K, V > m_map
Map.
Definition: auth_utility.h:113
Map_with_rw_lock(PSI_rwlock_key key)
Definition: auth_utility.h:39
Map_with_rw_lock & operator=(const Map_with_rw_lock &)=delete
bool find(const K &key, V &value)
Search an entry.
Definition: auth_utility.h:59
Map_with_rw_lock(Map_with_rw_lock &&)=delete
bool insert(K key, V value)
Insert an entry.
Definition: auth_utility.h:79
Locks RW-lock and releases lock on scope exit.
Definition: rwlock_scoped_lock.h:32
#define mysql_rwlock_init(K, T)
Definition: mysql_rwlock.h:41
#define mysql_rwlock_destroy(T)
Definition: mysql_rwlock.h:51
unsigned int PSI_rwlock_key
Instrumented rwlock key.
Definition: psi_rwlock_bits.h:44
Instrumentation helpers for rwlock.
required string key
Definition: replication_asynchronous_connection_failover.proto:60
An instrumented rwlock structure.
Definition: mysql_rwlock_bits.h:51