KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > datasystem > mergesystem > MergeDatabase


1 package com.daffodilwoods.daffodildb.server.datasystem.mergesystem;
2
3 import java.util.*;
4 import com.daffodilwoods.daffodildb.server.datasystem.indexsystem.*;
5 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*;
6 import com.daffodilwoods.daffodildb.utils.comparator.*;
7 import com.daffodilwoods.database.general.*;
8 import com.daffodilwoods.database.resource.*;
9 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.versioninfo.VersionHandler;
10 import com.daffodilwoods.daffodildb.server.sessionsystem.sessionversioninfo.SessionVersionHandler;
11 import java.io.File JavaDoc;
12 import java.io.*;
13 import com.daffodilwoods.daffodildb.server.datasystem.indexsystem._IndexInformation;
14
15 /**
16  *
17  * <p>Title: MergeDatabase</p>
18  * <p>Description: It handles all the table level operations on both the
19  * file and the memory database.It maintians a WeakOrderedKeyList for all
20  * the tables of database which are currently in use.
21  */

22 public class MergeDatabase implements _IndexDatabase {
23
24    /**
25     * To handle table level operations on File
26     */

27    private _IndexDatabase fileDatabase;
28
29    /**
30     * To handle table level operations on memory
31     */

32    private _IndexDatabase memoryDatabase;
33
34    /**
35     * Used to maintain a list of tables in the database.
36     */

37
38    private WeakOrderedKeyList tablesMap;
39
40
41    SessionVersionHandler sessionVersionHandler;
42    VersionHandler versionHandler;
43    /**
44     * Constructs the merge database with two databases,One for memory
45     * database and the other for file database and Initializes map
46     * to maintain tables which are currently in use.
47     * @param fileMemoryDatabase0 To handle table level operations on memory
48     * @param fileDatabase0 To handle table level operations on file
49     * */

50
51    public MergeDatabase(_IndexDatabase fileMemoryDatabase0, _IndexDatabase fileDatabase0) throws DException {
52       fileDatabase = fileDatabase0;
53       memoryDatabase = fileMemoryDatabase0;
54       tablesMap = new WeakOrderedKeyList(new CTusjohDbtfJoTfotjujwfDpnqbsbups());
55       versionHandler = ( (IndexDatabase) fileDatabase).getVersionHandler();
56       try {
57          if (versionHandler.getDbVersion() >= 3.2) {
58             sessionVersionHandler = (SessionVersionHandler) Class.forName("com.daffodilwoods.daffodildb.server.sessionsystem.sessionversioninfo.SessionVersionHandler3_2").newInstance();
59          } else
60             sessionVersionHandler = (SessionVersionHandler) Class.forName("com.daffodilwoods.daffodildb.server.sessionsystem.sessionversioninfo.SessionVersionHandlerUpto3_1").newInstance();
61       } catch (ClassNotFoundException JavaDoc ex) {
62          throw new DException("DSE5591", null);
63       } catch (IllegalAccessException JavaDoc ex) {
64          throw new DException("DSE5591", null);
65       } catch (InstantiationException JavaDoc ex) {
66          throw new DException("DSE5591", null);
67       }
68
69    }
70
71    /**
72     * creates table in file database and in memory database, It throws
73     * Exception if table already exists
74     * @param tableName name of the table
75     * @param columnInformation all column information of the table
76     * as column names,types,size and index
77     * @throws DException if the table already exists in the map.
78     */

79
80    public synchronized void createTable(QualifiedIdentifier tableName, Object JavaDoc columnInformation) throws DException {
81       _Table table = (_Table) tablesMap.get(tableName.getIdentifier());
82       if (table != null)
83          throw new DException("DSE1135", new Object JavaDoc[] {tableName.toString()});
84       fileDatabase.createTable(tableName, columnInformation);
85    }
86
87    /**
88     * drops table in file database and in memory database and removes
89     * entry of table from map.
90     * @param tableName name of the table which has to drop.
91     * @throws DException if table does not exist .
92     */

93
94    public synchronized void dropTable(QualifiedIdentifier tableName) throws DException {
95       fileDatabase.dropTable(tableName);
96       tablesMap.remove(tableName.getIdentifier());
97    }
98
99    /**
100     * returns the table if found in map with given name. if table
101     * does not exist, gets table from memoryDatabase and FileDatabase and
102     * makes mergeTable and makes mergeTable entry in map
103     * @param tableName name of the table
104     * @return a table with the given name from the database
105     */

106
107    public synchronized _Table getTable(QualifiedIdentifier tableName) throws DException {
108       _Table table = (_Table) tablesMap.get(tableName.getIdentifier());
109       if (table != null)
110          return table;
111       _IndexTable fileTable = (_IndexTable) fileDatabase.getTable(tableName);
112       _IndexInformation[] iinfs = fileTable.getIndexInformations();
113       _IndexTable memoryTable = null;
114       try {
115          memoryTable = (_IndexTable) ( (_IndexDatabase) memoryDatabase).getTable(tableName, iinfs);
116       } catch (DException e) {
117          memoryDatabase.createTable(tableName, ( (_IndexTable) fileTable).getTableCharacteristics());
118          memoryTable = (_IndexTable) ( (_IndexDatabase) memoryDatabase).getTable(tableName, iinfs);
119       }
120       table = new MergeTable(tableName, (_IndexTable) memoryTable, (_IndexTable) fileTable, sessionVersionHandler, this);
121       tablesMap.put(tableName.getIdentifier(), table);
122       return table;
123    }
124
125    /**
126     * retreives all the tables that have been created in the database, in the form of a list.
127     * @return an arraylist of the tables in the database
128     */

129
130    public synchronized ArrayList getAllTables() throws DException {
131       ArrayList tables = new ArrayList();
132       WeakOrderedKeyList.WeakOrderedKeyListIterator iterator = tablesMap.getWeakOrderedKeyListIterator();
133       if (iterator.top()) {
134          tables.add(iterator.getKey());
135       }
136       return tables;
137    }
138
139    /**
140     * Creates index on the memory table with the given parameters
141     *
142     * @param tableName name of the table
143     * @param indexName name of the index
144     */

145
146    public synchronized void createTemporaryIndex(QualifiedIdentifier tableName, String JavaDoc indexName, _IndexInformation indexInformationGetter) throws DException {
147       memoryDatabase.createTemporaryIndex(tableName, indexName, indexInformationGetter);
148    }
149
150    /**
151     * creates index on the file table with the given parameters
152     *
153     * @param tableName name of the table
154     * @param indexName name of the index
155     */

156
157    public synchronized void createPermanantIndex(QualifiedIdentifier tableName, String JavaDoc indexName, _IndexInformation indexInformationGetter, _DatabaseUser user) throws DException {
158       fileDatabase.createPermanantIndex(tableName, indexName, indexInformationGetter, user);
159       memoryDatabase.createTemporaryIndex(tableName, indexName, indexInformationGetter);
160    }
161
162    /**
163     * drops the index from the memory table with the given parameters
164     * @param tableName name of the table
165     * @param indexName name of the index
166     */

167
168    public void dropTemporaryIndex(QualifiedIdentifier tableName, String JavaDoc indexName) throws DException {
169       memoryDatabase.dropTemporaryIndex(tableName, indexName);
170    }
171
172    /**
173     * drops the index from the file table with the given parameters
174     * @param tableName name of the table
175     * @param indexName name of the index
176     */

177
178    public synchronized void dropPeramanantIndex(QualifiedIdentifier tableName, String JavaDoc indexName, _DatabaseUser user) throws DException {
179       fileDatabase.dropPeramanantIndex(tableName, indexName, user);
180    }
181
182    public _DatabaseUser getDatabaseUser() throws DException {
183       return fileDatabase.getDatabaseUser();
184    }
185
186    public _Table getSystemTable(QualifiedIdentifier parm1) throws com.daffodilwoods.database.resource.DException {
187       /**@todo Implement this com.daffodilwoods.daffodildb.server.datasystem.interfaces._Database method*/
188       throw new java.lang.UnsupportedOperationException JavaDoc("Method getSystemTable() not yet implemented.");
189    }
190
191    public synchronized void removeTable(QualifiedIdentifier tableName) throws DException {
192       tablesMap.remove(tableName.getIdentifier());
193       fileDatabase.removeTable(tableName);
194       memoryDatabase.removeTable(tableName);
195    }
196
197    public void alterTable(QualifiedIdentifier tableName, Object JavaDoc columnInfo, _AlterRecord alterRecord, Object JavaDoc defaultValue, _DatabaseUser user) throws DException {
198       fileDatabase.alterTable(tableName, columnInfo, alterRecord, defaultValue, user);
199       try {
200          memoryDatabase.dropTable(tableName);
201       } catch (DException ex) {
202          if (!ex.getDseCode().equalsIgnoreCase("DSE959"))
203             throw ex;
204       }
205    }
206
207    public _DatabaseUser getTempDatabaseUser() throws DException {
208       return fileDatabase.getTempDatabaseUser();
209    }
210
211    public _IndexDatabase getMemoryDatabase() throws DException {
212       return memoryDatabase;
213    }
214
215    public _IndexDatabase getFileDatabase() throws DException {
216       return fileDatabase;
217    }
218
219    public _DatabaseUser getDatabaseUser(ArrayList tableNames) throws DException {
220       return fileDatabase.getDatabaseUser(tableNames);
221    }
222
223    public void addFreeCluster(_DatabaseUser user, int address) throws DException {
224    }
225
226    public void getStatus() throws DException {
227       ( (IndexDatabase) fileDatabase).getStatus();
228       ( (TempIndexDatabase) memoryDatabase).getStatus();
229    }
230
231    public void createFullTextIndex(QualifiedIdentifier tableName, String JavaDoc indexName, String JavaDoc[] columnName) throws DException {
232       if (!getVersionHandler().isFullTextSupported())
233          throw new DException("DSE5590", new Object JavaDoc[] {"createFullTextIndex"});
234       fileDatabase.createFullTextIndex(tableName, indexName, columnName);
235    }
236
237    public void dropFullTextIndex(QualifiedIdentifier tableName, String JavaDoc indexName) throws DException {
238       fileDatabase.dropFullTextIndex(tableName, indexName);
239    }
240
241    public void setDropStatus(QualifiedIdentifier tableName, boolean dropTable) throws DException {
242       _MergeTable mergeTable = (_MergeTable) tablesMap.get(tableName.getIdentifier());
243       if (mergeTable != null)
244          (mergeTable).setTableDropped(dropTable);
245
246    }
247
248    public VersionHandler getVersionHandler() {
249       return versionHandler;
250    }
251
252    public SessionVersionHandler getSessionVersionHandler() {
253       return sessionVersionHandler;
254    }
255
256    public _Table getTable(QualifiedIdentifier tableName, _IndexInformation[] iinf) throws DException {
257       throw new java.lang.UnsupportedOperationException JavaDoc("Method getTable(QualifiedIdentifier tableName,_IndexInformation[] iinf) not yet implemented.");
258    }
259 /*
260     In releaseTable we remove table from file database map and drop memory table and memory indexes.
261     It is called from finalize of the merge table.
262     - Firstly we check that if it is present in map or not because some times java remove firstly
263        from weak key list map and then call its finalize but some times it calles finalize firstly.
264     - So if it is not present in map then we remove it from file database and drop memory indexes and table.
265     - But if we got is from map then there is two posibilities
266           1) It can happen that it is not removed from map by java.
267           2) It is removed from map by java but now another user wants to get it and create a new one
268               and put it into map.
269      - Now for identifing the actual case we maintain a flag in merge table and set it true if it
270         is to be finalized. If this flag is found set then we can reomve it from tables map
271         otherwise we simply reuturn.
272     */

273
274    public synchronized void releaseTable(QualifiedIdentifier tableName, _IndexTable memoryTable) throws DException {
275      _Table table = (_Table)tablesMap.get(tableName.getIdentifier() ) ;
276        if(table != null )
277         if( ((MergeTable)table).isToBeFinalized )
278           tablesMap.remove(tableName.getIdentifier());
279         else
280          return ;
281
282       fileDatabase.removeTable(tableName);
283
284       _IndexInformation[] iinf = memoryTable.getIndexInformations();
285       for (int i = 0; i < iinf.length; i++) {
286          memoryDatabase.dropTemporaryIndex(tableName, iinf[i].getIndexName());
287       }
288       memoryDatabase.dropTable(tableName);
289    }
290 }
291
Popular Tags