001: /*
002: * Copyright (C) Jahia Ltd. All rights reserved.
003: *
004: * This software is published under the terms of the Jahia Open Software
005: * License version 1.1, a copy of which has been included with this
006: * distribution in the LICENSE.txt file.
007: */
008: package org.jahia.sqlprofiler;
009:
010: import java.util.*;
011:
012: /**
013: * <p>Title: Stores the result of a parsed SQL Select statement, storing the
014: * table and columns names associated with the query.</p>
015: * <p>Description: </p>
016: * <p>Copyright: Copyright (c) 2003</p>
017: * <p>Company: Jahia Ltd</p>
018: * @author Serge Huber
019: * @version 1.0
020: */
021:
022: public class QueryStatEntry implements Comparable {
023: private ArrayList tableNames = new ArrayList();
024: private ArrayList columnNames = new ArrayList();
025: private Set queries = new TreeSet();
026: private int occurences = 0;
027: private long totalElapsedTime = 0;
028:
029: public QueryStatEntry() {
030: }
031:
032: public QueryStatEntry(ArrayList tableNames, ArrayList columnNames) {
033: // we copy here to make sure we have only one value for each table and
034: // column name, which is not guaranteed since we are using ArrayList
035: // to preserve order of the detected identifiers.
036: Iterator tableNameIter = tableNames.iterator();
037: while (tableNameIter.hasNext()) {
038: String curTableName = (String) tableNameIter.next();
039: if (!this .tableNames.contains(curTableName)) {
040: this .tableNames.add(curTableName);
041: }
042: }
043: Iterator columnNameIter = columnNames.iterator();
044: while (columnNameIter.hasNext()) {
045: String curColumnName = (String) columnNameIter.next();
046: if (!this .columnNames.contains(curColumnName)) {
047: this .columnNames.add(curColumnName);
048: }
049: }
050: }
051:
052: public java.util.ArrayList getTableNames() {
053: return tableNames;
054: }
055:
056: public java.util.ArrayList getColumnNames() {
057: return columnNames;
058: }
059:
060: public int getOccurences() {
061: return occurences;
062: }
063:
064: public long getTotalElapsedTime() {
065: return totalElapsedTime;
066: }
067:
068: public void incOccurences() {
069: occurences++;
070: }
071:
072: public void incTotalElapseTime(long elapsedTime) {
073: totalElapsedTime += elapsedTime;
074: }
075:
076: public void addQuery(QueryEntry query) {
077: queries.add(query);
078: }
079:
080: public Set getQueries() {
081: return queries;
082: }
083:
084: public String getKey() {
085: String hashCodeStr = tableNames.toString() + "_"
086: + columnNames.toString();
087: return hashCodeStr;
088: }
089:
090: public int hashCode() {
091: return getKey().hashCode();
092: }
093:
094: public boolean equals(Object o) {
095: if (o instanceof QueryStatEntry) {
096: QueryStatEntry right = (QueryStatEntry) o;
097: return getKey().equals(right.getKey());
098: } else {
099: return false;
100: }
101: }
102:
103: public int compareTo(Object o) throws ClassCastException {
104: QueryStatEntry right = (QueryStatEntry) o;
105:
106: // first let's compare total time.
107: Long leftElapsedTime = new Long(getTotalElapsedTime());
108: Long rightElapsedTime = new Long(right.getTotalElapsedTime());
109: int compareElapsed = -leftElapsedTime
110: .compareTo(rightElapsedTime);
111: if (compareElapsed != 0) {
112: return compareElapsed;
113: }
114:
115: Integer left = new Integer(occurences);
116: return -left.compareTo(new Integer(right.getOccurences()));
117: }
118:
119: }
|