~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/rpl_tblmap.cc

  • Committer: Monty Taylor
  • Date: 2008-10-23 00:05:28 UTC
  • Revision ID: monty@inaugust.com-20081023000528-grdvrd8c4058nutm
Moved my_handler to myisam, which is where it actually belongs.

Show diffs side-by-side

added added

removed removed

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