001: /*
002:
003: Derby - Class org.apache.derbyTesting.unitTests.store.T_RowSource
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.unitTests.store;
023:
024: import org.apache.derby.iapi.store.access.*;
025:
026: import org.apache.derby.iapi.types.SQLInteger;
027:
028: import org.apache.derby.iapi.services.io.FormatableBitSet;
029:
030: import org.apache.derby.iapi.error.StandardException;
031:
032: import org.apache.derby.iapi.store.raw.Transaction;
033:
034: import org.apache.derby.iapi.types.DataValueDescriptor;
035:
036: import org.apache.derby.iapi.types.RowLocation;
037:
038: /**
039: A RowSource is the mechanism for iterating over a set of rows. The RowSource
040: is the interface through which access recieved a set of rows from the client
041: for the purpose of inserting into a single container.
042:
043: <p>
044: A RowSource can come from many sources - from rows that are from fast path
045: import, to rows coming out of a sort for index creation.
046:
047: @see org.apache.derby.iapi.store.access.RowSource
048: */
049: public class T_RowSource implements RowSource {
050:
051: static public final int INTEGER_ROW_TYPE = 1;
052: static public final int STRING_ROW_TYPE = 2;
053:
054: static protected final String REC_001 = "McLaren";
055: static protected final String REC_002 = "Ferrari";
056: static protected final String REC_003 = "Benetton";
057: static protected final String REC_004 = "Prost";
058: static protected final String REC_005 = "Tyrell";
059: static protected final String REC_006 = "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
060: static protected final String REC_007 = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
061: static protected final String REC_008 = "z";
062:
063: static protected final int DEFAULT_ROW_COUNT = 500000;
064: static protected final int DEFAULT_COLUMN_COUNT = 13;
065: static protected final int DEFAULT_SEED = 53; // some random number
066:
067: private int rowCount;
068: private int columnCount;
069: private DataValueDescriptor row[];
070: private FormatableBitSet validColumns;
071: private boolean forceAbort;
072: private Transaction t;
073:
074: /*
075: * constructor
076: */
077: public T_RowSource() {
078:
079: // default will create DEFAULT_ROW_COUNT rows,
080: // of DEFAULT_COLUMN_COUNT columns string type rows
081: // validColumns will be set to null.
082: this .rowCount = DEFAULT_ROW_COUNT;
083: this .columnCount = DEFAULT_COLUMN_COUNT;
084: this .row = new DataValueDescriptor[DEFAULT_COLUMN_COUNT];
085: row = setStringRow();
086: }
087:
088: // if the caller does not pass in a validColumn, we will set it here
089: public T_RowSource(int count, int columnCount, int rowType,
090: boolean forceAbort, Transaction t) {
091:
092: this .rowCount = count;
093: this .columnCount = columnCount;
094: validColumns = new FormatableBitSet(columnCount);
095: for (int i = 0; i < columnCount; i++)
096: validColumns.set(i);
097:
098: this .row = new DataValueDescriptor[columnCount];
099: 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,
110: FormatableBitSet validColumns) {
111:
112: this .rowCount = count;
113: this .columnCount = columnCount;
114: this .validColumns = validColumns;
115:
116: this .row = new DataValueDescriptor[columnCount];
117: if (rowType == INTEGER_ROW_TYPE)
118: setIntegerRow();
119: else
120: row = setStringRow();
121: }
122:
123: /*
124: * methods for RowSource
125: */
126:
127: /**
128: @return true if more rows are coming, false if there is no more rows
129: in the RowSource
130: * @exception StandardException Thrown on error
131: */
132: public boolean hasMoreRows() throws StandardException {
133: if (rowCount > 0)
134: return true;
135: else
136: return false;
137: }
138:
139: /**
140: Get the next row as an array of column objects. The column objects can
141: be a JBMS Storable or any
142: Serializable/Externalizable/Formattable/Streaming type.
143:
144: @exception StandardException Derby Standard Error Policy
145: */
146: public DataValueDescriptor[] getNextRowFromRowSource()
147: throws StandardException {
148:
149: if (this .rowCount <= 0)
150: return null;
151:
152: // if we are testing error condition, force an abort now
153: if (forceAbort && (this .rowCount < 3))
154: t.abort();
155:
156: this .rowCount--;
157: return row;
158: }
159:
160: /**
161: getValidColumns describes the DataValueDescriptor[] returned by all calls
162: to the getNextRowFromRowSource() call.
163: */
164: public FormatableBitSet getValidColumns() {
165: return validColumns;
166: }
167:
168: /**
169: closeRowSource tells the RowSource that it will no longer need to
170: return any rows and it can release any resource it may have.
171: Subsequent call to any method on the RowSource will result in undefined
172: behavior. A closed rowSource can be closed again.
173: */
174: public void closeRowSource() {
175:
176: this .rowCount = 0;
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: return true;
197: }
198:
199: /**
200: rowLocation is not implemented here
201: */
202: public void rowLocation(RowLocation rl) {
203:
204: rl = null;
205: }
206:
207: /**
208: Get a copy of the template row. Cast each column to
209: a CloneableObject and clone it.
210:
211: @exception StandardException Derby Standard Error Policy
212: **/
213: public DataValueDescriptor[] getTemplate() throws StandardException {
214:
215: return row;
216:
217: }
218:
219: // set all column of the row to integer object
220: private void setIntegerRow() {
221: for (int i = 0; i < columnCount; i++)
222: this .row[i] = new SQLInteger(i + DEFAULT_SEED);
223: }
224:
225: private DataValueDescriptor[] setStringRow() {
226:
227: T_RawStoreRow row = new T_RawStoreRow(columnCount);
228:
229: for (int i = 0; i < columnCount; i++) {
230: switch (i % 13) {
231: case 0:
232: row.setColumn(i, (String) null);
233: break;
234: case 1:
235: row.setColumn(i, REC_001);
236: break;
237: case 2:
238: row.setColumn(i, REC_002);
239: break;
240: case 3:
241: row.setColumn(i, REC_003);
242: break;
243: case 4:
244: row.setColumn(i, REC_004);
245: break;
246: case 5:
247: row.setColumn(i, REC_005);
248: break;
249: case 6:
250: row.setColumn(i, REC_006);
251: break;
252: case 7:
253: row.setColumn(i, REC_007);
254: break;
255: case 8:
256: row.setColumn(i, (String) null);
257: break;
258: case 9:
259: row.setColumn(i, REC_008);
260: break;
261: case 10:
262: row.setColumn(i, REC_007);
263: break;
264: case 11:
265: row.setColumn(i, (String) null);
266: break;
267: case 12:
268: row.setColumn(i, REC_006);
269: break;
270: default:
271: row.setColumn(i, REC_008);
272: }
273: }
274: return row.getRow();
275: }
276: }
|