~drizzle-trunk/drizzle/development

1223.4.1 by Brian Aker
Table Identifier patch.
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1660.1.1 by Brian Aker
Merge in move identifier work.
4
 *  Copyright (C) 2010 Brian Aker
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
5
 *  Copyright (C) 2009 Sun Microsystems, Inc.
1223.4.1 by Brian Aker
Table Identifier patch.
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
2200 by Brian Aker
Testing pragma once for build farm.
33
#pragma once
1223.4.1 by Brian Aker
Table Identifier patch.
34
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
35
#include <drizzled/enum.h>
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
36
#include <drizzled/definitions.h>
37
#include <drizzled/message/table.pb.h>
1415 by Brian Aker
Mass overhaul to use schema_identifier.
38
1223.4.1 by Brian Aker
Table Identifier patch.
39
#include <string.h>
40
1341 by Brian Aker
See if these are now cleaned up.
41
#include <assert.h>
42
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
43
#include <ostream>
1309.1.11 by Brian Aker
Small helper bit for dealing with lists of table_identifiers.
44
#include <set>
1358.1.5 by Brian Aker
Cleans up use of static buffer in TableIdentifier.
45
#include <algorithm>
46
#include <functional>
1223.4.1 by Brian Aker
Table Identifier patch.
47
1608.2.1 by Brian Aker
Modified to table identifier to fix temporary table creation loss of file.
48
#include <boost/functional/hash.hpp>
49
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
50
#include <drizzled/visibility.h>
2241.4.14 by Stewart Smith
remove some includes from my_sys.h and instead only include where needed. This helps reduce the number of files that have to be rebuilt when you change some of the more widely included header files (such as the drizzled::identifier ones)
51
#include <drizzled/identifier/schema.h>
2119.4.1 by Monty Taylor
Turns on -fvisibility=hidden by default. Symbols intended to be used by
52
1223.4.1 by Brian Aker
Table Identifier patch.
53
namespace drizzled {
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
54
namespace identifier {
55
2126.2.1 by Brian Aker
Merge in changes for catalogs usage of constants for identifier.
56
class DRIZZLED_API Table : public Schema
1223.4.1 by Brian Aker
Table Identifier patch.
57
{
1395.1.9 by Brian Aker
Small cleanup around how we do types internally.
58
public:
59
  typedef message::Table::TableType Type;
1878.5.3 by Brian Aker
Update Key to work a bit faster.
60
61
  class Key
62
  {
63
    std::vector<char> key_buffer;
64
    size_t hash_value;
65
66
  public:
67
68
    Key() :
69
      hash_value(0)
70
    {
71
    }
72
73
    const char *vector() const
74
    {
75
      return &key_buffer[0];
76
    }
77
78
    std::vector<char> &vectorPtr()
79
    {
80
      return key_buffer;
81
    }
82
83
    void set(size_t resize_arg, const std::string &a, const std::string &b);
84
85
    friend bool operator==(const Key &left, const Key &right)
86
    {
87
      if (left.hash_value == right.hash_value and left.key_buffer.size() == right.key_buffer.size())
88
      {
89
        if (memcmp(&left.key_buffer[0], &right.key_buffer[0], left.key_buffer.size()) == 0)
90
          return true;
91
      }
92
93
      return false;
94
    }
95
96
    friend bool operator<(const Key &left, const Key &right)
97
    {
98
      return left.key_buffer < right.key_buffer;
99
    }
100
101
    size_t size() const
102
    {
103
      return key_buffer.size();
104
    }
105
106
    size_t getHashValue() const
107
    {
108
      return hash_value;
109
    }
110
  };
111
1395.1.9 by Brian Aker
Small cleanup around how we do types internally.
112
private:
113
114
  Type type;
1358.1.5 by Brian Aker
Cleans up use of static buffer in TableIdentifier.
115
  std::string path;
2139.3.2 by Brian Aker
Fix innodb to just directly use the identifier (this way we only need to
116
  std::string key_path;
1341 by Brian Aker
See if these are now cleaned up.
117
  std::string table_name;
1618 by Brian Aker
This is a rollup set of patches for modifications to TableIdentifier to have
118
  Key key;
1608.2.1 by Brian Aker
Modified to table identifier to fix temporary table creation loss of file.
119
  size_t hash_value;
1223.4.1 by Brian Aker
Table Identifier patch.
120
1415 by Brian Aker
Mass overhaul to use schema_identifier.
121
  void init();
1395.1.6 by Brian Aker
Modified TableIdentifier output for errors.
122
1618 by Brian Aker
This is a rollup set of patches for modifications to TableIdentifier to have
123
  size_t getKeySize() const
124
  {
125
    return getSchemaName().size() + getTableName().size() + 2;
126
  }
127
1223.4.1 by Brian Aker
Table Identifier patch.
128
public:
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
129
130
  Table(const drizzled::Table &table);
1417 by Brian Aker
Interface for Stewart.
131
                   
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
132
  Table(const identifier::Schema &schema,
133
        const std::string &table_name_arg,
134
        Type tmp_arg= message::Table::STANDARD) :
2087.4.1 by Brian Aker
Merge in schema identifier.
135
    Schema(schema),
1415 by Brian Aker
Mass overhaul to use schema_identifier.
136
    type(tmp_arg),
137
    table_name(table_name_arg)
138
  { 
139
    init();
140
  }
141
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
142
  Table( const std::string &db_arg,
1358.1.5 by Brian Aker
Cleans up use of static buffer in TableIdentifier.
143
                   const std::string &table_name_arg,
1395.1.9 by Brian Aker
Small cleanup around how we do types internally.
144
                   Type tmp_arg= message::Table::STANDARD) :
2087.4.1 by Brian Aker
Merge in schema identifier.
145
    Schema(db_arg),
1223.4.1 by Brian Aker
Table Identifier patch.
146
    type(tmp_arg),
1395.1.14 by Brian Aker
Fix for Innodb dfe files.
147
    table_name(table_name_arg)
1320.1.5 by Brian Aker
Second pass through remove proto write outside of SE.
148
  { 
1415 by Brian Aker
Mass overhaul to use schema_identifier.
149
    init();
1320.1.5 by Brian Aker
Second pass through remove proto write outside of SE.
150
  }
1223.4.1 by Brian Aker
Table Identifier patch.
151
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
152
  Table( const std::string &schema_name_arg,
1395.1.14 by Brian Aker
Fix for Innodb dfe files.
153
                   const std::string &table_name_arg,
154
                   const std::string &path_arg ) :
2087.4.1 by Brian Aker
Merge in schema identifier.
155
    Schema(schema_name_arg),
1395.1.7 by Brian Aker
Removed the internal table type in favor of the protobuf one.
156
    type(message::Table::TEMPORARY),
1389 by Brian Aker
Large reord in ALTER TABLE for RENAME.
157
    path(path_arg),
158
    table_name(table_name_arg)
159
  { 
1415 by Brian Aker
Mass overhaul to use schema_identifier.
160
    init();
1358.1.2 by Brian Aker
Long pass through the system to use more of TableIdentifiers.
161
  }
162
2087.4.1 by Brian Aker
Merge in schema identifier.
163
  using Schema::compare;
1415 by Brian Aker
Mass overhaul to use schema_identifier.
164
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
165
  bool isTmp() const
1223.4.1 by Brian Aker
Table Identifier patch.
166
  {
1395.1.7 by Brian Aker
Removed the internal table type in favor of the protobuf one.
167
    if (type == message::Table::TEMPORARY || type == message::Table::INTERNAL)
168
      return true;
2134.1.7 by Brian Aker
Collapse strings used for table.
169
1395.1.7 by Brian Aker
Removed the internal table type in favor of the protobuf one.
170
    return false;
1223.4.1 by Brian Aker
Table Identifier patch.
171
  }
172
1802.12.1 by Brian Aker
This solves bug lp:654905
173
  static bool isView(message::Table::TableType arg) // Not a SQL view, but a view for I_S
174
  {
175
    switch (arg)
176
    {
177
    default:
178
    case message::Table::STANDARD:
179
    case message::Table::TEMPORARY:
180
    case message::Table::INTERNAL:
181
      break;
182
    case message::Table::FUNCTION:
183
      return true;
184
    }
185
186
    return false;
187
  }
188
1643.3.11 by Brian Aker
Encapsulate a bit of logic.
189
  bool isView() const // Not a SQL view, but a view for I_S
190
  {
1802.12.1 by Brian Aker
This solves bug lp:654905
191
    return isView(type);
1643.3.11 by Brian Aker
Encapsulate a bit of logic.
192
  }
193
1395.1.9 by Brian Aker
Small cleanup around how we do types internally.
194
  Type getType() const
195
  {
196
    return type;
197
  }
198
2246.4.2 by Olaf van der Spek
Refactor Identifier::getSQLPath()
199
  virtual std::string getSQLPath() const;
1320.1.5 by Brian Aker
Second pass through remove proto write outside of SE.
200
1992.4.1 by Brian Aker
Update code for testing identifiers/table/schema name correctness.
201
  virtual const std::string &getPath() const;
2139.3.2 by Brian Aker
Fix innodb to just directly use the identifier (this way we only need to
202
  const std::string &getKeyPath() const;
1358.1.9 by Brian Aker
Update for std::string
203
1395.1.14 by Brian Aker
Fix for Innodb dfe files.
204
  void setPath(const std::string &new_path)
205
  {
206
    path= new_path;
207
  }
208
1411.3.3 by Monty Taylor
Made getTableName() return a const reference instead of a temporary const
209
  const std::string &getTableName() const
1358.1.9 by Brian Aker
Update for std::string
210
  {
211
    return table_name;
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
212
  }
213
1618.1.1 by Brian Aker
Modify TableIdentifier to be const
214
  void copyToTableMessage(message::Table &message) const;
1395.1.1 by Brian Aker
Fixes for alter table to make sure that the proto on disk is valid.
215
2246.4.10 by Olaf van der Spek
Remove const_reference and reference from identifier::Table
216
  friend bool operator<(const Table& left, const Table& right)
1429.1.3 by Brian Aker
Merge in work for fetching a list of table identifiers.
217
  {
1669.2.1 by Brian Aker
Modify how we make comparisons for tables.
218
    if (left.getKey() < right.getKey())
1429.1.3 by Brian Aker
Merge in work for fetching a list of table identifiers.
219
    {
220
      return true;
221
    }
222
223
    return false;
224
  }
225
2246.4.10 by Olaf van der Spek
Remove const_reference and reference from identifier::Table
226
  friend bool operator==(const Table& left, const Table& right)
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
227
  {
1843.8.2 by Brian Aker
Slight optimization on comparison.
228
    if (left.getHashValue() == right.getHashValue())
229
    {
230
      if (left.getKey() == right.getKey())
231
        return true;
232
    }
1223.4.1 by Brian Aker
Table Identifier patch.
233
1223.4.5 by Brian Aker
Added << and == operators to TableIdentifier
234
    return false;
235
  }
236
1601 by Brian Aker
Move functions to class methods.
237
  static uint32_t filename_to_tablename(const char *from, char *to, uint32_t to_length);
1864.3.9 by Brian Aker
Remove a couple of memset and now just build the table name in the string of
238
  static size_t build_table_filename(std::string &path, const std::string &db, const std::string &table_name, bool is_tmp);
1618 by Brian Aker
This is a rollup set of patches for modifications to TableIdentifier to have
239
  static size_t build_tmptable_filename(std::string &buffer);
240
  static size_t build_tmptable_filename(std::vector<char> &buffer);
241
1864.3.17 by Brian Aker
Simply the create of an identifier key.
242
public:
1992.4.1 by Brian Aker
Update code for testing identifiers/table/schema name correctness.
243
  bool isValid() const;
1601 by Brian Aker
Move functions to class methods.
244
1608.2.1 by Brian Aker
Modified to table identifier to fix temporary table creation loss of file.
245
  size_t getHashValue() const
246
  {
247
    return hash_value;
248
  }
1618 by Brian Aker
This is a rollup set of patches for modifications to TableIdentifier to have
249
250
  const Key &getKey() const
251
  {
252
    return key;
253
  }
1223.4.1 by Brian Aker
Table Identifier patch.
254
};
255
2246.4.10 by Olaf van der Spek
Remove const_reference and reference from identifier::Table
256
std::ostream& operator<<(std::ostream& output, const Table& identifier);
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
257
std::size_t hash_value(Table const& b);
258
std::size_t hash_value(Table::Key const& b);
1608.2.1 by Brian Aker
Modified to table identifier to fix temporary table creation loss of file.
259
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
260
} /* namespace identifier */
1223.4.1 by Brian Aker
Table Identifier patch.
261
} /* namespace drizzled */