~drizzle-trunk/drizzle/development

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
#ifndef TABLE_MAPPING_H
17
#define TABLE_MAPPING_H
18
19
/* Forward declarations */
20
struct st_table;
21
typedef st_table TABLE;
22
23
/*
24
  CLASS table_mapping
25
26
  RESPONSIBILITIES
27
    The table mapping is used to map table id's to table pointers
28
29
  COLLABORATION
30
    RELAY_LOG    For mapping table id:s to tables when receiving events.
31
 */
32
33
/*
34
  Guilhem to Mats:
35
  in the table_mapping class, the memory is allocated and never freed (until
36
  destruction). So this is a good candidate for allocating inside a MEM_ROOT:
37
  it gives the efficient allocation in chunks (like in expand()). So I have
38
  introduced a MEM_ROOT.
39
40
  Note that inheriting from Sql_alloc had no effect: it has effects only when
41
  "ptr= new table_mapping" is called, and this is never called. And it would
42
  then allocate from thd->mem_root which is a highly volatile object (reset
43
  from example after executing each query, see dispatch_command(), it has a
44
  free_root() at end); as the table_mapping object is supposed to live longer
45
  than a query, it was dangerous.
46
  A dedicated MEM_ROOT needs to be used, see below.
47
*/
48
49
class table_mapping {
50
51
private:
52
  MEM_ROOT m_mem_root;
53
54
public:
55
56
  enum enum_error {
57
      ERR_NO_ERROR = 0,
58
      ERR_LIMIT_EXCEEDED,
59
      ERR_MEMORY_ALLOCATION
60
  };
61
62
  table_mapping();
63
  ~table_mapping();
64
65
  TABLE* get_table(ulong table_id);
66
67
  int       set_table(ulong table_id, TABLE* table);
68
  int       remove_table(ulong table_id);
69
  void      clear_tables();
70
  ulong     count() const { return m_table_ids.records; }
71
72
private:
73
  /*
74
    This is a POD (Plain Old Data).  Keep it that way (we apply offsetof() to
75
    it, which only works for PODs)
76
  */
77
  struct entry { 
78
    ulong table_id;
79
    union {
80
      TABLE *table;
81
      entry *next;
82
    };
83
  };
84
85
  entry *find_entry(ulong table_id)
86
  {
87
    return (entry *)hash_search(&m_table_ids,
88
				(uchar*)&table_id,
89
				sizeof(table_id));
90
  }
91
  int expand();
92
93
  /*
94
    Head of the list of free entries; "free" in the sense that it's an
95
    allocated entry free for use, NOT in the sense that it's freed
96
    memory.
97
  */
98
  entry *m_free;
99
100
  /* Correspondance between an id (a number) and a TABLE object */
101
  HASH m_table_ids;
102
};
103
104
#endif