1 /* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
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 Foundation,
21    51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
22 
23 #ifndef TABLE_MAPPING_H
24 #define TABLE_MAPPING_H
25 
26 /* Forward declarations */
27 #ifndef MYSQL_CLIENT
28 struct TABLE;
29 #else
30 class Table_map_log_event;
31 typedef Table_map_log_event TABLE;
32 void free_table_map_log_event(TABLE *table);
33 #endif
34 
35 
36 /*
37   CLASS table_mapping
38 
39   RESPONSIBILITIES
40     The table mapping is used to map table id's to table pointers
41 
42   COLLABORATION
43     RELAY_LOG    For mapping table id:s to tables when receiving events.
44  */
45 
46 /*
47   Guilhem to Mats:
48   in the table_mapping class, the memory is allocated and never freed (until
49   destruction). So this is a good candidate for allocating inside a MEM_ROOT:
50   it gives the efficient allocation in chunks (like in expand()). So I have
51   introduced a MEM_ROOT.
52 
53   Note that inheriting from Sql_alloc had no effect: it has effects only when
54   "ptr= new table_mapping" is called, and this is never called. And it would
55   then allocate from thd->mem_root which is a highly volatile object (reset
56   from example after executing each query, see dispatch_command(), it has a
57   free_root() at end); as the table_mapping object is supposed to live longer
58   than a query, it was dangerous.
59   A dedicated MEM_ROOT needs to be used, see below.
60 */
61 
62 #include "hash.h"                               /* HASH */
63 
64 class table_mapping {
65 
66 private:
67   MEM_ROOT m_mem_root;
68 
69 public:
70 
71   enum enum_error {
72       ERR_NO_ERROR = 0,
73       ERR_LIMIT_EXCEEDED,
74       ERR_MEMORY_ALLOCATION
75   };
76 
77   table_mapping();
78   ~table_mapping();
79 
80   TABLE* get_table(ulonglong table_id);
81 
82   int       set_table(ulonglong table_id, TABLE* table);
83   int       remove_table(ulonglong table_id);
84   void      clear_tables();
count()85   ulong     count() const { return m_table_ids.records; }
86 
87 private:
88   /*
89     This is a POD (Plain Old Data).  Keep it that way (we apply offsetof() to
90     it, which only works for PODs)
91   */
92   struct entry {
93     ulonglong table_id;
94     union {
95       TABLE *table;
96       entry *next;
97     };
98   };
99 
find_entry(ulonglong table_id)100   entry *find_entry(ulonglong table_id)
101   {
102     return (entry *) my_hash_search(&m_table_ids,
103                                     (uchar*)&table_id,
104                                     sizeof(table_id));
105   }
106   int expand();
107 
108   /*
109     Head of the list of free entries; "free" in the sense that it's an
110     allocated entry free for use, NOT in the sense that it's freed
111     memory.
112   */
113   entry *m_free;
114 
115   /* Correspondance between an id (a number) and a TABLE object */
116   HASH m_table_ids;
117 };
118 
119 #endif
120