KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.execute.BasicSortObserver
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.store.access.SortObserver;
25 import org.apache.derby.iapi.services.io.Storable;
26
27 import org.apache.derby.iapi.types.CloneableObject;
28
29 import org.apache.derby.iapi.services.sanity.SanityManager;
30
31 import org.apache.derby.iapi.error.StandardException;
32
33 import org.apache.derby.iapi.sql.execute.ExecRow;
34
35 import org.apache.derby.iapi.types.DataValueDescriptor;
36
37 import java.util.Vector JavaDoc;
38
39 /**
40  * This is the most basic sort observer. It
41  * handles distinct sorts and non-distinct sorts.
42  *
43  * @author jamie
44  */

45 public class BasicSortObserver implements SortObserver
46 {
47     protected boolean doClone;
48     protected boolean distinct;
49     private boolean reuseWrappers;
50     private ExecRow execRow;
51     private Vector JavaDoc vector;
52
53     /**
54      * Simple constructor
55      *
56      * @param doClone If true, then rows that are retained
57      * by the sorter will be cloned. This is needed
58      * if language is reusing row wrappers.
59      *
60      * @param distinct If true, toss out duplicates.
61      * Otherwise, retain them.
62      *
63      * @param execRow ExecRow to use as source of clone for store.
64      *
65      * @param reuseWrappers Whether or not we can reuse the wrappers
66      */

67     public BasicSortObserver(boolean doClone, boolean distinct, ExecRow execRow, boolean reuseWrappers)
68     {
69         this.doClone = doClone;
70         this.distinct = distinct;
71         this.execRow = execRow;
72         this.reuseWrappers = reuseWrappers;
73         vector = new Vector JavaDoc();
74     }
75
76     /**
77      * Called prior to inserting a distinct sort
78      * key.
79      *
80      * @param insertRow the current row that the sorter
81      * is on the verge of retaining
82      *
83      * @return the row to be inserted by the sorter. If null,
84      * then nothing is inserted by the sorter. Distinct
85      * sorts will want to return null.
86      *
87      * @exception StandardException never thrown
88      */

89     public DataValueDescriptor[] insertNonDuplicateKey(DataValueDescriptor[] insertRow)
90         throws StandardException
91     {
92         return (doClone) ?
93                     getClone(insertRow) :
94                     insertRow;
95     }
96     /**
97      * Called prior to inserting a duplicate sort
98      * key.
99      *
100      * @param insertRow the current row that the sorter
101      * is on the verge of retaining. It is a duplicate
102      * of existingRow.
103      *
104      * @param existingRow the row that is already in the
105      * the sorter which is a duplicate of insertRow
106      *
107      * @exception StandardException never thrown
108      */

109     public DataValueDescriptor[] insertDuplicateKey(DataValueDescriptor[] insertRow, DataValueDescriptor[] existingRow)
110             throws StandardException
111     {
112         return (distinct) ?
113                     (DataValueDescriptor[])null :
114                         (doClone) ?
115                             getClone(insertRow) :
116                             insertRow;
117
118     }
119
120     public void addToFreeList(DataValueDescriptor[] objectArray, int maxFreeListSize)
121     {
122         if (reuseWrappers && vector.size() < maxFreeListSize)
123         {
124             vector.addElement(objectArray);
125         }
126     }
127
128     public DataValueDescriptor[] getArrayClone()
129         throws StandardException
130     {
131         int lastElement = vector.size();
132
133         if (lastElement > 0)
134         {
135             DataValueDescriptor[] retval = (DataValueDescriptor[]) vector.elementAt(lastElement - 1);
136             vector.removeElementAt(lastElement - 1);
137             return retval;
138         }
139         return execRow.getRowArrayClone();
140     }
141
142
143     private DataValueDescriptor[] getClone(DataValueDescriptor[] origArray)
144     {
145         /* If the free list is not empty, then
146          * get an DataValueDescriptor[] from there and swap
147          * objects between that DataValueDescriptor[] and
148          * origArray, returning the DataValueDescriptor[]
149          * from the free list. That will save
150          * on unnecessary cloning.
151          */

152 /* RESOLVE - We can't enable this code
153  * until Bug 2829 is fixed.
154  * (Close bug 2828 when enabling the code.
155         if (vector.size() > 0)
156         {
157             DataValueDescriptor[] retval = getArrayClone();
158             for (int index = 0; index < retval.length; index++)
159             {
160                 DataValueDescriptor tmp = origArray[index];
161                 origArray[index] = retval[index];
162                 retval[index] = tmp;
163             }
164             return retval;
165         }
166 */

167         DataValueDescriptor[] newArray = new DataValueDescriptor[origArray.length];
168         for (int i = 0; i < origArray.length; i++)
169         {
170             // the only difference between getClone and cloneObject is cloneObject does
171
// not objectify a stream. We use getClone here. Beetle 4896.
172
newArray[i] = origArray[i].getClone();
173         }
174
175         return newArray;
176     }
177 }
178
Popular Tags