1
by brian
clean slate |
1 |
/* Copyright (C) 2005 MySQL AB
|
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
|
|
14 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
15 |
||
16 |
#include "mysql_priv.h" |
|
17 |
||
18 |
#ifdef HAVE_REPLICATION
|
|
19 |
||
20 |
#include "rpl_tblmap.h" |
|
21 |
||
22 |
#define MAYBE_TABLE_NAME(T) ((T) ? (T)->s->table_name.str : "<>")
|
|
23 |
#define TABLE_ID_HASH_SIZE 32
|
|
24 |
#define TABLE_ID_CHUNK 256
|
|
25 |
||
26 |
table_mapping::table_mapping() |
|
27 |
: m_free(0) |
|
28 |
{
|
|
29 |
/*
|
|
30 |
No "free_element" function for entries passed here, as the entries are
|
|
31 |
allocated in a MEM_ROOT (freed as a whole in the destructor), they cannot
|
|
32 |
be freed one by one.
|
|
33 |
Note that below we don't test if hash_init() succeeded. This constructor
|
|
34 |
is called at startup only.
|
|
35 |
*/
|
|
36 |
(void) hash_init(&m_table_ids,&my_charset_bin,TABLE_ID_HASH_SIZE, |
|
37 |
offsetof(entry,table_id),sizeof(ulong), |
|
38 |
0,0,0); |
|
39 |
/* We don't preallocate any block, this is consistent with m_free=0 above */
|
|
40 |
init_alloc_root(&m_mem_root, TABLE_ID_HASH_SIZE*sizeof(entry), 0); |
|
41 |
}
|
|
42 |
||
43 |
table_mapping::~table_mapping() |
|
44 |
{
|
|
45 |
hash_free(&m_table_ids); |
|
46 |
free_root(&m_mem_root, MYF(0)); |
|
47 |
}
|
|
48 |
||
49 |
st_table* table_mapping::get_table(ulong table_id) |
|
50 |
{
|
|
51 |
entry *e= find_entry(table_id); |
|
52 |
if (e) |
|
53 |
{
|
|
51.1.43
by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE |
54 |
return(e->table); |
1
by brian
clean slate |
55 |
}
|
56 |
||
51.1.43
by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE |
57 |
return(NULL); |
1
by brian
clean slate |
58 |
}
|
59 |
||
60 |
/*
|
|
61 |
Called when we are out of table id entries. Creates TABLE_ID_CHUNK
|
|
62 |
new entries, chain them and attach them at the head of the list of free
|
|
63 |
(free for use) entries.
|
|
64 |
*/
|
|
65 |
int table_mapping::expand() |
|
66 |
{
|
|
67 |
/*
|
|
68 |
If we wanted to use "tmp= new (&m_mem_root) entry[TABLE_ID_CHUNK]",
|
|
69 |
we would have to make "entry" derive from Sql_alloc but then it would not
|
|
70 |
be a POD anymore and we want it to be (see rpl_tblmap.h). So we allocate
|
|
71 |
in C.
|
|
72 |
*/
|
|
73 |
entry *tmp= (entry *)alloc_root(&m_mem_root, TABLE_ID_CHUNK*sizeof(entry)); |
|
74 |
if (tmp == NULL) |
|
75 |
return ERR_MEMORY_ALLOCATION; // Memory allocation failed |
|
76 |
||
77 |
/* Find the end of this fresh new array of free entries */
|
|
78 |
entry *e_end= tmp+TABLE_ID_CHUNK-1; |
|
79 |
for (entry *e= tmp; e < e_end; e++) |
|
80 |
e->next= e+1; |
|
81 |
e_end->next= m_free; |
|
82 |
m_free= tmp; |
|
83 |
return 0; |
|
84 |
}
|
|
85 |
||
86 |
int table_mapping::set_table(ulong table_id, TABLE* table) |
|
87 |
{
|
|
88 |
entry *e= find_entry(table_id); |
|
89 |
if (e == 0) |
|
90 |
{
|
|
91 |
if (m_free == 0 && expand()) |
|
51.1.43
by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE |
92 |
return(ERR_MEMORY_ALLOCATION); // Memory allocation failed |
1
by brian
clean slate |
93 |
e= m_free; |
94 |
m_free= m_free->next; |
|
95 |
}
|
|
96 |
else
|
|
97 |
hash_delete(&m_table_ids,(uchar *)e); |
|
98 |
||
99 |
e->table_id= table_id; |
|
100 |
e->table= table; |
|
101 |
my_hash_insert(&m_table_ids,(uchar *)e); |
|
102 |
||
51.1.43
by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE |
103 |
return(0); // All OK |
1
by brian
clean slate |
104 |
}
|
105 |
||
106 |
int table_mapping::remove_table(ulong table_id) |
|
107 |
{
|
|
108 |
entry *e= find_entry(table_id); |
|
109 |
if (e) |
|
110 |
{
|
|
111 |
hash_delete(&m_table_ids,(uchar *)e); |
|
112 |
/* we add this entry to the chain of free (free for use) entries */
|
|
113 |
e->next= m_free; |
|
114 |
m_free= e; |
|
115 |
return 0; // All OK |
|
116 |
}
|
|
117 |
return 1; // No table to remove |
|
118 |
}
|
|
119 |
||
120 |
/*
|
|
121 |
Puts all entries into the list of free-for-use entries (does not free any
|
|
122 |
memory), and empties the hash.
|
|
123 |
*/
|
|
124 |
void table_mapping::clear_tables() |
|
125 |
{
|
|
126 |
for (uint i= 0; i < m_table_ids.records; i++) |
|
127 |
{
|
|
128 |
entry *e= (entry *)hash_element(&m_table_ids, i); |
|
129 |
e->next= m_free; |
|
130 |
m_free= e; |
|
131 |
}
|
|
132 |
my_hash_reset(&m_table_ids); |
|
51.1.43
by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE |
133 |
return; |
1
by brian
clean slate |
134 |
}
|
135 |
||
136 |
#endif
|