1 | /* |
2 | |
3 | Derby - Class org.apache.derby.impl.store.access.sort.SortScan |
4 | |
5 | Copyright 1997, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.impl.store.access.sort; |
22 | |
23 | import org.apache.derby.iapi.reference.SQLState; |
24 | |
25 | import org.apache.derby.iapi.services.io.FormatableBitSet; |
26 | |
27 | import org.apache.derby.iapi.services.sanity.SanityManager; |
28 | import org.apache.derby.iapi.services.io.Storable; |
29 | |
30 | import org.apache.derby.iapi.error.StandardException; |
31 | |
32 | import org.apache.derby.iapi.store.access.conglomerate.TransactionManager; |
33 | import org.apache.derby.iapi.store.access.RowUtil; |
34 | import org.apache.derby.iapi.store.access.ScanController; |
35 | |
36 | import org.apache.derby.iapi.types.DataValueDescriptor; |
37 | |
38 | |
39 | /** |
40 | |
41 | Abstract base class for merge sort scans. |
42 | |
43 | **/ |
44 | |
45 | public abstract class SortScan extends Scan |
46 | { |
47 | |
48 | /** |
49 | The sort that this class is scanning. |
50 | **/ |
51 | protected MergeSort sort = null; |
52 | |
53 | /** |
54 | The transactionManager that opened this scan. |
55 | **/ |
56 | protected TransactionManager tran = null; |
57 | |
58 | /** |
59 | The row at the current position of the scan, from which |
60 | fetch will return values. |
61 | **/ |
62 | protected DataValueDescriptor[] current; |
63 | |
64 | /** |
65 | The row at the current position of the scan, from which |
66 | fetch will return values. |
67 | **/ |
68 | protected boolean hold; |
69 | |
70 | /* |
71 | * Constructors |
72 | */ |
73 | SortScan(MergeSort sort, TransactionManager tran, boolean hold) |
74 | { |
75 | super(); |
76 | this.sort = sort; |
77 | this.tran = tran; |
78 | this.hold = hold; |
79 | } |
80 | |
81 | /* |
82 | * Abstract methods of Scan |
83 | */ |
84 | |
85 | /** |
86 | Fetch the row at the next position of the Scan. |
87 | |
88 | If there is a valid next position in the scan then |
89 | the value in the template storable row is replaced |
90 | with the value of the row at the current scan |
91 | position. The columns of the template row must |
92 | be of the same type as the actual columns in the |
93 | underlying conglomerate. |
94 | |
95 | The resulting contents of templateRow after a fetchNext() |
96 | which returns false is undefined. |
97 | |
98 | The result of calling fetchNext(row) is exactly logically |
99 | equivalent to making a next() call followed by a fetch(row) |
100 | call. This interface allows implementations to optimize |
101 | the 2 calls if possible. |
102 | |
103 | RESOLVE (mikem - 2/24/98) - come back to this and see if |
104 | coding this differently saves in sort scans, as did the |
105 | heap recoding. |
106 | |
107 | @param row The template row into which the value |
108 | of the next position in the scan is to be stored. |
109 | |
110 | @return True if there is a next position in the scan, |
111 | false if there isn't. |
112 | |
113 | @exception StandardException Standard exception policy. |
114 | **/ |
115 | public final boolean fetchNext(DataValueDescriptor[] row) |
116 | throws StandardException |
117 | { |
118 | boolean ret_val = next(); |
119 | |
120 | if (ret_val) |
121 | fetch(row); |
122 | |
123 | return(ret_val); |
124 | } |
125 | |
126 | /** |
127 | Fetch the row at the current position of the Scan. |
128 | @see ScanController#fetch |
129 | **/ |
130 | public final void fetch(DataValueDescriptor[] result) |
131 | throws StandardException |
132 | { |
133 | if (SanityManager.DEBUG) |
134 | { |
135 | SanityManager.ASSERT(sort != null); |
136 | } |
137 | |
138 | if (current == null) |
139 | { |
140 | throw StandardException.newException( |
141 | SQLState.SORT_SCAN_NOT_POSITIONED); |
142 | } |
143 | |
144 | // Make sure the passed in template row is of the correct type. |
145 | sort.checkColumnTypes(result); |
146 | |
147 | // RESOLVE |
148 | // Note that fetch() basically throws away the object's passed in. |
149 | // We should figure out how to allow callers in this situation to |
150 | // not go through the work of allocating objects in the first place. |
151 | |
152 | // Sort has allocated objects for this row, and will not |
153 | // reference them any more. So just pass the objects out |
154 | // to the caller instead of copying them into the provided |
155 | // objects. |
156 | System.arraycopy(current, 0, result, 0, result.length); |
157 | } |
158 | |
159 | /** |
160 | Fetch the row at the current position of the Scan and does not apply the |
161 | qualifiers. |
162 | |
163 | This method will always throw an exception. |
164 | (SQLState.SORT_IMPROPER_SCAN_METHOD) |
165 | |
166 | @see ScanController#fetchWithoutQualify |
167 | **/ |
168 | public final void fetchWithoutQualify(DataValueDescriptor[] result) |
169 | throws StandardException |
170 | { |
171 | throw StandardException.newException( |
172 | SQLState.SORT_IMPROPER_SCAN_METHOD); |
173 | |
174 | } |
175 | |
176 | /** |
177 | Close the scan. @see ScanController#close |
178 | **/ |
179 | public void close() |
180 | { |
181 | sort = null; |
182 | current = null; |
183 | |
184 | tran.closeMe(this); |
185 | } |
186 | |
187 | /* |
188 | * Methods of SortScan. Arranged alphabetically. |
189 | */ |
190 | } |