1 21 22 package org.apache.derby.impl.store.access.btree.index; 23 24 25 import java.io.ObjectOutput ; 26 import java.io.ObjectInput ; 27 import java.io.IOException ; 28 import java.util.Properties ; 29 30 import org.apache.derby.iapi.reference.SQLState; 31 32 import org.apache.derby.iapi.services.io.ArrayInputStream; 33 import org.apache.derby.iapi.services.io.FormatableBitSet; 34 35 import org.apache.derby.iapi.services.sanity.SanityManager; 36 import org.apache.derby.iapi.error.StandardException; 37 import org.apache.derby.iapi.store.access.conglomerate.Conglomerate; 38 import org.apache.derby.iapi.store.access.conglomerate.LogicalUndo; 39 import org.apache.derby.iapi.store.access.conglomerate.ScanManager; 40 import org.apache.derby.iapi.store.access.conglomerate.TransactionManager; 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.Qualifier; 44 import org.apache.derby.iapi.types.RowLocation; 45 import org.apache.derby.iapi.store.access.RowLocationRetRowSource; 46 import org.apache.derby.iapi.store.access.ScanController; 47 import org.apache.derby.iapi.store.access.StaticCompiledOpenConglomInfo; 48 import org.apache.derby.iapi.store.access.StoreCostController; 49 import org.apache.derby.iapi.store.access.TransactionController; 50 import org.apache.derby.iapi.store.access.ColumnOrdering; 51 52 import org.apache.derby.iapi.services.io.FormatIdUtil; 53 import org.apache.derby.iapi.services.io.StoredFormatIds; 54 55 import org.apache.derby.iapi.store.raw.ContainerHandle; 56 import org.apache.derby.iapi.store.raw.LockingPolicy; 57 import org.apache.derby.iapi.store.raw.Page; 58 import org.apache.derby.iapi.store.raw.RecordHandle; 59 import org.apache.derby.iapi.store.raw.Transaction; 60 61 import org.apache.derby.iapi.types.DataValueDescriptor; 62 63 import org.apache.derby.impl.store.access.btree.BTree; 64 import org.apache.derby.impl.store.access.btree.BTreeLockingPolicy; 65 import org.apache.derby.impl.store.access.btree.LeafControlRow; 66 import org.apache.derby.impl.store.access.btree.ControlRow; 67 import org.apache.derby.impl.store.access.btree.OpenBTree; 68 import org.apache.derby.impl.store.access.btree.WaitError; 69 70 import org.apache.derby.impl.store.access.conglomerate.ConglomerateUtil; 71 import org.apache.derby.iapi.services.io.FormatableBitSet; 72 import org.apache.derby.iapi.services.cache.ClassSize; 73 74 95 96 125 126 144 public class B2I extends BTree 145 { 146 public static final String PROPERTY_BASECONGLOMID = "baseConglomerateId"; 147 public static final String PROPERTY_ROWLOCCOLUMN = "rowLocationColumn"; 148 149 public static final int FORMAT_NUMBER = StoredFormatIds.ACCESS_B2I_V3_ID; 150 151 154 155 161 protected long baseConglomerateId; 162 163 171 protected int rowLocationColumn; 172 173 private static final int BASE_MEMORY_USAGE = ClassSize.estimateBaseFromCatalog( B2I.class); 174 175 public int estimateMemoryUsage() 176 { 177 return BASE_MEMORY_USAGE; 178 } 179 180 184 185 191 192 197 protected BTreeLockingPolicy getBtreeLockingPolicy( 198 Transaction rawtran, 199 int lock_level, 200 int mode, 201 int isolation_level, 202 ConglomerateController base_cc, 203 OpenBTree open_btree) 204 throws StandardException 205 { 206 BTreeLockingPolicy ret_locking_policy = null; 207 208 if (SanityManager.DEBUG) 209 { 210 SanityManager.ASSERT( 211 (isolation_level == 212 TransactionController.ISOLATION_SERIALIZABLE) || 213 (isolation_level == 214 TransactionController.ISOLATION_REPEATABLE_READ) || 215 (isolation_level == 216 TransactionController.ISOLATION_READ_COMMITTED_NOHOLDLOCK) || 217 (isolation_level == 218 TransactionController.ISOLATION_READ_COMMITTED) || 219 (isolation_level == 220 TransactionController.ISOLATION_READ_UNCOMMITTED), 221 "bad isolation_level = " + isolation_level); 222 } 223 224 if (lock_level == TransactionController.MODE_TABLE) 225 { 226 ret_locking_policy = 227 new B2ITableLocking3( 228 rawtran, 229 lock_level, 230 rawtran.newLockingPolicy( 231 LockingPolicy.MODE_CONTAINER, 232 isolation_level, 233 true), 234 base_cc, 235 open_btree); 236 } 237 else if (lock_level == TransactionController.MODE_RECORD) 238 { 239 if (isolation_level == TransactionController.ISOLATION_SERIALIZABLE) 240 { 241 ret_locking_policy = 242 new B2IRowLocking3( 243 rawtran, 244 lock_level, 245 rawtran.newLockingPolicy( 246 LockingPolicy.MODE_RECORD, 247 isolation_level, 248 true), 249 base_cc, 250 open_btree); 251 } 252 else if ((isolation_level == 253 TransactionController.ISOLATION_REPEATABLE_READ)) 254 { 255 ret_locking_policy = 256 new B2IRowLockingRR( 257 rawtran, 258 lock_level, 259 rawtran.newLockingPolicy( 260 LockingPolicy.MODE_RECORD, 261 isolation_level, 262 true), 263 base_cc, 264 open_btree); 265 } 266 else if ((isolation_level == 267 TransactionController.ISOLATION_READ_COMMITTED) || 268 (isolation_level == 269 TransactionController.ISOLATION_READ_COMMITTED_NOHOLDLOCK)) 270 { 271 ret_locking_policy = 272 new B2IRowLocking2( 273 rawtran, 274 lock_level, 275 rawtran.newLockingPolicy( 276 LockingPolicy.MODE_RECORD, 277 isolation_level, 278 true), 279 base_cc, 280 open_btree); 281 } 282 else if (isolation_level == 283 TransactionController.ISOLATION_READ_UNCOMMITTED) 284 { 285 ret_locking_policy = 286 new B2IRowLocking1( 287 rawtran, 288 lock_level, 289 rawtran.newLockingPolicy( 290 LockingPolicy.MODE_RECORD, 291 isolation_level, 292 true), 293 base_cc, 294 open_btree); 295 } 296 } 297 298 299 if (SanityManager.DEBUG) 300 { 301 SanityManager.ASSERT( 302 ret_locking_policy != null, "ret_locking_policy == null"); 303 } 304 305 return(ret_locking_policy); 306 } 307 308 321 public final ConglomerateController lockTable( 322 TransactionManager xact_manager, 323 int open_mode, 324 int lock_level, 325 int isolation_level) 326 throws StandardException 327 { 328 open_mode |= TransactionController.OPENMODE_FOR_LOCK_ONLY; 329 330 ConglomerateController cc = 332 xact_manager.openConglomerate( 333 this.baseConglomerateId, false, open_mode, lock_level, 334 isolation_level); 335 336 return(cc); 337 } 338 339 343 344 345 private void traverseRight() 346 { 347 349 if (SanityManager.DEBUG) 350 SanityManager.THROWASSERT("not implemented."); 351 } 352 353 354 357 358 395 public void create( 396 TransactionManager xact_manager, 397 int segmentId, 398 long input_conglomid, 399 DataValueDescriptor[] template, 400 ColumnOrdering[] columnOrder, 401 Properties properties, 402 int temporaryFlag) 403 throws StandardException 404 { 405 String property_value = null; 406 Transaction rawtran = xact_manager.getRawStoreXact(); 407 408 if (properties == null) 409 { 410 throw(StandardException.newException( 411 SQLState.BTREE_PROPERTY_NOT_FOUND, PROPERTY_BASECONGLOMID)); 412 } 413 414 property_value = properties.getProperty(PROPERTY_BASECONGLOMID); 416 if (property_value == null) 417 { 418 throw(StandardException.newException( 419 SQLState.BTREE_PROPERTY_NOT_FOUND, PROPERTY_BASECONGLOMID)); 420 } 421 422 if (SanityManager.DEBUG) 423 { 424 if (property_value == null) 425 SanityManager.THROWASSERT( 426 PROPERTY_BASECONGLOMID + 427 "property not passed to B2I.create()"); 428 } 429 430 baseConglomerateId = Long.parseLong(property_value); 431 432 property_value = properties.getProperty(PROPERTY_ROWLOCCOLUMN); 434 435 if (SanityManager.DEBUG) 436 { 437 if (property_value == null) 438 SanityManager.THROWASSERT( 439 PROPERTY_ROWLOCCOLUMN + 440 "property not passed to B2I.create()"); 441 } 442 443 if (property_value == null) 444 { 445 throw(StandardException.newException( 446 SQLState.BTREE_PROPERTY_NOT_FOUND, PROPERTY_BASECONGLOMID)); 447 } 448 449 rowLocationColumn = Integer.parseInt(property_value); 450 451 if (SanityManager.DEBUG) 454 { 455 SanityManager.ASSERT(rowLocationColumn == template.length - 1, 456 "rowLocationColumn is not the last column in the index"); 457 SanityManager.ASSERT( 458 template[rowLocationColumn] instanceof 459 RowLocation); 460 461 if (rowLocationColumn < 1) 463 SanityManager.THROWASSERT( 464 "rowLocationColumn (" + rowLocationColumn + 465 ") expected to be >= 1"); 466 } 467 468 469 477 478 ascDescInfo = new boolean[template.length]; 479 for (int i=0 ; i < ascDescInfo.length; i++) 480 { 481 if (columnOrder != null && i < columnOrder.length) 482 ascDescInfo[i] = columnOrder[i].getIsAscending(); 483 else 484 ascDescInfo[i] = true; } 486 487 super.create(rawtran, segmentId, input_conglomid, template, properties, getTypeFormatId(), temporaryFlag); 489 490 ConglomerateController base_cc = 492 xact_manager.openConglomerate( 493 baseConglomerateId, 494 false, 495 TransactionController.OPENMODE_FOR_LOCK_ONLY, 496 TransactionController.MODE_TABLE, 497 TransactionController.ISOLATION_SERIALIZABLE); 498 499 OpenBTree open_btree = new OpenBTree(); 500 501 BTreeLockingPolicy b2i_locking_policy = 502 new B2ITableLocking3( 503 rawtran, 504 TransactionController.MODE_TABLE, 505 rawtran.newLockingPolicy( 506 LockingPolicy.MODE_CONTAINER, 507 TransactionController.ISOLATION_SERIALIZABLE, true), base_cc, open_btree); 508 509 510 516 open_btree.init( 517 (TransactionManager) xact_manager, (TransactionManager) xact_manager, (ContainerHandle) null, rawtran, 521 false, 522 (ContainerHandle.MODE_FORUPDATE), 523 TransactionController.MODE_TABLE, 524 b2i_locking_policy, this, 526 (LogicalUndo) null, (DynamicCompiledOpenConglomInfo) null); 531 532 LeafControlRow.initEmptyBtree(open_btree); 534 535 open_btree.close(); 536 537 base_cc.close(); 538 } 539 540 541 542 545 546 586 public boolean fetchMaxOnBTree( 587 TransactionManager xact_manager, 588 Transaction rawtran, 589 long conglomId, 590 int open_mode, 591 int lock_level, 592 LockingPolicy locking_policy, 593 int isolation_level, 594 FormatableBitSet scanColumnList, 595 DataValueDescriptor[] fetchRow) 596 throws StandardException 597 { 598 boolean row_exists; 599 600 602 606 B2IMaxScan b2is = new B2IMaxScan(); 608 609 b2is.init( 611 xact_manager, 612 rawtran, 613 open_mode, 614 lock_level, 615 locking_policy, 616 isolation_level, 617 true , 618 scanColumnList, 619 this, 620 new B2IUndo()); 621 622 row_exists = b2is.fetchMax(fetchRow); 623 624 b2is.close(); 625 626 return(row_exists); 627 } 628 629 630 638 639 public long load( 640 TransactionManager xact_manager, 641 boolean createConglom, 642 RowLocationRetRowSource rowSource) 643 throws StandardException 644 { 645 long num_rows_loaded = 0; 646 B2IController b2ic = new B2IController(); 647 648 try 649 { 650 int open_mode = TransactionController.OPENMODE_FORUPDATE; 651 652 if (createConglom) 653 { 654 open_mode |= 655 (ContainerHandle.MODE_UNLOGGED | 656 ContainerHandle.MODE_CREATE_UNLOGGED); 657 } 658 659 b2ic.init( 661 xact_manager, xact_manager.getRawStoreXact(), false, open_mode, 665 TransactionController.MODE_TABLE, 666 xact_manager.getRawStoreXact().newLockingPolicy( 667 LockingPolicy.MODE_CONTAINER, 668 TransactionController.ISOLATION_SERIALIZABLE, true), 669 true, 670 this, 671 new B2IUndo(), 672 (B2IStaticCompiledInfo) null, 673 (DynamicCompiledOpenConglomInfo) null); 674 675 num_rows_loaded = b2ic.load(xact_manager, createConglom, rowSource); 676 677 } 678 finally 679 { 680 b2ic.close(); 681 } 682 683 return(num_rows_loaded); 684 } 685 686 692 public ConglomerateController open( 693 TransactionManager xact_manager, 694 Transaction rawtran, 695 boolean hold, 696 int open_mode, 697 int lock_level, 698 LockingPolicy locking_policy, 699 StaticCompiledOpenConglomInfo static_info, 700 DynamicCompiledOpenConglomInfo dynamic_info) 701 throws StandardException 702 { 703 B2IController b2ic = new B2IController(); 705 706 b2ic.init( 708 xact_manager, rawtran, hold, open_mode, 712 lock_level, 713 locking_policy, 714 true, 715 this, 716 new B2IUndo(), 717 (B2IStaticCompiledInfo) static_info, 718 dynamic_info); 719 720 return b2ic; 722 } 723 724 731 public ScanManager openScan( 732 TransactionManager xact_manager, 733 Transaction rawtran, 734 boolean hold, 735 int open_mode, 736 int lock_level, 737 LockingPolicy locking_policy, 738 int isolation_level, 739 FormatableBitSet scanColumnList, 740 DataValueDescriptor[] startKeyValue, 741 int startSearchOperator, 742 Qualifier qualifier[][], 743 DataValueDescriptor[] stopKeyValue, 744 int stopSearchOperator, 745 StaticCompiledOpenConglomInfo static_info, 746 DynamicCompiledOpenConglomInfo dynamic_info) 747 throws StandardException 748 { 749 B2IForwardScan b2is = new B2IForwardScan(); 751 752 b2is.init(xact_manager, rawtran, 754 hold, 755 open_mode, 756 lock_level, 757 locking_policy, 758 isolation_level, 759 true , 760 scanColumnList, 761 startKeyValue, startSearchOperator, 762 qualifier, 763 stopKeyValue, stopSearchOperator, this, new B2IUndo(), 764 (B2IStaticCompiledInfo) static_info, 765 dynamic_info); 766 767 return b2is; 769 } 770 771 780 public ScanManager defragmentConglomerate( 781 TransactionManager xact_manager, 782 Transaction rawtran, 783 boolean hold, 784 int open_mode, 785 int lock_level, 786 LockingPolicy locking_policy, 787 int isolation_level) 788 throws StandardException 789 { 790 throw StandardException.newException( 791 SQLState.BTREE_UNIMPLEMENTED_FEATURE); 792 } 793 794 public void purgeConglomerate( 795 TransactionManager xact_manager, 796 Transaction rawtran) 797 throws StandardException 798 { 799 return; 802 } 803 804 public void compressConglomerate( 805 TransactionManager xact_manager, 806 Transaction rawtran) 807 throws StandardException 808 { 809 B2IController b2ic = new B2IController(); 810 811 try 812 { 813 int open_mode = TransactionController.OPENMODE_FORUPDATE; 814 815 b2ic.init( 817 xact_manager, xact_manager.getRawStoreXact(), false, open_mode, 821 TransactionController.MODE_TABLE, 822 xact_manager.getRawStoreXact().newLockingPolicy( 823 LockingPolicy.MODE_CONTAINER, 824 TransactionController.ISOLATION_SERIALIZABLE, true), 825 true, 826 this, 827 new B2IUndo(), 828 (B2IStaticCompiledInfo) null, 829 (DynamicCompiledOpenConglomInfo) null); 830 831 b2ic.getContainer().compressContainer(); 832 833 } 834 finally 835 { 836 b2ic.close(); 837 } 838 839 return; 840 } 841 842 859 public StoreCostController openStoreCost( 860 TransactionManager xact_manager, 861 Transaction rawtran) 862 throws StandardException 863 { 864 B2ICostController b2icost = new B2ICostController(); 865 866 b2icost.init(xact_manager, this, rawtran); 867 868 return(b2icost); 869 } 870 871 878 public void drop(TransactionManager xact_manager) 879 throws StandardException 880 { 881 ConglomerateController base_cc = null; 884 885 886 887 base_cc = 888 lockTable( 889 xact_manager, 890 TransactionController.OPENMODE_FORUPDATE, 891 TransactionController.MODE_TABLE, 892 TransactionController.ISOLATION_REPEATABLE_READ); 893 894 xact_manager.getRawStoreXact().dropContainer(id); 895 896 if (base_cc != null) 897 base_cc.close(); 898 } 899 900 919 public StaticCompiledOpenConglomInfo getStaticCompiledConglomInfo( 920 TransactionController xact_manager, 921 long conglomId) 922 throws StandardException 923 { 924 return(new B2IStaticCompiledInfo(xact_manager, this)); 925 } 926 927 932 933 934 937 938 939 944 public int getTypeFormatId() 945 { 946 return StoredFormatIds.ACCESS_B2I_V3_ID; 947 } 948 949 953 public void writeExternal_v36(ObjectOutput out) throws IOException { 954 super.writeExternal(out); 955 out.writeLong(baseConglomerateId); 956 out.writeInt(rowLocationColumn); 957 } 958 959 967 public void readExternal_v36(ObjectInput in) 968 throws IOException , ClassNotFoundException 969 { 970 super.readExternal(in); 971 972 baseConglomerateId = in.readLong(); 974 rowLocationColumn = in.readInt(); 975 ascDescInfo = new boolean[nKeyFields]; 977 for (int i=0 ; i < ascDescInfo.length; i++) 978 ascDescInfo[i] = true; 979 } 980 981 982 988 public void writeExternal(ObjectOutput out) throws IOException { 989 super.writeExternal(out); 990 out.writeLong(baseConglomerateId); 991 out.writeInt(rowLocationColumn); 992 993 if (conglom_format_id != StoredFormatIds.ACCESS_B2I_V2_ID) 996 { 997 FormatableBitSet ascDescBits = new FormatableBitSet(ascDescInfo.length); 999 for (int i = 0; i < ascDescInfo.length; i++) 1000 { 1001 if (ascDescInfo[i]) 1002 ascDescBits.set(i); 1003 } 1004 ascDescBits.writeExternal(out); 1005 } 1006 1007 } 1008 1009 1017 private final void localReadExternal(ObjectInput in) 1018 throws IOException , ClassNotFoundException 1019 { 1020 super.readExternal(in); 1021 1022 baseConglomerateId = in.readLong(); 1024 rowLocationColumn = in.readInt(); 1025 1026 if (conglom_format_id != StoredFormatIds.ACCESS_B2I_V2_ID) 1029 { 1030 FormatableBitSet ascDescBits = new FormatableBitSet(); 1032 ascDescBits.readExternal(in); 1033 ascDescInfo = new boolean[ascDescBits.getLength()]; 1034 for(int i =0 ; i < ascDescBits.getLength(); i++) 1035 ascDescInfo[i] = ascDescBits.isSet(i); 1036 } 1037 else 1038 { 1039 ascDescInfo = new boolean[nKeyFields]; 1041 for (int i=0 ; i < ascDescInfo.length; i++) 1042 ascDescInfo[i] = true; 1043 1044 } 1045 1046 } 1047 1048 public void readExternal(ObjectInput in) 1049 throws IOException , ClassNotFoundException 1050 { 1051 localReadExternal(in); 1052 } 1053 public void readExternalFromArray(ArrayInputStream in) 1054 throws IOException , ClassNotFoundException 1055 { 1056 localReadExternal(in); 1057 } 1058} 1059 | Popular Tags |