KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > cache > CacheStatistics


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: sequoia@continuent.org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * Initial developer(s): Emmanuel Cecchet.
20  * Contributor(s): ______________________________________.
21  */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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