001: /*
002:
003: Derby - Class org.apache.derby.impl.store.access.sort.MergeSortInfo
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.store.access.sort;
023:
024: import org.apache.derby.iapi.store.access.SortInfo;
025:
026: import org.apache.derby.iapi.error.StandardException;
027:
028: import org.apache.derby.iapi.reference.SQLState;
029:
030: import org.apache.derby.iapi.services.i18n.MessageService;
031:
032: import java.util.Vector;
033: import java.util.Properties;
034:
035: /**
036:
037: This object provides performance information related to a sort.
038: The information is accumulated during operations on a SortController() and
039: then copied into this object and returned by a call to
040: SortController.getSortInfo().
041:
042: @see org.apache.derby.iapi.store.access.SortController#getSortInfo()
043:
044: **/
045: class MergeSortInfo implements SortInfo {
046: /**
047: * Performance counters ...
048: */
049:
050: private String stat_sortType;
051: // private long stat_estimMemUsed;
052: private int stat_numRowsInput;
053: private int stat_numRowsOutput;
054: private int stat_numMergeRuns;
055: private Vector stat_mergeRunsSize;
056:
057: /* Constructors for This class: */
058: MergeSortInfo(MergeInserter sort) {
059: // copy perfomance state out of sort, to get a fixed set of stats
060: stat_sortType = sort.stat_sortType;
061: // stat_estimMemUsed = sort.estimatedMemoryUsed;
062: stat_numRowsInput = sort.stat_numRowsInput;
063: stat_numRowsOutput = sort.stat_numRowsOutput;
064: stat_numMergeRuns = sort.stat_numMergeRuns;
065: stat_mergeRunsSize = sort.stat_mergeRunsSize;
066: }
067:
068: /**
069: * Return all information gathered about the sort.
070: * <p>
071: * This routine returns a list of properties which contains all information
072: * gathered about the sort. If a Property is passed in, then that property
073: * list is appended to, otherwise a new property object is created and
074: * returned.
075: * <p>
076: * Not all sorts may support all properties, if the property is not
077: * supported then it will not be returned. The following is a list of
078: * properties that may be returned:
079: *
080: * sortType
081: * - type of the sort being performed:
082: * internal
083: * external
084: * numRowsInput
085: * - the number of rows input to the sort. This
086: * number includes duplicates.
087: * numRowsOutput
088: * - the number of rows to be output by the sort. This number
089: * may be different from numRowsInput since duplicates may not
090: * be output.
091: * numMergeRuns
092: * - the number of merge runs for the sort.
093: * Applicable to external sorts only.
094: * Note: when a SortController is closed, numMergeRuns may increase by 1, to
095: * reflect the additional merge run that may be created for
096: * any data still in the sort buffer.
097: * mergeRunsSize
098: * - the size (number of rows) of each merge run for the sort.
099: * Applicable to external sorts only.
100: * e.g. [3,3,2] indicates 3 merge runs, where the first two runs
101: * have 3 rows each, and the last run has 2 rows.
102: * Note: when a SortController is closed, this vector may get an
103: * additional element, to reflect the additional merge run that
104: * may be created for any data still in the sort buffer.
105: * NOTE - this list will be expanded as more information about the sort
106: * is gathered and returned.
107: *
108: * @param prop Property list to fill in.
109: *
110: * @exception StandardException Standard exception policy.
111: **/
112:
113: public Properties getAllSortInfo(Properties prop)
114: throws StandardException {
115: if (prop == null)
116: prop = new Properties();
117:
118: prop
119: .put(
120: MessageService
121: .getTextMessage(SQLState.STORE_RTS_SORT_TYPE),
122: "external".equals(this .stat_sortType) ? MessageService
123: .getTextMessage(SQLState.STORE_RTS_EXTERNAL)
124: : MessageService
125: .getTextMessage(SQLState.STORE_RTS_INTERNAL));
126: // prop.put(
127: // MessageService.getTextMessage(SQLState.STORE_RTS_ESTIMATED_MEMORY_USED),
128: // Long.toString(stat_estimMemUsed));
129: prop.put(MessageService
130: .getTextMessage(SQLState.STORE_RTS_NUM_ROWS_INPUT),
131: Integer.toString(stat_numRowsInput));
132: prop.put(MessageService
133: .getTextMessage(SQLState.STORE_RTS_NUM_ROWS_OUTPUT),
134: Integer.toString(stat_numRowsOutput));
135: if (this .stat_sortType == "external") {
136: prop.put(MessageService
137: .getTextMessage(SQLState.STORE_RTS_NUM_MERGE_RUNS),
138: Integer.toString(stat_numMergeRuns));
139: prop
140: .put(
141: MessageService
142: .getTextMessage(SQLState.STORE_RTS_MERGE_RUNS_SIZE),
143: stat_mergeRunsSize.toString());
144: }
145: return (prop);
146: }
147: }
148:
149: /**
150: * estimMemUsed IS NOT CURRENTLY SUPPORTED SINCE IT IS UNRELIABLE
151: * estimMemUsed
152: * - the estimated memory used by the sort.
153: *
154: * This is only measured when the system runs out of sort
155: * buffer space, AND when it tries to avoid doing an external sort.
156: * It measures this by subtracting the memory usage at initialization
157: * from the memory usage at the time we are trying to avoid doing an
158: * external sort. The result could be negative: this probably indicates
159: * that there has been some garbage collection in the interim.
160: * If the attempt at keeping the sort internal succeeds, the buffer grows
161: * but the increased memory usage is not measured.
162: *
163: * The system may never measure the memory usage. This happens if
164: * it never runs out of sort buffer space, or if it is set up not
165: * to avoid external sorts. In cases that it is not measured, it returns 0.
166: *
167: * In future, this info may improve with an improved JVM API.
168: */
|