~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table_identifier.h

  • Committer: Stewart Smith
  • Date: 2010-02-05 01:20:18 UTC
  • mto: (1286.1.1 build)
  • mto: This revision was merged to the branch mainline in revision 1287.
  • Revision ID: stewart@flamingspork.com-20100205012018-blvmeky20wze8eyg
initial TableProtoTester engine. We can't just use table_write as some protobuf library versions don't let us write invalid protobuf messages :(

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
36
#include "drizzled/definitions.h"
38
 
#include "drizzled/message/table.pb.h"
39
 
 
40
37
#include <string.h>
41
38
 
42
 
#include <assert.h>
43
 
 
44
39
#include <ostream>
45
 
#include <set>
46
 
#include <algorithm>
47
 
#include <functional>
48
 
 
49
 
#include <boost/functional/hash.hpp>
50
 
 
51
 
#include "drizzled/visibility.h"
52
40
 
53
41
namespace drizzled {
54
 
class Table;
55
 
 
56
 
namespace identifier {
57
 
 
58
 
class DRIZZLED_API Table : public Schema
 
42
 
 
43
class TableIdentifier
59
44
{
 
45
private:
 
46
  bool path_inited;
 
47
 
 
48
  tmp_table_type type;
 
49
  char path[FN_REFLEN];
 
50
  const char *db;
 
51
  const char *table_name;
 
52
 
60
53
public:
61
 
  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())
 
54
  TableIdentifier( const char *db_arg,
 
55
                   const char *table_name_arg,
 
56
                   tmp_table_type tmp_arg) :
 
57
    path_inited(false),
 
58
    type(tmp_arg),
 
59
    db(db_arg),
 
60
    table_name(table_name_arg)
 
61
  { }
 
62
 
 
63
  bool isTmp() const
 
64
  {
 
65
    return type == NO_TMP_TABLE ? false  : true;
 
66
  }
 
67
 
 
68
  const char *getPath();
 
69
 
 
70
  const char *getDBName() const
 
71
  {
 
72
    return db;
 
73
  }
 
74
 
 
75
  const char *getTableName() const
 
76
  {
 
77
    return table_name;
 
78
  }
 
79
 
 
80
  friend std::ostream& operator<<(std::ostream& output, const TableIdentifier &identifier)
 
81
  {
 
82
    const char *type_str;
 
83
 
 
84
    output << "TableIdentifier:(";
 
85
    output <<  identifier.getDBName();
 
86
    output << ", ";
 
87
    output << identifier.getTableName();
 
88
    output << ", ";
 
89
 
 
90
    switch (identifier.type) {
 
91
    case NO_TMP_TABLE:
 
92
      type_str= "standard";
 
93
      break;
 
94
    case INTERNAL_TMP_TABLE:
 
95
      type_str= "internal";
 
96
      break;
 
97
    case TEMP_TABLE:
 
98
      type_str= "temporary";
 
99
      break;
 
100
    case SYSTEM_TMP_TABLE:
 
101
      type_str= "system";
 
102
    }
 
103
 
 
104
    output << type_str;
 
105
    output << ")";
 
106
 
 
107
    return output;  // for multiple << operators.
 
108
  }
 
109
 
 
110
  friend bool operator==(const TableIdentifier &left, const TableIdentifier &right)
 
111
  {
 
112
    if (left.type == right.type)
 
113
    {
 
114
      if (! strcmp(left.db, right.db))
93
115
      {
94
 
        if (memcmp(&left.key_buffer[0], &right.key_buffer[0], left.key_buffer.size()) == 0)
 
116
        if (! strcmp(left.table_name, right.table_name))
 
117
        {
95
118
          return true;
 
119
        }
96
120
      }
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
 
 
117
 
private:
118
 
 
119
 
  Type type;
120
 
  std::string path;
121
 
  std::string key_path;
122
 
  std::string table_name;
123
 
  Key key;
124
 
  size_t hash_value;
125
 
 
126
 
  void init();
127
 
 
128
 
  size_t getKeySize() const
129
 
  {
130
 
    return getSchemaName().size() + getTableName().size() + 2;
131
 
  }
132
 
 
133
 
public:
134
 
 
135
 
  Table(const drizzled::Table &table);
136
 
                   
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,
158
 
                   const std::string &table_name_arg,
159
 
                   const std::string &path_arg ) :
160
 
    Schema(schema_name_arg),
161
 
    type(message::Table::TEMPORARY),
162
 
    path(path_arg),
163
 
    table_name(table_name_arg)
164
 
  { 
165
 
    init();
166
 
  }
167
 
 
168
 
  using Schema::compare;
169
 
 
170
 
  bool isTmp() const
171
 
  {
172
 
    if (type == message::Table::TEMPORARY || type == message::Table::INTERNAL)
173
 
      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
 
    return false;
192
 
  }
193
 
 
194
 
  bool isView() const // Not a SQL view, but a view for I_S
195
 
  {
196
 
    return isView(type);
197
 
  }
198
 
 
199
 
  Type getType() const
200
 
  {
201
 
    return type;
202
 
  }
203
 
 
204
 
  virtual void getSQLPath(std::string &sql_path) const;
205
 
 
206
 
  virtual const std::string &getPath() const;
207
 
  const std::string &getKeyPath() const;
208
 
 
209
 
  void setPath(const std::string &new_path)
210
 
  {
211
 
    path= new_path;
212
 
  }
213
 
 
214
 
  const std::string &getTableName() const
215
 
  {
216
 
    return table_name;
217
 
  }
218
 
 
219
 
  void copyToTableMessage(message::Table &message) const;
220
 
 
221
 
  friend bool operator<(Table::const_reference left, Table::const_reference right)
222
 
  {
223
 
    if (left.getKey() < right.getKey())
224
 
    {
225
 
      return true;
226
 
    }
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())
236
 
        return true;
237
 
    }
238
 
 
239
 
    return false;
240
 
  }
241
 
 
242
 
  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);
244
 
  static size_t build_tmptable_filename(std::string &buffer);
245
 
  static size_t build_tmptable_filename(std::vector<char> &buffer);
246
 
 
247
 
public:
248
 
  bool isValid() const;
249
 
 
250
 
  size_t getHashValue() const
251
 
  {
252
 
    return hash_value;
253
 
  }
254
 
 
255
 
  const Key &getKey() const
256
 
  {
257
 
    return key;
258
 
  }
 
121
    }
 
122
 
 
123
    return false;
 
124
  }
 
125
 
259
126
};
260
127
 
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 */
266
128
} /* namespace drizzled */
267
129
 
268
 
#endif /* DRIZZLED_IDENTIFIER_TABLE_H */
 
130
#endif /* DRIZZLED_TABLE_IDENTIFIER_H */