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