Drizzled Public API Documentation

unused.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 <sys/types.h>
00024 #include <sys/stat.h>
00025 #include <fcntl.h>
00026 
00027 
00028 #include <drizzled/identifier.h>
00029 #include <drizzled/sql_base.h>
00030 #include <drizzled/set_var.h>
00031 
00032 #include <drizzled/table/unused.h>
00033 
00034 namespace drizzled
00035 {
00036 
00037 extern uint64_t table_cache_size;
00038 
00039 namespace table
00040 {
00041 
00042 UnusedTables &getUnused(void)
00043 {
00044   static UnusedTables unused_tables;
00045 
00046   return unused_tables;
00047 }
00048 
00049 void UnusedTables::cull()
00050 {
00051   /* Free cache if too big */
00052   while (cached_open_tables() > table_cache_size && getTable())
00053     remove_table(getTable());
00054 }
00055 
00056 void UnusedTables::cullByVersion()
00057 {
00058   while (getTable() && not getTable()->getShare()->getVersion())
00059     remove_table(getTable());
00060 }
00061 
00062 void UnusedTables::link(Concurrent *table)
00063 {
00064   if (getTable())
00065   {
00066     table->setNext(getTable());   /* Link in last */
00067     table->setPrev(getTable()->getPrev());
00068     getTable()->setPrev(table);
00069     table->getPrev()->setNext(table);
00070   }
00071   else
00072   {
00073     table->setPrev(setTable(table));
00074     table->setNext(table->getPrev());
00075     assert(table->getNext() == table && table->getPrev() == table);
00076   }
00077 }
00078 
00079 
00080 void UnusedTables::unlink(Concurrent *table)
00081 {
00082   table->unlink();
00083 
00084   /* Unlink the table from "unused_tables" list. */
00085   if (table == getTable())
00086   {  // First unused
00087     setTable(getTable()->getNext()); // Remove from link
00088     if (table == getTable())
00089       setTable(NULL);
00090   }
00091 }
00092 
00093 /* move table first in unused links */
00094 
00095 void UnusedTables::relink(Concurrent *table)
00096 {
00097   if (table != getTable())
00098   {
00099     table->unlink();
00100 
00101     table->setNext(getTable());     /* Link in unused tables */
00102     table->setPrev(getTable()->getPrev());
00103     getTable()->getPrev()->setNext(table);
00104     getTable()->setPrev(table);
00105     setTable(table);
00106   }
00107 }
00108 
00109 void UnusedTables::clear()
00110 {
00111   while (getTable())
00112     remove_table(getTable());
00113 }
00114 
00115 } /* namespace table */
00116 } /* namespace drizzled */