Package com.commsen.stopwatch.storages

Source Code of com.commsen.stopwatch.storages.LoadHSQLInMemoryStorage

/*
* $Id: LoadHSQLInMemoryStorage.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 java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

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

/**
* TODO Dokumentacja
*
* @author Milen Dyankov
* @deprecated use {@link com.commsen.stopwatch.Stopwatch#getLoad(int, int)}  instead!
* It works with any engine and storage.
*
*/
public class LoadHSQLInMemoryStorage extends DefaultHSQLInMemoryStorage {

  /**
   * Logger for this class
   */
  protected static final Logger log = Logger.getLogger(LoadHSQLInMemoryStorage.class);
 
 
  protected String getTableName() { return "load_stopwatch"; }
 
  protected String getCreateTableQuery() {
    return
    " create table " + getTableName() + " (" +
    "   _id INT GENERATED BY DEFAULT AS IDENTITY," +
    "   _group VARCHAR," +
    "   _label VARCHAR," +
    "   _start TIMESTAMP," +
    "   _end TIMESTAMP," +
    "   _load INT" +
    ")";
  }

  protected String getReturnColumns() {
    return 
    "  count(1), " +
    "   min (DATEDIFF('ms', _start, _end)) as minTime," +
    "   max (DATEDIFF('ms', _start, _end)) as maxTime," +
    "   avg (DATEDIFF('ms', _start, _end)) as avgTime," +
    "   sum (DATEDIFF('ms', _start, _end)) as totalTime, " +
    "   min(_load) as minLoad,  " +
    "   max(_load) as maxLoad,  " +
    "  avg(_load) as avgLoad ";
 
 
  public String getInsertQuery() {
    return "insert into " + getTableName() + " (_group, _label, _start, _load) values (?, ?, ?, ?)";
  }
 
 
  private Map byLabelCount = new HashMap();
  private Map byIdCount = new HashMap();
 
 
  /**
   * @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()) {
        // increase load counter
        String group = (String)parameters[0];
        String name = (String)parameters[1];
        String key = group + "|" + name;
        int count;
        if (byLabelCount.containsKey(key)) count = ((Integer)byLabelCount.get(key)).intValue() + 1;
        else count = 1;
        byLabelCount.put(key, new Integer(count));
       
        insertPreparedStatement.setString(1, group);
        insertPreparedStatement.setString(2, name);
        insertPreparedStatement.setTimestamp(3, new Timestamp(((Long)parameters[2]).longValue()));
        insertPreparedStatement.setInt(4, count);
        insertPreparedStatement.executeUpdate();
        ResultSet resultSet = lastIdentityStatement.executeQuery();
        resultSet.next();
        long result = resultSet.getLong(1);
        resultSet.close();
        byIdCount.put(new Long(result), key);
        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, id);
        updatePreparedStatement.executeUpdate();
       
        // decrease load counter
        Long longId = new Long(id);
        if (byIdCount.containsKey(longId)) {
          String key = (String)byIdCount.get(longId);
          if (byLabelCount.containsKey(key)) {
            int count = ((Integer)byLabelCount.get(key)).intValue() - 1;
            byLabelCount.put(key, new Integer(count));
          }
        }
       
        return true;
      }
    } catch (SQLException e) {
          throw new StopwatchStorageException("database error", e);
    }

  } 
 

  /**
   *
   * @see com.commsen.stopwatch.StopwatchStorage#removeRecord(long)
   */
  public boolean removeRecord(long id) throws StopwatchStorageException {
    if (id < 0) return false;
    try {
      synchronized (deletePreparedStatement.getConnection()) {
        deletePreparedStatement.setLong(1, id);
        deletePreparedStatement.executeUpdate();
       
        // decrease load counter
        Long longId = new Long(id);
        if (byIdCount.containsKey(longId)) {
          String key = (String)byIdCount.get(longId);
          if (byLabelCount.containsKey(key)) {
            int count = ((Integer)byLabelCount.get(key)).intValue() - 1;
            byLabelCount.put(key, new Integer(count));
          }
        }
       
        return true;
      }
    } catch (SQLException e) {
          throw new StopwatchStorageException("database error", e);
    }
  }
 
 
  /**
   *
   * @see com.commsen.stopwatch.storages.AbstractDatabaseStorage#prepareReports(java.sql.PreparedStatement, java.lang.Object[])
   */
  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 LoadStopwatchReport(
          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;
  }   
 
}
TOP

Related Classes of com.commsen.stopwatch.storages.LoadHSQLInMemoryStorage

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.