--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.impl.store.access.sort.SortBufferRowSource\r
+\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to you under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.impl.store.access.sort;\r
+\r
+import org.apache.derby.iapi.reference.SQLState;\r
+\r
+import org.apache.derby.iapi.services.sanity.SanityManager;\r
+import org.apache.derby.iapi.error.StandardException;\r
+import org.apache.derby.iapi.store.access.SortObserver;\r
+import org.apache.derby.iapi.types.RowLocation;\r
+import org.apache.derby.iapi.store.access.conglomerate.ScanControllerRowSource;\r
+import org.apache.derby.iapi.store.access.conglomerate.TransactionManager;\r
+\r
+import org.apache.derby.iapi.types.DataValueDescriptor;\r
+\r
+import org.apache.derby.iapi.services.io.FormatableBitSet;\r
+\r
+/**\r
+ Wrapping the output of a SortBuffer in a RowSource for the benefit of the\r
+ createAndLoadConglomerate and loadConglomerate interface.\r
+\r
+ Scan implements ScanController, this class just implements the\r
+ RowSource interface. \r
+\r
+*/\r
+public class SortBufferRowSource extends Scan\r
+ implements ScanControllerRowSource\r
+{\r
+ /**\r
+ The Sort buffer where rows come from\r
+ **/\r
+ SortBuffer sortBuffer = null;\r
+\r
+ /**\r
+ The TransactionManager that opened this scan.\r
+ **/\r
+ protected TransactionManager tran = null;\r
+\r
+ private int maxFreeListSize;\r
+ private boolean writingToDisk;\r
+ private SortObserver sortObserver;\r
+\r
+ /*\r
+ * Constructors.\r
+ */\r
+\r
+ SortBufferRowSource(\r
+ SortBuffer sortBuffer, \r
+ TransactionManager tran, \r
+ SortObserver sortObserver,\r
+ boolean writingToDisk, \r
+ int maxFreeListSize)\r
+ {\r
+ super();\r
+ this.sortBuffer = sortBuffer;\r
+ this.tran = tran;\r
+ this.sortObserver = sortObserver;\r
+ this.writingToDisk = writingToDisk;\r
+ this.maxFreeListSize = maxFreeListSize;\r
+ }\r
+\r
+ /* Private/Protected methods of This class: */\r
+ /* Public Methods of This class: */\r
+ /* Public Methods of RowSource class: */\r
+\r
+ public DataValueDescriptor[] getNextRowFromRowSource()\r
+ {\r
+ if (sortBuffer == null) // has been closed\r
+ return null;\r
+\r
+ DataValueDescriptor[] retval = sortBuffer.removeFirst();\r
+\r
+ // Return the removed object to the free DataValueDescriptor[]\r
+ if (retval != null && writingToDisk)\r
+ {\r
+ sortObserver.addToFreeList(retval, maxFreeListSize);\r
+ }\r
+ return retval;\r
+ }\r
+\r
+ public boolean needsRowLocation()\r
+ {\r
+ return false;\r
+ }\r
+\r
+ /**\r
+ * @see org.apache.derby.iapi.store.access.RowSource#needsToClone\r
+ */\r
+ public boolean needsToClone()\r
+ {\r
+ return false;\r
+ }\r
+\r
+ public void rowLocation(RowLocation rl)\r
+ {\r
+ if (SanityManager.DEBUG)\r
+ SanityManager.THROWASSERT("unexpected call to RowSource.rowLocation");\r
+ }\r
+\r
+\r
+ /**\r
+ All columns are always set from a sorter\r
+ */\r
+ public FormatableBitSet getValidColumns()\r
+ {\r
+ return null;\r
+ }\r
+\r
+ /**\r
+ Close the scan\r
+ */\r
+ public void close()\r
+ {\r
+ if (sortBuffer != null)\r
+ {\r
+ sortBuffer.close();\r
+ sortBuffer = null;\r
+ }\r
+ tran.closeMe(this);\r
+ }\r
+\r
+ /**\r
+ Close the scan\r
+ */\r
+ public boolean closeForEndTransaction(boolean closeHeldScan)\r
+ {\r
+ if (SanityManager.DEBUG)\r
+ SanityManager.ASSERT(\r
+ closeHeldScan, \r
+ "Sort scan should not be held open across commit.");\r
+\r
+ close();\r
+ return(true);\r
+ }\r
+\r
+ /**\r
+ Close the rowSource\r
+ */\r
+ public void closeRowSource()\r
+ {\r
+ close();\r
+ }\r
+\r
+ /*\r
+ * Disable illegal and dangerous scan controller interface call\r
+ */\r
+ public boolean next() throws StandardException\r
+ {\r
+ throw StandardException.newException(\r
+ SQLState.SORT_IMPROPER_SCAN_METHOD);\r
+ }\r
+\r
+ /**\r
+ * Fetch the row at the current position of the Scan and does not apply the\r
+ * qualifiers.\r
+ *\r
+ * This method will always throw an exception. \r
+ * (SQLState.SORT_IMPROPER_SCAN_METHOD)\r
+ *\r
+ * @see org.apache.derby.iapi.store.access.ScanController#fetchWithoutQualify\r
+ **/\r
+ public void fetchWithoutQualify(DataValueDescriptor[] result) \r
+ throws StandardException\r
+ {\r
+ throw StandardException.newException(\r
+ SQLState.SORT_IMPROPER_SCAN_METHOD);\r
+ }\r
+\r
+ /**\r
+ * Fetch the row at the current position of the Scan.\r
+ *\r
+ * @see org.apache.derby.iapi.store.access.ScanController#fetch\r
+ **/\r
+ public void fetch(DataValueDescriptor[] result) throws StandardException\r
+ {\r
+ throw StandardException.newException(\r
+ SQLState.SORT_IMPROPER_SCAN_METHOD);\r
+ }\r
+\r
+ public final boolean fetchNext(DataValueDescriptor[] row) \r
+ throws StandardException\r
+ {\r
+ throw StandardException.newException(\r
+ SQLState.SORT_IMPROPER_SCAN_METHOD);\r
+ }\r
+\r
+}\r