MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
rpl_tblmap.cc
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 as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software Foundation,
14  51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
15 
16 #include "sql_priv.h"
17 #include "my_global.h" // HAVE_REPLICATION
18 
19 #ifdef HAVE_REPLICATION
20 
21 #include "rpl_tblmap.h"
22 #ifndef MYSQL_CLIENT
23 #include "table.h"
24 #endif
25 
26 #ifdef MYSQL_CLIENT
27 #define MAYBE_TABLE_NAME(T) ("")
28 #else
29 #define MAYBE_TABLE_NAME(T) ((T) ? (T)->s->table_name.str : "<>")
30 #endif
31 #define TABLE_ID_HASH_SIZE 32
32 #define TABLE_ID_CHUNK 256
33 
34 table_mapping::table_mapping()
35  : m_free(0)
36 {
37  /*
38  No "free_element" function for entries passed here, as the entries are
39  allocated in a MEM_ROOT (freed as a whole in the destructor), they cannot
40  be freed one by one.
41  Note that below we don't test if my_hash_init() succeeded. This
42  constructor is called at startup only.
43  */
44  (void) my_hash_init(&m_table_ids,&my_charset_bin,TABLE_ID_HASH_SIZE,
45  offsetof(entry,table_id),sizeof(ulonglong),
46  0,0,0);
47  /* We don't preallocate any block, this is consistent with m_free=0 above */
48  init_alloc_root(&m_mem_root, TABLE_ID_HASH_SIZE*sizeof(entry), 0);
49 }
50 
51 table_mapping::~table_mapping()
52 {
53 #ifdef MYSQL_CLIENT
54  clear_tables();
55 #endif
56  my_hash_free(&m_table_ids);
57  free_root(&m_mem_root, MYF(0));
58 }
59 
60 TABLE* table_mapping::get_table(ulonglong table_id)
61 {
62  DBUG_ENTER("table_mapping::get_table(ulonglong)");
63  DBUG_PRINT("enter", ("table_id: %llu", table_id));
64  entry *e= find_entry(table_id);
65  if (e)
66  {
67  DBUG_PRINT("info", ("tid %llu -> table 0x%lx (%s)",
68  table_id, (long) e->table,
69  MAYBE_TABLE_NAME(e->table)));
70  DBUG_RETURN(e->table);
71  }
72 
73  DBUG_PRINT("info", ("tid %llu is not mapped!", table_id));
74  DBUG_RETURN(NULL);
75 }
76 
77 /*
78  Called when we are out of table id entries. Creates TABLE_ID_CHUNK
79  new entries, chain them and attach them at the head of the list of free
80  (free for use) entries.
81 */
82 int table_mapping::expand()
83 {
84  /*
85  If we wanted to use "tmp= new (&m_mem_root) entry[TABLE_ID_CHUNK]",
86  we would have to make "entry" derive from Sql_alloc but then it would not
87  be a POD anymore and we want it to be (see rpl_tblmap.h). So we allocate
88  in C.
89  */
90  entry *tmp= (entry *)alloc_root(&m_mem_root, TABLE_ID_CHUNK*sizeof(entry));
91  if (tmp == NULL)
92  return ERR_MEMORY_ALLOCATION; // Memory allocation failed
93 
94  /* Find the end of this fresh new array of free entries */
95  entry *e_end= tmp+TABLE_ID_CHUNK-1;
96  for (entry *e= tmp; e < e_end; e++)
97  e->next= e+1;
98  e_end->next= m_free;
99  m_free= tmp;
100  return 0;
101 }
102 
103 int table_mapping::set_table(ulonglong table_id, TABLE* table)
104 {
105  DBUG_ENTER("table_mapping::set_table(ulong,TABLE*)");
106  DBUG_PRINT("enter", ("table_id: %llu table: 0x%lx (%s)",
107  table_id,
108  (long) table, MAYBE_TABLE_NAME(table)));
109  entry *e= find_entry(table_id);
110  if (e == 0)
111  {
112  if (m_free == 0 && expand())
113  DBUG_RETURN(ERR_MEMORY_ALLOCATION); // Memory allocation failed
114  e= m_free;
115  m_free= m_free->next;
116  }
117  else
118  {
119 #ifdef MYSQL_CLIENT
120  free_table_map_log_event(e->table);
121 #endif
122  my_hash_delete(&m_table_ids,(uchar *)e);
123  }
124  e->table_id= table_id;
125  e->table= table;
126  if (my_hash_insert(&m_table_ids,(uchar *)e))
127  {
128  /* we add this entry to the chain of free (free for use) entries */
129  e->next= m_free;
130  m_free= e;
131  DBUG_RETURN(ERR_MEMORY_ALLOCATION);
132  }
133 
134  DBUG_PRINT("info", ("tid %llu -> table 0x%lx (%s)",
135  table_id, (long) e->table,
136  MAYBE_TABLE_NAME(e->table)));
137  DBUG_RETURN(0); // All OK
138 }
139 
140 int table_mapping::remove_table(ulonglong table_id)
141 {
142  entry *e= find_entry(table_id);
143  if (e)
144  {
145  my_hash_delete(&m_table_ids,(uchar *)e);
146  /* we add this entry to the chain of free (free for use) entries */
147  e->next= m_free;
148  m_free= e;
149  return 0; // All OK
150  }
151  return 1; // No table to remove
152 }
153 
154 /*
155  Puts all entries into the list of free-for-use entries (does not free any
156  memory), and empties the hash.
157 */
158 void table_mapping::clear_tables()
159 {
160  DBUG_ENTER("table_mapping::clear_tables()");
161  for (uint i= 0; i < m_table_ids.records; i++)
162  {
163  entry *e= (entry *)my_hash_element(&m_table_ids, i);
164 #ifdef MYSQL_CLIENT
165  free_table_map_log_event(e->table);
166 #endif
167  e->next= m_free;
168  m_free= e;
169  }
170  my_hash_reset(&m_table_ids);
171  DBUG_VOID_RETURN;
172 }
173 
174 #endif