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.execution.*; 6 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*; 7 import com.daffodilwoods.daffodildb.server.sql99.utils.*; 8 import com.daffodilwoods.daffodildb.utils.comparator.*; 9 import com.daffodilwoods.daffodildb.utils.field.*; 10 import com.daffodilwoods.database.resource.*; 11 import com.daffodilwoods.daffodildb.utils.byteconverter.CCzufDpowfsufs; 12 13 27 28 public class ExceptAllIterator extends BaseJoinIterator { 29 30 31 35 protected boolean checkInSecondIterator; 36 37 40 protected _Reference[] leftColumnReferences; 41 42 45 protected _Reference[] rightColumnReferences; 47 50 protected _Reference[] orderLeftCD; 51 54 protected _Reference[] orderRightCD; 55 56 59 63 protected SuperComparator comparator; 64 68 protected SuperComparator leftComparator; 69 73 protected SuperComparator rightComparator; 74 76 77 78 100 public ExceptAllIterator(_Iterator leftIterator, _Iterator rightIterator, _Reference[] leftColumnReferences0, _Reference[] rightColumnReferences0, SuperComparator comparator0, _Reference[] orderLeftCD0, _Reference[] orderRightCD0, SuperComparator leftComparator0, SuperComparator rightComparator0) throws DException { 101 super(leftIterator, rightIterator); 102 leftColumnReferences = leftColumnReferences0; 103 rightColumnReferences = rightColumnReferences0; 104 comparator = comparator0; 105 checkInSecondIterator = true; 106 orderLeftCD = orderLeftCD0; 107 orderRightCD = orderRightCD0; 108 leftComparator = leftComparator0; 109 rightComparator = rightComparator0; 110 } 111 112 147 public boolean first() throws com.daffodilwoods.database.resource.DException { 148 checkInSecondIterator = true; 149 if (!leftIterator.first()) { 150 state = AFTERLAST; 151 return false; 152 } 153 if (!rightIterator.first()) { 154 checkInSecondIterator = false; 155 state = VALIDSTATE; 156 return true; 157 } 158 return (state = alignNext() ? VALIDSTATE : AFTERLAST) != AFTERLAST; 159 } 160 195 public boolean last() throws com.daffodilwoods.database.resource.DException { 196 checkInSecondIterator = true; 197 if (!leftIterator.last()) { 198 state = BEFOREFIRST; 199 return false; 200 } 201 if (!rightIterator.last()) { 202 checkInSecondIterator = false; 203 state = VALIDSTATE; 204 return true; 205 } 206 return (state = alignPrevious() ? VALIDSTATE : BEFOREFIRST) != BEFOREFIRST; 207 } 208 244 public boolean next() throws com.daffodilwoods.database.resource.DException { 245 if (state == INVALIDSTATE) { 246 throw new DException("DSE4116", null); 247 } 248 if (state == BEFOREFIRST) { 249 return first(); 250 } 251 if (!leftIterator.next()) { 252 state = AFTERLAST; 253 return false; 254 } 255 return (state = alignNext() ? state : AFTERLAST) != AFTERLAST; 256 } 257 258 294 public boolean previous() throws com.daffodilwoods.database.resource.DException { 295 if (state == INVALIDSTATE) 296 throw new DException("DSE4117", null); 297 if (state == AFTERLAST) 298 return last(); 299 if (!leftIterator.previous()) { 300 state = BEFOREFIRST; 301 return false; 302 } 303 return (state = alignPrevious() ? state : BEFOREFIRST) != BEFOREFIRST; 304 } 305 306 317 boolean moveForwardDistinct(_Iterator iterator, Object value, _Reference[] references, boolean left) throws DException { 318 while (iterator.next()) { 319 if (compare(iterator.getColumnValues(references), value, left) != 0) { 320 return true; 321 } 322 } 323 return false; 324 } 325 326 337 338 boolean moveBackwardDistinct(_Iterator iterator, Object value, _Reference[] references, boolean left) throws DException { 339 while (iterator.previous()) { 340 if (compare(iterator.getColumnValues(references), value, left) != 0) { 341 return true; 342 } 343 } 344 return false; 345 } 346 347 355 boolean moveForward(_Iterator iterator, Object value, _Reference[] references) throws DException { 356 return iterator.next(); 357 } 358 359 367 boolean moveBackward(_Iterator iterator, Object value, _Reference[] references) throws DException { 368 return iterator.previous(); 369 } 370 371 394 boolean alignNext() throws DException { 395 if (!checkInSecondIterator && !rightIterator.next()) { 396 return true; 397 } while (true) { 398 Object leftObjectValues = leftIterator.getColumnValues(orderLeftCD); 399 Object rightObjectValues = rightIterator.getColumnValues(orderRightCD); 400 int cmp = compare(leftObjectValues, rightObjectValues); 401 if (cmp == 0) { 402 if (!moveForward(leftIterator, leftObjectValues, orderLeftCD)) { 403 return false; 404 } 405 if (!moveForward(rightIterator, rightObjectValues, orderRightCD)) { 406 checkInSecondIterator = false; 407 return true; 408 } 409 } else if (cmp > 0) { 410 if (!moveForwardDistinct(rightIterator, rightObjectValues, orderRightCD, false)) { 411 checkInSecondIterator = false; 412 return true; 413 } 414 } else { 415 checkInSecondIterator = true; 416 return true; 417 } 418 419 } 420 } 421 422 443 boolean alignPrevious() throws DException { 444 if (!checkInSecondIterator && !rightIterator.previous()) { 445 return true; 446 } while (true) { 447 Object leftObjectValues = leftIterator.getColumnValues(orderLeftCD); 448 Object rightObjectValues = rightIterator.getColumnValues(orderRightCD); 449 int cmp = compare(leftObjectValues, rightObjectValues); 450 if (cmp == 0) { 451 if (!moveBackward(leftIterator, leftObjectValues, orderLeftCD)) { 452 return false; 453 } 454 if (!moveBackward(rightIterator, rightObjectValues, orderRightCD)) { 455 checkInSecondIterator = false; 456 return true; 457 } 458 } else if (cmp < 0) { 459 if (!moveBackwardDistinct(rightIterator, rightObjectValues, orderRightCD, false)) { 460 checkInSecondIterator = false; 461 return true; 462 } 463 } else { 464 checkInSecondIterator = true; 465 return true; 466 } 467 } 468 } 469 470 480 int compare(Object object1, Object object2) throws DException{ 481 int cmp = comparator.compare(object1,object2); 482 return cmp; 483 } 484 485 486 499 int compare(Object object1, Object object2, boolean left) throws DException { 500 int cmp = left ? leftComparator.compare(object1, object2) : rightComparator.compare(object1, object2); 501 return cmp; 502 } 503 504 511 public Object getKey() throws DException { 512 if (state == INVALIDSTATE || state == BEFOREFIRST || state == AFTERLAST) 513 throw new DException("DSE4116", null); 514 return checkInSecondIterator ? new Object [] { 515 leftIterator.getKey(), rightIterator.getKey()} 516 : new Object [] { 517 leftIterator.getKey(), null}; 518 } 519 520 529 public void move(Object kee) throws DException { 530 Object [] key = (Object []) kee; 531 leftIterator.move(key[0]); 532 if (key[1] != null) { 533 rightIterator.move(key[1]); 534 checkInSecondIterator = true; 535 } 536 else 537 checkInSecondIterator = false; 538 } 539 540 544 553 public Object getColumnValues(_Reference[] references) throws DException { 554 int[] indexes = GeneralPurposeStaticClass.getSelectedIndexes(references, leftColumnReferences); 555 _Reference[] leftReferences = GeneralPurposeStaticClass.getReferencesToIndex(indexes, leftColumnReferences); 556 return leftIterator.getColumnValues(leftReferences); 557 } 558 559 568 public Object getColumnValues(_Reference reference) throws DException { 569 int index = GeneralPurposeStaticClass.getSelectedColumnIndex(reference, leftColumnReferences); 570 if (index == -1) { 571 return leftIterator.getColumnValues(reference); 572 } 573 _Reference leftReferences = leftColumnReferences[index]; 574 return leftIterator.getColumnValues(leftReferences); 575 } 576 577 585 public FieldBase[] fields(_Reference[] references) throws DException { 586 int[] indexes = GeneralPurposeStaticClass.getSelectedIndexes(references, leftColumnReferences); 587 _Reference[] leftReferences = GeneralPurposeStaticClass.getReferencesToIndex(indexes, leftColumnReferences); 588 return leftIterator.fields(leftReferences); 589 } 590 591 599 public FieldBase field(_Reference reference) throws DException { 600 int index = GeneralPurposeStaticClass.getSelectedColumnIndex(reference, leftColumnReferences); 601 if (index == -1) { 602 return leftIterator.field(reference); 603 } 604 _Reference leftReferences = leftColumnReferences[index]; 605 return leftIterator.field(leftReferences); 606 } 607 608 613 public _ExecutionPlan getExecutionPlan() throws DException { 614 _ExecutionPlan cplans[] = new _ExecutionPlan[2]; 615 cplans[0] = leftIterator.getExecutionPlan(); 616 cplans[1] = rightIterator.getExecutionPlan(); 617 return new ExecutionPlan("ExceptAllIterator", cplans, null, null, null); 618 } 619 620 625 public ExecutionPlanForBrowser getExecutionPlanForBrowser() throws DException { 626 ExecutionPlanForBrowser cplans[] = new ExecutionPlanForBrowser[2]; 627 cplans[0] = leftIterator.getExecutionPlanForBrowser(); 628 cplans[1] = rightIterator.getExecutionPlanForBrowser(); 629 return new ExecutionPlanForBrowser("Except All", "ExceptAllIterator", cplans, null, null, null); 630 } 631 632 638 public void releaseResource() throws DException { 639 leftIterator.releaseResource(); 640 rightIterator.releaseResource(); 641 642 } 643 644 650 public _Iterator getBaseIterator(ColumnDetails column) throws com.daffodilwoods.database.resource.DException { 651 return this; 652 } 653 654 658 664 public TableDetails[] getTableDetails() throws com.daffodilwoods.database. 665 resource.DException { 666 rightIterator.getTableDetails(); 667 return leftIterator.getTableDetails(); 668 } 669 670 public _KeyColumnInformation[] getKeyColumnInformation() throws com. 671 daffodilwoods.database.resource.DException { 672 return leftIterator.getKeyColumnInformations(); 673 } 674 675 public _KeyColumnInformation[] getKeyColumnInformations() throws DException { 676 return leftIterator.getKeyColumnInformations(); 677 } 678 679 684 685 public Object [] getUniqueColumnReference() throws DException { 686 return leftIterator.getUniqueColumnReference(); 687 } 688 689 public boolean seek(Object indexKey) throws DException { 690 return GeneralPurposeStaticClass.seek(indexKey, leftIterator, rightIterator); 691 } 692 693 public _OrderCount getOrderCounts() throws com.daffodilwoods.database. 694 resource.DException { 695 rightIterator.getOrderCounts(); return (orderCount = leftIterator.getOrderCounts()); 697 } 698 699 public byte[] getByteKey() throws DException { 700 if (state == INVALIDSTATE || state == BEFOREFIRST || state == AFTERLAST) 701 throw new DException("DSE4116", null); 702 return checkInSecondIterator ? getResultantByteKeys(true) : getResultantByteKeys(false); 703 704 } 705 706 public void moveByteKey(byte[] key) throws DException { 707 short leftLen = CCzufDpowfsufs.getShortValue(key,0); 708 byte[] leftKeys = new byte[leftLen]; 709 System.arraycopy(key,2,leftKeys,0,leftKeys.length); 710 leftIterator.moveByteKey(leftKeys); 711 if(key[leftKeys.length+1]!=0){ 712 short rightLen = CCzufDpowfsufs.getShortValue(key,leftKeys.length+2); 713 byte[] rightKeys = new byte[rightLen]; System.arraycopy(key,leftKeys.length+4,rightKeys,0,rightKeys.length); 715 rightIterator.moveByteKey(rightKeys); 716 checkInSecondIterator = true; 717 } 718 else 719 checkInSecondIterator = false; 720 } 721 722 private byte[] getResultantByteKeys(boolean flag) throws DException { 723 byte[] leftKeys = leftIterator.getByteKey(); 724 byte[] rightKeys = rightIterator.getByteKey(); 725 byte[] resultantKeys; 726 if (flag) { 727 resultantKeys = new byte[leftKeys.length + rightKeys.length + 4]; 728 short leftLen = (short)leftKeys.length; 729 short rightLen = (short)rightKeys.length; 730 System.arraycopy(CCzufDpowfsufs.getBytes(leftLen),0 ,resultantKeys,0,2); 731 System.arraycopy(leftKeys,0,resultantKeys,2,leftLen); 732 System.arraycopy(CCzufDpowfsufs.getBytes(rightLen),0,resultantKeys,2+leftLen,2); 733 System.arraycopy(rightKeys,0,resultantKeys,leftKeys.length+4,rightKeys.length); 734 } 735 else{ 736 resultantKeys = new byte[leftKeys.length + 4]; 737 short leftLen =(short)leftKeys.length; 738 System.arraycopy(CCzufDpowfsufs.getBytes(leftLen),0,resultantKeys,0,2); 739 System.arraycopy(leftKeys, 0, resultantKeys, 2, leftKeys.length); 740 System.arraycopy(CCzufDpowfsufs.getBytes((short)0),0,resultantKeys,leftLen+2,2); 741 resultantKeys[leftKeys.length + 1] = 0; 742 } 743 return resultantKeys; 744 } 745 746 public void setSpecificUnderlyingReferences(_Reference[] specificUnderlyingReferences) throws DException{ 747 throw new java.lang.UnsupportedOperationException ("Method setSpecificUnderlyingReferences(_Reference[]) not yet implemented."); 748 } 749 750 public String toString() { 751 return "ExceptAllIterator [" + leftIterator + "] [" + rightIterator + "]"; 752 } 753 } 754 | Popular Tags |