1 21 22 package org.apache.derby.impl.store.access; 23 24 import org.apache.derby.iapi.services.cache.Cacheable; 25 import org.apache.derby.iapi.services.cache.CacheableFactory; 26 import org.apache.derby.iapi.services.cache.CacheFactory; 27 import org.apache.derby.iapi.services.cache.CacheManager; 28 29 import org.apache.derby.iapi.services.context.ContextManager; 30 import org.apache.derby.iapi.services.context.ContextService; 31 import org.apache.derby.iapi.services.daemon.Serviceable; 32 import org.apache.derby.iapi.services.locks.LockFactory; 33 import org.apache.derby.iapi.services.monitor.ModuleControl; 34 import org.apache.derby.iapi.services.monitor.Monitor; 35 import org.apache.derby.iapi.services.property.PropertySetCallback; 36 37 import org.apache.derby.iapi.services.sanity.SanityManager; 38 import org.apache.derby.iapi.services.io.FormatIdUtil; 39 40 import org.apache.derby.iapi.error.StandardException; 41 42 import org.apache.derby.iapi.store.access.conglomerate.Conglomerate; 43 import org.apache.derby.iapi.store.access.conglomerate.ConglomerateFactory; 44 import org.apache.derby.iapi.store.access.conglomerate.MethodFactory; 45 import org.apache.derby.iapi.store.access.conglomerate.TransactionManager; 46 import org.apache.derby.iapi.services.property.PropertyUtil; 47 import org.apache.derby.iapi.store.access.AccessFactory; 48 import org.apache.derby.iapi.services.property.PropertyFactory; 49 50 import org.apache.derby.iapi.store.access.AccessFactoryGlobals; 51 import org.apache.derby.iapi.store.access.TransactionController; 52 import org.apache.derby.iapi.store.access.TransactionInfo; 53 54 import org.apache.derby.iapi.store.raw.ContainerHandle; 55 import org.apache.derby.iapi.store.raw.ContainerKey; 56 import org.apache.derby.iapi.store.raw.LockingPolicy; 57 import org.apache.derby.iapi.store.raw.RawStoreFactory; 58 import org.apache.derby.iapi.store.raw.Transaction; 59 60 import org.apache.derby.catalog.UUID; 61 62 import org.apache.derby.iapi.services.io.FormatableHashtable; 63 import org.apache.derby.iapi.reference.SQLState; 64 import org.apache.derby.iapi.reference.Attribute; 65 66 import java.util.Dictionary ; 67 import java.util.Enumeration ; 68 import java.util.Hashtable ; 69 import java.util.Properties ; 70 71 import java.io.File ; 72 import java.io.Serializable ; 73 74 75 public abstract class RAMAccessManager 76 implements AccessFactory, 77 CacheableFactory, 78 ModuleControl, 79 PropertySetCallback 80 { 81 85 86 89 private RawStoreFactory rawstore; 90 91 94 private Hashtable implhash; 95 96 99 private Hashtable formathash; 100 101 111 private Properties serviceProperties; 112 113 116 LockingPolicy system_default_locking_policy; 117 118 122 private PropertyConglomerate xactProperties; 123 private PropertyFactory pf; 124 125 protected LockingPolicy table_level_policy[]; 126 protected LockingPolicy record_level_policy[]; 127 128 129 145 protected ConglomerateFactory conglom_map[]; 146 147 152 private CacheManager conglom_cache; 153 154 158 159 public RAMAccessManager() 160 { 161 implhash = new Hashtable(); 164 formathash = new Hashtable(); 165 } 166 167 171 172 177 protected LockingPolicy getDefaultLockingPolicy() 178 { 179 return(system_default_locking_policy); 180 } 181 182 183 RawStoreFactory getRawStore() 184 { 185 return rawstore; 186 } 187 188 189 PropertyConglomerate getTransactionalProperties() 190 { 191 return xactProperties; 192 } 193 194 private void boot_load_conglom_map() 195 throws StandardException 196 { 197 199 conglom_map = new ConglomerateFactory[2]; 200 201 MethodFactory mfactory = findMethodFactoryByImpl("heap"); 203 204 if (mfactory == null || !(mfactory instanceof ConglomerateFactory)) 205 { 206 throw StandardException.newException( 207 SQLState.AM_NO_SUCH_CONGLOMERATE_TYPE, "heap"); 208 } 209 210 conglom_map[ConglomerateFactory.HEAP_FACTORY_ID] = 211 (ConglomerateFactory) mfactory; 212 213 mfactory = findMethodFactoryByImpl("BTREE"); 215 216 if (mfactory == null || !(mfactory instanceof ConglomerateFactory)) 217 { 218 throw StandardException.newException( 219 SQLState.AM_NO_SUCH_CONGLOMERATE_TYPE, "BTREE"); 220 } 221 conglom_map[ConglomerateFactory.BTREE_FACTORY_ID] = 222 (ConglomerateFactory) mfactory; 223 224 } 227 228 229 230 231 236 237 254 abstract protected int getSystemLockLevel(); 255 256 267 abstract protected void bootLookupSystemLockLevel( 268 TransactionController tc) 269 throws StandardException; 270 271 275 private long conglom_nextid = 0; 276 277 299 protected long getNextConglomId(int factory_type) 300 throws StandardException 301 { 302 long conglomid; 303 304 if (SanityManager.DEBUG) 305 { 306 309 SanityManager.ASSERT(factory_type >= 0x00 && factory_type <= 0x0f); 310 } 311 312 synchronized (conglom_cache) 313 { 314 if (conglom_nextid == 0) 315 { 316 conglom_nextid = (rawstore.getMaxContainerId() >> 4) + 1; 318 } 319 320 conglomid = conglom_nextid++; 321 } 322 323 325 return((conglomid << 4) | factory_type); 326 } 327 328 346 360 361 374 private ConglomerateFactory getFactoryFromConglomId( 375 long conglom_id) 376 throws StandardException 377 { 378 try 379 { 380 return(conglom_map[((int) (0x0f & conglom_id))]); 381 } 382 catch (java.lang.ArrayIndexOutOfBoundsException e) 383 { 384 throw StandardException.newException( 386 SQLState.STORE_CONGLOMERATE_DOES_NOT_EXIST, 387 new Long (conglom_id)); 388 } 389 } 390 391 392 396 397 420 421 429 private void conglomCacheInit() 430 throws StandardException 431 { 432 CacheFactory cf = 434 (CacheFactory) Monitor.startSystemModule( 435 org.apache.derby.iapi.reference.Module.CacheFactory); 436 437 439 conglom_cache = 440 cf.newCacheManager( 441 this, AccessFactoryGlobals.CFG_CONGLOMDIR_CACHE, 200, 300); 442 443 } 444 445 459 Conglomerate conglomCacheFind( 460 TransactionManager xact_mgr, 461 long conglomid) 462 throws StandardException 463 { 464 Conglomerate conglom = null; 465 Long conglomid_obj = new Long (conglomid); 466 467 synchronized (conglom_cache) 468 { 469 CacheableConglomerate cache_entry = 470 (CacheableConglomerate) conglom_cache.findCached(conglomid_obj); 471 472 if (cache_entry != null) 473 { 474 conglom = cache_entry.getConglom(); 475 conglom_cache.release(cache_entry); 476 477 } 479 else 480 { 481 483 485 conglom = 486 getFactoryFromConglomId(conglomid).readConglomerate( 487 xact_mgr, new ContainerKey(0, conglomid)); 488 489 if (conglom != null) 490 { 491 cache_entry = (CacheableConglomerate) 493 this.conglom_cache.create(conglomid_obj, conglom); 494 this.conglom_cache.release(cache_entry); 495 } 496 } 497 } 498 499 return(conglom); 500 } 501 502 512 protected void conglomCacheInvalidate() 513 throws StandardException 514 { 515 synchronized (conglom_cache) 516 { 517 conglom_cache.ageOut(); 518 } 519 520 return; 521 } 522 523 535 void conglomCacheUpdateEntry( 536 long conglomid, 537 Conglomerate new_conglom) 538 throws StandardException 539 { 540 Long conglomid_obj = new Long (conglomid); 541 542 synchronized (conglom_cache) 543 { 544 CacheableConglomerate conglom_entry = (CacheableConglomerate) 546 conglom_cache.findCached(conglomid_obj); 547 548 if (conglom_entry != null) 549 conglom_cache.remove(conglom_entry); 550 551 conglom_entry = (CacheableConglomerate) 553 conglom_cache.create(conglomid_obj, new_conglom); 554 conglom_cache.release(conglom_entry); 555 } 556 557 return; 558 } 559 560 569 void conglomCacheAddEntry( 570 long conglomid, 571 Conglomerate conglom) 572 throws StandardException 573 { 574 synchronized (conglom_cache) 575 { 576 CacheableConglomerate conglom_entry = (CacheableConglomerate) 578 conglom_cache.create(new Long (conglomid), conglom); 579 conglom_cache.release(conglom_entry); 580 } 581 582 return; 583 } 584 585 593 void conglomCacheRemoveEntry(long conglomid) 594 throws StandardException 595 { 596 synchronized (conglom_cache) 597 { 598 CacheableConglomerate conglom_entry = (CacheableConglomerate) 599 conglom_cache.findCached(new Long (conglomid)); 600 601 if (conglom_entry != null) 602 conglom_cache.remove(conglom_entry); 603 } 604 605 return; 606 } 607 608 609 610 614 615 619 public void createFinished() throws StandardException 620 { 621 rawstore.createFinished(); 622 } 623 624 628 public MethodFactory findMethodFactoryByFormat(UUID format) 629 { 630 MethodFactory factory; 631 632 factory = (MethodFactory) formathash.get(format); 635 if (factory != null) 636 return factory; 637 638 Enumeration e = formathash.elements(); 641 while (e.hasMoreElements()) 642 { 643 factory = (MethodFactory) e.nextElement(); 644 if (factory.supportsFormat(format)) 645 return factory; 646 } 647 648 return null; 650 } 651 652 656 public MethodFactory findMethodFactoryByImpl(String impltype) 657 throws StandardException 658 { 659 MethodFactory factory = (MethodFactory) implhash.get(impltype); 662 if (factory != null) 663 return factory; 664 665 Enumeration e = implhash.elements(); 668 while (e.hasMoreElements()) 669 { 670 factory = (MethodFactory) e.nextElement(); 671 if (factory.supportsImplementation(impltype)) 672 return factory; 673 } 674 factory = null; 675 676 Properties conglomProperties = new Properties (serviceProperties); 681 conglomProperties.put(AccessFactoryGlobals.CONGLOM_PROP, impltype); 682 683 try { 684 factory = 685 (MethodFactory) Monitor.bootServiceModule( 686 false, this, MethodFactory.MODULE, 687 impltype, conglomProperties); 688 } catch (StandardException se) { 689 if (!se.getMessageId().equals(SQLState.SERVICE_MISSING_IMPLEMENTATION)) 690 throw se; 691 } 692 693 conglomProperties = null; 694 695 if (factory != null) { 696 registerAccessMethod(factory); 697 return factory; 698 } 699 700 return null; 702 } 703 704 public LockFactory getLockFactory() { 705 return rawstore.getLockFactory(); 706 } 707 708 709 public TransactionController getTransaction( 710 ContextManager cm) 711 throws StandardException 712 { 713 return getAndNameTransaction(cm, AccessFactoryGlobals.USER_TRANS_NAME); 714 } 715 716 public TransactionController getAndNameTransaction( 717 ContextManager cm, String transName) 718 throws StandardException 719 { 720 if (cm == null) 721 return null; 723 RAMTransactionContext rtc = (RAMTransactionContext) 725 cm.getContext(AccessFactoryGlobals.RAMXACT_CONTEXT_ID); 726 727 if (rtc == null) 728 { 729 Transaction rawtran = rawstore.findUserTransaction(cm, transName); 734 RAMTransaction rt = new RAMTransaction(this, rawtran, null); 735 736 rtc = 737 new RAMTransactionContext( 738 cm, 739 AccessFactoryGlobals.RAMXACT_CONTEXT_ID, 740 rt, false ); 741 742 TransactionController tc = rtc.getTransaction(); 743 744 if (xactProperties != null) 745 { 746 rawtran.setup(tc); 747 tc.commit(); 748 } 749 750 rawtran.setDefaultLockingPolicy(system_default_locking_policy); 751 752 tc.commit(); 753 754 return tc; 755 } 756 return rtc.getTransaction(); 757 } 758 759 784 public Object startXATransaction( 785 ContextManager cm, 786 int format_id, 787 byte[] global_id, 788 byte[] branch_id) 789 throws StandardException 790 { 791 RAMTransaction xa_tc = null; 792 793 if (SanityManager.DEBUG) 794 { 795 SanityManager.ASSERT(global_id != null); 796 SanityManager.ASSERT(branch_id != null); 797 } 798 799 if (cm == null) 800 return null; 802 RAMTransactionContext rtc = (RAMTransactionContext) 804 cm.getContext(AccessFactoryGlobals.RAMXACT_CONTEXT_ID); 805 806 if (rtc == null) 807 { 808 Transaction rawtran = 813 rawstore.startGlobalTransaction( 814 cm, format_id, global_id, branch_id); 815 816 xa_tc = new RAMTransaction(this, rawtran, null); 817 818 rtc = 819 new RAMTransactionContext( 820 cm, 821 AccessFactoryGlobals.RAMXACT_CONTEXT_ID, 822 xa_tc, false ); 823 824 827 if (xactProperties != null) 828 { 829 rawtran.setup(xa_tc); 830 831 xa_tc.commitNoSync( 836 TransactionController.RELEASE_LOCKS | 837 TransactionController.READONLY_TRANSACTION_INITIALIZATION); 838 } 839 840 rawtran.setDefaultLockingPolicy(system_default_locking_policy); 841 842 xa_tc.commitNoSync( 847 TransactionController.RELEASE_LOCKS | 848 TransactionController.READONLY_TRANSACTION_INITIALIZATION); 849 } 850 else 851 { 852 if (SanityManager.DEBUG) 854 SanityManager.THROWASSERT( 855 "RAMTransactionContext found on stack."); 856 } 857 858 return(xa_tc); 859 } 860 861 862 872 public Object getXAResourceManager() 873 throws StandardException 874 { 875 return(rawstore.getXAResourceManager()); 876 } 877 878 public void registerAccessMethod(MethodFactory factory) 879 { 880 implhash.put(factory.primaryImplementationType(), factory); 883 884 formathash.put(factory.primaryFormat(), factory); 887 } 888 889 public boolean isReadOnly() 890 { 891 return rawstore.isReadOnly(); 892 } 893 894 private void addPropertySetNotification(PropertySetCallback who, TransactionController tc) { 895 896 pf.addPropertySetNotification(who); 897 898 Dictionary d = new Hashtable(); 902 try { 903 xactProperties.getProperties(tc,d,false,false); 904 } catch (StandardException se) { 905 return; 906 } 907 908 boolean dbOnly = PropertyUtil.isDBOnly(d); 909 910 who.init(dbOnly, d); 911 } 912 913 public TransactionInfo[] getTransactionInfo() 914 { 915 return rawstore.getTransactionInfo(); 916 } 917 918 public void freeze() throws StandardException 919 { 920 rawstore.freeze(); 921 } 922 923 public void unfreeze() throws StandardException 924 { 925 rawstore.unfreeze(); 926 } 927 928 public void backup( 929 String backupDir, 930 boolean wait) 931 throws StandardException 932 { 933 rawstore.backup(backupDir, wait); 934 } 935 936 937 public void backupAndEnableLogArchiveMode( 938 String backupDir, 939 boolean deleteOnlineArchivedLogFiles, 940 boolean wait) 941 throws StandardException 942 { 943 rawstore.backupAndEnableLogArchiveMode(backupDir, 944 deleteOnlineArchivedLogFiles, 945 wait); 946 } 947 948 public void disableLogArchiveMode(boolean deleteOnlineArchivedLogFiles) 949 throws StandardException 950 { 951 rawstore.disableLogArchiveMode(deleteOnlineArchivedLogFiles); 952 } 953 954 955 956 public void checkpoint() throws StandardException 957 { 958 rawstore.checkpoint(); 959 } 960 961 public void waitForPostCommitToFinishWork() 962 { 963 rawstore.getDaemon().waitUntilQueueIsEmpty(); 964 } 965 966 970 public void boot(boolean create, Properties startParams) 971 throws StandardException 972 { 973 this.serviceProperties = startParams; 974 975 boot_load_conglom_map(); 976 977 if (create) 978 { 979 conglom_nextid = 1; 984 } 985 986 rawstore = (RawStoreFactory) Monitor.bootServiceModule( 989 create, this, RawStoreFactory.MODULE, serviceProperties); 990 991 Monitor.bootServiceModule( 998 create, this, org.apache.derby.iapi.reference.Module.PropertyFactory, 999 startParams); 1000 1001 1003 conglomCacheInit(); 1004 1005 RAMTransaction tc = 1008 (RAMTransaction) getAndNameTransaction( 1009 ContextService.getFactory().getCurrentContextManager(), 1010 AccessFactoryGlobals.USER_TRANS_NAME); 1011 1012 1017 int lock_mode = LockingPolicy.MODE_CONTAINER; 1018 1019 system_default_locking_policy = 1020 tc.getRawStoreXact().newLockingPolicy( 1021 lock_mode, 1022 TransactionController.ISOLATION_SERIALIZABLE, true); 1023 1024 1025 1028 table_level_policy = new LockingPolicy[6]; 1029 1030 table_level_policy[TransactionController.ISOLATION_NOLOCK] = 1031 tc.getRawStoreXact().newLockingPolicy( 1032 LockingPolicy.MODE_CONTAINER, 1033 TransactionController.ISOLATION_NOLOCK, true); 1034 1035 table_level_policy[TransactionController.ISOLATION_READ_UNCOMMITTED] = 1036 tc.getRawStoreXact().newLockingPolicy( 1037 LockingPolicy.MODE_CONTAINER, 1038 TransactionController.ISOLATION_READ_UNCOMMITTED, true); 1039 1040 table_level_policy[TransactionController.ISOLATION_READ_COMMITTED] = 1041 tc.getRawStoreXact().newLockingPolicy( 1042 LockingPolicy.MODE_CONTAINER, 1043 TransactionController.ISOLATION_READ_COMMITTED, true); 1044 1045 table_level_policy[TransactionController.ISOLATION_READ_COMMITTED_NOHOLDLOCK] = 1046 tc.getRawStoreXact().newLockingPolicy( 1047 LockingPolicy.MODE_CONTAINER, 1048 TransactionController.ISOLATION_READ_COMMITTED_NOHOLDLOCK, 1049 true); 1050 1051 table_level_policy[TransactionController.ISOLATION_REPEATABLE_READ] = 1052 tc.getRawStoreXact().newLockingPolicy( 1053 LockingPolicy.MODE_CONTAINER, 1054 TransactionController.ISOLATION_REPEATABLE_READ, true); 1055 1056 table_level_policy[TransactionController.ISOLATION_SERIALIZABLE] = 1057 tc.getRawStoreXact().newLockingPolicy( 1058 LockingPolicy.MODE_CONTAINER, 1059 TransactionController.ISOLATION_SERIALIZABLE, true); 1060 1061 record_level_policy = new LockingPolicy[6]; 1062 1063 record_level_policy[TransactionController.ISOLATION_NOLOCK] = 1064 tc.getRawStoreXact().newLockingPolicy( 1065 LockingPolicy.MODE_RECORD, 1066 TransactionController.ISOLATION_NOLOCK, true); 1067 1068 record_level_policy[TransactionController.ISOLATION_READ_UNCOMMITTED] = 1069 tc.getRawStoreXact().newLockingPolicy( 1070 LockingPolicy.MODE_RECORD, 1071 TransactionController.ISOLATION_READ_UNCOMMITTED, true); 1072 1073 record_level_policy[TransactionController.ISOLATION_READ_COMMITTED] = 1074 tc.getRawStoreXact().newLockingPolicy( 1075 LockingPolicy.MODE_RECORD, 1076 TransactionController.ISOLATION_READ_COMMITTED, true); 1077 1078 record_level_policy[TransactionController.ISOLATION_READ_COMMITTED_NOHOLDLOCK] = 1079 tc.getRawStoreXact().newLockingPolicy( 1080 LockingPolicy.MODE_RECORD, 1081 TransactionController.ISOLATION_READ_COMMITTED_NOHOLDLOCK, 1082 true); 1083 1084 record_level_policy[TransactionController.ISOLATION_REPEATABLE_READ] = 1085 tc.getRawStoreXact().newLockingPolicy( 1086 LockingPolicy.MODE_RECORD, 1087 TransactionController.ISOLATION_REPEATABLE_READ, true); 1088 1089 record_level_policy[TransactionController.ISOLATION_SERIALIZABLE] = 1090 tc.getRawStoreXact().newLockingPolicy( 1091 LockingPolicy.MODE_RECORD, 1092 TransactionController.ISOLATION_SERIALIZABLE, true); 1093 1094 if (SanityManager.DEBUG) 1095 { 1096 for (int i = 0; 1097 i < TransactionController.ISOLATION_SERIALIZABLE; 1098 i++) 1099 { 1100 SanityManager.ASSERT( 1101 table_level_policy[i] != null, 1102 "table_level_policy[" + i + "] is null"); 1103 SanityManager.ASSERT( 1104 record_level_policy[i] != null, 1105 "record_level_policy[" + i + "] is null"); 1106 } 1107 } 1108 1109 tc.commit(); 1110 1111 pf = (PropertyFactory) 1113 Monitor.findServiceModule( 1114 this, org.apache.derby.iapi.reference.Module.PropertyFactory); 1115 1116 xactProperties = new PropertyConglomerate(tc, create, startParams, pf); 1120 1121 rawstore.getRawStoreProperties(tc); 1124 1125 bootLookupSystemLockLevel(tc); 1128 1129 lock_mode = 1130 (getSystemLockLevel() == TransactionController.MODE_TABLE ? 1131 LockingPolicy.MODE_CONTAINER : LockingPolicy.MODE_RECORD); 1132 1133 system_default_locking_policy = 1134 tc.getRawStoreXact().newLockingPolicy( 1135 lock_mode, 1136 TransactionController.ISOLATION_SERIALIZABLE, true); 1137 1138 addPropertySetNotification(getLockFactory(), tc); 1140 1141 addPropertySetNotification(this,tc); 1143 1144 tc.commit(); 1145 1146 tc.destroy(); 1147 tc = null; 1148 1149 if (SanityManager.DEBUG) 1150 { 1151 SanityManager.ASSERT( 1156 TransactionController.OPENMODE_USE_UPDATE_LOCKS == 1157 ContainerHandle.MODE_USE_UPDATE_LOCKS); 1158 SanityManager.ASSERT( 1159 TransactionController.OPENMODE_SECONDARY_LOCKED == 1160 ContainerHandle.MODE_SECONDARY_LOCKED); 1161 SanityManager.ASSERT( 1162 TransactionController.OPENMODE_BASEROW_INSERT_LOCKED == 1163 ContainerHandle.MODE_BASEROW_INSERT_LOCKED); 1164 SanityManager.ASSERT( 1165 TransactionController.OPENMODE_FORUPDATE == 1166 ContainerHandle.MODE_FORUPDATE); 1167 SanityManager.ASSERT( 1168 TransactionController.OPENMODE_FOR_LOCK_ONLY == 1169 ContainerHandle.MODE_OPEN_FOR_LOCK_ONLY); 1170 } 1171 } 1172 1173 public void stop() 1174 { 1175 } 1176 1177 1178 1179 1182 public void init(boolean dbOnly, Dictionary p) 1183 { 1184 } 1185 1186 public boolean validate(String key, Serializable value, Dictionary p) 1187 throws StandardException 1188 { 1189 if (key.equals(Attribute.CRYPTO_ALGORITHM)) 1190 { 1191 throw StandardException.newException(SQLState.ENCRYPTION_NOCHANGE_ALGORITHM); 1192 } 1193 if (key.equals(Attribute.CRYPTO_PROVIDER)) 1194 { 1195 throw StandardException.newException(SQLState.ENCRYPTION_NOCHANGE_PROVIDER); 1196 } 1197 return true; 1198 } 1199 1200 public Serviceable apply(String key, Serializable value, Dictionary p) 1201 throws StandardException 1202 { 1203 return null; 1204 } 1205 1206 public Serializable map(String key, Serializable value, Dictionary p) 1207 throws StandardException 1208 { 1209 return null; 1210 } 1211 1212 1214 1217 1218 public Cacheable newCacheable(CacheManager cm) { 1219 return new CacheableConglomerate(); 1220 } 1221 1222} 1223 | Popular Tags |