~drizzle-trunk/drizzle/development

1309.2.3 by Brian Aker
Update the code so use a faster index lookup method.
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"
1429.1.1 by Brian Aker
Shift show commands to their own dictionary.
22
#include "plugin/show_dictionary/dictionary.h"
1660.1.1 by Brian Aker
Merge in move identifier work.
23
#include "drizzled/identifier.h"
1309.2.3 by Brian Aker
Update the code so use a faster index lookup method.
24
25
26
using namespace std;
27
using namespace drizzled;
28
29
ShowIndexes::ShowIndexes() :
1874.2.4 by Brian Aker
Have show functions be invisible.
30
  show_dictionary::Show("SHOW_INDEXES")
1309.2.3 by Brian Aker
Update the code so use a faster index lookup method.
31
{
32
  add_field("Table");
1643.3.10 by Brian Aker
Column support, clean up of IS/DD for NULL type.
33
  add_field("Unique", plugin::TableFunction::BOOLEAN, 0, false);
1309.2.3 by Brian Aker
Update the code so use a faster index lookup method.
34
  add_field("Key_name");
1643.3.10 by Brian Aker
Column support, clean up of IS/DD for NULL type.
35
  add_field("Seq_in_index", plugin::TableFunction::NUMBER, 0, false);
1309.2.3 by Brian Aker
Update the code so use a faster index lookup method.
36
  add_field("Column_name");
37
}
38
39
ShowIndexes::Generator::Generator(Field **arg) :
1874.2.4 by Brian Aker
Have show functions be invisible.
40
  show_dictionary::Show::Generator(arg),
1309.2.3 by Brian Aker
Update the code so use a faster index lookup method.
41
  is_tables_primed(false),
42
  is_index_primed(false),
43
  is_index_part_primed(false),
44
  index_iterator(0),
45
  index_part_iterator(0)
46
{
2059.1.1 by Andrew Hutchings
Refix show_dictionary plugin crash when not using 'SHOW' to access.
47
  if (not isShowQuery())
48
    return;
49
1874.2.2 by Brian Aker
What was once old, is now new again. Seperated out show from select.
50
  statement::Show *select= static_cast<statement::Show *>(getSession().lex->statement);
1309.2.3 by Brian Aker
Update the code so use a faster index lookup method.
51
1448 by Brian Aker
Fix for bug on show tables.:w
52
  if (not select->getShowTable().empty() && not select->getShowSchema().empty())
53
  {
54
    table_name.append(select->getShowTable().c_str());
55
    TableIdentifier identifier(select->getShowSchema().c_str(), select->getShowTable().c_str());
1309.2.3 by Brian Aker
Update the code so use a faster index lookup method.
56
1448 by Brian Aker
Fix for bug on show tables.:w
57
    is_tables_primed= plugin::StorageEngine::getTableDefinition(getSession(),
58
                                                                identifier,
59
                                                                table_proto);
60
  }
1309.2.3 by Brian Aker
Update the code so use a faster index lookup method.
61
}
62
63
bool ShowIndexes::Generator::nextIndexCore()
64
{
65
  if (isIndexesPrimed())
66
  {
67
    index_iterator++;
68
  }
69
  else
70
  {
71
    if (not isTablesPrimed())
72
      return false;
73
74
    index_iterator= 0;
75
    is_index_primed= true;
76
  }
77
78
  if (index_iterator >= getTableProto().indexes_size())
79
    return false;
80
81
  index= getTableProto().indexes(index_iterator);
82
83
  return true;
84
}
85
86
bool ShowIndexes::Generator::nextIndex()
87
{
88
  while (not nextIndexCore())
89
  {
90
    return false;
91
  }
92
93
  return true;
94
}
95
96
bool ShowIndexes::Generator::nextIndexPartsCore()
97
{
98
  if (is_index_part_primed)
99
  {
100
    index_part_iterator++;
101
  }
102
  else
103
  {
104
    if (not isIndexesPrimed())
105
      return false;
106
107
    index_part_iterator= 0;
108
    is_index_part_primed= true;
109
  }
110
111
  if (index_part_iterator >= getIndex().index_part_size())
112
    return false;
113
114
  index_part= getIndex().index_part(index_part_iterator);
115
116
  return true;
117
}
118
119
120
bool ShowIndexes::Generator::nextIndexParts()
121
{
122
  while (not nextIndexPartsCore())
123
  {
124
    if (not nextIndex())
125
      return false;
126
    is_index_part_primed= false;
127
  }
128
129
  return true;
130
}
131
132
133
134
bool ShowIndexes::Generator::populate()
135
{
136
  if (not nextIndexParts())
137
    return false;
138
139
  fill();
140
141
  return true;
142
}
143
144
void ShowIndexes::Generator::fill()
145
{
146
  /* Table */
147
  push(getTableName());
148
149
  /* Unique */
150
  push(getIndex().is_unique());
151
152
  /* Key_name */
153
  push(getIndex().name());
154
155
  /* Seq_in_index */
156
  push(static_cast<int64_t>(index_part_iterator + 1));
157
158
  /* Column_name */
159
  push(getTableProto().field(getIndexPart().fieldnr()).name());
160
}