All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.commsen.stopwatch.storages.MemoryHSQLStorage Maven / Gradle / Ivy

Go to download

Stopwatch is a free, simple, highly extensible, Java API that allows developers to easily monitor whole application or any part of it. By default Stopwatch generate reports about hits, execution times (total, average, minimum, maximum) as well as load but it can be easily extended to measure anything else by providing custom engine. Out of the box Stopwatch uses an in-memory HSQL database. It is able to persist collected data using a "storage". There is "storage" provided to persist into HSQL database and custom "storage" can be easily integrated.

The newest version!
/*
 * $Id: MemoryHSQLStorage.java,v 1.1 2006/03/06 11:30:53 azzazzel Exp $
 *
 * Copyright 2006 Commsen International
 * 
 * Licensed under the Common Public License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.opensource.org/licenses/cpl1.0.txt
 * 
 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
 * EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS 
 * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
 *
 */
package com.commsen.stopwatch.storages;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;

import org.apache.log4j.Logger;

import com.commsen.stopwatch.Report;
import com.commsen.stopwatch.StopwatchStorageException;
import com.commsen.stopwatch.reports.MemoryStopwatchReport;

/**
 * TODO Dokumentacja 
 *
 * @author Milen Dyankov
 *
 */
public class MemoryHSQLStorage extends DefaultHSQLStorage {
	
	/**
	 * Logger for this class
	 */
	private static final Logger log = Logger.getLogger(MemoryHSQLStorage.class);

	/**
	 * @see com.commsen.stopwatch.storages.AbstractDatabaseStorage#getTableName()
	 */
	protected String getTableName() { return "memory_stopwatch"; }

	protected String getCreateTableQuery() { 
		return 
		" create table " + getTableName() + " (" +
		"   _id INT GENERATED BY DEFAULT AS IDENTITY, " +
		"   _group VARCHAR, " +
		"   _label VARCHAR, " +
		"   _start TIMESTAMP, " +
		"   _end TIMESTAMP, " +
		"   _start_mem int, " +
		"   _end_mem int " +
		")";	
	}

	protected String getReturnColumns() { 
		return  
		"  count(1), " +								//  3 
		"  min (DATEDIFF('ms', _start, _end)), " +		//  4
		"  max (DATEDIFF('ms', _start, _end)), " +		//  5
		"  avg (DATEDIFF('ms', _start, _end)), " +		//  6
		"  sum (DATEDIFF('ms', _start, _end)), " +		//  7
		"  min (_end_mem - _start_mem), " +				//  8
		"  max (_end_mem - _start_mem), " +				//  9
		"  avg (_end_mem - _start_mem) ";				// 10
	}
	
	
	

	public String getInsertQuery() { 
		return "insert into " + getTableName() + " (_group, _label, _start, _start_mem) values (?, ?, ?, ?)";
	}

	protected String getUpdateQuery() { 
		return "update " + getTableName() + " set _end = ?, _end_mem = ? where _id = ? and _end IS NULL";
	}
	
	
	/**
	 * @see com.commsen.stopwatch.StopwatchStorage#newRecord(java.lang.Object[])
	 */
	public long newRecord(Object[] parameters) throws StopwatchStorageException {
		if (insertPreparedStatement == null) return -1;
		try {
			synchronized (insertPreparedStatement.getConnection()) {
				insertPreparedStatement.setString(1, (String)parameters[0]);
				insertPreparedStatement.setString(2, (String)parameters[1]);
				insertPreparedStatement.setTimestamp(3, new Timestamp(((Long)parameters[2]).longValue()));
				insertPreparedStatement.setLong(4, ((Long)parameters[3]).longValue());
				insertPreparedStatement.executeUpdate();
				ResultSet resultSet = lastIdentityStatement.executeQuery();
				resultSet.next();
				long result = resultSet.getLong(1);
				resultSet.close();
				return result;
			}
		} catch (SQLException e) {
	        throw new StopwatchStorageException("database error", e);
		}
	}	

	/**
	 * @see com.commsen.stopwatch.StopwatchStorage#completeRecord(long, Object[])
	 */
	public boolean completeRecord(long id, Object[] parameters) throws StopwatchStorageException {
		if (id < 0) return false;
		try {
			synchronized (updatePreparedStatement.getConnection()) {
				updatePreparedStatement.setTimestamp(1, new Timestamp(((Long)parameters[0]).longValue()));
				updatePreparedStatement.setLong(2, ((Long)parameters[1]).longValue());
				updatePreparedStatement.setLong(3, id);
				updatePreparedStatement.executeUpdate();
				return true;
			}
		} catch (SQLException e) {
	        throw new StopwatchStorageException("database error", e);
		}

	}	
	
	/**
	 * 
	 * @param preparedStatement
	 * @return array of reports
	 * @throws SQLException
	 */
	protected Report[] prepareReports (PreparedStatement preparedStatement, Object[] params) throws SQLException {
		if (preparedStatement == null) return new Report[0];
		ArrayList list = new ArrayList();
		synchronized (preparedStatement.getConnection()) {
			
			if (params != null && params.length > 0) {
				for (int i = 0; i < params.length; i++) {
					preparedStatement.setObject(i+1, params[i]);
				}
			}
			
			ResultSet resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				list.add(new MemoryStopwatchReport( 
						resultSet.getString(1),
						resultSet.getString(2),
						resultSet.getLong(3),
						resultSet.getLong(4),
						resultSet.getLong(5),
						resultSet.getLong(6),
						resultSet.getLong(7),
						resultSet.getLong(8),
						resultSet.getLong(9),
						resultSet.getLong(10)
						
						));
			}
		}
		return (Report[])list.toArray(new Report[list.size()]);
	}
	
	protected Logger getLogger () {
		return log;
	}	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy