1 23 24 27 28 package org.objectweb.medor.eval.lib; 29 30 import org.objectweb.medor.api.MedorException; 31 import org.objectweb.medor.api.TupleStructure; 32 import org.objectweb.medor.eval.api.NodeEvaluator; 33 import org.objectweb.medor.expression.api.ParameterOperand; 34 import org.objectweb.medor.expression.api.VariableOperand; 35 import org.objectweb.medor.query.api.NestedField; 36 import org.objectweb.medor.query.api.PropagatedField; 37 import org.objectweb.medor.query.api.QueryTree; 38 import org.objectweb.medor.query.api.UnnestQueryNode; 39 import org.objectweb.medor.tuple.api.Tuple; 40 import org.objectweb.medor.tuple.api.TupleCollection; 41 import org.objectweb.medor.tuple.lib.MemoryTuple; 42 43 48 public class UnnestEvaluatedTC implements TupleCollection { 49 private TupleStructure tcStructure; 50 private ParameterOperand[] parameters; 51 private int sizeOfResult, sizeOfNotUnnestedFields, nestedFieldRank; 52 private NestedField unnestField; 53 private VariableOperand[] currentAttBuffers; 54 private Tuple nestedCurrentTuple, currentBuffer; 55 private TupleCollection nestedFieldTC, unnestedTupleCollection; 56 private int cursor; 57 private int[] indexes; 58 private boolean closed = false; 59 60 public UnnestEvaluatedTC(UnnestQueryNode unnestQueryTree, 61 NodeEvaluator subNodeEvaluator, 62 ParameterOperand[] parameters) 63 throws MedorException { 64 this.tcStructure = unnestQueryTree.getTupleStructure(); 65 this.parameters = parameters; 66 67 unnestField = unnestQueryTree.getUnnestedField(); 69 70 unnestedTupleCollection = subNodeEvaluator.fetchData(parameters); 72 73 sizeOfResult = unnestQueryTree.getTupleStructure().getSize(); 75 76 sizeOfNotUnnestedFields = unnestQueryTree.getProjectedFields().length; 79 80 86 indexes = new int[sizeOfNotUnnestedFields]; 87 QueryTree[] childs = unnestQueryTree.getChildren(); 88 PropagatedField[] notNestedFields = 89 unnestQueryTree.getProjectedFields(); 90 91 for (int cpt = 0; (cpt < sizeOfNotUnnestedFields); cpt++) { 92 indexes[cpt] = 93 childs[0].getTupleStructure().getFieldRank( 94 notNestedFields[cpt].getPreviousFields()[0]); 95 } 96 nestedFieldRank = 98 childs[0].getTupleStructure().getFieldRank(unnestField); 99 currentAttBuffers = new VariableOperand[sizeOfResult]; 100 currentBuffer = new MemoryTuple(currentAttBuffers); 101 init(); 102 } 103 104 private void init() throws MedorException { 105 cursor = 1; 106 if (!isEmpty()) { 107 nestedCurrentTuple = unnestedTupleCollection.getTuple(); 108 nestedFieldTC = nestedCurrentTuple.getTupleCollection( 109 indexes[nestedFieldRank]); 110 for (int cpt = 0; (cpt < sizeOfNotUnnestedFields); cpt++) { 111 currentAttBuffers[cpt] = 112 (VariableOperand) nestedCurrentTuple.getLikeOperand( 113 indexes[cpt]); 114 } 115 if (!(nestedFieldTC.isEmpty())) { 116 for (int cpt = sizeOfNotUnnestedFields; 117 (cpt < sizeOfResult); 118 cpt++) { 119 currentAttBuffers[cpt] = 120 (VariableOperand) 121 nestedFieldTC.getTuple().getLikeOperand( 122 cpt - sizeOfNotUnnestedFields + 1); 123 } } 125 } 126 } 127 128 public void close() throws MedorException { 129 closed = true; 130 if (nestedFieldTC != null) { 131 nestedFieldTC.close(); 132 } 133 if (unnestedTupleCollection != null) { 134 unnestedTupleCollection.close(); 135 } 136 } 137 138 141 public TupleStructure getMetaData() throws MedorException { 142 return tcStructure; 143 } 144 145 public boolean isLast() throws MedorException { 146 return ((unnestedTupleCollection.isLast()) && (nestedFieldTC.isLast())); 147 } 148 149 public boolean next() throws MedorException { 150 if (nestedFieldTC.next()) { 151 for (int cpt = sizeOfNotUnnestedFields; 152 (cpt < sizeOfResult); 153 cpt++) { 154 currentAttBuffers[cpt] = 155 (VariableOperand) nestedFieldTC.getTuple().getLikeOperand( 156 cpt - sizeOfNotUnnestedFields + 1); 157 } 158 cursor++; 159 return true; 160 } 161 else { 162 if (unnestedTupleCollection.next()) { 163 nestedCurrentTuple = unnestedTupleCollection.getTuple(); 164 nestedFieldTC = 165 nestedCurrentTuple.getTupleCollection(nestedFieldRank); 166 for (int cpt = 0; (cpt < sizeOfNotUnnestedFields); cpt++) { 167 currentAttBuffers[cpt] = 168 (VariableOperand) nestedCurrentTuple.getLikeOperand( 169 indexes[cpt]); 170 } 171 172 if (!(nestedFieldTC.isEmpty())) { 174 for (int cpt = sizeOfNotUnnestedFields; 175 (cpt < sizeOfResult); 176 cpt++) { 177 currentAttBuffers[cpt] = 178 (VariableOperand) 179 nestedFieldTC.getTuple().getLikeOperand( 180 cpt - sizeOfNotUnnestedFields + 1); 181 } 182 } 183 cursor++; 184 return true; 185 } 186 else 187 return false; 188 } 189 } 190 191 public void first() throws MedorException { 192 init(); 193 } 194 195 public int getRow() throws MedorException { 196 return cursor; 197 } 198 199 public Tuple getTuple() throws MedorException { 200 return currentBuffer; 201 } 202 203 public synchronized Tuple getTuple(int numTuple) throws MedorException { 204 return getTuple(); 205 } 206 207 public boolean row(int row) throws MedorException { 208 return false; 209 210 } 211 212 public int card() throws MedorException { 213 return 0; 214 } 215 216 public boolean isEmpty() throws MedorException { 217 return unnestedTupleCollection.isEmpty(); 218 } 219 } 220 | Popular Tags |