Drizzled Public API Documentation

sel_imerge.cc

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 #include <config.h>
00021 
00022 #include <drizzled/sql_base.h>
00023 #include <drizzled/sql_select.h>
00024 #include <drizzled/memory/sql_alloc.h>
00025 #include <drizzled/optimizer/range.h>
00026 #include <drizzled/optimizer/range_param.h>
00027 #include <drizzled/optimizer/sel_arg.h>
00028 #include <drizzled/optimizer/sel_tree.h>
00029 #include <drizzled/optimizer/sel_imerge.h>
00030 
00031 using namespace std;
00032 using namespace drizzled;
00033 
00034 optimizer::SEL_IMERGE::SEL_IMERGE() 
00035   :
00036     trees(&trees_prealloced[0]),
00037     trees_next(trees),
00038     trees_end(trees + PREALLOCED_TREES)
00039 {}
00040 
00041 
00042 int optimizer::SEL_IMERGE::or_sel_tree(optimizer::RangeParameter *param, optimizer::SEL_TREE *tree)
00043 {
00044   if (trees_next == trees_end)
00045   {
00046     const int realloc_ratio= 2;   /* Double size for next round */
00047     uint32_t old_elements= (trees_end - trees);
00048     uint32_t old_size= sizeof(optimizer::SEL_TREE**) * old_elements;
00049     uint32_t new_size= old_size * realloc_ratio;
00050     optimizer::SEL_TREE **new_trees= NULL;
00051     if (! (new_trees= (optimizer::SEL_TREE**) param->mem_root->alloc_root(new_size)))
00052       return -1;
00053     memcpy(new_trees, trees, old_size);
00054     trees= new_trees;
00055     trees_next= trees + old_elements;
00056     trees_end= trees + old_elements * realloc_ratio;
00057   }
00058   *(trees_next++)= tree;
00059   return 0;
00060 }
00061 
00062 
00063 int optimizer::SEL_IMERGE::or_sel_tree_with_checks(optimizer::RangeParameter *param, optimizer::SEL_TREE *new_tree)
00064 {
00065   for (optimizer::SEL_TREE** tree = trees;
00066        tree != trees_next;
00067        tree++)
00068   {
00069     if (sel_trees_can_be_ored(*tree, new_tree, param))
00070     {
00071       *tree = tree_or(param, *tree, new_tree);
00072       if (!*tree)
00073         return 1;
00074       if (((*tree)->type == optimizer::SEL_TREE::MAYBE) ||
00075           ((*tree)->type == optimizer::SEL_TREE::ALWAYS))
00076         return 1;
00077       /* optimizer::SEL_TREE::IMPOSSIBLE is impossible here */
00078       return 0;
00079     }
00080   }
00081 
00082   /* New tree cannot be combined with any of existing trees. */
00083   return or_sel_tree(param, new_tree);
00084 }
00085 
00086 
00087 int optimizer::SEL_IMERGE::or_sel_imerge_with_checks(optimizer::RangeParameter *param, optimizer::SEL_IMERGE* imerge)
00088 {
00089   for (optimizer::SEL_TREE** tree= imerge->trees;
00090        tree != imerge->trees_next;
00091        tree++)
00092   {
00093     if (or_sel_tree_with_checks(param, *tree))
00094       return 1;
00095   }
00096   return 0;
00097 }
00098