00001 /* 00002 // $Id: //open/dev/fennel/sorter/ExternalSortMerger.h#2 $ 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 #ifndef Fennel_ExternalSortMerger_Included 00025 #define Fennel_ExternalSortMerger_Included 00026 00027 #include "fennel/sorter/ExternalSortSubStream.h" 00028 #include "fennel/tuple/TupleAccessor.h" 00029 #include "fennel/tuple/TupleData.h" 00030 #include "fennel/tuple/TupleProjectionAccessor.h" 00031 00032 #include <boost/scoped_ptr.hpp> 00033 #include <boost/scoped_array.hpp> 00034 00035 #include <vector> 00036 00037 FENNEL_BEGIN_NAMESPACE 00038 00042 struct ExternalSortMergeInfo 00043 { 00044 PBuffer val; 00045 uint runOrd; 00046 00047 explicit ExternalSortMergeInfo() 00048 { 00049 val = NULL; 00050 runOrd = 0; 00051 } 00052 }; 00053 00054 class ExternalSortInfo; 00055 class ExternalSortRunAccessor; 00056 00057 typedef boost::shared_ptr<ExternalSortRunAccessor> 00058 SharedExternalSortRunAccessor; 00059 00063 class FENNEL_SORTER_EXPORT ExternalSortMerger 00064 : public ExternalSortSubStream 00065 { 00069 ExternalSortInfo &sortInfo; 00070 00074 boost::scoped_array<SharedExternalSortRunAccessor> ppRunAccessors; 00075 00079 boost::scoped_array<ExternalSortFetchArray *> ppFetchArrays; 00080 00084 boost::scoped_array<uint> pOrds; 00085 00089 uint nMergeMemPages; 00090 00094 uint nRuns; 00095 00099 boost::scoped_array<ExternalSortMergeInfo> mergeInfo; 00100 00105 ExternalSortFetchArray fetchArray; 00106 00111 PBuffer ppTupleBuffers[EXTSORT_FETCH_ARRAY_SIZE]; 00112 00113 // TODO: comment or replace 00114 TupleAccessor tupleAccessor; 00115 TupleAccessor tupleAccessor2; 00116 TupleProjectionAccessor keyAccessor; 00117 TupleProjectionAccessor keyAccessor2; 00118 TupleData keyData; 00119 TupleData keyData2; 00120 00121 // ---------------------------------------------------------------------- 00122 // private methods 00123 // ---------------------------------------------------------------------- 00124 00125 inline uint heapParent(uint i); 00126 inline uint heapLeft(uint i); 00127 inline uint heapRight(uint i); 00128 inline void heapExchange(uint i,uint j); 00129 void heapify(uint i); 00130 void heapBuild(); 00131 00132 // TODO: doc 00133 ExternalSortRC checkFetch(); 00134 inline ExternalSortMergeInfo &getMergeHigh(); 00135 00136 public: 00137 explicit ExternalSortMerger(ExternalSortInfo &info); 00138 virtual ~ExternalSortMerger(); 00139 00143 void initRunAccess(); 00144 00152 void startMerge( 00153 std::vector<SharedSegStreamAllocation>::iterator pStoredRun, 00154 uint nRunsToMerge); 00155 00159 void releaseResources(); 00160 00161 // implement ExternalSortSubStream 00162 virtual ExternalSortFetchArray &bindFetchArray(); 00163 virtual ExternalSortRC fetch(uint nTuplesRequested); 00164 }; 00165 00166 FENNEL_END_NAMESPACE 00167 00168 #endif 00169 00170 // End ExternalSortMerger.h