Drizzled Public API Documentation

range.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 /* classes to use when handling where clause */
00021 
00022 #pragma once
00023 
00024 #include <drizzled/field.h>
00025 #include <drizzled/item/sum.h>
00026 #include <drizzled/table_reference.h>
00027 
00028 #include <queue>
00029 
00030 #include <boost/dynamic_bitset.hpp>
00031 
00032 namespace drizzled {
00033 
00034 typedef struct st_key_part
00035 {
00036   uint16_t key;
00037   uint16_t part;
00038   /* See KeyPartInfo for meaning of the next two: */
00039   uint16_t store_length;
00040   uint16_t length;
00041   uint8_t null_bit;
00046   uint8_t flag;
00047   Field *field;
00048 } KEY_PART;
00049 
00050 
00051 namespace optimizer {
00052 
00095 class QuickSelectInterface
00096 {
00097 public:
00098   bool sorted;
00099   ha_rows records; 
00100   double read_time; 
00101   Table *head;
00106   uint32_t index;
00111   uint32_t max_used_key_length;
00119   uint32_t used_key_parts;
00124   unsigned char *last_rowid;
00125 
00129   unsigned char *record;
00130 
00131   QuickSelectInterface();
00132   virtual ~QuickSelectInterface(){};
00133 
00152   virtual int init() = 0;
00153 
00171   virtual int reset(void) = 0;
00173   virtual int get_next() = 0;
00174 
00176   virtual void range_end() {}
00177 
00178   virtual bool reverse_sorted() const = 0;
00179 
00180   virtual bool unique_key_range() const
00181   {
00182     return false;
00183   }
00184 
00185   enum 
00186   {
00187     QS_TYPE_RANGE= 0,
00188     QS_TYPE_INDEX_MERGE= 1,
00189     QS_TYPE_RANGE_DESC= 2,
00190     QS_TYPE_ROR_INTERSECT= 4,
00191     QS_TYPE_ROR_UNION= 5,
00192     QS_TYPE_GROUP_MIN_MAX= 6
00193   };
00194 
00196   virtual int get_type() const = 0;
00197 
00212   virtual int init_ror_merged_scan(bool)
00213   {
00214     assert(0);
00215     return 1;
00216   }
00217 
00221   virtual void save_last_pos(){};
00222 
00229   virtual void add_keys_and_lengths(std::string *key_names,
00230                                     std::string *used_lengths)=0;
00231 
00241   virtual void add_info_string(std::string *)
00242   {}
00243 
00248   virtual bool is_keys_used(const boost::dynamic_bitset<>& fields);
00249 };
00250 
00255 typedef struct st_quick_range_seq_ctx
00256 {
00257   QuickRange **first;
00258   QuickRange **cur;
00259   QuickRange **last;
00260 } QuickRangeSequenceContext;
00261 
00262 range_seq_t quick_range_seq_init(void *init_param, uint32_t n_ranges, uint32_t flags);
00263 
00264 uint32_t quick_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range);
00265 
00274 class SqlSelect : public memory::SqlAlloc 
00275 {
00276  public:
00277   QuickSelectInterface *quick; 
00278   COND *cond; 
00279   Table *head;
00280   internal::io_cache_st *file; 
00281   ha_rows records; 
00282   double read_time; 
00283   key_map quick_keys; 
00284   key_map needed_reg; 
00285   table_map const_tables;
00286   table_map read_tables;
00287   bool free_cond;
00288 
00289   SqlSelect();
00290   ~SqlSelect();
00291   void cleanup();
00292   bool check_quick(Session *session, bool force_quick_range, ha_rows limit);
00293   bool skip_record();
00294   int test_quick_select(Session *session, key_map keys, table_map prev_tables,
00295                         ha_rows limit, bool force_quick_range,
00296                         bool ordered_output);
00297 };
00298 
00299 QuickRangeSelect *get_quick_select_for_ref(Session *session, 
00300                                            Table *table,
00301                                            table_reference_st *ref,
00302                                            ha_rows records);
00303 
00304 /*
00305   Create a QuickRangeSelect from given key and SEL_ARG tree for that key.
00306 
00307   SYNOPSIS
00308     get_quick_select()
00309       param
00310       idx            Index of used key in param->key.
00311       key_tree       SEL_ARG tree for the used key
00312       mrr_flags      MRR parameter for quick select
00313       mrr_buf_size   MRR parameter for quick select
00314       parent_alloc   If not NULL, use it to allocate memory for
00315                      quick select data. Otherwise use quick->alloc.
00316   NOTES
00317     The caller must call QUICK_SELECT::init for returned quick select.
00318 
00319     CAUTION! This function may change session->mem_root to a memory::Root which will be
00320     deallocated when the returned quick select is deleted.
00321 
00322   RETURN
00323     NULL on error
00324     otherwise created quick select
00325 */
00326 QuickRangeSelect *get_quick_select(Parameter *param,
00327                                    uint32_t index,
00328                                    SEL_ARG *key_tree, 
00329                                    uint32_t mrr_flags,
00330                                    uint32_t mrr_buf_size, 
00331                                    memory::Root *alloc);
00332 
00333 uint32_t get_index_for_order(Table *table, Order *order, ha_rows limit);
00334 
00335 SqlSelect *make_select(Table *head, 
00336                        table_map const_tables,
00337                        table_map read_tables, 
00338                        COND *conds,
00339                        bool allow_null_cond,
00340                        int *error);
00341 
00342 bool get_quick_keys(Parameter *param, 
00343                     QuickRangeSelect *quick,
00344                     KEY_PART *key,
00345                     SEL_ARG *key_tree, 
00346                     unsigned char *min_key,
00347                     uint32_t min_key_flag,
00348                     unsigned char *max_key,
00349                     uint32_t max_key_flag);
00350 
00351 } /* namespace optimizer */
00352 
00353 } /* namespace drizzled */
00354