Drizzled Public API Documentation

table.h
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2010 Brian Aker
00005  *  Copyright (C) 2009 Sun Microsystems, Inc.
00006  *
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; either version 2 of the License, or
00010  *  (at your option) any later version.
00011  *
00012  *  This program is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *  GNU General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU General Public License
00018  *  along with this program; if not, write to the Free Software
00019  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020  */
00021 
00022 /* 
00023   This is a "work in progress". The concept needs to be replicated throughout
00024   the code, but we will start with baby steps for the moment. To not incur
00025   cost until we are complete, for the moment it will do no allocation.
00026 
00027   This is mainly here so that it can be used in the SE interface for
00028   the time being.
00029 
00030   This will replace Table_ident.
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 #include <drizzled/common_fwd.h>
00052 #include <drizzled/identifier/schema.h>
00053 
00054 namespace drizzled {
00055 namespace identifier {
00056 
00057 class DRIZZLED_API Table : public Schema
00058 {
00059 public:
00060   typedef message::Table::TableType Type;
00061 
00062   class Key
00063   {
00064     std::vector<char> key_buffer;
00065     size_t hash_value;
00066 
00067   public:
00068 
00069     Key() :
00070       hash_value(0)
00071     {
00072     }
00073 
00074     const char *vector() const
00075     {
00076       return &key_buffer[0];
00077     }
00078 
00079     std::vector<char> &vectorPtr()
00080     {
00081       return key_buffer;
00082     }
00083 
00084     void set(size_t resize_arg, const std::string &a, const std::string &b, const std::string &c);
00085 
00086     friend bool operator==(const Key &left, const Key &right)
00087     {
00088       if (left.hash_value == right.hash_value and left.key_buffer.size() == right.key_buffer.size())
00089       {
00090         if (memcmp(&left.key_buffer[0], &right.key_buffer[0], left.key_buffer.size()) == 0)
00091           return true;
00092       }
00093 
00094       return false;
00095     }
00096 
00097     friend bool operator<(const Key &left, const Key &right)
00098     {
00099       return left.key_buffer < right.key_buffer;
00100     }
00101 
00102     size_t size() const
00103     {
00104       return key_buffer.size();
00105     }
00106 
00107     size_t getHashValue() const
00108     {
00109       return hash_value;
00110     }
00111   };
00112 
00113 private:
00114 
00115   Type type;
00116   std::string path;
00117   std::string key_path;
00118   std::string table_name;
00119   Key key;
00120   size_t hash_value;
00121 
00122   void init();
00123 
00124   size_t getKeySize() const
00125   {
00126     return getCatalogName().size() + getSchemaName().size() + getTableName().size() + 3;
00127   }
00128 
00129 public:
00130 
00131   Table(const drizzled::Table &table);
00132                    
00133   Table(const identifier::Schema &schema,
00134         const std::string &table_name_arg,
00135         Type tmp_arg= message::Table::STANDARD) :
00136     Schema(schema),
00137     type(tmp_arg),
00138     table_name(table_name_arg)
00139   { 
00140     init();
00141   }
00142 
00143   Table( const std::string &db_arg,
00144                    const std::string &table_name_arg,
00145                    Type tmp_arg= message::Table::STANDARD) :
00146     Schema(db_arg),
00147     type(tmp_arg),
00148     table_name(table_name_arg)
00149   { 
00150     init();
00151   }
00152 
00153   Table( const std::string &schema_name_arg,
00154                    const std::string &table_name_arg,
00155                    const std::string &path_arg ) :
00156     Schema(schema_name_arg),
00157     type(message::Table::TEMPORARY),
00158     path(path_arg),
00159     table_name(table_name_arg)
00160   { 
00161     init();
00162   }
00163 
00164   using Schema::compare;
00165 
00166   bool isTmp() const
00167   {
00168     if (type == message::Table::TEMPORARY || type == message::Table::INTERNAL)
00169       return true;
00170 
00171     return false;
00172   }
00173 
00174   static bool isView(message::Table::TableType arg) // Not a SQL view, but a view for I_S
00175   {
00176     switch (arg)
00177     {
00178     default:
00179     case message::Table::STANDARD:
00180     case message::Table::TEMPORARY:
00181     case message::Table::INTERNAL:
00182       break;
00183     case message::Table::FUNCTION:
00184       return true;
00185     }
00186 
00187     return false;
00188   }
00189 
00190   bool isView() const // Not a SQL view, but a view for I_S
00191   {
00192     return isView(type);
00193   }
00194 
00195   Type getType() const
00196   {
00197     return type;
00198   }
00199 
00200   virtual std::string getSQLPath() const;
00201 
00202   virtual const std::string &getPath() const;
00203   const std::string &getKeyPath() const;
00204 
00205   void setPath(const std::string &new_path)
00206   {
00207     path= new_path;
00208   }
00209 
00210   const std::string &getTableName() const
00211   {
00212     return table_name;
00213   }
00214 
00215   void copyToTableMessage(message::Table &message) const;
00216 
00217   friend bool operator<(const Table& left, const Table& right)
00218   {
00219     if (left.getKey() < right.getKey())
00220     {
00221       return true;
00222     }
00223 
00224     return false;
00225   }
00226 
00227   friend bool operator==(const Table& left, const Table& right)
00228   {
00229     if (left.getHashValue() == right.getHashValue())
00230     {
00231       if (left.getKey() == right.getKey())
00232         return true;
00233     }
00234 
00235     return false;
00236   }
00237 
00238   static uint32_t filename_to_tablename(const char *from, char *to, uint32_t to_length);
00239   static std::string build_table_filename(const std::string &db, const std::string &table_name, bool is_tmp);
00240   static std::string build_tmptable_filename();
00241 
00242 public:
00243   bool isValid() const;
00244 
00245   size_t getHashValue() const
00246   {
00247     return hash_value;
00248   }
00249 
00250   const Key &getKey() const
00251   {
00252     return key;
00253   }
00254 };
00255 
00256 std::ostream& operator<<(std::ostream& output, const Table& identifier);
00257 std::size_t hash_value(Table const& b);
00258 std::size_t hash_value(Table::Key const& b);
00259 
00260 } /* namespace identifier */
00261 } /* namespace drizzled */