1 /*
2    Copyright (c) 2003, 2021, Oracle and/or its affiliates.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
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 */
24 
25 #ifndef KEY_TABLE2_HPP
26 #define KEY_TABLE2_HPP
27 
28 #include <DLHashTable2.hpp>
29 
30 #define JAM_FILE_ID 258
31 
32 
33 /**
34  * KeyTable2 is DLHashTable2 with hardcoded Uint32 key named "key".
35  */
36 template <class T, class U>
37 class KeyTable2 : public DLHashTable2<T, U> {
38 public:
KeyTable2(ArrayPool<U> & pool)39   KeyTable2(ArrayPool<U>& pool) :
40     DLHashTable2<T, U>(pool) {
41   }
42 
find(Ptr<T> & ptr,const T & rec) const43   bool find(Ptr<T>& ptr, const T& rec) const {
44     return DLHashTable2<T, U>::find(ptr, rec);
45   }
46 
find(Ptr<T> & ptr,Uint32 key) const47   bool find(Ptr<T>& ptr, Uint32 key) const {
48     T rec;
49     rec.key = key;
50     return DLHashTable2<T, U>::find(ptr, rec);
51   }
52 };
53 
54 template <class T, class U>
55 class KeyTable2C : public KeyTable2<T, U> {
56   Uint32 m_count;
57 public:
KeyTable2C(ArrayPool<U> & pool)58   KeyTable2C(ArrayPool<U>& pool) :
59     KeyTable2<T, U>(pool), m_count(0) {
60   }
61 
get_count() const62   Uint32 get_count() const { return m_count; }
63 
seize(Ptr<T> & ptr)64   bool seize(Ptr<T> & ptr) {
65     if (KeyTable2<T, U>::seize(ptr))
66     {
67       m_count ++;
68       return true;
69     }
70     return false;
71   }
72 
add(Ptr<T> & ptr)73   void add(Ptr<T> & ptr) {
74     KeyTable2<T, U>::add(ptr);
75     m_count ++;
76   }
77 
remove(Ptr<T> & ptr,const T & key)78   void remove(Ptr<T> & ptr, const T & key) {
79     KeyTable2<T, U>::remove(ptr, key);
80     if (ptr.i != RNIL)
81     {
82       assert(m_count);
83       m_count --;
84     }
85   }
86 
remove(Uint32 i)87   void remove(Uint32 i) {
88     KeyTable2<T, U>::remove(i);
89     assert(m_count);
90     m_count --;
91   }
92 
remove(Ptr<T> & ptr)93   void remove(Ptr<T> & ptr) {
94     KeyTable2<T, U>::remove(ptr);
95     assert(m_count);
96     m_count --;
97   }
98 
removeAll()99   void removeAll() {
100     KeyTable2<T, U>::removeAll();
101     m_count = 0;
102   }
103 
release(Ptr<T> & ptr,const T & key)104   void release(Ptr<T> & ptr, const T & key) {
105     KeyTable2<T, U>::release(ptr, key);
106     if (ptr.i != RNIL)
107     {
108       assert(m_count);
109       m_count --;
110     }
111   }
112 
release(Uint32 i)113   void release(Uint32 i) {
114     KeyTable2<T, U>::release(i);
115     assert(m_count);
116     m_count --;
117   }
118 
release(Ptr<T> & ptr)119   void release(Ptr<T> & ptr) {
120     KeyTable2<T, U>::release(ptr);
121     assert(m_count);
122     m_count --;
123   }
124 };
125 
126 
127 #undef JAM_FILE_ID
128 
129 #endif
130