LbmSortedAggExecStreamTest.cpp

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/lucidera/test/LbmSortedAggExecStreamTest.cpp#6 $
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 #include "fennel/common/CommonPreamble.h"
00023 #include "fennel/lucidera/test/LbmExecStreamTestBase.h"
00024 #include "fennel/lucidera/bitmap/LbmByteSegment.h"
00025 #include "fennel/lucidera/bitmap/LbmSortedAggExecStream.h"
00026 #include <stdarg.h>
00027 
00028 #include <boost/test/test_tools.hpp>
00029 
00030 using namespace fennel;
00031 
00035 class LbmSortedAggExecStreamTest : public LbmExecStreamTestBase
00036 {
00037 protected:
00058     void testSortedAgg(
00059         uint nRows,
00060         uint nKeys,
00061         std::vector<int> const &repeatSeqValues);
00062 
00063 public:
00064     explicit LbmSortedAggExecStreamTest()
00065     {
00066         FENNEL_UNIT_TEST_CASE(LbmSortedAggExecStreamTest, testScanFullKey);
00067         FENNEL_UNIT_TEST_CASE(LbmSortedAggExecStreamTest, testScanPartKey);
00068     }
00069 
00070     void testScanFullKey();
00071     void testScanPartKey();
00072 };
00073 
00077 uint getValueCount(uint nRows, uint interval, uint value)
00078 {
00079     uint nCopies = nRows / interval;
00080     if (value < nRows % interval) {
00081         nCopies++;
00082     }
00083     return nCopies;
00084 }
00085 
00094 class SortedAggExecStreamGenerator : public MockProducerExecStreamGenerator
00095 {
00096 protected:
00097     uint nRows;
00098     uint nKeys;
00099     std::vector<int> keyRepeats;
00100     uint interval;
00101     boost::shared_array<uint> sortedToUnsortedMap;
00102     int current;
00103     boost::shared_array<uint> currentRow;
00104 
00105 public:
00106     SortedAggExecStreamGenerator(
00107         uint nRows, uint nKeys, std::vector<int> repeatSeqValues)
00108     {
00109         this->nRows = nRows;
00110         this->nKeys = nKeys;
00111         for (uint i = 0; i < nKeys; i++) {
00112             keyRepeats.push_back(repeatSeqValues[i]);
00113         }
00114         interval = LbmExecStreamTestBase::getTupleInterval(keyRepeats);
00115 
00116         sortedToUnsortedMap.reset(new uint[interval]);
00117         for (uint i = 0; i < interval; i++) {
00118             uint value = 0;
00119             uint scale = 1;
00120             // calculate sorted position (backwards)
00121             // value = key0 * scale_1_to_n + key1 * scale_2_to_n + ...
00122             for (int j = nKeys - 1; j >= 0; j--) {
00123                 uint key = i % keyRepeats[j];
00124                 value += key * scale;
00125                 scale *= keyRepeats[j];
00126             }
00127             sortedToUnsortedMap[value] = i;
00128         }
00129         current = -1;
00130         currentRow.reset(new uint[nKeys + 1]);
00131     }
00132 
00133     virtual int64_t generateValue(uint iRow, uint iCol)
00134     {
00135         assert (iRow < interval);
00136         assert (iCol < nKeys + 1);
00137 
00138         if (iRow != current) {
00139             current = iRow;
00140             uint unsorted = sortedToUnsortedMap[current];
00141             for (uint i = 0; i < nKeys; i++) {
00142                 currentRow[i] = unsorted % keyRepeats[i];
00143             }
00144             currentRow[nKeys] = getValueCount(nRows, interval, unsorted);
00145         }
00146         return currentRow[iCol];
00147     }
00148 };
00149 
00150 void LbmSortedAggExecStreamTest::testScanFullKey()
00151 {
00152     std::vector<int> repeatSeqValues;
00153     repeatSeqValues.push_back(1);
00154     repeatSeqValues.push_back(5);
00155     repeatSeqValues.push_back(9);
00156 
00157     testSortedAgg(1000, 3, repeatSeqValues);
00158 }
00159 
00160 void LbmSortedAggExecStreamTest::testScanPartKey()
00161 {
00162     std::vector<int> repeatSeqValues;
00163     repeatSeqValues.push_back(13);
00164     repeatSeqValues.push_back(5);
00165     repeatSeqValues.push_back(9);
00166 
00167     testSortedAgg(1000, 2, repeatSeqValues);
00168 }
00169 
00170 void LbmSortedAggExecStreamTest::testSortedAgg(
00171     uint nRows,
00172     uint nKeys,
00173     std::vector<int> const &repeatSeqValues)
00174 {
00175     initKeyBitmap(nRows, repeatSeqValues);
00176 
00177     // test normalizer against the input
00178     ValuesExecStreamParams valuesParams;
00179     valuesParams.outputTupleDesc = keyBitmapTupleDesc;
00180     valuesParams.pTupleBuffer = keyBitmapBuf;
00181     valuesParams.bufSize = keyBitmapBufSize;
00182 
00183     ExecStreamEmbryo valuesStreamEmbryo;
00184     valuesStreamEmbryo.init(new ValuesExecStream(), valuesParams);
00185     valuesStreamEmbryo.getStream()->setName("ValuesExecStream");
00186 
00187     // build -> sorter -> agg transforms
00188     std::vector<ExecStreamEmbryo> transformEmbryos;
00189 
00190     ExternalSortExecStreamParams sortParams;
00191     ExecStreamEmbryo sortEmbryo;
00192     initSorterExecStream(sortParams, sortEmbryo, keyBitmapTupleDesc, nKeys);
00193     transformEmbryos.push_back(sortEmbryo);
00194 
00195     ExecStreamEmbryo aggEmbryo;
00196     LbmSortedAggExecStreamParams aggParams;
00197     aggParams.groupByKeyCount = nKeys;
00198     AggInvocation countStar;
00199     countStar.aggFunction = AGG_FUNC_COUNT;
00200     countStar.iInputAttr = -1;
00201     aggParams.aggInvocations.push_back(countStar);
00202 
00203     TupleProjection keyProj;
00204     for (int i = 0; i < nKeys; i++) {
00205         keyProj.push_back(i);
00206     }
00207     TupleDescriptor aggDesc;
00208     aggDesc.projectFrom(keyBitmapTupleDesc, keyProj);
00209     aggDesc.push_back(attrDesc_int64);
00210     aggParams.outputTupleDesc = aggDesc;
00211 
00212     aggEmbryo.init(new LbmSortedAggExecStream(), aggParams);
00213     aggEmbryo.getStream()->setName("SortedAgg");
00214     transformEmbryos.push_back(aggEmbryo);
00215 
00216     SharedExecStream pOutputStream = prepareTransformGraph(
00217         valuesStreamEmbryo, transformEmbryos);
00218     SortedAggExecStreamGenerator verifier(nRows, nKeys, repeatSeqValues);
00219     verifyOutput(
00220         *pOutputStream,
00221         getTupleInterval(repeatSeqValues, nKeys),
00222         verifier);
00223 }
00224 
00225 FENNEL_UNIT_TEST_SUITE(LbmSortedAggExecStreamTest);
00226 
00227 // End LbmSortedAggExecStreamTest.cpp

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