1 21 22 package org.apache.derby.impl.store.access.conglomerate; 23 24 import org.apache.derby.iapi.reference.SQLState; 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.store.access.conglomerate.Conglomerate; 31 import org.apache.derby.iapi.store.access.conglomerate.TransactionManager; 32 33 import org.apache.derby.iapi.store.access.ConglomPropertyQueryable; 34 import org.apache.derby.iapi.store.access.ConglomerateController; 35 import org.apache.derby.iapi.store.access.DynamicCompiledOpenConglomInfo; 36 import org.apache.derby.iapi.store.access.Qualifier; 37 import org.apache.derby.iapi.store.access.RowUtil; 38 import org.apache.derby.iapi.store.access.SpaceInfo; 39 import org.apache.derby.iapi.store.access.TransactionController; 40 41 import org.apache.derby.iapi.store.raw.ContainerHandle; 42 import org.apache.derby.iapi.store.raw.FetchDescriptor; 43 import org.apache.derby.iapi.store.raw.LockingPolicy; 44 import org.apache.derby.iapi.store.raw.Page; 45 import org.apache.derby.iapi.store.raw.RecordHandle; 46 import org.apache.derby.iapi.store.raw.Transaction; 47 48 import org.apache.derby.iapi.types.DataValueDescriptor; 49 50 import org.apache.derby.iapi.types.RowLocation; 51 52 import java.util.Properties ; 53 54 55 65 66 public abstract class OpenConglomerate 67 { 68 72 73 79 private Conglomerate init_conglomerate; 80 private TransactionManager init_xact_manager; 81 private Transaction init_rawtran; 82 private int init_openmode; 83 private int init_lock_level; 84 private DynamicCompiledOpenConglomInfo init_dynamic_info; 85 private boolean init_hold; 86 private LockingPolicy init_locking_policy; 87 88 89 92 private boolean useUpdateLocks; 93 private boolean forUpdate; 94 private boolean getBaseTableLocks; 95 96 99 private OpenConglomerateScratchSpace runtime_mem; 100 101 102 105 private ContainerHandle container; 106 107 111 112 116 117 121 122 130 protected abstract RowLocation newRowLocationTemplate() 131 throws StandardException; 132 133 abstract public int[] getFormatIds(); 134 135 136 145 169 public boolean latchPageAndRepositionScan(RowPosition pos) 170 throws StandardException 171 { 172 boolean scan_repositioned = false; 173 174 pos.current_page = null; 176 177 try 178 { 179 if (pos.current_rh != null) 180 { 181 pos.current_page = 182 container.getPage(pos.current_rh.getPageNumber()); 183 } 184 185 } 186 catch (Throwable t) 187 { 188 194 199 } 203 204 if (pos.current_page != null) 205 { 206 try 207 { 208 pos.current_slot = 210 pos.current_page.getSlotNumber(pos.current_rh); 211 } 212 catch (StandardException se) 213 { 214 scan_repositioned = true; 215 216 pos.current_slot = 223 pos.current_page.getNextSlotNumber(pos.current_rh); 224 225 if (pos.current_slot == -1) 226 { 227 233 pos.current_page.unlatch(); 234 pos.current_page = null; 235 } 236 else 237 { 238 pos.current_slot--; 242 } 243 } 244 } 245 246 if (pos.current_page == null) 247 { 248 long current_pageno; 250 251 if (pos.current_rh != null) 252 { 253 current_pageno = pos.current_rh.getPageNumber(); 254 } 255 else if (pos.current_pageno != ContainerHandle.INVALID_PAGE_NUMBER) 256 { 257 current_pageno = pos.current_pageno; 258 } 259 else 260 { 261 return(false); 263 } 264 265 pos.current_page = container.getNextPage(current_pageno); 266 267 pos.current_slot = Page.FIRST_SLOT_NUMBER - 1; 268 269 pos.current_pageno = ContainerHandle.INVALID_PAGE_NUMBER; 271 272 scan_repositioned = true; 273 } 274 275 if (scan_repositioned) 276 { 277 pos.current_rh = null; 278 } 279 280 return(scan_repositioned); 281 } 282 283 292 public boolean latchPage(RowPosition pos) 293 throws StandardException 294 { 295 pos.current_page = null; 296 297 try 298 { 299 pos.current_page = 300 container.getPage(pos.current_rh.getPageNumber()); 301 302 } 303 catch (Throwable t) 304 { 305 } 310 311 if (pos.current_page != null) 312 { 313 try 314 { 315 pos.current_slot = 316 pos.current_page.getSlotNumber(pos.current_rh); 317 318 return(true); 319 } 320 catch (Throwable t) 321 { 322 327 pos.current_page.unlatch(); 328 pos.current_page = null; 329 } 330 } 331 332 return(false); 333 } 334 335 336 376 public boolean lockPositionForRead( 377 RowPosition pos, 378 RowPosition aux_pos, 379 boolean moveForwardIfRowDisappears, 380 boolean waitForLock) 381 throws StandardException 382 { 383 if (pos.current_rh == null) 384 { 385 if (SanityManager.DEBUG) 386 { 387 SanityManager.ASSERT( 388 pos.current_page != null && 389 pos.current_slot != Page.INVALID_SLOT_NUMBER); 390 391 } 392 393 pos.current_rh = 396 pos.current_page.getRecordHandleAtSlot(pos.current_slot); 397 398 if (SanityManager.DEBUG) 399 { 400 if (pos.current_slot != 402 pos.current_page.getSlotNumber(pos.current_rh)) 403 { 404 SanityManager.THROWASSERT( 405 "current_slot = " + pos.current_slot + 406 "current_rh = " + pos.current_rh + 407 "current_rh.slot = " + 408 pos.current_page.getSlotNumber(pos.current_rh)); 409 } 410 } 411 } 412 413 if (SanityManager.DEBUG) 414 SanityManager.ASSERT(pos.current_rh != null); 415 416 boolean lock_granted_with_latch_held = 417 this.container.getLockingPolicy().lockRecordForRead( 418 init_rawtran, container, pos.current_rh, 419 false , forUpdate); 420 421 if (!lock_granted_with_latch_held) 422 { 423 424 pos.current_page.unlatch(); 426 pos.current_page = null; 427 428 429 if (aux_pos != null) 430 { 431 aux_pos.current_page.unlatch(); 432 aux_pos.current_page = null; 433 } 434 435 if (!waitForLock) 436 { 437 throw StandardException.newException(SQLState.LOCK_TIMEOUT); 439 } 440 441 this.container.getLockingPolicy().lockRecordForRead( 442 init_rawtran, container, pos.current_rh, 443 true , forUpdate); 444 445 if (moveForwardIfRowDisappears) 446 { 447 448 if (latchPageAndRepositionScan(pos)) 449 { 450 if (pos.current_slot != -1) 451 { 452 463 pos.positionAtNextSlot(); 464 lockPositionForRead(pos, aux_pos, true, true); 465 466 } 467 } 468 } 469 else 470 { 471 latchPage(pos); 472 } 473 } 474 475 return(lock_granted_with_latch_held); 476 } 477 478 public boolean lockPositionForWrite( 479 RowPosition pos, 480 boolean forInsert, 481 boolean waitForLock) 482 throws StandardException 483 { 484 if (pos.current_rh == null) 485 { 486 if (SanityManager.DEBUG) 487 { 488 SanityManager.ASSERT(pos.current_page != null); 489 SanityManager.ASSERT( 490 pos.current_slot != Page.INVALID_SLOT_NUMBER); 491 492 } 493 494 pos.current_rh = 497 pos.current_page.fetchFromSlot( 498 null, 499 pos.current_slot, 500 RowUtil.EMPTY_ROW, 501 RowUtil.EMPTY_ROW_FETCH_DESCRIPTOR, 502 true); 503 504 if (SanityManager.DEBUG) 505 { 506 if (pos.current_slot != 508 pos.current_page.getSlotNumber(pos.current_rh)) 509 { 510 SanityManager.THROWASSERT( 511 "current_slot = " + pos.current_slot + 512 "current_rh = " + pos.current_rh + 513 "current_rh.slot = " + 514 pos.current_page.getSlotNumber(pos.current_rh)); 515 } 516 } 517 } 518 519 if (SanityManager.DEBUG) 520 SanityManager.ASSERT(pos.current_rh != null); 521 522 boolean lock_granted_with_latch_held = 523 this.container.getLockingPolicy(). 524 lockRecordForWrite( 525 init_rawtran, pos.current_rh, 526 forInsert, false ); 527 528 if (!lock_granted_with_latch_held) 529 { 530 if (!waitForLock) 531 { 532 throw StandardException.newException(SQLState.LOCK_TIMEOUT); 534 } 535 536 pos.current_page.unlatch(); 538 pos.current_page = null; 539 540 if (!waitForLock) 541 { 542 throw StandardException.newException(SQLState.LOCK_TIMEOUT); 544 } 545 546 this.container.getLockingPolicy(). 547 lockRecordForWrite( 548 init_rawtran, pos.current_rh, forInsert, true ); 549 550 latchPage(pos); 551 } 552 553 return(lock_granted_with_latch_held); 554 } 555 556 557 571 public void unlockPositionAfterRead( 572 RowPosition pos) 573 throws StandardException 574 { 575 if (!isClosed()) 576 container.getLockingPolicy(). 577 unlockRecordAfterRead( 578 init_rawtran, container, pos.current_rh, forUpdate, 579 pos.current_rh_qualified); 580 } 581 582 583 587 588 613 public Properties getInternalTablePropertySet(Properties prop) 614 throws StandardException 615 { 616 Properties ret_properties = 617 ConglomerateUtil.createRawStorePropertySet(prop); 618 619 getTableProperties(ret_properties); 620 621 return(ret_properties); 622 } 623 624 653 public void getTableProperties(Properties prop) 654 throws StandardException 655 { 656 container.getContainerProperties(prop); 657 658 return; 659 } 660 661 665 public final TransactionManager getXactMgr() 666 { 667 return(init_xact_manager); 668 } 669 670 public final Transaction getRawTran() 671 { 672 return(init_rawtran); 673 } 674 675 public final ContainerHandle getContainer() 676 { 677 return(container); 678 } 679 680 public final int getOpenMode() 681 { 682 return(init_openmode); 683 } 684 685 public final Conglomerate getConglomerate() 686 { 687 return(init_conglomerate); 688 } 689 690 public final boolean getHold() 691 { 692 return(init_hold); 693 } 694 695 696 public final boolean isForUpdate() 697 { 698 return(forUpdate); 699 } 700 701 public final boolean isClosed() 702 { 703 return(container == null); 704 } 705 706 public final boolean isUseUpdateLocks() 707 { 708 return(useUpdateLocks); 709 } 710 711 public final OpenConglomerateScratchSpace getRuntimeMem() 712 { 713 return(runtime_mem); 714 } 715 716 720 721 722 734 public void checkConsistency() 735 throws StandardException 736 { 737 return; 738 } 739 740 741 742 public void debugConglomerate() 743 throws StandardException 744 { 745 if (SanityManager.DEBUG) 746 { 747 SanityManager.DEBUG_PRINT( 748 "p_heap", "\nHEAP DUMP:containerId " + container.getId()); 749 750 752 DataValueDescriptor[] row = runtime_mem.get_row_for_export(); 753 754 Page page = container.getFirstPage(); 756 757 while (page != null) 758 { 759 SanityManager.DEBUG_PRINT( 760 "p_heap", ConglomerateUtil.debugPage(page, 0, false, row)); 761 762 long pageid = page.getPageNumber(); 763 page.unlatch(); 764 page = container.getNextPage(pageid); 765 } 766 } 767 768 return; 769 } 770 771 772 775 public SpaceInfo getSpaceInfo() 776 throws StandardException 777 { 778 return container.getSpaceInfo(); 779 } 780 781 protected boolean isKeyed() 782 { 783 return false; 784 } 785 786 789 protected boolean isTableLocked() 790 { 791 return(init_lock_level == TransactionController.MODE_TABLE); 792 } 793 794 798 799 807 public ContainerHandle init( 808 ContainerHandle open_container, 809 Conglomerate conglomerate, 810 int[] format_ids, 811 TransactionManager xact_manager, 812 Transaction rawtran, 813 boolean hold, 814 int openmode, 815 int lock_level, 816 LockingPolicy locking_policy, 817 DynamicCompiledOpenConglomInfo dynamic_info) 818 throws StandardException 819 { 820 init_conglomerate = conglomerate; 822 init_xact_manager = xact_manager; 823 init_rawtran = rawtran; 824 init_openmode = openmode; 825 init_lock_level = lock_level; 826 init_dynamic_info = dynamic_info; 827 init_hold = hold; 828 init_locking_policy = locking_policy; 829 830 831 this.runtime_mem = 834 (dynamic_info != null ? 835 ((OpenConglomerateScratchSpace) dynamic_info) : 836 new OpenConglomerateScratchSpace(format_ids)); 837 838 this.forUpdate = 842 ((openmode & ContainerHandle.MODE_FORUPDATE) != 0); 843 844 this.useUpdateLocks = 846 ((openmode & ContainerHandle.MODE_USE_UPDATE_LOCKS) != 0); 847 848 this.getBaseTableLocks = 852 ((openmode & ContainerHandle.MODE_SECONDARY_LOCKED) == 0); 853 854 if (conglomerate.isTemporary()) 858 { 859 init_openmode |= ContainerHandle.MODE_TEMP_IS_KEPT; 860 } 861 862 if (!getBaseTableLocks) 863 init_locking_policy = null; 864 865 this.container = 867 (open_container != null ? 868 open_container : 869 rawtran.openContainer( 870 conglomerate.getId(), init_locking_policy, init_openmode)); 871 872 return(this.container); 873 } 874 875 884 public ContainerHandle reopen() 885 throws StandardException 886 { 887 895 896 if (this.container == null) 897 { 898 this.container = 899 init_rawtran.openContainer( 900 init_conglomerate.getId(), 901 init_locking_policy, 902 init_openmode); 903 } 904 905 return(this.container); 906 } 907 908 915 public void close() 916 throws StandardException 917 { 918 if (container != null) 919 { 920 container.close(); 921 container = null; 922 } 923 } 924 } 925 | Popular Tags |