KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
14  * It has the main database randomAccess File and other files if database has muliFileSupport. it sets the
15  * random access filePointer on desired current loctaion , read and write bytes from the file.
16
17  */

18 public class DRandomAccessFile extends DRandomAccessFileUpto3_2{
19
20
21
22     public DRandomAccessFile(RandomAccessFile raf0,long fileSize0,String JavaDoc databaseURL0,int incrementFactor0,boolean multiFileSupport1,FileGenerator fileGenerator0,int clusterSize0 ) throws DException{
23       super(raf0,fileSize0, databaseURL0,incrementFactor0, multiFileSupport1, fileGenerator0 ,clusterSize0);
24     }
25
26     /**
27       * If multiFileSupport is required then gets total number of files from filegenerator for this database.
28       * If it gets -1 then puts database Entry in Filegenerator otheriwse initializes all random access files
29       * by null in Map.
30       */

31
32      protected void init() throws DException {
33        int no_OfFiles = fileGenerator.getIndex(databaseName);
34        if(no_OfFiles == -1)
35          fileGenerator.insert(databaseName);
36        for( int i = 1 ; i < no_OfFiles; i++ ){
37          filesMap.put("_"+i,null);
38        }
39      }
40      /**
41       * It is called when a position is to be seeked which is large than length
42       * of file and index caluculated for dat file is > fileMap size
43       * than we make a new file with passed index.
44       *
45       * @param x int - index with which new .dat file is to be created.
46       * @throws DException
47       * @throws IOException
48       */

49      protected void makeNewFile(int x) throws DException,IOException{
50       try{
51         fileGenerator.addNewFile(x,databaseName);
52         String JavaDoc newFileName = "_"+x;
53         raf = new RandomAccessFile(path+File.separator+newFileName+".dat","rw");
54         raf.setLength(newFileSize);
55         filesMap.put(newFileName, raf);
56       }
57       catch(NullPointerException JavaDoc de) {
58         throw new DException("DSE0",new Object JavaDoc[] {de.getMessage()});
59       }
60     }
61
62     /**
63      * It is used when a position to be seeked is lagre than file length and
64      * index caluculated for dat file is less than or equal to fileMap size
65      * than we assume that file is already created and we need to load it.
66      *
67      * @param x int - index of dat file to be loaded.
68      * @throws DException
69      * @throws IOException
70      */

71
72      protected void loadNewFile(int x)throws DException,IOException{
73        String JavaDoc name = x == 0 ? databaseName : "_"+x;
74        raf = (RandomAccessFile)filesMap.get(name);
75        if(raf == null){
76          raf = new RandomAccessFile(path+File.separator+name+".dat","rw");
77          raf.setLength(newFileSize);
78          filesMap.put(name,raf);
79        }
80   }
81 }
82
Popular Tags