1 21 22 package org.apache.derby.impl.sql.execute; 23 24 import org.apache.derby.iapi.services.sanity.SanityManager; 25 import org.apache.derby.iapi.error.StandardException; 26 27 import org.apache.derby.iapi.store.access.ConglomerateController; 28 import org.apache.derby.iapi.store.access.ScanController; 29 import org.apache.derby.iapi.store.access.TransactionController; 30 31 import org.apache.derby.iapi.reference.SQLState; 32 33 import org.apache.derby.iapi.sql.execute.CursorResultSet; 34 import org.apache.derby.iapi.sql.execute.ExecRow; 35 import org.apache.derby.iapi.sql.execute.ExecutionContext; 36 import org.apache.derby.iapi.sql.execute.NoPutResultSet; 37 import org.apache.derby.iapi.sql.execute.ExecutionFactory; 38 import org.apache.derby.iapi.sql.execute.TargetResultSet; 39 40 import org.apache.derby.iapi.sql.Activation; 41 import org.apache.derby.iapi.sql.ResultDescription; 42 import org.apache.derby.iapi.sql.ResultSet; 43 import org.apache.derby.iapi.sql.Row; 44 45 import org.apache.derby.iapi.types.DataValueDescriptor; 46 import org.apache.derby.iapi.types.RowLocation; 47 import org.apache.derby.iapi.sql.execute.ExecIndexRow; 48 import org.apache.derby.iapi.types.SQLLongint; 49 50 import org.apache.derby.iapi.services.io.FormatableBitSet; 51 import java.sql.Timestamp ; 52 53 54 61 class TemporaryRowHolderResultSet implements CursorResultSet, NoPutResultSet, Cloneable 62 { 63 private ExecRow[] rowArray; 64 private int numRowsOut; 65 private ScanController scan; 66 private TransactionController tc; 67 private boolean isOpen; 68 private boolean finished; 69 private ExecRow currentRow; 70 private ResultDescription resultDescription; 71 private ExecutionFactory ef; 72 private boolean isAppendable = false; 73 private long positionIndexConglomId; 74 private boolean isVirtualMemHeap; 75 private boolean currRowFromMem; 76 private TemporaryRowHolderImpl holder; 77 78 ConglomerateController heapCC; 80 private RowLocation baseRowLocation; 81 82 89 public TemporaryRowHolderResultSet 90 ( 91 TransactionController tc, 92 ExecRow[] rowArray, 93 ResultDescription resultDescription, 94 boolean isVirtualMemHeap, 95 TemporaryRowHolderImpl holder 96 ) 97 { 98 99 this(tc, rowArray, resultDescription, isVirtualMemHeap, false, 0, holder); 100 101 102 } 103 104 114 public TemporaryRowHolderResultSet 115 ( 116 TransactionController tc, 117 ExecRow[] rowArray, 118 ResultDescription resultDescription, 119 boolean isVirtualMemHeap, 120 boolean isAppendable, 121 long positionIndexConglomId, 122 TemporaryRowHolderImpl holder 123 ) 124 { 125 this.tc = tc; 126 this.rowArray = rowArray; 127 this.resultDescription = resultDescription; 128 this.numRowsOut = 0; 129 isOpen = false; 130 finished = false; 131 this.isVirtualMemHeap = isVirtualMemHeap; 132 this.isAppendable = isAppendable; 133 this.positionIndexConglomId = positionIndexConglomId; 134 135 if (SanityManager.DEBUG) 136 { 137 SanityManager.ASSERT(rowArray != null, "rowArray is null"); 138 SanityManager.ASSERT(rowArray.length > 0, "rowArray has no elements, need at least one"); 139 } 140 141 this.holder = holder; 142 } 143 144 149 public void reset(ExecRow[] rowArray) 150 { 151 this.rowArray = rowArray; 152 this.numRowsOut = 0; 153 isOpen = false; 154 finished = false; 155 156 if (SanityManager.DEBUG) 157 { 158 SanityManager.ASSERT(rowArray != null, "rowArray is null"); 159 SanityManager.ASSERT(rowArray.length > 0, "rowArray has no elements, need at least one"); 160 } 161 } 162 163 164 167 public void reStartScan(long currentConglomId, long pconglomId) throws StandardException 168 { 169 if(isAppendable) 170 { 171 holder.CID = currentConglomId; 172 positionIndexConglomId = pconglomId; 173 setupPositionBasedScan(numRowsOut); 174 }else 175 { 176 numRowsOut--; 177 } 178 } 179 180 181 192 public static TemporaryRowHolderResultSet getNewRSOnCurrentRow 193 ( 194 Activation activation, 195 CursorResultSet rs 196 ) throws StandardException 197 { 198 TemporaryRowHolderImpl singleRow = 199 new TemporaryRowHolderImpl(activation, null, 200 rs.getResultDescription()); 201 singleRow.insert(rs.getCurrentRow()); 202 return (TemporaryRowHolderResultSet) singleRow.getResultSet(); 203 } 204 205 214 public void markAsTopResultSet() 215 { } 216 217 222 public void openCore() throws StandardException 223 { 224 this.numRowsOut = 0; 225 isOpen = true; 226 currentRow = null; 227 228 if(isAppendable) 229 setupPositionBasedScan(numRowsOut); 230 } 231 232 237 public void reopenCore() throws StandardException 238 { 239 numRowsOut = 0; 240 isOpen = true; 241 currentRow = null; 242 243 if(isAppendable) 244 { 245 setupPositionBasedScan(numRowsOut); 246 return; 247 } 248 249 if (scan != null) 250 { 251 scan.reopenScan( 252 (DataValueDescriptor[]) null, 0, null, (DataValueDescriptor[]) null, 0); } 258 } 259 260 267 public ExecRow getNextRowCore() 268 throws StandardException 269 { 270 271 if (!isOpen) 272 { 273 return (ExecRow)null; 274 } 275 276 if(isAppendable) 277 { 278 return getNextAppendedRow() ; 279 } 280 281 if (isVirtualMemHeap && holder.lastArraySlot >= 0) 282 { 283 numRowsOut++; 284 currentRow = rowArray[holder.lastArraySlot]; 285 currRowFromMem = true; 286 return currentRow; 287 } 288 else if (numRowsOut++ <= holder.lastArraySlot) 289 { 290 currentRow = rowArray[numRowsOut-1]; 291 return currentRow; 292 } 293 294 if (holder.CID == 0) 295 { 296 return (ExecRow)null; 297 } 298 299 302 if (scan == null) 303 { 304 scan = 305 tc.openScan( 306 holder.CID, 307 false, 0, TransactionController.MODE_TABLE, 310 TransactionController.ISOLATION_SERIALIZABLE, 311 (FormatableBitSet) null, 312 (DataValueDescriptor[]) null, 0, null, (DataValueDescriptor[]) null, 0); } 318 else if (isVirtualMemHeap && holder.state == TemporaryRowHolderImpl.STATE_INSERT) 319 { 320 holder.state = TemporaryRowHolderImpl.STATE_DRAIN; 321 scan.reopenScan( 322 (DataValueDescriptor[]) null, 0, null, (DataValueDescriptor[]) null, 0); } 328 329 if (scan.next()) 330 { 331 currentRow = rowArray[0].getNewNullRow(); 332 scan.fetch(currentRow.getRowArray()); 333 currRowFromMem = false; 334 return currentRow; 335 } 336 return (ExecRow)null; 337 } 338 339 public void deleteCurrentRow() 340 throws StandardException 341 { 342 if (SanityManager.DEBUG) 343 { 344 SanityManager.ASSERT(isVirtualMemHeap, "deleteCurrentRow is not implemented"); 345 } 346 if (currRowFromMem) 347 { 348 if (holder.lastArraySlot > 0) rowArray[holder.lastArraySlot] = null; holder.lastArraySlot--; 351 } 352 else 353 { 354 if (baseRowLocation == null) 355 baseRowLocation = scan.newRowLocationTemplate(); 356 scan.fetchLocation(baseRowLocation); 357 if(heapCC == null) 358 { 359 heapCC = tc.openConglomerate( holder.CID, 360 false, 361 TransactionController.OPENMODE_FORUPDATE, 362 TransactionController.MODE_TABLE, 363 TransactionController.ISOLATION_SERIALIZABLE); 364 } 365 heapCC.delete(baseRowLocation); 366 } 367 } 368 369 370 DataValueDescriptor[] indexRow; 372 ScanController indexsc; 373 374 private void setupPositionBasedScan(long position) throws StandardException 376 { 377 378 if(holder.CID ==0) 380 return; 381 if(heapCC == null) 382 { 383 heapCC = tc.openConglomerate( holder.CID, 384 false, 385 0, 386 TransactionController.MODE_TABLE, 387 TransactionController.ISOLATION_SERIALIZABLE); 388 389 } 390 391 currentRow = rowArray[0].getNewNullRow(); 392 indexRow = new DataValueDescriptor[2]; 393 indexRow[0] = new SQLLongint(position); 394 indexRow[1] = heapCC.newRowLocationTemplate(); 395 396 DataValueDescriptor[] searchRow = new DataValueDescriptor[1]; 397 searchRow[0] = new SQLLongint(position); 398 399 if(indexsc == null) 400 { 401 indexsc = tc.openScan(positionIndexConglomId, 402 false, 0, TransactionController.MODE_TABLE, 405 TransactionController.ISOLATION_SERIALIZABLE, 406 (FormatableBitSet) null, searchRow, ScanController.GE, null, null, ScanController.GT); }else 413 { 414 415 indexsc.reopenScan( 416 searchRow, ScanController.GE, null, null, ScanController.GT ); 422 } 423 424 } 425 426 427 private ExecRow getNextAppendedRow() throws StandardException 429 { 430 if (indexsc == null) return null; 431 if (!indexsc.fetchNext(indexRow)) 432 { 433 return null; 434 } 435 436 RowLocation baseRowLocation = (RowLocation) indexRow[1]; 437 boolean base_row_exists = 438 heapCC.fetch( 439 baseRowLocation, currentRow.getRowArray(), (FormatableBitSet) null); 440 441 if (SanityManager.DEBUG) 442 { 443 SanityManager.ASSERT(base_row_exists, "base row disappeared."); 444 } 445 numRowsOut++; 446 return currentRow; 447 } 448 449 450 451 459 public int getPointOfAttachment() 460 { 461 return -1; 462 } 463 464 471 public int getScanIsolationLevel() 472 { 473 return TransactionController.ISOLATION_SERIALIZABLE; 474 } 475 476 482 public void setTargetResultSet(TargetResultSet trs) 483 { 484 } 485 486 491 public void setNeedsRowLocation(boolean needsRowLocation) 492 { 493 } 494 495 500 public double getEstimatedRowCount() 501 { 502 return 0d; 503 } 504 505 509 public int resultSetNumber() 510 { 511 return 0; 512 } 513 514 520 public void setCurrentRow(ExecRow row) 521 { 522 currentRow = row; 523 } 524 525 529 public void clearCurrentRow() 530 { 531 currentRow = null; 532 } 533 534 543 public ExecRow getCurrentRow() throws StandardException 544 { 545 if (SanityManager.DEBUG) 546 { 547 SanityManager.ASSERT(isOpen, "resultSet expected to be open"); 548 } 549 550 return currentRow; 551 } 552 553 561 public RowLocation getRowLocation() 562 { 563 if (SanityManager.DEBUG) 564 { 565 SanityManager.ASSERT(isOpen, "resultSet expected to be open"); 566 } 567 return (RowLocation)null; 568 } 569 570 571 576 public void close() throws StandardException 577 { 578 isOpen = false; 579 numRowsOut = 0; 580 currentRow = null; 581 if (scan != null) 582 { 583 scan.close(); 584 scan = null; 585 } 586 } 587 588 589 595 601 public boolean returnsRows() 602 { 603 return true; 604 } 605 606 public int modifiedRowCount() { return 0;}; 607 608 617 public ResultDescription getResultDescription() 618 { 619 return resultDescription; 620 } 621 622 627 public void open() throws StandardException 628 { 629 openCore(); 630 } 631 632 647 public ExecRow getAbsoluteRow(int row) throws StandardException 648 { 649 if (SanityManager.DEBUG) 650 { 651 SanityManager.THROWASSERT( 652 "getAbsoluteRow() not expected to be called yet."); 653 } 654 655 return null; 656 } 657 658 675 public ExecRow getRelativeRow(int row) throws StandardException 676 { 677 if (SanityManager.DEBUG) 678 { 679 SanityManager.THROWASSERT( 680 "getRelativeRow() not expected to be called yet."); 681 } 682 683 return null; 684 } 685 686 695 public ExecRow setBeforeFirstRow() 696 throws StandardException 697 { 698 if (SanityManager.DEBUG) 699 { 700 SanityManager.THROWASSERT( 701 "setBeforeFirstRow() not expected to be called yet."); 702 } 703 704 return null; 705 } 706 707 716 public ExecRow getFirstRow() 717 throws StandardException 718 { 719 if (SanityManager.DEBUG) 720 { 721 SanityManager.THROWASSERT( 722 "getFirstRow() not expected to be called yet."); 723 } 724 725 return null; 726 } 727 728 737 public ExecRow getNextRow() throws StandardException 738 { 739 return getNextRowCore(); 740 } 741 742 751 public ExecRow getPreviousRow() 752 throws StandardException 753 { 754 if (SanityManager.DEBUG) 755 { 756 SanityManager.THROWASSERT( 757 "getPreviousRow() not expected to be called yet."); 758 } 759 760 return null; 761 } 762 763 772 public ExecRow getLastRow() 773 throws StandardException 774 { 775 if (SanityManager.DEBUG) 776 { 777 SanityManager.THROWASSERT( 778 "getLastRow() not expected to be called yet."); 779 } 780 781 return null; 782 } 783 784 793 public ExecRow setAfterLastRow() 794 throws StandardException 795 { 796 if (SanityManager.DEBUG) 797 { 798 SanityManager.THROWASSERT( 799 "getLastRow() not expected to be called yet."); 800 } 801 802 return null; 803 } 804 805 812 public boolean checkRowPosition(int isType) 813 { 814 return false; 815 } 816 817 826 public int getRowNumber() 827 { 828 return 0; 829 } 830 831 836 public void cleanUp() throws StandardException 837 { 838 close(); 839 } 840 841 842 848 public boolean isClosed() 849 { 850 return !isOpen; 851 } 852 853 861 public void finish() throws StandardException 862 { 863 finished = true; 864 close(); 865 } 866 867 868 873 public long getExecuteTime() 874 { 875 return 0L; 876 } 877 878 881 public ResultSet getAutoGeneratedKeysResultset() 882 { 883 return (ResultSet)null; 885 } 886 887 892 public Timestamp getBeginExecutionTimestamp() 893 { 894 return (Timestamp )null; 895 } 896 897 902 public Timestamp getEndExecutionTimestamp() 903 { 904 return (Timestamp )null; 905 } 906 907 915 public long getTimeSpent(int type) 916 { 917 return 0L; 918 } 919 920 921 929 public NoPutResultSet[] getSubqueryTrackingArray(int numSubqueries) 930 { 931 return (NoPutResultSet[])null; 932 } 933 934 942 public String getCursorName() 943 { 944 return (String ) null; 945 } 946 947 950 public boolean requiresRelocking() 951 { 952 if (SanityManager.DEBUG) 953 { 954 SanityManager.THROWASSERT( 955 "requiresRelocking() not expected to be called for " + 956 getClass().getName()); 957 } 958 return false; 959 } 960 961 995 public DataValueDescriptor[] getNextRowFromRowSource() throws StandardException 996 { 997 return null; 998 } 999 1000 1009 public boolean needsToClone() 1010 { 1011 return false; 1012 } 1013 1014 1015 1033 public FormatableBitSet getValidColumns() 1034 { 1035 return null; 1036 } 1037 1038 1044 public void closeRowSource() 1045 { } 1046 1047 1048 1062 public boolean needsRowLocation() 1063 { 1064 return false; 1065 } 1066 1067 1103 public void rowLocation(RowLocation rl) throws StandardException 1104 { } 1105 1106 1112 public void positionScanAtRowLocation(RowLocation rl) 1113 throws StandardException 1114 { 1115 } 1117 1118 1120 1126 public boolean isForUpdate() 1127 { 1128 return false; 1129 } 1130 1131 1135 public Object clone() 1136 { 1137 Object clo = null; 1138 try { 1139 clo = super.clone(); 1140 } 1141 catch (CloneNotSupportedException e) {} 1142 return clo; 1143 } 1144 public java.sql.SQLWarning getWarnings() { 1145 return null; 1146 } 1147 1148 1154 public void updateRow(ExecRow row) throws StandardException { 1155 } 1159 1160 1166 public void markRowAsDeleted() throws StandardException { 1167 } 1171 1172 1177 public final Activation getActivation() { 1178 return holder.activation; 1179 } 1180} 1181 | Popular Tags |