001: /*
002:
003: Derby - Class org.apache.derby.impl.store.access.sort.SortScan
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.io.FormatableBitSet;
027:
028: import org.apache.derby.iapi.services.sanity.SanityManager;
029: import org.apache.derby.iapi.services.io.Storable;
030:
031: import org.apache.derby.iapi.error.StandardException;
032:
033: import org.apache.derby.iapi.store.access.conglomerate.TransactionManager;
034: import org.apache.derby.iapi.store.access.RowUtil;
035: import org.apache.derby.iapi.store.access.ScanController;
036:
037: import org.apache.derby.iapi.types.DataValueDescriptor;
038:
039: /**
040:
041: Abstract base class for merge sort scans.
042:
043: **/
044:
045: public abstract class SortScan extends Scan {
046:
047: /**
048: The sort that this class is scanning.
049: **/
050: protected MergeSort sort = null;
051:
052: /**
053: The transactionManager that opened this scan.
054: **/
055: protected TransactionManager tran = null;
056:
057: /**
058: The row at the current position of the scan, from which
059: fetch will return values.
060: **/
061: protected DataValueDescriptor[] current;
062:
063: /**
064: The row at the current position of the scan, from which
065: fetch will return values.
066: **/
067: protected boolean hold;
068:
069: /*
070: * Constructors
071: */
072: SortScan(MergeSort sort, TransactionManager tran, boolean hold) {
073: super ();
074: this .sort = sort;
075: this .tran = tran;
076: this .hold = hold;
077: }
078:
079: /*
080: * Abstract methods of Scan
081: */
082:
083: /**
084: Fetch the row at the next position of the Scan.
085:
086: If there is a valid next position in the scan then
087: the value in the template storable row is replaced
088: with the value of the row at the current scan
089: position. The columns of the template row must
090: be of the same type as the actual columns in the
091: underlying conglomerate.
092:
093: The resulting contents of templateRow after a fetchNext()
094: which returns false is undefined.
095:
096: The result of calling fetchNext(row) is exactly logically
097: equivalent to making a next() call followed by a fetch(row)
098: call. This interface allows implementations to optimize
099: the 2 calls if possible.
100:
101: RESOLVE (mikem - 2/24/98) - come back to this and see if
102: coding this differently saves in sort scans, as did the
103: heap recoding.
104:
105: @param row The template row into which the value
106: of the next position in the scan is to be stored.
107:
108: @return True if there is a next position in the scan,
109: false if there isn't.
110:
111: @exception StandardException Standard exception policy.
112: **/
113: public final boolean fetchNext(DataValueDescriptor[] row)
114: throws StandardException {
115: boolean ret_val = next();
116:
117: if (ret_val)
118: fetch(row);
119:
120: return (ret_val);
121: }
122:
123: /**
124: Fetch the row at the current position of the Scan.
125: @see ScanController#fetch
126: **/
127: public final void fetch(DataValueDescriptor[] result)
128: throws StandardException {
129: if (SanityManager.DEBUG) {
130: SanityManager.ASSERT(sort != null);
131: }
132:
133: if (current == null) {
134: throw StandardException
135: .newException(SQLState.SORT_SCAN_NOT_POSITIONED);
136: }
137:
138: // Make sure the passed in template row is of the correct type.
139: sort.checkColumnTypes(result);
140:
141: // RESOLVE
142: // Note that fetch() basically throws away the object's passed in.
143: // We should figure out how to allow callers in this situation to
144: // not go through the work of allocating objects in the first place.
145:
146: // Sort has allocated objects for this row, and will not
147: // reference them any more. So just pass the objects out
148: // to the caller instead of copying them into the provided
149: // objects.
150: System.arraycopy(current, 0, result, 0, result.length);
151: }
152:
153: /**
154: Fetch the row at the current position of the Scan and does not apply the
155: qualifiers.
156:
157: This method will always throw an exception.
158: (SQLState.SORT_IMPROPER_SCAN_METHOD)
159:
160: @see ScanController#fetchWithoutQualify
161: **/
162: public final void fetchWithoutQualify(DataValueDescriptor[] result)
163: throws StandardException {
164: throw StandardException
165: .newException(SQLState.SORT_IMPROPER_SCAN_METHOD);
166:
167: }
168:
169: /**
170: Close the scan. @see ScanController#close
171: **/
172: public void close() {
173: sort = null;
174: current = null;
175:
176: tran.closeMe(this );
177: }
178:
179: /*
180: * Methods of SortScan. Arranged alphabetically.
181: */
182: }
|