1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008-2009 Sun Microsystems
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.
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.
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
21
#include "drizzled/session.h"
22
#include "drizzled/util/functors.h"
23
#include "drizzled/optimizer/range.h"
24
#include "drizzled/optimizer/quick_range_select.h"
25
#include "drizzled/optimizer/quick_ror_union_select.h"
31
using namespace drizzled;
34
optimizer::QuickRorUnionSelect::QuickRorUnionSelect(Session *session_param,
37
session(session_param),
42
rowid_length= table->cursor->ref_length;
43
record= head->record[0];
44
memory::init_sql_alloc(&alloc, session->variables.range_alloc_block_size, 0);
45
session_param->mem_root= &alloc;
49
* Function object that is used as the comparison function
50
* for the priority queue in the QuickRorUnionSelect
53
class optimizer::compare_functor
55
optimizer::QuickRorUnionSelect *self;
57
compare_functor(optimizer::QuickRorUnionSelect *in_arg)
59
inline bool operator()(const optimizer::QuickSelectInterface *i, const optimizer::QuickSelectInterface *j) const
61
int val= self->head->cursor->cmp_ref(i->last_rowid,
68
int optimizer::QuickRorUnionSelect::init()
71
new priority_queue<optimizer::QuickSelectInterface *,
72
vector<optimizer::QuickSelectInterface *>,
73
optimizer::compare_functor >(optimizer::compare_functor(this));
74
if (! (cur_rowid= (unsigned char*) alloc_root(&alloc, 2*head->cursor->ref_length)))
78
prev_rowid= cur_rowid + head->cursor->ref_length;
83
int optimizer::QuickRorUnionSelect::reset()
86
have_prev_rowid= false;
89
for (vector<optimizer::QuickSelectInterface *>::iterator it= quick_selects.begin();
90
it != quick_selects.end();
93
if ((*it)->init_ror_merged_scan(false))
100
while (! queue->empty())
105
Initialize scans for merged quick selects and put all merged quick
106
selects into the queue.
108
for (vector<optimizer::QuickSelectInterface *>::iterator it= quick_selects.begin();
109
it != quick_selects.end();
117
error= (*it)->get_next();
120
if (error == HA_ERR_END_OF_FILE)
125
(*it)->save_last_pos();
129
if (head->cursor->ha_rnd_init(1))
139
optimizer::QuickRorUnionSelect::push_quick_back(QuickSelectInterface *quick_sel_range)
141
quick_selects.push_back(quick_sel_range);
146
optimizer::QuickRorUnionSelect::~QuickRorUnionSelect()
148
while (! queue->empty())
153
for_each(quick_selects.begin(),
156
quick_selects.clear();
157
if (head->cursor->inited != Cursor::NONE)
159
head->cursor->ha_rnd_end();
161
free_root(&alloc,MYF(0));
166
bool optimizer::QuickRorUnionSelect::is_keys_used(const MyBitmap *fields)
168
for (vector<optimizer::QuickSelectInterface *>::iterator it= quick_selects.begin();
169
it != quick_selects.end();
172
if ((*it)->is_keys_used(fields))
181
int optimizer::QuickRorUnionSelect::get_next()
185
optimizer::QuickSelectInterface *quick= NULL;
186
unsigned char *tmp= NULL;
193
return HA_ERR_END_OF_FILE;
194
/* Ok, we have a queue with >= 1 scans */
197
memcpy(cur_rowid, quick->last_rowid, rowid_length);
199
/* put into queue rowid from the same stream as top element */
200
if ((error= quick->get_next()))
202
if (error != HA_ERR_END_OF_FILE)
208
quick->save_last_pos();
213
if (!have_prev_rowid)
215
/* No rows have been returned yet */
217
have_prev_rowid= true;
220
dup_row= ! head->cursor->cmp_ref(cur_rowid, prev_rowid);
224
cur_rowid= prev_rowid;
227
error= head->cursor->rnd_pos(quick->record, prev_rowid);
228
} while (error == HA_ERR_RECORD_DELETED);
233
void optimizer::QuickRorUnionSelect::add_info_string(String *str)
236
str->append(STRING_WITH_LEN("union("));
237
for (vector<optimizer::QuickSelectInterface *>::iterator it= quick_selects.begin();
238
it != quick_selects.end();
245
(*it)->add_info_string(str);
251
void optimizer::QuickRorUnionSelect::add_keys_and_lengths(String *key_names,
252
String *used_lengths)
255
for (vector<optimizer::QuickSelectInterface *>::iterator it= quick_selects.begin();
256
it != quick_selects.end();
265
used_lengths->append(',');
266
key_names->append(',');
268
(*it)->add_keys_and_lengths(key_names, used_lengths);