~posulliv/drizzle/optimizer-style-cleanup

« back to all changes in this revision

Viewing changes to plugin/table_cache_dictionary/table_cache.cc

  • Committer: Padraig O'Sullivan
  • Date: 2010-04-17 01:38:47 UTC
  • mfrom: (1237.9.238 bad-staging)
  • Revision ID: osullivan.padraig@gmail.com-20100417013847-ibjioqsfbmf5yg4g
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
21
#include "config.h"
 
22
 
 
23
#include "plugin/table_cache_dictionary/dictionary.h"
 
24
#include "drizzled/pthread_globals.h"
 
25
#include "drizzled/my_hash.h"
 
26
 
 
27
using namespace drizzled;
 
28
using namespace std;
 
29
 
 
30
table_cache_dictionary::TableCache::TableCache() :
 
31
  plugin::TableFunction("DATA_DICTIONARY", "TABLE_CACHE")
 
32
{
 
33
  add_field("SESSION_ID", plugin::TableFunction::NUMBER);
 
34
  add_field("TABLE_SCHEMA");
 
35
  add_field("TABLE_NAME");
 
36
  add_field("VERSION", plugin::TableFunction::NUMBER);
 
37
  add_field("IS_NAME_LOCKED", plugin::TableFunction::BOOLEAN);
 
38
  add_field("ROWS", plugin::TableFunction::NUMBER);
 
39
  add_field("AVG_ROW_LENGTH", plugin::TableFunction::NUMBER);
 
40
  add_field("TABLE_SIZE", plugin::TableFunction::NUMBER);
 
41
  add_field("AUTO_INCREMENT", plugin::TableFunction::NUMBER);
 
42
}
 
43
 
 
44
table_cache_dictionary::TableCache::Generator::Generator(drizzled::Field **arg) :
 
45
  drizzled::plugin::TableFunction::Generator(arg),
 
46
  is_primed(false)
 
47
{
 
48
  pthread_mutex_lock(&LOCK_open); /* Optionally lock for remove tables from open_cahe if not in use */
 
49
 
 
50
  drizzled::HASH *open_cache=
 
51
    get_open_cache();
 
52
 
 
53
  for (uint32_t idx= 0; idx < open_cache->records; idx++ )
 
54
  {
 
55
    table= (Table*) hash_element(open_cache, idx);
 
56
    table_list.push_back(table);
 
57
  }
 
58
  std::sort(table_list.begin(), table_list.end(), Table::compare);
 
59
}
 
60
 
 
61
table_cache_dictionary::TableCache::Generator::~Generator()
 
62
{
 
63
  pthread_mutex_unlock(&LOCK_open); /* Optionally lock for remove tables from open_cahe if not in use */
 
64
}
 
65
 
 
66
bool table_cache_dictionary::TableCache::Generator::nextCore()
 
67
{
 
68
  if (is_primed)
 
69
  {
 
70
    table_list_iterator++;
 
71
  }
 
72
  else
 
73
  {
 
74
    is_primed= true;
 
75
    table_list_iterator= table_list.begin();
 
76
  }
 
77
 
 
78
  if (table_list_iterator == table_list.end())
 
79
    return false;
 
80
 
 
81
  table= *table_list_iterator;
 
82
 
 
83
  return true;
 
84
}
 
85
 
 
86
bool table_cache_dictionary::TableCache::Generator::next()
 
87
{
 
88
  while (not nextCore())
 
89
  {
 
90
    if (table_list_iterator != table_list.end())
 
91
      continue;
 
92
 
 
93
    return false;
 
94
  }
 
95
 
 
96
  return true;
 
97
}
 
98
 
 
99
bool table_cache_dictionary::TableCache::Generator::populate()
 
100
{
 
101
  if (not next())
 
102
    return false;
 
103
  
 
104
  fill();
 
105
 
 
106
  return true;
 
107
}
 
108
 
 
109
void table_cache_dictionary::TableCache::Generator::fill()
 
110
{
 
111
  /**
 
112
    For test cases use:
 
113
    --replace_column 1 # 4 # 5 #  6 # 8 # 9 #
 
114
  */
 
115
 
 
116
  /* SESSION_ID 1 */
 
117
  if (table->getSession())
 
118
    push(table->getSession()->getSessionId());
 
119
  else
 
120
    push(static_cast<int64_t>(0));
 
121
 
 
122
  /* TABLE_SCHEMA 2 */
 
123
  string arg;
 
124
  push(table->getShare()->getSchemaName(arg));
 
125
 
 
126
  /* TABLE_NAME  3 */
 
127
  push(table->getShare()->getTableName(arg));
 
128
 
 
129
  /* VERSION 4 */
 
130
  push(static_cast<int64_t>(table->getShare()->version));
 
131
 
 
132
  /* IS_NAME_LOCKED 5 */
 
133
  push(table->getShare()->isNameLock());
 
134
 
 
135
  /* ROWS 6 */
 
136
  push(static_cast<uint64_t>(table->getCursor().records()));
 
137
 
 
138
  /* AVG_ROW_LENGTH 7 */
 
139
  push(table->getCursor().rowSize());
 
140
 
 
141
  /* TABLE_SIZE 8 */
 
142
  push(table->getCursor().tableSize());
 
143
 
 
144
  /* AUTO_INCREMENT 9 */
 
145
  push(table->getCursor().getNextInsertId());
 
146
}