KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > dql > plan > table > CostCalculator


1 package com.daffodilwoods.daffodildb.server.sql99.dql.plan.table;
2
3 import com.daffodilwoods.daffodildb.server.datasystem.indexsystem.*;
4 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.condition.*;
5 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*;
6 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.predicates.*;
7 import com.daffodilwoods.database.resource.*;
8 import com.daffodilwoods.database.sqlinitiator.*;
9
10
11 /**
12  * It provides functionality to calculate the cost for solving condition and
13  * order.
14  * <p>Title: </p>
15  * <p>Description: </p>
16  * <p>Copyright: Copyright (c) 2003</p>
17  * <p>Company: </p>
18  * @author unascribed
19  * @version 1.0
20  */

21
22 public class CostCalculator implements CostFactorConstants {
23
24    /**
25     * It allows user to obtain the cost as well as number of rows, when
26     * condition is to solve on the passed number of rows using index.
27     * @param indexPredicates contains the predicates which will be solved using
28     * index.
29     * @param estimatedRows represents the number of rows on which condition will
30     * be evaluated
31     * @return cost calculated and reduced number of rows that results after
32     * condition evaluation.
33     * @throws DException
34     */

35
36    public static Object JavaDoc getCostForIndexedPredicate(AllColumnPredicates indexPredicates, long estimatedRows) throws DException {
37       if (indexPredicates == null) {
38          return null;
39       }
40       _SingleColumnPredicate[] singleColumnPredicate = indexPredicates.getSingleColumnPredicates();
41       double ETROW = estimatedRows;
42       double cost = 0;
43       for (int i = 0, length = singleColumnPredicate.length; i < length; ++i) {
44          if (singleColumnPredicate[i] != null) {
45             predicate pred = singleColumnPredicate[i].getPredicate();
46             int predicateType = pred.getPredicateType();
47             cost += ETROW * INDEXPREDICATE / 100; /// Index Evaluating Cost
48
ETROW = pred.getEstimatedRows( (long) ETROW);
49          }
50       }
51       return new Object JavaDoc[] {new Double JavaDoc(cost), new Double JavaDoc(ETROW)};
52    }
53
54    /**
55     * It allows user to calculate the cost of passed index which is choosen for
56     * obtaining the result. It takes condition, order and query columns into the
57     * account while calculating the condition. Firstly cost of condition execution
58     * is calculated, then it is checked whether order can be solved using that
59     * index. If not, then cost for temporary index on the resultant rows of
60     * condition is added to the cost. At last query columns are checked, whether
61     * they can be satisfied by index passed, if not then cost of obtaining the
62     * values of those columns from actual table, are also added to the resultant
63     * cost.
64     * @param indexPredicate
65     * @param indexTable
66     * @param order
67     * @param queryColumns
68     * @return
69     * @throws DException
70     */

71    public static double computeCostForIndex(IndexPredicate indexPredicate, Object JavaDoc indexTable, _Order order, String JavaDoc[] queryColumns, _IndexInformation indexInformation, int indexPosition) throws DException {
72       double cost = indexPredicate.getCost(indexTable); //1500
73
double ETROW = indexPredicate.getEstimatedRow(); //200
74
int num = TableReferenceMerger.checkForOrderColumns(order, indexInformation);
75       boolean checkForOrderSolvableByIndex = num != -1;
76       if (!checkForOrderSolvableByIndex) { // true means, yes solvable by index.
77
cost = cost + getCostForTemporaryOrder(ETROW);
78          indexPredicate.setOrder(order);
79       } else {
80          if (order != null) {
81             indexPredicate.setIndex(indexPosition);
82             order.setIndexIsSameOrReverse(num == 0);
83          }
84       }
85       boolean checkForQueryColumnsSolvableByIndex = TableReferenceMerger.checkForQueryColumns(indexInformation.getColumns(), queryColumns);
86       if (!checkForQueryColumnsSolvableByIndex) { // true means, yes solvable by index.
87
cost = cost + getCostForQueryColumns(ETROW);
88       }
89       return cost;
90    }
91
92    /**
93     * It allows user to obtain the cost as well as number of rows, when
94     * condition is to solve on the passed number of rows without using index.
95     * @param nonIndexPredicates contains the predicates which will be solved
96     * sequentially.
97     * @param estimatedRows represents the number of rows on which condition will
98     * be evaluated
99     * @return cost calculated and reduced number of rows that results after
100     * condition evaluation.
101     * @throws DException
102     */

103
104    public static Object JavaDoc getCostForNonIndexedPredicate(AllColumnPredicates nonIndexPredicates, double estimatedRows) throws DException {
105       if (nonIndexPredicates == null) {
106          return null;
107       }
108       booleanvalueexpression cond = nonIndexPredicates.getNonIndexedCondition();
109       double cost = cond.getCost( (long) estimatedRows, false);
110       estimatedRows = (double) cond.getEstimatedRows( (long) estimatedRows);
111       return new Object JavaDoc[] {new Double JavaDoc(cost), new Double JavaDoc(estimatedRows)};
112    }
113
114    /**
115     * Returns the cost of making temporary order on the passed number of rows.
116     * @param estimatedRows
117     * @return
118     * @throws DException
119     */

120
121    public static double getCostForTemporaryOrder(double estimatedRows) throws DException {
122       return estimatedRows * ORDER / 100;
123    }
124
125    /**
126     * Returns the cost of retrieving the value of columns for passed number of
127     * rows from actual table. It is required when index is choosen for a table,
128     * but all the columns of the table do not belong to that index.
129     * @param estimatedRows
130     * @return
131     * @throws DException
132     */

133
134    public static double getCostForQueryColumns(double estimatedRows) throws DException {
135       return estimatedRows * TABLESCAN / 100;
136    }
137
138 }
139
Popular Tags