1 package com.daffodilwoods.daffodildb.server.sql99.dql.iterator.set; 2 3 import com.daffodilwoods.daffodildb.client.*; 4 import com.daffodilwoods.daffodildb.server.sql99.common.*; 5 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*; 6 import com.daffodilwoods.daffodildb.server.sql99.utils.*; 7 import com.daffodilwoods.database.resource.*; 8 import com.daffodilwoods.daffodildb.utils.byteconverter.CCzufDpowfsufs; 9 10 22 public class UnionAllIterator extends UnionAllOrderedIterator implements TypeConstants { 23 24 50 public UnionAllIterator(_Iterator leftIterator0, _Iterator rightIterator0, _Reference[] leftColumnReferences0, _Reference[] rightColumnReferences0, int[] appropriateDataTypes, int[] appropriateSizes, _Reference[] orderLeftCD0, _Reference[] orderRightCD0) throws DException { 51 super(leftIterator0, rightIterator0, leftColumnReferences0, rightColumnReferences0, appropriateDataTypes, appropriateSizes, orderLeftCD0, orderRightCD0); 52 state = INVALIDSTATE; 53 } 54 55 boolean flag = false; 56 57 73 public boolean first() throws com.daffodilwoods.database.resource.DException { 74 state = leftIterator.first() ? FIRSTISCURRENT 75 : rightIterator.first() ? SECONDISCURRENT : state; 76 return state != INVALIDSTATE; 77 } 78 79 80 81 82 98 public boolean last() throws com.daffodilwoods.database.resource.DException { 99 return (state = rightIterator.last() ? SECONDISCURRENT : leftIterator.last() ? FIRSTISCURRENT : state) != INVALIDSTATE; 100 } 101 102 127 public boolean next() throws com.daffodilwoods.database.resource.DException { 128 switch(state) { 129 case INVALIDSTATE : 130 throw new DException("DSE4116",null); 131 case BEFOREFIRST : 132 return first(); 133 case AFTERLAST : 134 return false; 135 } 136 state = state == FIRSTISCURRENT ? leftIterator.next() ? state 137 : rightIterator.first() ? SECONDISCURRENT 138 : AFTERLAST 139 : state == SECONDISCURRENT 140 ? rightIterator.next() ? state 141 : AFTERLAST 142 : state; 143 return state != AFTERLAST; 144 } 145 146 171 public boolean previous() throws com.daffodilwoods.database.resource.DException { 172 switch(state){ 173 case INVALIDSTATE : 174 throw new DException("DSE4117",null); 175 case BEFOREFIRST : 176 return false; 177 case AFTERLAST : 178 return last(); 179 } 180 if(state == SECONDISCURRENT){ 181 state = rightIterator.previous() ? state 182 : leftIterator.last() ? FIRSTISCURRENT : BEFOREFIRST; 183 } 184 else if(state == FIRSTISCURRENT) 185 state = leftIterator.previous() ? state : BEFOREFIRST; 186 return state != BEFOREFIRST; 187 } 188 189 197 public Object getKey() throws DException { 198 switch(state) { 199 case INVALIDSTATE : 200 case BEFOREFIRST : 201 case AFTERLAST : 202 throw new DException("DSE4116",null); 203 case FIRSTISCURRENT : 204 return new Object []{new SetOperatorKey(new Object [] {leftIterator.getKey(),null},state,direction)}; 205 case SECONDISCURRENT : 206 return new Object []{new SetOperatorKey(new Object [] {null,rightIterator.getKey()},state,direction)}; 207 } 208 new Exception (" CHECK THE CASE :: AKASH ").printStackTrace(); 209 return null; 210 } 211 212 219 public void move(Object keys) throws DException { 220 SetOperatorKey setKey = (SetOperatorKey)((Object [])keys)[0]; 221 state = setKey.getState(); 222 Object [] kees = setKey.getKeys(); 223 Object leftKeys = kees[0]; 224 Object rightKeys = kees[1]; 225 if(leftKeys != null){ 226 leftIterator.move(leftKeys); 227 } 228 if(rightKeys != null){ 229 rightIterator.move(rightKeys); 230 } 231 } 232 233 239 public _Iterator getBaseIterator(ColumnDetails column) throws com.daffodilwoods.database.resource.DException { 240 return this; 241 } 242 243 248 public _ExecutionPlan getExecutionPlan() throws DException{ 249 _ExecutionPlan cplans[] = new _ExecutionPlan[2]; 250 cplans[0] = leftIterator.getExecutionPlan(); 251 cplans[1] = rightIterator.getExecutionPlan(); 252 return new ExecutionPlan("UnionAllIterator",cplans,null,null,null); 253 } 254 255 260 public ExecutionPlanForBrowser getExecutionPlanForBrowser() throws DException { 261 ExecutionPlanForBrowser cplans[] = new ExecutionPlanForBrowser[2]; 262 cplans[0] = leftIterator.getExecutionPlanForBrowser(); 263 cplans[1] = rightIterator.getExecutionPlanForBrowser(); 264 return new ExecutionPlanForBrowser("Union All","Union All Iterator",cplans,null,null,null); 265 } 266 267 272 273 public byte[] getByteKey() throws DException { 274 byte[] resultantKeys ; 275 switch(state) { 276 case INVALIDSTATE : 277 case BEFOREFIRST : 278 case AFTERLAST : 279 throw new DException("DSE4116",null); 280 case FIRSTISCURRENT : 281 return getKeysInUnion(true,leftIterator,state,direction); 282 case SECONDISCURRENT : 283 return getKeysInUnion(false,rightIterator,state,direction); 284 } 285 return null; 286 } 287 288 public void moveByteKey(byte[] key) throws DException { 289 state = key[0]; 290 direction = key[1]; 291 short leftLen = CCzufDpowfsufs.getShortValue(key,2); 292 byte[] leftKeys = new byte[leftLen]; 293 short rightLen =CCzufDpowfsufs.getShortValue(key,4+leftLen); 294 byte[] rightKeys = new byte[rightLen]; 295 if(leftKeys.length != 0){ 296 System.arraycopy(key,4,leftKeys,0,leftKeys.length); 297 leftIterator.moveByteKey(leftKeys); 298 } 299 else if(rightKeys.length != 0){ 300 System.arraycopy(key ,6+leftKeys.length,rightKeys,0,rightKeys.length); 301 rightIterator.moveByteKey(rightKeys); 302 } 303 } 304 305 private byte[] getKeysInUnion(boolean firstCurrent,_Iterator iter,int state,int direction) throws DException{ 306 byte[] keys = iter.getByteKey(); 307 byte[] resultantKeys = new byte[keys.length+2+2+2]; 308 resultantKeys[0] =(byte) state; 309 resultantKeys[1] =(byte) direction; 310 if(firstCurrent){ 311 System.arraycopy(CCzufDpowfsufs.getBytes((short)keys.length),0,resultantKeys,2,2); 312 System.arraycopy(keys,0,resultantKeys,4,keys.length); 313 System.arraycopy(CCzufDpowfsufs.getBytes((short)0),0,resultantKeys,keys.length+4,2); 314 } 315 else{ 316 System.arraycopy(CCzufDpowfsufs.getBytes((short)0),0,resultantKeys,2,2); 317 System.arraycopy(CCzufDpowfsufs.getBytes((short)keys.length),0,resultantKeys,4,2); 318 System.arraycopy(keys,0,resultantKeys,6,keys.length); 319 } 320 return resultantKeys; 321 } 322 323 public String toString() { 324 return "UnionAllIterator [" + leftIterator + "] [" + rightIterator + "] "; 325 } 326 } 327 | Popular Tags |