~drizzle-trunk/drizzle/development

1223.4.1 by Brian Aker
Table Identifier patch.
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1660.1.1 by Brian Aker
Merge in move identifier work.
4
 *  Copyright (C) 2010 Brian Aker
1223.4.1 by Brian Aker
Table Identifier patch.
5
 *  Copyright (C) 2009 Sun Microsystems
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  (at your option) any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program; if not, write to the Free Software
19
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
 */
21
22
/* 
23
  This is a "work in progress". The concept needs to be replicated throughout
24
  the code, but we will start with baby steps for the moment. To not incur
25
  cost until we are complete, for the moment it will do no allocation.
26
27
  This is mainly here so that it can be used in the SE interface for
28
  the time being.
29
30
  This will replace Table_ident.
31
  */
32
1660.1.1 by Brian Aker
Merge in move identifier work.
33
#ifndef DRIZZLED_IDENTIFIER_TABLE_H
34
#define DRIZZLED_IDENTIFIER_TABLE_H
1223.4.1 by Brian Aker
Table Identifier patch.
35
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
36
#include <drizzled/enum.h>
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
37
#include "drizzled/definitions.h"
1395.1.1 by Brian Aker
Fixes for alter table to make sure that the proto on disk is valid.
38
#include "drizzled/message/table.pb.h"
1415 by Brian Aker
Mass overhaul to use schema_identifier.
39
1660.1.1 by Brian Aker
Merge in move identifier work.
40
#include "drizzled/identifier.h"
1415 by Brian Aker
Mass overhaul to use schema_identifier.
41
1223.4.1 by Brian Aker
Table Identifier patch.
42
#include <string.h>
43
1341 by Brian Aker
See if these are now cleaned up.
44
#include <assert.h>
45
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
46
#include <ostream>
1309.1.11 by Brian Aker
Small helper bit for dealing with lists of table_identifiers.
47
#include <set>
1358.1.5 by Brian Aker
Cleans up use of static buffer in TableIdentifier.
48
#include <algorithm>
49
#include <functional>
1223.4.1 by Brian Aker
Table Identifier patch.
50
1608.2.1 by Brian Aker
Modified to table identifier to fix temporary table creation loss of file.
51
#include <boost/functional/hash.hpp>
52
1223.4.1 by Brian Aker
Table Identifier patch.
53
namespace drizzled {
54
1417 by Brian Aker
Interface for Stewart.
55
class Table;
56
1415 by Brian Aker
Mass overhaul to use schema_identifier.
57
class TableIdentifier : public SchemaIdentifier
1223.4.1 by Brian Aker
Table Identifier patch.
58
{
1395.1.9 by Brian Aker
Small cleanup around how we do types internally.
59
public:
60
  typedef message::Table::TableType Type;
1618 by Brian Aker
This is a rollup set of patches for modifications to TableIdentifier to have
61
  typedef std::vector<char> Key;
1395.1.9 by Brian Aker
Small cleanup around how we do types internally.
62
private:
63
64
  Type type;
1358.1.5 by Brian Aker
Cleans up use of static buffer in TableIdentifier.
65
  std::string path;
1341 by Brian Aker
See if these are now cleaned up.
66
  std::string table_name;
1320.1.5 by Brian Aker
Second pass through remove proto write outside of SE.
67
  std::string sql_path;
1618 by Brian Aker
This is a rollup set of patches for modifications to TableIdentifier to have
68
  Key key;
1608.2.1 by Brian Aker
Modified to table identifier to fix temporary table creation loss of file.
69
  size_t hash_value;
1223.4.1 by Brian Aker
Table Identifier patch.
70
1415 by Brian Aker
Mass overhaul to use schema_identifier.
71
  void init();
1395.1.6 by Brian Aker
Modified TableIdentifier output for errors.
72
1618 by Brian Aker
This is a rollup set of patches for modifications to TableIdentifier to have
73
  size_t getKeySize() const
74
  {
75
    return getSchemaName().size() + getTableName().size() + 2;
76
  }
77
1223.4.1 by Brian Aker
Table Identifier patch.
78
public:
1417 by Brian Aker
Interface for Stewart.
79
  TableIdentifier(const Table &table);
80
                   
1415 by Brian Aker
Mass overhaul to use schema_identifier.
81
  TableIdentifier( const SchemaIdentifier &schema,
82
                   const std::string &table_name_arg,
83
                   Type tmp_arg= message::Table::STANDARD) :
84
    SchemaIdentifier(schema),
85
    type(tmp_arg),
86
    table_name(table_name_arg)
87
  { 
88
    init();
89
  }
90
1358.1.5 by Brian Aker
Cleans up use of static buffer in TableIdentifier.
91
  TableIdentifier( const std::string &db_arg,
92
                   const std::string &table_name_arg,
1395.1.9 by Brian Aker
Small cleanup around how we do types internally.
93
                   Type tmp_arg= message::Table::STANDARD) :
1415 by Brian Aker
Mass overhaul to use schema_identifier.
94
    SchemaIdentifier(db_arg),
1223.4.1 by Brian Aker
Table Identifier patch.
95
    type(tmp_arg),
1395.1.14 by Brian Aker
Fix for Innodb dfe files.
96
    table_name(table_name_arg)
1320.1.5 by Brian Aker
Second pass through remove proto write outside of SE.
97
  { 
1415 by Brian Aker
Mass overhaul to use schema_identifier.
98
    init();
1320.1.5 by Brian Aker
Second pass through remove proto write outside of SE.
99
  }
1223.4.1 by Brian Aker
Table Identifier patch.
100
1395.1.14 by Brian Aker
Fix for Innodb dfe files.
101
  TableIdentifier( const std::string &schema_name_arg,
102
                   const std::string &table_name_arg,
103
                   const std::string &path_arg ) :
1415 by Brian Aker
Mass overhaul to use schema_identifier.
104
    SchemaIdentifier(schema_name_arg),
1395.1.7 by Brian Aker
Removed the internal table type in favor of the protobuf one.
105
    type(message::Table::TEMPORARY),
1389 by Brian Aker
Large reord in ALTER TABLE for RENAME.
106
    path(path_arg),
107
    table_name(table_name_arg)
108
  { 
1415 by Brian Aker
Mass overhaul to use schema_identifier.
109
    init();
1358.1.2 by Brian Aker
Long pass through the system to use more of TableIdentifiers.
110
  }
111
1418.1.1 by Monty Taylor
Explicitly suck the base-class compare method into the child class to avoid
112
  using SchemaIdentifier::compare;
1415 by Brian Aker
Mass overhaul to use schema_identifier.
113
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
114
  bool isTmp() const
1223.4.1 by Brian Aker
Table Identifier patch.
115
  {
1395.1.7 by Brian Aker
Removed the internal table type in favor of the protobuf one.
116
    if (type == message::Table::TEMPORARY || type == message::Table::INTERNAL)
117
      return true;
118
    return false;
1223.4.1 by Brian Aker
Table Identifier patch.
119
  }
120
1802.12.1 by Brian Aker
This solves bug lp:654905
121
  static bool isView(message::Table::TableType arg) // Not a SQL view, but a view for I_S
122
  {
123
    switch (arg)
124
    {
125
    default:
126
    case message::Table::STANDARD:
127
    case message::Table::TEMPORARY:
128
    case message::Table::INTERNAL:
129
      break;
130
    case message::Table::FUNCTION:
131
      return true;
132
    }
133
134
    return false;
135
  }
136
1643.3.11 by Brian Aker
Encapsulate a bit of logic.
137
  bool isView() const // Not a SQL view, but a view for I_S
138
  {
1802.12.1 by Brian Aker
This solves bug lp:654905
139
    return isView(type);
1643.3.11 by Brian Aker
Encapsulate a bit of logic.
140
  }
141
1395.1.9 by Brian Aker
Small cleanup around how we do types internally.
142
  Type getType() const
143
  {
144
    return type;
145
  }
146
1395.1.6 by Brian Aker
Modified TableIdentifier output for errors.
147
  const std::string &getSQLPath();
1320.1.5 by Brian Aker
Second pass through remove proto write outside of SE.
148
1608.2.1 by Brian Aker
Modified to table identifier to fix temporary table creation loss of file.
149
  const std::string &getPath() const;
1358.1.9 by Brian Aker
Update for std::string
150
1395.1.14 by Brian Aker
Fix for Innodb dfe files.
151
  void setPath(const std::string &new_path)
152
  {
153
    path= new_path;
154
  }
155
1411.3.3 by Monty Taylor
Made getTableName() return a const reference instead of a temporary const
156
  const std::string &getTableName() const
1358.1.9 by Brian Aker
Update for std::string
157
  {
158
    return table_name;
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
159
  }
160
1618.1.1 by Brian Aker
Modify TableIdentifier to be const
161
  void copyToTableMessage(message::Table &message) const;
1395.1.1 by Brian Aker
Fixes for alter table to make sure that the proto on disk is valid.
162
1596.1.4 by Monty Taylor
Fixed TableIdentifier sorting. The customer operator<() wasn't getting called on Sun Studio.
163
  friend bool operator<(const TableIdentifier &left, const TableIdentifier &right)
1429.1.3 by Brian Aker
Merge in work for fetching a list of table identifiers.
164
  {
1669.2.1 by Brian Aker
Modify how we make comparisons for tables.
165
    if (left.getKey() < right.getKey())
1429.1.3 by Brian Aker
Merge in work for fetching a list of table identifiers.
166
    {
167
      return true;
168
    }
169
170
    return false;
171
  }
172
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
173
  friend std::ostream& operator<<(std::ostream& output, const TableIdentifier &identifier)
174
  {
175
    const char *type_str;
176
177
    output << "TableIdentifier:(";
1415 by Brian Aker
Mass overhaul to use schema_identifier.
178
    output <<  identifier.getSchemaName();
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
179
    output << ", ";
180
    output << identifier.getTableName();
181
    output << ", ";
182
183
    switch (identifier.type) {
1395.1.7 by Brian Aker
Removed the internal table type in favor of the protobuf one.
184
    case message::Table::STANDARD:
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
185
      type_str= "standard";
186
      break;
1395.1.7 by Brian Aker
Removed the internal table type in favor of the protobuf one.
187
    case message::Table::INTERNAL:
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
188
      type_str= "internal";
189
      break;
1395.1.7 by Brian Aker
Removed the internal table type in favor of the protobuf one.
190
    case message::Table::TEMPORARY:
1235.1.3 by Brian Aker
Remove the need for trans/non-trans temp tables for lock conditions.
191
      type_str= "temporary";
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
192
      break;
1395.1.7 by Brian Aker
Removed the internal table type in favor of the protobuf one.
193
    case message::Table::FUNCTION:
194
      type_str= "function";
195
      break;
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
196
    }
197
198
    output << type_str;
1395.1.14 by Brian Aker
Fix for Innodb dfe files.
199
    output << ", ";
200
    output << identifier.path;
1608.2.1 by Brian Aker
Modified to table identifier to fix temporary table creation loss of file.
201
    output << ", ";
202
    output << identifier.getHashValue();
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
203
    output << ")";
204
205
    return output;  // for multiple << operators.
206
  }
207
1395.1.6 by Brian Aker
Modified TableIdentifier output for errors.
208
  friend bool operator==(TableIdentifier &left, TableIdentifier &right)
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
209
  {
1669.2.1 by Brian Aker
Modify how we make comparisons for tables.
210
    if (left.getKey() == right.getKey())
211
      return true;
1223.4.1 by Brian Aker
Table Identifier patch.
212
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
213
    return false;
214
  }
215
1601 by Brian Aker
Move functions to class methods.
216
  static uint32_t filename_to_tablename(const char *from, char *to, uint32_t to_length);
217
  static size_t build_table_filename(std::string &buff, const char *db, const char *table_name, bool is_tmp);
1618 by Brian Aker
This is a rollup set of patches for modifications to TableIdentifier to have
218
  static size_t build_tmptable_filename(std::string &buffer);
219
  static size_t build_tmptable_filename(std::vector<char> &buffer);
220
221
  /*
222
    Create a table cache key
223
224
    SYNOPSIS
225
    createKey()
226
    key			Create key here (must be of size MAX_DBKEY_LENGTH)
227
    table_list		Table definition
228
229
    IMPLEMENTATION
230
    The table cache_key is created from:
231
    db_name + \0
232
    table_name + \0
233
234
    if the table is a tmp table, we add the following to make each tmp table
235
    unique on the slave:
236
237
    4 bytes for master thread id
238
    4 bytes pseudo thread id
239
240
    RETURN
241
    Length of key
242
  */
243
  static uint32_t createKey(char *key, const char *db_arg, const char *table_name_arg)
244
  {
245
    uint32_t key_length;
246
    char *key_pos= key;
247
248
    key_pos= strcpy(key_pos, db_arg) + strlen(db_arg);
249
    key_pos= strcpy(key_pos+1, table_name_arg) +
250
      strlen(table_name_arg);
251
    key_length= (uint32_t)(key_pos-key)+1;
252
253
    return key_length;
254
  }
255
256
  static uint32_t createKey(char *key, const TableIdentifier &identifier)
257
  {
258
    uint32_t key_length;
259
    char *key_pos= key;
260
261
    key_pos= strcpy(key_pos, identifier.getSchemaName().c_str()) + identifier.getSchemaName().length();
262
    key_pos= strcpy(key_pos + 1, identifier.getTableName().c_str()) + identifier.getTableName().length();
263
    key_length= (uint32_t)(key_pos-key)+1;
264
265
    return key_length;
266
  }
1601 by Brian Aker
Move functions to class methods.
267
1608.2.1 by Brian Aker
Modified to table identifier to fix temporary table creation loss of file.
268
  size_t getHashValue() const
269
  {
270
    return hash_value;
271
  }
1618 by Brian Aker
This is a rollup set of patches for modifications to TableIdentifier to have
272
273
  const Key &getKey() const
274
  {
275
    return key;
276
  }
1223.4.1 by Brian Aker
Table Identifier patch.
277
};
278
1608.2.1 by Brian Aker
Modified to table identifier to fix temporary table creation loss of file.
279
std::size_t hash_value(TableIdentifier const& b);
280
1643.3.13 by Brian Aker
Remove sort() and add in DEBUG mode to randomize the results of generators.
281
typedef std::vector <TableIdentifier> TableIdentifiers;
1309.1.11 by Brian Aker
Small helper bit for dealing with lists of table_identifiers.
282
1223.4.1 by Brian Aker
Table Identifier patch.
283
} /* namespace drizzled */
284
1660.1.1 by Brian Aker
Merge in move identifier work.
285
#endif /* DRIZZLED_IDENTIFIER_TABLE_H */