001: /*
002: * $Id: LoadHSQLInMemoryStorage.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 LoadHSQLInMemoryStorage extends DefaultHSQLInMemoryStorage {
042:
043: /**
044: * Logger for this class
045: */
046: protected static final Logger log = Logger
047: .getLogger(LoadHSQLInMemoryStorage.class);
048:
049: protected String getTableName() {
050: return "load_stopwatch";
051: }
052:
053: protected String getCreateTableQuery() {
054: return " create table " + getTableName() + " ("
055: + " _id INT GENERATED BY DEFAULT AS IDENTITY,"
056: + " _group VARCHAR," + " _label VARCHAR,"
057: + " _start TIMESTAMP," + " _end TIMESTAMP,"
058: + " _load INT" + ")";
059: }
060:
061: protected String getReturnColumns() {
062: return " count(1), "
063: + " min (DATEDIFF('ms', _start, _end)) as minTime,"
064: + " max (DATEDIFF('ms', _start, _end)) as maxTime,"
065: + " avg (DATEDIFF('ms', _start, _end)) as avgTime,"
066: + " sum (DATEDIFF('ms', _start, _end)) as totalTime, "
067: + " min(_load) as minLoad, "
068: + " max(_load) as maxLoad, "
069: + " avg(_load) as avgLoad ";
070: }
071:
072: public String getInsertQuery() {
073: return "insert into "
074: + getTableName()
075: + " (_group, _label, _start, _load) values (?, ?, ?, ?)";
076: }
077:
078: private Map byLabelCount = new HashMap();
079: private Map byIdCount = new HashMap();
080:
081: /**
082: * @see com.commsen.stopwatch.StopwatchStorage#newRecord(java.lang.Object[])
083: */
084: public long newRecord(Object[] parameters)
085: throws StopwatchStorageException {
086: if (insertPreparedStatement == null)
087: return -1;
088: try {
089: synchronized (insertPreparedStatement.getConnection()) {
090: // increase load counter
091: String group = (String) parameters[0];
092: String name = (String) parameters[1];
093: String key = group + "|" + name;
094: int count;
095: if (byLabelCount.containsKey(key))
096: count = ((Integer) byLabelCount.get(key))
097: .intValue() + 1;
098: else
099: count = 1;
100: byLabelCount.put(key, new Integer(count));
101:
102: insertPreparedStatement.setString(1, group);
103: insertPreparedStatement.setString(2, name);
104: insertPreparedStatement.setTimestamp(3, new Timestamp(
105: ((Long) parameters[2]).longValue()));
106: insertPreparedStatement.setInt(4, count);
107: insertPreparedStatement.executeUpdate();
108: ResultSet resultSet = lastIdentityStatement
109: .executeQuery();
110: resultSet.next();
111: long result = resultSet.getLong(1);
112: resultSet.close();
113: byIdCount.put(new Long(result), key);
114: return result;
115: }
116: } catch (SQLException e) {
117: throw new StopwatchStorageException("database error", e);
118: }
119: }
120:
121: /**
122: * @see com.commsen.stopwatch.StopwatchStorage#completeRecord(long, Object[])
123: */
124: public boolean completeRecord(long id, Object[] parameters)
125: throws StopwatchStorageException {
126: if (id < 0)
127: return false;
128: try {
129: synchronized (updatePreparedStatement.getConnection()) {
130: updatePreparedStatement.setTimestamp(1, new Timestamp(
131: ((Long) parameters[0]).longValue()));
132: updatePreparedStatement.setLong(2, id);
133: updatePreparedStatement.executeUpdate();
134:
135: // decrease load counter
136: Long longId = new Long(id);
137: if (byIdCount.containsKey(longId)) {
138: String key = (String) byIdCount.get(longId);
139: if (byLabelCount.containsKey(key)) {
140: int count = ((Integer) byLabelCount.get(key))
141: .intValue() - 1;
142: byLabelCount.put(key, new Integer(count));
143: }
144: }
145:
146: return true;
147: }
148: } catch (SQLException e) {
149: throw new StopwatchStorageException("database error", e);
150: }
151:
152: }
153:
154: /**
155: *
156: * @see com.commsen.stopwatch.StopwatchStorage#removeRecord(long)
157: */
158: public boolean removeRecord(long id)
159: throws StopwatchStorageException {
160: if (id < 0)
161: return false;
162: try {
163: synchronized (deletePreparedStatement.getConnection()) {
164: deletePreparedStatement.setLong(1, id);
165: deletePreparedStatement.executeUpdate();
166:
167: // decrease load counter
168: Long longId = new Long(id);
169: if (byIdCount.containsKey(longId)) {
170: String key = (String) byIdCount.get(longId);
171: if (byLabelCount.containsKey(key)) {
172: int count = ((Integer) byLabelCount.get(key))
173: .intValue() - 1;
174: byLabelCount.put(key, new Integer(count));
175: }
176: }
177:
178: return true;
179: }
180: } catch (SQLException e) {
181: throw new StopwatchStorageException("database error", e);
182: }
183: }
184:
185: /**
186: *
187: * @see com.commsen.stopwatch.storages.AbstractDatabaseStorage#prepareReports(java.sql.PreparedStatement, java.lang.Object[])
188: */
189: protected Report[] prepareReports(
190: PreparedStatement preparedStatement, Object[] params)
191: throws SQLException {
192: if (preparedStatement == null)
193: return new Report[0];
194: ArrayList list = new ArrayList();
195: synchronized (preparedStatement.getConnection()) {
196:
197: if (params != null && params.length > 0) {
198: for (int i = 0; i < params.length; i++) {
199: preparedStatement.setObject(i + 1, params[i]);
200: }
201: }
202:
203: ResultSet resultSet = preparedStatement.executeQuery();
204: while (resultSet.next()) {
205: list.add(new LoadStopwatchReport(
206: resultSet.getString(1), resultSet.getString(2),
207: resultSet.getLong(3), resultSet.getLong(4),
208: resultSet.getLong(5), resultSet.getLong(6),
209: resultSet.getLong(7), resultSet.getLong(8),
210: resultSet.getLong(9), resultSet.getLong(10)));
211: }
212: }
213: return (Report[]) list.toArray(new Report[list.size()]);
214: }
215:
216: protected Logger getLogger() {
217: return log;
218: }
219:
220: }
|