KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > store > access > sort > SortBufferRowSource


1 /*
2
3    Derby - Class org.apache.derby.impl.store.access.sort.SortBufferRowSource
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.store.access.sort;
23
24 import org.apache.derby.iapi.reference.SQLState;
25
26 import org.apache.derby.iapi.services.sanity.SanityManager;
27 import org.apache.derby.iapi.error.StandardException;
28 import org.apache.derby.iapi.store.access.RowSource;
29 import org.apache.derby.iapi.store.access.SortObserver;
30 import org.apache.derby.iapi.types.RowLocation;
31 import org.apache.derby.iapi.store.access.conglomerate.ScanControllerRowSource;
32 import org.apache.derby.iapi.store.access.conglomerate.TransactionManager;
33 import org.apache.derby.iapi.store.access.ScanController;
34
35 import org.apache.derby.iapi.types.DataValueDescriptor;
36
37 import org.apache.derby.iapi.services.io.FormatableBitSet;
38
39 /**
40     Wrapping the output of a SortBuffer in a RowSource for the benefit of the
41     createAndLoadConglomerate and loadConglomerate interface.
42
43     Scan implements ScanController, this class just implements the
44     RowSource interface.
45
46 */

47 public class SortBufferRowSource extends Scan
48         implements ScanControllerRowSource
49 {
50     /**
51     The Sort buffer where rows come from
52     **/

53     SortBuffer sortBuffer = null;
54
55     /**
56     The TransactionManager that opened this scan.
57     **/

58     protected TransactionManager tran = null;
59
60     private int maxFreeListSize;
61     private boolean writingToDisk;
62     private SortObserver sortObserver;
63
64     /*
65      * Constructors.
66      */

67
68     SortBufferRowSource(
69     SortBuffer sortBuffer,
70     TransactionManager tran,
71     SortObserver sortObserver,
72     boolean writingToDisk,
73     int maxFreeListSize)
74     {
75         super();
76         this.sortBuffer = sortBuffer;
77         this.tran = tran;
78         this.sortObserver = sortObserver;
79         this.writingToDisk = writingToDisk;
80         this.maxFreeListSize = maxFreeListSize;
81     }
82
83     /* Private/Protected methods of This class: */
84     /* Public Methods of This class: */
85     /* Public Methods of RowSource class: */
86
87     public DataValueDescriptor[] getNextRowFromRowSource()
88     {
89         if (sortBuffer == null) // has been closed
90
return null;
91
92         DataValueDescriptor[] retval = sortBuffer.removeFirst();
93
94         // Return the removed object to the free DataValueDescriptor[]
95
if (retval != null && writingToDisk)
96         {
97             sortObserver.addToFreeList(retval, maxFreeListSize);
98         }
99         return retval;
100       }
101
102     public boolean needsRowLocation()
103     {
104         return false;
105     }
106
107     /**
108      * @see RowSource#needsToClone
109      */

110     public boolean needsToClone()
111     {
112         return false;
113     }
114
115     public void rowLocation(RowLocation rl)
116     {
117         if (SanityManager.DEBUG)
118             SanityManager.THROWASSERT("unexpected call to RowSource.rowLocation");
119     }
120
121
122     /**
123         All columns are always set from a sorter
124     */

125     public FormatableBitSet getValidColumns()
126     {
127         return null;
128     }
129
130     /**
131         Close the scan
132      */

133     public void close()
134     {
135         if (sortBuffer != null)
136         {
137             sortBuffer.close();
138             sortBuffer = null;
139         }
140         tran.closeMe(this);
141     }
142
143     /**
144         Close the scan
145      */

146     public boolean closeForEndTransaction(boolean closeHeldScan)
147     {
148         if (SanityManager.DEBUG)
149             SanityManager.ASSERT(
150                 closeHeldScan,
151                 "Sort scan should not be held open across commit.");
152
153         close();
154         return(true);
155     }
156
157     /**
158         Close the rowSource
159      */

160     public void closeRowSource()
161     {
162         close();
163     }
164
165     /*
166      * Disable illegal and dangerous scan controller interface call
167      */

168     public boolean next() throws StandardException
169     {
170         throw StandardException.newException(
171                 SQLState.SORT_IMPROPER_SCAN_METHOD);
172     }
173
174     /**
175      * Fetch the row at the current position of the Scan and does not apply the
176      * qualifiers.
177      *
178      * This method will always throw an exception.
179      * (SQLState.SORT_IMPROPER_SCAN_METHOD)
180      *
181      * @see ScanController#fetchWithoutQualify
182      **/

183     public void fetchWithoutQualify(DataValueDescriptor[] result)
184         throws StandardException
185     {
186         throw StandardException.newException(
187                 SQLState.SORT_IMPROPER_SCAN_METHOD);
188     }
189
190     /**
191      * Fetch the row at the current position of the Scan.
192      *
193      * @see ScanController#fetch
194      **/

195     public void fetch(DataValueDescriptor[] result) throws StandardException
196     {
197         throw StandardException.newException(
198                 SQLState.SORT_IMPROPER_SCAN_METHOD);
199     }
200
201     public final boolean fetchNext(DataValueDescriptor[] row)
202         throws StandardException
203     {
204         throw StandardException.newException(
205                 SQLState.SORT_IMPROPER_SCAN_METHOD);
206     }
207
208 }
209
Popular Tags