-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreatePlanTree.java
More file actions
278 lines (223 loc) · 7.59 KB
/
Copy pathCreatePlanTree.java
File metadata and controls
278 lines (223 loc) · 7.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package quest;
/*
* this file is taken from Picasso code.
*/
import iisc.dsl.picasso.common.ds.TreeNode;
import iisc.dsl.picasso.server.plan.Node;
import iisc.dsl.picasso.server.plan.Plan;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;
public class CreatePlanTree
{
private int leafid=0;
public Plan otherPlans[] = new Plan[QUESTConstants.numExtraPlans - 1];
void createAllPlanTreeStructure(String planWithExplain[], int totalPlans, AllObjects allObjects)
{
TreeNode planTreeRootNodes[] = new TreeNode[totalPlans];
Vector<String> textualPlan = new Vector<String>();
try
{
ResultSet rs=null;
ConnectDB connectDBObj = allObjects.getConnectDBObj();
Connection con = connectDBObj.connection;
Statement st = con.createStatement();
CreatePlanTree createPlanTree = new CreatePlanTree();
for(int i=0;i<totalPlans;i++)
{
// Plan plan = new Plan();
textualPlan.clear();
rs = st.executeQuery(planWithExplain[i]);
while (rs.next())
textualPlan.add(rs.getString(1));
textualPlan.remove(textualPlan.size()-1); //removing completed/Not Completed Line
planTreeRootNodes[i] = createPlanTree.getPlanStructure(textualPlan);
}
BouquetData bouquetDataObj = allObjects.getBouquetDataObj();
bouquetDataObj.setPlanTreeRootNodes(planTreeRootNodes);
rs.close();
st.close();
}
catch(Exception e)
{
System.out.println("Execption in DrawPlan Class:"+e);
e.printStackTrace();
}
}
TreeNode getPlanStructure(Vector<String> textualPlan)
{
Plan plan = new Plan();
String str = (String)textualPlan.remove(0);
CreateNode(plan, str, 0, -1);
plan.isOptimal = true;
FindChilds(plan, 0, 1, textualPlan, 2);
SwapSORTChilds(plan);
TreeNode root = plan.createPlanTree();
return(root);
}
int CreateNode(Plan plan, String str, int id, int parentid)
{
if(id==1)
leafid=-1;
Node node = new Node();
if(str.indexOf("->")>=0)
str=str.substring(str.indexOf("->")+2).trim();
String cost = str.substring(str.indexOf("..") + 2, str.indexOf("rows") - 1);
String card = str.substring(str.indexOf("rows") + 5, str.indexOf("width")-1);
if(str.indexOf(" on ") != -1 ||str.startsWith("Subquery Scan")) {
node.setId(id++);
node.setParentId(parentid);
node.setCost(Double.parseDouble(cost));
node.setCard(Double.parseDouble(card));
if(str.startsWith("Index Scan"))
node.setName("Index Scan");
else if(str.startsWith("Subquery Scan"))
node.setName("Subquery Scan");
else
node.setName(str.substring(0,str.indexOf(" on ")).trim());
plan.setNode(node,plan.getSize());
node = new Node();
node.setId(leafid--);
node.setParentId(id-1);
node.setCost(0.0);
node.setCard(0.0);
if(str.startsWith("Subquery Scan"))
node.setName(str.trim().substring("Subquery Scan".length(),str.indexOf("(")).trim());
else
node.setName(str.substring(str.indexOf(" on ")+3,str.indexOf("(")).trim());
plan.setNode(node,plan.getSize());
} else {
node.setId(id++);
node.setParentId(parentid);
node.setCost(Double.parseDouble(cost));
node.setCard(Double.parseDouble(card));
node.setName(str.substring(0,str.indexOf("(")).trim());
plan.setNode(node,plan.getSize());
}
return id;
}
boolean optFlag;
int FindChilds(Plan plan, int parentid, int id, Vector text, int childindex)
{
String str ="";
int oldchildindex=-5;
while(text.size()>0) {
int stindex;
str = (String)text.remove(0);
if(QUESTConstants.TOP_K == true){
if(str.trim().startsWith("NEXT PLAN")){
if(QUESTConstants.currentExtraPlanNum > 0 && QUESTConstants.lookingExtraPlans == false){
text.addElement(( Object )str);
QUESTConstants.lookingExtraPlans = true;
return 0;
}
if(QUESTConstants.saveExtraPlans == false){
if(QUESTConstants.currentExtraPlanNum == 0) //8
QUESTConstants.lookingExtraPlans = true;
str = (String)text.remove(0); //7
int temp = QUESTConstants.currentExtraPlanNum + 2; //6
System.out.println("Plan "+ temp +" cost = " +str.substring(str.indexOf("..") + 2, str.indexOf("rows") - 1)); //5
QUESTConstants.otherPlanCosts[QUESTConstants.currentExtraPlanNum] = Double.parseDouble( str.substring(str.indexOf("..") + 2, str.indexOf("rows") - 1)); //4
// return id;
QUESTConstants.currentExtraPlanNum++; //3
if(QUESTConstants.lookingExtraPlans == true){//2
// System.out.println(str);
continue;
} //1
}
else if(QUESTConstants.lookingExtraPlans == false){
//text.addElement(( Object )str);
QUESTConstants.lookingExtraPlans = true;
continue;
}
}
else if(QUESTConstants.lookingExtraPlans == true) {
if(QUESTConstants.saveExtraPlans == true){
if(QUESTConstants.currentExtraPlanNum == 0)
SwapSORTChilds(plan);
otherPlans[QUESTConstants.currentExtraPlanNum] = new Plan();
// System.out.println("Create node works on: \n" + str);
CreateNode(otherPlans[QUESTConstants.currentExtraPlanNum], str, 0, -1);
QUESTConstants.lookingExtraPlans = false;
FindChilds(otherPlans[QUESTConstants.currentExtraPlanNum++], 0, 1, text, 2);
QUESTConstants.currentExtraPlanNum--;
SwapSORTChilds(otherPlans[QUESTConstants.currentExtraPlanNum]);
QUESTConstants.otherPlanCosts[QUESTConstants.currentExtraPlanNum] = otherPlans[QUESTConstants.currentExtraPlanNum].getCost();
//int temp = QUESTConstants.currentExtraPlanNum + 2;
//System.out.println("Plan "+ temp +"Cost from plan = " + otherPlans[QUESTConstants.currentExtraPlanNum].getCost());
//System.out.println(str);
}
continue;
}
}
// System.out.println("findling_childs");
if (str.indexOf("Plan Type: STABLE")>=0)
optFlag = false;
if(str.trim().startsWith("InitPlan"))
stindex=str.indexOf("InitPlan");
else if(str.trim().startsWith("SubPlan"))
stindex=str.indexOf("SubPlan");
else
stindex=str.indexOf("->");
if(stindex==-1)
continue;
if(stindex==oldchildindex) {
childindex=oldchildindex;
oldchildindex=-5;
}
if(stindex < childindex) {
text.add(0,str);
break;
}
if(stindex>childindex) {
if(str.trim().startsWith("InitPlan")||str.trim().startsWith("SubPlan")) {
str = (String)text.remove(0);
stindex=str.indexOf("->");
oldchildindex=childindex;
childindex=str.indexOf("->");
}
text.add(0,str);
id = FindChilds(plan, id-1, id, text, stindex);
continue;
}
if(str.trim().startsWith("InitPlan")||str.trim().startsWith("SubPlan")) {
str = (String)text.remove(0);
stindex=str.indexOf("->");
oldchildindex=childindex;
childindex=str.indexOf("->");
}
if(stindex==childindex)
id = CreateNode(plan,str, id, parentid);
}
return id;
}
void SwapSORTChilds(Plan plan)
{
for(int i =0;i<plan.getSize();i++) {
Node node = plan.getNode(i);
if(node.getName().equals("Sort")) {
int k=0;
Node[] chnodes = new Node[2];
for(int j=0;j<plan.getSize();j++) {
if(plan.getNode(j).getParentId() == node.getId()) {
if(k==0)chnodes[0]=plan.getNode(j);
else chnodes[1]=plan.getNode(j);
k++;
}
}
if(k>=2) {
k=chnodes[0].getId();
chnodes[0].setId(chnodes[1].getId());
chnodes[1].setId(k);
for(int j=0;j<plan.getSize();j++) {
if(plan.getNode(j).getParentId() == chnodes[0].getId())
plan.getNode(j).setParentId(chnodes[1].getId());
else if(plan.getNode(j).getParentId() == chnodes[1].getId())
plan.getNode(j).setParentId(chnodes[0].getId());
}
}
}
}
}
}