KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > datasystem > persistentsystem > FreeSpaceManager


1 package com.daffodilwoods.daffodildb.server.datasystem.persistentsystem;
2
3 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*;
4 import com.daffodilwoods.daffodildb.server.datasystem.btree.*;
5 import com.daffodilwoods.daffodildb.server.datasystem.indexsystem.*;
6 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*;
7 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.Utility;
8 import com.daffodilwoods.daffodildb.utils.*;
9 import com.daffodilwoods.daffodildb.utils.byteconverter.*;
10 import com.daffodilwoods.daffodildb.utils.comparator.*;
11 import com.daffodilwoods.database.general.*;
12 import com.daffodilwoods.database.resource.*;
13
14 /**
15  * FreeSpaceManager reuses the space in clusters which becomes free because of deletion of records.
16  * In daffodildb there is a system table known as FREE SPACE INFO.It has all the information about the
17  * free spaces in clusters means how much free space a cluster has in a particular table.Free Space
18  * manager use this table for getting the appropriate cluster.We use indexing technique on FREE SPACE
19  * INFO table for fast searching of desired cluster.
20  */

21 public class FreeSpaceManager {
22
23     /**
24      * It has all the information about the free spaces in clusters means how much free space a cluster
25      * has in a particular table.Free Space manager use this table for getting the appropriate cluster.
26      */

27
28    private _Table freeSpaceInfo;
29
30     /**
31      * Index on FreeSpaceInfoTable for fast searching
32      */

33    private _Index indexOnFreeSpaceInfo;
34
35     /**
36      * to compare bytes of table Name
37      */

38    private ByteComparator nameComparator;
39
40
41     public FreeSpaceManager(_Table freeSpaceInfo0) {
42        CTusjohJoTfotjujwfDpnqbsbups stringComparator = new CTusjohJoTfotjujwfDpnqbsbups();
43         nameComparator = new ByteComparator(new boolean[] {true,true,true},new SuperComparator[] {stringComparator,stringComparator,stringComparator});
44         freeSpaceInfo = freeSpaceInfo0;
45     }
46
47     /**
48      * It checks whether specified tableName and ClusterAddress exists in FreeSpaceInfoTable or not.
49      * If it does not exist then inserts tableName, clusteraddrress and freepace of this cluster in
50      * FreeSpaceInfoTable and btree on FreeSpaceInfoTable otherwise updates freeSpace of this cluster
51      * in FreeSpaceInfoTable and on Btree
52      *
53      * @param user To perform write operation
54      * @param tableName Name of table
55      * @param clusterAddress cluster address whose freeSpace has to maintain
56      * @param freeSpace freeSpace in specified Cluster
57      */

58
59      void addCluster(_DatabaseUser user,QualifiedIdentifier tableName,int clusterAddress,long freeSpace) throws DException{
60         if(tableName.equals(SystemTables.FREESPACEINFO) || tableName.equals(SystemTables.COLUMNINFO)|| tableName.equals(SystemTables.DATABASEINDEXCOLUMNS))
61             return;
62         DatabaseUserTableIterator tableIterator = (DatabaseUserTableIterator)((Table)freeSpaceInfo).getIterator();
63         BufferRange[] keyToSeek = getKey(tableName,clusterAddress);
64         try{
65             seek(tableName,keyToSeek);
66             BufferRange[] values = new BufferRange[]{keyToSeek[0],keyToSeek[1],keyToSeek[2],keyToSeek[3],new BufferRange(CCzufDpowfsufs.getBytes(freeSpace))};
67             tableIterator.insert(user,values);
68             indexOnFreeSpaceInfo.insert(user,keyToSeek,tableIterator.getKey());
69         }catch(DException de){
70             BTreeElement element = (BTreeElement)de.getParameters()[0];
71             Object JavaDoc keyValue = element.getValue();
72             tableIterator.move(keyValue);
73             byte[] [] values = new byte[][]{CCzufDpowfsufs.getBytes(freeSpace)};
74             tableIterator.update(user,new int[]{4},values);
75             Object JavaDoc newBtreeValue = tableIterator.getKey();
76             indexOnFreeSpaceInfo.update(user,keyToSeek,keyToSeek,keyValue,newBtreeValue);
77         }
78     }
79
80     /**
81      * updates freeSpace of this cluster in FreeSpaceInfoTable and on Btree of FreeSpaceInfoTable if this
82      * cluster exists in FreeSpaceInfoTable
83      *
84      * @param user To perform write operation
85      * @param tableName Name of table
86      * @param clusterAddress cluster address whose freeSpace has to maintain
87      * @param freeSpace freeSpace in specified Cluster
88      */

89
90      void updateCluster(_DatabaseUser user,QualifiedIdentifier tableName,int clusterAddress,long freeSpace) throws DException{
91         if(tableName.equals(SystemTables.FREESPACEINFO) || tableName.equals(SystemTables.COLUMNINFO)|| tableName.equals(SystemTables.DATABASEINDEXCOLUMNS))
92             return;
93         DatabaseUserTableIterator tableIterator = (DatabaseUserTableIterator)((Table)freeSpaceInfo).getIterator();
94         BufferRange[] keyToSeek = getKey(tableName,clusterAddress);
95         try{
96             seek(tableName,keyToSeek);
97         }
98         catch(DException de){
99             BTreeElement element = (BTreeElement)de.getParameters()[0];
100             Object JavaDoc keyValue = element.getValue();
101             tableIterator.move(keyValue);
102             byte[] [] values = new byte[][]{CCzufDpowfsufs.getBytes(freeSpace)};
103             tableIterator.update(user,new int[]{4},values);
104             Object JavaDoc newBtreeValue = tableIterator.getKey();
105             indexOnFreeSpaceInfo.update(user,keyToSeek,keyToSeek,keyValue,newBtreeValue);
106         }
107     }
108
109     /**
110      * Checks whether specified recordSize can be insertes in any Previous cluster of specified tablenName
111      * If such cluster exists then return that cluster adddress otherwise return -1
112      *
113      * @param user To perform write operation
114      * @param tableName Name of table
115      * @param recordSize Size of record which has to insert
116      *
117      * @return cluster adddress having freespace more than recordsize otherwise return -1
118      */

119
120      long getCluster(_DatabaseUser user,QualifiedIdentifier tableName,long recordSize) throws DException{
121         DatabaseUserTableIterator tableIterator = (DatabaseUserTableIterator)((Table)freeSpaceInfo).getIterator();
122         BufferRange[] bytesOfTableName = Utility.getBufferRangeForTableName(tableName);
123         try{
124             seek(tableName,bytesOfTableName);
125         }catch(DException de ){
126            BTreeElement element = (BTreeElement)de.getParameters()[0];
127            tableIterator.move(element.getValue());
128            do{
129                 BufferRange[] values = (BufferRange[])tableIterator.getColumnValues();
130                 if(nameComparator.compare(bytesOfTableName,new BufferRange[]{values[0],values[1],values[2]})==0){
131                     long freeSpace = CCzufDpowfsufs.getLong(values[4].getBytes()).longValue();//((Long)values[4]).longValue();
132
if(freeSpace > recordSize){
133                         Object JavaDoc oldKeyValue = tableIterator.getKey();
134                         tableIterator.update(user,new int[]{4},new byte[][]{CCzufDpowfsufs.getBytes(new Long JavaDoc(freeSpace - recordSize))});
135                         BufferRange[] btreeKey = new BufferRange[] {bytesOfTableName[0],bytesOfTableName[1],bytesOfTableName[2],values[3]};
136                         Object JavaDoc newBtreeValue = tableIterator.getKey();
137                         indexOnFreeSpaceInfo.update(user,btreeKey,btreeKey,oldKeyValue,newBtreeValue);
138                         return CCzufDpowfsufs.getLong(values[3].getBytes()).longValue();
139                     }
140                 }
141             }while(tableIterator.next());
142         }
143         return -1;
144     }
145
146     /**
147      * This method doesn't used
148      *
149      * Deletes cluster Address and tableName from FreeSpaceInfoTable and on Btree of FreeSpaceInfoTable
150      *
151      * @param user To perform write operation
152      * @param tableName Name of table
153      * @param clusterAddress cluster address which has to delete from FreeSpaceInfoTable
154      */

155
156      void deleteCluster(_DatabaseUser user,QualifiedIdentifier tableName,int clusterAddress)throws DException{
157         DatabaseUserTableIterator tableIterator = (DatabaseUserTableIterator)((Table)freeSpaceInfo).getIterator();
158         BufferRange[] keyToSeek = getKey(tableName,clusterAddress);
159         try{
160             seek(tableName,keyToSeek);
161         }catch(DException de){
162             BTreeElement element = (BTreeElement)de.getParameters()[0];
163             Object JavaDoc keyValue = element.getValue();
164             tableIterator.move(keyValue);
165             tableIterator.delete(user);
166             indexOnFreeSpaceInfo.delete(user,keyToSeek,keyValue);
167        }
168     }
169
170    private void seek(QualifiedIdentifier tableName,BufferRange[] indexKey) throws DException{
171          try{
172             Object JavaDoc obj = indexOnFreeSpaceInfo.seek(indexKey);
173             if(obj != null){
174                 throw new DException("DSE2037",new Object JavaDoc[] {obj});
175             }
176          }catch(NullPointerException JavaDoc ex ){
177              if(tableName.equals(SystemTables.TABLEINFO) || tableName.equals(SystemTables.COLUMNINFO) || tableName.equals(SystemTables.DATABASEINDEXINFO) || tableName.equals(SystemTables.DATABASEINDEXCOLUMNS) || tableName.equals(new QualifiedIdentifier(SystemTables.TABLEINFO.catalog,SystemTables.TABLEINFO.schema,SystemTables.TABLEINFO.name+"TEMP")) || tableName.equals(new QualifiedIdentifier(SystemTables.COLUMNINFO.catalog,SystemTables.COLUMNINFO.schema,SystemTables.COLUMNINFO.name+"TEMP"))){
178              }
179              else{
180          ;//// Removed By Program ** ex.printStackTrace();
181
throw new DException("DSE2025",new Object JavaDoc[]{"null pointer Exception"});
182              }
183            }
184     }
185
186
187    void setBtree(_Index indexOnFreeSpaceInfo0) throws DException{
188       indexOnFreeSpaceInfo = indexOnFreeSpaceInfo0;
189   }
190
191   private BufferRange[] getKey(QualifiedIdentifier tableName, int clusterAddress){
192       BufferRange bytesofClusterAddress = new BufferRange(CCzufDpowfsufs.getBytes(clusterAddress));
193       BufferRange[] bytesOfTableName = Utility.getBufferRangeForTableName(tableName);
194       BufferRange[] keyToSeek = new BufferRange[] {bytesOfTableName[0],bytesOfTableName[1],bytesOfTableName[2],bytesofClusterAddress};
195       return keyToSeek;
196   }
197
198
199
200
201 }
202
Popular Tags