tuple.cpp

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/calctest/tuple.cpp#3 $
00003 // Fennel is a library of data storage and processing components.
00004 // Copyright (C) 2004-2009 The Eigenbase Project
00005 // Copyright (C) 2004-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 // Test Calculator object directly by instantiating instruction objects,
00023 // creating programs, running them, and checking the register set values.
00024 */
00025 #include "fennel/common/CommonPreamble.h"
00026 #include "fennel/tuple/TupleDescriptor.h"
00027 #include "fennel/tuple/TupleData.h"
00028 #include "fennel/tuple/TupleAccessor.h"
00029 #include "fennel/tuple/TuplePrinter.h"
00030 #include "fennel/tuple/AttributeAccessor.h"
00031 #include "fennel/tuple/StandardTypeDescriptor.h"
00032 #include "fennel/common/TraceSource.h"
00033 
00034 #include <boost/scoped_array.hpp>
00035 #include <boost/test/unit_test_suite.hpp>
00036 #include <limits>
00037 #include <iostream.h>
00038 
00039 using namespace fennel;
00040 
00041 int const bufferlen = 8;
00042 int const num = 5;
00043 
00044 void
00045 tupleFiddle()
00046 {
00047     bool isNullable = true;    // Can tuple contain nulls?
00048     int i;
00049 
00050     TupleDescriptor tupleDesc;
00051     tupleDesc.clear();
00052 
00053     // Build up a description of what we'd like the tuple to look like
00054     StandardTypeDescriptorFactory typeFactory;
00055     for (i = 0; i < num; i++) {
00056         StoredTypeDescriptor const &typeDesc =
00057             typeFactory.newDataType(STANDARD_TYPE_VARCHAR);
00058         tupleDesc.push_back(
00059             TupleAttributeDescriptor(
00060                 typeDesc,
00061                 isNullable,
00062                 bufferlen));
00063     }
00064     for (i = 0; i < num; i++) {
00065         StoredTypeDescriptor const &typeDesc =
00066             typeFactory.newDataType(STANDARD_TYPE_INT_32);
00067         tupleDesc.push_back(TupleAttributeDescriptor(typeDesc, isNullable));
00068     }
00069     for (i = 0; i < num; i++) {
00070         StoredTypeDescriptor const &typeDesc =
00071             typeFactory.newDataType(STANDARD_TYPE_UINT_8);
00072         tupleDesc.push_back(TupleAttributeDescriptor(typeDesc, isNullable));
00073     }
00074     for (i = 0; i < num; i++) {
00075         StoredTypeDescriptor const &typeDesc =
00076             typeFactory.newDataType(STANDARD_TYPE_REAL);
00077         tupleDesc.push_back(TupleAttributeDescriptor(typeDesc, isNullable));
00078     }
00079 
00080     // Create a tuple accessor from the description
00081     //
00082     // Note: Must use an ALL_FIXED accessor when creating a tuple out of
00083     // the air like this, otherwise unmarshal() does not know what to
00084     // do. If you need a STANDARD type tuple with variable-lengty
00085     // fields, it has to be built as a copy.
00086     TupleAccessor tupleAccessorFixed;
00087     tupleAccessorFixed.compute(tupleDesc, TUPLE_FORMAT_ALL_FIXED);
00088 
00089     // Allocate memory for the tuple
00090     boost::scoped_array<FixedBuffer>
00091         pTupleBufFixed(new FixedBuffer[tupleAccessorFixed.getMaxByteCount()]);
00092 
00093     // Link memory to accessor
00094     tupleAccessorFixed.setCurrentTupleBuf(pTupleBufFixed.get(), false);
00095 
00096     // Create a vector of TupleDatum objects based on the description we built
00097     TupleData tupleDataFixed(tupleDesc);
00098 
00099     // Do something mysterious. Probably binding pointers in the accessor to
00100     // items in the TupleData vector
00101     tupleAccessorFixed.unmarshal(tupleDataFixed);
00102 
00103     TupleData::iterator itr = tupleDataFixed.begin();
00104 
00105     TupleDatum pDatum;
00106     PBuffer pData;
00107 
00108     for (i = 0; i < num; i++, itr++) {
00109         char buf[bufferlen * 10];
00110         sprintf(buf,"%d-A-%d-B-%d-C-", i, i, i); // longer than buflen
00111         strncpy(
00112             (reinterpret_cast<char *>(const_cast<PBuffer>(itr->pData))),
00113             buf, bufferlen);
00114     }
00115     for (i = 0; i < num; i++, itr++) {
00116         // exploded form
00117         pDatum = *itr;
00118         pData = const_cast<PBuffer>(pDatum.pData);
00119         *(reinterpret_cast<int32_t *>(pData)) = i;
00120     }
00121     for (i = 0; i < num; i++, itr++) {
00122         // condensed form
00123         *(reinterpret_cast<uint8_t *>(const_cast<PBuffer>(itr->pData))) = i;
00124     }
00125     for (i = 0; i < num; i++, itr++) {
00126         *(reinterpret_cast<float *>(const_cast<PBuffer>(itr->pData))) = i * 0.5;
00127     }
00128 
00129     // Print out the tuple
00130     TuplePrinter tuplePrinter;
00131     tuplePrinter.print(cout, tupleDesc, tupleDataFixed);
00132     cout << endl;
00133 
00134     // Create another TupleData object that will be nullable
00135     TupleData tupleDataNullable = tupleDataFixed;
00136 
00137     // null out last element of each type
00138     for (i = 1; i <= 3; i++) {
00139         tupleDataNullable[(i*num)-1].pData = NULL;
00140     }
00141 
00142     // Print out the nullable tuple
00143     tuplePrinter.print(cout, tupleDesc, tupleDataNullable);
00144     cout << endl;
00145 
00146     // Re-point second string to first string
00147     tupleDataNullable[1].pData = tupleDataNullable[0].pData;
00148 
00149     // Re-point third string to part way into first string
00150     tupleDataNullable[2].pData = (tupleDataNullable[0].pData + 1);
00151 
00152     // Print out the modified nullable tuple
00153     tuplePrinter.print(cout, tupleDesc, tupleDataNullable);
00154     cout << endl;
00155 
00156 }
00157 
00158 int
00159 main(int argc, char *argv[])
00160 {
00161     tupleFiddle();
00162     return 0;
00163 }
00164 
00165 boost::unit_test_framework::test_suite *init_unit_test_suite(int,char **)
00166 {
00167     return NULL;
00168 }
00169 
00170 // End tuple.cpp

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