KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.daffodilwoods.daffodildb.server.datasystem.persistentsystem;
2
3 import com.daffodilwoods.daffodildb.utils.byteconverter.CCzufDpowfsufs;
4 import com.daffodilwoods.database.resource.DException;
5 import java.io.*;
6 import java.lang.ref.*;
7 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.versioninfo.VersionHandler;
8
9 /**
10  * The smallest entity in Persistent System is Cluster which is frequently used to perform it?s operations
11  * (read or write) on database file.Persistent System defines a cluster as collection of fixed number of
12  * consecutive bytes.
13  */

14
15 public class Cluster implements DatabaseConstants{
16
17     /**
18      * Characterisitics of this cluster having it's adress in database file and it's size.
19      */

20
21     ClusterCharacteristics clusterCharacteristics;
22
23     /**
24      * all the bytes related to this cluster.
25      */

26
27     protected byte[] clusterBytes;
28
29     /**
30      * actual record count in that Cluster
31      */

32
33     protected short actualRecordCount;
34
35     /**
36      * active record count in that cluster
37      */

38
39     protected short activeRecordCount;
40
41     /**
42      * Array Having StartAddress of all records In that Cluster for cache.
43      */

44
45     private short[] columnPositions;
46
47     /**
48      * Whether it is used for writting or reading
49      * true : read
50      * false : write
51      */

52
53     public boolean readMode;
54
55     /**
56      * isDirty true if clusterBytes in database file and in cluster manager map are not identical , otherwise false.
57      * in other words if bytes are changed by user operation so to save it in database file.
58      */

59
60     private boolean isDirty;
61
62     /**
63      * Database properties for using database constants such as cluster size.
64      */

65     public DatabaseProperties databaseProperties;
66
67     /**
68      * versionHandler for database constants of particular version.
69      */

70     protected VersionHandler versionHandler;
71     /**
72      * Used in variable record cluster
73      */

74     protected short recordsToBeShifted = 0;
75
76     public Cluster(ClusterCharacteristics clusterCharacteristics1,DatabaseProperties databaseProperties0,VersionHandler versionHandler0) throws DException {
77         clusterCharacteristics = clusterCharacteristics1;
78         databaseProperties = databaseProperties0;
79         versionHandler =versionHandler0;
80     }
81
82     /**
83      * updates clusterBytes by the byte at the position passed.
84      *
85      * @param position : position of byte to update
86      * @param value : new byte
87      */

88
89     public final void updateByte(int position,byte value) throws DException{
90         setDirty(true);
91         clusterBytes[position] = value;
92     }
93
94     /**
95      * updates clusterBytes by the byte array starting from the position passed.
96      *
97      * @param position : position from where updation starts
98      * @param value : new byte array
99      */

100
101     public final void updateBytes(int position,byte[] value) throws DException{
102         setDirty(true);
103         System.arraycopy(value,0,clusterBytes,position,value.length);
104     }
105     /**
106      * updates cluster bytes from specified position and from given bytes array
107      * and from specified index and length also.
108      * @param source byte[] - new bytes which is to be stored in cluster bytes
109      * @param start int - position of new bytes from where we starts to copy bytes into cluster bytes.
110      * @param targetPosition int - position of cluster bytes array where these new bytes to be copied.
111      * @param length int - number of bytes which are to be copied from new byte array
112      * to cluster bytes.
113      * @throws DException
114      */

115     public final void updateBytes(byte[] source,int start,int targetPosition,int length) throws DException{
116         setDirty(true);
117           System.arraycopy(source,start,clusterBytes,targetPosition,length);
118     }
119
120     /**
121      * Return cluster bytes
122      */

123
124     public final byte[] getBytes() {
125         return clusterBytes;
126     }
127     /**
128      * Compares that the cluster and object passed are same or note by comparing
129      * there cluster characterstics.
130      * @param object Object - to be comared with this cluster
131      * @return boolean - returns true if both objects have same characterstics.
132      */

133     public final boolean equals(Object JavaDoc object) {
134         return clusterCharacteristics.equals(((Cluster)object).clusterCharacteristics);
135     }
136
137     /**
138      * Sets bytes in the new cluster without initialization of parameters
139      * as garbage values are present there in case of Windows 98
140      */

141
142     public void setBytes(byte[] bytes ) throws DException {
143         clusterBytes = bytes;
144     }
145
146     /**
147         * Initializes cluster bytes ,actual record, active record of Cluster and initializes Column Position array.
148         * @param bytes : bytes of Cluster read from file
149         *
150         */

151
152     protected void setBytesWithInitializeParameters(byte[] bytes ) throws DException {
153           clusterBytes = bytes;
154           initializeParameters();
155       }
156
157
158     /**
159      * Returns the characteristics of next Cluster to it.
160      */

161
162     public ClusterCharacteristics getNextClusterCharacteristics() throws DException{
163         int add = CCzufDpowfsufs.getIntValue(clusterBytes,databaseProperties.NEXTCLUSTERADDRESS);
164         return add == 0 ? null : new ClusterCharacteristics(add,false);
165     }
166
167     /**
168      * Returns the characteristics of previous Cluster of it.
169      */

170
171     public ClusterCharacteristics getPreviousClusterCharacteristics() throws DException{
172         int add = CCzufDpowfsufs.getIntValue(clusterBytes,versionHandler.PREVIOUSCLUSTERADDRESS);
173         return add == 0 ? null : new ClusterCharacteristics(add,false);
174     }
175
176     /**
177      * Sets nextCluster characteristics of this Cluster.
178      */

179
180     void setNextCluster(Cluster nextCluster) throws DException{
181         byte[] addressBytes = CCzufDpowfsufs.getBytes(nextCluster.clusterCharacteristics.getStartAddress());
182         updateBytes(databaseProperties.NEXTCLUSTERADDRESS,addressBytes);
183         byte[] previousClusterAddress = CCzufDpowfsufs.getBytes(clusterCharacteristics.getStartAddress());
184         nextCluster.updateBytes(versionHandler.PREVIOUSCLUSTERADDRESS,previousClusterAddress);
185     }
186
187     public String JavaDoc toString() {
188         return "["+clusterCharacteristics.toString() +"] --- ["+activeRecordCount +"] --- ["+actualRecordCount+"]";
189     }
190
191     /**
192      * returns the number of free bytes in this cluster
193      * by calculaing used space with help of actualRecords , and insertable address etc.
194      */

195
196     public int freeSpace() throws DException {
197         return (databaseProperties.CLUSTERSIZE - CCzufDpowfsufs.getShortValue(clusterBytes,0) - versionHandler.NEWADDRESSLENGTH - actualRecordCount * versionHandler.LENGTH - versionHandler.UPDATEBYTES);
198     }
199
200     /**
201      * Returns the startposition of the record of recordnumber passed.Start position means the position from where it's bytes are written
202      * in this cluster i.e.insertable address of a record.
203      * Firstly it checks it in column positions array ( with --recordNumber is
204      * used because array index starts with 0 and we starts records with 1 )if it
205      * gets 0 then it calcualte it with help of record start pointer
206      * maintained at end of cluster and also put its entry in column positions array
207      * otherwise value at that position in array is returned.
208      *
209      */

210
211     short getStartPointerOfRecord(short recordNumber) throws DException{
212         return columnPositions[--recordNumber] == 0 ? columnPositions[recordNumber] = CCzufDpowfsufs.getShortValue(clusterBytes,databaseProperties.CLUSTERSIZE - versionHandler.NEWADDRESSLENGTH - (recordNumber + 1) * versionHandler.LENGTH)
213              : columnPositions[recordNumber];
214  }
215
216
217
218
219
220
221 /*
222     short getStartPointerOfRecord(short recordNumber) throws DException{
223         short a = -1;
224         short aa = recordNumber;
225         try {
226             a = columnPositions[--recordNumber] == 0 ? columnPositions[recordNumber] = CCzufDpowfsufs.getShortValue(clusterBytes,databaseProperties.CLUSTERSIZE - NEWADDRESSLENGTH - (recordNumber + 1) * LENGTH)
227                 : columnPositions[recordNumber];
228         }
229         catch (ArrayIndexOutOfBoundsException ex) {
230             throw new DException("DSE0",new Object[] {ex.getMessage()});
231         }
232         catch (NullPointerException nex) {
233             throw new DException("DSE0",new Object[] {nex.getMessage()});
234         }
235         return a;
236     }
237 */

238 /**
239  * Setting mode of cluster is for read or write
240  * @param mode boolean true for read mode and false for write mode.
241  */

242
243     public void setMode(boolean mode) {
244         readMode = mode;
245     }
246
247     /**
248      * Not Used Now
249      */

250
251     public void rollBack() {
252         clusterBytes = null;
253     }
254
255     /**
256      * initializes the cluster's parameters like activeRecordCount,actualRecordCount,columnPositions etc.
257      */

258
259     void initializeParameters() throws DException{
260         if(clusterCharacteristics.getStartAddress() != 0){
261             try{
262                 actualRecordCount = CCzufDpowfsufs.getShortValue( clusterBytes,versionHandler.LENGTH);
263                 activeRecordCount = CCzufDpowfsufs.getShortValue(clusterBytes,2*versionHandler.LENGTH);
264                 recordsToBeShifted = activeRecordCount;
265                 columnPositions = new short[actualRecordCount];
266             }
267             catch(Exception JavaDoc ne){
268               throw new DException("DSE0", new Object JavaDoc[] {ne.getMessage()});
269             }
270         }
271     }
272
273     /**
274      * update the cluster bytes for activeRecordCount,actualRecordCount,insertable address.
275      *
276      * @param add insertable address.
277      */

278
279     protected void updateClusterInformation(short add) throws DException {
280         if ( add > 0 )
281             updateBytes(0, CCzufDpowfsufs.getBytes(add) );
282         updateBytes(versionHandler.LENGTH , CCzufDpowfsufs.getBytes(actualRecordCount));
283         updateBytes( 2* versionHandler.LENGTH ,CCzufDpowfsufs.getBytes(activeRecordCount) );
284     }
285
286     /**
287      * @return short returning actual records count of cluster.
288      */

289     public short getActualRecordCount(){
290         return actualRecordCount;
291     }
292     /**
293      *
294      * @return short returning currently active record count (i.e. which are not deleted)
295      */

296     public short getActiveRecordCount(){
297         return activeRecordCount;
298     }
299
300     /**
301      *
302      * @return int returns address of cluster
303      */

304     protected int getClusterAddress(){
305         return clusterCharacteristics.getStartAddress();
306     }
307     /**
308      *
309      * @return ClusterCharacteristics
310      */

311     public ClusterCharacteristics getClusterCharacteristics(){
312         return clusterCharacteristics;
313     }
314
315     /**
316      * Every cluster have previouse and next cluster link but first
317      * cluster of a table doesn't have any previous so
318      * We maintains a link bettween first and last cluster address
319      * by providing lastCluster address to the first cluster so
320      * for storing last cluster in first cluster in palce of its
321      * previouse cluster address we use it.
322      * @param lastClusterCC ClusterCharacteristics
323      * @throws DException
324      */

325     void setLastCluster(ClusterCharacteristics lastClusterCC) throws DException{
326         byte[] addressBytes = CCzufDpowfsufs.getBytes(lastClusterCC.getStartAddress());
327         updateBytes(versionHandler.PREVIOUSCLUSTERADDRESS,addressBytes);
328     }
329
330     /**
331      * It is used when bytes of a cluster is changed by any operation.
332      * @param flag boolean true for changed and false for unchanged.
333      */

334     public final void setDirty(boolean flag){
335         isDirty = flag;
336     }
337
338
339     /**
340      * used at commit or for addeing into freelist etc.
341      * checking that cluster have any uncommitted bytes or not
342      * @return boolean true if it have some uncommittted bytes or
343      * false if its bytes doesn't changed.
344      */

345
346     public boolean isDirty(){
347         return isDirty;
348     }
349
350     /**
351      * add an element in column positions array
352      * used at time while any record is inserted into cluster
353      */

354
355     void addNewEntry() throws DException {
356         int length = columnPositions.length;
357         short[] temp = new short[length+1];
358         System.arraycopy(columnPositions,0,temp,0,length);
359         columnPositions = temp;
360     }
361
362     /**
363      * Update the appropriate element of Column Positions array.
364      *
365      * @param index : if index is -1 update the last elemment of array.
366      * @param value : to be stored at last or specified index value is IAdd of record.
367      */

368
369     protected void updateColumnPositions(int index,short value) {
370         columnPositions[index == -1 ? (columnPositions.length - 1) : index] = value;
371     }
372
373     /**
374      *
375      * @return short[] all records start addesses.
376      */

377     protected short[] getColumnPositions() {
378         return columnPositions;
379     }
380     /**
381      *
382      * @return DatabaseProperties
383      */

384     public DatabaseProperties getDatabaseProperties() {
385         return databaseProperties;
386     }
387
388     /**
389      *
390      * @return VersionHandler
391      */

392     public VersionHandler getVersionHandler() {
393       return versionHandler;
394     }
395 }
396
Popular Tags