~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
1223.4.1 by Brian Aker
Table Identifier patch.
40
#include <string.h>
41
1341 by Brian Aker
See if these are now cleaned up.
42
#include <assert.h>
43
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
44
#include <ostream>
1309.1.11 by Brian Aker
Small helper bit for dealing with lists of table_identifiers.
45
#include <set>
1358.1.5 by Brian Aker
Cleans up use of static buffer in TableIdentifier.
46
#include <algorithm>
47
#include <functional>
1223.4.1 by Brian Aker
Table Identifier patch.
48
1608.2.1 by Brian Aker
Modified to table identifier to fix temporary table creation loss of file.
49
#include <boost/functional/hash.hpp>
50
2119.4.1 by Monty Taylor
Turns on -fvisibility=hidden by default. Symbols intended to be used by
51
#include "drizzled/visibility.h"
52
1223.4.1 by Brian Aker
Table Identifier patch.
53
namespace drizzled {
1417 by Brian Aker
Interface for Stewart.
54
class Table;
55
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
56
namespace identifier {
57
2126.2.1 by Brian Aker
Merge in changes for catalogs usage of constants for identifier.
58
class DRIZZLED_API Table : public Schema
1223.4.1 by Brian Aker
Table Identifier patch.
59
{
1395.1.9 by Brian Aker
Small cleanup around how we do types internally.
60
public:
61
  typedef message::Table::TableType Type;
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
62
  typedef std::vector <Table> vector;
63
  typedef const Table& const_reference;
64
  typedef Table& reference;
1878.5.3 by Brian Aker
Update Key to work a bit faster.
65
66
  class Key
67
  {
68
    std::vector<char> key_buffer;
69
    size_t hash_value;
70
71
  public:
72
73
    Key() :
74
      hash_value(0)
75
    {
76
    }
77
78
    const char *vector() const
79
    {
80
      return &key_buffer[0];
81
    }
82
83
    std::vector<char> &vectorPtr()
84
    {
85
      return key_buffer;
86
    }
87
88
    void set(size_t resize_arg, const std::string &a, const std::string &b);
89
90
    friend bool operator==(const Key &left, const Key &right)
91
    {
92
      if (left.hash_value == right.hash_value and left.key_buffer.size() == right.key_buffer.size())
93
      {
94
        if (memcmp(&left.key_buffer[0], &right.key_buffer[0], left.key_buffer.size()) == 0)
95
          return true;
96
      }
97
98
      return false;
99
    }
100
101
    friend bool operator<(const Key &left, const Key &right)
102
    {
103
      return left.key_buffer < right.key_buffer;
104
    }
105
106
    size_t size() const
107
    {
108
      return key_buffer.size();
109
    }
110
111
    size_t getHashValue() const
112
    {
113
      return hash_value;
114
    }
115
  };
116
1395.1.9 by Brian Aker
Small cleanup around how we do types internally.
117
private:
118
119
  Type type;
1358.1.5 by Brian Aker
Cleans up use of static buffer in TableIdentifier.
120
  std::string path;
2139.3.2 by Brian Aker
Fix innodb to just directly use the identifier (this way we only need to
121
  std::string key_path;
1341 by Brian Aker
See if these are now cleaned up.
122
  std::string table_name;
1618 by Brian Aker
This is a rollup set of patches for modifications to TableIdentifier to have
123
  Key key;
1608.2.1 by Brian Aker
Modified to table identifier to fix temporary table creation loss of file.
124
  size_t hash_value;
1223.4.1 by Brian Aker
Table Identifier patch.
125
1415 by Brian Aker
Mass overhaul to use schema_identifier.
126
  void init();
1395.1.6 by Brian Aker
Modified TableIdentifier output for errors.
127
1618 by Brian Aker
This is a rollup set of patches for modifications to TableIdentifier to have
128
  size_t getKeySize() const
129
  {
130
    return getSchemaName().size() + getTableName().size() + 2;
131
  }
132
1223.4.1 by Brian Aker
Table Identifier patch.
133
public:
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
134
135
  Table(const drizzled::Table &table);
1417 by Brian Aker
Interface for Stewart.
136
                   
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
137
  Table(const identifier::Schema &schema,
138
        const std::string &table_name_arg,
139
        Type tmp_arg= message::Table::STANDARD) :
2087.4.1 by Brian Aker
Merge in schema identifier.
140
    Schema(schema),
1415 by Brian Aker
Mass overhaul to use schema_identifier.
141
    type(tmp_arg),
142
    table_name(table_name_arg)
143
  { 
144
    init();
145
  }
146
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
147
  Table( const std::string &db_arg,
1358.1.5 by Brian Aker
Cleans up use of static buffer in TableIdentifier.
148
                   const std::string &table_name_arg,
1395.1.9 by Brian Aker
Small cleanup around how we do types internally.
149
                   Type tmp_arg= message::Table::STANDARD) :
2087.4.1 by Brian Aker
Merge in schema identifier.
150
    Schema(db_arg),
1223.4.1 by Brian Aker
Table Identifier patch.
151
    type(tmp_arg),
1395.1.14 by Brian Aker
Fix for Innodb dfe files.
152
    table_name(table_name_arg)
1320.1.5 by Brian Aker
Second pass through remove proto write outside of SE.
153
  { 
1415 by Brian Aker
Mass overhaul to use schema_identifier.
154
    init();
1320.1.5 by Brian Aker
Second pass through remove proto write outside of SE.
155
  }
1223.4.1 by Brian Aker
Table Identifier patch.
156
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
157
  Table( const std::string &schema_name_arg,
1395.1.14 by Brian Aker
Fix for Innodb dfe files.
158
                   const std::string &table_name_arg,
159
                   const std::string &path_arg ) :
2087.4.1 by Brian Aker
Merge in schema identifier.
160
    Schema(schema_name_arg),
1395.1.7 by Brian Aker
Removed the internal table type in favor of the protobuf one.
161
    type(message::Table::TEMPORARY),
1389 by Brian Aker
Large reord in ALTER TABLE for RENAME.
162
    path(path_arg),
163
    table_name(table_name_arg)
164
  { 
1415 by Brian Aker
Mass overhaul to use schema_identifier.
165
    init();
1358.1.2 by Brian Aker
Long pass through the system to use more of TableIdentifiers.
166
  }
167
2087.4.1 by Brian Aker
Merge in schema identifier.
168
  using Schema::compare;
1415 by Brian Aker
Mass overhaul to use schema_identifier.
169
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
170
  bool isTmp() const
1223.4.1 by Brian Aker
Table Identifier patch.
171
  {
1395.1.7 by Brian Aker
Removed the internal table type in favor of the protobuf one.
172
    if (type == message::Table::TEMPORARY || type == message::Table::INTERNAL)
173
      return true;
2134.1.7 by Brian Aker
Collapse strings used for table.
174
1395.1.7 by Brian Aker
Removed the internal table type in favor of the protobuf one.
175
    return false;
1223.4.1 by Brian Aker
Table Identifier patch.
176
  }
177
1802.12.1 by Brian Aker
This solves bug lp:654905
178
  static bool isView(message::Table::TableType arg) // Not a SQL view, but a view for I_S
179
  {
180
    switch (arg)
181
    {
182
    default:
183
    case message::Table::STANDARD:
184
    case message::Table::TEMPORARY:
185
    case message::Table::INTERNAL:
186
      break;
187
    case message::Table::FUNCTION:
188
      return true;
189
    }
190
191
    return false;
192
  }
193
1643.3.11 by Brian Aker
Encapsulate a bit of logic.
194
  bool isView() const // Not a SQL view, but a view for I_S
195
  {
1802.12.1 by Brian Aker
This solves bug lp:654905
196
    return isView(type);
1643.3.11 by Brian Aker
Encapsulate a bit of logic.
197
  }
198
1395.1.9 by Brian Aker
Small cleanup around how we do types internally.
199
  Type getType() const
200
  {
201
    return type;
202
  }
203
1992.4.1 by Brian Aker
Update code for testing identifiers/table/schema name correctness.
204
  virtual void getSQLPath(std::string &sql_path) const;
1320.1.5 by Brian Aker
Second pass through remove proto write outside of SE.
205
1992.4.1 by Brian Aker
Update code for testing identifiers/table/schema name correctness.
206
  virtual const std::string &getPath() const;
2139.3.2 by Brian Aker
Fix innodb to just directly use the identifier (this way we only need to
207
  const std::string &getKeyPath() const;
1358.1.9 by Brian Aker
Update for std::string
208
1395.1.14 by Brian Aker
Fix for Innodb dfe files.
209
  void setPath(const std::string &new_path)
210
  {
211
    path= new_path;
212
  }
213
1411.3.3 by Monty Taylor
Made getTableName() return a const reference instead of a temporary const
214
  const std::string &getTableName() const
1358.1.9 by Brian Aker
Update for std::string
215
  {
216
    return table_name;
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
217
  }
218
1618.1.1 by Brian Aker
Modify TableIdentifier to be const
219
  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.
220
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
221
  friend bool operator<(Table::const_reference left, Table::const_reference right)
1429.1.3 by Brian Aker
Merge in work for fetching a list of table identifiers.
222
  {
1669.2.1 by Brian Aker
Modify how we make comparisons for tables.
223
    if (left.getKey() < right.getKey())
1429.1.3 by Brian Aker
Merge in work for fetching a list of table identifiers.
224
    {
225
      return true;
226
    }
227
228
    return false;
229
  }
230
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
231
  friend bool operator==(Table::const_reference left, Table::const_reference right)
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
232
  {
1843.8.2 by Brian Aker
Slight optimization on comparison.
233
    if (left.getHashValue() == right.getHashValue())
234
    {
235
      if (left.getKey() == right.getKey())
236
        return true;
237
    }
1223.4.1 by Brian Aker
Table Identifier patch.
238
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
239
    return false;
240
  }
241
1601 by Brian Aker
Move functions to class methods.
242
  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
243
  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
244
  static size_t build_tmptable_filename(std::string &buffer);
245
  static size_t build_tmptable_filename(std::vector<char> &buffer);
246
1864.3.17 by Brian Aker
Simply the create of an identifier key.
247
public:
1992.4.1 by Brian Aker
Update code for testing identifiers/table/schema name correctness.
248
  bool isValid() const;
1601 by Brian Aker
Move functions to class methods.
249
1608.2.1 by Brian Aker
Modified to table identifier to fix temporary table creation loss of file.
250
  size_t getHashValue() const
251
  {
252
    return hash_value;
253
  }
1618 by Brian Aker
This is a rollup set of patches for modifications to TableIdentifier to have
254
255
  const Key &getKey() const
256
  {
257
    return key;
258
  }
1223.4.1 by Brian Aker
Table Identifier patch.
259
};
260
2134.1.7 by Brian Aker
Collapse strings used for table.
261
std::ostream& operator<<(std::ostream& output, Table::const_reference identifier);
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
262
std::size_t hash_value(Table const& b);
263
std::size_t hash_value(Table::Key const& b);
1608.2.1 by Brian Aker
Modified to table identifier to fix temporary table creation loss of file.
264
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
265
} /* namespace identifier */
1223.4.1 by Brian Aker
Table Identifier patch.
266
} /* namespace drizzled */
267
1660.1.1 by Brian Aker
Merge in move identifier work.
268
#endif /* DRIZZLED_IDENTIFIER_TABLE_H */