~drizzle-trunk/drizzle/development

1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2008-2009 Sun Microsystems
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; version 2 of the License.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, write to the Free Software
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 */
19
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
20
#include "config.h"
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
21
#include "drizzled/session.h"
1237.13.15 by Padraig O'Sullivan
Used std::vector instead of List in the QuickRorUnionSelect class.
22
#include "drizzled/util/functors.h"
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
23
#include "drizzled/optimizer/range.h"
24
#include "drizzled/optimizer/quick_range_select.h"
25
#include "drizzled/optimizer/quick_ror_union_select.h"
26
1237.13.15 by Padraig O'Sullivan
Used std::vector instead of List in the QuickRorUnionSelect class.
27
#include <vector>
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
28
#include <algorithm>
29
30
using namespace std;
31
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
32
namespace drizzled
33
{
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
34
1237.13.14 by Padraig O'Sullivan
Corrected some style issues in quick_ror_union_select.[cc,h] and range.cc
35
optimizer::QuickRorUnionSelect::QuickRorUnionSelect(Session *session_param,
36
                                                    Table *table)
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
37
  :
38
    session(session_param),
39
    scans_inited(false)
40
{
41
  index= MAX_KEY;
42
  head= table;
43
  rowid_length= table->cursor->ref_length;
44
  record= head->record[0];
1253.1.6 by Monty Taylor
Moved mem_root functions into drizzled::memory:: namespace.
45
  memory::init_sql_alloc(&alloc, session->variables.range_alloc_block_size, 0);
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
46
  session_param->mem_root= &alloc;
47
}
48
49
/*
50
 * Function object that is used as the comparison function
1237.13.14 by Padraig O'Sullivan
Corrected some style issues in quick_ror_union_select.[cc,h] and range.cc
51
 * for the priority queue in the QuickRorUnionSelect
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
52
 * class.
53
 */
54
class optimizer::compare_functor
55
{
1237.13.14 by Padraig O'Sullivan
Corrected some style issues in quick_ror_union_select.[cc,h] and range.cc
56
  optimizer::QuickRorUnionSelect *self;
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
57
  public:
1237.13.14 by Padraig O'Sullivan
Corrected some style issues in quick_ror_union_select.[cc,h] and range.cc
58
  compare_functor(optimizer::QuickRorUnionSelect *in_arg)
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
59
    : self(in_arg) { }
60
  inline bool operator()(const optimizer::QuickSelectInterface *i, const optimizer::QuickSelectInterface *j) const
61
  {
62
    int val= self->head->cursor->cmp_ref(i->last_rowid,
63
                                         j->last_rowid);
64
    return (val >= 0);
65
  }
66
};
67
68
1237.13.14 by Padraig O'Sullivan
Corrected some style issues in quick_ror_union_select.[cc,h] and range.cc
69
int optimizer::QuickRorUnionSelect::init()
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
70
{
71
  queue=
72
    new priority_queue<optimizer::QuickSelectInterface *,
73
                       vector<optimizer::QuickSelectInterface *>,
74
                       optimizer::compare_functor >(optimizer::compare_functor(this));
1485 by Brian Aker
Updates to confine memroot
75
  if (! (cur_rowid= (unsigned char*) alloc.alloc_root(2 * head->cursor->ref_length)))
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
76
  {
77
    return 0;
78
  }
79
  prev_rowid= cur_rowid + head->cursor->ref_length;
80
  return 0;
81
}
82
83
1237.13.14 by Padraig O'Sullivan
Corrected some style issues in quick_ror_union_select.[cc,h] and range.cc
84
int optimizer::QuickRorUnionSelect::reset()
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
85
{
86
  int error;
87
  have_prev_rowid= false;
88
  if (! scans_inited)
89
  {
1237.13.15 by Padraig O'Sullivan
Used std::vector instead of List in the QuickRorUnionSelect class.
90
    for (vector<optimizer::QuickSelectInterface *>::iterator it= quick_selects.begin();
91
         it != quick_selects.end();
92
         ++it)
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
93
    {
1237.13.15 by Padraig O'Sullivan
Used std::vector instead of List in the QuickRorUnionSelect class.
94
      if ((*it)->init_ror_merged_scan(false))
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
95
      {
96
        return 0;
97
      }
98
    }
99
    scans_inited= true;
100
  }
101
  while (! queue->empty())
102
  {
103
    queue->pop();
104
  }
105
  /*
106
    Initialize scans for merged quick selects and put all merged quick
107
    selects into the queue.
108
  */
1237.13.15 by Padraig O'Sullivan
Used std::vector instead of List in the QuickRorUnionSelect class.
109
  for (vector<optimizer::QuickSelectInterface *>::iterator it= quick_selects.begin();
110
       it != quick_selects.end();
111
       ++it)
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
112
  {
1237.13.15 by Padraig O'Sullivan
Used std::vector instead of List in the QuickRorUnionSelect class.
113
    if ((*it)->reset())
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
114
    {
115
      return 0;
116
    }
1237.13.15 by Padraig O'Sullivan
Used std::vector instead of List in the QuickRorUnionSelect class.
117
    
118
    error= (*it)->get_next();
119
    if (error)
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
120
    {
121
      if (error == HA_ERR_END_OF_FILE)
122
      {
123
        continue;
124
      }
125
    }
1237.13.15 by Padraig O'Sullivan
Used std::vector instead of List in the QuickRorUnionSelect class.
126
    (*it)->save_last_pos();
127
    queue->push(*it);
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
128
  }
129
1491.1.10 by Jay Pipes
ha_rnd_init -> startTableScan, rnd_init -> doStartTableScan, ha_rnd_end -> endTableScan, rnd_end -> doEndTableScan
130
  if (head->cursor->startTableScan(1))
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
131
  {
132
    return 0;
133
  }
134
135
  return 0;
136
}
137
138
139
bool
1237.13.14 by Padraig O'Sullivan
Corrected some style issues in quick_ror_union_select.[cc,h] and range.cc
140
optimizer::QuickRorUnionSelect::push_quick_back(QuickSelectInterface *quick_sel_range)
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
141
{
1237.13.15 by Padraig O'Sullivan
Used std::vector instead of List in the QuickRorUnionSelect class.
142
  quick_selects.push_back(quick_sel_range);
143
  return false;
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
144
}
145
146
1237.13.14 by Padraig O'Sullivan
Corrected some style issues in quick_ror_union_select.[cc,h] and range.cc
147
optimizer::QuickRorUnionSelect::~QuickRorUnionSelect()
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
148
{
149
  while (! queue->empty())
150
  {
151
    queue->pop();
152
  }
153
  delete queue;
1237.13.15 by Padraig O'Sullivan
Used std::vector instead of List in the QuickRorUnionSelect class.
154
  for_each(quick_selects.begin(),
155
           quick_selects.end(),
156
           DeletePtr());
157
  quick_selects.clear();
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
158
  if (head->cursor->inited != Cursor::NONE)
159
  {
1491.1.10 by Jay Pipes
ha_rnd_init -> startTableScan, rnd_init -> doStartTableScan, ha_rnd_end -> endTableScan, rnd_end -> doEndTableScan
160
    head->cursor->endTableScan();
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
161
  }
1487 by Brian Aker
More updates for memory::Root
162
  alloc.free_root(MYF(0));
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
163
}
164
165
1237.13.14 by Padraig O'Sullivan
Corrected some style issues in quick_ror_union_select.[cc,h] and range.cc
166
bool optimizer::QuickRorUnionSelect::is_keys_used(const MyBitmap *fields)
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
167
{
1237.13.15 by Padraig O'Sullivan
Used std::vector instead of List in the QuickRorUnionSelect class.
168
  for (vector<optimizer::QuickSelectInterface *>::iterator it= quick_selects.begin();
169
       it != quick_selects.end();
170
       ++it)
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
171
  {
1237.13.15 by Padraig O'Sullivan
Used std::vector instead of List in the QuickRorUnionSelect class.
172
    if ((*it)->is_keys_used(fields))
173
    {
174
      return true;
175
    }
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
176
  }
1237.13.15 by Padraig O'Sullivan
Used std::vector instead of List in the QuickRorUnionSelect class.
177
  return false;
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
178
}
179
180
1237.13.14 by Padraig O'Sullivan
Corrected some style issues in quick_ror_union_select.[cc,h] and range.cc
181
int optimizer::QuickRorUnionSelect::get_next()
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
182
{
1237.13.14 by Padraig O'Sullivan
Corrected some style issues in quick_ror_union_select.[cc,h] and range.cc
183
  int error;
184
  int dup_row;
185
  optimizer::QuickSelectInterface *quick= NULL;
186
  unsigned char *tmp= NULL;
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
187
188
  do
189
  {
190
    do
191
    {
192
      if (queue->empty())
1237.13.14 by Padraig O'Sullivan
Corrected some style issues in quick_ror_union_select.[cc,h] and range.cc
193
        return HA_ERR_END_OF_FILE;
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
194
      /* Ok, we have a queue with >= 1 scans */
195
196
      quick= queue->top();
197
      memcpy(cur_rowid, quick->last_rowid, rowid_length);
198
199
      /* put into queue rowid from the same stream as top element */
200
      if ((error= quick->get_next()))
201
      {
202
        if (error != HA_ERR_END_OF_FILE)
203
          return(error);
204
        queue->pop();
205
      }
206
      else
207
      {
208
        quick->save_last_pos();
209
        queue->pop();
210
        queue->push(quick);
211
      }
212
213
      if (!have_prev_rowid)
214
      {
215
        /* No rows have been returned yet */
216
        dup_row= false;
217
        have_prev_rowid= true;
218
      }
219
      else
1237.13.14 by Padraig O'Sullivan
Corrected some style issues in quick_ror_union_select.[cc,h] and range.cc
220
        dup_row= ! head->cursor->cmp_ref(cur_rowid, prev_rowid);
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
221
    } while (dup_row);
222
223
    tmp= cur_rowid;
224
    cur_rowid= prev_rowid;
225
    prev_rowid= tmp;
226
227
    error= head->cursor->rnd_pos(quick->record, prev_rowid);
228
  } while (error == HA_ERR_RECORD_DELETED);
1237.13.14 by Padraig O'Sullivan
Corrected some style issues in quick_ror_union_select.[cc,h] and range.cc
229
  return error;
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
230
}
231
232
1237.13.14 by Padraig O'Sullivan
Corrected some style issues in quick_ror_union_select.[cc,h] and range.cc
233
void optimizer::QuickRorUnionSelect::add_info_string(String *str)
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
234
{
235
  bool first= true;
236
  str->append(STRING_WITH_LEN("union("));
1237.13.15 by Padraig O'Sullivan
Used std::vector instead of List in the QuickRorUnionSelect class.
237
  for (vector<optimizer::QuickSelectInterface *>::iterator it= quick_selects.begin();
238
       it != quick_selects.end();
239
       ++it)
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
240
  {
241
    if (! first)
242
      str->append(',');
243
    else
244
      first= false;
1237.13.15 by Padraig O'Sullivan
Used std::vector instead of List in the QuickRorUnionSelect class.
245
    (*it)->add_info_string(str);
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
246
  }
247
  str->append(')');
248
}
249
250
1237.13.14 by Padraig O'Sullivan
Corrected some style issues in quick_ror_union_select.[cc,h] and range.cc
251
void optimizer::QuickRorUnionSelect::add_keys_and_lengths(String *key_names,
252
                                                          String *used_lengths)
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
253
{
254
  bool first= true;
1237.13.15 by Padraig O'Sullivan
Used std::vector instead of List in the QuickRorUnionSelect class.
255
  for (vector<optimizer::QuickSelectInterface *>::iterator it= quick_selects.begin();
256
       it != quick_selects.end();
257
       ++it)
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
258
  {
259
    if (first)
1237.13.15 by Padraig O'Sullivan
Used std::vector instead of List in the QuickRorUnionSelect class.
260
    {
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
261
      first= false;
1237.13.15 by Padraig O'Sullivan
Used std::vector instead of List in the QuickRorUnionSelect class.
262
    }
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
263
    else
264
    {
265
      used_lengths->append(',');
266
      key_names->append(',');
267
    }
1237.13.15 by Padraig O'Sullivan
Used std::vector instead of List in the QuickRorUnionSelect class.
268
    (*it)->add_keys_and_lengths(key_names, used_lengths);
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
269
  }
270
}
271
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
272
} /* namespace drizzled */