SnapshotSegmentTestBase.cpp

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/test/SnapshotSegmentTestBase.cpp#4 $
00003 // Fennel is a library of data storage and processing components.
00004 // Copyright (C) 2005-2009 The Eigenbase Project
00005 // Copyright (C) 2005-2009 SQLstream, Inc.
00006 // Copyright (C) 2005-2009 LucidEra, Inc.
00007 // Portions Copyright (C) 1999-2009 John V. Sichi
00008 //
00009 // This program is free software; you can redistribute it and/or modify it
00010 // under the terms of the GNU General Public License as published by the Free
00011 // Software Foundation; either version 2 of the License, or (at your option)
00012 // any later version approved by The Eigenbase Project.
00013 //
00014 // This program is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with this program; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 */
00023 
00024 #include "fennel/common/CommonPreamble.h"
00025 #include "fennel/test/SnapshotSegmentTestBase.h"
00026 #include "fennel/segment/VersionedRandomAllocationSegment.h"
00027 #include "fennel/segment/SnapshotRandomAllocationSegment.h"
00028 #include "fennel/segment/LinearViewSegment.h"
00029 
00030 #include <boost/test/test_tools.hpp>
00031 
00032 using namespace fennel;
00033 
00034 SnapshotSegmentTestBase::SnapshotSegmentTestBase()
00035 {
00036     nDiskPagesTotal = nDiskPages;
00037     tempDeviceId = DeviceId(42);
00038 }
00039 
00040 void SnapshotSegmentTestBase::testCaseSetUp()
00041 {
00042     currCsn = TxnId(0);
00043     commit = true;
00044     updatedCsns.clear();
00045 }
00046 
00047 void SnapshotSegmentTestBase::openSegmentStorage(DeviceMode openMode)
00048 {
00049     nDiskPages = nDiskPagesTotal;
00050     if (openMode.create) {
00051         firstPageId = NULL_PAGE_ID;
00052     }
00053 
00054     pTempDevice =
00055         openDevice("temp.dat", openMode, nDiskPages / 50, tempDeviceId);
00056     SharedSegment pTempDeviceSegment =
00057         createLinearDeviceSegment(tempDeviceId, nDiskPages / 50);
00058     pTempSegment =
00059         pSegmentFactory->newRandomAllocationSegment(
00060             pTempDeviceSegment,
00061             openMode.create);
00062 
00063     SharedSegment pDeviceSegment =
00064         createLinearDeviceSegment(dataDeviceId, nDiskPages);
00065     pVersionedRandomSegment =
00066         pSegmentFactory->newVersionedRandomAllocationSegment(
00067             pDeviceSegment,
00068             pTempSegment,
00069             openMode.create);
00070     pSnapshotRandomSegment =
00071         pSegmentFactory->newSnapshotRandomAllocationSegment(
00072             pVersionedRandomSegment,
00073             pVersionedRandomSegment,
00074             currCsn);
00075     setForceCacheUnmap(pSnapshotRandomSegment);
00076 
00077     pRandomSegment = pSnapshotRandomSegment;
00078 
00079     nDiskPages /= 2;
00080     SharedSegment pLinearViewSegment =
00081         pSegmentFactory->newLinearViewSegment(
00082             pSnapshotRandomSegment,
00083             firstPageId);
00084     pLinearSegment = pLinearViewSegment;
00085 }
00086 
00087 void SnapshotSegmentTestBase::setForceCacheUnmap(SharedSegment pSegment)
00088 {
00089     // Force the snapshot segment to always execute its checkpoints during
00090     // a cache flush and unmap, in order to unmap these page from the cache
00091     if (pSegment) {
00092         SnapshotRandomAllocationSegment *pSnapshotSegment =
00093             SegmentFactory::dynamicCast<SnapshotRandomAllocationSegment *>(
00094                 pSegment);
00095         pSnapshotSegment->setForceCacheUnmap();
00096     }
00097 }
00098 
00099 void SnapshotSegmentTestBase::closeStorage()
00100 {
00101     commitChanges(currCsn);
00102     closeLinearSegment();
00103     pRandomSegment.reset();
00104     closeSnapshotRandomSegment();
00105     if (pSnapshotRandomSegment2) {
00106         assert(pSnapshotRandomSegment2.unique());
00107         pSnapshotRandomSegment2.reset();
00108     }
00109     // Free leftover temp pages used during page versioning
00110     if (pVersionedRandomSegment) {
00111         VersionedRandomAllocationSegment *pVRSegment =
00112             SegmentFactory::dynamicCast<VersionedRandomAllocationSegment *>(
00113                 pVersionedRandomSegment);
00114         pVRSegment->freeTempPages();
00115     }
00116     closeVersionedRandomSegment();
00117     if (pTempSegment) {
00118         // Confirm that all temp pages have been freed.
00119         BOOST_REQUIRE(pTempSegment->getAllocatedSizeInPages() == 0);
00120         assert(pTempSegment.unique());
00121         pTempSegment.reset();
00122     }
00123     if (pTempDevice) {
00124         closeDevice(tempDeviceId, pTempDevice);
00125     }
00126     SegmentTestBase::closeStorage();
00127 }
00128 
00129 void SnapshotSegmentTestBase::testAllocateAll()
00130 {
00131     SegmentTestBase::testAllocateAll();
00132     assert(firstPageId == NULL_PAGE_ID);
00133     LinearViewSegment *pLinearViewSegment =
00134         SegmentFactory::dynamicCast<LinearViewSegment *>(pLinearSegment);
00135     assert(pLinearViewSegment);
00136     firstPageId = pLinearViewSegment->getFirstPageId();
00137 }
00138 
00139 void SnapshotSegmentTestBase::verifyPage(CachePage &page, uint x)
00140 {
00141     // If the pageId is a multiple of one of the csn's smaller than the
00142     // current csn, then the page should reflect the update made by
00143     // that smaller csn
00144     uint update = 0;
00145     for (int i = updatedCsns.size() - 1; i >= 0; i--) {
00146         if (updatedCsns[i] <= currCsn &&
00147             x % opaqueToInt(updatedCsns[i]) == 0)
00148         {
00149             update = opaqueToInt(updatedCsns[i]);
00150             break;
00151         }
00152     }
00153     SegmentTestBase::verifyPage(page, x + update);
00154 }
00155 
00156 void SnapshotSegmentTestBase::fillPage(CachePage &page, uint x)
00157 {
00158     SegmentTestBase::fillPage(page, x + opaqueToInt(currCsn));
00159 }
00160 
00161 void SnapshotSegmentTestBase::commitChanges(TxnId commitCsn)
00162 {
00163     if (pSnapshotRandomSegment) {
00164         SnapshotRandomAllocationSegment *pSnapshotSegment =
00165             SegmentFactory::dynamicCast<SnapshotRandomAllocationSegment *>(
00166                 pSnapshotRandomSegment);
00167         if (commit) {
00168             pSnapshotSegment->commitChanges(commitCsn);
00169         } else {
00170             pSnapshotSegment->rollbackChanges();
00171         }
00172     }
00173 }
00174 
00175 // End SnapshotSegmentTestBase.cpp

Generated on Mon Jun 22 04:00:21 2009 for Fennel by  doxygen 1.5.1