CompoundId.h

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/common/CompoundId.h#11 $
00003 // Fennel is a library of data storage and processing components.
00004 // Copyright (C) 2005-2009 The Eigenbase Project
00005 // Copyright (C) 2005-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 #ifndef Fennel_CompoundId_Included
00025 #define Fennel_CompoundId_Included
00026 
00027 FENNEL_BEGIN_NAMESPACE
00028 
00029 // NOTE:  read the comment on struct StoredNode before
00030 // modifying the encoding below
00031 
00072 class FENNEL_COMMON_EXPORT CompoundId
00073 {
00074      // masks for extracting portions of a PageId, BlockId, or SegByteId
00075     static const uint64_t DEVICE_ID_MASK =   0xFFF0000000000000ULL;
00076     static const uint64_t BYTE_OFFSET_MASK = 0x000FFFFF00000000ULL;
00077     static const uint64_t BLOCK_NUM_MASK =   0x00000000FFFFFFFFULL;
00078 
00082     static const uint DEVICE_ID_SHIFT = 52;
00083 
00087     static const uint BYTE_OFFSET_SHIFT = 32;
00088 
00089 public:
00093     static const uint MAX_DEVICES = 0x1000;
00094 
00098     static const uint MAX_BYTE_OFFSET = 0xFFFFF;
00099 
00107     template <class PageOrBlockId>
00108     static BlockNum getBlockNum(PageOrBlockId pageId)
00109     {
00110         return (opaqueToInt(pageId) & BLOCK_NUM_MASK);
00111     }
00112 
00120     template <class PageOrBlockId>
00121     static void setBlockNum(PageOrBlockId &pageId,BlockNum blockNum)
00122     {
00123         assert(blockNum == (blockNum & BLOCK_NUM_MASK));
00124         pageId = PageOrBlockId(
00125             (opaqueToInt(pageId) & DEVICE_ID_MASK) | blockNum);
00126     }
00127 
00133     template <class PageOrBlockId>
00134     static void incBlockNum(PageOrBlockId &pageId)
00135     {
00136         setBlockNum(pageId,getBlockNum(pageId) + 1);
00137     }
00138 
00144     template <class PageOrBlockId>
00145     static void decBlockNum(PageOrBlockId &pageId)
00146     {
00147         setBlockNum(pageId,getBlockNum(pageId) - 1);
00148     }
00149 
00157     template <class PageOrBlockId>
00158     static DeviceId getDeviceId(PageOrBlockId pageId)
00159     {
00160         return DeviceId(
00161             (opaqueToInt(pageId) & DEVICE_ID_MASK) >> DEVICE_ID_SHIFT);
00162     }
00163 
00171     template <class PageOrBlockId>
00172     static void setDeviceId(PageOrBlockId &pageId,DeviceId deviceId)
00173     {
00174         assert(opaqueToInt(deviceId) < MAX_DEVICES);
00175         pageId = PageOrBlockId(
00176             (uint64_t(opaqueToInt(deviceId)) << DEVICE_ID_SHIFT)
00177             | getBlockNum(pageId));
00178     }
00179 
00187     static PageId getPageId(SegByteId segByteId)
00188     {
00189         return PageId(opaqueToInt(segByteId) & ~BYTE_OFFSET_MASK);
00190     }
00191 
00199     static void setPageId(SegByteId &segByteId,PageId pageId)
00200     {
00201         segByteId = SegByteId(
00202             opaqueToInt(pageId)
00203             | (opaqueToInt(segByteId) & BYTE_OFFSET_MASK));
00204     }
00205 
00213     static uint getByteOffset(SegByteId segByteId)
00214     {
00215         return (opaqueToInt(segByteId) & BYTE_OFFSET_MASK) >> BYTE_OFFSET_SHIFT;
00216     }
00217 
00225     static void setByteOffset(SegByteId &segByteId,uint offset)
00226     {
00227         assert(offset == (offset & (BYTE_OFFSET_MASK >> BYTE_OFFSET_SHIFT)));
00228         segByteId = SegByteId(
00229             opaqueToInt(getPageId(segByteId))
00230             | (SegByteIdPrimitive(offset) << BYTE_OFFSET_SHIFT));
00231     }
00232 
00243     static int comparePageIds(PageId p1,PageId p2)
00244     {
00245         return (p1 > p2) ? 1
00246             : ((p1 < p2) ? -1 : 0);
00247     }
00248 
00259     static int compareSegByteIds(SegByteId t1,SegByteId t2)
00260     {
00261         return (t1 > t2) ? 1
00262             : ((t1 < t2) ? -1 : 0);
00263     }
00264 
00269     static uint getMaxDeviceCount()
00270     {
00271         return MAX_DEVICES;
00272     }
00273 };
00274 
00275 FENNEL_END_NAMESPACE
00276 
00277 #endif
00278 
00279 // End CompoundId.h

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