📄 t_filesystemdata.java
字号:
/* Derby - Class org.apache.derbyTesting.unitTests.store.T_FileSystemData Copyright 1997, 2005 The Apache Software Foundation or its licensors, as applicable. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */package org.apache.derbyTesting.unitTests.store;import org.apache.derby.impl.store.raw.data.*;import org.apache.derbyTesting.unitTests.harness.T_MultiThreadedIterations;import org.apache.derbyTesting.unitTests.harness.T_Fail;import org.apache.derby.iapi.services.context.ContextService;import org.apache.derby.iapi.services.context.ContextManager;import org.apache.derby.iapi.services.locks.*;import org.apache.derby.iapi.services.monitor.Monitor;import org.apache.derby.iapi.services.sanity.SanityManager;import org.apache.derby.iapi.services.io.Storable;import org.apache.derby.iapi.services.property.PropertyUtil;import org.apache.derby.iapi.error.StandardException;import org.apache.derby.iapi.store.raw.*;import org.apache.derby.iapi.store.raw.xact.RawTransaction;import org.apache.derby.iapi.store.raw.data.RawContainerHandle;import org.apache.derby.iapi.store.access.conglomerate.LogicalUndo;import org.apache.derby.iapi.reference.Property;import java.io.*;import java.util.Properties;/** An Impl unittest for rawstore data that is based on the FileSystem*/public class T_FileSystemData extends T_MultiThreadedIterations { private static final String testService = "fileSystemDataTest"; static final String REC_001 = "McLaren"; static final String REC_002 = "Ferrari"; static final String REC_003 = "Benetton"; static final String REC_004 = "Prost"; static final String REC_005 = "Tyrell"; static final String REC_006 = "Derby, Natscape, Goatscape, the popular names"; static final String REC_007 = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"; static final String SP1 = "savepoint1"; static final String SP2 = "savepoint2"; static RawStoreFactory factory; static LockFactory lf; static long commonContainer = -1; static boolean testRollback; // initialize in start static final String TEST_ROLLBACK_OFF = "derby.RawStore.RollbackTestOff"; private static ContextService contextService; private T_Util t_util; public T_FileSystemData() { super(); } /** @exception StandardException cannot startup the context service */ public void boot(boolean create, Properties startParams) throws StandardException { super.boot(create, startParams); contextService = ContextService.getFactory(); } /* ** Methods required by T_Generic */ protected String getModuleToTestProtocolName() { return RawStoreFactory.MODULE; } /** Run the tests @exception T_Fail Unexpected behaviour from the API */ protected void setupTest() throws T_Fail { String rollbackOff = PropertyUtil.getSystemProperty(TEST_ROLLBACK_OFF); testRollback = !Boolean.valueOf(rollbackOff).booleanValue(); // don't automatic boot this service if it gets left around if (startParams == null) { startParams = new Properties(); } // see if we are testing encryption startParams = T_Util.setEncryptionParam(startParams); startParams.put(Property.NO_AUTO_BOOT, Boolean.TRUE.toString()); // remove the service directory to ensure a clean run startParams.put(Property.DELETE_ON_CREATE, Boolean.TRUE.toString()); try { factory = (RawStoreFactory) Monitor.createPersistentService(getModuleToTestProtocolName(), testService, startParams); if (factory == null) { throw T_Fail.testFailMsg(getModuleToTestProtocolName() + " service not started."); } lf = factory.getLockFactory(); if (lf == null) { throw T_Fail.testFailMsg("LockFactory.MODULE not found"); } } catch (StandardException mse) { throw T_Fail.exceptionFail(mse); } t_util = new T_Util(factory, lf, contextService); commonContainer = commonContainer(); return; } /** * T_MultiThreadedIteration method * * @exception T_Fail Unexpected behaviour from the API */ protected void joinSetupTest() throws T_Fail { T_Fail.T_ASSERT(factory != null, "raw store factory not setup "); T_Fail.T_ASSERT(contextService != null, "Context service not setup "); T_Fail.T_ASSERT(commonContainer != -1, "common container not setup "); t_util = new T_Util(factory, lf, contextService); } protected T_MultiThreadedIterations newTestObject() { return new T_FileSystemData(); } /** run the test @exception T_Fail Unexpected behaviour from the API */ protected void runTestSet() throws T_Fail { // get a utility helper ContextManager cm1 = contextService.newContextManager(); contextService.setCurrentContextManager(cm1); try { runCostEstimationTests(); runAllocationTests(); } catch (StandardException se) { cm1.cleanupOnError(se); throw T_Fail.exceptionFail(se); } finally { contextService.resetCurrentContextManager(cm1); } } /* * create a container that all threads can use */ private long commonContainer() throws T_Fail { ContextManager cm1 = contextService.newContextManager(); contextService.setCurrentContextManager(cm1); long cid; try { Transaction t = t_util.t_startTransaction(); cid = t_util.t_addContainer(t, 0); t_util.t_commit(t); t.close(); } catch (StandardException se) { cm1.cleanupOnError(se); throw T_Fail.exceptionFail(se); } finally { contextService.resetCurrentContextManager(cm1); } return cid; } protected void runCostEstimationTests() throws T_Fail, StandardException { CostEstimationTest1(); } protected void runAllocationTests() throws T_Fail, StandardException { // don't run these for > 2 threads if (threadNumber < 2) { AllocTest1(); // test remove and reuse of page AllocTest2(); // test remove and drop and rollback of remove AllocTest3(); // test multiple alloc page AllocTest4(); // test preallocation } // can't get this test to pass consistently because it depends on // timing of the cache. // AllocTest5(); // test gettting 1/2 filled page for insert AllocMTest1(commonContainer); // test multi thread access to the same container } /** @exception T_Fail Unexpected behaviour from the API @exception StandardException Standard Derby error policy */ protected void CostEstimationTest1() throws StandardException, T_Fail { // getEstimatedRowCount(0), setEstimatedRowCount(long count, int flag), // getEstimatedPageCount(int flag); Transaction t = t_util.t_startTransaction(); long cid = t_util.t_addContainer(t, 0); t_util.t_commit(t); ContainerHandle c = t_util.t_openContainer(t, 0, cid, true); try { int numRows = 10; T_RawStoreRow row = new T_RawStoreRow(REC_001); RecordHandle rh[] = new RecordHandle[numRows]; // insert numRows rows into container for (int i = 0; i < numRows; i++) rh[i] = t_util.t_insert(c, row); t_util.t_commit(t); c = t_util.t_openContainer(t, 0, cid, true); if ((c.getEstimatedRowCount(0) != numRows) && (c.getEstimatedRowCount(0) != (numRows - 1))) { // due to timing, sometimes estimate row count is 9 rather than // 10. throw T_Fail.testFailMsg( "expect estimated row count to be " + (numRows - 1) + " or " + numRows + ", got " + c.getEstimatedRowCount(0)); } // now update them that cause overflowing - expect the same row count T_RawStoreRow longRow = new T_RawStoreRow(REC_007); for (int i = 0; i < numRows; i++) t_util.t_update(c, rh[i], longRow); t_util.t_commit(t); c = t_util.t_openContainer(t, 0, cid, true); if (c.getEstimatedRowCount(0) != numRows) if ((c.getEstimatedRowCount(0) != numRows) && (c.getEstimatedRowCount(0) != (numRows - 1))) { // due to timing, sometimes estimate row count is 9 rather than // 10. throw T_Fail.testFailMsg( "expect after update same estimated row count, but it is not." + "expect estimated row count to be " + (numRows - 1) + " or " + numRows + ", got " + c.getEstimatedRowCount(0)); } // now focibly set the row count c.setEstimatedRowCount(2*numRows, 0); if (c.getEstimatedRowCount(0) != 2*numRows) throw T_Fail.testFailMsg("forcibly setting estimated row count doesn't seem to work"); // now purge some rows, this should alter the row count. Page p = null; long pnum = 0; long purgedCount = 0; for (p = c.getFirstPage(); p != null; p = c.getNextPage(pnum)) { int rcount = p.recordCount()/3; pnum = p.getPageNumber(); p.deleteAtSlot(0, true, (LogicalUndo)null); p.purgeAtSlot(rcount, rcount, true); // purget the middle 1/3 of the page purgedCount += rcount + 1; p.unlatch(); } t_util.t_commit(t); c = t_util.t_openContainer(t, 0, cid, true); if (c.getEstimatedRowCount(0) != (2*numRows - purgedCount)) throw T_Fail.testFailMsg("expect " + (2*numRows-purgedCount) + " after purge"); // now get rid of some pages to alter the row count REPORT("before page delete, estRC = " + (2*numRows) + " - " + purgedCount); for (p = c.getFirstPage(); p != null; p = c.getNextPage(pnum)) { pnum = p.getPageNumber(); if ((pnum%2) == 0) { purgedCount += p.nonDeletedRecordCount(); c.removePage(p); } else p.unlatch(); } t_util.t_commit(t); c = t_util.t_openContainer(t, 0, cid, true); if (c.getEstimatedRowCount(0) != (2*numRows - purgedCount)) throw T_Fail.testFailMsg("expect " + (2*numRows-purgedCount) + " after page remove, got " + c.getEstimatedRowCount(0)); PASS("CostEstimationTest1"); } finally { t_util.t_commit(t); t.close(); } } protected void AllocTest1() throws StandardException, T_Fail { /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -