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/pthread_globals.h>
00026 
00027 using namespace drizzled;
00028 using namespace std;
00029 
00030 table_cache_dictionary::TableCache::TableCache() :
00031   plugin::TableFunction("DATA_DICTIONARY", "TABLE_CACHE")
00032 {
00033   add_field("SESSION_ID", plugin::TableFunction::NUMBER, 0, false);
00034   add_field("TABLE_SCHEMA");
00035   add_field("TABLE_NAME");
00036   add_field("VERSION", plugin::TableFunction::NUMBER, 0, false);
00037   add_field("IS_NAME_LOCKED", plugin::TableFunction::BOOLEAN, 0, false);
00038   add_field("ROWS", plugin::TableFunction::NUMBER, 0, false);
00039   add_field("AVG_ROW_LENGTH", plugin::TableFunction::NUMBER, 0, false);
00040   add_field("TABLE_SIZE", plugin::TableFunction::NUMBER, 0, false);
00041   add_field("AUTO_INCREMENT", plugin::TableFunction::NUMBER, 0, false);
00042 }
00043 
00044 table_cache_dictionary::TableCache::Generator::Generator(drizzled::Field **arg) :
00045   drizzled::plugin::TableFunction::Generator(arg),
00046   is_primed(false),
00047   scopedLock(table::Cache::singleton().mutex())
00048 {
00049 
00050   for (table::CacheMap::const_iterator iter= table::getCache().begin();
00051        iter != table::getCache().end();
00052        iter++)
00053    {
00054     table_list.push_back(iter->second);
00055   }
00056   std::sort(table_list.begin(), table_list.end(), Table::compare);
00057 }
00058 
00059 table_cache_dictionary::TableCache::Generator::~Generator()
00060 {
00061 }
00062 
00063 bool table_cache_dictionary::TableCache::Generator::nextCore()
00064 {
00065   if (is_primed)
00066   {
00067     table_list_iterator++;
00068   }
00069   else
00070   {
00071     is_primed= true;
00072     table_list_iterator= table_list.begin();
00073   }
00074 
00075   if (table_list_iterator == table_list.end())
00076     return false;
00077 
00078   table= *table_list_iterator;
00079 
00080   return true;
00081 }
00082 
00083 bool table_cache_dictionary::TableCache::Generator::next()
00084 {
00085   while (not nextCore())
00086   {
00087     if (table_list_iterator != table_list.end())
00088       continue;
00089 
00090     return false;
00091   }
00092 
00093   return true;
00094 }
00095 
00096 bool table_cache_dictionary::TableCache::Generator::populate()
00097 {
00098   if (not next())
00099     return false;
00100   
00101   fill();
00102 
00103   return true;
00104 }
00105 
00106 void table_cache_dictionary::TableCache::Generator::fill()
00107 {
00113   /* SESSION_ID 1 */
00114   if (table->getSession())
00115     push(table->getSession()->getSessionId());
00116   else
00117     push(static_cast<int64_t>(0));
00118 
00119   /* TABLE_SCHEMA 2 */
00120   string arg;
00121   push(table->getShare()->getSchemaName(arg));
00122 
00123   /* TABLE_NAME  3 */
00124   push(table->getShare()->getTableName(arg));
00125 
00126   /* VERSION 4 */
00127   push(static_cast<int64_t>(table->getShare()->getVersion()));
00128 
00129   /* IS_NAME_LOCKED 5 */
00130   push(table->isNameLock());
00131 
00132   /* ROWS 6 */
00133   push(static_cast<uint64_t>(table->getCursor().records()));
00134 
00135   /* AVG_ROW_LENGTH 7 */
00136   push(table->getCursor().rowSize());
00137 
00138   /* TABLE_SIZE 8 */
00139   push(table->getCursor().tableSize());
00140 
00141   /* AUTO_INCREMENT 9 */
00142   push(table->getCursor().getNextInsertId());
00143 }