~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/identifier/table.h

  • Committer: Monty Taylor
  • Date: 2009-04-25 19:24:49 UTC
  • mto: (997.2.5 mordred)
  • mto: This revision was merged to the branch mainline in revision 1003.
  • Revision ID: mordred@inaugust.com-20090425192449-0htujbt2r9jzupn5
Moved heap.

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) 2010 Brian Aker
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
 
 
33
 
#ifndef DRIZZLED_IDENTIFIER_TABLE_H
34
 
#define DRIZZLED_IDENTIFIER_TABLE_H
35
 
 
36
 
#include <drizzled/enum.h>
37
 
#include "drizzled/definitions.h"
38
 
#include "drizzled/message/table.pb.h"
39
 
 
40
 
#include "drizzled/identifier.h"
41
 
 
42
 
#include <string.h>
43
 
 
44
 
#include <assert.h>
45
 
 
46
 
#include <ostream>
47
 
#include <set>
48
 
#include <algorithm>
49
 
#include <functional>
50
 
 
51
 
#include <boost/functional/hash.hpp>
52
 
 
53
 
namespace drizzled {
54
 
 
55
 
class Table;
56
 
 
57
 
class TableIdentifier : public SchemaIdentifier
58
 
{
59
 
public:
60
 
  typedef message::Table::TableType Type;
61
 
  typedef std::vector <TableIdentifier> vector;
62
 
  typedef const TableIdentifier& const_reference;
63
 
  typedef TableIdentifier& reference;
64
 
 
65
 
  class Key
66
 
  {
67
 
    std::vector<char> key_buffer;
68
 
    size_t hash_value;
69
 
 
70
 
  public:
71
 
 
72
 
    Key() :
73
 
      hash_value(0)
74
 
    {
75
 
    }
76
 
 
77
 
    const char *vector() const
78
 
    {
79
 
      return &key_buffer[0];
80
 
    }
81
 
 
82
 
    std::vector<char> &vectorPtr()
83
 
    {
84
 
      return key_buffer;
85
 
    }
86
 
 
87
 
    void set(size_t resize_arg, const std::string &a, const std::string &b);
88
 
 
89
 
    friend bool operator==(const Key &left, const Key &right)
90
 
    {
91
 
      if (left.hash_value == right.hash_value and left.key_buffer.size() == right.key_buffer.size())
92
 
      {
93
 
        if (memcmp(&left.key_buffer[0], &right.key_buffer[0], left.key_buffer.size()) == 0)
94
 
          return true;
95
 
      }
96
 
 
97
 
      return false;
98
 
    }
99
 
 
100
 
    friend bool operator<(const Key &left, const Key &right)
101
 
    {
102
 
      return left.key_buffer < right.key_buffer;
103
 
    }
104
 
 
105
 
    size_t size() const
106
 
    {
107
 
      return key_buffer.size();
108
 
    }
109
 
 
110
 
    size_t getHashValue() const
111
 
    {
112
 
      return hash_value;
113
 
    }
114
 
  };
115
 
 
116
 
private:
117
 
 
118
 
  Type type;
119
 
  std::string path;
120
 
  std::string table_name;
121
 
  Key key;
122
 
  size_t hash_value;
123
 
 
124
 
  void init();
125
 
 
126
 
  size_t getKeySize() const
127
 
  {
128
 
    return getSchemaName().size() + getTableName().size() + 2;
129
 
  }
130
 
 
131
 
public:
132
 
  TableIdentifier(const Table &table);
133
 
                   
134
 
  TableIdentifier( const SchemaIdentifier &schema,
135
 
                   const std::string &table_name_arg,
136
 
                   Type tmp_arg= message::Table::STANDARD) :
137
 
    SchemaIdentifier(schema),
138
 
    type(tmp_arg),
139
 
    table_name(table_name_arg)
140
 
  { 
141
 
    init();
142
 
  }
143
 
 
144
 
  TableIdentifier( const std::string &db_arg,
145
 
                   const std::string &table_name_arg,
146
 
                   Type tmp_arg= message::Table::STANDARD) :
147
 
    SchemaIdentifier(db_arg),
148
 
    type(tmp_arg),
149
 
    table_name(table_name_arg)
150
 
  { 
151
 
    init();
152
 
  }
153
 
 
154
 
  TableIdentifier( const std::string &schema_name_arg,
155
 
                   const std::string &table_name_arg,
156
 
                   const std::string &path_arg ) :
157
 
    SchemaIdentifier(schema_name_arg),
158
 
    type(message::Table::TEMPORARY),
159
 
    path(path_arg),
160
 
    table_name(table_name_arg)
161
 
  { 
162
 
    init();
163
 
  }
164
 
 
165
 
  using SchemaIdentifier::compare;
166
 
 
167
 
  bool isTmp() const
168
 
  {
169
 
    if (type == message::Table::TEMPORARY || type == message::Table::INTERNAL)
170
 
      return true;
171
 
    return false;
172
 
  }
173
 
 
174
 
  static bool isView(message::Table::TableType arg) // Not a SQL view, but a view for I_S
175
 
  {
176
 
    switch (arg)
177
 
    {
178
 
    default:
179
 
    case message::Table::STANDARD:
180
 
    case message::Table::TEMPORARY:
181
 
    case message::Table::INTERNAL:
182
 
      break;
183
 
    case message::Table::FUNCTION:
184
 
      return true;
185
 
    }
186
 
 
187
 
    return false;
188
 
  }
189
 
 
190
 
  bool isView() const // Not a SQL view, but a view for I_S
191
 
  {
192
 
    return isView(type);
193
 
  }
194
 
 
195
 
  Type getType() const
196
 
  {
197
 
    return type;
198
 
  }
199
 
 
200
 
  virtual void getSQLPath(std::string &sql_path) const;
201
 
 
202
 
  virtual const std::string &getPath() const;
203
 
 
204
 
  void setPath(const std::string &new_path)
205
 
  {
206
 
    path= new_path;
207
 
  }
208
 
 
209
 
  const std::string &getTableName() const
210
 
  {
211
 
    return table_name;
212
 
  }
213
 
 
214
 
  void copyToTableMessage(message::Table &message) const;
215
 
 
216
 
  friend bool operator<(TableIdentifier::const_reference left, TableIdentifier::const_reference right)
217
 
  {
218
 
    if (left.getKey() < right.getKey())
219
 
    {
220
 
      return true;
221
 
    }
222
 
 
223
 
    return false;
224
 
  }
225
 
 
226
 
  friend std::ostream& operator<<(std::ostream& output, TableIdentifier::const_reference identifier)
227
 
  {
228
 
    const char *type_str;
229
 
 
230
 
    output << "TableIdentifier:(";
231
 
    output <<  identifier.getSchemaName();
232
 
    output << ", ";
233
 
    output << identifier.getTableName();
234
 
    output << ", ";
235
 
 
236
 
    switch (identifier.type) {
237
 
    case message::Table::STANDARD:
238
 
      type_str= "standard";
239
 
      break;
240
 
    case message::Table::INTERNAL:
241
 
      type_str= "internal";
242
 
      break;
243
 
    case message::Table::TEMPORARY:
244
 
      type_str= "temporary";
245
 
      break;
246
 
    case message::Table::FUNCTION:
247
 
      type_str= "function";
248
 
      break;
249
 
    }
250
 
 
251
 
    output << type_str;
252
 
    output << ", ";
253
 
    output << identifier.path;
254
 
    output << ", ";
255
 
    output << identifier.getHashValue();
256
 
    output << ")";
257
 
 
258
 
    return output;  // for multiple << operators.
259
 
  }
260
 
 
261
 
  friend bool operator==(TableIdentifier::const_reference left, TableIdentifier::const_reference right)
262
 
  {
263
 
    if (left.getHashValue() == right.getHashValue())
264
 
    {
265
 
      if (left.getKey() == right.getKey())
266
 
        return true;
267
 
    }
268
 
 
269
 
    return false;
270
 
  }
271
 
 
272
 
  static uint32_t filename_to_tablename(const char *from, char *to, uint32_t to_length);
273
 
  static size_t build_table_filename(std::string &path, const std::string &db, const std::string &table_name, bool is_tmp);
274
 
  static size_t build_tmptable_filename(std::string &buffer);
275
 
  static size_t build_tmptable_filename(std::vector<char> &buffer);
276
 
 
277
 
public:
278
 
  bool isValid() const;
279
 
 
280
 
  size_t getHashValue() const
281
 
  {
282
 
    return hash_value;
283
 
  }
284
 
 
285
 
  const Key &getKey() const
286
 
  {
287
 
    return key;
288
 
  }
289
 
};
290
 
 
291
 
std::size_t hash_value(TableIdentifier const& b);
292
 
std::size_t hash_value(TableIdentifier::Key const& b);
293
 
 
294
 
} /* namespace drizzled */
295
 
 
296
 
#endif /* DRIZZLED_IDENTIFIER_TABLE_H */