~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/rpl_tblmap.h

Renamed strings to mystrings, for include/lib naming consistency.

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