StandardTypeDescriptor.cpp

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/tuple/StandardTypeDescriptor.cpp#13 $
00003 // Fennel is a library of data storage and processing components.
00004 // Copyright (C) 2005-2009 The Eigenbase Project
00005 // Copyright (C) 2003-2009 SQLstream, Inc.
00006 // Copyright (C) 2005-2009 LucidEra, Inc.
00007 // Portions Copyright (C) 1999-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 #include "fennel/common/CommonPreamble.h"
00025 #include "fennel/tuple/StandardTypeDescriptor.h"
00026 #include "fennel/common/DataVisitor.h"
00027 
00028 #include <limits>
00029 
00030 FENNEL_BEGIN_CPPFILE("$Id: //open/dev/fennel/tuple/StandardTypeDescriptor.cpp#13 $");
00031 
00032 // TODO:  move these
00033 
00034 StoredTypeDescriptor::~StoredTypeDescriptor()
00035 {
00036 }
00037 
00038 StoredTypeDescriptorFactory::~StoredTypeDescriptorFactory()
00039 {
00040 }
00041 
00042 template <class T,StandardTypeDescriptorOrdinal typeOrdinal>
00043 class NumericType : public StoredTypeDescriptor
00044 {
00045     virtual Ordinal getOrdinal() const
00046     {
00047         return typeOrdinal;
00048     }
00049 
00050     virtual uint getBitCount() const
00051     {
00052         return 0;
00053     }
00054 
00055     virtual uint getFixedByteCount() const
00056     {
00057         return sizeof(T);
00058     }
00059 
00060     virtual uint getMinByteCount(uint cbMaxWidth) const
00061     {
00062         assert(cbMaxWidth == sizeof(T));
00063         return cbMaxWidth;
00064     }
00065 
00066     virtual uint getAlignmentByteCount(uint cbWidth) const
00067     {
00068         return sizeof(T);
00069     }
00070 
00071     virtual void visitValue(
00072         DataVisitor &dataVisitor,
00073         void const *pData,
00074         TupleStorageByteLength cbData) const
00075     {
00076         T t = *static_cast<T const *>(pData);
00077         assert(cbData == sizeof(T));
00078         if (std::numeric_limits<T>::is_signed) {
00079             dataVisitor.visitSignedInt(t);
00080         } else {
00081             dataVisitor.visitUnsignedInt(t);
00082         }
00083     }
00084 
00085     virtual int compareValues(
00086         void const *pData1,
00087         TupleStorageByteLength cbData1,
00088         void const *pData2,
00089         TupleStorageByteLength cbData2) const
00090     {
00091         assert(cbData1 == sizeof(T));
00092         assert(cbData2 == sizeof(T));
00093         T t1 = *static_cast<T const *>(pData1);
00094         T t2 = *static_cast<T const *>(pData2);
00095         if (t1 < t2) {
00096             return -1;
00097         } else if (t1 > t2) {
00098             return 1;
00099         } else {
00100             return 0;
00101         }
00102     }
00103 };
00104 
00105 template<>
00106 void NumericType<double,STANDARD_TYPE_DOUBLE>::visitValue(
00107     DataVisitor &dataVisitor,
00108     void const *pData,
00109     TupleStorageByteLength cbData) const
00110 {
00111     double d = *static_cast<double const *>(pData);
00112     assert(cbData == sizeof(double));
00113     dataVisitor.visitDouble(d);
00114 }
00115 
00116 template<>
00117 void NumericType<float,STANDARD_TYPE_REAL>::visitValue(
00118     DataVisitor &dataVisitor,
00119     void const *pData,
00120     TupleStorageByteLength cbData) const
00121 {
00122     float d = *static_cast<float const *>(pData);
00123     assert(cbData == sizeof(float));
00124     dataVisitor.visitFloat(d);
00125 }
00126 
00127 template<>
00128 uint NumericType<bool,STANDARD_TYPE_BOOL>::getBitCount() const
00129 {
00130     return 1;
00131 }
00132 
00133 class CharType : public StoredTypeDescriptor
00134 {
00135     virtual Ordinal getOrdinal() const
00136     {
00137         return STANDARD_TYPE_CHAR;
00138     }
00139 
00140     virtual uint getBitCount() const
00141     {
00142         return 0;
00143     }
00144 
00145     virtual uint getFixedByteCount() const
00146     {
00147         return 0;
00148     }
00149 
00150     virtual uint getMinByteCount(uint cbMaxWidth) const
00151     {
00152         return cbMaxWidth;
00153     }
00154 
00155     virtual uint getAlignmentByteCount(uint cbWidth) const
00156     {
00157         return 1;
00158     }
00159 
00160     virtual void visitValue(
00161         DataVisitor &dataVisitor,
00162         void const *pData,
00163         TupleStorageByteLength cbData) const
00164     {
00165         char const *pStr = static_cast<char const *>(pData);
00166         dataVisitor.visitChars(pStr,cbData);
00167     }
00168 
00169     virtual int compareValues(
00170         void const *pData1,
00171         TupleStorageByteLength cbData1,
00172         void const *pData2,
00173         TupleStorageByteLength cbData2) const
00174     {
00175         assert(cbData1 == cbData2);
00176         // REVIEW jvs:  should be using strncmp here and below?
00177         return memcmp(pData1,pData2,cbData1);
00178     }
00179 };
00180 
00181 class UnicodeCharType : public StoredTypeDescriptor
00182 {
00183     virtual Ordinal getOrdinal() const
00184     {
00185         return STANDARD_TYPE_UNICODE_CHAR;
00186     }
00187 
00188     virtual uint getBitCount() const
00189     {
00190         return 0;
00191     }
00192 
00193     virtual uint getFixedByteCount() const
00194     {
00195         return 0;
00196     }
00197 
00198     virtual uint getMinByteCount(uint cbMaxWidth) const
00199     {
00200         return cbMaxWidth;
00201     }
00202 
00203     virtual uint getAlignmentByteCount(uint cbWidth) const
00204     {
00205         return 2;
00206     }
00207 
00208     virtual void visitValue(
00209         DataVisitor &dataVisitor,
00210         void const *pData,
00211         TupleStorageByteLength cbData) const
00212     {
00213         assert((cbData & 1) == 0);
00214         Ucs2ConstBuffer pStr = static_cast<Ucs2ConstBuffer>(pData);
00215         dataVisitor.visitUnicodeChars(pStr,(cbData >> 1));
00216     }
00217 
00218     virtual int compareValues(
00219         void const *pData1,
00220         TupleStorageByteLength cbData1,
00221         void const *pData2,
00222         TupleStorageByteLength cbData2) const
00223     {
00224         assert(cbData1 == cbData2);
00225         assert((cbData1 & 1) == 0);
00226         Ucs2ConstBuffer pStr1 = static_cast<Ucs2ConstBuffer>(pData1);
00227         Ucs2ConstBuffer pStr2 = static_cast<Ucs2ConstBuffer>(pData2);
00228         uint nChars = (cbData1 >> 1);
00229         int c = compareStrings(pStr1, pStr2, nChars);
00230         return c;
00231     }
00232 
00233 public:
00234     static inline int compareStrings(
00235         Ucs2ConstBuffer pStr1, Ucs2ConstBuffer pStr2, uint nChars)
00236     {
00237         for (uint i = 0; i < nChars; ++i) {
00238             int c = *pStr1;
00239             c -= *pStr2;
00240             if (c) {
00241                 return c;
00242             }
00243             ++pStr1;
00244             ++pStr2;
00245         }
00246         return 0;
00247     }
00248 };
00249 
00250 class VarCharType : public StoredTypeDescriptor
00251 {
00252     virtual Ordinal getOrdinal() const
00253     {
00254         return STANDARD_TYPE_VARCHAR;
00255     }
00256 
00257     virtual uint getBitCount() const
00258     {
00259         return 0;
00260     }
00261 
00262     virtual uint getFixedByteCount() const
00263     {
00264         return 0;
00265     }
00266 
00267     virtual uint getMinByteCount(uint cbMaxWidth) const
00268     {
00269         return 0;
00270     }
00271 
00272     virtual uint getAlignmentByteCount(uint cbWidth) const
00273     {
00274         return 1;
00275     }
00276 
00277     virtual void visitValue(
00278         DataVisitor &dataVisitor,
00279         void const *pData,
00280         TupleStorageByteLength cbData) const
00281     {
00282         char const *pStr = static_cast<char const *>(pData);
00283         dataVisitor.visitChars(pStr,cbData);
00284     }
00285 
00286     virtual int compareValues(
00287         void const *pData1,
00288         TupleStorageByteLength cbData1,
00289         void const *pData2,
00290         TupleStorageByteLength cbData2) const
00291     {
00292         TupleStorageByteLength cbMin = std::min(cbData1,cbData2);
00293         int rc = memcmp(pData1, pData2, cbMin);
00294         if (rc) {
00295             return rc;
00296         }
00297         if (cbData1 == cbData2) {
00298             return 0;
00299         }
00300         PConstBuffer pBuf1 = static_cast<PConstBuffer>(pData1);
00301         PConstBuffer pBuf2 = static_cast<PConstBuffer>(pData2);
00302         PConstBuffer trailStart,trailEnd;
00303         if (cbData1 > cbData2) {
00304             trailStart = pBuf1 + cbMin;
00305             trailEnd = pBuf1 + cbData1;
00306             rc = 1;
00307         } else {
00308             trailStart = pBuf2 + cbMin;
00309             trailEnd = pBuf2 + cbData2;
00310             rc = -1;
00311         }
00312         for (; trailStart < trailEnd; trailStart++) {
00313             if (*trailStart != ' ') {
00314                 return rc;
00315             }
00316         }
00317         return 0;
00318     }
00319 };
00320 
00321 class UnicodeVarCharType : public StoredTypeDescriptor
00322 {
00323     virtual Ordinal getOrdinal() const
00324     {
00325         return STANDARD_TYPE_UNICODE_VARCHAR;
00326     }
00327 
00328     virtual uint getBitCount() const
00329     {
00330         return 0;
00331     }
00332 
00333     virtual uint getFixedByteCount() const
00334     {
00335         return 0;
00336     }
00337 
00338     virtual uint getMinByteCount(uint cbMaxWidth) const
00339     {
00340         return 0;
00341     }
00342 
00343     virtual uint getAlignmentByteCount(uint cbWidth) const
00344     {
00345         return 2;
00346     }
00347 
00348     virtual void visitValue(
00349         DataVisitor &dataVisitor,
00350         void const *pData,
00351         TupleStorageByteLength cbData) const
00352     {
00353         assert((cbData & 1) == 0);
00354         Ucs2ConstBuffer pStr = static_cast<Ucs2ConstBuffer>(pData);
00355         dataVisitor.visitUnicodeChars(pStr,(cbData >> 1));
00356     }
00357 
00358     virtual int compareValues(
00359         void const *pData1,
00360         TupleStorageByteLength cbData1,
00361         void const *pData2,
00362         TupleStorageByteLength cbData2) const
00363     {
00364         assert((cbData1 & 1) == 0);
00365         assert((cbData2 & 1) == 0);
00366         Ucs2ConstBuffer pStr1 = static_cast<Ucs2ConstBuffer>(pData1);
00367         Ucs2ConstBuffer pStr2 = static_cast<Ucs2ConstBuffer>(pData2);
00368         TupleStorageByteLength cbMin = std::min(cbData1,cbData2);
00369         uint nCharsMin = (cbMin >> 1);
00370         int rc = UnicodeCharType::compareStrings(pStr1, pStr2, nCharsMin);
00371         if (rc) {
00372             return rc;
00373         }
00374         if (cbData1 == cbData2) {
00375             return 0;
00376         }
00377         Ucs2ConstBuffer trailStart,trailEnd;
00378         if (cbData1 > cbData2) {
00379             trailStart = pStr1 + nCharsMin;
00380             trailEnd = pStr1 + (cbData1 >> 1);
00381             rc = 1;
00382         } else {
00383             trailStart = pStr2 + nCharsMin;
00384             trailEnd = pStr2 + (cbData2 >> 1);
00385             rc = -1;
00386         }
00387         for (; trailStart < trailEnd; trailStart++) {
00388             if (*trailStart != ' ') {
00389                 return rc;
00390             }
00391         }
00392         return 0;
00393     }
00394 };
00395 
00396 class BinaryType : public StoredTypeDescriptor
00397 {
00398     virtual Ordinal getOrdinal() const
00399     {
00400         return STANDARD_TYPE_BINARY;
00401     }
00402 
00403     virtual uint getBitCount() const
00404     {
00405         return 0;
00406     }
00407 
00408     virtual uint getFixedByteCount() const
00409     {
00410         return 0;
00411     }
00412 
00413     virtual uint getMinByteCount(uint cbMaxWidth) const
00414     {
00415         return cbMaxWidth;
00416     }
00417 
00418     virtual uint getAlignmentByteCount(uint cbWidth) const
00419     {
00420         return 1;
00421     }
00422 
00423     virtual void visitValue(
00424         DataVisitor &dataVisitor,
00425         void const *pData,
00426         TupleStorageByteLength cbData) const
00427     {
00428         dataVisitor.visitBytes(pData,cbData);
00429     }
00430 
00431     virtual int compareValues(
00432         void const *pData1,
00433         TupleStorageByteLength cbData1,
00434         void const *pData2,
00435         TupleStorageByteLength cbData2) const
00436     {
00437         assert(cbData1 == cbData2);
00438         return memcmp(pData1,pData2,cbData1);
00439     }
00440 };
00441 
00442 class VarBinaryType : public StoredTypeDescriptor
00443 {
00444     virtual Ordinal getOrdinal() const
00445     {
00446         return STANDARD_TYPE_VARBINARY;
00447     }
00448 
00449     virtual uint getBitCount() const
00450     {
00451         return 0;
00452     }
00453 
00454     virtual uint getFixedByteCount() const
00455     {
00456         return 0;
00457     }
00458 
00459     virtual uint getMinByteCount(uint cbMaxWidth) const
00460     {
00461         return 0;
00462     }
00463 
00464     virtual uint getAlignmentByteCount(uint cbWidth) const
00465     {
00466         return 1;
00467     }
00468 
00469     virtual void visitValue(
00470         DataVisitor &dataVisitor,
00471         void const *pData,
00472         TupleStorageByteLength cbData) const
00473     {
00474         dataVisitor.visitBytes(pData,cbData);
00475     }
00476 
00477     virtual int compareValues(
00478         void const *pData1,
00479         TupleStorageByteLength cbData1,
00480         void const *pData2,
00481         TupleStorageByteLength cbData2) const
00482     {
00483         TupleStorageByteLength cbMin = std::min(cbData1,cbData2);
00484         int rc = memcmp(pData1, pData2, cbMin);
00485         if (rc) {
00486             return rc;
00487         }
00488         if (cbData1 == cbData2) {
00489             return 0;
00490         }
00491         if (cbData1 > cbData2) {
00492             return 1;
00493         } else {
00494             return -1;
00495         }
00496     }
00497 };
00498 
00499 static NumericType<int8_t,STANDARD_TYPE_INT_8> stdINT_8;
00500 static NumericType<uint8_t,STANDARD_TYPE_UINT_8> stdUINT_8;
00501 static NumericType<int16_t,STANDARD_TYPE_INT_16> stdINT_16;
00502 static NumericType<uint16_t,STANDARD_TYPE_UINT_16> stdUINT_16;
00503 static NumericType<int32_t,STANDARD_TYPE_INT_32> stdINT_32;
00504 static NumericType<uint32_t,STANDARD_TYPE_UINT_32> stdUINT_32;
00505 static NumericType<int64_t,STANDARD_TYPE_INT_64> stdINT_64;
00506 static NumericType<uint64_t,STANDARD_TYPE_UINT_64> stdUINT_64;
00507 static NumericType<float,STANDARD_TYPE_REAL> stdREAL;
00508 static NumericType<double,STANDARD_TYPE_DOUBLE> stdDOUBLE;
00509 static NumericType<bool,STANDARD_TYPE_BOOL> stdBOOL;
00510 static CharType stdCHAR;
00511 static VarCharType stdVARCHAR;
00512 static BinaryType stdBINARY;
00513 static VarBinaryType stdVARBINARY;
00514 static UnicodeCharType stdUNICODE_CHAR;
00515 static UnicodeVarCharType stdUNICODE_VARCHAR;
00516 
00524 static StoredTypeDescriptor const *standardTypes[] = {
00525     NULL,                       // for 0
00526     &stdINT_8,
00527     &stdUINT_8,
00528     &stdINT_16,
00529     &stdUINT_16,
00530     &stdINT_32,
00531     &stdUINT_32,
00532     &stdINT_64,
00533     &stdUINT_64,
00534     &stdBOOL,
00535     &stdREAL,
00536     &stdDOUBLE,
00537     &stdCHAR,
00538     &stdVARCHAR,
00539     &stdBINARY,
00540     &stdVARBINARY,
00541     &stdUNICODE_CHAR,
00542     &stdUNICODE_VARCHAR,
00543 };
00544 
00545 StandardTypeDescriptorFactory::StandardTypeDescriptorFactory()
00546 {
00547 }
00548 
00549 StoredTypeDescriptor const &StandardTypeDescriptorFactory::newDataType(
00550     StoredTypeDescriptor::Ordinal iTypeOrdinal) const
00551 {
00552     return *(standardTypes[iTypeOrdinal]);
00553 }
00554 
00555 FENNEL_END_CPPFILE("$Id: //open/dev/fennel/tuple/StandardTypeDescriptor.cpp#13 $");
00556 
00557 // End StandardTypeDescriptor.cpp

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