~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/identifier/table.h

  • Committer: Monty Taylor
  • Date: 2008-09-15 00:46:33 UTC
  • mfrom: (383.1.55 client-split)
  • Revision ID: monty@inaugust.com-20080915004633-fmjw27fi41cxs35w
Merged from client-split.

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<char> Key;
62
 
private:
63
 
 
64
 
  Type type;
65
 
  std::string path;
66
 
  std::string table_name;
67
 
  std::string sql_path;
68
 
  Key key;
69
 
  size_t hash_value;
70
 
 
71
 
  void init();
72
 
 
73
 
  size_t getKeySize() const
74
 
  {
75
 
    return getSchemaName().size() + getTableName().size() + 2;
76
 
  }
77
 
 
78
 
public:
79
 
  TableIdentifier(const Table &table);
80
 
                   
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
 
 
91
 
  TableIdentifier( const std::string &db_arg,
92
 
                   const std::string &table_name_arg,
93
 
                   Type tmp_arg= message::Table::STANDARD) :
94
 
    SchemaIdentifier(db_arg),
95
 
    type(tmp_arg),
96
 
    table_name(table_name_arg)
97
 
  { 
98
 
    init();
99
 
  }
100
 
 
101
 
  TableIdentifier( const std::string &schema_name_arg,
102
 
                   const std::string &table_name_arg,
103
 
                   const std::string &path_arg ) :
104
 
    SchemaIdentifier(schema_name_arg),
105
 
    type(message::Table::TEMPORARY),
106
 
    path(path_arg),
107
 
    table_name(table_name_arg)
108
 
  { 
109
 
    init();
110
 
  }
111
 
 
112
 
  using SchemaIdentifier::compare;
113
 
 
114
 
  bool isTmp() const
115
 
  {
116
 
    if (type == message::Table::TEMPORARY || type == message::Table::INTERNAL)
117
 
      return true;
118
 
    return false;
119
 
  }
120
 
 
121
 
  bool isView() const // Not a SQL view, but a view for I_S
122
 
  {
123
 
    if (type == message::Table::FUNCTION)
124
 
      return true;
125
 
    return false;
126
 
  }
127
 
 
128
 
  Type getType() const
129
 
  {
130
 
    return type;
131
 
  }
132
 
 
133
 
  const std::string &getSQLPath();
134
 
 
135
 
  const std::string &getPath() const;
136
 
 
137
 
  void setPath(const std::string &new_path)
138
 
  {
139
 
    path= new_path;
140
 
  }
141
 
 
142
 
  const std::string &getTableName() const
143
 
  {
144
 
    return table_name;
145
 
  }
146
 
 
147
 
  void copyToTableMessage(message::Table &message) const;
148
 
 
149
 
  friend bool operator<(const TableIdentifier &left, const TableIdentifier &right)
150
 
  {
151
 
    if (left.getKey() < right.getKey())
152
 
    {
153
 
      return true;
154
 
    }
155
 
 
156
 
    return false;
157
 
  }
158
 
 
159
 
  friend std::ostream& operator<<(std::ostream& output, const TableIdentifier &identifier)
160
 
  {
161
 
    const char *type_str;
162
 
 
163
 
    output << "TableIdentifier:(";
164
 
    output <<  identifier.getSchemaName();
165
 
    output << ", ";
166
 
    output << identifier.getTableName();
167
 
    output << ", ";
168
 
 
169
 
    switch (identifier.type) {
170
 
    case message::Table::STANDARD:
171
 
      type_str= "standard";
172
 
      break;
173
 
    case message::Table::INTERNAL:
174
 
      type_str= "internal";
175
 
      break;
176
 
    case message::Table::TEMPORARY:
177
 
      type_str= "temporary";
178
 
      break;
179
 
    case message::Table::FUNCTION:
180
 
      type_str= "function";
181
 
      break;
182
 
    }
183
 
 
184
 
    output << type_str;
185
 
    output << ", ";
186
 
    output << identifier.path;
187
 
    output << ", ";
188
 
    output << identifier.getHashValue();
189
 
    output << ")";
190
 
 
191
 
    return output;  // for multiple << operators.
192
 
  }
193
 
 
194
 
  friend bool operator==(TableIdentifier &left, TableIdentifier &right)
195
 
  {
196
 
    if (left.getKey() == right.getKey())
197
 
      return true;
198
 
 
199
 
    return false;
200
 
  }
201
 
 
202
 
  static uint32_t filename_to_tablename(const char *from, char *to, uint32_t to_length);
203
 
  static size_t build_table_filename(std::string &buff, const char *db, const char *table_name, bool is_tmp);
204
 
  static size_t build_tmptable_filename(std::string &buffer);
205
 
  static size_t build_tmptable_filename(std::vector<char> &buffer);
206
 
 
207
 
  /*
208
 
    Create a table cache key
209
 
 
210
 
    SYNOPSIS
211
 
    createKey()
212
 
    key                 Create key here (must be of size MAX_DBKEY_LENGTH)
213
 
    table_list          Table definition
214
 
 
215
 
    IMPLEMENTATION
216
 
    The table cache_key is created from:
217
 
    db_name + \0
218
 
    table_name + \0
219
 
 
220
 
    if the table is a tmp table, we add the following to make each tmp table
221
 
    unique on the slave:
222
 
 
223
 
    4 bytes for master thread id
224
 
    4 bytes pseudo thread id
225
 
 
226
 
    RETURN
227
 
    Length of key
228
 
  */
229
 
  static uint32_t createKey(char *key, const char *db_arg, const char *table_name_arg)
230
 
  {
231
 
    uint32_t key_length;
232
 
    char *key_pos= key;
233
 
 
234
 
    key_pos= strcpy(key_pos, db_arg) + strlen(db_arg);
235
 
    key_pos= strcpy(key_pos+1, table_name_arg) +
236
 
      strlen(table_name_arg);
237
 
    key_length= (uint32_t)(key_pos-key)+1;
238
 
 
239
 
    return key_length;
240
 
  }
241
 
 
242
 
  static uint32_t createKey(char *key, const TableIdentifier &identifier)
243
 
  {
244
 
    uint32_t key_length;
245
 
    char *key_pos= key;
246
 
 
247
 
    key_pos= strcpy(key_pos, identifier.getSchemaName().c_str()) + identifier.getSchemaName().length();
248
 
    key_pos= strcpy(key_pos + 1, identifier.getTableName().c_str()) + identifier.getTableName().length();
249
 
    key_length= (uint32_t)(key_pos-key)+1;
250
 
 
251
 
    return key_length;
252
 
  }
253
 
 
254
 
  size_t getHashValue() const
255
 
  {
256
 
    return hash_value;
257
 
  }
258
 
 
259
 
  const Key &getKey() const
260
 
  {
261
 
    return key;
262
 
  }
263
 
};
264
 
 
265
 
std::size_t hash_value(TableIdentifier const& b);
266
 
 
267
 
typedef std::vector <TableIdentifier> TableIdentifiers;
268
 
 
269
 
} /* namespace drizzled */
270
 
 
271
 
#endif /* DRIZZLED_IDENTIFIER_TABLE_H */