~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/identifier/table.h

  • Committer: Stewart Smith
  • Date: 2010-08-05 16:41:55 UTC
  • mfrom: (1638.9.13)
  • mto: (1720.1.1 drizzle)
  • mto: This revision was merged to the branch mainline in revision 1721.
  • Revision ID: stewart@flamingspork.com-20100805164155-7olu6iv6rwoxfsne
Merged store-foreign-key-in-table-proto into show-create-table-using-table-message.

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
4
5
 *  Copyright (C) 2009 Sun Microsystems
5
6
 *
6
7
 *  This program is free software; you can redistribute it and/or modify
29
30
  This will replace Table_ident.
30
31
  */
31
32
 
32
 
#ifndef DRIZZLED_TABLE_IDENTIFIER_H
33
 
#define DRIZZLED_TABLE_IDENTIFIER_H
 
33
#ifndef DRIZZLED_IDENTIFIER_TABLE_H
 
34
#define DRIZZLED_IDENTIFIER_TABLE_H
34
35
 
35
36
#include <drizzled/enum.h>
36
37
#include "drizzled/definitions.h"
37
38
#include "drizzled/message/table.pb.h"
38
39
 
39
 
#include "drizzled/schema_identifier.h"
 
40
#include "drizzled/identifier.h"
40
41
 
41
42
#include <string.h>
42
43
 
47
48
#include <algorithm>
48
49
#include <functional>
49
50
 
 
51
#include <boost/functional/hash.hpp>
 
52
 
50
53
namespace drizzled {
51
54
 
52
55
class Table;
53
56
 
54
 
uint32_t filename_to_tablename(const char *from, char *to, uint32_t to_length);
55
 
size_t build_table_filename(std::string &buff, const char *db, const char *table_name, bool is_tmp);
56
 
 
57
57
class TableIdentifier : public SchemaIdentifier
58
58
{
59
59
public:
60
60
  typedef message::Table::TableType Type;
 
61
  typedef std::vector<char> Key;
61
62
private:
62
63
 
63
64
  Type type;
64
65
  std::string path;
65
66
  std::string table_name;
66
 
  std::string lower_table_name;
67
67
  std::string sql_path;
 
68
  Key key;
 
69
  size_t hash_value;
68
70
 
69
71
  void init();
70
72
 
 
73
  size_t getKeySize() const
 
74
  {
 
75
    return getSchemaName().size() + getTableName().size() + 2;
 
76
  }
 
77
 
71
78
public:
72
79
  TableIdentifier(const Table &table);
73
80
                   
103
110
  }
104
111
 
105
112
  using SchemaIdentifier::compare;
106
 
  bool compare(std::string schema_arg, std::string table_arg);
107
113
 
108
114
  bool isTmp() const
109
115
  {
112
118
    return false;
113
119
  }
114
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
 
115
128
  Type getType() const
116
129
  {
117
130
    return type;
119
132
 
120
133
  const std::string &getSQLPath();
121
134
 
122
 
  const std::string &getPath();
 
135
  const std::string &getPath() const;
123
136
 
124
137
  void setPath(const std::string &new_path)
125
138
  {
131
144
    return table_name;
132
145
  }
133
146
 
134
 
  void copyToTableMessage(message::Table &message);
 
147
  void copyToTableMessage(message::Table &message) const;
135
148
 
136
 
  bool operator<(TableIdentifier &right)
 
149
  friend bool operator<(const TableIdentifier &left, const TableIdentifier &right)
137
150
  {
138
 
    int first= getLower().compare(right.getLower());
139
 
 
140
 
    if (first < 0)
 
151
    if (left.getKey() < right.getKey())
141
152
    {
142
153
      return true;
143
154
    }
144
 
    else if (first > 0)
145
 
    {
146
 
      return false;
147
 
    }
148
 
    else
149
 
    {
150
 
      int val= lower_table_name.compare(right.lower_table_name);
151
 
 
152
 
      if (val < 0)
153
 
      {
154
 
        return true;
155
 
      }
156
 
      else if (val > 0)
157
 
      {
158
 
        return false;
159
 
      }
160
 
      else
161
 
      {
162
 
        if (type < right.type)
163
 
        {
164
 
          return true;
165
 
        }
166
 
      }
167
 
    }
168
155
 
169
156
    return false;
170
157
  }
197
184
    output << type_str;
198
185
    output << ", ";
199
186
    output << identifier.path;
 
187
    output << ", ";
 
188
    output << identifier.getHashValue();
200
189
    output << ")";
201
190
 
202
191
    return output;  // for multiple << operators.
204
193
 
205
194
  friend bool operator==(TableIdentifier &left, TableIdentifier &right)
206
195
  {
207
 
    if (left.type == right.type)
208
 
    {
209
 
      if (static_cast<SchemaIdentifier &>(left) == static_cast<SchemaIdentifier &>(right))
210
 
      {
211
 
        if (left.lower_table_name == right.lower_table_name)
212
 
        {
213
 
          return true;
214
 
        }
215
 
      }
216
 
    }
 
196
    if (left.getKey() == right.getKey())
 
197
      return true;
217
198
 
218
199
    return false;
219
200
  }
220
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
  }
221
263
};
222
264
 
223
 
typedef std::vector <TableIdentifier> TableIdentifierList;
224
 
typedef std::list <TableIdentifier> TableIdentifiers;
 
265
std::size_t hash_value(TableIdentifier const& b);
 
266
 
 
267
typedef std::vector <TableIdentifier> TableIdentifiers;
225
268
 
226
269
} /* namespace drizzled */
227
270
 
228
 
#endif /* DRIZZLED_TABLE_IDENTIFIER_H */
 
271
#endif /* DRIZZLED_IDENTIFIER_TABLE_H */