1 21 22 package org.apache.derby.impl.sql.execute; 23 24 import org.apache.derby.iapi.services.io.FormatableBitSet; 25 26 import org.apache.derby.iapi.services.sanity.SanityManager; 27 28 import org.apache.derby.iapi.error.StandardException; 29 30 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 31 import org.apache.derby.iapi.sql.conn.StatementContext; 32 import org.apache.derby.iapi.sql.dictionary.IndexRowGenerator; 33 import org.apache.derby.iapi.sql.execute.ExecRow; 34 import org.apache.derby.iapi.sql.execute.ExecutionContext; 35 import org.apache.derby.iapi.sql.execute.RowChanger; 36 import org.apache.derby.iapi.sql.execute.ExecutionFactory; 37 import org.apache.derby.iapi.sql.execute.TemporaryRowHolder; 38 39 import org.apache.derby.iapi.sql.Activation; 40 41 import org.apache.derby.iapi.store.access.ConglomerateController; 42 import org.apache.derby.iapi.store.access.DynamicCompiledOpenConglomInfo; 43 import org.apache.derby.iapi.store.access.StaticCompiledOpenConglomInfo; 44 import org.apache.derby.iapi.store.access.TransactionController; 45 46 import org.apache.derby.iapi.types.DataValueDescriptor; 47 48 import org.apache.derby.iapi.types.RowLocation; 49 50 import java.util.Vector ; 51 52 55 class RowChangerImpl implements RowChanger 56 { 57 boolean isOpen = false; 58 59 boolean[] fixOnUpdate = null; 62 long heapConglom; 63 DynamicCompiledOpenConglomInfo heapDCOCI; 64 StaticCompiledOpenConglomInfo heapSCOCI; 65 long[] indexCIDS = null; 66 DynamicCompiledOpenConglomInfo[] indexDCOCIs; 67 StaticCompiledOpenConglomInfo[] indexSCOCIs; 68 IndexRowGenerator[] irgs = null; 69 Activation activation; 70 TransactionController tc; 71 FormatableBitSet changedColumnBitSet; 72 FormatableBitSet baseRowReadList; 73 private int[] baseRowReadMap; int[] changedColumnIds; 75 TemporaryRowHolderImpl rowHolder; 76 77 String [] indexNames; 79 80 private ConglomerateController baseCC; 83 private RowLocation baseRowLocation; 84 private IndexSetChanger isc; 85 86 private DataValueDescriptor[] sparseRowArray; 88 private int[] partialChangedColumnIds; 89 90 114 public RowChangerImpl( 115 long heapConglom, 116 StaticCompiledOpenConglomInfo heapSCOCI, 117 DynamicCompiledOpenConglomInfo heapDCOCI, 118 IndexRowGenerator[] irgs, 119 long[] indexCIDS, 120 StaticCompiledOpenConglomInfo[] indexSCOCIs, 121 DynamicCompiledOpenConglomInfo[] indexDCOCIs, 122 int numberOfColumns, 123 int[] changedColumnIdsInput, 124 TransactionController tc, 125 FormatableBitSet baseRowReadList, 126 int[] baseRowReadMap, 127 Activation activation) 128 throws StandardException 129 { 130 this.heapConglom = heapConglom; 131 this.heapSCOCI = heapSCOCI; 132 this.heapDCOCI = heapDCOCI; 133 this.irgs = irgs; 134 this.indexCIDS = indexCIDS; 135 this.indexSCOCIs = indexSCOCIs; 136 this.indexDCOCIs = indexDCOCIs; 137 this.tc = tc; 138 this.baseRowReadList = baseRowReadList; 139 this.baseRowReadMap = baseRowReadMap; 140 this.activation = activation; 141 142 if (SanityManager.DEBUG) 143 { 144 SanityManager.ASSERT(indexCIDS != null, "indexCIDS is null"); 145 } 146 147 152 if (changedColumnIdsInput != null) 153 { 154 160 changedColumnIds = RowUtil.inAscendingOrder(changedColumnIdsInput) ? 161 changedColumnIdsInput : sortArray(changedColumnIdsInput); 162 163 173 sparseRowArray = 174 new DataValueDescriptor[changedColumnIds[changedColumnIds.length - 1] + 1]; 175 changedColumnBitSet = new FormatableBitSet(numberOfColumns); 176 for (int i = 0; i < changedColumnIds.length; i++) 177 { 178 changedColumnBitSet.grow(changedColumnIds[i]); 181 changedColumnBitSet.set(changedColumnIds[i] - 1); 182 } 183 184 189 if (baseRowReadList != null) 190 { 191 partialChangedColumnIds = new int[changedColumnIds.length]; 192 int partialColumnNumber = 1; 193 int currentColumn = 0; 194 for (int i = 0; i < changedColumnIds.length; i++) 195 { 196 for (; currentColumn < changedColumnIds[i]; currentColumn++) 197 { 198 if (baseRowReadList.get(currentColumn)) 199 { 200 partialColumnNumber++; 201 } 202 } 203 partialChangedColumnIds[i] = partialColumnNumber; 204 } 205 } 206 } 207 208 if (SanityManager.DEBUG) 209 { 210 SanityManager.ASSERT(indexCIDS != null, "indexCIDS is null"); 211 } 212 213 } 214 215 224 public void setRowHolder(TemporaryRowHolder rowHolder) 225 { 226 this.rowHolder = (TemporaryRowHolderImpl)rowHolder; 227 } 228 229 232 public void setIndexNames(String [] indexNames) 233 { 234 this.indexNames = indexNames; 235 } 236 237 247 public void open(int lockMode) 248 throws StandardException 249 { 250 open(lockMode, true); 251 } 252 253 256 public void open(int lockMode, boolean wait) 257 throws StandardException 258 { 259 if (fixOnUpdate == null) 263 { 264 fixOnUpdate = new boolean[irgs.length]; 265 for (int ix = 0; ix < irgs.length; ix++) 266 fixOnUpdate[ix] = true; 267 } 268 openForUpdate(fixOnUpdate, lockMode, wait); 269 } 270 271 285 public void openForUpdate( 286 boolean[] fixOnUpdate, int lockMode, boolean wait 287 ) 288 throws StandardException 289 { 290 LanguageConnectionContext lcc = null; 291 292 if (SanityManager.DEBUG) 293 SanityManager.ASSERT( ! isOpen, "RowChanger already open"); 294 295 if (activation != null) 296 { 297 lcc = activation.getLanguageConnectionContext(); 298 } 299 300 301 int isolationLevel; 302 if (lcc == null) 303 { 304 isolationLevel = ExecutionContext.READ_COMMITTED_ISOLATION_LEVEL; 305 } 306 else 307 { 308 isolationLevel = lcc.getCurrentIsolationLevel(); 309 } 310 311 312 switch (isolationLevel) 313 { 314 case ExecutionContext.READ_UNCOMMITTED_ISOLATION_LEVEL: 317 isolationLevel = 318 TransactionController.ISOLATION_READ_UNCOMMITTED; 319 break; 320 321 case ExecutionContext.READ_COMMITTED_ISOLATION_LEVEL: 322 isolationLevel = 323 TransactionController.ISOLATION_READ_COMMITTED; 324 break; 325 326 case ExecutionContext.REPEATABLE_READ_ISOLATION_LEVEL: 327 isolationLevel = 328 TransactionController.ISOLATION_REPEATABLE_READ; 329 break; 330 331 case ExecutionContext.SERIALIZABLE_ISOLATION_LEVEL: 332 isolationLevel = 333 TransactionController.ISOLATION_SERIALIZABLE; 334 break; 335 336 default: 337 if (SanityManager.DEBUG) 338 { 339 SanityManager.THROWASSERT( 340 "Invalid isolation level - " + isolationLevel); 341 } 342 } 343 344 try { 345 346 350 if (heapSCOCI != null) 351 { 352 baseCC = 353 tc.openCompiledConglomerate( 354 false, 355 (TransactionController.OPENMODE_FORUPDATE | 356 ((wait) ? 0 : TransactionController.OPENMODE_LOCK_NOWAIT)), 357 lockMode, 358 isolationLevel, 359 heapSCOCI, 360 heapDCOCI); 361 } 362 else 363 { 364 baseCC = 365 tc.openConglomerate( 366 heapConglom, 367 false, 368 (TransactionController.OPENMODE_FORUPDATE | 369 ((wait) ? 0 : TransactionController.OPENMODE_LOCK_NOWAIT)), 370 lockMode, 371 isolationLevel); 372 } 373 374 } catch (StandardException se) { 375 if (activation != null) 376 activation.checkStatementValidity(); 377 throw se; 378 } 379 380 387 if (activation != null) 388 { 389 activation.checkStatementValidity(); 390 activation.setHeapConglomerateController(baseCC); 391 } 392 393 394 if (indexCIDS.length != 0) 395 { 396 397 if (isc == null) 398 { 399 isc = new IndexSetChanger(irgs, 400 indexCIDS, 401 indexSCOCIs, 402 indexDCOCIs, 403 indexNames, 404 baseCC, 405 tc, 406 lockMode, 407 baseRowReadList, 408 isolationLevel, 409 activation 410 ); 411 isc.setRowHolder(rowHolder); 412 } 413 else 414 { 415 416 419 isc.setBaseCC(baseCC); 420 } 421 422 isc.open(fixOnUpdate); 423 424 if (baseRowLocation == null) 425 baseRowLocation = baseCC.newRowLocationTemplate(); 426 } 427 428 isOpen = true; 429 } 430 431 437 public void insertRow(ExecRow baseRow) 438 throws StandardException 439 { 440 if (SanityManager.DEBUG) 441 SanityManager.ASSERT(! baseCC.isKeyed(), 442 "Keyed inserts not yet supported"); 443 444 if (baseCC.isKeyed()) 445 { 446 } 448 else 449 { 450 if (isc != null) 451 { 452 baseCC.insertAndFetchLocation(baseRow.getRowArray(), baseRowLocation); 453 isc.insert(baseRow, baseRowLocation); 454 } 455 else 456 { 457 baseCC.insert(baseRow.getRowArray()); 458 } 459 } 460 } 461 462 463 471 public void deleteRow(ExecRow baseRow, RowLocation baseRowLocation) 472 throws StandardException 473 { 474 if (isc != null) 475 { 476 isc.delete(baseRow, baseRowLocation); 477 } 478 baseCC.delete(baseRowLocation); 479 } 480 481 490 public void updateRow(ExecRow oldBaseRow, 491 ExecRow newBaseRow, 492 RowLocation baseRowLocation) 493 throws StandardException 494 { 495 if (isc != null) 496 { 497 isc.update(oldBaseRow, newBaseRow, baseRowLocation); 498 } 499 500 if (changedColumnBitSet != null) 501 { 502 DataValueDescriptor[] baseRowArray = newBaseRow.getRowArray(); 503 int[] changedColumnArray = (partialChangedColumnIds == null) ? 504 changedColumnIds : partialChangedColumnIds; 505 int nextColumnToUpdate = -1; 506 for (int i = 0; i < changedColumnArray.length; i++) 507 { 508 int copyFrom = changedColumnArray[i] - 1; 509 nextColumnToUpdate = 510 changedColumnBitSet.anySetBit(nextColumnToUpdate); 511 if (SanityManager.DEBUG) 512 { 513 SanityManager.ASSERT(nextColumnToUpdate >= 0, 514 "More columns in changedColumnArray than in changedColumnBitSet"); 515 } 516 sparseRowArray[nextColumnToUpdate] = baseRowArray[copyFrom]; 517 } 518 } 519 else 520 { 521 sparseRowArray = newBaseRow.getRowArray(); 522 } 523 baseCC.replace(baseRowLocation, 524 sparseRowArray, 525 changedColumnBitSet); 526 } 527 528 534 public void finish() 535 throws StandardException 536 { 537 if (isc != null) 538 { 539 isc.finish(); 540 } 541 } 542 543 548 public void close() 549 throws StandardException 550 { 551 if (isc != null) 557 { 558 isc.close(); 559 } 560 561 if (baseCC != null) 562 { 563 if (activation == null || activation.getForUpdateIndexScan() == null) 564 baseCC.close(); baseCC = null; 566 } 567 568 isOpen = false; 569 570 573 if (activation != null) 574 { 575 activation.clearHeapConglomerateController(); 576 } 577 } 578 579 580 public ConglomerateController getHeapConglomerateController() 581 { 582 return baseCC; 583 } 584 585 private int[] sortArray(int[] input) 586 { 587 591 int[] output = new int[input.length]; 592 System.arraycopy(input, 0, output, 0, input.length); 593 java.util.Arrays.sort(output); 594 return output; 595 } 596 } 597 | Popular Tags |