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 namespace optimizer {
00030 
00037 class QuickRangeSelect : public QuickSelectInterface
00038 {
00039 protected:
00040   Cursor *cursor;
00041   DYNAMIC_ARRAY ranges; 
00044   bool in_ror_merged_scan;
00045   boost::dynamic_bitset<> *column_bitmap;
00046   boost::dynamic_bitset<> *save_read_set;
00047   boost::dynamic_bitset<> *save_write_set;
00048   bool free_file; 
00050   /* Range pointers to be used when not using MRR interface */
00051   QuickRange **cur_range; 
00052   QuickRange *last_range;
00053 
00055   QuickRangeSequenceContext qr_traversal_ctx;
00056   uint32_t mrr_buf_size; 
00059   KEY_PART *key_parts;
00060   KeyPartInfo *key_part_info;
00061 
00062   bool dont_free; 
00069   int cmp_next(QuickRange *range);
00070 
00074   int cmp_prev(QuickRange *range);
00075 
00093   bool row_in_ranges();
00094 
00095 public:
00096 
00097   uint32_t mrr_flags; 
00099   memory::Root alloc;
00100 
00101   QuickRangeSelect(Session *session,
00102                      Table *table,
00103                      uint32_t index_arg,
00104                      bool no_alloc,
00105                      memory::Root *parent_alloc);
00106 
00107   ~QuickRangeSelect();
00108 
00109   int init();
00110 
00111   int reset(void);
00112 
00127   int get_next();
00128 
00129   void range_end();
00130 
00158   int get_next_prefix(uint32_t prefix_length,
00159                       key_part_map keypart_map,
00160                       unsigned char *cur_prefix);
00161 
00162   bool reverse_sorted() const
00163   {
00164     return false;
00165   }
00166 
00170   bool unique_key_range() const;
00171 
00192   int init_ror_merged_scan(bool reuse_handler);
00193 
00194   void save_last_pos();
00195 
00196   int get_type() const
00197   {
00198     return QS_TYPE_RANGE;
00199   }
00200 
00201   void add_keys_and_lengths(std::string *key_names, std::string *used_lengths);
00202 
00203   void add_info_string(std::string *str);
00204 
00205   void resetCursor()
00206   {
00207     cursor= NULL;
00208   }
00209 
00210 private:
00211 
00212   /* Used only by QuickSelectDescending */
00213   QuickRangeSelect(const QuickRangeSelect& org) : QuickSelectInterface()
00214   {
00215     memmove(this, &org, sizeof(*this));
00216     /*
00217       Use default MRR implementation for reverse scans. No table engine
00218       currently can do an MRR scan with output in reverse index order.
00219     */
00220     mrr_flags|= HA_MRR_USE_DEFAULT_IMPL;
00221     mrr_buf_size= 0;
00222   }
00223 
00224   friend class ::drizzled::RorIntersectReadPlan; 
00225 
00226   friend
00227   QuickRangeSelect *get_quick_select_for_ref(Session *session, Table *table,
00228                                              struct table_reference_st *ref,
00229                                              ha_rows records);
00230 
00231   friend bool get_quick_keys(Parameter *param, 
00232                              QuickRangeSelect *quick,
00233                              KEY_PART *key, 
00234                              SEL_ARG *key_tree,
00235                              unsigned char *min_key, 
00236                              uint32_t min_key_flag,
00237                              unsigned char *max_key, 
00238                              uint32_t max_key_flag);
00239 
00240   friend QuickRangeSelect *get_quick_select(Parameter *,
00241                                             uint32_t idx,
00242                                             SEL_ARG *key_tree,
00243                                             uint32_t mrr_flags,
00244                                             uint32_t mrr_buf_size,
00245                                             memory::Root *alloc);
00246   friend class QuickSelectDescending;
00247 
00248   friend class QuickIndexMergeSelect;
00249 
00250   friend class QuickRorIntersectSelect;
00251 
00252   friend class QuickGroupMinMaxSelect;
00253 
00254   friend uint32_t quick_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range);
00255 
00256   friend range_seq_t quick_range_seq_init(void *init_param,
00257                                           uint32_t n_ranges, 
00258                                           uint32_t flags);
00259 
00260   friend void select_describe(Join *join, 
00261                               bool need_tmp_table, 
00262                               bool need_order,
00263                               bool distinct,
00264                               const char *message);
00265 };
00266 
00267 class QuickSelectDescending : public QuickRangeSelect
00268 {
00269 public:
00270 
00271   QuickSelectDescending(QuickRangeSelect *q, 
00272                         uint32_t used_key_parts,
00273                         bool *create_err);
00274 
00275   int get_next();
00276 
00277   bool reverse_sorted() const
00278   { 
00279     return true; 
00280   }
00281 
00282   int get_type() const
00283   { 
00284     return QS_TYPE_RANGE_DESC;
00285   }
00286 
00287 private:
00288 
00289   bool range_reads_after_key(QuickRange *range);
00290 
00291   int reset(void) 
00292   { 
00293     rev_it= rev_ranges.begin();
00294     return QuickRangeSelect::reset();
00295   }
00296 
00297   std::vector<QuickRange *> rev_ranges;
00298 
00299   std::vector<QuickRange *>::iterator rev_it;
00300 
00301 };
00302 
00303 } /* namespace optimizer */
00304 
00305 } /* namespace drizzled */
00306