View Javadoc

1   /*
2    * $Id: LoadHSQLInMemoryStorage.java,v 1.1 2006/03/06 11:30:53 azzazzel Exp $
3    *
4    * Copyright 2006 Commsen International
5    * 
6    * Licensed under the Common Public License, Version 1.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * 
10   *      http://www.opensource.org/licenses/cpl1.0.txt
11   * 
12   * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS 
14   * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
15   *
16   */
17  package com.commsen.stopwatch.storages;
18  
19  import java.sql.PreparedStatement;
20  import java.sql.ResultSet;
21  import java.sql.SQLException;
22  import java.sql.Timestamp;
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import org.apache.log4j.Logger;
28  
29  import com.commsen.stopwatch.Report;
30  import com.commsen.stopwatch.StopwatchStorageException;
31  import com.commsen.stopwatch.reports.LoadStopwatchReport;
32  
33  /***
34   * TODO Dokumentacja 
35   *
36   * @author Milen Dyankov
37   * @deprecated use {@link com.commsen.stopwatch.Stopwatch#getLoad(int, int)}  instead! 
38   * It works with any engine and storage.
39   *
40   */
41  public class LoadHSQLInMemoryStorage extends DefaultHSQLInMemoryStorage {
42  
43  	/***
44  	 * Logger for this class
45  	 */
46  	protected static final Logger log = Logger.getLogger(LoadHSQLInMemoryStorage.class);
47  	
48  	
49  	protected String getTableName() { return "load_stopwatch"; }
50  	
51  	protected String getCreateTableQuery() { 
52  		return 
53  		" create table " + getTableName() + " (" +
54  		"   _id INT GENERATED BY DEFAULT AS IDENTITY," +
55  		"   _group VARCHAR," +
56  		"   _label VARCHAR," +
57  		"   _start TIMESTAMP," +
58  		"   _end TIMESTAMP," +
59  		"   _load INT" +
60  		")";
61  	}
62  
63  	protected String getReturnColumns() { 
64  		return  
65  		"  count(1), " +
66  		"   min (DATEDIFF('ms', _start, _end)) as minTime," +
67  		"   max (DATEDIFF('ms', _start, _end)) as maxTime," +
68  		"   avg (DATEDIFF('ms', _start, _end)) as avgTime," +
69  		"   sum (DATEDIFF('ms', _start, _end)) as totalTime, " +
70  		"   min(_load) as minLoad,  " +
71  		"   max(_load) as maxLoad,  " +
72  		"	avg(_load) as avgLoad ";
73  	}	
74  	
75  	public String getInsertQuery() { 
76  		return "insert into " + getTableName() + " (_group, _label, _start, _load) values (?, ?, ?, ?)";
77  	}
78  	
79  	
80  	private Map byLabelCount = new HashMap();
81  	private Map byIdCount = new HashMap();
82  	
83  	
84  	/***
85  	 * @see com.commsen.stopwatch.StopwatchStorage#newRecord(java.lang.Object[])
86  	 */
87  	public long newRecord(Object[] parameters) throws StopwatchStorageException {
88  		if (insertPreparedStatement == null) return -1;
89  		try {
90  			synchronized (insertPreparedStatement.getConnection()) {
91  				// increase load counter
92  				String group = (String)parameters[0];
93  				String name = (String)parameters[1];
94  				String key = group + "|" + name;
95  				int count;
96  				if (byLabelCount.containsKey(key)) count = ((Integer)byLabelCount.get(key)).intValue() + 1;
97  				else count = 1;
98  				byLabelCount.put(key, new Integer(count));
99  				
100 				insertPreparedStatement.setString(1, group);
101 				insertPreparedStatement.setString(2, name);
102 				insertPreparedStatement.setTimestamp(3, new Timestamp(((Long)parameters[2]).longValue()));
103 				insertPreparedStatement.setInt(4, count);
104 				insertPreparedStatement.executeUpdate();
105 				ResultSet resultSet = lastIdentityStatement.executeQuery();
106 				resultSet.next();
107 				long result = resultSet.getLong(1);
108 				resultSet.close();
109 				byIdCount.put(new Long(result), key);
110 				return result;
111 			}
112 		} catch (SQLException e) {
113 	        throw new StopwatchStorageException("database error", e);
114 		}
115 	}	
116 
117 	/***
118 	 * @see com.commsen.stopwatch.StopwatchStorage#completeRecord(long, Object[])
119 	 */
120 	public boolean completeRecord(long id, Object[] parameters) throws StopwatchStorageException {
121 		if (id < 0) return false;
122 		try {
123 			synchronized (updatePreparedStatement.getConnection()) {
124 				updatePreparedStatement.setTimestamp(1, new Timestamp(((Long)parameters[0]).longValue()));
125 				updatePreparedStatement.setLong(2, id);
126 				updatePreparedStatement.executeUpdate();
127 				
128 				// decrease load counter
129 				Long longId = new Long(id);
130 				if (byIdCount.containsKey(longId)) {
131 					String key = (String)byIdCount.get(longId);
132 					if (byLabelCount.containsKey(key)) {
133 						int count = ((Integer)byLabelCount.get(key)).intValue() - 1;
134 						byLabelCount.put(key, new Integer(count));
135 					}
136 				}
137 				
138 				return true;
139 			}
140 		} catch (SQLException e) {
141 	        throw new StopwatchStorageException("database error", e);
142 		}
143 
144 	}	
145 	
146 
147 	/***
148 	 * 
149 	 * @see com.commsen.stopwatch.StopwatchStorage#removeRecord(long)
150 	 */
151 	public boolean removeRecord(long id) throws StopwatchStorageException {
152 		if (id < 0) return false;
153 		try {
154 			synchronized (deletePreparedStatement.getConnection()) {
155 				deletePreparedStatement.setLong(1, id);
156 				deletePreparedStatement.executeUpdate();
157 				
158 				// decrease load counter
159 				Long longId = new Long(id);
160 				if (byIdCount.containsKey(longId)) {
161 					String key = (String)byIdCount.get(longId);
162 					if (byLabelCount.containsKey(key)) {
163 						int count = ((Integer)byLabelCount.get(key)).intValue() - 1;
164 						byLabelCount.put(key, new Integer(count));
165 					}
166 				}
167 				
168 				return true;
169 			}
170 		} catch (SQLException e) {
171 	        throw new StopwatchStorageException("database error", e);
172 		}
173 	}
174 	
175 	
176 	/***
177 	 * 
178 	 * @see com.commsen.stopwatch.storages.AbstractDatabaseStorage#prepareReports(java.sql.PreparedStatement, java.lang.Object[])
179 	 */
180 	protected Report[] prepareReports (PreparedStatement preparedStatement, Object[] params) throws SQLException {
181 		if (preparedStatement == null) return new Report[0];
182 		ArrayList list = new ArrayList();
183 		synchronized (preparedStatement.getConnection()) {
184 			
185 			if (params != null && params.length > 0) {
186 				for (int i = 0; i < params.length; i++) {
187 					preparedStatement.setObject(i+1, params[i]);
188 				}
189 			}
190 			
191 			ResultSet resultSet = preparedStatement.executeQuery();
192 			while (resultSet.next()) {
193 				list.add(new LoadStopwatchReport( 
194 					resultSet.getString(1),
195 					resultSet.getString(2),
196 					resultSet.getLong(3),
197 					resultSet.getLong(4),
198 					resultSet.getLong(5),
199 					resultSet.getLong(6),
200 					resultSet.getLong(7),
201 					resultSet.getLong(8),
202 					resultSet.getLong(9),
203 					resultSet.getLong(10)
204 					)
205 				);
206 			}
207 		}
208 		return (Report[])list.toArray(new Report[list.size()]);
209 	}
210 	
211 	protected Logger getLogger () {
212 		return log;
213 	}		
214 	
215 }