KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.daffodilwoods.daffodildb.server.datasystem.persistentsystem;
2
3 import com.daffodilwoods.daffodildb.utils.byteconverter.CCzufDpowfsufs;
4 import java.io.*;
5 import java.util.*;
6 import com.daffodilwoods.database.utility.P;
7 import com.daffodilwoods.database.resource.DException;
8 import com.daffodilwoods.database.general.QualifiedIdentifier;
9 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*;
10 import com.daffodilwoods.daffodildb.server.sql99.utils._Reference;
11
12 /**
13  * It has the main database randomAccess File and other files if database has muliFileSupport. it sets the
14  * random access filePointer on desired current loctaion , read and write bytes from the file.
15
16  */

17 public class DRandomAccessFileUpto3_2 {
18
19   /**
20    * Map to maintain all random access Files of database,Initially only main file is loaded and other
21    * files are set as null.
22    *
23    */

24
25   protected HashMap filesMap;
26
27   /**
28    * whether mulitifile support is required or not
29    */

30   private boolean multiFileSupport;
31
32   /**
33    * To Get new File names when multiFileSupport is true
34    */

35   protected FileGenerator fileGenerator;
36
37   /**
38    * size of main database File
39    */

40   protected long fileSize;
41
42   /**
43    * current Random access file on which read or write has to perform
44    */

45   protected RandomAccessFile raf;
46
47   /**
48    * path of database file
49    */

50   protected String JavaDoc path ;
51
52   /**
53    * when first database file is fully used then size is used , it has two cases:
54    * 1. if multiFilesupport is not required ,size by which old file size should be increased
55    * 2. if multiFilesupport is required, then size of new File
56    */

57   protected long newFileSize;
58
59   /**
60    * Name of database to get new file names
61    */

62   protected String JavaDoc databaseName;
63
64   private boolean fileClosed = false;
65
66   /**
67    * current random access file length.
68    */

69   private long length;
70
71   /**
72    * Constructs DRandomAccessFile and initializes its attributes. If multiFileSupport is required, then
73    * initializes all random access files in map with null.
74    */

75
76
77
78   public DRandomAccessFileUpto3_2(RandomAccessFile raf0,long fileSize0,String JavaDoc databaseURL0,int incrementFactor0,boolean multiFileSupport1,FileGenerator fileGenerator0,int clusterSize ) throws DException{
79     path = databaseURL0.substring(0,databaseURL0.lastIndexOf(File.separator));
80     databaseName = databaseURL0.substring(databaseURL0.lastIndexOf(File.separator)+ 1,databaseURL0.lastIndexOf('.'));
81     fileSize = fileSize0;
82
83     newFileSize = fileSize*incrementFactor0/100;
84     long remainder = newFileSize%clusterSize;
85     newFileSize = newFileSize - remainder;
86     multiFileSupport = multiFileSupport1;
87     fileGenerator = fileGenerator0;
88     raf = raf0;
89     try {
90       length = raf.length();
91     }
92     catch (IOException ex) {
93     }
94     if(multiFileSupport){
95       filesMap = new HashMap();
96       init();
97       filesMap.put(databaseName,raf);
98     }
99   }
100
101   /**
102    * If multiFileSupport is required then gets total number of files from filegenerator for this database.
103    * If it gets -1 then puts database Entry in Filegenerator otheriwse initializes all random access files
104    * by null in Map.
105    */

106
107   protected void init() throws DException {
108     int no_OfFiles = fileGenerator.getIndex(databaseName);
109     if(no_OfFiles == -1)
110       fileGenerator.insert(databaseName);
111     for( int i = 1 ; i < no_OfFiles; i++ ){
112       filesMap.put(databaseName+i,null);
113     }
114   }
115
116   /**
117    * Moves pointer on specified position.
118    * If multiFileSupoort is required , then it checks whether current position is more than initial file
119    * length or not ,if required then makes new random access file or if file is previously made then
120    * loads it and moves pointer on desired file.It generates new FileName with the help of fileGenerator
121    * and allocates first cluster of each file to store database specific information
122    *
123    * @param pos position where pointer has to move
124    */

125
126   protected final void seek(long pos) throws DException{
127     try {
128
129       if(multiFileSupport){
130         long diff = pos - fileSize;
131         int x = (int)((diff < 0 ) ? 0 : (diff/newFileSize)+1);
132         if(x >= filesMap.size())
133           makeNewFile(x);
134         else
135           loadNewFile(x);
136         long relativePointer = diff < 0 ? pos : diff % newFileSize;
137         raf.seek(relativePointer);
138       }
139       else{
140         while(pos >= length){
141           length = length + newFileSize;
142           raf.setLength(length);
143         }
144         if(pos!= raf.getFilePointer())
145           raf.seek(pos);
146       }
147     }
148     catch (IOException ex) {
149       if(!fileClosed){
150         Exception JavaDoc xyzabc = new Exception JavaDoc(path+File.separator+databaseName+".ddb");
151         xyzabc.printStackTrace();
152         throw new DException("DSE2025",new Object JavaDoc[] {ex.getMessage()});
153       }
154     }
155   }
156
157   /**
158    * moves pointer of file on specified adddress and writes given bytes on random acccess file
159    *
160    * @param startAddress address of file where has to write
161    * @param bytes bytes which has to write in random acccess file
162    */

163
164  protected synchronized void write(long startAddress,byte [] bytes) throws DException{
165     try {
166       seek(startAddress);
167       raf.write(bytes);
168     }
169     catch (IOException ex) {
170       if(!fileClosed )
171         throw new DException("DSE2025",new Object JavaDoc[] {ex.getMessage()});
172     }
173   }
174   /**
175    * Not Used Never.
176    * @return long
177    */

178   /**
179    * To read long value from specified address.
180    *
181    * @param startAddr long - address of location from where a long value to be readed.
182    * @throws DException
183    * @return long - long value of bytes from provided start addresss.
184    */

185   synchronized final long readLong(long startAddr) throws DException {
186     try {
187       seek(startAddr);
188       long val = raf.readLong();
189       return val;
190     }
191     catch (IOException ex) {
192       if(fileClosed )
193         return 0;
194       throw new DException("DSE2025",new Object JavaDoc[] {ex.getMessage()});
195     }
196   }
197
198   /**
199    * moves pointer of file on specified adddress and reads given bytes from random acccess file
200
201    * @param b bytes in which bytes from Random access file has to read
202    * @param addr address of file from where has to read
203    * @return bytes read from Random access file
204    */

205
206  protected synchronized byte[] read(byte b[],long addr) throws DException {
207     try {
208       seek(addr);
209       raf.read(b);
210       return b;
211     }
212     catch (IOException ex) {
213       if(fileClosed )
214         return b;
215       throw new DException("DSE2025",new Object JavaDoc[] {ex.getMessage()});
216     }
217   }
218   /**
219    * To close Database file it close all multiple files if any exists.
220    * @throws IOException
221    */

222   final void close() throws IOException{
223     if(filesMap != null){
224       String JavaDoc [] str = (String JavaDoc[])filesMap.keySet().toArray(new String JavaDoc[0]);
225       for(int i = 0,size = str.length; i < size ; i++){
226         RandomAccessFile raf1 = (RandomAccessFile) filesMap.get(str[i]);
227         if(raf1 != null)
228           raf1.close();
229       }
230     }
231     raf.close();
232     fileClosed = true;
233   }
234   /**
235      * It is called when a position is to be seeked which is large than length
236      * of file and index caluculated for dat file is > fileMap size
237      * than we make a new file with passed index.
238      *
239      * @param x int - index with which new .dat file is to be created.
240      * @throws DException
241      * @throws IOException
242      */

243
244   protected void makeNewFile(int x) throws DException,IOException{
245         try{
246             String JavaDoc newFileName = fileGenerator.addNewFile(x,databaseName);
247             raf = new RandomAccessFile(path+File.separator+newFileName+".dat","rw");
248             raf.setLength(newFileSize);
249             filesMap.put(newFileName, raf);
250         }
251         catch(NullPointerException JavaDoc de) {
252             throw new DException("DSE0",new Object JavaDoc[] {de.getMessage()});
253         }
254     }
255     /**
256        * It is used when a position to be seeked is lagre than file length and
257        * index caluculated for dat file is less than or equal to fileMap size
258        * than we assume that file is already created and we need to load it.
259        *
260        * @param x int - index of dat file to be loaded.
261        * @throws DException
262        * @throws IOException
263        */

264
265   protected void loadNewFile(int x)throws DException,IOException{
266       String JavaDoc name = x == 0 ? databaseName : databaseName+x;
267       raf = (RandomAccessFile)filesMap.get(name);
268       if(raf == null){
269           raf = new RandomAccessFile(path+File.separator+name+".dat","rw");
270           raf.setLength(newFileSize);
271           filesMap.put(name,raf);
272       }
273   }
274
275   /**
276    * It is used to get any information which we needs during any operation
277    * and we write these bytes without encryption if user use any algo so
278    * we use these method to get those informations by calling readFirst or writeFirst
279    * which are not implemented in EncryptDRandomAccessFile, so to get data without decrypt
280    * than we use this method.
281    *
282    * @param b byte[] - in which we have to read bytes and read as it can store(i.e. upto its length).
283    * @param addr long - address from which we starts to read bytes.
284    * @throws DException
285    * @return byte[] - bytes without decrption readed from specified index.
286    */

287   synchronized byte[] readFirst(byte b[],long addr) throws DException {
288     try {
289       seek(addr);
290       raf.read(b);
291       return b;
292     }
293     catch (IOException ex) {
294       if(fileClosed )
295         return b;
296       throw new DException("DSE2025",new Object JavaDoc[] {ex.getMessage()});
297     }
298   }
299   /**
300    * It is used to write any information which we needs during any operation
301    * and we write these bytes without encryption if user use any algo so
302    * we use these method to get/save those informations by calling readFirst or writeFirst
303    * which are not implemented in EncryptDRandomAccessFile, so to
304    * save without encryption than we use this methods.
305    *
306    * @param b byte[] - bytes to be write with out encryption.
307    * @param addr long - address at which these bytes are to be written.
308    * @throws DException
309    */

310
311   synchronized void writeFirst(long startAddress,byte [] bytes) throws DException{
312     try {
313       seek(startAddress);
314       raf.write(bytes);
315     }
316     catch (IOException ex) {
317       if(!fileClosed )
318         throw new DException("DSE2025",new Object JavaDoc[] {ex.getMessage()});
319     }
320   }
321
322
323
324   /**
325    * It is used in Persistent DataBase constructor to initialize database URL.
326    * @return String - complete path for database file.
327    */

328   String JavaDoc getURL(){
329     return path+File.separator+databaseName+".ddb";
330   }
331   /**
332    * It is used in backup related work to get database file.
333    * @return RandomAccessFile - currently seted dtabase file.
334    */

335   public RandomAccessFile getRandomAccessFile(){
336     return raf;
337   }
338   /**
339    * It is used in BackUp related work to get MultipleFileSupport property of database
340    * @return boolean multipleFileSupport.
341    */

342   public boolean isMultiFileSupport(){
343     return multiFileSupport;
344   }
345   /**
346    * It is used in BackUp realted work to get All files of a database.
347    * @return HashMap - containing all database files.
348    */

349   public HashMap getMultiFileMap(){
350     return filesMap;
351
352   }
353   /**
354    * To get index for multiple files.
355    * @throws DException
356    * @return int - index for multiple files.
357    */

358   public int getIndexMultipleFiles() throws DException{
359     return fileGenerator!= null ? fileGenerator.getIndex(databaseName) : -1;
360   }
361
362
363 }
364
Popular Tags