~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/rpl_tblmap.cc

  • Committer: Monty Taylor
  • Date: 2009-03-24 17:44:41 UTC
  • mto: (960.5.2 mordred)
  • mto: This revision was merged to the branch mainline in revision 964.
  • Revision ID: mordred@inaugust.com-20090324174441-nmsq0gwjlgf7f0mt
Changed handlerton to StorageEngine.

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 MAYBE_TABLE_NAME(T) ((T) ? (T)->s->table_name.str : "<>")
21
 
#define TABLE_ID_HASH_SIZE 32
22
 
#define TABLE_ID_CHUNK 256
23
 
 
24
 
table_mapping::table_mapping()
25
 
  : m_free(0)
26
 
{
27
 
  /*
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
30
 
    be freed one by one.
31
 
    Note that below we don't test if hash_init() succeeded. This constructor
32
 
    is called at startup only.
33
 
  */
34
 
  (void) hash_init(&m_table_ids,&my_charset_bin,TABLE_ID_HASH_SIZE,
35
 
                   offsetof(entry,table_id),sizeof(ulong),
36
 
                   0,0,0);
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);
39
 
}
40
 
 
41
 
table_mapping::~table_mapping()
42
 
{
43
 
  hash_free(&m_table_ids);
44
 
  free_root(&m_mem_root, MYF(0));
45
 
}
46
 
 
47
 
Table* table_mapping::get_table(ulong table_id)
48
 
{
49
 
  entry *e= find_entry(table_id);
50
 
  if (e) 
51
 
  {
52
 
    return(e->table);
53
 
  }
54
 
 
55
 
  return(NULL);
56
 
}
57
 
 
58
 
/*
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.
62
 
*/
63
 
int table_mapping::expand()
64
 
{
65
 
  /*
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
69
 
    in C.
70
 
  */
71
 
  entry *tmp= (entry *)alloc_root(&m_mem_root, TABLE_ID_CHUNK*sizeof(entry));
72
 
  if (tmp == NULL)
73
 
    return ERR_MEMORY_ALLOCATION; // Memory allocation failed
74
 
 
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++)
78
 
    e->next= e+1;
79
 
  e_end->next= m_free;
80
 
  m_free= tmp;
81
 
  return 0;
82
 
}
83
 
 
84
 
int table_mapping::set_table(ulong table_id, Table* table)
85
 
{
86
 
  entry *e= find_entry(table_id);
87
 
  if (e == 0)
88
 
  {
89
 
    if (m_free == 0 && expand())
90
 
      return(ERR_MEMORY_ALLOCATION); // Memory allocation failed      
91
 
    e= m_free;
92
 
    m_free= m_free->next;
93
 
  }
94
 
  else
95
 
    hash_delete(&m_table_ids,(unsigned char *)e);
96
 
 
97
 
  e->table_id= table_id;
98
 
  e->table= table;
99
 
  my_hash_insert(&m_table_ids,(unsigned char *)e);
100
 
 
101
 
  return(0);            // All OK
102
 
}
103
 
 
104
 
int table_mapping::remove_table(ulong table_id)
105
 
{
106
 
  entry *e= find_entry(table_id);
107
 
  if (e)
108
 
  {
109
 
    hash_delete(&m_table_ids,(unsigned char *)e);
110
 
    /* we add this entry to the chain of free (free for use) entries */
111
 
    e->next= m_free;
112
 
    m_free= e;
113
 
    return 0;                   // All OK
114
 
  }
115
 
  return 1;                     // No table to remove
116
 
}
117
 
 
118
 
/*
119
 
  Puts all entries into the list of free-for-use entries (does not free any
120
 
  memory), and empties the hash.
121
 
*/
122
 
void table_mapping::clear_tables()
123
 
{
124
 
  for (uint32_t i= 0; i < m_table_ids.records; i++)
125
 
  {
126
 
    entry *e= (entry *)hash_element(&m_table_ids, i);
127
 
    e->next= m_free;
128
 
    m_free= e;
129
 
  }
130
 
  my_hash_reset(&m_table_ids);
131
 
  return;
132
 
}