KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sessionsystem > SessionDatabase


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 /**
22  *
23  * <p>Title: SessionDatabase</p>
24  * <p>Description:</p>
25  * Session Database is Database specific whose main perpose is to provide a session for a Database,
26  * it also provides the information like constraint to SessionSystem, getting chid session for a different operations.
27  */

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 JavaDoc databaseURL;
44    private HashMap deadLockDetectorMap;
45    /**
46     * @param sessionSystem
47     * @param dataDictionary
48     * @param cacheDatabase
49     * @param rowIdFetcher
50     * @param deferredConstraintDatabase
51     * @param constraintDatabase
52     * @throws DException
53     */

54
55    public SessionDatabase(SessionSystem sessionSystem, _DataDictionary dataDictionary, _Database cacheDatabase, RowIdFetcher rowIdFetcher, _ConstraintDatabase deferredConstraintDatabase, _ConstraintDatabase constraintDatabase, String JavaDoc corruptionChecking, String JavaDoc 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    /**
84     * returns an Object of DataDictonary.
85     * @return _DataDictonary
86     * @throws DException
87     */

88
89    public _DataDictionary getDataDictionary() throws DException {
90       return dataDictionary;
91    }
92
93    /**
94     * returns the merge Database
95     * @return _Database
96     * @throws DException
97     */

98
99    public _Database getMergeDatabase() throws DException {
100       return mergeDatabase;
101    }
102
103    /**
104     * returns systemFieldsValue
105     * @return SystemFieldsValue
106     * @throws DException
107     */

108
109    public SystemFieldsValue getSystemFieldsValue() throws DException {
110       return systemFieldsValue;
111    }
112
113    /**
114     * Returns a Session for a user to work on.
115     * <br><br>
116     * if sessionConstant is null it returns new Session by passing user, session constant, and session properties
117     * if session properties is null it returns existing session by passing session constant
118     * otherwise it fetches a new session.
119     * @param user
120     * @param sessionConstant
121     * @param sessionProperties
122     * @return _Session
123     * @throws DException
124     */

125
126    public _Session getSession(String JavaDoc user, Object JavaDoc 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    /**
138     * returns the session user is working on has done any work.
139     * @param sessionConstant
140     * @return _Session
141     * @throws DException
142     */

143
144    private _Session getExistingSession(Object JavaDoc 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    /**
152     * returns the new Session according to sessionConstant.
153     * @param user
154     * @param sessionConstant
155     * @param sessionProperties
156     * @return _Session
157     * @throws DException
158     */

159
160    private _Session getNewSession(String JavaDoc user, Object JavaDoc sessionConstant, Properties sessionProperties) throws DException {
161       _Session session = null;
162       Object JavaDoc 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 JavaDoc 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       /** @todo check the code uncomment the above line */
181
182
183       String JavaDoc constrintChecking = sessionProperties.getProperty(_Server.CONSTRAINTCHECKING, "true");
184       if (constrintChecking.equalsIgnoreCase("false"))
185          ( (Session) session).stopConstraintChecking();
186       return session;
187    }
188
189    /**
190     * returns an Obejct of global session.
191     * @return _GlobalSession
192     * @throws DException
193     */

194
195    public _ServerSession getGlobalSession() throws DException {
196       return globalSession;
197    }
198
199    /**
200     * returns row ID for the last row in the table.
201     * @param tableName
202     * @return TableRowIdGetter
203     * @throws DException
204     */

205
206    public TableRowIdGetter getTableRowIdGetter(QualifiedIdentifier tableName) throws DException {
207       return rowIdFetcher.getTableRowIdGetter(tableName);
208    }
209
210    /**
211     * returns constraint table of Deffered mode transactions.
212     * @param tableName
213     * @return _ConstraintTable
214     * @throws DException
215     */

216
217    public _ConstraintTable getDeferrableConstraintTable(QualifiedIdentifier tableName) throws DException {
218       return deferredConstraintDatabase.getConstraintTable(tableName);
219    }
220
221    /**
222     * retunrs Constraitn table.
223     * @param tableName
224     * @return _ConstraintTable
225     * @throws DException
226     */

227
228    public _ConstraintTable getConstraintTable(QualifiedIdentifier tableName) throws DException {
229       return constraintDatabase.getConstraintTable(tableName);
230    }
231
232    /**
233     *
234     * <p>Title: Object Comparator </p>
235     * <p>Description: </p>
236     * implements Comparator, and compares two Object
237     */

238
239    public static class CPckfduDpnqbsbups extends SuperComparator /*implements Comparator*/ {
240       public int compare(Object JavaDoc obj1, Object JavaDoc obj2) {
241          if (obj1 == null)
242             return obj2 == null ? 0 : -1;
243          return obj2 == null ? 1 : ( (Comparable JavaDoc) obj1).compareTo(obj2);
244       }
245    }
246
247    /**
248     * removes the table name from all session table list (weak ordered key list iterator).
249     * @param tableName
250     * @param dropTable
251     * @throws DException
252     */

253
254    public void removeTable(QualifiedIdentifier tableName, boolean dropTable) throws DException {
255       /** @todo uncomment the next line and add removeTable method in mergeDatabase */
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 JavaDoc 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 JavaDoc isComplete = CCzufDpowfsufs.getBoolean(bytes);
300       if (!isComplete.booleanValue())
301          throw new DException("DSE5544", null);
302    }
303
304    public SaveModeHandler getSaveModeHandler(String JavaDoc 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