KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.daffodilwoods.daffodildb.server.datasystem.persistentsystem;
2
3 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.versioninfo.*;
4 import com.daffodilwoods.daffodildb.utils.*;
5 import com.daffodilwoods.daffodildb.utils.byteconverter.*;
6 import com.daffodilwoods.database.resource.*;
7 import com.daffodilwoods.daffodildb.server.datasystem.btree.*;
8
9 /**
10  *
11  * <p>Title: Fixed Record Cluster</p>
12  * <p>Description: Maintains insert, delete, update and retrieve operations on table having all Columns of
13  * fixed type
14  * </p>
15  */

16
17 public class FixedRecordCluster implements _RecordCluster{
18     static final int NONE = -1;
19     static final int FULLY = 0;
20     static final int PARTIALY = 1;
21
22     /**
23      * TableProperties to get ColumnCount in Table and RecordSize
24      */

25     protected TableProperties tableProperties;
26
27     /**
28      * Cluster to read and write bytes
29      */

30
31     protected Cluster cluster;
32
33
34     /**
35      * Used for optimization
36      */

37
38     private int moveLength;
39
40     /**
41      * Contains Database Properties for the database.
42      */

43
44     protected DatabaseProperties databaseProperties;
45
46     /**
47      * Used to calculate newAddressLength,Delete,Patial etc. information according to database version.
48      */

49     protected VersionHandler versionHandler;
50
51     public FixedRecordCluster(TableProperties tp,DatabaseProperties databaseProperties0,VersionHandler versionHandler0) throws DException {
52         tableProperties = tp;
53         databaseProperties = databaseProperties0;
54         versionHandler = versionHandler0;
55         moveLength = tableProperties.recordSize + tableProperties.columnCount + versionHandler.ACTIVE_DELETE + versionHandler.FULL_PARTIAL;
56
57     }
58
59     /**
60      * checks InsertType of Record whether Record can be fully Inserted, partially Inserted or can not be
61      * inserted in current cluster, write bytes and return insertType and written number of bytes if record
62      * is partially inserted.
63      *
64      * if isUpdate is true then we mark the record in the current cluster as UPDATE and insert it in some other cluster and keep its TableKey
65      * at the current position
66      *
67      * @param bytes bytes of record which are to be written
68      * @param startColumnIndex Position from where bytes are to be written
69      * @param isUpdate true if Updated Record is to be written else false
70      *
71      * @return InsertType and written Number of Bytes
72      */

73
74     public int[] insert(byte[] bytes,int startColumnIndex, boolean isUpdate) throws DException {
75         int insertType = insertType(bytes, startColumnIndex);
76         return insertType == NONE ? new int[]{FAILED,0} : insertType == FULLY ? insertRecord(bytes,getNextInsertableAddress(),isUpdate)
77                            : partialInsertRecord(bytes,getNextInsertableAddress(),startColumnIndex,isUpdate);
78     }
79
80     /**
81      * Checks whether record can be Fully or Partailly inserted or can not be inserted in current cluster
82      *
83      * @param bytes bytes of record which has to insert
84      *
85      * @param startPosition position from where have to write
86      *
87      * @return type of insertion (Fully, Partially or None)
88      */

89
90     private int insertType(byte[] bytes,int startPosition) throws DException {
91         int recordSize = bytes.length + 2; // 2 for ACTIVE/DELETED and FULL/PARTIAL bytes
92
return recordSize <= freeSpace() ? FULLY : NONE;
93     }
94
95
96    /**
97     * writes record bytes in Cluster and increments actual record count, nextInsertable Address in Cluster
98     *
99     * if isUpdate is true then we mark the record in the current cluster as UPDATE and insert it in some other cluster and keep its TableKey
100     * at the current position if IsUpdate is false only then increments active record count
101     *
102     * @param bytes bytes which are to be inserted
103     * @param insertAdd insertable address in cluster *
104     * @param isUpdate true if Updated Record is to be written else false
105
106     * @return insertType(SuccessFull) and 0
107     */

108
109     private int[] insertRecord(byte[] bytes,short insertAdd, boolean isUpdate) throws DException {
110       short currentPointer = insertAdd;
111         cluster.updateByte(currentPointer++,versionHandler.ACTIVE);
112         cluster.updateByte(currentPointer++,versionHandler.COMPLETE);
113         cluster.updateBytes(currentPointer,bytes);
114         cluster.activeRecordCount++;
115         cluster.actualRecordCount++;
116         currentPointer += bytes.length;
117         cluster.updateClusterInformation(currentPointer);
118         return new int[]{SUCCESSFUL,0};
119     }
120
121     /**
122      * Marks Record DELETED
123      *
124      * @param recordNumber recordNumber which is to be deleted
125      * @return ClusterStatus current cluster status
126      *
127      * @throws DException if RecordNumber to be deleted exceeds actualRecordCount throws DException(DSE2007)
128      */

129
130
131     public ClusterStatus delete(short recordNumber, boolean checkKeyValidity) throws DException{
132         if(recordNumber > cluster.actualRecordCount )
133             throw new DException("DSE2007", new Object JavaDoc[]{cluster.toString() });
134           byte [] clusterBytes = cluster.getBytes();
135         short skip = moveToRecordNumber(clusterBytes,recordNumber);
136         boolean curre = clusterBytes[skip+1] != versionHandler.FULL;
137         cluster.updateByte(skip,versionHandler.DELETE);
138         cluster.activeRecordCount--;
139         cluster.updateBytes(2*versionHandler.LENGTH,CCzufDpowfsufs.getBytes(cluster.activeRecordCount));
140         return new ClusterStatus(cluster,cluster.activeRecordCount,cluster.actualRecordCount,false,false,curre);
141     }
142
143     /**
144      * Updates specifed RecordNumber in current Cluster by new bytes.
145      *
146      * Length of old and new must be same.
147      *
148      * @param recordNumber recordnumber which has to update
149      * @param newBytes new bytes of Record
150      */

151
152
153     public void update(short recordNumber,byte[] newBytes) throws DException {
154         short currentPointer = (short)(moveToRecordNumber(cluster.getBytes(),recordNumber)+2);
155         cluster.updateBytes(currentPointer,newBytes);
156     }
157
158     /**
159      * Not used now.Can be removed.
160      */

161
162
163     /**
164      * Not used now.Can be removed.
165      */

166
167
168     /**
169      * Returns actual records in current cluster (Active + Delete both).
170      * @throws DException
171      * @return short
172      */

173     public short getRecordCount() throws DException {
174         return cluster.actualRecordCount;
175     }
176
177
178     /**
179      * To calculate free space available in current cluster.
180      * @throws DException
181      * @return int - free space
182      */

183     public int freeSpace() throws DException {
184         short spaceUsed = CCzufDpowfsufs.getShortValue(cluster.getBytes(),0);
185         return databaseProperties.CLUSTERSIZE - spaceUsed - versionHandler.NEWADDRESSLENGTH;
186     }
187     /**
188      * To return currentCluster.
189      * @return Cluster
190      */

191     public Cluster getCluster(){
192         return cluster;
193     }
194     /**
195      * To set current cluster
196      * @param cluster0 Cluster - cluster to be seted as current.
197      * @throws DException
198      */

199     public void setCluster(Cluster cluster0) throws DException{
200         cluster = cluster0;
201     }
202     /**
203      * To return only active records of current cluster.
204      * @return short
205      */

206     public short getActiveRecordCount(){
207         return cluster.activeRecordCount;
208     }
209     /**
210      * To get next free address in cluster to save a record.
211      * @throws DException
212      * @return short
213      */

214     protected short getNextInsertableAddress() throws DException {
215         return CCzufDpowfsufs.getShortValue(cluster.getBytes(),0);
216     }
217
218     /**
219      * returns the start pointer of record number specified.
220      *
221      * @param clusterBytes Bytes of current cluster
222      * @param recordNumber Whose startpointer is required
223      */

224
225     protected short moveToRecordNumber(byte[] clusterBytes,short recordNumber) throws DException {
226         short skip = versionHandler.CLUSTER_STARTPOINTER;//+ ADDRESSLENGTH + ADDRESSLENGTH + ADDRESSLENGTH + ADDRESSLENGTH;
227
skip += moveLength * (recordNumber -1);
228         return skip ;
229     }
230
231     /**
232      * Checks whether given recordNumber is deleted or active
233      *
234      * @param recordNumber recordNumber whose validity has to check
235      * @throws DException if RecordNumber is deleted
236      */

237
238     public void checkValidity(short recordNumber)throws DException{
239         byte[] clusterBytes = cluster.getBytes();
240         short pointer = moveToRecordNumber(clusterBytes,recordNumber);
241         if(clusterBytes[pointer++] == versionHandler.DELETE)
242           throw StaticExceptions.RECORD_DELETED_EXCEPTION ;
243     }
244
245     /**
246      * returns whether that record is completely Written or not
247      * @param recordNumber RecordNumber
248      * @return whether that record is completely Written or not
249      */

250
251     public boolean isComplete(short recordNumber)throws DException {
252       /**@todo: Implement this com.daffodilwoods.daffodildb.server.datasystem.persistentsystem._RecordCluster method*/
253         throw new java.lang.UnsupportedOperationException JavaDoc("Method isComplete() not yet implemented.");
254     }
255
256
257     public String JavaDoc toString() {
258         return "Fixed RecordCluster " ;//+ cluster.clusterCharacteristics+" --- "+hashCode();
259
}
260     /**
261      * It is used to get free space in a cluster .
262      * @throws DException
263      * @return int - free space of cluster.
264      */

265     public int getRange() throws DException {
266         return databaseProperties.CLUSTERSIZE - CCzufDpowfsufs.getShortValue(cluster.getBytes(),0) - versionHandler.NEWADDRESSLENGTH;
267     }
268
269     /**
270     * Returns a BufferRange of record which conatins the NULL/NOT NULL bytes and the record's actual bytes
271     * @param recordNumber record number for which bufferRange is to be retrieved
272     * @return bufferRange
273     *
274     * @throws DException if current Record is marked as DELETE then throws RECORD_DELETED_EXCEPTION
275     */

276
277     public BufferRange retrieveBufferRange(short recordNumber) throws DException{
278       byte[] clusterBytes = cluster.getBytes();
279         short pointer = moveToRecordNumber(clusterBytes,recordNumber);
280         if(clusterBytes[pointer++] == versionHandler.DELETE ){
281           throw StaticExceptions.RECORD_DELETED_EXCEPTION ;
282         }
283         pointer+=versionHandler.FULL_PARTIAL ;
284         int sad = tableProperties.recordSize + tableProperties.columnCount;
285         byte[] recordBytes = new byte[sad];
286         System.arraycopy(clusterBytes,pointer,recordBytes,0,sad);
287         BufferRange range = new BufferRange(recordBytes,0,sad);
288         return range;
289
290     }
291
292     /**
293      * Methods writtent below are required by VariableRecord Cluster and PartialFixed Record Cluster.
294      */

295
296     public int partialUpdate(short recordNumber, int startPosition, byte[] newBytes) throws DException {
297         /**@todo: Implement this com.daffodilwoods.daffodildb.server.datasystem.persistentsystem._RecordCluster method*/
298         throw new java.lang.UnsupportedOperationException JavaDoc("Method partialUpdate() not yet implemented.");
299     }
300
301     public ClusterStatus partialDelete(short recordNumber) throws DException{
302         /**@todo: Implement this com.daffodilwoods.daffodildb.server.datasystem.persistentsystem._RecordCluster method*/
303         throw new java.lang.UnsupportedOperationException JavaDoc("Method partialDelete() not yet implemented.");
304     }
305
306     private int[] partialInsertRecord(byte[] bytes,short insertAdd,int startPosition, boolean isUpdate) throws DException {
307         /**@todo: Implement this com.daffodilwoods.daffodildb.server.datasystem.persistentsystem._RecordCluster method*/
308         throw new java.lang.UnsupportedOperationException JavaDoc("Method partialInsertRecord() not yet implemented.");
309     }
310
311
312     public Object JavaDoc [] partialRetrieve(short recordNumber ) throws DException {
313         /**@todo: Implement this com.daffodilwoods.daffodildb.server.datasystem.persistentsystem._RecordCluster method*/
314         throw new java.lang.UnsupportedOperationException JavaDoc("Method partialRetrieve() not yet implemented.");
315     }
316
317     public int getLength0fPartialRecord(short recordNumber) throws DException{
318         /**@todo: Implement this com.daffodilwoods.daffodildb.server.datasystem.persistentsystem._RecordCluster method*/
319         throw new java.lang.UnsupportedOperationException JavaDoc("Method getLength0fPartialRecord() not yet implemented.");
320     }
321 }
322
Popular Tags