~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/replication/tblmap.h

  • Committer: Brian Aker
  • Date: 2009-01-23 02:15:04 UTC
  • mfrom: (798.2.32 drizzle)
  • Revision ID: brian@tangent.org-20090123021504-2j99e6hxab1ew601
Merge for replication removal.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems
5
 
 *
6
 
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation; version 2 of the License.
9
 
 *
10
 
 *  This program is distributed in the hope that it will be useful,
11
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *  GNU General Public License for more details.
14
 
 *
15
 
 *  You should have received a copy of the GNU General Public License
16
 
 *  along with this program; if not, write to the Free Software
17
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 
 */
19
 
 
20
 
#ifndef DRIZZLED_RPL_TBLMAP_H
21
 
#define DRIZZLED_RPL_TBLMAP_H
22
 
 
23
 
#include <mysys/hash.h>
24
 
 
25
 
/* Forward declarations */
26
 
class Table;
27
 
 
28
 
/*
29
 
  CLASS table_mapping
30
 
 
31
 
  RESPONSIBILITIES
32
 
    The table mapping is used to map table id's to table pointers
33
 
 
34
 
  COLLABORATION
35
 
    RELAY_LOG    For mapping table id:s to tables when receiving events.
36
 
 */
37
 
 
38
 
/*
39
 
  Guilhem to Mats:
40
 
  in the table_mapping class, the memory is allocated and never freed (until
41
 
  destruction). So this is a good candidate for allocating inside a MEM_ROOT:
42
 
  it gives the efficient allocation in chunks (like in expand()). So I have
43
 
  introduced a MEM_ROOT.
44
 
 
45
 
  Note that inheriting from Sql_alloc had no effect: it has effects only when
46
 
  "ptr= new table_mapping" is called, and this is never called. And it would
47
 
  then allocate from session->mem_root which is a highly volatile object (reset
48
 
  from example after executing each query, see dispatch_command(), it has a
49
 
  free_root() at end); as the table_mapping object is supposed to live longer
50
 
  than a query, it was dangerous.
51
 
  A dedicated MEM_ROOT needs to be used, see below.
52
 
*/
53
 
 
54
 
class table_mapping {
55
 
 
56
 
private:
57
 
  MEM_ROOT m_mem_root;
58
 
 
59
 
public:
60
 
 
61
 
  enum enum_error {
62
 
      ERR_NO_ERROR = 0,
63
 
      ERR_LIMIT_EXCEEDED,
64
 
      ERR_MEMORY_ALLOCATION
65
 
  };
66
 
 
67
 
  table_mapping();
68
 
  ~table_mapping();
69
 
 
70
 
  Table* get_table(ulong table_id);
71
 
 
72
 
  int       set_table(ulong table_id, Table* table);
73
 
  int       remove_table(ulong table_id);
74
 
  void      clear_tables();
75
 
  ulong     count() const { return m_table_ids.records; }
76
 
 
77
 
private:
78
 
  /*
79
 
    This is a POD (Plain Old Data).  Keep it that way (we apply offsetof() to
80
 
    it, which only works for PODs)
81
 
  */
82
 
  struct entry {
83
 
    ulong table_id;
84
 
    union {
85
 
      Table *table;
86
 
      entry *next;
87
 
    };
88
 
  };
89
 
 
90
 
  entry *find_entry(ulong table_id)
91
 
  {
92
 
    return (entry *)hash_search(&m_table_ids,
93
 
                                (unsigned char*)&table_id,
94
 
                                sizeof(table_id));
95
 
  }
96
 
  int expand();
97
 
 
98
 
  /*
99
 
    Head of the list of free entries; "free" in the sense that it's an
100
 
    allocated entry free for use, NOT in the sense that it's freed
101
 
    memory.
102
 
  */
103
 
  entry *m_free;
104
 
 
105
 
  /* Correspondance between an id (a number) and a Table object */
106
 
  HASH m_table_ids;
107
 
};
108
 
 
109
 
#endif /* DRIZZLED_RPL_TBLMAP_H */