00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
00090
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
00110 if (pVersionedRandomSegment) {
00111 VersionedRandomAllocationSegment *pVRSegment =
00112 SegmentFactory::dynamicCast<VersionedRandomAllocationSegment *>(
00113 pVersionedRandomSegment);
00114 pVRSegment->freeTempPages();
00115 }
00116 closeVersionedRandomSegment();
00117 if (pTempSegment) {
00118
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
00142
00143
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