View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package net.sf.ehcache.hibernate.management.impl;
18  
19  import java.io.Serializable;
20  import java.util.ArrayList;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import javax.management.openmbean.CompositeData;
25  import javax.management.openmbean.CompositeDataSupport;
26  import javax.management.openmbean.CompositeType;
27  import javax.management.openmbean.OpenDataException;
28  import javax.management.openmbean.OpenType;
29  import javax.management.openmbean.SimpleType;
30  import javax.management.openmbean.TabularData;
31  import javax.management.openmbean.TabularDataSupport;
32  import javax.management.openmbean.TabularType;
33  
34  import org.hibernate.stat.QueryStatistics;
35  
36  /***
37   * @author gkeim
38   *
39   */
40  public class QueryStats implements Serializable {
41    private static final String        COMPOSITE_TYPE_NAME        = "QueryStats";
42    private static final String        COMPOSITE_TYPE_DESCRIPTION = "Statistics per Query";
43    private static final String[]      ITEM_NAMES                 = new String[] {"query", "cacheHitCount",
44        "cacheMissCount", "cachePutCount", "executionCount", "executionRowCount", "executionAvgTime", "executionMaxTime",
45        "executionMinTime", };
46    private static final String[]      ITEM_DESCRIPTIONS          = new String[] {"query", "cacheHitCount",
47        "cacheMissCount", "cachePutCount", "executionCount", "executionRowCount", "executionAvgTime", "executionMaxTime",
48        "executionMinTime", };
49    private static final OpenType[]    ITEM_TYPES                 = new OpenType[] {SimpleType.STRING, SimpleType.LONG,
50        SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG,
51        SimpleType.LONG, };
52    private static final CompositeType COMPOSITE_TYPE;
53    private static final String        TABULAR_TYPE_NAME          = "Statistics by Query";
54    private static final String        TABULAR_TYPE_DESCRIPTION   = "All Query Statistics";
55    private static final String[]      INDEX_NAMES                = new String[] {"query", };
56    private static final TabularType   TABULAR_TYPE;
57  
58    static {
59      try {
60        COMPOSITE_TYPE = new CompositeType(COMPOSITE_TYPE_NAME, COMPOSITE_TYPE_DESCRIPTION, ITEM_NAMES,
61                                           ITEM_DESCRIPTIONS, ITEM_TYPES);
62        TABULAR_TYPE = new TabularType(TABULAR_TYPE_NAME, TABULAR_TYPE_DESCRIPTION, COMPOSITE_TYPE, INDEX_NAMES);
63      } catch (OpenDataException e) {
64        throw new RuntimeException(e);
65      }
66    }
67  
68    /***
69     * query
70     */
71    protected final String query;
72    
73    /***
74     * cacheHitCount
75     */
76    protected long cacheHitCount;
77    
78    /***
79     * cacheMissCount
80     */
81    protected long cacheMissCount;
82    
83    /***
84     * cachePutCount
85     */
86    protected long cachePutCount;
87    
88    /***
89     * executionCount
90     */
91    protected long executionCount;
92    
93    /***
94     * executionRowCount
95     */
96    protected long executionRowCount;
97    
98    /***
99     * executionAvgTime
100    */
101   protected long executionAvgTime;
102   
103   /***
104    * executionMaxTime
105    */
106   protected long executionMaxTime;
107   
108   /***
109    * executionMinTime
110    */
111   protected long executionMinTime;
112 
113   /***
114    * @param name
115    */
116   public QueryStats(String name) {
117     this.query = name;
118   }
119 
120   /***
121    * @param name
122    * @param src
123    */
124   public QueryStats(String name, QueryStatistics src) {
125     this(name);
126     
127     try {
128       this.cacheHitCount = BeanUtils.getLongBeanProperty(src, "cacheHitCount");
129       this.cacheMissCount = BeanUtils.getLongBeanProperty(src, "cacheMissCount");
130       this.cachePutCount = BeanUtils.getLongBeanProperty(src, "cachePutCount");
131       this.executionCount = BeanUtils.getLongBeanProperty(src, "executionCount");
132       this.executionRowCount = BeanUtils.getLongBeanProperty(src, "executionRowCount");
133       this.executionAvgTime = BeanUtils.getLongBeanProperty(src, "executionAvgTime");
134       this.executionMaxTime = BeanUtils.getLongBeanProperty(src, "executionMaxTime");
135       this.executionMinTime = BeanUtils.getLongBeanProperty(src, "executionMinTime");
136     } catch (Exception e) {
137       e.printStackTrace();
138       throw new RuntimeException("Exception retrieving statistics", e);
139     }
140   }
141 
142   /***
143    * @param cData
144    */
145   public QueryStats(final CompositeData cData) {
146     int i = 0;
147     query = (String) cData.get(ITEM_NAMES[i++]);
148     cacheHitCount = (Long) cData.get(ITEM_NAMES[i++]);
149     cacheMissCount = (Long) cData.get(ITEM_NAMES[i++]);
150     cachePutCount = (Long) cData.get(ITEM_NAMES[i++]);
151     executionCount = (Long) cData.get(ITEM_NAMES[i++]);
152     executionRowCount = (Long) cData.get(ITEM_NAMES[i++]);
153     executionAvgTime = (Long) cData.get(ITEM_NAMES[i++]);
154     executionMaxTime = (Long) cData.get(ITEM_NAMES[i++]);
155     executionMinTime = (Long) cData.get(ITEM_NAMES[i++]);
156   }
157 
158   private static int safeParseInt(String s) {
159       try {
160           return Integer.parseInt(s);
161       } catch (Exception e) {
162           return -1;
163       }
164   }
165   
166   /***
167    * @param stats
168    */
169   public void add(QueryStats stats) {
170     cacheHitCount += stats.getCacheHitCount();
171     cacheMissCount += stats.getCacheMissCount();
172     cachePutCount += stats.getCachePutCount();
173     executionCount += stats.getExecutionCount();
174     executionRowCount += stats.getExecutionRowCount();
175     executionAvgTime += stats.getExecutionAvgTime();
176     executionMaxTime += stats.getExecutionMaxTime();
177     executionMinTime += stats.getExecutionMinTime();
178   }
179 
180   /***
181    * toString
182    */
183   @Override
184   public String toString() {
185     return "query=" + query + ", cacheHitCount=" + cacheHitCount + ", cacheMissCount=" + cacheMissCount
186            + ", cachePutCount=" + cachePutCount + ", executionCount=" + executionCount + ", executionRowCount="
187            + executionRowCount + ", executionAvgTime=" + executionAvgTime + ", executionMaxTime=" + executionMaxTime
188            + ", executionMinTime=" + executionMinTime;
189   }
190 
191   /***
192    * getQuery
193    */
194   public String getQuery() {
195     return query;
196   }
197 
198   /***
199    * getCacheHitCount
200    */
201   public long getCacheHitCount() {
202     return cacheHitCount;
203   }
204 
205   /***
206    * getCacheMissCount
207    */
208   public long getCacheMissCount() {
209     return cacheMissCount;
210   }
211 
212   /***
213    * getCachePutCount
214    */
215   public long getCachePutCount() {
216     return cachePutCount;
217   }
218 
219   /***
220    * getExecutionCount
221    */
222   public long getExecutionCount() {
223     return executionCount;
224   }
225 
226   /***
227    * getExecutionRowCount
228    */
229   public long getExecutionRowCount() {
230     return executionRowCount;
231   }
232 
233   /***
234    * getExecutionAvgTime
235    */
236   public long getExecutionAvgTime() {
237     return executionAvgTime;
238   }
239 
240   /***
241    * getExecutionMaxTime
242    */
243   public long getExecutionMaxTime() {
244     return executionMaxTime;
245   }
246 
247   /***
248    * getExecutionMinTime
249    */
250   public long getExecutionMinTime() {
251     return executionMinTime;
252   }
253 
254   /***
255    * toCompositeData
256    */
257   public CompositeData toCompositeData() {
258     try {
259       return new CompositeDataSupport(COMPOSITE_TYPE, ITEM_NAMES, new Object[] {query, cacheHitCount, cacheMissCount,
260           cachePutCount, executionCount, executionRowCount, executionAvgTime, executionMaxTime, executionMinTime, });
261     } catch (OpenDataException e) {
262       throw new RuntimeException(e);
263     }
264   }
265 
266   /***
267    * newTabularDataInstance
268    */
269   public static TabularData newTabularDataInstance() {
270     return new TabularDataSupport(TABULAR_TYPE);
271   }
272 
273   /***
274    * fromTabularData
275    */
276   public static QueryStats[] fromTabularData(final TabularData tabularData) {
277     final List<QueryStats> countList = new ArrayList(tabularData.size());
278     for (final Iterator pos = tabularData.values().iterator(); pos.hasNext();) {
279       countList.add(new QueryStats((CompositeData) pos.next()));
280     }
281     return countList.toArray(new QueryStats[countList.size()]);
282   }
283 }