00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #pragma once
00034
00035 #include <drizzled/enum.h>
00036 #include <drizzled/definitions.h>
00037 #include <drizzled/message/table.pb.h>
00038
00039 #include <string.h>
00040
00041 #include <assert.h>
00042
00043 #include <ostream>
00044 #include <set>
00045 #include <algorithm>
00046 #include <functional>
00047
00048 #include <boost/functional/hash.hpp>
00049
00050 #include <drizzled/visibility.h>
00051
00052 namespace drizzled {
00053 class Table;
00054
00055 namespace identifier {
00056
00057 class DRIZZLED_API Table : public Schema
00058 {
00059 public:
00060 typedef message::Table::TableType Type;
00061 typedef std::vector <Table> vector;
00062 typedef const Table& const_reference;
00063 typedef Table& reference;
00064
00065 class Key
00066 {
00067 std::vector<char> key_buffer;
00068 size_t hash_value;
00069
00070 public:
00071
00072 Key() :
00073 hash_value(0)
00074 {
00075 }
00076
00077 const char *vector() const
00078 {
00079 return &key_buffer[0];
00080 }
00081
00082 std::vector<char> &vectorPtr()
00083 {
00084 return key_buffer;
00085 }
00086
00087 void set(size_t resize_arg, const std::string &a, const std::string &b);
00088
00089 friend bool operator==(const Key &left, const Key &right)
00090 {
00091 if (left.hash_value == right.hash_value and left.key_buffer.size() == right.key_buffer.size())
00092 {
00093 if (memcmp(&left.key_buffer[0], &right.key_buffer[0], left.key_buffer.size()) == 0)
00094 return true;
00095 }
00096
00097 return false;
00098 }
00099
00100 friend bool operator<(const Key &left, const Key &right)
00101 {
00102 return left.key_buffer < right.key_buffer;
00103 }
00104
00105 size_t size() const
00106 {
00107 return key_buffer.size();
00108 }
00109
00110 size_t getHashValue() const
00111 {
00112 return hash_value;
00113 }
00114 };
00115
00116 private:
00117
00118 Type type;
00119 std::string path;
00120 std::string key_path;
00121 std::string table_name;
00122 Key key;
00123 size_t hash_value;
00124
00125 void init();
00126
00127 size_t getKeySize() const
00128 {
00129 return getSchemaName().size() + getTableName().size() + 2;
00130 }
00131
00132 public:
00133
00134 Table(const drizzled::Table &table);
00135
00136 Table(const identifier::Schema &schema,
00137 const std::string &table_name_arg,
00138 Type tmp_arg= message::Table::STANDARD) :
00139 Schema(schema),
00140 type(tmp_arg),
00141 table_name(table_name_arg)
00142 {
00143 init();
00144 }
00145
00146 Table( const std::string &db_arg,
00147 const std::string &table_name_arg,
00148 Type tmp_arg= message::Table::STANDARD) :
00149 Schema(db_arg),
00150 type(tmp_arg),
00151 table_name(table_name_arg)
00152 {
00153 init();
00154 }
00155
00156 Table( const std::string &schema_name_arg,
00157 const std::string &table_name_arg,
00158 const std::string &path_arg ) :
00159 Schema(schema_name_arg),
00160 type(message::Table::TEMPORARY),
00161 path(path_arg),
00162 table_name(table_name_arg)
00163 {
00164 init();
00165 }
00166
00167 using Schema::compare;
00168
00169 bool isTmp() const
00170 {
00171 if (type == message::Table::TEMPORARY || type == message::Table::INTERNAL)
00172 return true;
00173
00174 return false;
00175 }
00176
00177 static bool isView(message::Table::TableType arg)
00178 {
00179 switch (arg)
00180 {
00181 default:
00182 case message::Table::STANDARD:
00183 case message::Table::TEMPORARY:
00184 case message::Table::INTERNAL:
00185 break;
00186 case message::Table::FUNCTION:
00187 return true;
00188 }
00189
00190 return false;
00191 }
00192
00193 bool isView() const
00194 {
00195 return isView(type);
00196 }
00197
00198 Type getType() const
00199 {
00200 return type;
00201 }
00202
00203 virtual void getSQLPath(std::string &sql_path) const;
00204
00205 virtual const std::string &getPath() const;
00206 const std::string &getKeyPath() const;
00207
00208 void setPath(const std::string &new_path)
00209 {
00210 path= new_path;
00211 }
00212
00213 const std::string &getTableName() const
00214 {
00215 return table_name;
00216 }
00217
00218 void copyToTableMessage(message::Table &message) const;
00219
00220 friend bool operator<(Table::const_reference left, Table::const_reference right)
00221 {
00222 if (left.getKey() < right.getKey())
00223 {
00224 return true;
00225 }
00226
00227 return false;
00228 }
00229
00230 friend bool operator==(Table::const_reference left, Table::const_reference right)
00231 {
00232 if (left.getHashValue() == right.getHashValue())
00233 {
00234 if (left.getKey() == right.getKey())
00235 return true;
00236 }
00237
00238 return false;
00239 }
00240
00241 static uint32_t filename_to_tablename(const char *from, char *to, uint32_t to_length);
00242 static size_t build_table_filename(std::string &path, const std::string &db, const std::string &table_name, bool is_tmp);
00243 static size_t build_tmptable_filename(std::string &buffer);
00244 static size_t build_tmptable_filename(std::vector<char> &buffer);
00245
00246 public:
00247 bool isValid() const;
00248
00249 size_t getHashValue() const
00250 {
00251 return hash_value;
00252 }
00253
00254 const Key &getKey() const
00255 {
00256 return key;
00257 }
00258 };
00259
00260 std::ostream& operator<<(std::ostream& output, Table::const_reference identifier);
00261 std::size_t hash_value(Table const& b);
00262 std::size_t hash_value(Table::Key const& b);
00263
00264 }
00265 }