drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
drizzled/identifier/table.h
1/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2010 Brian Aker
5 * Copyright (C) 2009 Sun Microsystems, Inc.
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
33#pragma once
34
35#include <drizzled/enum.h>
37#include <drizzled/message/table.pb.h>
38
39#include <drizzled/util/backtrace.h>
40
41#include <functional>
42#include <string.h>
43
44#include <assert.h>
45
46#include <ostream>
47#include <set>
48#include <algorithm>
49#include <functional>
50
51#include <boost/functional/hash.hpp>
52
53#include <drizzled/visibility.h>
54#include <drizzled/common_fwd.h>
55#include <drizzled/identifier/schema.h>
56
57namespace drizzled {
58namespace identifier {
59
60class DRIZZLED_API Table : public Schema
61{
62public:
63 typedef message::Table::TableType Type;
64
65 class Key
66 {
67 std::vector<char> key_buffer;
68 size_t hash_value;
69 size_t schema_offset;
70 size_t table_offset;
71
72 public:
73
74 Key() :
75 hash_value(0),
76 schema_offset(0),
77 table_offset(0)
78 {
79 }
80
81 const char *vector() const
82 {
83 return &key_buffer[0];
84 }
85
86 const char *schema_name() const
87 {
88 return &key_buffer[0] +schema_offset;
89 }
90
91 const char *table_name() const
92 {
93 return &key_buffer[0] +table_offset;
94 }
95
96 size_t hash() const
97 {
98 return hash_value;
99 }
100
101 std::vector<char> &vectorPtr()
102 {
103 return key_buffer;
104 }
105
106 void set(size_t resize_arg, const std::string &catalog_arg, const std::string &schema_arg, const std::string &table_arg);
107
108 friend bool operator==(const Key &left, const Key &right)
109 {
110 if (left.hash_value == right.hash_value and left.key_buffer.size() == right.key_buffer.size())
111 {
112 if (memcmp(&left.key_buffer[0], &right.key_buffer[0], left.key_buffer.size()) == 0)
113 return true;
114 }
115
116 return false;
117 }
118
119 friend bool operator<(const Key &left, const Key &right)
120 {
121 return left.key_buffer < right.key_buffer;
122 }
123
124 size_t size() const
125 {
126 return key_buffer.size();
127 }
128
129 size_t getHashValue() const
130 {
131 return hash_value;
132 }
133 };
134
135private:
136
137 Type type;
138 std::string path;
139 std::string key_path;
140 std::string table_name;
141 Key key;
142 size_t hash_value;
143
144 void init();
145
146 size_t getKeySize() const
147 {
148 return getCatalogName().size() + getSchemaName().size() + getTableName().size() + 3;
149 }
150
151public:
152
153 Table(const drizzled::Table &table);
154
155 Table(const identifier::Schema &schema,
156 const std::string &table_name_arg,
157 Type tmp_arg= message::Table::STANDARD);
158
160 const std::string &db_arg,
161 const std::string &table_name_arg,
162 Type tmp_arg= message::Table::STANDARD);
163
165 const std::string &schema_name_arg,
166 const std::string &table_name_arg,
167 const std::string &path_arg );
168
169 using Schema::compare;
170
171 bool isTmp() const
172 {
173 if (type == message::Table::TEMPORARY || type == message::Table::INTERNAL)
174 return true;
175
176 return false;
177 }
178
179 static bool isView(message::Table::TableType arg) // Not a SQL view, but a view for I_S
180 {
181 switch (arg)
182 {
183 default:
184 case message::Table::STANDARD:
185 case message::Table::TEMPORARY:
186 case message::Table::INTERNAL:
187 break;
188 case message::Table::FUNCTION:
189 return true;
190 }
191
192 return false;
193 }
194
195 bool isView() const // Not a SQL view, but a view for I_S
196 {
197 return isView(type);
198 }
199
200 Type getType() const
201 {
202 return type;
203 }
204
205 virtual std::string getSQLPath() const;
206
207 virtual const std::string &getPath() const;
208 const std::string &getKeyPath() const;
209
210 void setPath(const std::string &new_path)
211 {
212 path= new_path;
213 }
214
215 const std::string &getTableName() const
216 {
217 return table_name;
218 }
219
220 void copyToTableMessage(message::Table &message) const;
221
222 friend bool operator<(const Table& left, const Table& right)
223 {
224 if (left.getKey() < right.getKey())
225 {
226 return true;
227 }
228
229 return false;
230 }
231
232 friend bool operator==(const Table& left, const Table& right)
233 {
234 if (left.getHashValue() == right.getHashValue())
235 {
236 if (left.getKey() == right.getKey())
237 return true;
238 }
239
240 return false;
241 }
242
243 static uint32_t filename_to_tablename(const char *from, char *to, uint32_t to_length);
244 static std::string build_table_filename(const drizzled::identifier::Table&, bool is_tmp);
245 static std::string build_tmptable_filename();
246
247public:
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 }
259};
260
261std::ostream& operator<<(std::ostream& output, const Table&);
262std::ostream& operator<<(std::ostream& output, const Table::Key&);
263std::size_t hash_value(Table const& b);
264std::size_t hash_value(Table::Key const& b);
265
266} /* namespace identifier */
267} /* namespace drizzled */
268
269namespace std {
270template<>
271struct hash<drizzled::identifier::Table>
272{
273 size_t operator()(const drizzled::identifier::Table& v) const
274 {
275 return drizzled::identifier::hash_value(v);
276 }
277};
278template<>
279struct hash<drizzled::identifier::Table::Key>
280{
281 size_t operator()(const drizzled::identifier::Table::Key& v) const
282 {
283 return drizzled::identifier::hash_value(v);
284 }
285};
286} /* namespace std */
Convert from decimal to binary representation.
Definition common_fwd.h:215
TODO: Rename this file - func.h is stupid.
STL namespace.
Visibility Control Macros.
#define DRIZZLED_API
Definition visibility.h:38