KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.daffodilwoods.daffodildb.server.sql99.dql.plan.table;
2
3 import com.daffodilwoods.daffodildb.server.serversystem.*;
4 import com.daffodilwoods.daffodildb.server.sql99.common.*;
5 import com.daffodilwoods.daffodildb.server.sql99.dql.common.*;
6 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*;
7 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.*;
8 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*;
9 import com.daffodilwoods.database.resource.*;
10 import com.daffodilwoods.database.sqlinitiator.*;
11
12 /**
13  * This abstract class is responsible for providing the commong functionality of
14  * unexecutable plans like OrderSequncePlan and TemporaryMerge. It basically
15  * provides the functionality of merging of join condition to any one of
16  * unexecutable plans.
17  * <p>Title: </p>
18  * <p>Description: </p>
19  * <p>Copyright: Copyright (c) 2003</p>
20  * <p>Company: </p>
21  * @author unascribed
22  * @version 1.0
23  */

24
25 public abstract class OrderTablePlanAbstract extends PlanExceptionAbstract implements TableExpressionConstants {
26
27    /**
28     * This method is required when we've to merge join condition to this plan. If
29     * the plans, corresponding to tables involved in condition, are at
30     * consecutive places then join of both plans is formed and it replaces
31     * both the plans in the array of child plans.
32     * If the plans are not at consecutive places, then a temporary merge is
33     * formed which includes all the plans from one position to another in the
34     * same sequence. And this plan replaces all the child plans in the array
35     * of child plans.
36     * @param joinRelations
37     * @throws DException
38     */

39
40    public void merge(_JoinRelation joinRelations) throws DException {
41       TableDetails[] tableDetails = joinRelations.getTableDetails();
42       int mappingPosition1 = getTablePosition(tableDetails[0]);
43       _TablePlan tablePlan1 = childPlans[mappingPosition1];
44       int mappingPosition2 = getTablePosition(tableDetails[1]);
45       _TablePlan tablePlan2 = childPlans[mappingPosition2];
46       int[] tablePositions = new int[] {mappingPosition1, mappingPosition2};
47       changeTablePlans(tablePlan1, tablePlan2, joinRelations, tableDetails, tablePositions);
48    }
49
50    /**
51     * This method is used to replace both plans from the resultant plan that
52     * is formed with the help of join condition.
53     * @param tablePlan1
54     * @param tablePlan2
55     * @param joinRelation
56     * @param tableDetails
57     * @param mappingPositions
58     * @throws DException
59     */

60
61    private void changeTablePlans(_TablePlan tablePlan1, _TablePlan tablePlan2, _JoinRelation joinRelation, TableDetails[] tableDetails, int[] mappingPositions) throws DException {
62       int index1 = getIndex(tableDetails[0]);
63       int index2 = getIndex(tableDetails[1]);
64       if (index1 > index2) {
65          int temp = index1;
66          index1 = index2;
67          index2 = temp;
68       }
69       if (index2 - index1 == 0) {
70          tablePlan1.merge(joinRelation);
71       } else
72       if ( (index2 - index1) > 1) { //prepare TemporaryMerge
73
prepareTableMergeAndUpdateTablePlans(tablePlan1, joinRelation.getCondition(), index1, index2);
74       } else { //when TempMerge is not required.So Only TowTableJoinPlan 'll be formed.
75
prepareTwoTableJoinPlanAndUpdateTablePlans(tablePlan1, joinRelation.getCondition(), index1, index2);
76       }
77    }
78
79    /**
80     * It helps in providing the index of tableplan to which passed table belongs
81     * to.
82     * @param tableDetails
83     * @return
84     * @throws DException
85     */

86
87    private int getTablePosition(TableDetails tableDetails) throws DException {
88       int length = childPlans.length;
89       for (int i = 0; i < length; i++) {
90          if (childPlans[i].ifExists(tableDetails)) {
91             return i;
92          }
93       }
94       throw new DException("DSE3526", new Object JavaDoc[] {tableDetails.getNameOfTable()});
95    }
96
97    /**
98     * This method is required when we've to merge join condition to this plan. And
99     * If the plans are not at consecutive places, then a temporary merge is
100     * formed which includes all the plans from one position to another in the
101     * same sequence. And this plan replaces all the child plans in the array
102     * of child plans.
103     * @param orderSequencePlan
104     * @param bve
105     * @param index1
106     * @param index2
107     * @throws DException
108     */

109
110    private void prepareTableMergeAndUpdateTablePlans(_TablePlan orderSequencePlan, booleanvalueexpression bve, int index1, int index2) throws DException {
111       _TablePlan[] tp = childPlans;
112       int len = tp.length;
113       TemporaryMerge tempMerge = new TemporaryMerge();
114       for (int i = index1; i <= index2; i++) {
115          tempMerge.addTablePlan(tp[i]);
116       }
117       tempMerge.addCondition(bve);
118       _TablePlan[] newTp = new _TablePlan[tp.length - (index2 - index1)];
119       System.arraycopy(tp, 0, newTp, 0, index1);
120       newTp[index1] = tempMerge;
121       System.arraycopy(tp, index2 + 1, newTp, index1 + 1, tp.length - index2 - 1);
122       setPlans(newTp);
123    }
124
125    /**
126     * This method is required when we've to merge join condition to this plan. If
127     * the plans, corresponding to tables involved in condition, are at
128     * consecutive places then join of both plans is formed and it replaces
129     * both the plans in the array of child plans.
130     * @param orderSequencePlan
131     * @param bve
132     * @param index1
133     * @param index2
134     * @throws DException
135     */

136
137    private void prepareTwoTableJoinPlanAndUpdateTablePlans(_TablePlan orderSequencePlan, booleanvalueexpression bve, int index1, int index2) throws DException {
138       _TablePlan[] tp = childPlans;
139       int len = tp.length;
140       TwoTableJoinPlan ttJp = new TwoTableJoinPlan(tp[index1], tp[index2], bve);
141       _TablePlan[] newTp = new _TablePlan[tp.length - 1];
142       System.arraycopy(tp, 0, newTp, 0, index1);
143       newTp[index1] = ttJp;
144       System.arraycopy(tp, index1 + 1, newTp, index1 + 1, index2 - index1 - 1);
145       System.arraycopy(tp, index2 + 1, newTp, index2, newTp.length - index2);
146       setPlans(newTp);
147    }
148
149    /**
150     * Returns the index of the plan to which passed table belongs to.
151     * @param tableDetails
152     * @return
153     * @throws DException
154     */

155
156    public int getIndex(TableDetails tableDetails) throws DException {
157       for (int i = 0; i < childPlans.length; i++) {
158          if (childPlans[i].ifExists(tableDetails)) {
159             return i;
160          }
161       }
162       throw new DException("DSE3526", new Object JavaDoc[] {tableDetails.getNameOfTable()});
163    }
164
165    /**
166     * Sets the child plans.
167     * @param childPlans0
168     */

169
170    private void setPlans(_TablePlan childPlans0[]) {
171       childPlans = childPlans0;
172    }
173
174    /**
175     * This method is used to set the join condition to the plans present on
176     * passed index.
177     * @param index
178     * @param joinRelation
179     * @throws DException
180     */

181
182    public void setJoinConditionOfChild(int index, _JoinRelation joinRelation) throws DException {
183       int type = childPlans[index].getType();
184       if (type != TableExpressionConstants.SINGLETABLEPLAN) {
185          childPlans[index].merge(joinRelation);
186       } else {
187          ( (_SingleTablePlan) childPlans[index]).setCondition(joinRelation.getCondition());
188       }
189    }
190
191    /**
192     * Returns the underlying plans
193     * @return
194     * @throws DException
195     */

196
197    public _TablePlan[] getTablePlans() throws DException {
198       return this.childPlans;
199    }
200
201    /**
202     * This method is used to remove the cover of unExecutable plans like
203     * OrderSequencePlan and TemporaryMergePlan. It returns the NestedLoopJoinPlan
204     * of underlying plans.
205     * @return
206     * @throws DException
207     */

208
209    public _TablePlan joinMissingLink() throws DException {
210       for (int i = 0; i < childPlans.length; i++) {
211          childPlans[i] = childPlans[i].joinMissingLink();
212       }
213       return childPlans.length == 1 ? childPlans[0] : new NestedLoopJoinPlan(childPlans, remainingCondition);
214    }
215
216    /**
217     * This method is used to check whether there is any OrderSequencePlan is
218     * present in plan hierarchy. This is required when we've to merge join
219     * relation among different plan.
220     * @return
221     * @throws DException
222     */

223
224    public boolean containsOrderSequencePlan() throws DException {
225       return true;
226    }
227
228    /**
229     * Returns the underlying plans
230     * @return
231     * @throws DException
232     */

233
234    public _TablePlan[] getChildTablePlans() throws DException {
235       return childPlans;
236    }
237
238    /**
239     * The following methods will never be called as this non executable plan.
240     */

241
242    public _Iterator execute(_ServerSession session) throws DException {
243       /**@todo: Implement this com.daffodilwoods.database.sql.select._TablePlan method*/
244       throw new java.lang.UnsupportedOperationException JavaDoc("Method execute() not yet implemented.");
245    }
246
247    public _Iterator execute(_ServerSession session, booleanvalueexpression condition) throws DException {
248       /**@todo Implement this com.daffodilwoods.daffodildb.server.sql99.dql.tableexpression.fromclause._TablePlan method*/
249       throw new java.lang.UnsupportedOperationException JavaDoc("Method execute() not yet implemented.");
250    }
251
252    public _Iterator executeWithoutOrder(_ServerSession session) throws DException {
253       /**@todo Implement this com.daffodilwoods.daffodildb.server.sql99.dql.tableexpression.fromclause._TablePlan method*/
254       throw new java.lang.UnsupportedOperationException JavaDoc("Method executeWithoutOrder() not yet implemented.");
255    }
256
257    public _Iterator executeWithOutOrder(_ServerSession session, booleanvalueexpression condition) throws DException {
258       /**@todo Implement this com.daffodilwoods.daffodildb.server.sql99.dql.tableexpression.fromclause._TablePlan method*/
259       throw new java.lang.UnsupportedOperationException JavaDoc("Method executeWithOutOrder() not yet implemented.");
260    }
261
262    public double getCost(_ServerSession session, booleanvalueexpression condition) throws DException {
263       /**@todo: Implement this com.daffodilwoods.database.sql.select._TablePlan method*/
264       throw new java.lang.UnsupportedOperationException JavaDoc("Method getCost() not yet implemented.");
265    }
266
267    public _Order getOrder() throws DException {
268       throw new java.lang.UnsupportedOperationException JavaDoc("Method getOrder() not yet implemented." + getClass());
269    }
270
271 }
272
Popular Tags