LbmEntry.h

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/lucidera/bitmap/LbmEntry.h#25 $
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_LbmEntry_Included
00023 #define Fennel_LbmEntry_Included
00024 
00025 #include "fennel/tuple/TupleData.h"
00026 #include "fennel/tuple/TupleDescriptor.h"
00027 #include "fennel/tuple/TupleAccessor.h"
00028 #include "fennel/lucidera/bitmap/LbmSegment.h"
00029 #include <algorithm>
00030 
00031 using namespace std;
00032 
00033 FENNEL_BEGIN_NAMESPACE
00034 
00041 class FENNEL_LBM_EXPORT LbmEntry
00042     : public LbmSegment
00043 {
00047     PBuffer scratchBuffer;
00048 
00052     PBuffer mergeScratchBuffer;
00053 
00058     uint scratchBufferSize;
00059 
00060     uint scratchBufferUsableSize;
00061 
00070     TupleData entryTuple;
00071 
00075     TupleDescriptor keyDesc;
00076 
00080     LcsRid currSegByteStartRID;
00081 
00088     uint currentEntrySize;
00089 
00093     uint keySize;
00094 
00098     uint bitmapSegSize;
00099 
00104     uint maxSegSize;
00105 
00109     PBuffer currSegDescByte;
00110 
00114     PBuffer currSegByte;
00115     uint currSegLength;
00116 
00123     inline bool isSingleton() const;
00124 
00130     inline bool isSingleBitmap() const;
00131 
00138     inline void setRIDSegByte(PBuffer pSegByte, LcsRid rid);
00139 
00145     inline void setRIDCurrentSegByte(LcsRid rid);
00146 
00154     bool setRIDAdjacentSegByte(LcsRid rid);
00155 
00164     bool setRIDNewSegment(LcsRid rid);
00165 
00169     inline void resetSegment();
00170 
00177     bool openNewSegment(LcsRid rid);
00178 
00182     void openLastSegment();
00183 
00189     void closeCurrentSegment();
00190 
00200     bool closeCurrentSegment(LcsRid rid);
00201 
00206     inline bool isSegmentOpen();
00207 
00208     /*
00209      * Get the last segment descriptor and calculate the number of rows
00210      * contained in the current entry, by going through
00211      * the segement descriptors from the start to the end.
00212      *
00213      * @param [out] lastSegDescByte the last SegDesc byte in this entry
00214      *
00215      * @param [out] lastZeroRIDs number of RIDs from rows not having this
00216      *  key value.
00217      *
00218      * @return number of rows encoded by this entry.
00219      */
00220     uint getRowCount(PBuffer &lastSegDescByte, uint &lastZeroRIDs);
00221 
00222     static uint getCompressedRowCount(
00223         PBuffer pDescStart, PBuffer pDescEnd,
00224         PBuffer &lastSegDescByte, uint &lastZeroRIDs);
00225 
00238     bool growEntry(LcsRid rid, uint reserveSpace);
00239 
00251     bool adjustEntry(TupleData &inputTuple);
00252 
00262     bool addSegDesc(uint reservedSpace, uint bitmapLength);
00263 
00267     bool singleton2Bitmap();
00268 
00277     bool spliceSingleton(TupleData &inputTuple);
00278 
00288     void copyToMergeBuffer(
00289         TupleData &newEntry, LcsRid startRid, PBuffer segStart,
00290         PBuffer segDescStart);
00291 
00314     bool addNewSegment(
00315         TupleData &inputTuple, LcsRid prevSrid, PBuffer prevSegDesc,
00316         PBuffer nextSegDesc, PBuffer prevSeg, uint prevSegBytes,
00317         uint prevZeroBytes);
00318 
00340     bool addNewMiddleSegment(
00341         TupleData &inputTuple, LcsRid prevSrid, PBuffer prevSegDesc,
00342         PBuffer nextSegDesc, PBuffer prevSeg, uint prevSegBytes,
00343         uint prevZeroBytes);
00344 
00368     bool addNewAdjacentSegment(
00369         TupleData &inputTuple, LcsRid prevSrid, PBuffer prevSegDesc,
00370         PBuffer nextSegDesc, PBuffer prevSeg, uint prevSegBytes,
00371         uint prevZeroBytes);
00372 
00383     bool setZeroLength(
00384         uint nZeroBytes, PBuffer pLenDesc, uint &lengthBytes);
00385 
00399     void addNewRid(
00400         PBuffer nextSegDesc, PBuffer newSeg, LcsRid newRid,
00401         uint remainingSegLen);
00402 
00410     void splitEntry(TupleData &inputTuple);
00411 
00419     void mergeIntoSplitEntry(
00420         TupleData &inputTuple, TupleData &splitEntry);
00421 
00435     int segmentContainsRid(
00436         LcsRid rid,
00437         LcsRid startRid,
00438         PBuffer pSeg,
00439         uint nSegBytes);
00440 
00448     uint getMergeSpaceRequired(TupleData const &inputTuple);
00449 
00454     void validateEntrySize();
00455 
00460     static const uint LbmMinEntryPerPage = 8;
00461 
00462     static const uint LbmSmallSingleBitmap = 256;
00463 
00471     inline static bool isSingleBitmap(TupleData const &inputTuple);
00472 
00476     static string toBitmapString(TupleData const &inputTuple);
00477 
00481     static string dumpSeg(PBuffer segDesc, PBuffer segDescEnd, PBuffer seg);
00482 
00486     static string dumpBitmap(PBuffer seg, uint segBytes);
00487 
00491     static string printDatum(
00492         TupleDatum const &tupleDatum,
00493         bool reverseByteOrder);
00494 
00498     static string printByte(uint8_t byte);
00499 
00503     static string toRIDString(TupleData const &inputTuple);
00504 
00508     static string dumpSegRID(
00509         PBuffer segDesc, PBuffer segDescEnd, PBuffer seg,
00510         string prefix, LcsRid srid);
00511 
00515     static string dumpBitmapRID(
00516         PBuffer seg, uint segBytes,
00517         string prefix, LcsRid srid);
00518 
00522     static void generateSegRIDs(
00523         PBuffer segDesc, PBuffer segDescEnd, PBuffer seg,
00524         vector<LcsRid> &ridValues, LcsRid srid);
00525 
00529     static void generateBitmapRIDs(
00530         PBuffer seg, uint segBytes, vector<LcsRid> &ridValues,
00531         LcsRid srid);
00532 
00533 public:
00534 
00538     explicit LbmEntry();
00539 
00540     ~LbmEntry() {}
00541 
00553     void init(
00554         PBuffer scratchBufferInit, PBuffer mergeScratchBufferInit,
00555         uint scratchBufferSizeInit, TupleDescriptor const &tupleDesc);
00556 
00569     void setEntryTuple(TupleData const &entryTuple);
00570 
00583     bool setRID(LcsRid rid);
00584 
00591     int compareEntry(
00592         TupleData const &inputTuple,
00593         TupleDescriptor const &tupleDesc) const;
00594 
00605     bool mergeEntry(TupleData &inputTuple);
00606 
00612     TupleData const &produceEntryTuple();
00613 
00617     string toString();
00618 
00624     uint getRowCount();
00625 
00633     static uint getRowCount(TupleData const &inputTuple);
00634 
00642     bool containsRid(LcsRid rid);
00643 
00652     bool inRange(LcsRid rid);
00653 
00665     static string toString(TupleData const &inputTuple, bool printRID = false);
00666 
00676     static void getSizeBounds(
00677         TupleDescriptor const &indexTupleDesc, uint pageSize,
00678         uint &minEntrySize, uint &maxEntrySize);
00679 
00687     static void generateRIDs(
00688         TupleData const &inputTuple, vector<LcsRid> &ridValues);
00689 
00698     static uint getScratchBufferSize(uint bitmapColSize);
00699 
00707     static uint getMaxBitmapSize(uint bitmapColSize);
00708 
00716     inline static bool isSingleton(TupleData const &inputTuple);
00717 
00727     inline static LcsRid getStartRid(
00728         TupleData const &tuple);
00729 };
00730 
00731 
00732 /**************************************************
00733   Definitions of inline methods for class LbmEntry
00734  **************************************************/
00735 
00736 inline bool LbmEntry::isSingleton(TupleData const &inputTuple)
00737 {
00738     return (inputTuple[inputTuple.size() - 2].isNull() &&
00739         inputTuple[inputTuple.size() - 1].isNull());
00740 }
00741 
00742 inline LcsRid LbmEntry::getStartRid(
00743     TupleData const &tuple)
00744 {
00745     return *reinterpret_cast<LcsRid const *> (tuple[tuple.size()-3].pData);
00746 }
00747 
00748 inline bool LbmEntry::isSingleton() const
00749 {
00750     return (pSegStart == pSegEnd);
00751 }
00752 
00753 
00754 inline bool LbmEntry::isSingleBitmap(TupleData const &inputTuple)
00755 {
00756     return (inputTuple[inputTuple.size() - 2].isNull() &&
00757         !inputTuple[inputTuple.size() - 1].isNull());
00758 }
00759 
00760 
00761 inline bool LbmEntry::isSingleBitmap() const
00762 {
00763     return (pSegDescStart == NULL);
00764 }
00765 
00766 inline void LbmEntry::setRIDSegByte(PBuffer pSegByte, LcsRid rid)
00767 {
00768     assert(pSegByte);
00769     *pSegByte |= (uint8_t)(1 << (opaqueToInt(rid) % LbmOneByteSize));
00770 }
00771 
00772 inline void LbmEntry::setRIDCurrentSegByte(LcsRid rid)
00773 {
00774     setRIDSegByte(currSegByte, rid);
00775 }
00776 
00777 inline bool LbmEntry::isSegmentOpen()
00778 {
00779     return (currSegDescByte != NULL);
00780 }
00781 
00782 inline void LbmEntry::resetSegment()
00783 {
00784     currSegByte = NULL;
00785     currSegLength = 0;
00786     currSegByteStartRID = (LcsRid)0;
00787     currSegDescByte = NULL;
00788 }
00789 
00790 FENNEL_END_NAMESPACE
00791 
00792 #endif
00793 
00794 // End LbmEntry.h

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