1 21 22 package org.apache.derby.impl.store.raw.data; 23 24 import org.apache.derby.iapi.reference.SQLState; 25 26 import org.apache.derby.iapi.services.locks.Lockable; 27 import org.apache.derby.iapi.services.locks.VirtualLockTable; 28 import org.apache.derby.iapi.services.sanity.SanityManager; 29 import org.apache.derby.iapi.error.StandardException; 30 import org.apache.derby.iapi.store.access.SpaceInfo; 31 import org.apache.derby.iapi.store.raw.ContainerHandle; 32 import org.apache.derby.iapi.store.raw.ContainerLock; 33 import org.apache.derby.iapi.store.raw.LockingPolicy; 34 import org.apache.derby.iapi.store.raw.Page; 35 import org.apache.derby.iapi.store.raw.PageKey; 36 import org.apache.derby.iapi.store.raw.PageTimeStamp; 37 import org.apache.derby.iapi.store.raw.RecordHandle; 38 import org.apache.derby.iapi.store.raw.ContainerKey; 39 40 import org.apache.derby.iapi.store.raw.data.RawContainerHandle; 41 import org.apache.derby.iapi.store.raw.xact.RawTransaction; 42 import org.apache.derby.iapi.store.raw.log.LogInstant; 43 44 import org.apache.derby.iapi.util.ByteArray; 45 import org.apache.derby.catalog.UUID; 46 47 import java.util.Hashtable ; 48 import java.util.Observable ; 49 import java.util.Observer ; 50 import java.util.Properties ; 51 52 64 65 public class BaseContainerHandle extends Observable 66 implements RawContainerHandle, Observer 67 { 68 69 72 73 77 protected ContainerKey identity; 78 79 84 protected boolean active; 85 86 91 protected BaseContainer container; 92 93 99 100 private LockingPolicy locking; 101 102 107 protected RawTransaction xact; 108 109 114 private boolean forUpdate; 115 116 protected int mode; 118 119 protected PageActions actionsSet; 120 protected AllocationActions allocActionsSet; 121 122 123 126 127 130 public BaseContainerHandle(UUID rawStoreId, RawTransaction xact, 131 ContainerKey identity, LockingPolicy locking, int mode) 132 { 133 this.identity = identity; 134 this.xact = xact; 135 this.locking = locking; 136 this.mode = mode; 137 this.forUpdate = (mode & MODE_FORUPDATE) == MODE_FORUPDATE; 138 } 139 140 143 public BaseContainerHandle( 144 UUID rawStoreId, 145 RawTransaction xact, 146 PageActions actionsSet, 147 AllocationActions allocActionsSet, 148 LockingPolicy locking, 149 BaseContainer container, 150 int mode) 151 { 152 this(rawStoreId, xact, 153 (ContainerKey) container.getIdentity(), locking, mode); 154 155 156 this.actionsSet = actionsSet; 157 this.allocActionsSet = allocActionsSet; 158 this.container = container; 159 160 } 162 163 166 167 175 public Page addPage() throws StandardException 176 { 177 checkUpdateOpen(); 178 179 Page page = container.addPage(this, false ); 180 181 return page; 182 } 183 184 193 public void compressContainer() throws StandardException 194 { 195 checkUpdateOpen(); 196 197 container.compressContainer(this); 198 } 199 200 206 public long getReusableRecordIdSequenceNumber() throws StandardException 207 { 208 checkOpen(); 209 210 return container.getReusableRecordIdSequenceNumber(); 211 } 212 213 223 public Page addPage(int flag) throws StandardException { 224 225 if ((flag & ContainerHandle.ADD_PAGE_BULK) != 0 && active && forUpdate) 226 { 227 container.clearPreallocThreshold(); 231 } 232 233 return addPage(); 234 } 235 236 239 public void preAllocate(int numPage) 240 { 241 if (numPage > 0 && active && forUpdate) 242 container.prepareForBulkLoad(this, numPage); 243 } 244 245 273 public void getContainerProperties(Properties prop) 274 throws StandardException 275 { 276 checkOpen(); 277 278 container.getContainerProperties(prop); 279 280 return; 281 } 282 283 289 public void removePage(Page page) throws StandardException 290 { 291 if (!active) 292 { 293 if (page != null) 294 page.unlatch(); 295 throw StandardException.newException( 296 SQLState.DATA_CONTAINER_CLOSED); 297 } 298 299 if (!forUpdate) 300 { 301 if (page != null) 302 page.unlatch(); 303 throw StandardException.newException( 304 SQLState.DATA_CONTAINER_READ_ONLY); 305 } 306 307 container.removePage(this, (BasePage)page); 308 } 309 310 public Page getPage(long pageNumber) throws StandardException 311 { 312 313 checkOpen(); 314 315 return container.getPage(this, pageNumber, true); 316 } 317 318 319 public Page getAllocPage(long pageNumber) throws StandardException 320 { 321 checkOpen(); 322 323 return container.getAllocPage(this, pageNumber, true); 324 } 325 326 public Page getUserPageNoWait(long pageNumber) 327 throws StandardException 328 { 329 checkOpen(); 330 331 return container.getHeadPage(this, pageNumber, false); 332 } 333 public Page getUserPageWait(long pageNumber) 334 throws StandardException 335 { 336 checkOpen(); 337 338 return container.getHeadPage(this, pageNumber, true); 339 } 340 341 public Page getPageNoWait(long pageNumber) 342 throws StandardException 343 { 344 checkOpen(); 345 346 return container.getPage(this, pageNumber, false); 347 } 348 349 public Page getFirstPage() throws StandardException 350 { 351 checkOpen(); 352 353 return container.getFirstPage(this); 354 } 355 356 public Page getNextPage(long pageNumber) throws StandardException 357 { 358 checkOpen(); 359 360 return container.getNextPage(this, pageNumber); 361 } 362 363 public Page getPageForInsert(int flag) 364 throws StandardException 365 { 366 checkUpdateOpen(); 367 368 return container.getPageForInsert(this, flag); 369 } 370 371 public Page getPageForCompress(int flag, long pageno) 372 throws StandardException 373 { 374 checkUpdateOpen(); 375 376 return container.getPageForCompress(this, flag, pageno); 377 } 378 379 382 public final boolean isReadOnly() 383 { 384 return(!forUpdate); 385 } 386 387 390 391 public void close() 392 { 393 394 if (xact == null) 395 { 396 399 if (SanityManager.DEBUG) 400 SanityManager.ASSERT(!active); 401 402 return; 403 } 404 405 informObservers(); 407 408 active = false; 409 410 getLockingPolicy().unlockContainer(xact, this); 411 412 if (container != null) 414 { 415 container.letGo(this); 416 container = null; 417 } 418 419 xact.deleteObserver(this); 421 422 xact = null; 423 424 } 425 426 427 428 432 public long getEstimatedRowCount(int flag) throws StandardException 433 { 434 checkOpen(); 435 436 return container.getEstimatedRowCount(flag); 437 } 438 439 443 public void setEstimatedRowCount(long count, int flag) 444 throws StandardException 445 { 446 checkOpen(); 447 448 container.setEstimatedRowCount(count, flag); 449 } 450 451 455 public long getEstimatedPageCount(int flag) 456 throws StandardException 457 { 458 checkOpen(); 459 460 return container.getEstimatedPageCount(this, flag); 461 } 462 463 467 public void flushContainer() 468 throws StandardException 469 { 470 checkUpdateOpen(); 471 472 477 container.flushAll(); 478 479 } 480 481 485 public void compactRecord(RecordHandle record) 486 throws StandardException 487 { 488 if (!forUpdate) 489 { 490 throw StandardException.newException( 491 SQLState.DATA_CONTAINER_READ_ONLY); 492 } 493 494 PageKey pkey = (PageKey)record.getPageId(); 495 BasePage headPage = (BasePage)getPage(pkey.getPageNumber()); 496 497 if (headPage != null) 498 { 499 try 503 { 504 headPage.compactRecord(record); 505 } 506 finally 507 { 508 headPage.unlatch(); 509 } 510 } 511 } 512 513 514 517 518 524 public int getContainerStatus() throws StandardException 525 { 526 checkOpen(); 527 528 return container.getContainerStatus(); 529 } 530 531 537 public void removeContainer(LogInstant instant) throws StandardException 538 { 539 checkUpdateOpen(); 540 541 548 container.removeContainer(instant, true); 549 } 550 551 554 public ContainerKey getId() 555 { 556 return identity; 557 } 558 559 562 public Object getUniqueId() 563 { 564 return(this); 565 } 566 567 568 572 public void dropContainer(LogInstant instant, boolean drop) 573 throws StandardException 574 { 575 checkUpdateOpen(); 576 577 container.dropContainer(instant, drop); 578 } 579 580 584 public long getContainerVersion() 585 throws StandardException 586 { 587 checkOpen(); 588 589 return container.getContainerVersion(); 590 } 591 592 593 600 public Page getAnyPage(long pageNumber) throws StandardException 601 { 602 checkOpen(); 603 604 return container.getAnyPage(this, pageNumber, true ); 605 } 606 607 638 public Page reCreatePageForRedoRecovery( 639 int pageFormat, 640 long pageNumber, 641 long pageOffset) 642 throws StandardException 643 { 644 checkUpdateOpen(); 645 646 return container.reCreatePageForRedoRecovery( 647 this, pageFormat, pageNumber, pageOffset); 648 } 649 650 656 public ByteArray logCreateContainerInfo() 657 throws StandardException 658 { 659 checkUpdateOpen(); 660 661 return container.logCreateContainerInfo(); 662 } 663 664 675 public RecordHandle makeRecordHandle(long pageNumber, int recordId) 676 throws StandardException 677 { 678 return new RecordId(identity, pageNumber, recordId); 679 } 680 681 682 685 686 691 public void update(Observable obj, Object arg) 692 { 693 if (SanityManager.DEBUG) 694 { 695 if (arg == null) 696 SanityManager.THROWASSERT("still on observr list " + this); 697 } 698 699 if (xact == null) 701 { 702 return; 703 } 704 705 if (SanityManager.DEBUG) 706 { 707 709 if (obj != xact) 710 { 711 SanityManager.THROWASSERT( 712 "Observable passed to update is incorrect expected " + 713 xact + " got " + obj); 714 } 715 } 716 717 if (arg.equals(RawTransaction.COMMIT) || 719 arg.equals(RawTransaction.ABORT) || 720 arg.equals(identity)) 721 { 722 close(); 724 return; 725 726 } 727 728 if (arg.equals(RawTransaction.SAVEPOINT_ROLLBACK)) 729 { 730 731 informObservers(); 733 734 return; 736 } 737 738 743 if (arg.equals(RawTransaction.LOCK_ESCALATE)) 744 { 745 746 if (getLockingPolicy().getMode() != LockingPolicy.MODE_RECORD) 748 return; 749 750 try 751 { 752 getLockingPolicy().lockContainer( 753 getTransaction(), this, false, forUpdate); 754 } 755 catch (StandardException se) 756 { 757 xact.setObserverException(se); 758 } 759 } 760 } 761 762 769 770 public PageActions getActionSet() 771 { 772 return actionsSet; 773 } 774 775 public AllocationActions getAllocationActionSet() 776 { 777 return allocActionsSet; 778 } 779 780 791 public boolean useContainer( 792 boolean droppedOK, 793 boolean waitForLock) 794 throws StandardException 795 { 796 797 if (SanityManager.DEBUG) 798 { 799 SanityManager.ASSERT(!active); 800 } 801 802 boolean gotLock = 803 getLockingPolicy().lockContainer( 804 getTransaction(), this, waitForLock, forUpdate); 805 806 if (gotLock == false) 807 { 808 if (SanityManager.DEBUG) 811 SanityManager.ASSERT(waitForLock == false, 812 "lockContainer wait returns false"); 813 814 container = null; 815 816 throw StandardException.newException(SQLState.LOCK_TIMEOUT); 817 } 818 819 if ((mode & ContainerHandle.MODE_OPEN_FOR_LOCK_ONLY) == 0) 820 { 821 822 if (SanityManager.DEBUG) 823 { 824 SanityManager.ASSERT(container != null); 825 } 826 827 if (!container.use(this, forUpdate, droppedOK)) 828 { 829 830 getLockingPolicy().unlockContainer(xact, this); 837 838 container = null; 839 840 return false; 841 } 842 active = true; 843 } 844 else 845 { 846 if (getLockingPolicy().getMode() != LockingPolicy.MODE_RECORD) 849 return true; 850 } 851 852 xact.addObserver(this); 854 855 856 858 if ((mode & (ContainerHandle.MODE_READONLY | 859 ContainerHandle.MODE_NO_ACTIONS_ON_COMMIT)) == 0) 860 { 861 if ((mode & MODE_TRUNCATE_ON_COMMIT) == MODE_TRUNCATE_ON_COMMIT) 862 { 863 xact.addObserver( 864 new TruncateOnCommit(identity, true )); 865 } 866 else if ((mode & MODE_TRUNCATE_ON_ROLLBACK) == 867 MODE_TRUNCATE_ON_ROLLBACK) 868 { 869 xact.addObserver( 870 new TruncateOnCommit(identity, false )); 871 } 872 873 if ((mode & MODE_DROP_ON_COMMIT) == MODE_DROP_ON_COMMIT) 874 { 875 xact.addObserver(new DropOnCommit(identity)); 876 } 877 878 if ((mode & MODE_FLUSH_ON_COMMIT) == MODE_FLUSH_ON_COMMIT) 879 { 880 xact.addObserver(new SyncOnCommit(identity)); 881 } 882 } 883 884 return true; 885 } 886 887 890 public final RawTransaction getTransaction() 891 { 892 return xact; 893 } 894 895 899 public final LockingPolicy getLockingPolicy() 900 { 901 902 if (SanityManager.DEBUG) 903 { 904 SanityManager.ASSERT(locking != null); 905 } 906 907 return locking; 908 } 909 910 public final void setLockingPolicy(LockingPolicy newLockingPolicy) 911 { 912 locking = newLockingPolicy; 913 } 914 915 921 public final boolean updateOK() 922 { 923 return forUpdate; 924 } 925 926 929 public int getMode() 930 { 931 return mode; 932 } 933 934 942 public void preDirty(boolean preDirtyOn) throws StandardException 943 { 944 945 checkUpdateOpen(); 946 947 container.preDirty(preDirtyOn); 948 949 } 950 951 955 public boolean isTemporaryContainer() throws StandardException 956 { 957 958 checkOpen(); 959 960 return (identity != null && 961 identity.getSegmentId() == ContainerHandle.TEMPORARY_SEGMENT); 962 } 963 964 967 968 protected void checkOpen() throws StandardException 969 { 970 if (!active) 971 throw StandardException.newException( 972 SQLState.DATA_CONTAINER_CLOSED); 973 } 974 975 976 private void checkUpdateOpen() throws StandardException 977 { 978 979 if (!active) 980 { 981 throw StandardException.newException( 982 SQLState.DATA_CONTAINER_CLOSED); 983 } 984 985 if (!forUpdate) 986 { 987 throw StandardException.newException( 988 SQLState.DATA_CONTAINER_READ_ONLY); 989 } 990 } 991 992 protected void informObservers() 993 { 994 995 998 if (countObservers() != 0) 999 { 1000 setChanged(); 1001 notifyObservers(); 1002 } 1003 } 1004 1005 1006 1009 public SpaceInfo getSpaceInfo() 1010 throws StandardException 1011 { 1012 return container.getSpaceInfo(this); 1013 } 1014 1015 1016 1021 public void backupContainer(String backupContainerPath) throws StandardException 1022 { 1023 checkOpen(); 1024 container.backupContainer(this, backupContainerPath); 1025 } 1026 1027 1028 1029 1035 public void encryptContainer(String newFilePath) throws StandardException 1036 { 1037 checkOpen(); 1038 container.encryptContainer(this, newFilePath); 1039 } 1040 1041 1042 public String toString() 1043 { 1044 if (SanityManager.DEBUG) 1045 { 1046 String str = new String (); 1047 str += "BaseContainerHandle:(" + identity.toString() + ")"; 1048 return(str); 1049 } 1050 else 1051 { 1052 return(super.toString()); 1053 } 1054 } 1055 1056} 1057 | Popular Tags |