00001 /* 00002 // $Id: //open/dev/fennel/calculator/WinAggHistogram.h#2 $ 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) 2009-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 // WinAggHistogram - Windowed Aggregation Histogram object. 00023 */ 00024 00025 #ifndef Fennel_WinAggHistogram_Included 00026 #define Fennel_WinAggHistogram_Included 00027 00028 #include "fennel/tuple/TupleDescriptor.h" 00029 #include "fennel/calculator/CalcCommon.h" 00030 #include "fennel/common/TraceSource.h" 00031 00032 #include <utility> 00033 #include <set> 00034 #include <deque> 00035 #include <list> 00036 00037 FENNEL_BEGIN_NAMESPACE 00038 00054 template <typename STDTYPE> 00055 class WinAggHistogram 00056 { 00057 00058 public: 00059 WinAggHistogram() 00060 : currentWindow(), 00061 nullRows(0), 00062 currentSum(0), 00063 queue() 00064 {} 00065 00066 ~WinAggHistogram() 00067 {} 00068 00069 typedef multiset<STDTYPE> WinAggData; 00070 00071 // REVIEW: jhyde, 2006/6/14: We don't need a double-ended queue. We only 00072 // add to the tail, and remove from the head. 00073 typedef list<STDTYPE> WinAggQueue; 00074 00079 // 00080 void addRow(RegisterRef<STDTYPE>* node) 00081 { 00082 // Add the new node to the histogram strucuture 00083 00084 if (!node->isNull()) { 00085 STDTYPE val = node->value(); 00086 (void) currentWindow.insert(val); 00087 currentSum += val; 00088 00089 // Add to the FIFO queue. 00090 queue.push_back(val); 00091 00092 } else { 00093 ++nullRows; 00094 } 00095 } 00096 00101 // 00102 void dropRow(RegisterRef<STDTYPE>* node) 00103 { 00104 if (!node->isNull()) { 00105 assert(0 != currentWindow.size()); 00106 STDTYPE* pData = node->refer(); 00107 00108 pair< 00109 typename WinAggData::iterator, 00110 typename WinAggData::iterator> entries = 00111 currentWindow.equal_range(*pData); 00112 00113 assert(entries.first != entries.second); 00114 if (entries.first != entries.second) { 00115 currentWindow.erase(entries.first); 00116 currentSum -= *pData; 00117 } 00118 00119 // Remove from the FIFO queue. 00120 queue.pop_front(); 00121 } else { 00122 assert(0 != nullRows); 00123 --nullRows; 00124 } 00125 } 00126 00130 void getMin(RegisterRef<STDTYPE>* node) 00131 { 00132 if (0 != currentWindow.size()) { 00133 node->value(*(currentWindow.begin())); 00134 } else { 00135 node->toNull(); 00136 } 00137 } 00138 00142 void getMax(RegisterRef<STDTYPE>* node) 00143 { 00144 if (0 != currentWindow.size()) { 00145 node->value(*(--(currentWindow.end()))); 00146 } else { 00147 node->toNull(); 00148 } 00149 } 00150 00154 void getSum(RegisterRef<STDTYPE>* node) 00155 { 00156 if (0 != currentWindow.size()) { 00157 node->value(currentSum); 00158 } else { 00159 node->toNull(); 00160 } 00161 } 00162 00166 void getCount(RegisterRef<int64_t>* node) 00167 { 00168 node->value(currentWindow.size() + nullRows); 00169 } 00170 00175 void getAvg(RegisterRef<STDTYPE>* node) 00176 { 00177 node->value( 00178 (0 != currentWindow.size()) ? 00179 (currentSum / static_cast<STDTYPE>(currentWindow.size())) : 00180 0); 00181 } 00182 00186 void getFirstValue(RegisterRef<STDTYPE>* node) 00187 { 00188 if (queue.empty()) { 00189 node->toNull(); 00190 } else { 00191 node->value(queue.front()); 00192 } 00193 } 00194 00198 void getLastValue(RegisterRef<STDTYPE>* node) 00199 { 00200 if (queue.empty()) { 00201 node->toNull(); 00202 } else { 00203 node->value(queue.back()); 00204 } 00205 } 00206 00207 private: 00208 00209 WinAggData currentWindow; // Holds the values currently in the window. 00210 int64_t nullRows; // Couunt of null entries 00211 00212 // REVIEW (jhyde, 2006/6/14): We need to support char datatypes, so it's 00213 // not appropriate that sum has the same type as the values in the 00214 // window. Maybe break histogram into a base class only min/max support, 00215 // and a derived class with sum. The sum type will be an extra template 00216 // parameter. 00217 00220 STDTYPE currentSum; 00221 00223 WinAggQueue queue; 00224 }; 00225 00226 FENNEL_END_NAMESPACE 00227 00228 #endif 00229 00230 // End WinAggHistogram.h