1 5 package org.h2.table; 6 7 import java.sql.SQLException ; 8 import java.util.HashMap ; 9 10 import org.h2.engine.Session; 11 import org.h2.expression.Expression; 12 import org.h2.expression.ExpressionVisitor; 13 import org.h2.util.ObjectArray; 14 15 18 public class Plan { 19 private TableFilter[] filters; 20 private HashMap planItems = new HashMap (); 21 private Expression[] allConditions; 22 private TableFilter[] allFilters; 23 24 public Plan(TableFilter[] filters, int count, Expression condition) { 25 this.filters = new TableFilter[count]; 26 System.arraycopy(filters, 0, this.filters, 0, count); 27 ObjectArray allCond = new ObjectArray(); 28 ObjectArray allFilt= new ObjectArray(); 29 if(condition != null) { 30 allCond.add(condition); 31 } 32 for(int i=0; i<count; i++) { 33 TableFilter f = filters[i]; 34 do { 35 allFilt.add(f); 36 if(f.getJoinCondition() != null) { 37 allCond.add(f.getJoinCondition()); 38 } 39 f = f.getJoin(); 40 } while(f != null); 41 } 42 allConditions = new Expression[allCond.size()]; 43 allCond.toArray(allConditions); 44 allFilters = new TableFilter[allFilt.size()]; 45 allFilt.toArray(allFilters); 46 } 47 48 public PlanItem getItem(TableFilter filter) { 49 return (PlanItem) planItems.get(filter); 50 } 51 52 public TableFilter[] getFilters() { 53 return filters; 54 } 55 56 public void removeUnusableIndexConditions() { 57 for(int i=0; i<allFilters.length; i++) { 58 TableFilter f = allFilters[i]; 59 setEvaluatable(f, true); 60 if(i < allFilters.length - 1) { 61 f.optimizeFullCondition(false); 65 } 66 f.removeUnusableIndexConditions(); 67 } 68 for(int i=0; i<allFilters.length; i++) { 69 TableFilter f = allFilters[i]; 70 setEvaluatable(f, false); 71 } 72 } 73 74 public double calculateCost(Session session) throws SQLException { 75 double cost = 1; 76 boolean invalidPlan = false; 77 for (int i = 0; i < allFilters.length; i++) { 78 TableFilter tableFilter = allFilters[i]; 79 PlanItem item = tableFilter.getBestPlanItem(session); 80 planItems.put(tableFilter, item); 81 cost += cost * item.cost; 82 setEvaluatable(tableFilter, true); 83 Expression on = tableFilter.getJoinCondition(); 84 if(on != null) { 85 if(!on.isEverything(ExpressionVisitor.EVALUATABLE)) { 86 invalidPlan = true; 87 break; 88 } 89 } 90 } 91 if(invalidPlan) { 92 cost = 1./0.; } 94 for (int i = 0; i < allFilters.length; i++) { 95 setEvaluatable(allFilters[i], false); 96 } 97 return cost; 98 } 99 100 private void setEvaluatable(TableFilter filter, boolean b) { 101 for(int j=0; j<allConditions.length; j++) { 102 allConditions[j].setEvaluatable(filter, b); 103 } 104 } 105 } 106 | Popular Tags |