KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > controller > cache > CacheStatistics


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): ______________________________________.
23  */

24
25 package org.objectweb.cjdbc.controller.cache;
26
27 import org.objectweb.cjdbc.common.util.Stats;
28
29 /**
30  * This class handles the statistics for request caches.
31  *
32  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
33  * @version 1.0
34  */

35 public class CacheStatistics
36 {
37   // Cache statistics
38
private Stats select;
39   private Stats hits;
40   private Stats insert;
41   private Stats update;
42   private Stats uncacheable;
43   private Stats delete;
44   private Stats unknown;
45   private Stats remove;
46   private Stats create;
47   private Stats drop;
48
49   /**
50    * Creates a new CacheStatistics object.
51    */

52   public CacheStatistics()
53   {
54     select = new Stats("select");
55     hits = new Stats("hits");
56     insert = new Stats("insert");
57     update = new Stats("update");
58     uncacheable = new Stats("uncacheable");
59     delete = new Stats("delete");
60     unknown = new Stats("unknown");
61     remove = new Stats("remove");
62     create = new Stats("create");
63     drop = new Stats("drop");
64   }
65
66   /**
67    * Resets all stats to zero.
68    */

69   public void reset()
70   {
71     select.reset();
72     hits.reset();
73     insert.reset();
74     update.reset();
75     uncacheable.reset();
76     delete.reset();
77     unknown.reset();
78     remove.reset();
79     create.reset();
80     drop.reset();
81   }
82
83   /**
84    * Returns the create.
85    *
86    * @return an <code>int</code> value
87    */

88   public int getCreate()
89   {
90     return create.getCount();
91   }
92
93   /**
94    * Returns the delete.
95    *
96    * @return an <code>int</code> value
97    */

98   public int getDelete()
99   {
100     return delete.getCount();
101   }
102
103   /**
104    * Returns the drop.
105    *
106    * @return an <code>int</code> value
107    */

108   public int getDrop()
109   {
110     return drop.getCount();
111   }
112
113   /**
114    * Returns the hits.
115    *
116    * @return an <code>int</code> value
117    */

118   public int getHits()
119   {
120     return hits.getCount();
121   }
122
123   /**
124    * Returns the insert.
125    *
126    * @return an <code>int</code> value
127    */

128   public int getInsert()
129   {
130     return insert.getCount();
131   }
132
133   /**
134    * Returns the remove.
135    *
136    * @return an <code>int</code> value
137    */

138   public int getRemove()
139   {
140     return remove.getCount();
141   }
142
143   /**
144    * Returns the select.
145    *
146    * @return an <code>int</code> value
147    */

148   public int getSelect()
149   {
150     return select.getCount();
151   }
152
153   /**
154    * Returns the unknown.
155    *
156    * @return an <code>int</code> value
157    */

158   public int getUnknown()
159   {
160     return unknown.getCount();
161   }
162
163   /**
164    * Returns the update.
165    *
166    * @return an <code>int</code> value
167    */

168   public int getUpdate()
169   {
170     return update.getCount();
171   }
172
173   /**
174    * Returns the uncacheable.
175    *
176    * @return an <code>int</code> value
177    */

178   public int getUncacheable()
179   {
180     return uncacheable.getCount();
181   }
182
183   /**
184    * Increments the create count.
185    */

186   public void addCreate()
187   {
188     create.incrementCount();
189   }
190
191   /**
192    * Increments the delete count.
193    */

194   public void addDelete()
195   {
196     delete.incrementCount();
197   }
198
199   /**
200    * Increments the drop count.
201    */

202   public void addDrop()
203   {
204     drop.incrementCount();
205   }
206
207   /**
208    * Increments the hits count.
209    */

210   public void addHits()
211   {
212     hits.incrementCount();
213   }
214
215   /**
216    * Increments the insert count.
217    */

218   public void addInsert()
219   {
220     insert.incrementCount();
221   }
222
223   /**
224    * Increments the remove count.
225    */

226   public void addRemove()
227   {
228     remove.incrementCount();
229   }
230
231   /**
232    * Increments the select count.
233    */

234   public void addSelect()
235   {
236     select.incrementCount();
237   }
238
239   /**
240    * Increments the unkwnown count.
241    */

242   public void addUnknown()
243   {
244     unknown.incrementCount();
245   }
246
247   /**
248    * Increments the update count.
249    */

250   public void addUpdate()
251   {
252     update.incrementCount();
253   }
254
255   /**
256    * Increments the uncacheable count.
257    */

258   public void addUncacheable()
259   {
260     uncacheable.incrementCount();
261   }
262
263   /**
264    * Retrieve cache statistics as a table
265    *
266    * @return an array of String containing the different cache values, like
267    * number of select, number of hits ...
268    */

269   public String JavaDoc[] getCacheStatsData()
270   {
271     String JavaDoc[] stats = new String JavaDoc[11];
272     stats[0] = "" + getSelect();
273     stats[1] = "" + getHits();
274     stats[2] = "" + getInsert();
275     stats[3] = "" + getUpdate();
276     stats[4] = "" + getUncacheable();
277     stats[5] = "" + getDelete();
278     stats[6] = "" + getUnknown();
279     stats[7] = "" + getRemove();
280     stats[8] = "" + getCreate();
281     stats[9] = "" + getDrop();
282     stats[10] = "" + getCacheHitRatio();
283     return stats;
284   }
285
286   /**
287    * Get percentage of hits
288    *
289    * @return hits / select
290    */

291   public long getCacheHitRatio()
292   {
293     if (select.getCount() == 0)
294       return 0;
295     else
296       return (long) ((float) hits.getCount() / (float) select.getCount() * 100.0);
297   }
298 }
299
Popular Tags