LbmSegmentReaderBase.cpp

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/lucidera/bitmap/LbmSegmentReaderBase.cpp#9 $
00003 // Fennel is a library of data storage and processing components.
00004 // Copyright (C) 2006-2009 LucidEra, Inc.
00005 // Copyright (C) 2006-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/exec/ExecStreamBufAccessor.h"
00024 #include "fennel/lucidera/bitmap/LbmSegmentReader.h"
00025 #include "fennel/lucidera/bitmap/LbmTupleReader.h"
00026 
00027 FENNEL_BEGIN_CPPFILE("$Id: //open/dev/fennel/lucidera/bitmap/LbmSegmentReaderBase.cpp#9 $");
00028 
00029 void LbmSegmentReaderBase::init(
00030     SharedExecStreamBufAccessor &pInAccessorInit,
00031     TupleData &bitmapSegTuple)
00032 {
00033     LbmStreamTupleReader *pNewReader = new LbmStreamTupleReader();
00034     pNewReader->init(pInAccessorInit, bitmapSegTuple);
00035     SharedLbmTupleReader pTupleReader(pNewReader);
00036     init(pTupleReader, bitmapSegTuple, false, NULL);
00037 }
00038 
00039 void LbmSegmentReaderBase::init(
00040     SharedExecStreamBufAccessor &pInAccessorInit,
00041     TupleData &bitmapSegTuple,
00042     bool setBitmapInit,
00043     boost::dynamic_bitset<> *pBitmapInit)
00044 {
00045     LbmStreamTupleReader *pNewReader = new LbmStreamTupleReader();
00046     pNewReader->init(pInAccessorInit, bitmapSegTuple);
00047     SharedLbmTupleReader pTupleReader(pNewReader);
00048     init(pTupleReader, bitmapSegTuple, setBitmapInit, pBitmapInit);
00049 }
00050 
00051 void LbmSegmentReaderBase::init(
00052     SharedLbmTupleReader &pTupleReaderInit,
00053     TupleData &bitmapSegTuple)
00054 {
00055     init(pTupleReaderInit, bitmapSegTuple, false, NULL);
00056 }
00057 
00058 void LbmSegmentReaderBase::init(
00059     SharedLbmTupleReader &pTupleReaderInit,
00060     TupleData &bitmapSegTuple,
00061     bool setBitmapInit,
00062     boost::dynamic_bitset<> *pBitmapInit)
00063 {
00064     pTupleReader = pTupleReaderInit;
00065     pBitmapSegTuple = &bitmapSegTuple;
00066     iSrid = bitmapSegTuple.size() - 3;
00067     iSegmentDesc = iSrid + 1;
00068     iSegments = iSrid + 2;
00069     byteSegLen = 0;
00070     byteSegOffset = LbmByteNumber(0);
00071     pSegStart = NULL;
00072     pSegDescStart = NULL;
00073     pSegDescEnd = NULL;
00074     zeroBytes = 0;
00075     tupleChange = false;
00076     setBitmap = setBitmapInit;
00077     pBitmap = pBitmapInit;
00078     maxRidSet = LcsRid(0);
00079 }
00080 
00081 ExecStreamResult LbmSegmentReaderBase::readBitmapSegTuple()
00082 {
00083     ExecStreamResult rc = pTupleReader->read(pBitmapSegTuple);
00084     if (rc != EXECRC_YIELD) {
00085         return rc;
00086     }
00087 
00088     // extract starting rid and compute its equivalent byte segment number
00089     startRID = *reinterpret_cast<LcsRid const *>
00090         ((*pBitmapSegTuple)[iSrid].pData);
00091     byteSegOffset = ridToByteNumber(startRID);
00092     zeroBytes = 0;
00093 
00094     // determine where the segment descriptor starts and ends, if there is
00095     // one
00096     pSegDescStart = (PBuffer) (*pBitmapSegTuple)[iSegmentDesc].pData;
00097     // descriptor can be NULL
00098     if (pSegDescStart != NULL) {
00099         pSegDescEnd = pSegDescStart + (*pBitmapSegTuple)[iSegmentDesc].cbData;
00100     } else {
00101         pSegDescEnd = NULL;
00102     }
00103 
00104     // determine where the bitmap segment starts and its length
00105     if ((*pBitmapSegTuple)[iSegments].pData) {
00106         // note that bit segment is stored backwards
00107         byteSegLen = (*pBitmapSegTuple)[iSegments].cbData;
00108         pSegStart = (PBuffer)
00109             ((*pBitmapSegTuple)[iSegments].pData + byteSegLen - 1);
00110     } else {
00111         // singletons do not have a corresponding bitmap, so create one
00112         byteSegLen = 1;
00113         pSegStart = &singleton;
00114         singleton = (uint8_t)(1 << (opaqueToInt(startRID) % LbmOneByteSize));
00115         if (setBitmap) {
00116             pBitmap->set(opaqueToInt(startRID % pBitmap->size()));
00117             if (startRID > maxRidSet) {
00118                 maxRidSet = startRID;
00119             }
00120         }
00121     }
00122 
00123     if (!pSegDescStart) {
00124         // For bitmaps without a descriptor, set the bits in the segment.
00125         // Bitmaps with descriptors will be handled as we advance through
00126         // each segment within the bitmap.
00127         setBitsRead(startRID, pSegStart, byteSegLen);
00128     }
00129 
00130     tupleChange = true;
00131     return EXECRC_YIELD;
00132 }
00133 
00134 void LbmSegmentReaderBase::setBitsRead(
00135     LcsRid startRid,
00136     PBuffer segStart,
00137     uint segLen)
00138 {
00139     if (setBitmap) {
00140         uint bitmapSize = pBitmap->size();
00141         PBuffer seg = segStart;
00142         LcsRid rid = startRid;
00143         for (uint i = 0; i < segLen; i++) {
00144             uint8_t byte = *(uint8_t *) seg;
00145             for (uint j = 0; j < LbmOneByteSize; j++) {
00146                 if (byte & 1) {
00147                     pBitmap->set(opaqueToInt(rid % bitmapSize));
00148                     if (rid > maxRidSet) {
00149                         maxRidSet = rid;
00150                     }
00151                 }
00152                 byte = byte >> 1;
00153                 rid++;
00154             }
00155             seg--;
00156         }
00157     }
00158 }
00159 
00160 void LbmSegmentReaderBase::advanceSegment()
00161 {
00162     // first, advance byte segment offset and segment pointer by the
00163     // length of the remaining part of the previous segment and the
00164     // trailing zero bytes
00165     byteSegOffset += byteSegLen + zeroBytes;
00166     pSegStart -= byteSegLen;
00167 
00168     // then, read the segment descriptor to determine where the segment
00169     // starts and its length; also advance the segment descriptor to the
00170     // next descriptor
00171     readSegDescAndAdvance(pSegDescStart, byteSegLen, zeroBytes);
00172 
00173     // keep track of the bits in the segment that we just advanced to
00174     setBitsRead(byteNumberToRid(byteSegOffset), pSegStart, byteSegLen);
00175 }
00176 
00177 bool LbmSegmentReaderBase::getTupleChange()
00178 {
00179     return tupleChange;
00180 }
00181 
00182 void LbmSegmentReaderBase::resetChangeListener()
00183 {
00184     tupleChange = false;
00185 }
00186 
00187 LcsRid LbmSegmentReaderBase::getMaxRidSet()
00188 {
00189     return maxRidSet;
00190 }
00191 
00192 FENNEL_END_CPPFILE("$Id: //open/dev/fennel/lucidera/bitmap/LbmSegmentReaderBase.cpp#9 $");
00193 
00194 // End LbmSegmentReaderBase.cpp

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