| 1 package com.daffodilwoods.daffodildb.server.sessionsystem; 2 3 4 import java.util.*; 5 import com.daffodilwoods.database.resource.*; 6 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*; 7 import com.daffodilwoods.daffodildb.server.serversystem.dmlvalidation.constraintsystem.*; 8 import com.daffodilwoods.database.general.*; 9 import com.daffodilwoods.database.sqlinitiator.*; 10 11 import com.daffodilwoods.daffodildb.server.serversystem.*; 12 import com.daffodilwoods.daffodildb.server.datasystem.interfaces._Database; 13 import com.daffodilwoods.daffodildb.server.datasystem.mergesystem.*; 14 import com.daffodilwoods.daffodildb.utils.*; 15 import com.daffodilwoods.daffodildb.server.datasystem.indexsystem.IndexDatabase; 16 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.PersistentDatabase; 17 import com.daffodilwoods.daffodildb.utils.byteconverter.CCzufDpowfsufs; 18 import com.daffodilwoods.daffodildb.utils.comparator.SuperComparator; 19 import java.io.*; 20 21 28 public class SessionDatabase implements _SessionDatabase { 29 30 private _Database mergeDatabase; 31 private _DataDictionary dataDictionary; 32 public WeakOrderedKeyList allSessions = new WeakOrderedKeyList(new CPckfduDpnqbsbups()); 33 private RowIdFetcher rowIdFetcher; 34 private SystemFieldsValue systemFieldsValue; 35 private _ServerSession globalSession; 36 private _ConstraintDatabase deferredConstraintDatabase; 37 private _ConstraintDatabase constraintDatabase; 38 private SessionSystem sessionSystem; 39 private TransactionIsolationLevelHandler tih; 40 private HashMap forUpdateHandlerMap; 41 private TreeMap rowLockers; 42 private SaveModeHandler saveModeHandler; 43 private String databaseURL; 44 private HashMap deadLockDetectorMap; 45 54 55 public SessionDatabase(SessionSystem sessionSystem, _DataDictionary dataDictionary, _Database cacheDatabase, RowIdFetcher rowIdFetcher, _ConstraintDatabase deferredConstraintDatabase, _ConstraintDatabase constraintDatabase, String corruptionChecking, String databaseURL0) throws DException { 56 this.sessionSystem = sessionSystem; 57 this.dataDictionary = dataDictionary; 58 this.mergeDatabase = cacheDatabase; 59 this.rowIdFetcher = rowIdFetcher; 60 this.deferredConstraintDatabase = deferredConstraintDatabase; 61 this.constraintDatabase = constraintDatabase; 62 globalSession = new GlobalSession(cacheDatabase, dataDictionary, sessionSystem.isReadOnlyMode()); 63 dataDictionary.setServerSession(globalSession); 64 65 IndexDatabase md = (IndexDatabase) ( (MergeDatabase) mergeDatabase).getFileDatabase(); 66 PersistentDatabase pd = (PersistentDatabase) md.getUnderLyingDatabase(); 67 byte[] bytes = null; 68 if (corruptionChecking.equalsIgnoreCase("true")) { 69 checkDatabaseVersion(pd); 70 } 71 bytes = pd.readBytes(pd.getDatabaseProperties().LAST_TRANSACTION_ID, 8); 72 long transactionId = CCzufDpowfsufs.getLongValue(bytes, 0); 73 bytes = pd.readBytes(pd.getDatabaseProperties().LAST_SESSION_ID, 8); 74 long sessionId = CCzufDpowfsufs.getLongValue(bytes, 0); 75 systemFieldsValue = new SystemFieldsValue(pd, transactionId, sessionId, sessionSystem.isReadOnlyMode()); 76 tih = new TransactionIsolationLevelHandler(); 77 forUpdateHandlerMap = new HashMap(); 78 rowLockers = new TreeMap(String.CASE_INSENSITIVE_ORDER); 79 databaseURL = databaseURL0; 80 deadLockDetectorMap = new HashMap(); 81 } 82 83 88 89 public _DataDictionary getDataDictionary() throws DException { 90 return dataDictionary; 91 } 92 93 98 99 public _Database getMergeDatabase() throws DException { 100 return mergeDatabase; 101 } 102 103 108 109 public SystemFieldsValue getSystemFieldsValue() throws DException { 110 return systemFieldsValue; 111 } 112 113 125 126 public _Session getSession(String user, Object sessionConstant, Properties sessionProperties) throws DException { 127 if (sessionConstant == null) 128 return getNewSession(user, sessionConstant, sessionProperties); 129 synchronized (sessionConstant.toString().intern()) { 130 if (sessionProperties == null) 131 return getExistingSession(sessionConstant); 132 else 133 return getNewSession(user, sessionConstant, sessionProperties); 134 } 135 } 136 137 143 144 private _Session getExistingSession(Object sessionConstant) throws DException { 145 _Session session = (_Session) allSessions.get(sessionConstant); 146 if (session == null) 147 throw new SessionException("DSE915", null); 148 return session; 149 } 150 151 159 160 private _Session getNewSession(String user, Object sessionConstant, Properties sessionProperties) throws DException { 161 _Session session = null; 162 Object sessionId = null; 163 if (sessionConstant != null) { 164 session = (_Session) allSessions.get(sessionConstant); 165 if (session != null) 166 return session; 167 168 } 169 170 sessionId = systemFieldsValue.getNextSessionId(); 171 if (sessionConstant == null) 172 sessionConstant = user + sessionId.toString(); 173 174 Object transactionId = systemFieldsValue.getLastTransactionId(); 175 176 session = new SessionWithCommit(sessionConstant, sessionId, transactionId, this); 177 tih.transactionStarted(sessionId, session.getIsolationLevel()); 178 allSessions.put(sessionConstant, session); 179 180 181 182 183 String constrintChecking = sessionProperties.getProperty(_Server.CONSTRAINTCHECKING, "true"); 184 if (constrintChecking.equalsIgnoreCase("false")) 185 ( (Session) session).stopConstraintChecking(); 186 return session; 187 } 188 189 194 195 public _ServerSession getGlobalSession() throws DException { 196 return globalSession; 197 } 198 199 205 206 public TableRowIdGetter getTableRowIdGetter(QualifiedIdentifier tableName) throws DException { 207 return rowIdFetcher.getTableRowIdGetter(tableName); 208 } 209 210 216 217 public _ConstraintTable getDeferrableConstraintTable(QualifiedIdentifier tableName) throws DException { 218 return deferredConstraintDatabase.getConstraintTable(tableName); 219 } 220 221 227 228 public _ConstraintTable getConstraintTable(QualifiedIdentifier tableName) throws DException { 229 return constraintDatabase.getConstraintTable(tableName); 230 } 231 232 238 239 public static class CPckfduDpnqbsbups extends SuperComparator { 240 public int compare(Object obj1, Object obj2) { 241 if (obj1 == null) 242 return obj2 == null ? 0 : -1; 243 return obj2 == null ? 1 : ( (Comparable ) obj1).compareTo(obj2); 244 } 245 } 246 247 253 254 public void removeTable(QualifiedIdentifier tableName, boolean dropTable) throws DException { 255 256 257 ( (MergeDatabase) mergeDatabase).setDropStatus(tableName, dropTable); 258 mergeDatabase.removeTable(tableName); 259 rowIdFetcher.removeTable(tableName); 260 deferredConstraintDatabase.removeTable(tableName); 261 WeakOrderedKeyList.WeakOrderedKeyListIterator allSessionList = allSessions.getWeakOrderedKeyListIterator(); 262 if (allSessionList.top()) 263 do { 264 _Session session = (_Session) allSessionList.getObject(); 265 if (session != null) 266 session.removeTable(tableName); 267 } while (allSessionList.next()); 268 } 269 270 public TransactionIsolationLevelHandler getTransactionIsolationLevelHandler() throws DException { 271 return tih; 272 } 273 274 public ForUpdateTableHandler getForUpdateTableHandler(QualifiedIdentifier tableName) { 275 synchronized (forUpdateHandlerMap) { 276 Object uh = forUpdateHandlerMap.get(tableName); 277 if (uh != null) 278 return (ForUpdateTableHandler) uh; 279 ForUpdateTableHandler fuh = new ForUpdateTableHandler(); 280 forUpdateHandlerMap.put(tableName, fuh); 281 return fuh; 282 } 283 } 284 285 public SessionRowLocker getRowLocker(QualifiedIdentifier tableName) throws DException { 286 synchronized (rowLockers) { 287 SessionRowLocker rowLocker = (SessionRowLocker) rowLockers.get(tableName.getIdentifier()); 288 if (rowLocker != null) 289 return rowLocker; 290 rowLocker = new SessionRowLocker(); 291 rowLockers.put(tableName.getIdentifier(), rowLocker); 292 return rowLocker; 293 } 294 295 } 296 297 private void checkDatabaseVersion(PersistentDatabase pd) throws DException { 298 byte[] bytes = pd.readBytes(pd.getDatabaseProperties().ISCOMPLETE_BIT_IN_DATABASE, 1); 299 Boolean isComplete = CCzufDpowfsufs.getBoolean(bytes); 300 if (!isComplete.booleanValue()) 301 throw new DException("DSE5544", null); 302 } 303 304 public SaveModeHandler getSaveModeHandler(String daffodilHome) throws DException { 305 if (saveModeHandler != null) 306 return saveModeHandler; 307 synchronized (this) { 308 if (saveModeHandler != null) 309 return saveModeHandler; 310 saveModeHandler = new SaveModeHandler(databaseURL, daffodilHome); 311 } 312 return saveModeHandler; 313 } 314 315 public synchronized DeadLockDetector getDeadLockDetector(QualifiedIdentifier tableName){ 316 DeadLockDetector deadLockDetector = (DeadLockDetector)deadLockDetectorMap.get(tableName.getIdentifier() ) ; 317 if(deadLockDetector!=null) 318 return deadLockDetector; 319 deadLockDetector = new DeadLockDetector(); 320 deadLockDetectorMap.put(tableName.getIdentifier() , deadLockDetector) ; 321 return deadLockDetector; 322 } 323 324 325 } 326 | Popular Tags |