LbmExecStreamTestBase.h

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/lucidera/test/LbmExecStreamTestBase.h#10 $
00003 // Fennel is a library of data storage and processing components.
00004 // Copyright (C) 2005-2009 LucidEra, Inc.
00005 // Copyright (C) 2005-2009 The Eigenbase Project
00006 //
00007 // This program is free software; you can redistribute it and/or modify it
00008 // under the terms of the GNU General Public License as published by the Free
00009 // Software Foundation; either version 2 of the License, or (at your option)
00010 // any later version approved by The Eigenbase Project.
00011 //
00012 // This program is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with this program; if not, write to the Free Software
00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 */
00021 
00022 #ifndef Fennel_LbmExecStreamTestBase_Included
00023 #define Fennel_LbmExecStreamTestBase_Included
00024 
00025 #include "fennel/test/ExecStreamUnitTestBase.h"
00026 #include "fennel/tuple/StandardTypeDescriptor.h"
00027 #include "fennel/exec/ValuesExecStream.h"
00028 #include "fennel/exec/ExecStreamEmbryo.h"
00029 #include "fennel/lucidera/bitmap/LbmEntry.h"
00030 #include "fennel/lucidera/bitmap/LbmNormalizerExecStream.h"
00031 #include "fennel/sorter/ExternalSortExecStream.h"
00032 
00033 FENNEL_BEGIN_NAMESPACE
00034 
00039 struct InputData
00040 {
00044     uint bitmapSize;
00045 
00049     LcsRid startRid;
00050 
00054     uint skipRows;
00055 };
00056 
00061 struct BitmapInput
00062 {
00066     boost::shared_array<FixedBuffer> bufArray;
00067 
00071     uint currBufSize;
00072 
00076     uint fullBufSize;
00077 
00081     uint nBitmaps;
00082 };
00083 
00084 class NumberStream;
00085 typedef boost::shared_ptr<NumberStream> SharedNumberStream;
00086 
00091 class NumberStream
00092 {
00093 public:
00094     virtual ~NumberStream()
00095     {}
00096 
00097     // invalid/null value
00098     static const uint BIG_NUMBER = 0xffffffff;
00099 
00100     // clones this object
00101     virtual NumberStream *clone() = 0;
00102 
00103     // upper row count limit, used to allocate data buffer
00104     virtual uint getMaxRowCount(uint maxRid) = 0;
00105 
00106     // whether the stream has any more numbers
00107     virtual bool hasNext() = 0;
00108 
00109     // returns the next number
00110     virtual uint getNext() = 0;
00111 };
00112 
00116 class SkipNumberStream : public NumberStream
00117 {
00118     uint first, last, skip;
00119     uint prev, next;
00120 
00121 public:
00122     SkipNumberStream(uint first, uint last, uint skip)
00123     {
00124         this->first = first;
00125         this->last = last;
00126         this->skip = skip;
00127         next = first;
00128     }
00129 
00130     NumberStream *clone()
00131     {
00132         return new SkipNumberStream(first, last, skip);
00133     }
00134 
00135     uint getMaxRowCount(uint maxRid)
00136     {
00137         uint upperBound = std::min(maxRid, last);
00138         return ((upperBound - first) / skip) + 1;
00139     }
00140 
00141     bool hasNext()
00142     {
00143         return next <= last;
00144     }
00145 
00146     uint getNext()
00147     {
00148         uint value = next;
00149         next += skip;
00150         return value;
00151     }
00152 };
00153 
00157 class UnionNumberStream : public NumberStream
00158 {
00159     std::vector<SharedNumberStream> children;
00160     VectorOfUint currentValues;
00161     uint prev, next;
00162 
00163     bool findNext()
00164     {
00165         if (next != BIG_NUMBER) {
00166             return true;
00167         }
00168         // scan to values greater than prev
00169         for (uint i = 0; i < children.size(); i++) {
00170             if (currentValues[i] <= prev) {
00171                 if (children[i]->hasNext()) {
00172                     currentValues[i] = children[i]->getNext();
00173                 }
00174             }
00175         }
00176         // search for lowest value greater than prev
00177         for (uint i = 0; i < children.size(); i++) {
00178             if (currentValues[i] > prev && currentValues[i] < next) {
00179                 next = currentValues[i];
00180             }
00181         }
00182         return next != BIG_NUMBER;
00183     }
00184 
00185 public:
00186     UnionNumberStream()
00187     {
00188         prev = 0;
00189         next = BIG_NUMBER;
00190     }
00191 
00192     virtual ~UnionNumberStream()
00193     {}
00194 
00195     void addChild(SharedNumberStream pStream)
00196     {
00197         children.push_back(pStream);
00198         currentValues.push_back(0);
00199     }
00200 
00201     NumberStream *clone()
00202     {
00203         UnionNumberStream *pStream = new UnionNumberStream();
00204         for (uint i = 0; i < children.size(); i++) {
00205             SharedNumberStream pChild(children[i]->clone());
00206             pStream->addChild(pChild);
00207         }
00208         return pStream;
00209     }
00210 
00211     uint getMaxRowCount(uint maxRid)
00212     {
00213         uint total = 0;
00214         for (uint i = 0; i < children.size(); i++) {
00215             total += children[i]->getMaxRowCount(maxRid);
00216         }
00217         return total;
00218     }
00219 
00220     bool hasNext()
00221     {
00222         return findNext();
00223     }
00224 
00225     uint getNext()
00226     {
00227         bool hasNext = findNext();
00228         assert(hasNext);
00229         prev = next;
00230         next = BIG_NUMBER;
00231         return prev;
00232     }
00233 };
00234 
00238 struct LbmNumberStreamInput
00239 {
00240     SharedNumberStream pStream;
00241     uint bitmapSize;
00242 };
00243 
00244 class NumberStreamExecStreamGenerator : public MockProducerExecStreamGenerator
00245 {
00246 protected:
00247     SharedNumberStream pStream;
00248 public:
00249     NumberStreamExecStreamGenerator(SharedNumberStream pNumberStream)
00250     {
00251         pStream = SharedNumberStream(pNumberStream->clone());
00252     }
00253 
00254     virtual int64_t generateValue(uint iRow, uint iCol)
00255     {
00256         if (pStream->hasNext()) {
00257             return pStream->getNext();
00258         }
00259         return 0;
00260     }
00261 };
00262 
00263 static const std::string traceName = "net.sf.fennel.test.lbm";
00264 
00270 class LbmExecStreamTestBase : public ExecStreamUnitTestBase
00271 {
00272 protected:
00273     StandardTypeDescriptorFactory stdTypeFactory;
00274     TupleAttributeDescriptor attrDesc_int64;
00275     TupleAttributeDescriptor attrDesc_bitmap;
00276 
00280     uint bitmapColSize;
00281 
00286     TupleDescriptor bitmapTupleDesc;
00287     TupleData bitmapTupleData;
00288     TupleAccessor bitmapTupleAccessor;
00289 
00294     TupleDescriptor keyBitmapTupleDesc;
00295     TupleData keyBitmapTupleData;
00296     TupleAccessor keyBitmapTupleAccessor;
00297     boost::shared_array<FixedBuffer> keyBitmapBuf;
00298     uint keyBitmapBufSize;
00299 
00300     inline static const std::string &getTraceName()
00301     {
00302         return traceName;
00303     }
00304 
00305     // common, and specific functions for initializing inputs
00306     void initBitmapInput(
00307         BitmapInput &bmInput, uint nRows, InputData const &inputData);
00308     void initBitmapInput(
00309         BitmapInput &bmInput, uint nRows, LbmNumberStreamInput input);
00310 
00311     void generateBitmaps(
00312         uint nRows, LbmNumberStreamInput input, BitmapInput &bmInput);
00313 
00314     void produceEntry(
00315         LbmEntry &lbmEntry, TupleAccessor &bitmapTupleAccessor,
00316         BitmapInput &bmInput);
00317 
00318     void initValuesExecStream(
00319         uint idx, ValuesExecStreamParams &valuesParams,
00320         ExecStreamEmbryo &valuesStreamEmbryo, BitmapInput &bmInput);
00321 
00322     void initSorterExecStream(
00323         ExternalSortExecStreamParams &params,
00324         ExecStreamEmbryo &embryo,
00325         TupleDescriptor const &outputDesc,
00326         uint nKeys = 1);
00327 
00328     void initNormalizerExecStream(
00329         LbmNormalizerExecStreamParams &params,
00330         ExecStreamEmbryo &embryo,
00331         uint nKeys);
00332 
00339     uint resultBitmapSize(uint start, uint end)
00340     {
00341         return resultBitmapSize(end - start);
00342     }
00343 
00344     uint resultBitmapSize(uint nRids)
00345     {
00346         // the result bitmap should be large enough for all rids in range,
00347         // nRids/8, plus extra space that allows the segment builder some
00348         // breathing room to operate
00349         uint extraSpace = 16;
00350         return (nRids / 8) + extraSpace;
00351     }
00352 
00374     void generateBitmaps(
00375         uint nRows, uint start, uint skipRows, PBuffer pBuf, uint &bufSize,
00376         uint fullBufSize, uint &nBitmaps, bool includeKeys = false);
00377 
00378     void produceEntry(
00379         LbmEntry &lbmEntry, TupleAccessor &bitmapTupleAccessor, PBuffer pBuf,
00380         uint &bufSize, uint &nBitmaps, bool includeKeys);
00381 
00385     void initKeyBitmap(uint nRows, std::vector<int> const &repeatSeqValues);
00386 
00387 public:
00388     void testCaseSetUp();
00389 
00393     static uint getTupleInterval(
00394         std::vector<int> const &repeatSeqValues,
00395         uint nKeys = 0);
00396 };
00397 
00398 FENNEL_END_NAMESPACE
00399 
00400 #endif
00401 
00402 // End LbmExecStreamTestBase.h

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