00001 /* 00002 // $Id: //open/dev/fennel/segment/SegStreamAllocation.cpp#5 $ 00003 // Fennel is a library of data storage and processing components. 00004 // Copyright (C) 2006-2009 The Eigenbase Project 00005 // Copyright (C) 2006-2009 SQLstream, Inc. 00006 // Copyright (C) 2006-2009 LucidEra, Inc. 00007 // 00008 // This program is free software; you can redistribute it and/or modify it 00009 // under the terms of the GNU General Public License as published by the Free 00010 // Software Foundation; either version 2 of the License, or (at your option) 00011 // any later version approved by The Eigenbase Project. 00012 // 00013 // This program is distributed in the hope that it will be useful, 00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 // GNU General Public License for more details. 00017 // 00018 // You should have received a copy of the GNU General Public License 00019 // along with this program; if not, write to the Free Software 00020 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00021 */ 00022 00023 #include "fennel/common/CommonPreamble.h" 00024 #include "fennel/segment/SegStreamAllocation.h" 00025 #include "fennel/segment/SegInputStream.h" 00026 #include "fennel/segment/SegOutputStream.h" 00027 00028 FENNEL_BEGIN_CPPFILE("$Id: //open/dev/fennel/segment/SegStreamAllocation.cpp#5 $"); 00029 00030 SharedSegStreamAllocation SegStreamAllocation::newSegStreamAllocation() 00031 { 00032 return SharedSegStreamAllocation( 00033 new SegStreamAllocation(), 00034 ClosableObjectDestructor()); 00035 } 00036 00037 SegStreamAllocation::SegStreamAllocation() 00038 { 00039 nPagesWritten = 0; 00040 } 00041 00042 void SegStreamAllocation::beginWrite(SharedSegOutputStream pSegOutputStreamInit) 00043 { 00044 // go from state UNALLOCATED 00045 assert(!pSegOutputStream); 00046 assert(!pSegInputStream); 00047 00048 // to state WRITING 00049 needsClose = true; 00050 pSegOutputStream = pSegOutputStreamInit; 00051 } 00052 00053 void SegStreamAllocation::endWrite() 00054 { 00055 // from state WRITING 00056 assert(pSegOutputStream); 00057 00058 nPagesWritten = pSegOutputStream->getPageCount(); 00059 00060 SegmentAccessor segmentAccessor = pSegOutputStream->getSegmentAccessor(); 00061 PageId firstPageId = pSegOutputStream->getFirstPageId(); 00062 pSegOutputStream->close(); 00063 pSegOutputStream.reset(); 00064 if (firstPageId == NULL_PAGE_ID) { 00065 // go directly to UNALLOCATED 00066 return; 00067 } 00068 00069 // go to state READING 00070 pSegInputStream = SegInputStream::newSegInputStream( 00071 segmentAccessor, 00072 firstPageId); 00073 pSegInputStream->setDeallocate(true); 00074 } 00075 00076 SharedSegInputStream const &SegStreamAllocation::getInputStream() const 00077 { 00078 return pSegInputStream; 00079 } 00080 00081 void SegStreamAllocation::closeImpl() 00082 { 00083 if (pSegOutputStream) { 00084 // state WRITING 00085 assert(!pSegInputStream); 00086 // do a fake read; this won't really read the pages, it will just 00087 // deallocate them 00088 endWrite(); 00089 } 00090 00091 if (pSegInputStream) { 00092 // state READING 00093 assert(!pSegOutputStream); 00094 assert(pSegInputStream->isDeallocating()); 00095 // this will deallocate all remaining pages as a side-effect 00096 pSegInputStream->close(); 00097 pSegInputStream.reset(); 00098 } else { 00099 // state UNALLOCATED: nothing to do 00100 } 00101 00102 nPagesWritten = 0; 00103 } 00104 00105 BlockNum SegStreamAllocation::getWrittenPageCount() const 00106 { 00107 if (pSegOutputStream) { 00108 return pSegOutputStream->getPageCount(); 00109 } else { 00110 return nPagesWritten; 00111 } 00112 } 00113 00114 FENNEL_END_CPPFILE("$Id: //open/dev/fennel/segment/SegStreamAllocation.cpp#5 $"); 00115 00116 // End SegStreamAllocation.cpp