KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > unitTests > store > T_RowSource


1 /*
2
3    Derby - Class org.apache.derbyTesting.unitTests.store.T_RowSource
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.derbyTesting.unitTests.store;
23
24 import org.apache.derby.iapi.store.access.*;
25
26 import org.apache.derby.iapi.types.SQLInteger;
27
28 import org.apache.derby.iapi.services.io.FormatableBitSet;
29
30 import org.apache.derby.iapi.error.StandardException;
31
32 import org.apache.derby.iapi.store.raw.Transaction;
33
34 import org.apache.derby.iapi.types.DataValueDescriptor;
35
36 import org.apache.derby.iapi.types.RowLocation;
37
38
39 /**
40   A RowSource is the mechanism for iterating over a set of rows. The RowSource
41   is the interface through which access recieved a set of rows from the client
42   for the purpose of inserting into a single container.
43
44   <p>
45   A RowSource can come from many sources - from rows that are from fast path
46   import, to rows coming out of a sort for index creation.
47
48   @see org.apache.derby.iapi.store.access.RowSource
49 */

50 public class T_RowSource implements RowSource {
51
52     static public final int INTEGER_ROW_TYPE = 1;
53     static public final int STRING_ROW_TYPE = 2;
54
55     static protected final String JavaDoc REC_001 = "McLaren";
56     static protected final String JavaDoc REC_002 = "Ferrari";
57     static protected final String JavaDoc REC_003 = "Benetton";
58     static protected final String JavaDoc REC_004 = "Prost";
59     static protected final String JavaDoc REC_005 = "Tyrell";
60     static protected final String JavaDoc REC_006 = "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
61     static protected final String JavaDoc REC_007 = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
62     static protected final String JavaDoc REC_008 = "z";
63
64     static protected final int DEFAULT_ROW_COUNT = 500000;
65     static protected final int DEFAULT_COLUMN_COUNT = 13;
66     static protected final int DEFAULT_SEED = 53; // some random number
67

68     private int rowCount;
69     private int columnCount;
70     private DataValueDescriptor row[];
71     private FormatableBitSet validColumns;
72     private boolean forceAbort;
73     private Transaction t;
74
75     /*
76      * constructor
77      */

78     public T_RowSource() {
79
80         // default will create DEFAULT_ROW_COUNT rows,
81
// of DEFAULT_COLUMN_COUNT columns string type rows
82
// validColumns will be set to null.
83
this.rowCount = DEFAULT_ROW_COUNT;
84         this.columnCount = DEFAULT_COLUMN_COUNT;
85         this.row = new DataValueDescriptor[DEFAULT_COLUMN_COUNT];
86         row = setStringRow();
87     }
88
89     // if the caller does not pass in a validColumn, we will set it here
90
public T_RowSource(int count, int columnCount, int rowType, boolean forceAbort, Transaction t) {
91
92         this.rowCount = count;
93         this.columnCount = columnCount;
94         validColumns = new FormatableBitSet(columnCount);
95         for (int i = 0; i < columnCount; i++)
96             validColumns.set(i);
97
98         this.row = new DataValueDescriptor[columnCount];
99         if (rowType == INTEGER_ROW_TYPE)
100             setIntegerRow();
101         else
102             row = setStringRow();
103
104         this.forceAbort = forceAbort;
105         this.t = t;
106     }
107
108     // the caller has a chance to set the valisColumns to anything they want.
109
public T_RowSource(int count, int columnCount, int rowType, FormatableBitSet validColumns) {
110
111         this.rowCount = count;
112         this.columnCount = columnCount;
113         this.validColumns = validColumns;
114
115         this.row = new DataValueDescriptor[columnCount];
116         if (rowType == INTEGER_ROW_TYPE)
117             setIntegerRow();
118         else
119             row = setStringRow();
120     }
121
122     /*
123      * methods for RowSource
124      */

125
126     /**
127         @return true if more rows are coming, false if there is no more rows
128         in the RowSource
129      * @exception StandardException Thrown on error
130      */

131     public boolean hasMoreRows() throws StandardException {
132         if (rowCount > 0)
133             return true;
134         else
135             return false;
136     }
137
138     /**
139         Get the next row as an array of column objects. The column objects can
140         be a JBMS Storable or any
141         Serializable/Externalizable/Formattable/Streaming type.
142
143         @exception StandardException Derby Standard Error Policy
144      */

145     public DataValueDescriptor[] getNextRowFromRowSource()
146         throws StandardException {
147
148         if (this.rowCount <= 0)
149             return null;
150
151         // if we are testing error condition, force an abort now
152
if (forceAbort && (this.rowCount < 3))
153             t.abort();
154
155         this.rowCount--;
156         return row;
157     }
158
159     /**
160       getValidColumns describes the DataValueDescriptor[] returned by all calls
161       to the getNextRowFromRowSource() call.
162     */

163     public FormatableBitSet getValidColumns() {
164         return validColumns;
165     }
166
167     /**
168         closeRowSource tells the RowSource that it will no longer need to
169         return any rows and it can release any resource it may have.
170         Subsequent call to any method on the RowSource will result in undefined
171         behavior. A closed rowSource can be closed again.
172     */

173     public void closeRowSource() {
174
175         this.rowCount = 0;
176     }
177
178
179     /**
180         needsRowLocation returns true iff this the row source expects the
181         drainer of the row source to call rowLocation after getting a row from
182         getNextRowFromRowSource.
183
184         @return true iff this row source expects some row location to be
185         returned
186         @see #rowLocation
187      */

188     public boolean needsRowLocation() {
189         return false;
190     }
191
192     /**
193      * @see RowSource#needsToClone
194      */

195     public boolean needsToClone()
196     {
197         return true;
198     }
199
200     /**
201         rowLocation is not implemented here
202      */

203     public void rowLocation(RowLocation rl) {
204
205         rl = null;
206     }
207
208     /**
209         Get a copy of the template row. Cast each column to
210         a CloneableObject and clone it.
211
212         @exception StandardException Derby Standard Error Policy
213     **/

214     public DataValueDescriptor[] getTemplate() throws StandardException {
215
216         return row;
217
218     }
219
220     // set all column of the row to integer object
221
private void setIntegerRow() {
222         for (int i = 0; i < columnCount; i++)
223             this.row[i] = new SQLInteger(i + DEFAULT_SEED);
224     }
225
226     private DataValueDescriptor[] setStringRow() {
227
228         T_RawStoreRow row = new T_RawStoreRow(columnCount);
229
230         for (int i = 0; i < columnCount; i++) {
231             switch (i % 13) {
232             case 0:
233                 row.setColumn(i, (String JavaDoc) null);
234                 break;
235             case 1:
236                 row.setColumn(i, REC_001);
237                 break;
238             case 2:
239                 row.setColumn(i, REC_002);
240                 break;
241             case 3:
242                 row.setColumn(i, REC_003);
243                 break;
244             case 4:
245                 row.setColumn(i, REC_004);
246                 break;
247             case 5:
248                 row.setColumn(i, REC_005);
249                 break;
250             case 6:
251                 row.setColumn(i, REC_006);
252                 break;
253             case 7:
254                 row.setColumn(i, REC_007);
255                 break;
256             case 8:
257                 row.setColumn(i, (String JavaDoc) null);
258                 break;
259             case 9:
260                 row.setColumn(i, REC_008);
261                 break;
262             case 10:
263                 row.setColumn(i, REC_007);
264                 break;
265             case 11:
266                 row.setColumn(i, (String JavaDoc) null);
267                 break;
268             case 12:
269                 row.setColumn(i, REC_006);
270                 break;
271             default:
272                 row.setColumn(i, REC_008);
273             }
274         }
275         return row.getRow();
276     }
277 }
278
Popular Tags