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 DictCache_H
26 #define DictCache_H
27 
28 #include <ndb_types.h>
29 #include <NdbError.hpp>
30 #include <BaseString.hpp>
31 #include <Vector.hpp>
32 #include <UtilBuffer.hpp>
33 #include <NdbDictionary.hpp>
34 #include <Ndb.hpp>
35 #include <NdbCondition.h>
36 #include "NdbLinHash.hpp"
37 
38 class Ndb_local_table_info {
39 public:
40   static Ndb_local_table_info *create(NdbTableImpl *table_impl, Uint32 sz=0);
41   static void destroy(Ndb_local_table_info *);
42   NdbTableImpl *m_table_impl;
43 
44   // range of cached tuple ids per thread
45   Ndb::TupleIdRange m_tuple_id_range;
46 
47   Uint64 m_local_data[1]; // Must be last member. Used to access extra space.
48 private:
49   Ndb_local_table_info(NdbTableImpl *table_impl);
50   ~Ndb_local_table_info();
51 };
52 
53 /**
54  * A non thread safe dict cache
55  */
56 class LocalDictCache {
57 public:
58   LocalDictCache();
59   ~LocalDictCache();
60 
61   Ndb_local_table_info * get(const char * name);
62 
63   void put(const char * name, Ndb_local_table_info *);
64   void drop(const char * name);
65 
66   NdbLinHash<Ndb_local_table_info> m_tableHash; // On name
67 };
68 
69 /**
70  * A thread safe dict cache
71  */
72 class GlobalDictCache : public NdbLockable {
73 public:
74   GlobalDictCache();
75   ~GlobalDictCache();
76 
77   NdbTableImpl * get(const char * name, int *error);
78 
79   NdbTableImpl* put(const char * name, NdbTableImpl *);
80   void release(const NdbTableImpl *, int invalidate = 0);
81 
82   void alter_table_rep(const char * name,
83 		       Uint32 tableId, Uint32 tableVersion, bool altered);
84 
85   unsigned get_size();
86   void invalidate_all();
87 
88   // update reference count by +1 or -1
inc_ref_count(const NdbTableImpl * impl)89   int inc_ref_count(const NdbTableImpl * impl) {
90     return chg_ref_count(impl, +1);
91   }
dec_ref_count(const NdbTableImpl * impl)92   int dec_ref_count(const NdbTableImpl * impl) {
93     return chg_ref_count(impl, -1);
94   }
95 
96   void invalidateDb(const char * name, size_t len);
97 
98 public:
99   enum Status {
100     OK = 0,
101     DROPPED = 1,
102     RETREIVING = 2
103   };
104 
105 private:
106   void printCache();
107   int chg_ref_count(const NdbTableImpl *, int value);
108 
109   struct TableVersion {
110     Uint32 m_version;
111     Uint32 m_refCount;
112     NdbTableImpl * m_impl;
113     Status m_status;
114   };
115 
116   NdbLinHash<Vector<TableVersion> > m_tableHash;
117   NdbCondition * m_waitForTableCondition;
118 };
119 
120 #endif
121 
122 
123