~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/identifier/table.h

Fixed for null values in transaction log.

Show diffs side-by-side

added added

removed removed

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