1 package com.daffodilwoods.daffodildb.server.sql99.dql.iterator.table; 2 3 import java.util.*; 4 5 import com.daffodilwoods.daffodildb.client.*; 6 import com.daffodilwoods.daffodildb.server.serversystem.*; 7 import com.daffodilwoods.daffodildb.server.sql99.common.*; 8 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*; 9 import com.daffodilwoods.daffodildb.server.sql99.dql.listenerevents.*; 10 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*; 11 import com.daffodilwoods.daffodildb.server.sql99.utils.*; 12 import com.daffodilwoods.daffodildb.utils.*; 13 import com.daffodilwoods.daffodildb.utils.field.*; 14 import com.daffodilwoods.database.resource.*; 15 import com.daffodilwoods.daffodildb.utils.byteconverter.CCzufDpowfsufs; 16 import com.daffodilwoods.database.utility.P; 17 18 38 public class SemiJoinFilteredIterator extends AbstractQualifiedJoinIterator implements _HasRecordIterator { 39 42 protected ConditionVariableValue conditionVariableValue; 43 46 protected int rightCount = -1; 48 59 public SemiJoinFilteredIterator(_Iterator leftIterator0, _Iterator rightIterator0, booleanvalueexpression condition, _KeyColumnInformation[] leftKeyColumnInformation0, _KeyColumnInformation[] rightKeyColumnInformation0, ColumnDetails[] hasRecordReferences0, _ServerSession session) throws DException { 60 super(leftIterator0, rightIterator0, leftKeyColumnInformation0, rightKeyColumnInformation0, hasRecordReferences0); 61 leftIterator = leftIterator0; 62 _VariableValues vValues = GeneralPurposeStaticClass.getNewVariableValues(condition, getTableDetails(), session); 63 vValues.setIterator(this); 64 conditionVariableValue = new ConditionVariableValue(vValues, condition); 65 rightCount = rightKeyColumnInformation0.length; 66 } 67 68 98 99 public boolean first() throws DException { 100 rightNull = false; 101 if (!leftIterator.first()) { 102 state = AFTERLAST; 103 return false; 104 } 105 if (rightIterator.first()) { 106 if (ifConditionSolvedForward()) { 107 return true; 108 } 109 } 110 rightNull = true; 111 state = VALIDSTATE; 112 return true; 113 } 114 115 boolean result; 116 117 165 public boolean next() throws DException { 166 switch (state) { 167 case INVALIDSTATE: 168 throw new DException("DSE4116", null); 169 case BEFOREFIRST: 170 result = first(); 171 return result; 172 case AFTERLAST: 173 result = false; 174 return false; 175 default: { 176 if (rightNull) { 177 result = checkInBothForward(); 178 return result; 179 } else { 180 if (!rightIterator.next()) { 181 result = checkInBothForward(); 182 return result; 183 } else { 184 if (ifConditionSolvedForward()) { 185 result = true; 186 return result; 187 } 188 result = checkInBothForward(); 189 return result; 190 } 191 } 192 } 193 } 194 } 195 196 228 public boolean last() throws DException { 229 rightNull = false; 230 if (!leftIterator.last()) { 231 state = BEFOREFIRST; 232 return false; 233 } 234 if (rightIterator.last()) { 235 if (ifConditionSolvedBackward()) { 236 return true; 237 } 238 } 239 rightNull = true; 240 state = VALIDSTATE; 241 return true; 242 } 243 244 292 public boolean previous() throws DException { 293 switch (state) { 294 case INVALIDSTATE: 295 throw new DException("DSE4117", null); 296 case BEFOREFIRST: 297 result = false; 298 return false; 299 case AFTERLAST: 300 result = last(); 301 return result; 302 default: { 303 if (rightNull) { 304 result = checkInBothBackward(); 305 return result; 306 } else { 307 if (!rightIterator.previous()) { 308 result = checkInBothBackward(); 309 return result; 310 } else { 311 if (ifConditionSolvedBackward()) { 312 result = true; 313 return result; 314 } 315 result = checkInBothBackward(); 316 return result; 317 } 318 } 319 } 320 } 321 } 322 323 332 private boolean ifConditionSolvedForward() throws DException { 333 do { 334 if (conditionVariableValue.run().hashCode() == 0) { 335 state = VALIDSTATE; 336 rightNull = false; 337 return true; 338 } 339 } while (rightIterator.next()); 340 return false; 341 } 342 343 352 private boolean ifConditionSolvedBackward() throws DException { 353 do { 354 if (conditionVariableValue.run().hashCode() == 0) { 355 state = VALIDSTATE; 356 return true; 357 } 358 } while (rightIterator.previous()); 359 return false; 360 } 361 362 375 private boolean checkInBothForward() throws DException { 376 if (!leftIterator.next()) { 377 state = AFTERLAST; 378 return false; 379 } 380 rightNull = false; 381 if (rightIterator.first()) { 382 if (ifConditionSolvedForward()) { 383 return true; 384 } 385 } 386 rightNull = true; 387 state = VALIDSTATE; 388 return true; 389 } 390 391 404 private boolean checkInBothBackward() throws DException { 405 if (!leftIterator.previous()) { 406 state = BEFOREFIRST; 407 return false; 408 } 409 rightNull = false; 410 if (rightIterator.last()) { 411 if (ifConditionSolvedBackward()) { 412 return true; 413 } 414 } 415 rightNull = true; 416 state = VALIDSTATE; 417 return true; 418 } 419 420 421 428 public Object getKey() throws com.daffodilwoods.database.resource.DException { 429 ArrayList list = new ArrayList(); 430 Object [] leftKeys = (Object []) leftIterator.getKey(); 431 list.addAll(java.util.Arrays.asList(leftKeys)); 432 if (rightNull) { 433 for (int i = 0; i < rightCount; i++) { 434 list.add(null); 435 } 436 } else { 437 Object [] rightKeys = (Object []) rightIterator.getKey(); 438 list.addAll(java.util.Arrays.asList(rightKeys)); 439 } 440 return list.toArray(); 441 } 442 443 454 private void moveAcctoGroupKey(Object [] keys) throws DException { 455 if (leftKeyCount == 0) { 456 leftIterator.first(); 457 } else { 458 Object [] leftKeys = new Object [leftKeyCount]; 459 System.arraycopy(keys, 0, leftKeys, 0, leftKeyCount); 460 leftIterator.move(leftKeys); 461 } 462 if (rightKeyCount == 0) { 463 rightNull = !rightIterator.first(); 464 } else { 465 int rightLength = keys.length - leftKeyCount; 466 Object [] rightKeys = new Object [rightLength]; 467 System.arraycopy(keys, leftKeyCount, rightKeys, 0, rightLength); 468 if (isKeyNull(rightKeys)) { 469 rightNull = true; 470 } else { 471 rightNull = false; 472 rightIterator.move(rightKeys); 473 } 474 } 475 state = 0; 476 } 477 478 479 486 public byte[] getByteKey() throws DException { 487 byte[] leftKeys = leftIterator.getByteKey(); 488 if (rightNull) { 489 byte[] result = new byte[leftKeys.length + 4]; 490 short leftLen = (short)leftKeys.length; 491 System.arraycopy(CCzufDpowfsufs.getBytes(leftLen),0,result,0,2); 492 System.arraycopy(leftKeys, 0, result, 2, leftKeys.length); 493 System.arraycopy(CCzufDpowfsufs.getBytes((short)-1),0,result,leftLen+2,2); 494 return result; 495 } 496 else{ 497 byte[] rightKeys = rightIterator.getByteKey(); 498 byte[] resultantKeys = new byte[leftKeys.length + rightKeys.length + 4]; 499 short leftLen = (short)leftKeys.length; 500 System.arraycopy(CCzufDpowfsufs.getBytes(leftLen),0,resultantKeys,0,2); 501 System.arraycopy(leftKeys, 0, resultantKeys, 2, leftKeys.length); 502 short rightLen = (short)rightKeys.length; 503 System.arraycopy(CCzufDpowfsufs.getBytes(rightLen),0,resultantKeys,leftLen+2,2); 504 System.arraycopy(rightKeys, 0, resultantKeys, leftKeys.length + 4, 505 rightKeys.length); 506 return resultantKeys; 507 } 508 } 509 510 523 public void move(Object keys) throws com.daffodilwoods.database.resource.DException { 524 if (leftKeyCount != 0 || rightKeyCount != 0) { 525 moveAcctoGroupKey( (Object []) keys); 526 return; 527 } 528 Object [] rightKeys = new Object [rightCount]; 529 int leftCount = ( (Object []) keys).length - rightCount; 530 Object [] leftKeys = new Object [leftCount]; 531 System.arraycopy(keys, 0, leftKeys, 0, leftCount); 532 System.arraycopy(keys, leftCount, rightKeys, 0, rightCount); 533 leftIterator.move(leftKeys); 534 if (isKeyNull(rightKeys)) { 535 rightNull = true; 536 } else { 537 rightNull = false; 538 rightIterator.move(rightKeys); 539 } 540 state = 0; 541 } 542 543 544 552 561 public Object getColumnValues(_Reference[] columnReferences) throws DException { 562 int len = columnReferences.length; 563 Object [] values = new Object [len]; 564 for (int i = 0; i < len; i++) { 565 values[i] = getColumnValues(columnReferences[i]); 566 } 567 return values; 568 } 569 570 577 private int searchInMapping(TableDetails tDetails) throws DException { 578 for (int i = 0; i < tableDetailsMapping.length; i++) { 579 if (tableDetailsMapping[i][0] == tDetails) { 580 return ( (Integer ) tableDetailsMapping[i][1]).intValue(); 581 } 582 } 583 return -1; 584 } 585 586 595 public Object getColumnValues() throws DException { 596 ArrayList aList = new ArrayList(); 597 Object obj1 = leftIterator.getColumnValues(); 598 aList.addAll(Arrays.asList( (Object []) obj1)); 599 Object [] obj2 = null; 600 if (rightNull) { 601 obj2=makeNullFieldLiteralArray(rightColumnCount); 602 } else { 603 obj2 = (Object []) rightIterator.getColumnValues(); 604 } 605 aList.addAll(Arrays.asList(obj2)); 606 return aList.toArray(); 607 } 608 609 619 private void moveAcctoGroupByteKey(byte[] keys) throws DException { 620 int k=0,index =0; 621 int[] t=null; 622 if (leftKeyCount == 0) 623 throw new DException("DSE0", new Object [] {"Left Key Count can not be null"}); 624 else{ 625 index = moveBytes(leftTableDetails,leftIterator,index,keys); 626 } 627 if (rightKeyCount == 0) 628 rightIterator.first(); 629 else{ 630 index = moveBytes(rightTableDetails,rightIterator,index,keys); 631 } 632 state = VALIDSTATE; 633 } 634 635 644 private int moveBytes(TableDetails[] td, _Iterator iter, int index, 645 byte[] keys) throws DException { 646 byte[] temp; 647 int length = 0; 648 for (int j = 0; j < td.length; j++) { 649 short len = CCzufDpowfsufs.getShortValue(keys,index); 650 length +=len; 651 index = length + 2; 652 } 653 byte[] resultantKeys = new byte[length]; 654 System.arraycopy(keys, 0, resultantKeys, 0, resultantKeys.length); 655 iter.move(resultantKeys); 656 rightNull = isKeyNull(resultantKeys); 657 return index; 658 } 659 660 674 public void moveByteKey(byte[] key) throws DException { 675 676 if (leftKeyCount != 0 || rightKeyCount != 0) { 677 moveAcctoGroupByteKey(key); 678 return; 679 } 680 short leftLength = CCzufDpowfsufs.getShortValue(key,0); 681 byte[] leftKeys = new byte[leftLength]; 682 System.arraycopy(key, 2, leftKeys, 0, leftKeys.length); 683 leftIterator.moveByteKey(leftKeys); 684 short rightLength =CCzufDpowfsufs.getShortValue(key,leftKeys.length+2); 685 if (rightLength < 0 ) 686 rightNull = true; 687 else { 688 rightNull = false; 689 byte[] rightKeys = new byte[rightLength]; 690 System.arraycopy(key, leftKeys.length + 4, rightKeys, 0, rightLength); 691 rightIterator.moveByteKey(rightKeys); 692 } 693 state = 0; 694 } 695 696 709 public Object getColumnValues(_Reference references) throws DException { 710 Object values; 711 ColumnDetails refColumnDetail = (ColumnDetails) references; 712 TableDetails tDetails = refColumnDetail.getTable(); 713 int index = searchInMapping(tDetails); 714 if (index == -1) { 715 return GeneralPurposeStaticClass.getColumnValuesFromQualified(leftIterator, rightIterator, references, rightNull); 716 } 717 if (index == SimpleConstants.LEFT) { 718 values = leftIterator.getColumnValues(references); 719 } else if (rightNull) { 720 values = new FieldLiteral(FieldUtility.NULLBUFFERRANGE, ( (ColumnDetails) references).getDatatype()); 721 } else { 722 values = rightIterator.getColumnValues(references); 723 } 724 return values; 725 } 726 727 735 public FieldBase field(_Reference references) throws com.daffodilwoods.database.resource.DException { 736 FieldBase values; 737 ColumnDetails refColumnDetail = (ColumnDetails) references; 738 TableDetails tDetails = refColumnDetail.getTable(); 739 int index = searchInMapping(tDetails); 740 if (index == -1) { 741 return GeneralPurposeStaticClass.getFieldFromQualified(leftIterator, rightIterator, references, rightNull); 742 } 743 if (index == SimpleConstants.LEFT) { 744 values = leftIterator.field(references); 745 } else if (rightNull) { 746 values = new FieldLiteral(null, ( (ColumnDetails) references).getDatatype()); 747 ( (FieldLiteral) values).setBufferRange(FieldUtility.NULLBUFFERRANGE); 748 } else { 749 values = rightIterator.field(references); 750 } 751 return values; 752 } 753 754 762 public FieldBase[] fields(_Reference[] columnReferences) throws com.daffodilwoods.database.resource.DException { 763 int len = columnReferences.length; 764 FieldBase[] values = new FieldBase[len]; 765 for (int i = 0; i < len; i++) { 766 values[i] = field(columnReferences[i]); 767 } 768 return values; 769 } 770 771 782 public _Iterator getBaseIterator(ColumnDetails column) throws com.daffodilwoods.database.resource.DException { 783 return this; 784 } 785 786 791 public void releaseResource() throws DException { 792 conditionVariableValue.releaseResource(); 793 leftIterator.releaseResource(); 794 rightIterator.releaseResource(); 795 } 796 801 public _ExecutionPlan getExecutionPlan() throws DException { 802 _ExecutionPlan plan = leftIterator.getExecutionPlan(); 803 _ExecutionPlan plan1 = rightIterator.getExecutionPlan(); 804 _ExecutionPlan cplans[] = new _ExecutionPlan[] {plan, plan1}; 805 return new ExecutionPlan("SemiJoinFilteredIterator", cplans, null, null, null); 806 } 807 808 813 public ExecutionPlanForBrowser getExecutionPlanForBrowser() throws DException { 814 int length = 2; 815 ExecutionPlanForBrowser cplans[] = new ExecutionPlanForBrowser[length]; 816 cplans[0] = leftIterator.getExecutionPlanForBrowser(); 817 cplans[1] = rightIterator.getExecutionPlanForBrowser(); 818 return new ExecutionPlanForBrowser("Nested Join/Left Join", "Join Iterator", cplans, "" + conditionVariableValue, null, null); 819 } 820 821 public void setConditionVariableValue(_Reference[] parm1, Object [] parm2, int parm3) throws DException { 822 if (underlyingRef != null) { 823 parm1 = GeneralPurposeStaticClass.getJointReferences(parm1, underlyingRef); 824 parm2 = GeneralPurposeStaticClass.getJointValues(this, parm2, 825 underlyingRef.length); 826 } 827 super.setConditionVariableValue(parm1, parm2, parm3); 828 conditionVariableValue.setVariableValues(parm1, parm2, parm3); 829 } 830 831 public String toString() { 832 return "SemiJoinFilteredIterator[" + leftIterator + "][" + rightIterator + "]" + conditionVariableValue + "]"; 833 } 834 } 835 | Popular Tags |