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.sql99.common.*; 7 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*; 8 import com.daffodilwoods.daffodildb.server.sql99.dql.listenerevents.*; 9 import com.daffodilwoods.daffodildb.server.sql99.utils.*; 10 import com.daffodilwoods.daffodildb.utils.*; 11 import com.daffodilwoods.daffodildb.utils.field.*; 12 import com.daffodilwoods.database.resource.*; 13 import com.daffodilwoods.daffodildb.utils.byteconverter.CCzufDpowfsufs; 14 15 33 public class SemiJoinIteratorWithoutCondition extends AbstractQualifiedJoinIterator implements _HasRecordIterator, SimpleConstants { 34 35 38 39 private int leftCount = -1; 41 public SemiJoinIteratorWithoutCondition(_Iterator leftIterator, _Iterator rightIterator, _KeyColumnInformation[] leftKeyColumnInformation0, _KeyColumnInformation[] rightKeyColumnInformation0, ColumnDetails[] hasRecordReferences0) { 42 super(leftIterator, rightIterator, leftKeyColumnInformation0, rightKeyColumnInformation0, hasRecordReferences0); 43 leftCount = leftKeyColumnInformation0.length; 44 } 45 46 71 public boolean first() throws DException { 72 rightNull = false; 73 if (!leftIterator.first()) { 74 state = AFTERLAST; 75 return false; 76 } 77 rightNull = !rightIterator.first(); 78 state = VALIDSTATE; 79 return true; 80 } 81 82 107 public boolean last() throws DException { 108 rightNull = false; 109 if (!leftIterator.last()) { 110 state = BEFOREFIRST; 111 return false; 112 } 113 rightNull = !rightIterator.last(); 114 state = VALIDSTATE; 115 return true; 116 } 117 118 169 public boolean next() throws DException { 170 switch (state) { 171 case INVALIDSTATE: 172 throw new DException("DSE4116", null); 173 case AFTERLAST: 174 return false; 175 case BEFOREFIRST: 176 return first(); 177 default: { 178 if (rightNull) { 179 if (!leftIterator.next()) { 180 state = AFTERLAST; 181 return false; 182 } 183 } else { 184 if (!rightIterator.next()) { 185 if (!leftIterator.next()) { 186 state = AFTERLAST; 187 return false; 188 } 189 rightIterator.first(); 190 } 191 } 192 state = VALIDSTATE; 193 return true; 194 } 195 } 196 } 197 198 244 public boolean previous() throws DException { 245 switch (state) { 246 case INVALIDSTATE: 247 throw new DException("DSE4117", null); 248 case BEFOREFIRST: 249 return false; 250 case AFTERLAST: 251 return last(); 252 default: { 253 if (rightNull) { 254 if (!leftIterator.previous()) { 255 state = BEFOREFIRST; 256 return false; 257 } 258 } else { 259 if (!rightIterator.previous()) { 260 if (!leftIterator.previous()) { 261 state = BEFOREFIRST; 262 return false; 263 } 264 rightIterator.last(); 265 } 266 } 267 state = VALIDSTATE; 268 return true; 269 } 270 } 271 } 272 273 279 public Object getKey() throws DException { 280 ArrayList list = new ArrayList(); 281 Object [] leftKeys = (Object []) leftIterator.getKey(); 282 list.addAll(java.util.Arrays.asList(leftKeys)); 283 this.leftCount = leftKeys.length; 284 if (rightNull) { 285 list.add(null); 286 } else { 287 Object [] rightKeys = (Object []) rightIterator.getKey(); 288 list.addAll(java.util.Arrays.asList(rightKeys)); 289 } 290 return list.toArray(); 291 } 292 293 304 private void moveAcctoGroupKey(Object [] keys) throws DException { 305 if (leftKeyCount == 0) { 306 leftIterator.first(); 307 } else { 308 Object [] leftKeys = new Object [leftKeyCount]; 309 System.arraycopy(keys, 0, leftKeys, 0, leftKeyCount); 310 leftIterator.move(leftKeys); 311 } 312 if (rightKeyCount == 0) { 313 rightNull = !rightIterator.first(); 314 } else { 315 int rightLength = keys.length - leftKeyCount; 316 Object [] rightKeys = new Object [rightLength]; 317 System.arraycopy(keys, leftKeyCount, rightKeys, 0, rightLength); 318 if (isKeyNull(rightKeys)) { 319 rightNull = true; 320 } else { 321 rightNull = false; 322 rightIterator.move(rightKeys); 323 } 324 } 325 state = VALIDSTATE; 326 } 327 328 341 public void move(Object keys) throws DException { 342 if (leftKeyCount != 0 || rightKeyCount != 0) { 343 moveAcctoGroupKey( (Object []) keys); 344 return; 345 } 346 Object [] leftKeys = new Object [leftCount]; 347 Object [] rightKeys = new Object [ ( (Object []) keys).length - leftCount]; 348 System.arraycopy(keys, 0, leftKeys, 0, leftCount); 349 System.arraycopy(keys, leftCount, rightKeys, 0, rightKeys.length); 350 leftIterator.move(leftKeys); 351 if (isKeyNull(rightKeys)) { 352 rightNull = true; 353 } else { 354 rightNull = false; 355 rightIterator.move(rightKeys); 356 } 357 state = VALIDSTATE; 358 } 359 360 368 377 public Object getColumnValues(_Reference[] leftColumnReferences) throws DException { 378 int len = leftColumnReferences.length; 379 Object [] values = new Object [len]; 380 for (int i = 0; i < len; i++) { 381 ColumnDetails refColumnDetail = (ColumnDetails) leftColumnReferences[i]; 382 TableDetails tDetails = refColumnDetail.getTable(); 383 if (searchInMapping(tDetails) == SimpleConstants.LEFT) { 384 values[i] = leftIterator.getColumnValues(leftColumnReferences[i]); 385 } else 386 if (!rightNull) { 387 values[i] = rightIterator.getColumnValues(leftColumnReferences[i]); 388 } else { 389 values[i] = new FieldLiteral(FieldUtility.NULLBUFFERRANGE, ( (ColumnDetails) leftColumnReferences[i]).getDatatype()); 390 } 391 } 392 return values; 393 } 394 395 404 public Object getColumnValues() throws DException { 405 ArrayList aList = new ArrayList(); 406 Object obj1 = leftIterator.getColumnValues(); 407 aList.addAll(Arrays.asList( (Object []) obj1)); 408 Object [] obj2 = null; 409 if (rightNull) { 410 obj2=makeNullFieldLiteralArray(rightColumnCount); 411 } else { 412 obj2 = (Object []) rightIterator.getColumnValues(); 413 } 414 aList.addAll(Arrays.asList(obj2)); 415 return aList.toArray(); 416 } 417 418 428 public Object getColumnValues(_Reference references) throws DException { 429 field(references); 430 Object values = null; 431 ColumnDetails refColumnDetail = (ColumnDetails) references; 432 TableDetails tDetails = refColumnDetail.getTable(); 433 int index = searchInMapping(tDetails); 434 if (index == -1) { 435 return GeneralPurposeStaticClass.getColumnValuesFromQualified(leftIterator, rightIterator, references, rightNull); 436 } 437 if (index == SimpleConstants.LEFT) { 438 values = leftIterator.getColumnValues(references); 439 } else if (rightNull) { 440 values = new FieldLiteral(FieldUtility.NULLBUFFERRANGE, ( (ColumnDetails) references).getDatatype()); 441 } else { 442 values = rightIterator.getColumnValues(references); 443 } 444 return values; 445 } 446 447 455 public FieldBase field(_Reference references) throws DException { 456 FieldBase values = null; 457 ColumnDetails refColumnDetail = (ColumnDetails) references; 458 TableDetails tDetails = refColumnDetail.getTable(); 459 int index = searchInMapping(tDetails); 460 if (index == -1) { 461 return GeneralPurposeStaticClass.getFieldFromQualified(leftIterator, rightIterator, references, rightNull); 462 } 463 if (index == SimpleConstants.LEFT) { 464 values = leftIterator.field(references); 465 } else if (rightNull) { 466 values = new FieldLiteral(null, ( (ColumnDetails) references).getDatatype()); 467 ( (FieldLiteral) values).setBufferRange(FieldUtility.NULLBUFFERRANGE); 468 } else { 469 values = rightIterator.field(references); 470 } 471 return values; 472 } 473 474 482 public FieldBase[] fields(_Reference[] leftColumnReferences) throws DException { 483 int len = leftColumnReferences.length; 484 FieldBase[] values = new FieldBase[len]; 485 for (int i = 0; i < len; i++) { 486 values[i] = field(leftColumnReferences[i]); 487 } 488 return values; 489 } 490 491 498 public byte[] getByteKey() throws DException { 499 byte[] leftKeys = leftIterator.getByteKey(); 500 if (rightNull) { 501 short leftLen = (short)leftKeys.length; 502 byte[] result = new byte[leftLen + 4]; 503 System.arraycopy(CCzufDpowfsufs.getBytes(leftLen),0,result,0,2); 504 System.arraycopy(leftKeys, 0, result, 2, leftKeys.length); 505 System.arraycopy(CCzufDpowfsufs.getBytes((short)-1),0,result,leftLen+2,2); 506 return result; 507 508 } 509 else{ 510 byte[] rightKeys = rightIterator.getByteKey(); 511 byte[] resultantKeys = new byte[leftKeys.length + rightKeys.length + 4]; 512 short leftLen = (short)leftKeys.length; 513 System.arraycopy(CCzufDpowfsufs.getBytes(leftLen),0,resultantKeys,0,2); 514 System.arraycopy(leftKeys, 0, resultantKeys, 2, leftKeys.length); 515 short rightLen = (short)rightKeys.length; 516 System.arraycopy(CCzufDpowfsufs.getBytes(rightLen),0,resultantKeys,leftLen+2,2); 517 System.arraycopy(rightKeys, 0, resultantKeys, leftKeys.length + 4, 518 rightKeys.length); 519 return resultantKeys; 520 } 521 } 522 523 533 private void moveAcctoGroupByteKey(byte[] keys) throws DException { 534 int k = 0, index = 0; 535 int[] t = null; 536 if (leftKeyCount == 0) 537 throw new DException("DSE0", new Object [] {"Left Key Count can not be null"}); 538 else { 539 index = moveBytes(leftTableDetails, leftIterator, index, keys); 540 } 541 if (rightKeyCount == 0) 542 rightIterator.first(); 543 else { 544 index = moveBytes(rightTableDetails, rightIterator, index, keys); 545 } 546 state = VALIDSTATE; 547 } 548 549 558 private int moveBytes(TableDetails[] td, _Iterator iter, int index, 559 byte[] keys) throws DException { 560 int length = 0; 561 for (int j = 0; j < td.length; j++) { 562 short len = CCzufDpowfsufs.getShortValue(keys,index); 563 length +=len; 564 index = length + 2; 565 } 566 byte[] resultantKeys = new byte[length]; 567 System.arraycopy(keys, 0, resultantKeys, 0, resultantKeys.length); 568 iter.move(resultantKeys); 569 rightNull = isKeyNull(resultantKeys); 570 return index; 571 572 } 573 574 public void moveByteKey(byte[] key) throws DException { 575 576 if (leftKeyCount != 0 || rightKeyCount != 0) { 577 moveAcctoGroupByteKey(key); 578 return; 579 } 580 short leftLength = CCzufDpowfsufs.getShortValue(key,0); 581 byte[] leftKeys = new byte[leftLength]; 582 System.arraycopy(key, 2, leftKeys, 0, leftKeys.length); 583 leftIterator.moveByteKey(leftKeys); 584 short rightLength =CCzufDpowfsufs.getShortValue(key,leftKeys.length+2); 585 if (rightLength < 0) 586 rightNull = true; 587 else { 588 rightNull = false; 589 byte[] rightKeys = new byte[rightLength]; 590 System.arraycopy(key, leftKeys.length + 4, rightKeys, 0, rightLength); 591 rightIterator.moveByteKey(rightKeys); 592 } 593 state = 0; 594 595 } 596 597 598 599 600 607 private int searchInMapping(TableDetails tDetails) throws DException { 608 for (int i = 0; i < tableDetailsMapping.length; i++) { 609 if (tableDetailsMapping[i][0] == tDetails) { 610 return ( (Integer ) tableDetailsMapping[i][1]).intValue(); 611 } 612 } 613 return -1; 614 } 615 616 627 public _Iterator getBaseIterator(ColumnDetails column) throws DException { 628 return this; 629 } 630 631 636 public _ExecutionPlan getExecutionPlan() throws DException { 637 _ExecutionPlan plan = leftIterator.getExecutionPlan(); 638 _ExecutionPlan plan1 = rightIterator.getExecutionPlan(); 639 _ExecutionPlan cplans[] = new _ExecutionPlan[] {plan, plan1}; 640 return new ExecutionPlan("SemiJoinIteratorWithoutCondition", cplans, null, null, null); 641 } 642 643 648 public ExecutionPlanForBrowser getExecutionPlanForBrowser() throws DException { 649 int length = 2; 650 ExecutionPlanForBrowser cplans[] = new ExecutionPlanForBrowser[length]; 651 cplans[0] = leftIterator.getExecutionPlanForBrowser(); 652 cplans[1] = rightIterator.getExecutionPlanForBrowser(); 653 String refer = ""; 654 return new ExecutionPlanForBrowser("Nested Join/Left Join" + refer, "Left Join Iterator", cplans, null, null, null); 655 } 656 657 public String toString() { 658 return "SemiJoinIteratorWithoutCondition[" + leftIterator + "]\n\n\n[" + rightIterator + "]"; 659 } 660 } 661 | Popular Tags |