Drizzled Public API Documentation

quick_range_select.h
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008-2009 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 #pragma once
00021 
00022 #include <drizzled/dynamic_array.h>
00023 #include <drizzled/optimizer/range.h>
00024 
00025 #include <boost/dynamic_bitset.hpp>
00026 #include <vector>
00027 
00028 namespace drizzled
00029 {
00030 
00031 class Cursor;
00032 
00033 namespace optimizer
00034 {
00035 
00042 class QuickRangeSelect : public QuickSelectInterface
00043 {
00044 protected:
00045   Cursor *cursor;
00046   DYNAMIC_ARRAY ranges; 
00049   bool in_ror_merged_scan;
00050   boost::dynamic_bitset<> *column_bitmap;
00051   boost::dynamic_bitset<> *save_read_set;
00052   boost::dynamic_bitset<> *save_write_set;
00053   bool free_file; 
00055   /* Range pointers to be used when not using MRR interface */
00056   QuickRange **cur_range; 
00057   QuickRange *last_range;
00058 
00060   QuickRangeSequenceContext qr_traversal_ctx;
00061   uint32_t mrr_buf_size; 
00064   KEY_PART *key_parts;
00065   KeyPartInfo *key_part_info;
00066 
00067   bool dont_free; 
00074   int cmp_next(QuickRange *range);
00075 
00079   int cmp_prev(QuickRange *range);
00080 
00098   bool row_in_ranges();
00099 
00100 public:
00101 
00102   uint32_t mrr_flags; 
00104   memory::Root alloc;
00105 
00106   QuickRangeSelect(Session *session,
00107                      Table *table,
00108                      uint32_t index_arg,
00109                      bool no_alloc,
00110                      memory::Root *parent_alloc);
00111 
00112   ~QuickRangeSelect();
00113 
00114   int init();
00115 
00116   int reset(void);
00117 
00132   int get_next();
00133 
00134   void range_end();
00135 
00163   int get_next_prefix(uint32_t prefix_length,
00164                       key_part_map keypart_map,
00165                       unsigned char *cur_prefix);
00166 
00167   bool reverse_sorted() const
00168   {
00169     return false;
00170   }
00171 
00175   bool unique_key_range() const;
00176 
00197   int init_ror_merged_scan(bool reuse_handler);
00198 
00199   void save_last_pos();
00200 
00201   int get_type() const
00202   {
00203     return QS_TYPE_RANGE;
00204   }
00205 
00206   void add_keys_and_lengths(std::string *key_names, std::string *used_lengths);
00207 
00208   void add_info_string(std::string *str);
00209 
00210   void resetCursor()
00211   {
00212     cursor= NULL;
00213   }
00214 
00215 private:
00216 
00217   /* Used only by QuickSelectDescending */
00218   QuickRangeSelect(const QuickRangeSelect& org) : QuickSelectInterface()
00219   {
00220     memmove(this, &org, sizeof(*this));
00221     /*
00222       Use default MRR implementation for reverse scans. No table engine
00223       currently can do an MRR scan with output in reverse index order.
00224     */
00225     mrr_flags|= HA_MRR_USE_DEFAULT_IMPL;
00226     mrr_buf_size= 0;
00227   }
00228 
00229   friend class ::drizzled::RorIntersectReadPlan; 
00230 
00231   friend
00232   QuickRangeSelect *get_quick_select_for_ref(Session *session, Table *table,
00233                                              struct table_reference_st *ref,
00234                                              ha_rows records);
00235 
00236   friend bool get_quick_keys(Parameter *param, 
00237                              QuickRangeSelect *quick,
00238                              KEY_PART *key, 
00239                              SEL_ARG *key_tree,
00240                              unsigned char *min_key, 
00241                              uint32_t min_key_flag,
00242                              unsigned char *max_key, 
00243                              uint32_t max_key_flag);
00244 
00245   friend QuickRangeSelect *get_quick_select(Parameter *,
00246                                             uint32_t idx,
00247                                             SEL_ARG *key_tree,
00248                                             uint32_t mrr_flags,
00249                                             uint32_t mrr_buf_size,
00250                                             memory::Root *alloc);
00251   friend class QuickSelectDescending;
00252 
00253   friend class QuickIndexMergeSelect;
00254 
00255   friend class QuickRorIntersectSelect;
00256 
00257   friend class QuickGroupMinMaxSelect;
00258 
00259   friend uint32_t quick_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range);
00260 
00261   friend range_seq_t quick_range_seq_init(void *init_param,
00262                                           uint32_t n_ranges, 
00263                                           uint32_t flags);
00264 
00265   friend void select_describe(Join *join, 
00266                               bool need_tmp_table, 
00267                               bool need_order,
00268                               bool distinct,
00269                               const char *message);
00270 };
00271 
00272 class QuickSelectDescending : public QuickRangeSelect
00273 {
00274 public:
00275 
00276   QuickSelectDescending(QuickRangeSelect *q, 
00277                         uint32_t used_key_parts,
00278                         bool *create_err);
00279 
00280   int get_next();
00281 
00282   bool reverse_sorted() const
00283   { 
00284     return true; 
00285   }
00286 
00287   int get_type() const
00288   { 
00289     return QS_TYPE_RANGE_DESC;
00290   }
00291 
00292 private:
00293 
00294   bool range_reads_after_key(QuickRange *range);
00295 
00296   int reset(void) 
00297   { 
00298     rev_it= rev_ranges.begin();
00299     return QuickRangeSelect::reset();
00300   }
00301 
00302   std::vector<QuickRange *> rev_ranges;
00303 
00304   std::vector<QuickRange *>::iterator rev_it;
00305 
00306 };
00307 
00308 } /* namespace optimizer */
00309 
00310 } /* namespace drizzled */
00311