00001 /* 00002 // $Id: //open/dev/fennel/sorter/ExternalSortOutput.cpp#1 $ 00003 // Fennel is a library of data storage and processing components. 00004 // Copyright (C) 2005-2009 The Eigenbase Project 00005 // Copyright (C) 2009-2009 SQLstream, Inc. 00006 // Copyright (C) 2004-2009 LucidEra, Inc. 00007 // Portions Copyright (C) 2004-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/sorter/ExternalSortOutput.h" 00026 #include "fennel/sorter/ExternalSortInfo.h" 00027 #include "fennel/common/ByteOutputStream.h" 00028 #include "fennel/exec/ExecStreamBufAccessor.h" 00029 00030 FENNEL_BEGIN_CPPFILE("$Id: //open/dev/fennel/sorter/ExternalSortOutput.cpp#1 $"); 00031 00032 ExternalSortOutput::ExternalSortOutput(ExternalSortInfo &sortInfoIn) 00033 : sortInfo(sortInfoIn) 00034 { 00035 pSubStream = NULL; 00036 pFetchArray = NULL; 00037 iCurrentTuple = 0; 00038 00039 tupleAccessor.compute(sortInfo.tupleDesc); 00040 } 00041 00042 ExternalSortOutput::~ExternalSortOutput() 00043 { 00044 releaseResources(); 00045 } 00046 00047 void ExternalSortOutput::releaseResources() 00048 { 00049 } 00050 00051 void ExternalSortOutput::setSubStream(ExternalSortSubStream &subStream) 00052 { 00053 iCurrentTuple = 0; 00054 00055 pSubStream = &subStream; 00056 pFetchArray = &(subStream.bindFetchArray()); 00057 } 00058 00059 ExecStreamResult ExternalSortOutput::fetch( 00060 ExecStreamBufAccessor &bufAccessor) 00061 { 00062 uint cbRemaining = bufAccessor.getProductionAvailable(); 00063 PBuffer pOutBuf = bufAccessor.getProductionStart(); 00064 PBuffer pNextTuple = pOutBuf; 00065 00066 for (;;) { 00067 if (iCurrentTuple >= pFetchArray->nTuples) { 00068 ExternalSortRC rc = pSubStream->fetch(EXTSORT_FETCH_ARRAY_SIZE); 00069 if (rc == EXTSORT_ENDOFDATA) { 00070 goto done; 00071 } 00072 iCurrentTuple = 0; 00073 } 00074 00075 while (iCurrentTuple < pFetchArray->nTuples) { 00076 PConstBuffer pSrcTuple = 00077 pFetchArray->ppTupleBuffers[iCurrentTuple]; 00078 uint cbTuple = tupleAccessor.getBufferByteCount(pSrcTuple); 00079 if (cbTuple > cbRemaining) { 00080 if (pNextTuple == pOutBuf) { 00081 bufAccessor.requestConsumption(); 00082 return EXECRC_BUF_OVERFLOW; 00083 } 00084 goto done; 00085 } 00086 memcpy(pNextTuple,pSrcTuple,cbTuple); 00087 cbRemaining -= cbTuple; 00088 pNextTuple += cbTuple; 00089 iCurrentTuple++; 00090 } 00091 } 00092 00093 done: 00094 if (pNextTuple == pOutBuf) { 00095 bufAccessor.markEOS(); 00096 return EXECRC_EOS; 00097 } else { 00098 bufAccessor.produceData(pNextTuple); 00099 bufAccessor.requestConsumption(); 00100 // REVIEW: sometimes should be EXECRC_EOS instead 00101 return EXECRC_BUF_OVERFLOW; 00102 } 00103 } 00104 00105 FENNEL_END_CPPFILE("$Id: //open/dev/fennel/sorter/ExternalSortOutput.cpp#1 $"); 00106 00107 // End ExternalSortOutput.cpp