KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derbyTesting.unitTests.store.T_StreamFile
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.derbyTesting.unitTests.harness.T_MultiThreadedIterations;
25 import org.apache.derbyTesting.unitTests.harness.T_Fail;
26
27 import org.apache.derby.impl.store.raw.data.*;
28
29 import org.apache.derby.iapi.services.context.ContextService;
30 import org.apache.derby.iapi.services.context.ContextManager;
31 import org.apache.derby.iapi.services.locks.*;
32 import org.apache.derby.iapi.services.property.PropertyUtil;
33 import org.apache.derby.iapi.services.monitor.Monitor;
34 import org.apache.derby.iapi.services.sanity.SanityManager;
35 import org.apache.derby.iapi.services.io.Storable;
36 import org.apache.derby.iapi.services.io.FormatIdOutputStream;
37
38 import org.apache.derby.iapi.error.StandardException;
39 import org.apache.derby.iapi.store.raw.*;
40 import org.apache.derby.iapi.store.raw.xact.RawTransaction;
41 import org.apache.derby.iapi.store.raw.data.RawContainerHandle;
42
43 import org.apache.derby.iapi.store.access.conglomerate.LogicalUndo;
44
45 import org.apache.derby.iapi.types.DataValueDescriptor;
46
47 import org.apache.derby.iapi.reference.Property;
48
49 import org.apache.derby.iapi.services.io.FormatableBitSet;
50 import org.apache.derby.iapi.services.io.DynamicByteArrayOutputStream;
51
52 import java.io.*;
53 import java.util.Properties JavaDoc;
54
55 /**
56     An Impl unittest for rawstore data that is based on the stream file
57 */

58
59 public class T_StreamFile extends T_MultiThreadedIterations {
60
61     private static final String JavaDoc testService = "streamFileTest";
62
63     static final String JavaDoc REC_001 = "McLaren";
64     static final String JavaDoc REC_002 = "Ferrari";
65     static final String JavaDoc REC_003 = "Benetton";
66     static final String JavaDoc REC_004 = "Prost";
67     static final String JavaDoc REC_005 = "Tyrell";
68     static final String JavaDoc REC_006 = "Derby, Natscape, Goatscape, the popular names";
69     static final String JavaDoc REC_007 = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
70
71     static final String JavaDoc SP1 = "savepoint1";
72     static final String JavaDoc SP2 = "savepoint2";
73
74
75     static RawStoreFactory factory;
76     static LockFactory lf;
77     static long commonContainer = -1;
78
79     static boolean testRollback; // initialize in start
80
static final String JavaDoc TEST_ROLLBACK_OFF = "derby.RawStore.RollbackTestOff";
81
82     private static ContextService contextService;
83     private T_Util t_util;
84
85     public T_StreamFile() {
86         super();
87     }
88
89     /**
90       @exception StandardException cannot startup the context service
91      */

92     public void boot(boolean create, Properties JavaDoc startParams)
93          throws StandardException {
94         super.boot(create, startParams);
95         contextService = ContextService.getFactory();
96     }
97
98
99     /*
100     ** Methods required by T_Generic
101     */

102
103     protected String JavaDoc getModuleToTestProtocolName() {
104         return RawStoreFactory.MODULE;
105     }
106
107
108     /**
109         Run the tests
110
111         @exception T_Fail Unexpected behaviour from the API
112      */

113     protected void setupTest() throws T_Fail {
114         String JavaDoc rollbackOff = PropertyUtil.getSystemProperty(TEST_ROLLBACK_OFF);
115         testRollback = !Boolean.valueOf(rollbackOff).booleanValue();
116
117         // don't automatic boot this service if it gets left around
118
if (startParams == null) {
119             startParams = new Properties JavaDoc();
120         }
121
122         startParams.put(Property.NO_AUTO_BOOT, Boolean.TRUE.toString());
123         // remove the service directory to ensure a clean run
124
startParams.put(Property.DELETE_ON_CREATE, Boolean.TRUE.toString());
125
126         // see if we are testing encryption
127
startParams = T_Util.setEncryptionParam(startParams);
128
129         try {
130             factory = (RawStoreFactory) Monitor.createPersistentService(getModuleToTestProtocolName(),
131                                 testService, startParams);
132             if (factory == null) {
133                 throw T_Fail.testFailMsg(getModuleToTestProtocolName() + " service not started.");
134             }
135
136             lf = factory.getLockFactory();
137             if (lf == null) {
138                 throw T_Fail.testFailMsg("LockFactory.MODULE not found");
139             }
140         } catch (StandardException mse) {
141             throw T_Fail.exceptionFail(mse);
142         }
143
144         t_util = new T_Util(factory, lf, contextService);
145         commonContainer = commonContainer();
146
147         return;
148     }
149
150
151     /**
152      * T_MultiThreadedIteration method
153      *
154      * @exception T_Fail Unexpected behaviour from the API
155      */

156     protected void joinSetupTest() throws T_Fail {
157
158         T_Fail.T_ASSERT(factory != null, "raw store factory not setup ");
159         T_Fail.T_ASSERT(contextService != null, "Context service not setup ");
160         T_Fail.T_ASSERT(commonContainer != -1, "common container not setup ");
161
162         t_util = new T_Util(factory, lf, contextService);
163
164     }
165
166     protected T_MultiThreadedIterations newTestObject() {
167         return new T_StreamFile();
168     }
169
170     /**
171       run the test
172
173       @exception T_Fail Unexpected behaviour from the API
174     */

175     protected void runTestSet() throws T_Fail {
176
177         // get a utility helper
178

179         ContextManager cm1 = contextService.newContextManager();
180         contextService.setCurrentContextManager(cm1);
181
182         try {
183
184             // boundry case: 1 row, 13 columns, string types
185
SF001(1, 13, T_RowSource.STRING_ROW_TYPE, false);
186
187             // boundry case: 1 rows, 1 null column, string types
188
SF001(1, 1, T_RowSource.STRING_ROW_TYPE, false);
189
190             // boundry case: 1000 rows, 1 null column, string types
191
SF001(1000, 1, T_RowSource.STRING_ROW_TYPE, false);
192
193             // boundry case: 1000 rows, 2 column (first null, second not null), string types
194
SF001(1000, 2, T_RowSource.STRING_ROW_TYPE, false);
195
196             // boundry case: 500 rows, 9 columns (first & last column null), string types
197
SF001(500, 9, T_RowSource.STRING_ROW_TYPE, false);
198
199             // 500 rows, 13 column, string type
200
SF001(500, 13, T_RowSource.STRING_ROW_TYPE, false);
201
202             // test error condition
203
SF001(1000, 2, T_RowSource.STRING_ROW_TYPE, true);
204
205             // The following test tests externalizable types, but we currently don't support it.
206
// do, don't run the test yet.
207
// 100 rows, 5 column, Integer object type
208
//SF001(100, 5, T_RowSource.INTEGER_ROW_TYPE, false);
209
// 100 rows, 1 column, Integer object type
210
//SF001(100, 1, T_RowSource.INTEGER_ROW_TYPE, false);
211

212             // SF002() tests are used to check performance of the stream file.
213
// no need to run them regularly.
214
//SF002(0);
215
//SF002(1);
216

217         } catch (StandardException se) {
218
219             cm1.cleanupOnError(se);
220             throw T_Fail.exceptionFail(se);
221         }
222         finally {
223             contextService.resetCurrentContextManager(cm1);
224         }
225     }
226
227     /*
228      * create a container that all threads can use
229      */

230     private long commonContainer() throws T_Fail {
231
232         ContextManager cm1 = contextService.newContextManager();
233         contextService.setCurrentContextManager(cm1);
234         long cid;
235
236         try {
237             Transaction t = t_util.t_startTransaction();
238             cid = t_util.t_addContainer(t, 0);
239             t_util.t_commit(t);
240             t.close();
241         }
242         catch (StandardException se) {
243
244             cm1.cleanupOnError(se);
245             throw T_Fail.exceptionFail(se);
246         }
247         finally {
248             contextService.resetCurrentContextManager(cm1);
249         }
250         return cid;
251     }
252
253     /*
254      * create a stream container load with rowCount number of rows.
255      * fetch it all back, and check to make sure all rows are correct.
256      */

257     protected void SF001(int rowCount, int columnCount, int columnType, boolean forceAbort)
258         throws StandardException, T_Fail {
259
260         Transaction t = t_util.t_startTransaction();
261
262         int segmentId = StreamContainerHandle.TEMPORARY_SEGMENT;
263         Properties JavaDoc properties = new Properties JavaDoc();
264         properties.put(RawStoreFactory.STREAM_FILE_BUFFER_SIZE_PARAMETER, "16384");
265  
266         // create row source
267
T_RowSource testRowSource = null;
268         if (forceAbort)
269             testRowSource = new T_RowSource(rowCount, columnCount, columnType, forceAbort, t);
270         else
271             testRowSource = new T_RowSource(rowCount, columnCount, columnType, forceAbort, null);
272
273         long startms = System.currentTimeMillis();
274
275         long containerId = t.addAndLoadStreamContainer(segmentId, properties, testRowSource);
276
277         long endms = System.currentTimeMillis();
278         long time = endms - startms;
279         REPORT("SF001 - write: " + time + "ms");
280
281         // open the container, and start fetching...
282
StreamContainerHandle scHandle =
283             t.openStreamContainer(segmentId, containerId, false);
284
285         // set up the template row
286
DataValueDescriptor template[] = null;
287         template = testRowSource.getTemplate();
288
289         DataValueDescriptor readRow[] = null;
290         readRow = testRowSource.getTemplate();
291         segmentId = StreamContainerHandle.TEMPORARY_SEGMENT;
292         int fetchCount = 0;
293         startms = System.currentTimeMillis();
294         while (scHandle.fetchNext(readRow)) {
295             fetchCount++;
296             // check to make sure the row is what we inserted.
297
// this depends on T_RowSource generate identical rows.
298
if (!readRow.toString().equals(template.toString()))
299                 throw T_Fail.testFailMsg("Record's value incorrect, expected :"
300                     + template.toString() + ": - got :" + readRow.toString());
301         }
302         endms = System.currentTimeMillis();
303         time = endms - startms;
304         // check to make sure we got the right number of rows.
305
if (fetchCount != rowCount)
306             throw T_Fail.testFailMsg("incorrect number of row fetched. Expecting " + rowCount
307                 + " rows, got " + fetchCount + ", rows instead.");
308         REPORT("SF001 - fetch: " + time + "ms");
309
310         scHandle.close();
311
312         t_util.t_commit(t);
313         t.close();
314
315         PASS("SF001, rowCount = " + rowCount + ", columnCount = " + columnCount + ", clumn type: " + columnType);
316     }
317
318     // this test test the rowSource over head.
319
// when param set to 1, also gets the overhead for writeExternal for Storables
320
protected void SF002(int param) throws StandardException, T_Fail {
321
322         T_RowSource rowSource = new T_RowSource(500000, 13, 2, false, null);
323
324         DynamicByteArrayOutputStream out = new DynamicByteArrayOutputStream(16384);
325         FormatIdOutputStream logicalDataOut = new FormatIdOutputStream(out);
326
327         long startms = System.currentTimeMillis();
328         System.out.println("starting rowSource test, time: " + startms);
329         try {
330
331             FormatableBitSet validColumns = rowSource.getValidColumns();
332
333             int numberFields = 0;
334             if (validColumns != null) {
335                 for (int i = validColumns.size() - 1; i >= 0; i--) {
336                     if (validColumns.get(i)) {
337                         numberFields = i + 1;
338                         break;
339                     }
340                 }
341             }
342
343             DataValueDescriptor[] row = rowSource.getNextRowFromRowSource();
344             while (row != null) {
345                 if (SanityManager.DEBUG) {
346                     SanityManager.ASSERT(row != null,
347                                          "RowSource returned null");
348                 }
349
350                 int arrayPosition = -1;
351                 for (int i = 0; i < numberFields; i++) {
352                     // write each column out
353
if (validColumns.get(i)) {
354                         arrayPosition++;
355                         DataValueDescriptor column = row[arrayPosition];
356
357                         if (param == 1) {
358                             try {
359                                 Storable sColumn = (Storable) column;
360                                 if (!sColumn.isNull()) {
361                                     sColumn.writeExternal(logicalDataOut);
362                                     out.reset();
363                                 }
364                             } catch (IOException ioe) {
365                                 throw T_Fail.exceptionFail(ioe);
366                             }
367                         }
368
369                     }
370                 }
371
372                 row = rowSource.getNextRowFromRowSource();
373             }
374
375         } finally {
376
377         }
378
379         long endms = System.currentTimeMillis();
380         long time2 = endms - startms;
381         if (param != 1)
382             System.out.println("ended rowSource test, time: " + endms
383                 + ", time spent = " + time2);
384         else
385             System.out.println("------ writeExternal called....\n ended rowSource test, time: " + endms
386                 + ", time spent = " + time2);
387         
388         PASS("SF002");
389     }
390 }
391
Popular Tags