~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
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
5
 *  Copyright (C) 2009 Sun Microsystems, Inc.
1223.4.1 by Brian Aker
Table Identifier patch.
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;
1966.2.3 by Brian Aker
Fix another style issue.
61
  typedef std::vector <TableIdentifier> vector;
1878.5.3 by Brian Aker
Update Key to work a bit faster.
62
63
  class Key
64
  {
65
    std::vector<char> key_buffer;
66
    size_t hash_value;
67
68
  public:
69
70
    Key() :
71
      hash_value(0)
72
    {
73
    }
74
75
    const char *vector() const
76
    {
77
      return &key_buffer[0];
78
    }
79
80
    std::vector<char> &vectorPtr()
81
    {
82
      return key_buffer;
83
    }
84
85
    void set(size_t resize_arg, const std::string &a, const std::string &b);
86
87
    friend bool operator==(const Key &left, const Key &right)
88
    {
89
      if (left.hash_value == right.hash_value and left.key_buffer.size() == right.key_buffer.size())
90
      {
91
        if (memcmp(&left.key_buffer[0], &right.key_buffer[0], left.key_buffer.size()) == 0)
92
          return true;
93
      }
94
95
      return false;
96
    }
97
98
    friend bool operator<(const Key &left, const Key &right)
99
    {
100
      return left.key_buffer < right.key_buffer;
101
    }
102
103
    size_t size() const
104
    {
105
      return key_buffer.size();
106
    }
107
108
    size_t getHashValue() const
109
    {
110
      return hash_value;
111
    }
112
  };
113
1395.1.9 by Brian Aker
Small cleanup around how we do types internally.
114
private:
115
116
  Type type;
1358.1.5 by Brian Aker
Cleans up use of static buffer in TableIdentifier.
117
  std::string path;
1341 by Brian Aker
See if these are now cleaned up.
118
  std::string table_name;
1618 by Brian Aker
This is a rollup set of patches for modifications to TableIdentifier to have
119
  Key key;
1608.2.1 by Brian Aker
Modified to table identifier to fix temporary table creation loss of file.
120
  size_t hash_value;
1223.4.1 by Brian Aker
Table Identifier patch.
121
1415 by Brian Aker
Mass overhaul to use schema_identifier.
122
  void init();
1395.1.6 by Brian Aker
Modified TableIdentifier output for errors.
123
1618 by Brian Aker
This is a rollup set of patches for modifications to TableIdentifier to have
124
  size_t getKeySize() const
125
  {
126
    return getSchemaName().size() + getTableName().size() + 2;
127
  }
128
1223.4.1 by Brian Aker
Table Identifier patch.
129
public:
1417 by Brian Aker
Interface for Stewart.
130
  TableIdentifier(const Table &table);
131
                   
1415 by Brian Aker
Mass overhaul to use schema_identifier.
132
  TableIdentifier( const SchemaIdentifier &schema,
133
                   const std::string &table_name_arg,
134
                   Type tmp_arg= message::Table::STANDARD) :
135
    SchemaIdentifier(schema),
136
    type(tmp_arg),
137
    table_name(table_name_arg)
138
  { 
139
    init();
140
  }
141
1358.1.5 by Brian Aker
Cleans up use of static buffer in TableIdentifier.
142
  TableIdentifier( const std::string &db_arg,
143
                   const std::string &table_name_arg,
1395.1.9 by Brian Aker
Small cleanup around how we do types internally.
144
                   Type tmp_arg= message::Table::STANDARD) :
1415 by Brian Aker
Mass overhaul to use schema_identifier.
145
    SchemaIdentifier(db_arg),
1223.4.1 by Brian Aker
Table Identifier patch.
146
    type(tmp_arg),
1395.1.14 by Brian Aker
Fix for Innodb dfe files.
147
    table_name(table_name_arg)
1320.1.5 by Brian Aker
Second pass through remove proto write outside of SE.
148
  { 
1415 by Brian Aker
Mass overhaul to use schema_identifier.
149
    init();
1320.1.5 by Brian Aker
Second pass through remove proto write outside of SE.
150
  }
1223.4.1 by Brian Aker
Table Identifier patch.
151
1395.1.14 by Brian Aker
Fix for Innodb dfe files.
152
  TableIdentifier( const std::string &schema_name_arg,
153
                   const std::string &table_name_arg,
154
                   const std::string &path_arg ) :
1415 by Brian Aker
Mass overhaul to use schema_identifier.
155
    SchemaIdentifier(schema_name_arg),
1395.1.7 by Brian Aker
Removed the internal table type in favor of the protobuf one.
156
    type(message::Table::TEMPORARY),
1389 by Brian Aker
Large reord in ALTER TABLE for RENAME.
157
    path(path_arg),
158
    table_name(table_name_arg)
159
  { 
1415 by Brian Aker
Mass overhaul to use schema_identifier.
160
    init();
1358.1.2 by Brian Aker
Long pass through the system to use more of TableIdentifiers.
161
  }
162
1418.1.1 by Monty Taylor
Explicitly suck the base-class compare method into the child class to avoid
163
  using SchemaIdentifier::compare;
1415 by Brian Aker
Mass overhaul to use schema_identifier.
164
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
165
  bool isTmp() const
1223.4.1 by Brian Aker
Table Identifier patch.
166
  {
1395.1.7 by Brian Aker
Removed the internal table type in favor of the protobuf one.
167
    if (type == message::Table::TEMPORARY || type == message::Table::INTERNAL)
168
      return true;
169
    return false;
1223.4.1 by Brian Aker
Table Identifier patch.
170
  }
171
1802.12.1 by Brian Aker
This solves bug lp:654905
172
  static bool isView(message::Table::TableType arg) // Not a SQL view, but a view for I_S
173
  {
174
    switch (arg)
175
    {
176
    default:
177
    case message::Table::STANDARD:
178
    case message::Table::TEMPORARY:
179
    case message::Table::INTERNAL:
180
      break;
181
    case message::Table::FUNCTION:
182
      return true;
183
    }
184
185
    return false;
186
  }
187
1643.3.11 by Brian Aker
Encapsulate a bit of logic.
188
  bool isView() const // Not a SQL view, but a view for I_S
189
  {
1802.12.1 by Brian Aker
This solves bug lp:654905
190
    return isView(type);
1643.3.11 by Brian Aker
Encapsulate a bit of logic.
191
  }
192
1395.1.9 by Brian Aker
Small cleanup around how we do types internally.
193
  Type getType() const
194
  {
195
    return type;
196
  }
197
1954.2.1 by Brian Aker
getSQLPath() modified to take a string so that we can const the table
198
  void getSQLPath(std::string &sql_path) const;
1320.1.5 by Brian Aker
Second pass through remove proto write outside of SE.
199
1608.2.1 by Brian Aker
Modified to table identifier to fix temporary table creation loss of file.
200
  const std::string &getPath() const;
1358.1.9 by Brian Aker
Update for std::string
201
1395.1.14 by Brian Aker
Fix for Innodb dfe files.
202
  void setPath(const std::string &new_path)
203
  {
204
    path= new_path;
205
  }
206
1411.3.3 by Monty Taylor
Made getTableName() return a const reference instead of a temporary const
207
  const std::string &getTableName() const
1358.1.9 by Brian Aker
Update for std::string
208
  {
209
    return table_name;
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
210
  }
211
1618.1.1 by Brian Aker
Modify TableIdentifier to be const
212
  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.
213
1596.1.4 by Monty Taylor
Fixed TableIdentifier sorting. The customer operator<() wasn't getting called on Sun Studio.
214
  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.
215
  {
1669.2.1 by Brian Aker
Modify how we make comparisons for tables.
216
    if (left.getKey() < right.getKey())
1429.1.3 by Brian Aker
Merge in work for fetching a list of table identifiers.
217
    {
218
      return true;
219
    }
220
221
    return false;
222
  }
223
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
224
  friend std::ostream& operator<<(std::ostream& output, const TableIdentifier &identifier)
225
  {
226
    const char *type_str;
227
228
    output << "TableIdentifier:(";
1415 by Brian Aker
Mass overhaul to use schema_identifier.
229
    output <<  identifier.getSchemaName();
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
230
    output << ", ";
231
    output << identifier.getTableName();
232
    output << ", ";
233
234
    switch (identifier.type) {
1395.1.7 by Brian Aker
Removed the internal table type in favor of the protobuf one.
235
    case message::Table::STANDARD:
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
236
      type_str= "standard";
237
      break;
1395.1.7 by Brian Aker
Removed the internal table type in favor of the protobuf one.
238
    case message::Table::INTERNAL:
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
239
      type_str= "internal";
240
      break;
1395.1.7 by Brian Aker
Removed the internal table type in favor of the protobuf one.
241
    case message::Table::TEMPORARY:
1235.1.3 by Brian Aker
Remove the need for trans/non-trans temp tables for lock conditions.
242
      type_str= "temporary";
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
243
      break;
1395.1.7 by Brian Aker
Removed the internal table type in favor of the protobuf one.
244
    case message::Table::FUNCTION:
245
      type_str= "function";
246
      break;
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
247
    }
248
249
    output << type_str;
1395.1.14 by Brian Aker
Fix for Innodb dfe files.
250
    output << ", ";
251
    output << identifier.path;
1608.2.1 by Brian Aker
Modified to table identifier to fix temporary table creation loss of file.
252
    output << ", ";
253
    output << identifier.getHashValue();
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
254
    output << ")";
255
256
    return output;  // for multiple << operators.
257
  }
258
1395.1.6 by Brian Aker
Modified TableIdentifier output for errors.
259
  friend bool operator==(TableIdentifier &left, TableIdentifier &right)
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
260
  {
1843.8.2 by Brian Aker
Slight optimization on comparison.
261
    if (left.getHashValue() == right.getHashValue())
262
    {
263
      if (left.getKey() == right.getKey())
264
        return true;
265
    }
1223.4.1 by Brian Aker
Table Identifier patch.
266
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
267
    return false;
268
  }
269
1601 by Brian Aker
Move functions to class methods.
270
  static uint32_t filename_to_tablename(const char *from, char *to, uint32_t to_length);
1864.3.9 by Brian Aker
Remove a couple of memset and now just build the table name in the string of
271
  static size_t build_table_filename(std::string &path, const std::string &db, const std::string &table_name, bool is_tmp);
1618 by Brian Aker
This is a rollup set of patches for modifications to TableIdentifier to have
272
  static size_t build_tmptable_filename(std::string &buffer);
273
  static size_t build_tmptable_filename(std::vector<char> &buffer);
274
1864.3.17 by Brian Aker
Simply the create of an identifier key.
275
public:
1601 by Brian Aker
Move functions to class methods.
276
1608.2.1 by Brian Aker
Modified to table identifier to fix temporary table creation loss of file.
277
  size_t getHashValue() const
278
  {
279
    return hash_value;
280
  }
1618 by Brian Aker
This is a rollup set of patches for modifications to TableIdentifier to have
281
282
  const Key &getKey() const
283
  {
284
    return key;
285
  }
1223.4.1 by Brian Aker
Table Identifier patch.
286
};
287
1608.2.1 by Brian Aker
Modified to table identifier to fix temporary table creation loss of file.
288
std::size_t hash_value(TableIdentifier const& b);
1878.5.3 by Brian Aker
Update Key to work a bit faster.
289
std::size_t hash_value(TableIdentifier::Key const& b);
1608.2.1 by Brian Aker
Modified to table identifier to fix temporary table creation loss of file.
290
1223.4.1 by Brian Aker
Table Identifier patch.
291
} /* namespace drizzled */
292
1660.1.1 by Brian Aker
Merge in move identifier work.
293
#endif /* DRIZZLED_IDENTIFIER_TABLE_H */