001: /*
002: * $Id: LoadHSQLStorage.java,v 1.1 2006/03/06 11:30:53 azzazzel Exp $
003: *
004: * Copyright 2006 Commsen International
005: *
006: * Licensed under the Common Public License, Version 1.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.opensource.org/licenses/cpl1.0.txt
011: *
012: * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013: * EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS
014: * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
015: *
016: */
017: package com.commsen.stopwatch.storages;
018:
019: import java.sql.PreparedStatement;
020: import java.sql.ResultSet;
021: import java.sql.SQLException;
022: import java.sql.Timestamp;
023: import java.util.ArrayList;
024: import java.util.HashMap;
025: import java.util.Map;
026:
027: import org.apache.log4j.Logger;
028:
029: import com.commsen.stopwatch.Report;
030: import com.commsen.stopwatch.StopwatchStorageException;
031: import com.commsen.stopwatch.reports.LoadStopwatchReport;
032:
033: /**
034: * TODO Dokumentacja
035: *
036: * @author Milen Dyankov
037: * @deprecated use {@link com.commsen.stopwatch.Stopwatch#getLoad(int, int)} instead!
038: * It works with any engine and storage.
039: *
040: */
041: public class LoadHSQLStorage extends DefaultHSQLStorage {
042:
043: /**
044: * Logger for this class
045: */
046: protected static final Logger log = Logger
047: .getLogger(LoadHSQLStorage.class);
048:
049: // protected String getTableName() { return "load_stopwatch"; }
050:
051: protected String getCreateTableQuery() {
052: return " create table " + getTableName() + " ("
053: + " _id INT GENERATED BY DEFAULT AS IDENTITY,"
054: + " _group VARCHAR," + " _label VARCHAR,"
055: + " _start TIMESTAMP," + " _end TIMESTAMP,"
056: + " _load INT" + ")";
057: }
058:
059: protected String getReturnColumns() {
060: return " count(1), "
061: + " min (DATEDIFF('ms', _start, _end)) as minTime,"
062: + " max (DATEDIFF('ms', _start, _end)) as maxTime,"
063: + " avg (DATEDIFF('ms', _start, _end)) as avgTime,"
064: + " sum (DATEDIFF('ms', _start, _end)) as totalTime, "
065: + " min(_load) as minLoad, "
066: + " max(_load) as maxLoad, "
067: + " avg(_load) as avgLoad ";
068: }
069:
070: public String getInsertQuery() {
071: return "insert into "
072: + getTableName()
073: + " (_group, _label, _start, _load) values (?, ?, ?, ?)";
074: }
075:
076: private Map byLabelCount = new HashMap();
077: private Map byIdCount = new HashMap();
078:
079: /**
080: * @see com.commsen.stopwatch.StopwatchStorage#newRecord(java.lang.Object[])
081: */
082: public long newRecord(Object[] parameters)
083: throws StopwatchStorageException {
084: if (insertPreparedStatement == null)
085: return -1;
086: try {
087: synchronized (insertPreparedStatement.getConnection()) {
088: // increase load counter
089: String group = (String) parameters[0];
090: String name = (String) parameters[1];
091: String key = group + "|" + name;
092: int count;
093: if (byLabelCount.containsKey(key))
094: count = ((Integer) byLabelCount.get(key))
095: .intValue() + 1;
096: else
097: count = 1;
098: byLabelCount.put(key, new Integer(count));
099:
100: insertPreparedStatement.setString(1, group);
101: insertPreparedStatement.setString(2, name);
102: insertPreparedStatement.setTimestamp(3, new Timestamp(
103: ((Long) parameters[2]).longValue()));
104: insertPreparedStatement.setInt(4, count);
105: insertPreparedStatement.executeUpdate();
106: ResultSet resultSet = lastIdentityStatement
107: .executeQuery();
108: resultSet.next();
109: long result = resultSet.getLong(1);
110: resultSet.close();
111: byIdCount.put(new Long(result), key);
112: return result;
113: }
114: } catch (SQLException e) {
115: throw new StopwatchStorageException("database error", e);
116: }
117: }
118:
119: /**
120: * @see com.commsen.stopwatch.StopwatchStorage#completeRecord(long, Object[])
121: */
122: public boolean completeRecord(long id, Object[] parameters)
123: throws StopwatchStorageException {
124: if (id < 0)
125: return false;
126: try {
127: synchronized (updatePreparedStatement.getConnection()) {
128: updatePreparedStatement.setTimestamp(1, new Timestamp(
129: ((Long) parameters[0]).longValue()));
130: updatePreparedStatement.setLong(2, id);
131: updatePreparedStatement.executeUpdate();
132:
133: // decrease load counter
134: Long longId = new Long(id);
135: if (byIdCount.containsKey(longId)) {
136: String key = (String) byIdCount.get(longId);
137: if (byLabelCount.containsKey(key)) {
138: int count = ((Integer) byLabelCount.get(key))
139: .intValue() - 1;
140: byLabelCount.put(key, new Integer(count));
141: }
142: }
143:
144: return true;
145: }
146: } catch (SQLException e) {
147: throw new StopwatchStorageException("database error", e);
148: }
149:
150: }
151:
152: /**
153: *
154: * @see com.commsen.stopwatch.StopwatchStorage#removeRecord(long)
155: */
156: public boolean removeRecord(long id)
157: throws StopwatchStorageException {
158: if (id < 0)
159: return false;
160: try {
161: synchronized (deletePreparedStatement.getConnection()) {
162: deletePreparedStatement.setLong(1, id);
163: deletePreparedStatement.executeUpdate();
164:
165: // decrease load counter
166: Long longId = new Long(id);
167: if (byIdCount.containsKey(longId)) {
168: String key = (String) byIdCount.get(longId);
169: if (byLabelCount.containsKey(key)) {
170: int count = ((Integer) byLabelCount.get(key))
171: .intValue() - 1;
172: byLabelCount.put(key, new Integer(count));
173: }
174: }
175:
176: return true;
177: }
178: } catch (SQLException e) {
179: throw new StopwatchStorageException("database error", e);
180: }
181: }
182:
183: /**
184: *
185: * @see com.commsen.stopwatch.storages.AbstractDatabaseStorage#prepareReports(java.sql.PreparedStatement, java.lang.Object[])
186: */
187: protected Report[] prepareReports(
188: PreparedStatement preparedStatement, Object[] params)
189: throws SQLException {
190: if (preparedStatement == null)
191: return new Report[0];
192: ArrayList list = new ArrayList();
193: synchronized (preparedStatement.getConnection()) {
194:
195: if (params != null && params.length > 0) {
196: for (int i = 0; i < params.length; i++) {
197: preparedStatement.setObject(i + 1, params[i]);
198: }
199: }
200:
201: ResultSet resultSet = preparedStatement.executeQuery();
202: while (resultSet.next()) {
203: list.add(new LoadStopwatchReport(
204: resultSet.getString(1), resultSet.getString(2),
205: resultSet.getLong(3), resultSet.getLong(4),
206: resultSet.getLong(5), resultSet.getLong(6),
207: resultSet.getLong(7), resultSet.getLong(8),
208: resultSet.getLong(9), resultSet.getLong(10)));
209: }
210: }
211: return (Report[]) list.toArray(new Report[list.size()]);
212: }
213:
214: protected Logger getLogger() {
215: return log;
216: }
217:
218: }
|