1 21 22 package org.apache.derby.impl.store.access.btree; 23 24 import org.apache.derby.iapi.services.sanity.SanityManager; 25 26 import org.apache.derby.iapi.error.StandardException; 27 28 import org.apache.derby.iapi.reference.SQLState; 29 30 import org.apache.derby.iapi.store.access.conglomerate.Conglomerate; 31 import org.apache.derby.iapi.store.access.conglomerate.LogicalUndo; 32 import org.apache.derby.iapi.store.access.conglomerate.TransactionManager; 33 import org.apache.derby.iapi.store.access.ConglomerateController; 34 import org.apache.derby.iapi.store.access.DynamicCompiledOpenConglomInfo; 35 import org.apache.derby.iapi.store.access.Qualifier; 36 import org.apache.derby.iapi.store.access.ScanController; 37 import org.apache.derby.iapi.store.access.TransactionController; 38 import org.apache.derby.iapi.store.access.SpaceInfo; 39 import org.apache.derby.iapi.store.raw.ContainerHandle; 40 import org.apache.derby.iapi.store.raw.LockingPolicy; 41 import org.apache.derby.iapi.store.raw.RecordHandle; 42 import org.apache.derby.iapi.store.raw.Transaction; 43 44 import org.apache.derby.iapi.types.DataValueDescriptor; 45 46 import org.apache.derby.iapi.types.RowLocation; 47 48 import org.apache.derby.impl.store.access.conglomerate.OpenConglomerateScratchSpace; 49 50 51 52 63 64 public class OpenBTree 65 { 66 69 70 76 private BTree init_conglomerate; 77 78 84 private TransactionManager init_xact_manager; 85 86 private Transaction init_rawtran; 87 88 93 private int init_openmode; 94 95 98 protected int init_lock_level; 99 100 private DynamicCompiledOpenConglomInfo init_dynamic_info; 101 private boolean init_hold; 102 103 106 private BTreeLockingPolicy init_btree_locking_policy; 107 108 109 112 protected ContainerHandle container; 113 114 117 protected long err_containerid; 118 119 128 protected TransactionManager init_open_user_scans = null; 129 130 131 protected LogicalUndo btree_undo = null; 132 133 136 protected OpenConglomerateScratchSpace runtime_mem; 137 138 142 public final TransactionManager getXactMgr() 143 { 144 return(init_xact_manager); 145 } 146 147 public final Transaction getRawTran() 148 { 149 return(init_rawtran); 150 } 151 public final int getLockLevel() 152 { 153 return(init_lock_level); 154 } 155 156 public final ContainerHandle getContainer() 157 { 158 return(container); 159 } 160 161 public final int getOpenMode() 162 { 163 return(init_openmode); 164 } 165 166 public final BTree getConglomerate() 167 { 168 return(init_conglomerate); 169 } 170 171 public final boolean getHold() 172 { 173 return(init_hold); 174 } 175 public final BTreeLockingPolicy getLockingPolicy() 176 { 177 return(init_btree_locking_policy); 178 } 179 public final void setLockingPolicy(BTreeLockingPolicy policy) 180 { 181 init_btree_locking_policy = policy; 182 } 183 184 185 public final boolean isClosed() 186 { 187 return(container == null); 188 } 189 190 public final OpenConglomerateScratchSpace getRuntimeMem() 191 { 192 return(runtime_mem); 193 } 194 195 199 200 216 public long getEstimatedRowCount() 217 throws StandardException 218 { 219 if (container == null) 220 reopen(); 221 222 long row_count = 230 this.container.getEstimatedRowCount( 0); 231 232 return(row_count == 0 ? 1 : row_count); 233 } 234 235 253 public void setEstimatedRowCount(long count) 254 throws StandardException 255 { 256 if (container == null) 257 reopen(); 258 259 this.container.setEstimatedRowCount(count, 0); 260 } 261 262 263 267 268 279 public void checkConsistency() 280 throws StandardException 281 { 282 ControlRow root = null; 283 284 try 285 { 286 if (this.container == null) 287 { 288 throw(StandardException.newException( 289 SQLState.BTREE_IS_CLOSED, new Long (err_containerid))); 290 } 291 292 if (SanityManager.DEBUG) 293 SanityManager.ASSERT(this.init_conglomerate.format_ids != null); 294 295 root = ControlRow.Get(this, BTree.ROOTPAGEID); 296 297 int actualpages = root.checkConsistency(this, null, true); 298 299 } 302 finally 303 { 304 if (root != null) 305 root.release(); 306 } 307 } 308 309 313 314 317 public boolean isTableLocked() 318 { 319 return(init_lock_level == TransactionController.MODE_TABLE); 320 } 321 322 323 326 327 335 359 public void init( 360 TransactionManager open_user_scans, 361 TransactionManager xact_manager, 362 ContainerHandle input_container, 363 Transaction rawtran, 364 boolean hold, 365 int open_mode, 366 int lock_level, 367 BTreeLockingPolicy btree_locking_policy, 368 BTree conglomerate, 369 LogicalUndo undo, 370 DynamicCompiledOpenConglomInfo dynamic_info) 371 throws StandardException 372 { 373 if (this.container != null) 375 { 376 if (SanityManager.DEBUG) 377 SanityManager.ASSERT(false, "why is the container open?"); 378 close(); 379 } 380 err_containerid = conglomerate.id.getContainerId(); 381 382 this.init_btree_locking_policy = btree_locking_policy; 384 385 if (conglomerate.isTemporary()) 389 open_mode |= ContainerHandle.MODE_TEMP_IS_KEPT; 390 391 if (input_container == null) 394 { 395 this.container = 397 rawtran.openContainer( 398 conglomerate.id, 399 (LockingPolicy) null , 400 open_mode); 401 } 402 else 403 { 404 this.container = input_container; 406 407 } 410 411 if (this.container == null) 412 { 413 throw StandardException.newException( 414 SQLState.BTREE_CONTAINER_NOT_FOUND, 415 new Long (err_containerid)); 416 } 417 418 init_conglomerate = conglomerate; 420 421 init_xact_manager = xact_manager; 423 424 init_rawtran = rawtran; 425 426 init_openmode = open_mode; 427 428 init_lock_level = lock_level; 430 431 init_dynamic_info = dynamic_info; 432 433 init_hold = hold; 434 435 436 this.init_open_user_scans = open_user_scans; 438 439 this.btree_undo = undo; 441 442 this.runtime_mem = 445 (dynamic_info != null ? 446 ((OpenConglomerateScratchSpace) dynamic_info) : 447 new OpenConglomerateScratchSpace(conglomerate.format_ids)); 448 449 } 450 451 460 public ContainerHandle reopen() 461 throws StandardException 462 { 463 471 472 474 480 481 if (SanityManager.DEBUG) 482 { 483 SanityManager.ASSERT(init_xact_manager != null); 484 SanityManager.ASSERT(init_xact_manager.getRawStoreXact() != null); 485 SanityManager.ASSERT(init_conglomerate != null); 486 } 487 488 if (container == null) 489 { 490 this.container = 492 init_xact_manager.getRawStoreXact().openContainer( 493 init_conglomerate.id, 494 (LockingPolicy) null , 495 init_openmode); 496 } 497 498 return(this.container); 499 } 500 501 504 public void close() 505 throws StandardException 506 { 507 if (container != null) 508 container.close(); 509 container = null; 510 } 511 512 519 void isIndexableRowConsistent(DataValueDescriptor[] row) 520 throws StandardException 521 { 522 if (SanityManager.DEBUG) 523 { 524 DataValueDescriptor[] template = 525 this.init_conglomerate.createTemplate(); 526 527 530 for (int i = 0; i < row.length; i++) 531 { 532 if (!row[i].getClass().equals(template[i].getClass())) 534 { 535 SanityManager.THROWASSERT( 536 "type of inserted column[" + i + "] = " + 537 row[i].getClass().getName() + 538 "type of template column[" + i + "] = " + 539 template[i].getClass().getName()); 540 } 541 } 542 } 543 } 544 545 550 public ContainerHandle getContainerHandle() 551 { 552 return(container); 553 } 554 555 564 public int getHeight() 565 throws StandardException 566 { 567 569 ControlRow root = null; 570 571 try 572 { 573 root = ControlRow.Get(this, BTree.ROOTPAGEID); 574 575 int height = root.getLevel() + 1; 576 577 return(height); 578 } 579 finally 580 { 581 if (root != null) 582 root.release(); 583 } 584 } 585 586 public RecordHandle makeRecordHandle( 587 long page_number, 588 int rec_id) 589 throws StandardException 590 { 591 return( 592 container.makeRecordHandle( 593 page_number, rec_id)); 594 } 595 596 603 public void debugConglomerate() 604 throws StandardException 605 { 606 608 ControlRow root = null; 609 610 try 611 { 612 if (SanityManager.DEBUG) 613 { 614 SanityManager.DEBUG_PRINT( 615 "p_tree", "BTREE Dump: containerId " + container.getId()); 616 SanityManager.DEBUG_PRINT( 617 "p_tree", "BTREE Dump: btree " + this.init_conglomerate); 618 } 619 620 root = ControlRow.Get(this, BTree.ROOTPAGEID); 621 root.printTree(this); 622 } 623 finally 624 { 625 if (root != null) 626 root.release(); 627 } 628 } 629 630 641 public static boolean test_errors( 642 OpenBTree open_btree, 643 String debug_string, 644 boolean release_scan_lock, 645 BTreeLockingPolicy btree_locking_policy, 646 LeafControlRow leaf, 647 boolean input_latch_released) 648 throws StandardException 649 { 650 boolean latch_released = input_latch_released; 651 652 if (SanityManager.DEBUG) 654 { 655 String debug_lost_latch = debug_string + "1"; 656 657 if (SanityManager.DEBUG_ON(debug_lost_latch)) 658 { 659 if (!latch_released) 661 { 662 if (release_scan_lock) 663 { 664 btree_locking_policy.unlockScan( 665 leaf.page.getPageNumber()); 666 } 667 leaf.release(); 668 669 latch_released = true; 670 SanityManager.DEBUG_PRINT( 671 debug_lost_latch, debug_lost_latch); 672 SanityManager.DEBUG_CLEAR(debug_lost_latch); 673 } 674 } 675 676 String debug_deadlock = debug_string + "2"; 677 678 if (SanityManager.DEBUG_ON(debug_deadlock)) 679 { 680 SanityManager.DEBUG_PRINT(debug_deadlock, debug_deadlock); 681 SanityManager.DEBUG_CLEAR(debug_deadlock); 682 683 StandardException se = 685 StandardException.newException( 686 SQLState.DEADLOCK, "fake deadlock", "fake victim"); 687 688 se.setReport(StandardException.REPORT_ALWAYS); 689 throw se; 690 } 691 } 692 693 return(latch_released); 694 } 695 696 public SpaceInfo getSpaceInfo() 697 throws StandardException 698 { 699 return container.getSpaceInfo(); 700 } 701 702 public boolean[] getColumnSortOrderInfo() 704 throws StandardException 705 { 706 return init_conglomerate.ascDescInfo; 707 } 708 } 709 | Popular Tags |