001: /*
002:
003: Derby - Class org.apache.derbyTesting.unitTests.store.T_StreamFile
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.derbyTesting.unitTests.harness.T_MultiThreadedIterations;
025: import org.apache.derbyTesting.unitTests.harness.T_Fail;
026:
027: import org.apache.derby.impl.store.raw.data.*;
028:
029: import org.apache.derby.iapi.services.context.ContextService;
030: import org.apache.derby.iapi.services.context.ContextManager;
031: import org.apache.derby.iapi.services.locks.*;
032: import org.apache.derby.iapi.services.property.PropertyUtil;
033: import org.apache.derby.iapi.services.monitor.Monitor;
034: import org.apache.derby.iapi.services.sanity.SanityManager;
035: import org.apache.derby.iapi.services.io.Storable;
036: import org.apache.derby.iapi.services.io.FormatIdOutputStream;
037:
038: import org.apache.derby.iapi.error.StandardException;
039: import org.apache.derby.iapi.store.raw.*;
040: import org.apache.derby.iapi.store.raw.xact.RawTransaction;
041: import org.apache.derby.iapi.store.raw.data.RawContainerHandle;
042:
043: import org.apache.derby.iapi.store.access.conglomerate.LogicalUndo;
044:
045: import org.apache.derby.iapi.types.DataValueDescriptor;
046:
047: import org.apache.derby.iapi.reference.Property;
048:
049: import org.apache.derby.iapi.services.io.FormatableBitSet;
050: import org.apache.derby.iapi.services.io.DynamicByteArrayOutputStream;
051:
052: import java.io.*;
053: import java.util.Properties;
054:
055: /**
056: An Impl unittest for rawstore data that is based on the stream file
057: */
058:
059: public class T_StreamFile extends T_MultiThreadedIterations {
060:
061: private static final String testService = "streamFileTest";
062:
063: static final String REC_001 = "McLaren";
064: static final String REC_002 = "Ferrari";
065: static final String REC_003 = "Benetton";
066: static final String REC_004 = "Prost";
067: static final String REC_005 = "Tyrell";
068: static final String REC_006 = "Derby, Natscape, Goatscape, the popular names";
069: static final String REC_007 = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
070:
071: static final String SP1 = "savepoint1";
072: static final String SP2 = "savepoint2";
073:
074: static RawStoreFactory factory;
075: static LockFactory lf;
076: static long commonContainer = -1;
077:
078: static boolean testRollback; // initialize in start
079: static final String TEST_ROLLBACK_OFF = "derby.RawStore.RollbackTestOff";
080:
081: private static ContextService contextService;
082: private T_Util t_util;
083:
084: public T_StreamFile() {
085: super ();
086: }
087:
088: /**
089: @exception StandardException cannot startup the context service
090: */
091: public void boot(boolean create, Properties startParams)
092: throws StandardException {
093: super .boot(create, startParams);
094: contextService = ContextService.getFactory();
095: }
096:
097: /*
098: ** Methods required by T_Generic
099: */
100:
101: protected String getModuleToTestProtocolName() {
102: return RawStoreFactory.MODULE;
103: }
104:
105: /**
106: Run the tests
107:
108: @exception T_Fail Unexpected behaviour from the API
109: */
110: protected void setupTest() throws T_Fail {
111: String rollbackOff = PropertyUtil
112: .getSystemProperty(TEST_ROLLBACK_OFF);
113: testRollback = !Boolean.valueOf(rollbackOff).booleanValue();
114:
115: // don't automatic boot this service if it gets left around
116: if (startParams == null) {
117: startParams = new Properties();
118: }
119:
120: startParams.put(Property.NO_AUTO_BOOT, Boolean.TRUE.toString());
121: // remove the service directory to ensure a clean run
122: startParams.put(Property.DELETE_ON_CREATE, Boolean.TRUE
123: .toString());
124:
125: // see if we are testing encryption
126: startParams = T_Util.setEncryptionParam(startParams);
127:
128: try {
129: factory = (RawStoreFactory) Monitor
130: .createPersistentService(
131: getModuleToTestProtocolName(), testService,
132: startParams);
133: if (factory == null) {
134: throw T_Fail.testFailMsg(getModuleToTestProtocolName()
135: + " service not started.");
136: }
137:
138: lf = factory.getLockFactory();
139: if (lf == null) {
140: throw T_Fail
141: .testFailMsg("LockFactory.MODULE not found");
142: }
143: } catch (StandardException mse) {
144: throw T_Fail.exceptionFail(mse);
145: }
146:
147: t_util = new T_Util(factory, lf, contextService);
148: commonContainer = commonContainer();
149:
150: return;
151: }
152:
153: /**
154: * T_MultiThreadedIteration method
155: *
156: * @exception T_Fail Unexpected behaviour from the API
157: */
158: protected void joinSetupTest() throws T_Fail {
159:
160: T_Fail
161: .T_ASSERT(factory != null,
162: "raw store factory not setup ");
163: T_Fail.T_ASSERT(contextService != null,
164: "Context service not setup ");
165: T_Fail.T_ASSERT(commonContainer != -1,
166: "common container not setup ");
167:
168: t_util = new T_Util(factory, lf, contextService);
169:
170: }
171:
172: protected T_MultiThreadedIterations newTestObject() {
173: return new T_StreamFile();
174: }
175:
176: /**
177: run the test
178:
179: @exception T_Fail Unexpected behaviour from the API
180: */
181: protected void runTestSet() throws T_Fail {
182:
183: // get a utility helper
184:
185: ContextManager cm1 = contextService.newContextManager();
186: contextService.setCurrentContextManager(cm1);
187:
188: try {
189:
190: // boundry case: 1 row, 13 columns, string types
191: SF001(1, 13, T_RowSource.STRING_ROW_TYPE, false);
192:
193: // boundry case: 1 rows, 1 null column, string types
194: SF001(1, 1, T_RowSource.STRING_ROW_TYPE, false);
195:
196: // boundry case: 1000 rows, 1 null column, string types
197: SF001(1000, 1, T_RowSource.STRING_ROW_TYPE, false);
198:
199: // boundry case: 1000 rows, 2 column (first null, second not null), string types
200: SF001(1000, 2, T_RowSource.STRING_ROW_TYPE, false);
201:
202: // boundry case: 500 rows, 9 columns (first & last column null), string types
203: SF001(500, 9, T_RowSource.STRING_ROW_TYPE, false);
204:
205: // 500 rows, 13 column, string type
206: SF001(500, 13, T_RowSource.STRING_ROW_TYPE, false);
207:
208: // test error condition
209: SF001(1000, 2, T_RowSource.STRING_ROW_TYPE, true);
210:
211: // The following test tests externalizable types, but we currently don't support it.
212: // do, don't run the test yet.
213: // 100 rows, 5 column, Integer object type
214: //SF001(100, 5, T_RowSource.INTEGER_ROW_TYPE, false);
215: // 100 rows, 1 column, Integer object type
216: //SF001(100, 1, T_RowSource.INTEGER_ROW_TYPE, false);
217:
218: // SF002() tests are used to check performance of the stream file.
219: // no need to run them regularly.
220: //SF002(0);
221: //SF002(1);
222:
223: } catch (StandardException se) {
224:
225: cm1.cleanupOnError(se);
226: throw T_Fail.exceptionFail(se);
227: } finally {
228: contextService.resetCurrentContextManager(cm1);
229: }
230: }
231:
232: /*
233: * create a container that all threads can use
234: */
235: private long commonContainer() throws T_Fail {
236:
237: ContextManager cm1 = contextService.newContextManager();
238: contextService.setCurrentContextManager(cm1);
239: long cid;
240:
241: try {
242: Transaction t = t_util.t_startTransaction();
243: cid = t_util.t_addContainer(t, 0);
244: t_util.t_commit(t);
245: t.close();
246: } catch (StandardException se) {
247:
248: cm1.cleanupOnError(se);
249: throw T_Fail.exceptionFail(se);
250: } finally {
251: contextService.resetCurrentContextManager(cm1);
252: }
253: return cid;
254: }
255:
256: /*
257: * create a stream container load with rowCount number of rows.
258: * fetch it all back, and check to make sure all rows are correct.
259: */
260: protected void SF001(int rowCount, int columnCount, int columnType,
261: boolean forceAbort) throws StandardException, T_Fail {
262:
263: Transaction t = t_util.t_startTransaction();
264:
265: int segmentId = StreamContainerHandle.TEMPORARY_SEGMENT;
266: Properties properties = new Properties();
267: properties.put(
268: RawStoreFactory.STREAM_FILE_BUFFER_SIZE_PARAMETER,
269: "16384");
270:
271: // create row source
272: T_RowSource testRowSource = null;
273: if (forceAbort)
274: testRowSource = new T_RowSource(rowCount, columnCount,
275: columnType, forceAbort, t);
276: else
277: testRowSource = new T_RowSource(rowCount, columnCount,
278: columnType, forceAbort, null);
279:
280: long startms = System.currentTimeMillis();
281:
282: long containerId = t.addAndLoadStreamContainer(segmentId,
283: properties, testRowSource);
284:
285: long endms = System.currentTimeMillis();
286: long time = endms - startms;
287: REPORT("SF001 - write: " + time + "ms");
288:
289: // open the container, and start fetching...
290: StreamContainerHandle scHandle = t.openStreamContainer(
291: segmentId, containerId, false);
292:
293: // set up the template row
294: DataValueDescriptor template[] = null;
295: template = testRowSource.getTemplate();
296:
297: DataValueDescriptor readRow[] = null;
298: readRow = testRowSource.getTemplate();
299: segmentId = StreamContainerHandle.TEMPORARY_SEGMENT;
300: int fetchCount = 0;
301: startms = System.currentTimeMillis();
302: while (scHandle.fetchNext(readRow)) {
303: fetchCount++;
304: // check to make sure the row is what we inserted.
305: // this depends on T_RowSource generate identical rows.
306: if (!readRow.toString().equals(template.toString()))
307: throw T_Fail
308: .testFailMsg("Record's value incorrect, expected :"
309: + template.toString()
310: + ": - got :"
311: + readRow.toString());
312: }
313: endms = System.currentTimeMillis();
314: time = endms - startms;
315: // check to make sure we got the right number of rows.
316: if (fetchCount != rowCount)
317: throw T_Fail
318: .testFailMsg("incorrect number of row fetched. Expecting "
319: + rowCount
320: + " rows, got "
321: + fetchCount
322: + ", rows instead.");
323: REPORT("SF001 - fetch: " + time + "ms");
324:
325: scHandle.close();
326:
327: t_util.t_commit(t);
328: t.close();
329:
330: PASS("SF001, rowCount = " + rowCount + ", columnCount = "
331: + columnCount + ", clumn type: " + columnType);
332: }
333:
334: // this test test the rowSource over head.
335: // when param set to 1, also gets the overhead for writeExternal for Storables
336: protected void SF002(int param) throws StandardException, T_Fail {
337:
338: T_RowSource rowSource = new T_RowSource(500000, 13, 2, false,
339: null);
340:
341: DynamicByteArrayOutputStream out = new DynamicByteArrayOutputStream(
342: 16384);
343: FormatIdOutputStream logicalDataOut = new FormatIdOutputStream(
344: out);
345:
346: long startms = System.currentTimeMillis();
347: System.out.println("starting rowSource test, time: " + startms);
348: try {
349:
350: FormatableBitSet validColumns = rowSource.getValidColumns();
351:
352: int numberFields = 0;
353: if (validColumns != null) {
354: for (int i = validColumns.size() - 1; i >= 0; i--) {
355: if (validColumns.get(i)) {
356: numberFields = i + 1;
357: break;
358: }
359: }
360: }
361:
362: DataValueDescriptor[] row = rowSource
363: .getNextRowFromRowSource();
364: while (row != null) {
365: if (SanityManager.DEBUG) {
366: SanityManager.ASSERT(row != null,
367: "RowSource returned null");
368: }
369:
370: int arrayPosition = -1;
371: for (int i = 0; i < numberFields; i++) {
372: // write each column out
373: if (validColumns.get(i)) {
374: arrayPosition++;
375: DataValueDescriptor column = row[arrayPosition];
376:
377: if (param == 1) {
378: try {
379: Storable sColumn = (Storable) column;
380: if (!sColumn.isNull()) {
381: sColumn
382: .writeExternal(logicalDataOut);
383: out.reset();
384: }
385: } catch (IOException ioe) {
386: throw T_Fail.exceptionFail(ioe);
387: }
388: }
389:
390: }
391: }
392:
393: row = rowSource.getNextRowFromRowSource();
394: }
395:
396: } finally {
397:
398: }
399:
400: long endms = System.currentTimeMillis();
401: long time2 = endms - startms;
402: if (param != 1)
403: System.out.println("ended rowSource test, time: " + endms
404: + ", time spent = " + time2);
405: else
406: System.out
407: .println("------ writeExternal called....\n ended rowSource test, time: "
408: + endms + ", time spent = " + time2);
409:
410: PASS("SF002");
411: }
412: }
|