Drizzled Public API Documentation

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>
36 #include <drizzled/definitions.h>
37 #include <drizzled/message/table.pb.h>
38 
39 #include <drizzled/util/backtrace.h>
40 
41 #include <string.h>
42 
43 #include <assert.h>
44 
45 #include <ostream>
46 #include <set>
47 #include <algorithm>
48 #include <functional>
49 
50 #include <boost/functional/hash.hpp>
51 
52 #include <drizzled/visibility.h>
53 #include <drizzled/common_fwd.h>
54 #include <drizzled/identifier/schema.h>
55 
56 namespace drizzled {
57 namespace identifier {
58 
59 class DRIZZLED_API Table : public Schema
60 {
61 public:
62  typedef message::Table::TableType Type;
63 
64  class Key
65  {
66  std::vector<char> key_buffer;
67  size_t hash_value;
68  size_t schema_offset;
69  size_t table_offset;
70 
71  public:
72 
73  Key() :
74  hash_value(0),
75  schema_offset(0),
76  table_offset(0)
77  {
78  }
79 
80  const char *vector() const
81  {
82  return &key_buffer[0];
83  }
84 
85  const char *schema_name() const
86  {
87  return &key_buffer[0] +schema_offset;
88  }
89 
90  const char *table_name() const
91  {
92  return &key_buffer[0] +table_offset;
93  }
94 
95  size_t hash() const
96  {
97  return hash_value;
98  }
99 
100  std::vector<char> &vectorPtr()
101  {
102  return key_buffer;
103  }
104 
105  void set(size_t resize_arg, const std::string &catalog_arg, const std::string &schema_arg, const std::string &table_arg);
106 
107  friend bool operator==(const Key &left, const Key &right)
108  {
109  if (left.hash_value == right.hash_value and left.key_buffer.size() == right.key_buffer.size())
110  {
111  if (memcmp(&left.key_buffer[0], &right.key_buffer[0], left.key_buffer.size()) == 0)
112  return true;
113  }
114 
115  return false;
116  }
117 
118  friend bool operator<(const Key &left, const Key &right)
119  {
120  return left.key_buffer < right.key_buffer;
121  }
122 
123  size_t size() const
124  {
125  return key_buffer.size();
126  }
127 
128  size_t getHashValue() const
129  {
130  return hash_value;
131  }
132  };
133 
134 private:
135 
136  Type type;
137  std::string path;
138  std::string key_path;
139  std::string table_name;
140  Key key;
141  size_t hash_value;
142 
143  void init();
144 
145  size_t getKeySize() const
146  {
147  return getCatalogName().size() + getSchemaName().size() + getTableName().size() + 3;
148  }
149 
150 public:
151 
152  Table(const drizzled::Table &table);
153 
154  Table(const identifier::Schema &schema,
155  const std::string &table_name_arg,
156  Type tmp_arg= message::Table::STANDARD);
157 
158  Table(const std::string &db_arg,
159  const std::string &table_name_arg,
160  Type tmp_arg= message::Table::STANDARD);
161 
162  Table(const std::string &schema_name_arg,
163  const std::string &table_name_arg,
164  const std::string &path_arg );
165 
166  using Schema::compare;
167 
168  bool isTmp() const
169  {
170  if (type == message::Table::TEMPORARY || type == message::Table::INTERNAL)
171  return true;
172 
173  return false;
174  }
175 
176  static bool isView(message::Table::TableType arg) // Not a SQL view, but a view for I_S
177  {
178  switch (arg)
179  {
180  default:
181  case message::Table::STANDARD:
182  case message::Table::TEMPORARY:
183  case message::Table::INTERNAL:
184  break;
185  case message::Table::FUNCTION:
186  return true;
187  }
188 
189  return false;
190  }
191 
192  bool isView() const // Not a SQL view, but a view for I_S
193  {
194  return isView(type);
195  }
196 
197  Type getType() const
198  {
199  return type;
200  }
201 
202  virtual std::string getSQLPath() const;
203 
204  virtual const std::string &getPath() const;
205  const std::string &getKeyPath() const;
206 
207  void setPath(const std::string &new_path)
208  {
209  path= new_path;
210  }
211 
212  const std::string &getTableName() const
213  {
214  return table_name;
215  }
216 
217  void copyToTableMessage(message::Table &message) const;
218 
219  friend bool operator<(const Table& left, const Table& right)
220  {
221  if (left.getKey() < right.getKey())
222  {
223  return true;
224  }
225 
226  return false;
227  }
228 
229  friend bool operator==(const Table& left, const Table& right)
230  {
231  if (left.getHashValue() == right.getHashValue())
232  {
233  if (left.getKey() == right.getKey())
234  return true;
235  }
236 
237  return false;
238  }
239 
240  static uint32_t filename_to_tablename(const char *from, char *to, uint32_t to_length);
241  static std::string build_table_filename(const std::string &db, const std::string &table_name, bool is_tmp);
242  static std::string build_tmptable_filename();
243 
244 public:
245  bool isValid() const;
246 
247  size_t getHashValue() const
248  {
249  return hash_value;
250  }
251 
252  const Key &getKey() const
253  {
254  return key;
255  }
256 };
257 
258 std::ostream& operator<<(std::ostream& output, const Table&);
259 std::ostream& operator<<(std::ostream& output, const Table::Key&);
260 std::size_t hash_value(Table const& b);
261 std::size_t hash_value(Table::Key const& b);
262 
263 } /* namespace identifier */
264 } /* namespace drizzled */