Drizzled Public API Documentation

table_cache.cc
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  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 #include <config.h>
00022 
00023 #include <plugin/table_cache_dictionary/dictionary.h>
00024 #include <drizzled/table.h>
00025 #include <drizzled/table/cache.h>
00026 #include <drizzled/pthread_globals.h>
00027 
00028 using namespace drizzled;
00029 using namespace std;
00030 
00031 table_cache_dictionary::TableCache::TableCache() :
00032   plugin::TableFunction("DATA_DICTIONARY", "TABLE_CACHE")
00033 {
00034   add_field("SESSION_ID", plugin::TableFunction::NUMBER, 0, false);
00035   add_field("TABLE_SCHEMA", plugin::TableFunction::STRING, MAXIMUM_IDENTIFIER_LENGTH, false);
00036   add_field("TABLE_NAME", plugin::TableFunction::STRING, MAXIMUM_IDENTIFIER_LENGTH, false);
00037   add_field("VERSION", plugin::TableFunction::NUMBER, 0, false);
00038   add_field("IS_NAME_LOCKED", plugin::TableFunction::BOOLEAN, 0, false);
00039   add_field("ROWS", plugin::TableFunction::NUMBER, 0, false);
00040   add_field("AVG_ROW_LENGTH", plugin::TableFunction::NUMBER, 0, false);
00041   add_field("TABLE_SIZE", plugin::TableFunction::NUMBER, 0, false);
00042   add_field("AUTO_INCREMENT", plugin::TableFunction::NUMBER, 0, false);
00043 }
00044 
00045 table_cache_dictionary::TableCache::Generator::Generator(drizzled::Field **arg) :
00046   drizzled::plugin::TableFunction::Generator(arg),
00047   is_primed(false),
00048   scopedLock(table::Cache::mutex())
00049 {
00050 
00051   for (table::CacheMap::const_iterator iter= table::getCache().begin();
00052        iter != table::getCache().end();
00053        iter++)
00054    {
00055     table_list.push_back(iter->second);
00056   }
00057   std::sort(table_list.begin(), table_list.end(), Table::compare);
00058 }
00059 
00060 bool table_cache_dictionary::TableCache::Generator::nextCore()
00061 {
00062   if (is_primed)
00063   {
00064     table_list_iterator++;
00065   }
00066   else
00067   {
00068     is_primed= true;
00069     table_list_iterator= table_list.begin();
00070   }
00071 
00072   if (table_list_iterator == table_list.end())
00073     return false;
00074 
00075   table= *table_list_iterator;
00076 
00077   return true;
00078 }
00079 
00080 bool table_cache_dictionary::TableCache::Generator::next()
00081 {
00082   while (not nextCore())
00083   {
00084     if (table_list_iterator != table_list.end())
00085       continue;
00086 
00087     return false;
00088   }
00089 
00090   return true;
00091 }
00092 
00093 bool table_cache_dictionary::TableCache::Generator::populate()
00094 {
00095   if (not next())
00096     return false;
00097   
00098   fill();
00099 
00100   return true;
00101 }
00102 
00103 void table_cache_dictionary::TableCache::Generator::fill()
00104 {
00110   /* SESSION_ID 1 */
00111   if (table->getSession())
00112     push(table->getSession()->getSessionId());
00113   else
00114     push(static_cast<int64_t>(0));
00115 
00116   /* TABLE_SCHEMA 2 */
00117   push(table->getShare()->getSchemaNameRef());
00118 
00119   /* TABLE_NAME  3 */
00120   push(table->getShare()->getTableNameRef());
00121 
00122   /* VERSION 4 */
00123   push(static_cast<int64_t>(table->getShare()->getVersion()));
00124 
00125   /* IS_NAME_LOCKED 5 */
00126   push(table->isNameLock());
00127 
00128   /* ROWS 6 */
00129   push(static_cast<uint64_t>(table->getCursor().records()));
00130 
00131   /* AVG_ROW_LENGTH 7 */
00132   push(table->getCursor().rowSize());
00133 
00134   /* TABLE_SIZE 8 */
00135   push(table->getCursor().tableSize());
00136 
00137   /* AUTO_INCREMENT 9 */
00138   push(table->getCursor().getNextInsertId());
00139 }