~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table_identifier.h

  • Committer: Monty Taylor
  • Date: 2010-07-09 14:06:50 UTC
  • Revision ID: mordred@inaugust.com-20100709140650-ojeih829v3wbdkyv
Added include guard for generator.h to make cpplint happy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2010 Brian Aker
5
 
 *  Copyright (C) 2009 Sun Microsystems, Inc.
 
4
 *  Copyright (C) 2009 Sun Microsystems
6
5
 *
7
6
 *  This program is free software; you can redistribute it and/or modify
8
7
 *  it under the terms of the GNU General Public License as published by
30
29
  This will replace Table_ident.
31
30
  */
32
31
 
33
 
#ifndef DRIZZLED_IDENTIFIER_TABLE_H
34
 
#define DRIZZLED_IDENTIFIER_TABLE_H
 
32
#ifndef DRIZZLED_TABLE_IDENTIFIER_H
 
33
#define DRIZZLED_TABLE_IDENTIFIER_H
35
34
 
36
35
#include <drizzled/enum.h>
37
 
#include <drizzled/definitions.h>
38
 
#include <drizzled/message/table.pb.h>
 
36
#include "drizzled/definitions.h"
 
37
#include "drizzled/message/table.pb.h"
 
38
 
 
39
#include "drizzled/schema_identifier.h"
39
40
 
40
41
#include <string.h>
41
42
 
48
49
 
49
50
#include <boost/functional/hash.hpp>
50
51
 
51
 
#include <drizzled/visibility.h>
52
 
 
53
52
namespace drizzled {
 
53
 
54
54
class Table;
55
55
 
56
 
namespace identifier {
57
 
 
58
 
class DRIZZLED_API Table : public Schema
 
56
class TableIdentifier : public SchemaIdentifier
59
57
{
60
58
public:
61
59
  typedef message::Table::TableType Type;
62
 
  typedef std::vector <Table> vector;
63
 
  typedef const Table& const_reference;
64
 
  typedef Table& reference;
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
 
 
 
60
  typedef std::vector<char> Key;
117
61
private:
118
62
 
119
63
  Type type;
120
64
  std::string path;
121
 
  std::string key_path;
122
65
  std::string table_name;
 
66
  std::string lower_table_name;
 
67
  std::string sql_path;
123
68
  Key key;
124
69
  size_t hash_value;
125
70
 
131
76
  }
132
77
 
133
78
public:
134
 
 
135
 
  Table(const drizzled::Table &table);
 
79
  TableIdentifier(const Table &table);
136
80
                   
137
 
  Table(const identifier::Schema &schema,
138
 
        const std::string &table_name_arg,
139
 
        Type tmp_arg= message::Table::STANDARD) :
140
 
    Schema(schema),
141
 
    type(tmp_arg),
142
 
    table_name(table_name_arg)
143
 
  { 
144
 
    init();
145
 
  }
146
 
 
147
 
  Table( const std::string &db_arg,
148
 
                   const std::string &table_name_arg,
149
 
                   Type tmp_arg= message::Table::STANDARD) :
150
 
    Schema(db_arg),
151
 
    type(tmp_arg),
152
 
    table_name(table_name_arg)
153
 
  { 
154
 
    init();
155
 
  }
156
 
 
157
 
  Table( const std::string &schema_name_arg,
 
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,
158
102
                   const std::string &table_name_arg,
159
103
                   const std::string &path_arg ) :
160
 
    Schema(schema_name_arg),
 
104
    SchemaIdentifier(schema_name_arg),
161
105
    type(message::Table::TEMPORARY),
162
106
    path(path_arg),
163
107
    table_name(table_name_arg)
165
109
    init();
166
110
  }
167
111
 
168
 
  using Schema::compare;
 
112
  using SchemaIdentifier::compare;
 
113
  bool compare(std::string schema_arg, std::string table_arg) const;
169
114
 
170
115
  bool isTmp() const
171
116
  {
172
117
    if (type == message::Table::TEMPORARY || type == message::Table::INTERNAL)
173
118
      return true;
174
 
 
175
 
    return false;
176
 
  }
177
 
 
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
119
    return false;
192
120
  }
193
121
 
194
122
  bool isView() const // Not a SQL view, but a view for I_S
195
123
  {
196
 
    return isView(type);
 
124
    if (type == message::Table::FUNCTION)
 
125
      return true;
 
126
    return false;
197
127
  }
198
128
 
199
129
  Type getType() const
201
131
    return type;
202
132
  }
203
133
 
204
 
  virtual void getSQLPath(std::string &sql_path) const;
 
134
  const std::string &getSQLPath();
205
135
 
206
 
  virtual const std::string &getPath() const;
207
 
  const std::string &getKeyPath() const;
 
136
  const std::string &getPath() const;
208
137
 
209
138
  void setPath(const std::string &new_path)
210
139
  {
218
147
 
219
148
  void copyToTableMessage(message::Table &message) const;
220
149
 
221
 
  friend bool operator<(Table::const_reference left, Table::const_reference right)
 
150
  friend bool operator<(const TableIdentifier &left, const TableIdentifier &right)
222
151
  {
223
 
    if (left.getKey() < right.getKey())
 
152
    // Compare the schema names. You must use schema, path is not valid for
 
153
    // this operation.
 
154
    int first= left.getLower().compare(right.getLower());
 
155
    if (first < 0)
224
156
    {
225
157
      return true;
226
158
    }
227
 
 
228
 
    return false;
229
 
  }
230
 
 
231
 
  friend bool operator==(Table::const_reference left, Table::const_reference right)
232
 
  {
233
 
    if (left.getHashValue() == right.getHashValue())
234
 
    {
235
 
      if (left.getKey() == right.getKey())
 
159
    else if (first > 0)
 
160
    {
 
161
      return false;
 
162
    }
 
163
    else
 
164
    {
 
165
      // Compare the tables names.
 
166
      int val= left.lower_table_name.compare(right.lower_table_name);
 
167
 
 
168
      if (val < 0)
 
169
      {
236
170
        return true;
 
171
      }
 
172
      else if (val > 0)
 
173
      {
 
174
        return false;
 
175
      }
 
176
      else
 
177
      {
 
178
        if (left.type < right.type)
 
179
        {
 
180
          return true;
 
181
        }
 
182
      }
 
183
    }
 
184
 
 
185
    return false;
 
186
  }
 
187
 
 
188
  friend std::ostream& operator<<(std::ostream& output, const TableIdentifier &identifier)
 
189
  {
 
190
    const char *type_str;
 
191
 
 
192
    output << "TableIdentifier:(";
 
193
    output <<  identifier.getSchemaName();
 
194
    output << ", ";
 
195
    output << identifier.getTableName();
 
196
    output << ", ";
 
197
 
 
198
    switch (identifier.type) {
 
199
    case message::Table::STANDARD:
 
200
      type_str= "standard";
 
201
      break;
 
202
    case message::Table::INTERNAL:
 
203
      type_str= "internal";
 
204
      break;
 
205
    case message::Table::TEMPORARY:
 
206
      type_str= "temporary";
 
207
      break;
 
208
    case message::Table::FUNCTION:
 
209
      type_str= "function";
 
210
      break;
 
211
    }
 
212
 
 
213
    output << type_str;
 
214
    output << ", ";
 
215
    output << identifier.path;
 
216
    output << ", ";
 
217
    output << identifier.getHashValue();
 
218
    output << ")";
 
219
 
 
220
    return output;  // for multiple << operators.
 
221
  }
 
222
 
 
223
  friend bool operator==(TableIdentifier &left, TableIdentifier &right)
 
224
  {
 
225
    if (left.type == right.type)
 
226
    {
 
227
      if (static_cast<SchemaIdentifier &>(left) == static_cast<SchemaIdentifier &>(right))
 
228
      {
 
229
        if (left.lower_table_name == right.lower_table_name)
 
230
        {
 
231
          return true;
 
232
        }
 
233
      }
237
234
    }
238
235
 
239
236
    return false;
240
237
  }
241
238
 
242
239
  static uint32_t filename_to_tablename(const char *from, char *to, uint32_t to_length);
243
 
  static size_t build_table_filename(std::string &path, const std::string &db, const std::string &table_name, bool is_tmp);
 
240
  static size_t build_table_filename(std::string &buff, const char *db, const char *table_name, bool is_tmp);
244
241
  static size_t build_tmptable_filename(std::string &buffer);
245
242
  static size_t build_tmptable_filename(std::vector<char> &buffer);
246
243
 
247
 
public:
248
 
  bool isValid() const;
 
244
  /*
 
245
    Create a table cache key
 
246
 
 
247
    SYNOPSIS
 
248
    createKey()
 
249
    key                 Create key here (must be of size MAX_DBKEY_LENGTH)
 
250
    table_list          Table definition
 
251
 
 
252
    IMPLEMENTATION
 
253
    The table cache_key is created from:
 
254
    db_name + \0
 
255
    table_name + \0
 
256
 
 
257
    if the table is a tmp table, we add the following to make each tmp table
 
258
    unique on the slave:
 
259
 
 
260
    4 bytes for master thread id
 
261
    4 bytes pseudo thread id
 
262
 
 
263
    RETURN
 
264
    Length of key
 
265
  */
 
266
  static uint32_t createKey(char *key, const char *db_arg, const char *table_name_arg)
 
267
  {
 
268
    uint32_t key_length;
 
269
    char *key_pos= key;
 
270
 
 
271
    key_pos= strcpy(key_pos, db_arg) + strlen(db_arg);
 
272
    key_pos= strcpy(key_pos+1, table_name_arg) +
 
273
      strlen(table_name_arg);
 
274
    key_length= (uint32_t)(key_pos-key)+1;
 
275
 
 
276
    return key_length;
 
277
  }
 
278
 
 
279
  static uint32_t createKey(char *key, const TableIdentifier &identifier)
 
280
  {
 
281
    uint32_t key_length;
 
282
    char *key_pos= key;
 
283
 
 
284
    key_pos= strcpy(key_pos, identifier.getSchemaName().c_str()) + identifier.getSchemaName().length();
 
285
    key_pos= strcpy(key_pos + 1, identifier.getTableName().c_str()) + identifier.getTableName().length();
 
286
    key_length= (uint32_t)(key_pos-key)+1;
 
287
 
 
288
    return key_length;
 
289
  }
249
290
 
250
291
  size_t getHashValue() const
251
292
  {
258
299
  }
259
300
};
260
301
 
261
 
std::ostream& operator<<(std::ostream& output, Table::const_reference identifier);
262
 
std::size_t hash_value(Table const& b);
263
 
std::size_t hash_value(Table::Key const& b);
264
 
 
265
 
} /* namespace identifier */
 
302
std::size_t hash_value(TableIdentifier const& b);
 
303
 
 
304
typedef std::vector <TableIdentifier> TableIdentifiers;
 
305
 
266
306
} /* namespace drizzled */
267
307
 
268
 
#endif /* DRIZZLED_IDENTIFIER_TABLE_H */
 
308
#endif /* DRIZZLED_TABLE_IDENTIFIER_H */