ExecStreamGenerator.h

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/test/ExecStreamGenerator.h#26 $
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) 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_ExecStreamGenerator_Included
00025 #define Fennel_ExecStreamGenerator_Included
00026 
00027 #include "fennel/exec/MockProducerExecStream.h"
00028 #include "fennel/exec/DynamicParam.h"
00029 #include <boost/shared_ptr.hpp>
00030 #include <algorithm>
00031 #include <numeric>
00032 
00033 FENNEL_BEGIN_NAMESPACE
00034 
00035 using boost::shared_ptr;
00036 using std::vector;
00037 
00045 class RampExecStreamGenerator : public MockProducerExecStreamGenerator
00046 {
00047 protected:
00048     int offset;
00049     int factor;
00050 public:
00051     RampExecStreamGenerator(int offsetInit, int factorInit) {
00052         offset = offsetInit;
00053         factor = factorInit;
00054     }
00055 
00056     RampExecStreamGenerator(int offsetInit) {
00057         offset = offsetInit;
00058         factor = 1;
00059     }
00060 
00061     RampExecStreamGenerator() {
00062         offset = 0;
00063         factor = 1;
00064     }
00065 
00066     virtual int64_t generateValue(uint iRow, uint iCol)
00067     {
00068         return iRow * factor + offset;
00069     }
00070 };
00071 
00075 class PermutationGenerator : public MockProducerExecStreamGenerator
00076 {
00077     std::vector<int64_t> values;
00078 
00079 public:
00080     explicit PermutationGenerator(uint nRows)
00081     {
00082         values.resize(nRows);
00083         std::iota(values.begin(), values.end(), 0);
00084         std::random_shuffle(values.begin(), values.end());
00085     }
00086 
00087     virtual int64_t generateValue(uint iRow, uint iCol)
00088     {
00089         // iCol ignored
00090         return values[iRow];
00091     }
00092 };
00093 
00103 class StairCaseExecStreamGenerator : public MockProducerExecStreamGenerator
00104 {
00105     int s;
00106     int h;
00107     int w;
00108 public:
00109     StairCaseExecStreamGenerator(int height, uint width, int start = 0) :
00110         s(start),
00111         h(height),
00112         w(width)
00113     {
00114         // empty
00115     }
00116 
00117     virtual int64_t generateValue(uint iRow, uint iCol)
00118     {
00119         return s + h * (iRow / w);
00120     }
00121 };
00122 
00128 class DynamicParamExecStreamGenerator : public MockProducerExecStreamGenerator
00129 {
00130     DynamicParamId dynamicParamId;
00131     SharedDynamicParamManager paramManager;
00132 
00133 public:
00134     DynamicParamExecStreamGenerator(
00135         DynamicParamId dynamicParamId_,
00136         SharedDynamicParamManager paramManager_)
00137         : dynamicParamId(dynamicParamId_),
00138           paramManager(paramManager_)
00139     {
00140         // empty
00141     }
00142 
00143     virtual int64_t generateValue(uint iRow, uint iCol)
00144     {
00145         int64_t value = *reinterpret_cast<int64_t const *>(
00146             paramManager->getParam(dynamicParamId).getDatum().pData);
00147         return value;
00148     }
00149 };
00150 
00155 class RandomColumnGenerator : public ColumnGenerator<int64_t>
00156 {
00157     std::subtractive_rng rng;
00158     int max;
00159 
00160 public:
00161     RandomColumnGenerator(int max) : rng(42), max(max)
00162         {}
00163 
00164     int64_t next()
00165     {
00166         return rng(max);
00167     }
00168 };
00169 
00188 template <class T = int64_t>
00189 class PoissonColumnGenerator : public ColumnGenerator<T>
00190 {
00191     T currentValue;
00193     vector<T> nextValues;
00195     int ordinalInBatch;
00197     T batchUpper;
00198     std::subtractive_rng rng;
00199     double meanDistance;
00200 
00201 public:
00202     explicit PoissonColumnGenerator(
00203         T startValue,
00204         double meanDistance,
00205         int batchSize,
00206         uint seed) : rng(seed)
00207     {
00208         assert(batchSize > 0);
00209         assert(meanDistance > 0);
00210         assert(meanDistance * batchSize >= 1);
00211         this->batchUpper = startValue;
00212         nextValues.resize(batchSize);
00213         this->ordinalInBatch = batchSize;
00214         this->meanDistance = meanDistance;
00215     }
00216 
00217     virtual ~PoissonColumnGenerator()
00218         {}
00219 
00220     T next()
00221     {
00222         if (ordinalInBatch >= nextValues.size()) {
00223             generateBatch();
00224         }
00225         return nextValues[ordinalInBatch++];
00226     }
00227 
00228 private:
00230     void generateBatch() {
00231         // The next batch will contain nextValues.size() values with a mean
00232         // inter-value distance of meanDistance, hence its values will range
00233         // from batchLower to batchLower + nextValues.size() * meanDistance.
00234         T batchLower = this->batchUpper;
00235         int batchRange = (int) (meanDistance * nextValues.size());
00236         T batchUpper = batchLower + batchRange;
00237         assert(batchUpper > batchLower);
00238         for (int i = 0; i < nextValues.size(); i++) {
00239             nextValues[i] = batchLower + static_cast<T>(rng(batchRange));
00240         }
00241         std::sort(nextValues.begin(), nextValues.end());
00242         this->batchUpper = batchUpper;
00243         this->ordinalInBatch = 0;
00244     }
00245 };
00246 
00247 
00254 class CompositeExecStreamGenerator : public MockProducerExecStreamGenerator
00255 {
00256     vector<boost::shared_ptr<ColumnGenerator<int64_t> > > generators;
00257     uint currentRow;
00258     uint currentCol;
00259 
00260 public:
00261     explicit CompositeExecStreamGenerator(
00262         vector<shared_ptr<ColumnGenerator<int64_t> > > const &generatorsInit)
00263         : generators(generatorsInit)
00264     {
00265         currentRow = uint(-1);
00266         currentCol = columnCount() - 1;
00267     }
00268 
00269     virtual int64_t generateValue(uint iRow, uint iCol)
00270     {
00271         // Check that access is sequential.
00272         if (iCol == 0) {
00273             assert(iRow == currentRow + 1);
00274             assert(currentCol == columnCount() - 1);
00275         } else {
00276             assert(iRow == currentRow);
00277             assert(iCol == currentCol + 1);
00278         }
00279         currentRow = iRow;
00280         currentCol = iCol;
00281 
00282         return generators[iCol]->next();
00283     }
00284 
00285 private:
00286     uint columnCount()
00287     {
00288         return generators.size();
00289     }
00290 };
00291 
00300 class RampDuplicateExecStreamGenerator : public MockProducerExecStreamGenerator
00301 {
00302 
00303 public:
00304     virtual int64_t generateValue(uint iRow, uint iCol)
00305     {
00306         return iRow / 2;
00307     }
00308 };
00309 
00310 
00311 class ConstExecStreamGenerator : public MockProducerExecStreamGenerator
00312 {
00313     uint constVal;
00314 
00315 public:
00316     ConstExecStreamGenerator (uint constValInit)
00317     {
00318         constVal = constValInit;
00319     }
00320 
00321     virtual int64_t generateValue(uint iRow, uint iCol)
00322     {
00323         return constVal;
00324     }
00325 };
00326 
00331 class SeqColumnGenerator : public ColumnGenerator<int64_t>
00332 {
00333     int offset;
00334     int curr;
00335 
00336 public:
00337     explicit SeqColumnGenerator()
00338     {
00339         offset = 1;
00340         curr = -1;
00341     }
00342     explicit SeqColumnGenerator(int startInit)
00343     {
00344         offset = 1;
00345         curr = startInit - 1;
00346     }
00347 
00348     explicit SeqColumnGenerator(int startInit, int offsetInit)
00349     {
00350         offset = offsetInit;
00351         curr = startInit - offset;
00352     }
00353 
00354     int64_t next()
00355     {
00356         curr += offset;
00357         return curr;
00358     }
00359 };
00360 
00361 
00370 class ConstColumnGenerator : public ColumnGenerator<int64_t>
00371 {
00372     int64_t constvalue;
00373 
00374 public:
00375     explicit ConstColumnGenerator(int constInit) {
00376         constvalue = constInit;
00377     }
00378 
00379     int64_t next()
00380     {
00381         return constvalue;
00382     }
00383 };
00384 
00391 class DupColumnGenerator : public ColumnGenerator<int64_t>
00392 {
00393     int numDups;
00394     int64_t curValue;
00395 
00396 public:
00397     explicit DupColumnGenerator(int numDupsInit, int startValue = 0) {
00398         assert(numDupsInit > 0);
00399         numDups = numDupsInit;
00400         curValue = startValue * numDups;
00401     }
00402 
00403     int64_t next()
00404     {
00405         return (curValue++ / numDups);
00406     }
00407 };
00408 
00416 class DupRepeatingSeqColumnGenerator : public ColumnGenerator<int64_t>
00417 {
00418     int numDups;
00419     int numSequence;
00420     int64_t curValue;
00421 
00422 public:
00423     explicit DupRepeatingSeqColumnGenerator(
00424         int numSequenceInit,
00425         int numDupsInit)
00426     {
00427         assert(numSequenceInit > 0);
00428         assert(numDupsInit > 0);
00429         numSequence = numSequenceInit;
00430         numDups = numDupsInit;
00431         curValue = 0;
00432     }
00433 
00434     int64_t next()
00435     {
00436         return (curValue++ % (numDups * numSequence)) / numDups;
00437     }
00438 };
00439 
00447 class RepeatingSeqColumnGenerator : public ColumnGenerator<int64_t>
00448 {
00449     int nSequence;
00450     int64_t curValue;
00451 
00452 public:
00453     explicit RepeatingSeqColumnGenerator(int nSequenceInit) {
00454         assert(nSequenceInit > 0);
00455         nSequence = nSequenceInit;
00456         curValue = 0;
00457     }
00458 
00459     int64_t next()
00460     {
00461         return curValue++ % nSequence;
00462     }
00463 };
00464 
00473 class MixedDupColumnGenerator : public ColumnGenerator<int64_t>
00474 {
00475     int numDups;
00476     int64_t curValue;
00477     int width;
00478     int nextValue;
00479     int initialValue;
00480 
00481 public:
00482     explicit MixedDupColumnGenerator(
00483         int numDupsInit,
00484         int startValue = 0,
00485         int wid = 1)
00486     {
00487         assert(numDupsInit > 0);
00488         numDups = numDupsInit;
00489         curValue = 0;
00490         width = wid;
00491         initialValue = nextValue = startValue;
00492     }
00493 
00494     int64_t next()
00495     {
00496         int res;
00497 
00498         if ((((nextValue - initialValue) / width) % 2)) {
00499             res = nextValue + curValue++ / numDups;
00500             if (curValue == numDups) {
00501                 curValue = 0;
00502                 nextValue++;
00503             }
00504         } else {
00505             res = nextValue++;
00506         }
00507 
00508         return res;
00509     }
00510 };
00511 
00515 class StairCaseColumnGenerator : public ColumnGenerator<int64_t>
00516 {
00517     int s;
00518     int h;
00519     int w;
00520     uint iRow;
00521 
00522 public:
00523     StairCaseColumnGenerator(int height, uint width, int start = 0) :
00524         s(start),
00525         h(height),
00526         w(width),
00527         iRow(0)
00528     {
00529         // empty
00530     }
00531 
00532     int64_t next()
00533     {
00534         return s + h * (iRow++ / w);
00535     }
00536 };
00537 
00538 FENNEL_END_NAMESPACE
00539 
00540 #endif
00541 
00542 // End ExecStreamGenerator.h

Generated on Mon Jun 22 04:00:20 2009 for Fennel by  doxygen 1.5.1