00001 /* 00002 // $Id: //open/dev/fennel/segment/SegOutputStream.cpp#8 $ 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/SegOutputStream.h" 00026 00027 FENNEL_BEGIN_CPPFILE("$Id: //open/dev/fennel/segment/SegOutputStream.cpp#8 $"); 00028 00029 SharedSegOutputStream SegOutputStream::newSegOutputStream( 00030 SegmentAccessor const &segmentAccessor) 00031 { 00032 return SharedSegOutputStream( 00033 new SegOutputStream(segmentAccessor), 00034 ClosableObjectDestructor()); 00035 } 00036 00037 SegOutputStream::SegOutputStream( 00038 SegmentAccessor const &segmentAccessor, 00039 uint cbExtraHeader) 00040 : SegStream(segmentAccessor,cbExtraHeader) 00041 { 00042 firstPageId = NULL_PAGE_ID; 00043 lastPageId = NULL_PAGE_ID; 00044 nPagesAllocated = 0; 00045 cbMaxPageData = getSegment()->getUsablePageSize() - cbPageHeader; 00046 writeLatency = WRITE_LAZY; 00047 // force allocation of first page 00048 getWritePointer(1); 00049 } 00050 00051 PageId SegOutputStream::getFirstPageId() const 00052 { 00053 return firstPageId; 00054 } 00055 00056 BlockNum SegOutputStream::getPageCount() const 00057 { 00058 return nPagesAllocated; 00059 } 00060 00061 void SegOutputStream::updatePage() 00062 { 00063 if (!pageLock.isLocked()) { 00064 return; 00065 } 00066 SegStreamNode &node = pageLock.getNodeForWrite(); 00067 node.cbData = getBytesWrittenThisPage(); 00068 writeExtraHeaders(node); 00069 } 00070 00071 void SegOutputStream::writeExtraHeaders(SegStreamNode &) 00072 { 00073 } 00074 00075 void SegOutputStream::flushBuffer(uint cbRequested) 00076 { 00077 assert(cbRequested <= cbMaxPageData); 00078 updatePage(); 00079 if (pageLock.isLocked()) { 00080 if (writeLatency != WRITE_LAZY) { 00081 pageLock.getCacheAccessor()->flushPage( 00082 pageLock.getPage(), 00083 writeLatency == WRITE_EAGER_ASYNC); 00084 } 00085 pageLock.unlock(); 00086 } 00087 if (!cbRequested) { 00088 return; 00089 } 00090 PageId pageId = pageLock.allocatePage(); 00091 ++nPagesAllocated; 00092 if (firstPageId == NULL_PAGE_ID) { 00093 firstPageId = pageId; 00094 } else { 00095 getSegment()->setPageSuccessor(lastPageId,pageId); 00096 } 00097 lastPageId = pageId; 00098 SegStreamNode &node = pageLock.getNodeForWrite(); 00099 setBuffer( 00100 reinterpret_cast<PBuffer>(&node)+cbPageHeader, 00101 cbMaxPageData); 00102 } 00103 00104 void SegOutputStream::closeImpl() 00105 { 00106 ByteOutputStream::closeImpl(); 00107 SegStream::closeImpl(); 00108 } 00109 00110 uint SegOutputStream::getBytesWrittenThisPage() const 00111 { 00112 return cbMaxPageData - getBytesAvailable(); 00113 } 00114 00115 void SegOutputStream::getSegPos(SegStreamPosition &pos) 00116 { 00117 CompoundId::setPageId(pos.segByteId,lastPageId); 00118 if (getBytesAvailable()) { 00119 CompoundId::setByteOffset(pos.segByteId,getBytesWrittenThisPage()); 00120 } else { 00121 // after a hard page break, use a special sentinel value to indicate 00122 // the last byte on the page 00123 CompoundId::setByteOffset( 00124 pos.segByteId,CompoundId::MAX_BYTE_OFFSET); 00125 } 00126 pos.cbOffset = cbOffset; 00127 } 00128 00129 FENNEL_END_CPPFILE("$Id: //open/dev/fennel/segment/SegOutputStream.cpp#8 $"); 00130 00131 // End SegOutputStream.cpp