1 package com.daffodilwoods.daffodildb.server.sql99.dql.iterator.set; 2 3 4 import java.util.*; 5 import com.daffodilwoods.daffodildb.client.*; 6 import com.daffodilwoods.daffodildb.server.sql99.common.*; 7 import com.daffodilwoods.daffodildb.server.sql99.dql.execution.*; 8 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*; 9 import com.daffodilwoods.daffodildb.server.sql99.utils.*; 10 import com.daffodilwoods.daffodildb.utils.comparator.*; 11 import com.daffodilwoods.daffodildb.utils.field.*; 12 import com.daffodilwoods.database.resource.*; 13 import com.daffodilwoods.database.utility.*; 14 import com.daffodilwoods.daffodildb.utils.byteconverter.CCzufDpowfsufs; 15 16 28 29 public class IntersectAllIterator extends BaseJoinIterator { 30 33 protected _Reference[] leftColumnReferences; 34 37 protected _Reference[] rightColumnReferences; 38 41 protected _Reference[] orderLeftCD; 42 45 protected _Reference[] orderRightCD; 46 50 protected SuperComparator comparator; 51 52 84 public IntersectAllIterator(_Iterator leftIterator, _Iterator rightIterator, _Reference[] leftColumnReferences0, _Reference[] rightColumnReferences0, SuperComparator comparator0, _Reference[] orderLeftCD0, _Reference[] orderRightCD0) throws DException { 85 super(leftIterator, rightIterator); 86 leftColumnReferences = leftColumnReferences0; 87 rightColumnReferences = rightColumnReferences0; 88 comparator = comparator0; 89 orderLeftCD = orderLeftCD0; 90 orderRightCD = orderRightCD0; 91 state = INVALIDSTATE; 92 } 93 94 100 118 public boolean first() throws DException { 119 return (state = leftIterator.first() && rightIterator.first() 120 && alignForward() ? VALIDSTATE : AFTERLAST) != AFTERLAST; 121 } 122 123 141 public boolean last() throws DException { 142 return (state = leftIterator.last() && rightIterator.last() 143 && alignBackward() ? VALIDSTATE : BEFOREFIRST) != BEFOREFIRST; 144 } 145 146 172 public boolean next() throws DException { 173 switch (state) { 174 case INVALIDSTATE: 175 throw new DException("DSE4116", null); 176 case BEFOREFIRST: 177 return first(); 178 case AFTERLAST: 179 return false; 180 default: 181 if (!leftIterator.next()) { 182 return false; 183 } 184 if (!rightIterator.next()) { 185 return false; 186 } 187 return alignForward(); 188 } 189 } 190 191 217 public boolean previous() throws DException { 218 switch (state) { 219 case INVALIDSTATE: 220 throw new DException("DSE4117", null); 221 case AFTERLAST: 222 return last(); 223 case BEFOREFIRST: 224 return false; 225 default: 226 if (!leftIterator.previous()) { 227 state = BEFOREFIRST; 228 return false; 229 } 230 if (!rightIterator.previous()) { 231 state = BEFOREFIRST; 232 return false; 233 } 234 return alignBackward(); 235 } 236 } 237 238 255 protected boolean alignForward() throws DException { 256 Object leftObjectValues = leftIterator.getColumnValues(orderLeftCD); 257 Object rightObjectValues = rightIterator.getColumnValues(orderRightCD); 258 int cmp = compare(leftObjectValues, rightObjectValues); 259 while (cmp != 0) { 260 if (cmp > 0) { 261 if (!seekFromTopRelative(leftObjectValues, rightIterator, orderRightCD, true)) { 262 return false; 263 } 264 } else { 265 if (!seekFromTopRelative(rightObjectValues, leftIterator, orderLeftCD, false)) { 266 return false; 267 } 268 } 269 leftObjectValues = leftIterator.getColumnValues(orderLeftCD); 270 rightObjectValues = rightIterator.getColumnValues(orderRightCD); 271 cmp = compare(leftObjectValues, rightObjectValues); 272 } 273 return true; 274 } 275 276 289 private boolean seekFromTopRelative(Object toCompare, _Iterator iterator, _Reference[] references, boolean leftRight) throws DException { 290 while (iterator.next()) { 291 if (leftRight) { 292 if (compare(toCompare, iterator.getColumnValues(references)) <= 0) { 293 return true; 294 } 295 } else { 296 if (compare(iterator.getColumnValues(references), toCompare) >= 0) { 297 return true; 298 } 299 } 300 } 301 return false; 302 } 303 304 320 protected boolean alignBackward() throws DException { 321 Object leftObjectValues = leftIterator.getColumnValues(orderLeftCD); 322 Object rightObjectValues = rightIterator.getColumnValues(orderRightCD); 323 int cmp = compare(leftObjectValues, rightObjectValues); 324 325 while (cmp != 0) { 326 if (cmp < 0) { 327 try { 328 if (!seekFromBottomRelative(leftObjectValues, rightIterator, orderRightCD, true)) { 329 return false; 330 } 331 } catch (NullPointerException ex) { 332 throw ex; 333 } 334 } else { 335 336 if (!seekFromBottomRelative(rightObjectValues, leftIterator, orderLeftCD, false)) { 337 return false; 338 } 339 } 340 leftObjectValues = leftIterator.getColumnValues(orderLeftCD); 341 rightObjectValues = rightIterator.getColumnValues(orderRightCD); 342 cmp = compare(leftObjectValues, rightObjectValues); 343 344 } 345 return true; 346 } 347 348 361 private boolean seekFromBottomRelative(Object toCompare, _Iterator iterator, _Reference[] references, boolean leftRight) throws DException { 362 while (iterator.previous()) { 363 if (leftRight) { 364 if (compare(toCompare, iterator.getColumnValues(references)) >= 0) { 365 return true; 366 } 367 } else { 368 if (compare(iterator.getColumnValues(references), toCompare) <= 0) { 369 return true; 370 } 371 } 372 } 373 return false; 374 } 375 376 382 public Object getKey() throws DException { 383 switch (state) { 384 case INVALIDSTATE: 385 case BEFOREFIRST: 386 case AFTERLAST: 387 throw new DException("DSE4116", null); 388 default: 389 return new Object [] {leftIterator.getKey(), rightIterator.getKey()}; 390 } 391 } 392 393 400 public void move(Object kee) throws DException { 401 Object [] key = (Object []) kee; 402 leftIterator.move(key[0]); 403 rightIterator.move(key[1]); 404 } 405 406 410 419 public Object getColumnValues(_Reference[] references) throws DException { 420 int[] indexes = GeneralPurposeStaticClass.getSelectedIndexes(references, leftColumnReferences); 421 _Reference[] leftReferences = GeneralPurposeStaticClass.getReferencesToIndex(indexes, leftColumnReferences); 422 return leftIterator.getColumnValues(leftReferences); 423 } 424 425 434 public Object getColumnValues(_Reference reference) throws DException { 435 int index = GeneralPurposeStaticClass.getSelectedColumnIndex(reference, leftColumnReferences); 436 if (index == -1) { 437 return leftIterator.getColumnValues(reference); 438 } 439 _Reference leftReferences = leftColumnReferences[index]; 440 return leftIterator.getColumnValues(leftReferences); 441 } 442 443 451 public FieldBase[] fields(_Reference[] references) throws DException { 452 int[] indexes = GeneralPurposeStaticClass.getSelectedIndexes(references, leftColumnReferences); 453 _Reference[] leftReferences = GeneralPurposeStaticClass.getReferencesToIndex(indexes, leftColumnReferences); 454 return leftIterator.fields(leftReferences); 455 } 456 457 465 public FieldBase field(_Reference reference) throws DException { 466 int index = GeneralPurposeStaticClass.getSelectedColumnIndex(reference, leftColumnReferences); 467 if (index == -1) { 468 return leftIterator.field(reference); 469 } 470 _Reference leftReferences = leftColumnReferences[index]; 471 return leftIterator.field(leftReferences); 472 } 473 474 479 public _ExecutionPlan getExecutionPlan() throws DException { 480 _ExecutionPlan cplans[] = new _ExecutionPlan[2]; 481 cplans[0] = leftIterator.getExecutionPlan(); 482 cplans[1] = rightIterator.getExecutionPlan(); 483 return new ExecutionPlan("IntersectAllIterator", cplans, null, null, null); 484 } 485 486 491 public ExecutionPlanForBrowser getExecutionPlanForBrowser() throws DException { 492 ExecutionPlanForBrowser cplans[] = new ExecutionPlanForBrowser[2]; 493 cplans[0] = leftIterator.getExecutionPlanForBrowser(); 494 cplans[1] = rightIterator.getExecutionPlanForBrowser(); 495 return new ExecutionPlanForBrowser("InterSect All", "Intersect All Iterator", cplans, null, null, null); 496 } 497 498 508 protected int compare(Object object1, Object object2) throws DException { 509 int cmp = comparator.compare(object1, object2); 510 return cmp; 511 } 512 513 519 public void releaseResource() throws DException { 520 leftIterator.releaseResource(); 521 rightIterator.releaseResource(); 522 523 } 524 525 531 public _Iterator getBaseIterator(ColumnDetails column) throws DException { 532 return this; 533 } 534 535 539 545 public TableDetails[] getTableDetails() throws DException { 546 rightIterator.getTableDetails(); 547 return leftIterator.getTableDetails(); 548 } 549 550 public void setKeyCount(Object [][] tableAndKeyCount) throws DException { 551 leftIterator.setKeyCount(tableAndKeyCount); 552 } 553 554 public _KeyColumnInformation[] getKeyColumnInformations() throws DException { 555 return leftIterator.getKeyColumnInformations(); 556 } 557 558 563 public Object [] getUniqueColumnReference() throws DException { 564 return leftIterator.getUniqueColumnReference(); 565 } 566 567 public boolean seekFromTopRelative(Object parm1) throws DException { 568 return leftIterator.seekFromTopRelative(parm1) ? rightIterator.seekFromTopRelative(parm1) : false; 569 } 570 571 public boolean seekFromBottomRelative(Object parm1) throws DException { 572 return leftIterator.seekFromBottomRelative(parm1) ? rightIterator.seekFromBottomRelative(parm1) : false; 573 } 574 575 public _OrderCount getOrderCounts() throws DException { 576 rightIterator.getOrderCounts(); return (orderCount = leftIterator.getOrderCounts()); 578 } 579 580 public boolean seek(Object parm1) throws DException { 581 return GeneralPurposeStaticClass.seek(parm1, leftIterator, rightIterator); 582 } 583 584 public String toString() { 585 return " IntersectAllIterator [" + leftIterator + "] [" + rightIterator + "]"; 586 } 587 588 public byte[] getByteKey() throws DException { 589 switch(state){ 590 case INVALIDSTATE: 591 case BEFOREFIRST: 592 case AFTERLAST: 593 throw new DException("", null); 594 default : 595 byte[] leftKey = leftIterator.getByteKey(); 596 byte[] rightKey = rightIterator.getByteKey(); 597 short leftLen = (short)leftKey.length; 598 byte[] resultantKeys = new byte[leftKey.length+rightKey.length+2]; 599 System.arraycopy(CCzufDpowfsufs.getBytes(leftLen),0,resultantKeys,0,2); 600 System.arraycopy(leftKey,0,resultantKeys,2,leftKey.length); 601 System.arraycopy(rightKey,0,resultantKeys,leftKey.length+2,rightKey.length); 602 return resultantKeys; 603 } 604 } 605 606 public void moveByteKey(byte[] key) throws DException { 607 short leftLen =CCzufDpowfsufs.getShortValue(key,0); 608 byte[] leftKeys = new byte[leftLen]; 609 byte[] rightKeys = new byte[key.length-leftKeys.length-2]; 610 System.arraycopy(key,2,leftKeys,0,leftKeys.length); 611 System.arraycopy(key,leftLen+2,rightKeys,0,rightKeys.length); 612 leftIterator.moveByteKey(leftKeys); 613 rightIterator.move(rightKeys); 614 } 615 616 public void setSpecificUnderlyingReferences(_Reference[] specificUnderlyingReferences) throws DException{ 617 throw new java.lang.UnsupportedOperationException ("Method setSpecificUnderlyingReferences(_Reference[]) not yet implemented."); 618 } 619 620 621 } 622 | Popular Tags |