1
/* Copyright (C) 2005 MySQL AB
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.
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.
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 */
16
#include <drizzled/server_includes.h>
18
#include "rpl_tblmap.h"
20
#define MAYBE_TABLE_NAME(T) ((T) ? (T)->s->table_name.str : "<>")
21
#define TABLE_ID_HASH_SIZE 32
22
#define TABLE_ID_CHUNK 256
24
table_mapping::table_mapping()
28
No "free_element" function for entries passed here, as the entries are
29
allocated in a MEM_ROOT (freed as a whole in the destructor), they cannot
31
Note that below we don't test if hash_init() succeeded. This constructor
32
is called at startup only.
34
(void) hash_init(&m_table_ids,&my_charset_bin,TABLE_ID_HASH_SIZE,
35
offsetof(entry,table_id),sizeof(ulong),
37
/* We don't preallocate any block, this is consistent with m_free=0 above */
38
init_alloc_root(&m_mem_root, TABLE_ID_HASH_SIZE*sizeof(entry), 0);
41
table_mapping::~table_mapping()
43
hash_free(&m_table_ids);
44
free_root(&m_mem_root, MYF(0));
47
Table* table_mapping::get_table(ulong table_id)
49
entry *e= find_entry(table_id);
59
Called when we are out of table id entries. Creates TABLE_ID_CHUNK
60
new entries, chain them and attach them at the head of the list of free
61
(free for use) entries.
63
int table_mapping::expand()
66
If we wanted to use "tmp= new (&m_mem_root) entry[TABLE_ID_CHUNK]",
67
we would have to make "entry" derive from Sql_alloc but then it would not
68
be a POD anymore and we want it to be (see rpl_tblmap.h). So we allocate
71
entry *tmp= (entry *)alloc_root(&m_mem_root, TABLE_ID_CHUNK*sizeof(entry));
73
return ERR_MEMORY_ALLOCATION; // Memory allocation failed
75
/* Find the end of this fresh new array of free entries */
76
entry *e_end= tmp+TABLE_ID_CHUNK-1;
77
for (entry *e= tmp; e < e_end; e++)
84
int table_mapping::set_table(ulong table_id, Table* table)
86
entry *e= find_entry(table_id);
89
if (m_free == 0 && expand())
90
return(ERR_MEMORY_ALLOCATION); // Memory allocation failed
95
hash_delete(&m_table_ids,(unsigned char *)e);
97
e->table_id= table_id;
99
my_hash_insert(&m_table_ids,(unsigned char *)e);
104
int table_mapping::remove_table(ulong table_id)
106
entry *e= find_entry(table_id);
109
hash_delete(&m_table_ids,(unsigned char *)e);
110
/* we add this entry to the chain of free (free for use) entries */
115
return 1; // No table to remove
119
Puts all entries into the list of free-for-use entries (does not free any
120
memory), and empties the hash.
122
void table_mapping::clear_tables()
124
for (uint32_t i= 0; i < m_table_ids.records; i++)
126
entry *e= (entry *)hash_element(&m_table_ids, i);
130
my_hash_reset(&m_table_ids);