~drizzle-trunk/drizzle/development

1340.1.2 by Brian Aker
Merge of table cache/def DD.
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2010 Brian Aker
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, write to the Free Software
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 */
20
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
21
#include <config.h>
1340.1.2 by Brian Aker
Merge of table cache/def DD.
22
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
23
#include <plugin/table_cache_dictionary/dictionary.h>
2234.1.1 by Olaf van der Spek
Refactor includes
24
#include <drizzled/table.h>
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
25
#include <drizzled/pthread_globals.h>
1340.1.2 by Brian Aker
Merge of table cache/def DD.
26
27
using namespace drizzled;
28
using namespace std;
29
30
table_cache_dictionary::TableCache::TableCache() :
31
  plugin::TableFunction("DATA_DICTIONARY", "TABLE_CACHE")
32
{
1643.3.10 by Brian Aker
Column support, clean up of IS/DD for NULL type.
33
  add_field("SESSION_ID", plugin::TableFunction::NUMBER, 0, false);
1340.1.2 by Brian Aker
Merge of table cache/def DD.
34
  add_field("TABLE_SCHEMA");
35
  add_field("TABLE_NAME");
1643.3.10 by Brian Aker
Column support, clean up of IS/DD for NULL type.
36
  add_field("VERSION", plugin::TableFunction::NUMBER, 0, false);
37
  add_field("IS_NAME_LOCKED", plugin::TableFunction::BOOLEAN, 0, false);
38
  add_field("ROWS", plugin::TableFunction::NUMBER, 0, false);
39
  add_field("AVG_ROW_LENGTH", plugin::TableFunction::NUMBER, 0, false);
40
  add_field("TABLE_SIZE", plugin::TableFunction::NUMBER, 0, false);
41
  add_field("AUTO_INCREMENT", plugin::TableFunction::NUMBER, 0, false);
1340.1.2 by Brian Aker
Merge of table cache/def DD.
42
}
43
44
table_cache_dictionary::TableCache::Generator::Generator(drizzled::Field **arg) :
45
  drizzled::plugin::TableFunction::Generator(arg),
1938.4.11 by Brian Aker
Scoped locks a bit better.
46
  is_primed(false),
47
  scopedLock(table::Cache::singleton().mutex())
1340.1.2 by Brian Aker
Merge of table cache/def DD.
48
{
49
1877.2.6 by Brian Aker
Fixed header/etc.
50
  for (table::CacheMap::const_iterator iter= table::getCache().begin();
1877.2.5 by Brian Aker
Shift a bit more of the guts of cache to its own space.
51
       iter != table::getCache().end();
1669 by Brian Aker
This patch turns the table_cache into boost::unordered_multimap.
52
       iter++)
53
   {
2192.4.7 by Olaf van der Spek
Use "iter->" instead of "(*iter)."
54
    table_list.push_back(iter->second);
1340.1.2 by Brian Aker
Merge of table cache/def DD.
55
  }
56
  std::sort(table_list.begin(), table_list.end(), Table::compare);
57
}
58
59
table_cache_dictionary::TableCache::Generator::~Generator()
60
{
61
}
62
63
bool table_cache_dictionary::TableCache::Generator::nextCore()
64
{
65
  if (is_primed)
66
  {
67
    table_list_iterator++;
68
  }
69
  else
70
  {
71
    is_primed= true;
72
    table_list_iterator= table_list.begin();
73
  }
74
75
  if (table_list_iterator == table_list.end())
76
    return false;
77
78
  table= *table_list_iterator;
79
80
  return true;
81
}
82
83
bool table_cache_dictionary::TableCache::Generator::next()
84
{
85
  while (not nextCore())
86
  {
87
    if (table_list_iterator != table_list.end())
88
      continue;
89
90
    return false;
91
  }
92
93
  return true;
94
}
95
96
bool table_cache_dictionary::TableCache::Generator::populate()
97
{
98
  if (not next())
99
    return false;
100
  
101
  fill();
102
103
  return true;
104
}
105
106
void table_cache_dictionary::TableCache::Generator::fill()
107
{
108
  /**
109
    For test cases use:
1347 by Brian Aker
Update/fix for leak.
110
    --replace_column 1 # 4 # 5 #  6 # 8 # 9 #
1340.1.2 by Brian Aker
Merge of table cache/def DD.
111
  */
112
113
  /* SESSION_ID 1 */
114
  if (table->getSession())
115
    push(table->getSession()->getSessionId());
116
  else
117
    push(static_cast<int64_t>(0));
118
119
  /* TABLE_SCHEMA 2 */
1346 by Brian Aker
Solve non-null termination issue.
120
  string arg;
121
  push(table->getShare()->getSchemaName(arg));
1340.1.2 by Brian Aker
Merge of table cache/def DD.
122
123
  /* TABLE_NAME  3 */
1347 by Brian Aker
Update/fix for leak.
124
  push(table->getShare()->getTableName(arg));
125
126
  /* VERSION 4 */
1578.2.3 by Brian Aker
Take version and encapsulate it in TableShare
127
  push(static_cast<int64_t>(table->getShare()->getVersion()));
1340.1.2 by Brian Aker
Merge of table cache/def DD.
128
1347 by Brian Aker
Update/fix for leak.
129
  /* IS_NAME_LOCKED 5 */
1910.2.9 by Brian Aker
Remove a bunch of dead code from within table share.
130
  push(table->isNameLock());
1340.1.2 by Brian Aker
Merge of table cache/def DD.
131
1347 by Brian Aker
Update/fix for leak.
132
  /* ROWS 6 */
1340.1.2 by Brian Aker
Merge of table cache/def DD.
133
  push(static_cast<uint64_t>(table->getCursor().records()));
134
1347 by Brian Aker
Update/fix for leak.
135
  /* AVG_ROW_LENGTH 7 */
1340.1.2 by Brian Aker
Merge of table cache/def DD.
136
  push(table->getCursor().rowSize());
137
1347 by Brian Aker
Update/fix for leak.
138
  /* TABLE_SIZE 8 */
1340.1.2 by Brian Aker
Merge of table cache/def DD.
139
  push(table->getCursor().tableSize());
140
1347 by Brian Aker
Update/fix for leak.
141
  /* AUTO_INCREMENT 9 */
1340.1.2 by Brian Aker
Merge of table cache/def DD.
142
  push(table->getCursor().getNextInsertId());
143
}