001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2002-2004 French National Institute For Research In Computer
004: * Science And Control (INRIA).
005: * Contact: sequoia@continuent.org
006: *
007: * Licensed under the Apache License, Version 2.0 (the "License");
008: * you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: *
019: * Initial developer(s): Emmanuel Cecchet.
020: * Contributor(s): ______________________________________.
021: */package org.continuent.sequoia.controller.cache;
022:
023: import org.continuent.sequoia.common.util.Stats;
024:
025: /**
026: * This class handles the statistics for request caches.
027: *
028: * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
029: * @version 1.0
030: */
031: public class CacheStatistics {
032: // Cache statistics
033: private Stats select;
034: private Stats hits;
035: private Stats insert;
036: private Stats update;
037: private Stats uncacheable;
038: private Stats delete;
039: private Stats unknown;
040: private Stats remove;
041: private Stats create;
042: private Stats drop;
043:
044: /**
045: * Creates a new CacheStatistics object.
046: */
047: public CacheStatistics() {
048: select = new Stats("select");
049: hits = new Stats("hits");
050: insert = new Stats("insert");
051: update = new Stats("update");
052: uncacheable = new Stats("uncacheable");
053: delete = new Stats("delete");
054: unknown = new Stats("unknown");
055: remove = new Stats("remove");
056: create = new Stats("create");
057: drop = new Stats("drop");
058: }
059:
060: /**
061: * Resets all stats to zero.
062: */
063: public void reset() {
064: select.reset();
065: hits.reset();
066: insert.reset();
067: update.reset();
068: uncacheable.reset();
069: delete.reset();
070: unknown.reset();
071: remove.reset();
072: create.reset();
073: drop.reset();
074: }
075:
076: /**
077: * Returns the create.
078: *
079: * @return an <code>int</code> value
080: */
081: public int getCreate() {
082: return create.getCount();
083: }
084:
085: /**
086: * Returns the delete.
087: *
088: * @return an <code>int</code> value
089: */
090: public int getDelete() {
091: return delete.getCount();
092: }
093:
094: /**
095: * Returns the drop.
096: *
097: * @return an <code>int</code> value
098: */
099: public int getDrop() {
100: return drop.getCount();
101: }
102:
103: /**
104: * Returns the hits.
105: *
106: * @return an <code>int</code> value
107: */
108: public int getHits() {
109: return hits.getCount();
110: }
111:
112: /**
113: * Returns the insert.
114: *
115: * @return an <code>int</code> value
116: */
117: public int getInsert() {
118: return insert.getCount();
119: }
120:
121: /**
122: * Returns the remove.
123: *
124: * @return an <code>int</code> value
125: */
126: public int getRemove() {
127: return remove.getCount();
128: }
129:
130: /**
131: * Returns the select.
132: *
133: * @return an <code>int</code> value
134: */
135: public int getSelect() {
136: return select.getCount();
137: }
138:
139: /**
140: * Returns the unknown.
141: *
142: * @return an <code>int</code> value
143: */
144: public int getUnknown() {
145: return unknown.getCount();
146: }
147:
148: /**
149: * Returns the update.
150: *
151: * @return an <code>int</code> value
152: */
153: public int getUpdate() {
154: return update.getCount();
155: }
156:
157: /**
158: * Returns the uncacheable.
159: *
160: * @return an <code>int</code> value
161: */
162: public int getUncacheable() {
163: return uncacheable.getCount();
164: }
165:
166: /**
167: * Increments the create count.
168: */
169: public void addCreate() {
170: create.incrementCount();
171: }
172:
173: /**
174: * Increments the delete count.
175: */
176: public void addDelete() {
177: delete.incrementCount();
178: }
179:
180: /**
181: * Increments the drop count.
182: */
183: public void addDrop() {
184: drop.incrementCount();
185: }
186:
187: /**
188: * Increments the hits count.
189: */
190: public void addHits() {
191: hits.incrementCount();
192: }
193:
194: /**
195: * Increments the insert count.
196: */
197: public void addInsert() {
198: insert.incrementCount();
199: }
200:
201: /**
202: * Increments the remove count.
203: */
204: public void addRemove() {
205: remove.incrementCount();
206: }
207:
208: /**
209: * Increments the select count.
210: */
211: public void addSelect() {
212: select.incrementCount();
213: }
214:
215: /**
216: * Increments the unkwnown count.
217: */
218: public void addUnknown() {
219: unknown.incrementCount();
220: }
221:
222: /**
223: * Increments the update count.
224: */
225: public void addUpdate() {
226: update.incrementCount();
227: }
228:
229: /**
230: * Increments the uncacheable count.
231: */
232: public void addUncacheable() {
233: uncacheable.incrementCount();
234: }
235:
236: /**
237: * Retrieve cache statistics as a table
238: *
239: * @return an array of String containing the different cache values, like
240: * number of select, number of hits ...
241: */
242: public String[] getCacheStatsData() {
243: String[] stats = new String[11];
244: stats[0] = "" + getSelect();
245: stats[1] = "" + getHits();
246: stats[2] = "" + getInsert();
247: stats[3] = "" + getUpdate();
248: stats[4] = "" + getUncacheable();
249: stats[5] = "" + getDelete();
250: stats[6] = "" + getUnknown();
251: stats[7] = "" + getRemove();
252: stats[8] = "" + getCreate();
253: stats[9] = "" + getDrop();
254: stats[10] = "" + getCacheHitRatio();
255: return stats;
256: }
257:
258: /**
259: * Get percentage of hits
260: *
261: * @return hits / select
262: */
263: public long getCacheHitRatio() {
264: if (select.getCount() == 0)
265: return 0;
266: else
267: return (long) ((float) hits.getCount()
268: / (float) select.getCount() * 100.0);
269: }
270: }
|