Segment.cpp

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/segment/Segment.cpp#15 $
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/cache/Cache.h"
00026 #include "fennel/cache/CachePage.h"
00027 #include "fennel/cache/PagePredicate.h"
00028 #include "fennel/segment/Segment.h"
00029 #include "fennel/segment/SegmentAccessor.h"
00030 #include "fennel/segment/SegmentMap.h"
00031 
00032 FENNEL_BEGIN_CPPFILE("$Id: //open/dev/fennel/segment/Segment.cpp#15 $");
00033 
00034 Segment::Segment(SharedCache pCacheInit)
00035     : pCache(pCacheInit)
00036 {
00037     setUsablePageSize(getFullPageSize());
00038 }
00039 
00040 Segment::~Segment()
00041 {
00042     close();
00043 }
00044 
00045 void Segment::closeImpl()
00046 {
00047     checkpoint(CHECKPOINT_FLUSH_AND_UNMAP);
00048 }
00049 
00050 SharedSegment Segment::getTracingSegment()
00051 {
00052     SharedSegment sharedPtr = pTracingSegment.lock();
00053     if (sharedPtr && sharedPtr.get()) {
00054         return sharedPtr;
00055     } else {
00056         return shared_from_this();
00057     }
00058 }
00059 
00060 void Segment::setTracingSegment(WeakSegment pTracingSegmentInit)
00061 {
00062     pTracingSegment = pTracingSegmentInit;
00063 }
00064 
00065 MappedPageListener *Segment::getTracingListener()
00066 {
00067     return getTracingSegment().get();
00068 }
00069 
00070 void Segment::setUsablePageSize(uint cb)
00071 {
00072     cbUsablePerPage = cb;
00073 }
00074 
00075 PConstBuffer Segment::getReadableFooter(CachePage &page)
00076 {
00077     return page.getReadableData() + getUsablePageSize();
00078 }
00079 
00080 PBuffer Segment::getWritableFooter(CachePage &page)
00081 {
00082     return page.getWritableData() + getUsablePageSize();
00083 }
00084 
00085 PageId Segment::getLinearPageSuccessor(PageId pageId)
00086 {
00087     assert(isPageIdAllocated(pageId));
00088     ++pageId;
00089     if (!isPageIdAllocated(pageId)) {
00090         return NULL_PAGE_ID;
00091     }
00092     return pageId;
00093 }
00094 
00095 void Segment::setLinearPageSuccessor(PageId pageId,PageId successorId)
00096 {
00097     assert(isPageIdAllocated(pageId));
00098     assert(isPageIdAllocated(successorId));
00099     assert(getLinearBlockNum(successorId)
00100            == getLinearBlockNum(pageId) + 1);
00101 }
00102 
00103 bool Segment::isLinearPageIdAllocated(PageId pageId)
00104 {
00105     if (getLinearBlockNum(pageId) >= getAllocatedSizeInPages()) {
00106         return false;
00107     }
00108     return true;
00109 }
00110 
00111 void Segment::checkpoint(CheckpointType checkpointType)
00112 {
00113     // Note that we can't use getTracingSegment() here because that method
00114     // references the shared ptr associated with this segment, and the
00115     // shared segment may have already been freed during shutdown by the
00116     // time this method is called.
00117     SharedSegment sharedPtr = pTracingSegment.lock();
00118     if (sharedPtr && sharedPtr.get()) {
00119         delegatedCheckpoint(*(sharedPtr.get()),checkpointType);
00120     } else {
00121         delegatedCheckpoint(*this,checkpointType);
00122     }
00123 }
00124 
00125 void Segment::delegatedCheckpoint(
00126     Segment &delegatingSegment,
00127     CheckpointType checkpointType)
00128 {
00129     MappedPageListenerPredicate pagePredicate(delegatingSegment);
00130     pCache->checkpointPages(pagePredicate,checkpointType);
00131 }
00132 
00133 uint Segment::getFullPageSize() const
00134 {
00135     return pCache->getPageSize();
00136 }
00137 
00138 bool Segment::ensureAllocatedSize(BlockNum nPages)
00139 {
00140     while (getAllocatedSizeInPages() < nPages) {
00141         if (allocatePageId() == NULL_PAGE_ID) {
00142             return false;
00143         }
00144     }
00145     return true;
00146 }
00147 
00148 PageId Segment::updatePage(PageId pageId, bool needsTranslation)
00149 {
00150     return NULL_PAGE_ID;
00151 }
00152 
00153 MappedPageListener *Segment::getMappedPageListener(BlockId blockId)
00154 {
00155     return this;
00156 }
00157 
00158 bool Segment::isWriteVersioned()
00159 {
00160     return false;
00161 }
00162 
00163 void Segment::initForUse()
00164 {
00165 }
00166 
00167 // force references to some classes which aren't referenced elsewhere
00168 #ifdef __MSVC__
00169 class UnreferencedSegmentStructs
00170 {
00171     SegmentMap &segmentMap;
00172 };
00173 #endif
00174 
00175 FENNEL_END_CPPFILE("$Id: //open/dev/fennel/segment/Segment.cpp#15 $");
00176 
00177 // End Segment.cpp

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