KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.daffodilwoods.daffodildb.server.datasystem.persistentsystem;
2
3 import java.io.*;
4 import java.lang.ref.*;
5 import java.util.*;
6
7 import com.daffodilwoods.database.resource.*;
8
9 /**
10  * it maintains the weakHashMap for Clusters taken for read and mapping of Clusters whose bytes are
11  * written in tempFile
12  */

13 public class TempFileHandler {
14
15    /**
16     * Map to maintain clusters taken for read
17     */

18
19    private Map clusterMap;
20
21    /**
22     * Map to maintain cluster address and corresponding temp file address where bytes of that cluster are
23     * written in temp File
24     */

25    private HashMap addressesMap;
26
27    /**
28     * To reuse FreeSpace of tempFile
29     */

30    private ClusterManager clusterManager;
31
32    public TempFileHandler(ClusterManager clusterManager0) {
33       clusterMap = Collections.synchronizedMap(new HashMap());
34       addressesMap = new HashMap();
35       clusterManager = clusterManager0;
36    }
37
38    /**
39     * Checks whether specified Clustercharateristics bytes are written in temp file or not.
40     * If exists then removes that cluster address from map and adds temp file addres in freeAddressList
41     * of ClusterManager and returns address of temp file where bytes for specified cluster are written
42     * otherwise return -1
43     *
44     * @param cc ClusterCharacteristics whose corresponding temp file address has to get to read bytes of
45     * cluster
46     *
47     * @return returns address of temp file where bytes for specified cluster are written or -1
48     */

49
50    long getAddressOfTempFile(ClusterCharacteristics cc) throws DException {
51       Object JavaDoc obj = addressesMap.get(cc);
52       if (obj != null) {
53          removeEntryFromAddressMap(cc);
54          return ( (Long JavaDoc) obj).longValue();
55       }
56       return -1;
57    }
58
59    /**
60     * puts clusterCharacteristics and corresponding tempFile address in Map
61     * @param cc clusterCharacteristics
62     * @param add address of tempFile corresponding to clusterCharacteristics
63     */

64    void addAddressOfTempFile(ClusterCharacteristics cc, long add) throws DException {
65       addressesMap.put(cc, new Long JavaDoc(add));
66    }
67
68    /**
69     * checks whether cluster corresponding to given Cluster characteristics Exists , If Exists then
70     * returns cluster and removes cluster entry from map otherwise return null
71     * Weak HashMap contains null as key.
72     *
73     * @param cc ClusterCharacteristics whose corresponding cluster is required
74     *
75     * @return Cluster corresponding to ClusterCharacteristics or null
76     */

77
78    /**
79     * Checks for cluster corresponding to passed cc and if get than return it else null
80     */

81    Cluster getClusterFromClustersMap(ClusterCharacteristics cc) {
82       Reference sr = (Reference) clusterMap.remove(cc);
83    return sr==null?null:(Cluster)sr.get();
84     }
85
86    /**
87     * puts cluster in weakHashmap
88     * @param cls Cluster which has to put in weakmap
89     */

90
91    void addCluster(Cluster cls) {
92       clusterMap.put(cls.getClusterCharacteristics(), new SoftReference(cls));
93    }
94    /**
95     * Remove entry of cc from map while it returns address corresponding to these
96     * cc in temp file.
97     * @param cc ClusterCharacteristics
98     * @throws DException
99     */

100    void removeEntryFromAddressMap(ClusterCharacteristics cc) throws DException {
101       addressesMap.remove(cc);
102    }
103
104    /**
105     * write the cluster bytes in temp file at the appropriate location.
106     *
107     */

108
109    void writeInTempFile(Cluster cluster, byte[] bytes) throws DException {
110       synchronized (clusterManager.getTempFile()) {
111          try {
112             long address = clusterManager.getTempFile().length();
113             int size = clusterManager.getFreeAddresses().size();
114             if (size > 0) {
115                address = ( (Long JavaDoc) clusterManager.getFreeAddresses().get(size - 1)).longValue();
116                clusterManager.getTempFile().seek(address);
117                clusterManager.getTempFile().write(bytes);
118                clusterManager.getFreeAddresses().remove(size - 1);
119                size--;
120             } else {
121                clusterManager.getTempFile().seek(address);
122                clusterManager.getTempFile().write(bytes);
123             }
124             addAddressOfTempFile(cluster.clusterCharacteristics, (int) address);
125          } catch (IOException ex) {
126             throw new DException("DSE2025", new Object JavaDoc[] {ex.getMessage()});
127          }
128       }
129    }
130
131    /**
132     * Checks whether specified Clustercharateristics bytes are written in temp file or not.
133     * If exists returns address of temp file where bytes for specified cluster are written
134     * otherwise return -1
135     *
136     * @param cc ClusterCharacteristics whose corresponding temp file address has to get to read bytes of
137     * cluster
138     *
139     * @return returns address of temp file where bytes for specified cluster are written or -1
140     */

141
142    long getAddressOfTempFileForPowerFile(ClusterCharacteristics cc) throws DException {
143       Object JavaDoc obj = addressesMap.get(cc);
144       return obj == null ? -1 : ( (Long JavaDoc) obj).longValue();
145    }
146 }
147
Popular Tags