ScratchSegment.cpp

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/segment/ScratchSegment.cpp#18 $
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/segment/ScratchSegment.h"
00026 #include "fennel/segment/ScratchMemExcn.h"
00027 #include "fennel/cache/Cache.h"
00028 #include "fennel/common/FennelExcn.h"
00029 
00030 FENNEL_BEGIN_CPPFILE("$Id: //open/dev/fennel/segment/ScratchSegment.cpp#18 $");
00031 
00032 ScratchSegment::ScratchSegment(
00033     SharedCache pCache,
00034     uint nPagesMaxInit)
00035     : Segment(pCache)
00036 {
00037     nPagesMax = nPagesMaxInit;
00038 }
00039 
00040 void ScratchSegment::closeImpl()
00041 {
00042     // this overrides Segment::closeImpl; we know exactly which pages to
00043     // discard, so we can skip the full-cache sweep
00044     clearPages();
00045 }
00046 
00047 void ScratchSegment::clearPages()
00048 {
00049     // TODO:  separate unlockScratchPage method in Cache
00050 
00051     for (PageListIter ppPage = pages.begin(); ppPage != pages.end(); ++ppPage) {
00052         CachePage &page = **ppPage;
00053         getCache()->unlockPage(page,LOCKMODE_X);
00054     }
00055     pages.clear();
00056 }
00057 
00058 BlockId ScratchSegment::translatePageId(PageId pageId)
00059 {
00060     assert(isPageIdAllocated(pageId));
00061     BlockId blockId(0);
00062     CompoundId::setDeviceId(blockId,Cache::NULL_DEVICE_ID);
00063     CompoundId::setBlockNum(
00064         blockId,
00065         getLinearBlockNum(pageId));
00066     return blockId;
00067 }
00068 
00069 PageId ScratchSegment::translateBlockId(BlockId blockId)
00070 {
00071     return getLinearPageId(CompoundId::getBlockNum(blockId));
00072 }
00073 
00074 BlockNum ScratchSegment::getAllocatedSizeInPages()
00075 {
00076     return pages.size();
00077 }
00078 
00079 BlockNum ScratchSegment::getNumPagesOccupiedHighWater()
00080 {
00081     return getAllocatedSizeInPages();
00082 }
00083 
00084 BlockNum ScratchSegment::getNumPagesExtended()
00085 {
00086     return BlockNum(0);
00087 }
00088 
00089 PageId ScratchSegment::allocatePageId(PageOwnerId)
00090 {
00091     StrictMutexGuard mutexGuard(mutex);
00092 
00093     // nothing to do with PageOwnerId
00094 
00095     if (getAllocatedSizeInPages() >= nPagesMax) {
00096         return NULL_PAGE_ID;
00097     }
00098 
00099     BlockNum blockNum = pages.size();
00100     CachePage &page = getCache()->lockScratchPage(blockNum);
00101     pages.push_back(&page);
00102     return getLinearPageId(blockNum);
00103 }
00104 
00105 void ScratchSegment::deallocatePageRange(PageId startPageId,PageId endPageId)
00106 {
00107     assert(startPageId == NULL_PAGE_ID);
00108     assert(endPageId == NULL_PAGE_ID);
00109 
00110     StrictMutexGuard mutexGuard(mutex);
00111     clearPages();
00112 }
00113 
00114 bool ScratchSegment::isPageIdAllocated(PageId pageId)
00115 {
00116     return isLinearPageIdAllocated(pageId);
00117 }
00118 
00119 PageId ScratchSegment::getPageSuccessor(PageId pageId)
00120 {
00121     return getLinearPageSuccessor(pageId);
00122 }
00123 
00124 void ScratchSegment::setPageSuccessor(PageId pageId,PageId successorId)
00125 {
00126     setLinearPageSuccessor(pageId,successorId);
00127 }
00128 
00129 Segment::AllocationOrder ScratchSegment::getAllocationOrder() const
00130 {
00131     return LINEAR_ALLOCATION;
00132 }
00133 
00134 CachePage *ScratchSegment::lockPage(
00135     BlockId blockId,
00136     LockMode lockMode,
00137     bool readIfUnmapped,
00138     MappedPageListener *pMappedPageListener,
00139     TxnId txnId)
00140 {
00141     StrictMutexGuard mutexGuard(mutex);
00142 
00143     assert(CompoundId::getDeviceId(blockId) == Cache::NULL_DEVICE_ID);
00144     BlockNum blockNum = CompoundId::getBlockNum(blockId);
00145     assert(blockNum < pages.size());
00146     // TODO:  should assert(pMappedPageListener == this), but that doesn't work
00147     // when tracing is enabled
00148     return pages[blockNum];
00149 }
00150 
00151 void ScratchSegment::unlockPage(
00152     CachePage &,
00153     LockMode,
00154     TxnId)
00155 {
00156     // ignore; pages remain locked until segment is closed
00157 }
00158 
00159 // REVIEW:  should assert on some of the following currently ignored?
00160 
00161 void ScratchSegment::discardPage(
00162     BlockId)
00163 {
00164 }
00165 
00166 bool ScratchSegment::prefetchPage(
00167     BlockId,
00168     MappedPageListener *)
00169 {
00170     return false;
00171 }
00172 
00173 void ScratchSegment::prefetchBatch(
00174     BlockId,uint,
00175     MappedPageListener *)
00176 {
00177 }
00178 
00179 void ScratchSegment::flushPage(CachePage &,bool)
00180 {
00181 }
00182 
00183 void ScratchSegment::nicePage(CachePage &)
00184 {
00185     // ignore
00186 }
00187 
00188 SharedCache ScratchSegment::getCache()
00189 {
00190     return Segment::getCache();
00191 }
00192 
00193 uint ScratchSegment::getMaxLockedPages()
00194 {
00195     if (isMAXU(nPagesMax)) {
00196         return getCache()->getMaxLockedPages();
00197     } else {
00198         return nPagesMax;
00199     }
00200 }
00201 
00202 void ScratchSegment::setMaxLockedPages(uint nPages)
00203 {
00204     StrictMutexGuard mutexGuard(mutex);
00205     assert(nPages >= pages.size());
00206     nPagesMax = nPages;
00207 }
00208 
00209 void ScratchSegment::setTxnId(TxnId)
00210 {
00211 }
00212 
00213 TxnId ScratchSegment::getTxnId() const
00214 {
00215     return IMPLICIT_TXN_ID;
00216 }
00217 
00218 void ScratchSegment::getPrefetchParams(
00219     uint &nPagesPerBatch,
00220     uint &nBatchPrefetches)
00221 {
00222 }
00223 
00224 FENNEL_END_CPPFILE("$Id: //open/dev/fennel/segment/ScratchSegment.cpp#18 $");
00225 
00226 // End ScratchSegment.cpp

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