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 
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) // Not a SQL view, but a view for I_S
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 // Not a SQL view, but a view for I_S
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 } /* namespace identifier */
00265 } /* namespace drizzled */