KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.daffodilwoods.daffodildb.server.datasystem.persistentsystem;
2
3 import java.util.*;
4 import com.daffodilwoods.database.general.*;
5 import com.daffodilwoods.database.resource.DException;
6 import java.io.*;
7 import com.daffodilwoods.daffodildb.server.sql99.utils._Reference;
8 import com.daffodilwoods.daffodildb.server.serversystem.ServerSystem;
9
10 /**
11  * it is used to maintain the cluster bytes in memory.This map has so much intelligence that it can work
12  * within it?s limit, means it assures the server that it is not consuming a lot of memory if number of Clusters
13  * in memory are more then specified limit then it removes the bytes of cluster from map which were taken
14  * for read
15  */

16 public class TableHashMap extends HashMap {
17
18    private transient double totalSize;
19
20    /**
21     * To shift clusters in weakhashMap
22     */

23    private transient TempFileHandler tempFileHandler;
24
25    transient DatabaseProperties databaseProperties;
26
27    public TableHashMap(TempFileHandler tempFileHandler0, String JavaDoc databaseName, DatabaseProperties databaseProperties0) {
28       super(databaseProperties0.getIntialSizeOfMap(), 0.95f);
29       tempFileHandler = tempFileHandler0;
30       databaseProperties = databaseProperties0;
31    }
32
33    /**
34     *checks whether clusters in memory are more than specified limit, if limit exceeds then removes
35     * cluster from map and puts these clusters in weakhashmap which were taken for read
36     *
37     * @param clusterCharacteristics whose corresponding KeyValue has to put in map
38     * @param recordCreator KeyValue which has to put in map
39     */

40
41    final void updateClusterMappingForRead(ClusterCharacteristics clusterCharacteristics,
42                                           KeyValue recordCreator) throws DException {
43       updateClusterMappingForRead();
44       synchronized (this) {
45          put(clusterCharacteristics, recordCreator);
46       }
47    }
48    /**
49     *checks whether clusters in memory are more than specified limit, if limit exceeds then removes
50     * cluster from map and puts these clusters in weakhashmap which is handled by tempFileHandler
51     * which were taken for read
52     * @throws DException
53     */

54    public void updateClusterMappingForRead() throws DException {
55       if (size() > databaseProperties.CLUSTERS_IN_MEMORY_FOR_READ) {
56          synchronized (this) {
57             if (size() < databaseProperties.CLUSTERS_IN_MEMORY_FOR_READ)
58                return;
59             ArrayList arr = new ArrayList(5);
60             Iterator iter = values().iterator();
61             KeyValue kv;
62             while (iter.hasNext()) {
63                kv = (KeyValue) iter.next();
64                if (kv.check())
65                   arr.add(kv);
66             }
67             int size = arr.size();
68
69             for (int i = 0; i < size; i++) {
70                kv = (KeyValue) arr.get(i);
71                remove(kv.cluster.getClusterCharacteristics());
72                tempFileHandler.addCluster(kv.cluster);
73             }
74          }
75       }
76
77    }
78    /**
79      *checks whether clusters in memory are more than specified limit, if limit exceeds then removes
80      * cluster from map and puts these clusters in weakhashmap which is handled by tempFileHandler
81      * which were taken for read
82      * @throws DException
83      */

84
85     public void updateClusterMappingForReadWrite() throws DException {
86       synchronized (this) {
87          ArrayList arr = new ArrayList(5);
88          Iterator iter = values().iterator();
89          KeyValue kv;
90          while (iter.hasNext()) {
91             kv = (KeyValue) iter.next();
92             if (kv.check())
93                arr.add(kv);
94          }
95          int size = arr.size();
96          for (int i = 0; i < size; i++) {
97             kv = (KeyValue) arr.get(i);
98             remove(kv.cluster.getClusterCharacteristics());
99             tempFileHandler.addCluster(kv.cluster);
100          }
101       }
102
103    }
104    public void updateClusterMappingForWrite() throws DException {
105       if (size() > databaseProperties.CLUSTERS_IN_MEMORY_FOR_WRITE) {
106          int Cluster_Limit = (databaseProperties.CLUSTERS_IN_MEMORY_FOR_WRITE * 10) / 100;
107          synchronized (this) {
108             if (size() < databaseProperties.CLUSTERS_IN_MEMORY_FOR_WRITE)
109                return;
110             ArrayList arr = new ArrayList(5);
111             Iterator iter = values().iterator();
112             int cnt = 0;
113             KeyValue kv;
114             while (iter.hasNext() && ++cnt < Cluster_Limit) {
115                kv = (KeyValue) iter.next();
116                if (!kv.check())
117                   arr.add(kv);
118             }
119             cnt = arr.size();
120             Cluster cluster;
121             for (int i = 0; i < cnt; i++) {
122                kv = (KeyValue) arr.get(i);
123                cluster = kv.cluster;
124                synchronized (String.valueOf( -cluster.getClusterCharacteristics().getStartAddress()).intern()) {
125                   remove(cluster.getClusterCharacteristics());
126                   tempFileHandler.writeInTempFile(cluster, cluster.getBytes());
127                   tempFileHandler.addCluster(cluster);
128                }
129             }
130          }
131       }
132
133    }
134
135    public double get_size() throws DException {
136       if (totalSize < 0) {
137          totalSize = 0;
138          return 0;
139       }
140       return totalSize;
141    }
142
143
144    /**
145     * changes status of cluster as read when bytes of cluster are written in databaseFile
146     * @param cc clusterCharacteristics whose status has to set read
147     */

148
149    final void setStatusToRead(ClusterCharacteristics cc) throws DException {
150       KeyValue kv = (KeyValue) get(cc);
151       if (kv != null)
152          kv.setKey(null);
153    }
154 }
155
Popular Tags