⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sortresultset.java

📁 derby database source code.good for you.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*   Derby - Class org.apache.derby.impl.sql.execute.SortResultSet   Copyright 1997, 2004 The Apache Software Foundation or its licensors, as applicable.   Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0   Unless required by applicable law or agreed to in writing, software   distributed under the License is distributed on an "AS IS" BASIS,   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   See the License for the specific language governing permissions and   limitations under the License. */package org.apache.derby.impl.sql.execute;import org.apache.derby.iapi.services.monitor.Monitor;import org.apache.derby.iapi.services.sanity.SanityManager;import org.apache.derby.iapi.services.stream.HeaderPrintWriter;import org.apache.derby.iapi.services.stream.InfoStreams;import org.apache.derby.iapi.services.io.Formatable;import org.apache.derby.iapi.sql.execute.CursorResultSet;import org.apache.derby.iapi.sql.ResultSet;import org.apache.derby.iapi.sql.execute.ExecRow;import org.apache.derby.iapi.sql.execute.NoPutResultSet;import org.apache.derby.iapi.sql.Activation;import org.apache.derby.iapi.store.access.ColumnOrdering;import org.apache.derby.iapi.types.DataValueDescriptor;import org.apache.derby.iapi.store.access.SortObserver;import org.apache.derby.iapi.store.access.TransactionController;import org.apache.derby.iapi.store.access.SortController;import org.apache.derby.iapi.store.access.ScanController;import org.apache.derby.iapi.services.loader.GeneratedMethod;import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;import org.apache.derby.iapi.error.StandardException;import org.apache.derby.iapi.types.RowLocation;import org.apache.derby.iapi.services.io.FormatableArrayHolder;import java.util.Properties;import java.util.Vector;import java.util.Enumeration;/** * Takes a source result set, sends it to the sorter, * and returns the results.  If distinct is true, removes * all but one copy of duplicate rows using DistinctAggregator, * which really doesn't aggregate anything at all -- the sorter * assumes that the presence of an aggregator means that * it should return a single row for each set with identical * ordering columns. * <p> * If aggregate is true, then it feeds any number of aggregates * to the sorter.  Each aggregate is an instance of GenericAggregator * which knows which Aggregator to call to perform the * aggregation. * <p> * Brief background on the sorter and aggregates: the sorter * has some rudimentary knowledge about aggregates.  If * it is passed aggregates, it will eliminate duplicates * on the ordering columns.  In the process it will call the * aggregator on each row that is discarded. * <p>  * Note that a DISTINCT on the SELECT list and an aggregate cannot  * be processed by the same SortResultSet(), if there are both * aggregates (distinct or otherwise) and a DISTINCT on the select * list, then 2 separate SortResultSets are required (the DISTINCT * is a sort on the output of the sort with the aggregation).  * <p> * Currently, all rows are fed through the sorter.  This is * true even if there is no sorting needed.  In this case * we feed every row in and just pull every row out (this is * an obvious area for a performance improvement).  We'll * need to know if the rows are sorted before we can make * any optimizations in this area. * <p> * <B>CLONING</B>: Cloning and sorts are an important topic. * Currently we do a lot of cloning.  We clone the following: <UL> * <LI> every row that is inserted into the sorter.  We * need to clone the rows because the source result set might * be reusing rows, and we need to be able to accumulate the * entire result set in the sorter. </LI> * <p> * There are two cloning APIs: cloning by the sorter on * rows that are not discarded as duplicates or cloning * in the SortResultSet prior to inserting into the sorter. * If we have any aggregates at all we always clone prior * to inserting into the sorter.  We need to do this  * because we have to set up the aggregators before passing * them into the sorter.  When we don't have aggregates * we let the sorter to the cloning to avoid unnecessary * clones on duplicate rows that are going to be discarded * anyway. * * @author ames, rewrite for aggregates by jamie, aggregate removal by jerry */public class SortResultSet extends NoPutResultSetImpl	implements CursorResultSet {	/* Run time statistics variables */	public int rowsInput;	public int rowsReturned;	public boolean distinct;    // set in constructor and not altered during    // life of object.    public NoPutResultSet source;    private GeneratedMethod closeCleanup;	private GeneratedMethod rowAllocator;	private ColumnOrdering[] order;	private ColumnOrdering[] savedOrder;	private SortObserver observer;	private ExecRow sortTemplateRow;	public	boolean isInSortedOrder;				// true if source results in sorted order	private	NoPutResultSet	originalSource; // used for run time stats only	private int maxRowSize;	// set in open and not modified thereafter    private ScanController scanController;	// argument to getNextRowFromRS()	private ExecRow sortResultRow;	// In order distincts	private ExecRow currSortedRow;	private boolean nextCalled;	private int numColumns;	// used to track and close sorts	private long genericSortId;	private boolean dropGenericSort;	// remember whether or not any sort was performed	private boolean sorted;	// RTS	public Properties sortProperties = new Properties();    /**	 * Constructor	 *	 * @param	s			input result set	 * @param	distinct	if this is a DISTINCT select list.  	 *		Also set to true for a GROUP BY w/o aggretates	 * @param	isInSortedOrder	true if the source results are in sorted order	 * @param	orderingItem	indicates the number of the	 *		SavedObject off of the PreparedStatement that holds the	 *		ColumOrdering array used by this routine	 * @param	a				activation	 * @param	ra				generated method to build an empty	 *	 	output row 	 * @param	maxRowSize		approx row size, passed to sorter	 * @param	resultSetNumber	The resultSetNumber for this result set	 *	 * @exception StandardException Thrown on error	 */    public SortResultSet(NoPutResultSet s,					boolean distinct,					boolean isInSortedOrder,					int	orderingItem,					Activation a,					GeneratedMethod ra,					int maxRowSize,					int resultSetNumber,				    double optimizerEstimatedRowCount,				    double optimizerEstimatedCost,					GeneratedMethod c) throws StandardException 	{		super(a, resultSetNumber, optimizerEstimatedRowCount, optimizerEstimatedCost);		this.distinct = distinct;		this.isInSortedOrder = isInSortedOrder;        source = s;        originalSource = s;		rowAllocator = ra;		this.maxRowSize = maxRowSize;        closeCleanup = c;		sortTemplateRow = (ExecRow) rowAllocator.invoke(activation);		order = (ColumnOrdering[])					((FormatableArrayHolder)						(a.getPreparedStatement().getSavedObject(orderingItem)))					.getArray(ColumnOrdering.class);		/* NOTE: We need to save order to another variable		 * in the constructor and reset it on every open.		 * This is important because order can get reset in the		 * guts of execution below.  Subsequent sorts could get		 * the wrong result without this logic.		 */		savedOrder = order;		/*		** Create a sort observer that are retained by the		** sort.		*/		observer = new BasicSortObserver(true, distinct, sortTemplateRow, true);		constructorTime += getElapsedMillis(beginTime);    }	///////////////////////////////////////////////////////////////////////////////	//	// ResultSet interface (leftover from NoPutResultSet)	//	///////////////////////////////////////////////////////////////////////////////	/**	 * Open the scan.  Load the sorter and prepare to get	 * rows from it.	 *	 * @exception StandardException thrown if cursor finished.     */	public void	openCore() throws StandardException 	{		nextCalled = false;		beginTime = getCurrentTimeMillis();		// REVISIT: through the direct DB API, this needs to be an		// error, not an ASSERT; users can open twice. Only through JDBC		// is access to open controlled and ensured valid.		if (SanityManager.DEBUG)	    	SanityManager.ASSERT( ! isOpen, "SortResultSet already open");		/* NOTE: We need to save order to another variable		 * in the constructor and reset it on every open.		 * This is important because order can get reset in the		 * guts of execution below.  Subsequent sorts could get		 * the wrong result without this logic.		 */		order = savedOrder;		sortResultRow = sortTemplateRow.getClone();        source.openCore();		/* If this is an in-order distinct then we do not need the sorter.		 * (We filter out the duplicate rows ourselves.)		 * We save a clone of the first row so that subsequent next()s		 * do not overwrite the saved row.		 */		if (isInSortedOrder && distinct)		{			currSortedRow = getNextRowFromRS();			if (currSortedRow != null)			{				currSortedRow = (ExecRow) currSortedRow.getClone();			}		}		else		{			/*			** Load up the sorter.			*/			scanController = loadSorter();			sorted = true;		}	    isOpen = true;		numOpens++;		openTime += getElapsedMillis(beginTime);	}	/**	 * Load up the sorter.  Feed it every row from the	 * source scan.  When done, close	 * the source scan and open the sort.  Return the sort	 * scan controller.	 *	 * @exception StandardException thrown on failure.	 *	 * @return	the sort controller 	 */	private ScanController loadSorter()		throws StandardException	{		SortController 			sorter;		long 					sortId;		ExecRow 				sourceRow;		ExecRow					inputRow;		boolean					inOrder = (order.length == 0 || isInSortedOrder);		int						inputRowCountEstimate = (int) optimizerEstimatedRowCount;		// find the language context and        // Get the current transaction controller		TransactionController tc = getTransactionController();		sortId = tc.createSort((Properties)null, 						sortTemplateRow.getRowArray(),						order,						observer,						inOrder,						inputRowCountEstimate, // est rows					 	maxRowSize			// est rowsize 						);		sorter = tc.openSort(sortId);		genericSortId = sortId;		dropGenericSort = true;			/* The sorter is responsible for doing the cloning */		while ((inputRow = getNextRowFromRS()) != null) 		{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -