001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.execute.rts.RealBasicNoPutResultSetStatistics
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.sql.execute.rts;
023:
024: import org.apache.derby.iapi.services.io.StoredFormatIds;
025: import org.apache.derby.iapi.services.io.Formatable;
026:
027: import org.apache.derby.iapi.services.i18n.MessageService;
028: import org.apache.derby.iapi.reference.SQLState;
029:
030: import org.apache.derby.iapi.services.io.FormatableHashtable;
031:
032: import java.util.Vector;
033:
034: import java.io.ObjectOutput;
035: import java.io.ObjectInput;
036: import java.io.IOException;
037:
038: import java.text.DecimalFormat;
039:
040: /**
041: ResultSetStatistics implemenation for BasicNoPutResultSetImpl.
042:
043: @author jerry
044:
045: */
046: abstract class RealBasicNoPutResultSetStatistics implements
047: ResultSetStatistics {
048:
049: /* Leave these fields public for object inspectors */
050: public int numOpens;
051: public int rowsSeen;
052: public int rowsFiltered;
053: public long constructorTime;
054: public long openTime;
055: public long nextTime;
056: public long closeTime;
057: public long inspectOverall;
058: public long inspectNum;
059: public String inspectDesc;
060: public double optimizerEstimatedRowCount;
061: public double optimizerEstimatedCost;
062:
063: // CONSTRUCTORS
064:
065: /**
066: *
067: *
068: */
069: public RealBasicNoPutResultSetStatistics(int numOpens,
070: int rowsSeen, int rowsFiltered, long constructorTime,
071: long openTime, long nextTime, long closeTime,
072: double optimizerEstimatedRowCount,
073: double optimizerEstimatedCost) {
074: this .numOpens = numOpens;
075: this .rowsSeen = rowsSeen;
076: this .rowsFiltered = rowsFiltered;
077: this .constructorTime = constructorTime;
078: this .openTime = openTime;
079: this .nextTime = nextTime;
080: this .closeTime = closeTime;
081: this .optimizerEstimatedRowCount = optimizerEstimatedRowCount;
082: this .optimizerEstimatedCost = optimizerEstimatedCost;
083: }
084:
085: // Class implementation
086: /**
087: * Dump out the time information for run time stats.
088: *
089: * @return Nothing.
090: */
091: protected final String dumpTimeStats(String indent, String subIndent) {
092: return
093: /*
094: indent + "time spent in this ResultSet = " +
095: getTimeSpent(ResultSet.CURRENT_RESULTSET_ONLY) + "\n" +
096: indent + "time spent in this ResultSet and below = " +
097: getTimeSpent(NoPutResultSet.ENTIRE_RESULTSET_TREE) + "\n" +
098: indent + "total time breakdown: " + "\n" +
099: */
100: subIndent
101: + MessageService
102: .getTextMessage(SQLState.LANG_CONSTRUCTOR_TIME)
103: + " "
104: + constructorTime
105: + "\n"
106: + subIndent
107: + MessageService
108: .getTextMessage(SQLState.LANG_OPEN_TIME)
109: + " "
110: + openTime
111: + "\n"
112: + subIndent
113: + MessageService
114: .getTextMessage(SQLState.LANG_NEXT_TIME)
115: + " "
116: + nextTime
117: + "\n"
118: + subIndent
119: + MessageService
120: .getTextMessage(SQLState.LANG_CLOSE_TIME) + " "
121: + closeTime;
122: }
123:
124: /**
125: * Dump out the estimated cost information
126: *
127: * @return Nothing.
128: */
129: protected final String dumpEstimatedCosts(String subIndent) {
130: return subIndent
131: + MessageService
132: .getTextMessage(SQLState.RTS_OPT_EST_RC)
133: + ": "
134: + formatDouble(optimizerEstimatedRowCount)
135: + "\n"
136: + subIndent
137: + MessageService
138: .getTextMessage(SQLState.RTS_OPT_EST_COST)
139: + ": " + formatDouble(optimizerEstimatedCost) + "\n";
140: }
141:
142: /**
143: * Format a double as a String with leading spaces and two digits
144: * after the decimal.
145: */
146: private static DecimalFormat df = null;
147:
148: private String formatDouble(double toFormat) {
149: if (df == null) {
150: // RESOLVE: This really should use the database locale to
151: // format the number.
152: df = new DecimalFormat("###########0.00");
153: df.setMinimumIntegerDigits(1);
154: }
155:
156: String retval = df.format(toFormat);
157:
158: if (retval.length() < 15) {
159: retval = " ".substring(0, 15 - retval
160: .length())
161: + retval;
162: }
163:
164: return retval;
165: }
166:
167: /**
168: * Get the objects to be displayed when this tree object is expanded.
169: * <P>
170: * The objects returned can be of any type, including addtional Inspectables.
171: *
172: * @return java.util.Vector A vector of objects.
173: */
174: public Vector getChildren() {
175: return new Vector();
176: }
177:
178: /**
179: * Return the time for all operations performed by this node, and the children
180: * of this node. The times included open, next, and close.
181: *
182: */
183: public long getTotalTime() {
184: //The method below is the original calculation. However, the constructor
185: //time was found to be inaccurate, and was therefore removed from the calculation.
186: //return constructorTime + openTime + nextTime + closeTime;
187: return openTime + nextTime + closeTime;
188: }
189:
190: /**
191: * Return the time for all operations performed by the children of this node.
192: *
193: */
194: public long getChildrenTime() {
195: long childrenTime = 0;
196: java.util.Enumeration e = getChildren().elements();
197: while (e.hasMoreElements()) {
198: childrenTime = childrenTime
199: + ((RealBasicNoPutResultSetStatistics) e
200: .nextElement()).getTotalTime();
201: }
202: return childrenTime;
203: }
204:
205: /**
206: * Return the time for all operations performed by this node, but not the
207: * time for the children of this node.
208: *
209: */
210: public long getNodeTime() {
211: return getTotalTime() - getChildrenTime();
212: }
213:
214: /**
215: * Format for display, a name for this node.
216: *
217: */
218: public abstract String getNodeName();
219:
220: /**
221: * If this node is on a database item (like a table or an index), then provide a
222: * string that describes the on item.
223: *
224: */
225: public String getNodeOn() {
226: return "";
227: }
228:
229: /**
230: * Get the estimated row count for the number of rows returned
231: * by the associated query or statement.
232: *
233: * @return The estimated number of rows returned by the associated
234: * query or statement.
235: */
236: public double getEstimatedRowCount() {
237: return optimizerEstimatedRowCount;
238: }
239: }
|