001: /*
002:
003: Derby - Class org.apache.derby.impl.store.access.sort.SortBufferRowSource
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.reference.SQLState;
025:
026: import org.apache.derby.iapi.services.sanity.SanityManager;
027: import org.apache.derby.iapi.error.StandardException;
028: import org.apache.derby.iapi.store.access.RowSource;
029: import org.apache.derby.iapi.store.access.SortObserver;
030: import org.apache.derby.iapi.types.RowLocation;
031: import org.apache.derby.iapi.store.access.conglomerate.ScanControllerRowSource;
032: import org.apache.derby.iapi.store.access.conglomerate.TransactionManager;
033: import org.apache.derby.iapi.store.access.ScanController;
034:
035: import org.apache.derby.iapi.types.DataValueDescriptor;
036:
037: import org.apache.derby.iapi.services.io.FormatableBitSet;
038:
039: /**
040: Wrapping the output of a SortBuffer in a RowSource for the benefit of the
041: createAndLoadConglomerate and loadConglomerate interface.
042:
043: Scan implements ScanController, this class just implements the
044: RowSource interface.
045:
046: */
047: public class SortBufferRowSource extends Scan implements
048: ScanControllerRowSource {
049: /**
050: The Sort buffer where rows come from
051: **/
052: SortBuffer sortBuffer = null;
053:
054: /**
055: The TransactionManager that opened this scan.
056: **/
057: protected TransactionManager tran = null;
058:
059: private int maxFreeListSize;
060: private boolean writingToDisk;
061: private SortObserver sortObserver;
062:
063: /*
064: * Constructors.
065: */
066:
067: SortBufferRowSource(SortBuffer sortBuffer, TransactionManager tran,
068: SortObserver sortObserver, boolean writingToDisk,
069: int maxFreeListSize) {
070: super ();
071: this .sortBuffer = sortBuffer;
072: this .tran = tran;
073: this .sortObserver = sortObserver;
074: this .writingToDisk = writingToDisk;
075: this .maxFreeListSize = maxFreeListSize;
076: }
077:
078: /* Private/Protected methods of This class: */
079: /* Public Methods of This class: */
080: /* Public Methods of RowSource class: */
081:
082: public DataValueDescriptor[] getNextRowFromRowSource() {
083: if (sortBuffer == null) // has been closed
084: return null;
085:
086: DataValueDescriptor[] retval = sortBuffer.removeFirst();
087:
088: // Return the removed object to the free DataValueDescriptor[]
089: if (retval != null && writingToDisk) {
090: sortObserver.addToFreeList(retval, maxFreeListSize);
091: }
092: return retval;
093: }
094:
095: public boolean needsRowLocation() {
096: return false;
097: }
098:
099: /**
100: * @see RowSource#needsToClone
101: */
102: public boolean needsToClone() {
103: return false;
104: }
105:
106: public void rowLocation(RowLocation rl) {
107: if (SanityManager.DEBUG)
108: SanityManager
109: .THROWASSERT("unexpected call to RowSource.rowLocation");
110: }
111:
112: /**
113: All columns are always set from a sorter
114: */
115: public FormatableBitSet getValidColumns() {
116: return null;
117: }
118:
119: /**
120: Close the scan
121: */
122: public void close() {
123: if (sortBuffer != null) {
124: sortBuffer.close();
125: sortBuffer = null;
126: }
127: tran.closeMe(this );
128: }
129:
130: /**
131: Close the scan
132: */
133: public boolean closeForEndTransaction(boolean closeHeldScan) {
134: if (SanityManager.DEBUG)
135: SanityManager.ASSERT(closeHeldScan,
136: "Sort scan should not be held open across commit.");
137:
138: close();
139: return (true);
140: }
141:
142: /**
143: Close the rowSource
144: */
145: public void closeRowSource() {
146: close();
147: }
148:
149: /*
150: * Disable illegal and dangerous scan controller interface call
151: */
152: public boolean next() throws StandardException {
153: throw StandardException
154: .newException(SQLState.SORT_IMPROPER_SCAN_METHOD);
155: }
156:
157: /**
158: * Fetch the row at the current position of the Scan and does not apply the
159: * qualifiers.
160: *
161: * This method will always throw an exception.
162: * (SQLState.SORT_IMPROPER_SCAN_METHOD)
163: *
164: * @see ScanController#fetchWithoutQualify
165: **/
166: public void fetchWithoutQualify(DataValueDescriptor[] result)
167: throws StandardException {
168: throw StandardException
169: .newException(SQLState.SORT_IMPROPER_SCAN_METHOD);
170: }
171:
172: /**
173: * Fetch the row at the current position of the Scan.
174: *
175: * @see ScanController#fetch
176: **/
177: public void fetch(DataValueDescriptor[] result)
178: throws StandardException {
179: throw StandardException
180: .newException(SQLState.SORT_IMPROPER_SCAN_METHOD);
181: }
182:
183: public final boolean fetchNext(DataValueDescriptor[] row)
184: throws StandardException {
185: throw StandardException
186: .newException(SQLState.SORT_IMPROPER_SCAN_METHOD);
187: }
188:
189: }
|