KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > execute > JoinResultSet


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.execute.JoinResultSet
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.sql.execute;
23
24 import org.apache.derby.iapi.error.StandardException;
25
26 import org.apache.derby.iapi.sql.execute.CursorResultSet;
27 import org.apache.derby.iapi.sql.execute.ExecRow;
28 import org.apache.derby.iapi.sql.execute.NoPutResultSet;
29
30 import org.apache.derby.iapi.sql.Activation;
31 import org.apache.derby.iapi.sql.ResultSet;
32
33 import org.apache.derby.iapi.services.sanity.SanityManager;
34
35 import org.apache.derby.iapi.services.loader.GeneratedMethod;
36
37 import org.apache.derby.iapi.types.RowLocation;
38
39 /**
40  * Takes 2 NoPutResultSets and a join filter and returns
41  * the join's rows satisfying the filter as a result set.
42  *
43  * @author ames
44  */

45 abstract class JoinResultSet extends NoPutResultSetImpl
46     implements CursorResultSet
47 {
48     /* Run time statistics variables */
49     public int rowsSeenLeft;
50     public int rowsSeenRight;
51     public int rowsReturned;
52     public long restrictionTime;
53
54     protected boolean isRightOpen;
55     protected ExecRow leftRow;
56     protected ExecRow rightRow;
57     protected ExecRow mergedRow;
58
59     // set in constructor and not altered during
60
// life of object.
61
public NoPutResultSet leftResultSet;
62     protected int leftNumCols;
63     public NoPutResultSet rightResultSet;
64     protected int rightNumCols;
65     protected GeneratedMethod restriction;
66     public boolean oneRowRightSide;
67     public boolean notExistsRightSide; //right side is NOT EXISTS
68

69     String JavaDoc userSuppliedOptimizerOverrides;
70
71     /*
72      * class interface
73      *
74      */

75     JoinResultSet(NoPutResultSet leftResultSet,
76                                    int leftNumCols,
77                                    NoPutResultSet rightResultSet,
78                                    int rightNumCols,
79                                    Activation activation,
80                                    GeneratedMethod restriction,
81                                    int resultSetNumber,
82                                    boolean oneRowRightSide,
83                                    boolean notExistsRightSide,
84                                    double optimizerEstimatedRowCount,
85                                    double optimizerEstimatedCost,
86                                    String JavaDoc userSuppliedOptimizerOverrides)
87     {
88         super(activation, resultSetNumber, optimizerEstimatedRowCount,
89               optimizerEstimatedCost);
90         this.leftResultSet = leftResultSet;
91         this.leftNumCols = leftNumCols;
92         this.rightResultSet = rightResultSet;
93         this.rightNumCols = rightNumCols;
94         this.restriction = restriction;
95         this.oneRowRightSide = oneRowRightSide;
96         this.notExistsRightSide = notExistsRightSide;
97         constructorTime += getElapsedMillis(beginTime);
98         this.userSuppliedOptimizerOverrides = userSuppliedOptimizerOverrides;
99     }
100
101     //
102
// ResultSet interface (leftover from NoPutResultSet)
103
//
104

105     /**
106      * Clear any private state that changes during scans.
107      * This includes things like the last row seen, etc.
108      * THis does not include immutable things that are
109      * typically set up in the constructor.
110      * <p>
111      * This method is called on open()/close() and reopen()
112      */

113     void clearScanState()
114     {
115         leftRow = null;
116         rightRow = null;
117         mergedRow = null;
118     }
119
120     /**
121      * open a scan on the join.
122      * For a join, this means:
123      * o Open the left ResultSet
124      * o Do a getNextRow() on the left ResultSet to establish a position
125      * and get "parameter values" for the right ResultSet.
126      * NOTE: It is possible for the getNextRow() to return null, in which
127      * case there is no need to open the RightResultSet. We must remember
128      * this condition.
129      * o If the getNextRow() on the left ResultSet succeeded, then open()
130      * the right ResultSet.
131      *
132      * scan parameters are evaluated at each open, so there is probably
133      * some way of altering their values...
134      *
135      * @exception StandardException Thrown on error
136      */

137     public void openCore() throws StandardException
138     {
139         clearScanState();
140
141         beginTime = getCurrentTimeMillis();
142         if (SanityManager.DEBUG)
143             SanityManager.ASSERT( ! isOpen, "JoinResultSet already open");
144
145         isOpen = true;
146         leftResultSet.openCore();
147         leftRow = leftResultSet.getNextRowCore();
148         if (leftRow != null)
149         {
150             openRight();
151             rowsSeenLeft++;
152         }
153         numOpens++;
154
155         openTime += getElapsedMillis(beginTime);
156     }
157
158     /**
159      * reopen a a join.
160      *
161      * @exception StandardException thrown if cursor finished.
162      */

163     public void reopenCore() throws StandardException
164     {
165         clearScanState();
166
167         // Reopen the left and get the next row
168
leftResultSet.reopenCore();
169         leftRow = leftResultSet.getNextRowCore();
170         if (leftRow != null)
171         {
172             // Open the right
173
openRight();
174             rowsSeenLeft++;
175         }
176         else if (isRightOpen)
177         {
178             closeRight();
179         }
180
181         numOpens++;
182
183         openTime += getElapsedMillis(beginTime);
184     }
185
186
187     /**
188      * If the result set has been opened,
189      * close the open scan.
190      * <n>
191      * <B>WARNING</B> does not track close
192      * time, since it is expected to be called
193      * directly by its subclasses, and we don't
194      * want to skew the times
195      *
196      * @exception StandardException thrown on error
197      */

198     public void close() throws StandardException
199     {
200         clearScanState();
201
202         if ( isOpen )
203         {
204             leftResultSet.close();
205             if (isRightOpen)
206             {
207                 closeRight();
208             }
209
210             super.close();
211         }
212         else
213             if (SanityManager.DEBUG)
214                 SanityManager.DEBUG("CloseRepeatInfo","Close of JoinResultSet repeated");
215
216     }
217
218     public void finish() throws StandardException {
219         leftResultSet.finish();
220         rightResultSet.finish();
221         super.finish();
222     }
223
224     //
225
// CursorResultSet interface
226
//
227
/**
228      * A join is combining rows from two sources, so it has no
229      * single row location to return; just return a null.
230      *
231      * @see CursorResultSet
232      *
233      * @return the row location of the current cursor row.
234      */

235     public RowLocation getRowLocation() {
236         if (SanityManager.DEBUG)
237             SanityManager.THROWASSERT("Join used in positioned update/delete");
238         return null;
239     }
240
241     /**
242      * A join is combining rows from two sources, so it
243      * should never be used in a positioned update or delete.
244      *
245      * @see CursorResultSet
246      *
247      * @return a null value.
248      */

249     public ExecRow getCurrentRow() {
250         if (SanityManager.DEBUG)
251             SanityManager.THROWASSERT("Join used in positioned update/delete");
252         return null;
253     }
254
255     /* Class implementation */
256
257     /**
258      * open the rightResultSet. If already open,
259      * just reopen.
260      *
261      * @exception StandardException Thrown on error
262      */

263     protected void openRight() throws StandardException
264     {
265         if (isRightOpen)
266         {
267             rightResultSet.reopenCore();
268         }
269         else
270         {
271             rightResultSet.openCore();
272             isRightOpen = true;
273         }
274     }
275
276     /**
277      * close the rightResultSet
278      *
279      */

280     protected void closeRight() throws StandardException
281     {
282         if (SanityManager.DEBUG)
283             SanityManager.ASSERT(isRightOpen, "isRightOpen is expected to be true");
284         rightResultSet.close();
285         isRightOpen = false;
286     }
287
288 }
289
Popular Tags