BTreeKeyedNodeAccessor.h

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/btree/BTreeKeyedNodeAccessor.h#16 $
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_BTreeKeyedNodeAccessor_Included
00025 #define Fennel_BTreeKeyedNodeAccessor_Included
00026 
00027 #include "fennel/btree/BTreeNodeAccessor.h"
00028 
00029 FENNEL_BEGIN_NAMESPACE
00030 
00031 // TODO:  more doc
00032 
00038 template <class NodeAccessor,class KeyAccessor>
00039 class BTreeKeyedNodeAccessor : public NodeAccessor
00040 {
00041 public:
00042     KeyAccessor *pKeyAccessor;
00043 
00044     virtual void accessTupleInline(BTreeNode const &node,uint iEntry)
00045     {
00046         assert(iEntry < node.nEntries);
00047         NodeAccessor::tupleAccessor.setCurrentTupleBuf(
00048             NodeAccessor::getEntryForReadInline(node,iEntry));
00049     }
00050 
00051     virtual void accessTuple(BTreeNode const &node,uint iEntry)
00052     {
00053         return accessTupleInline(node,iEntry);
00054     }
00055 
00056     virtual void unmarshalKey(TupleData &keyData)
00057     {
00058         pKeyAccessor->unmarshal(keyData);
00059     }
00060 
00061     virtual uint binarySearch(
00062         BTreeNode const &node,
00063         TupleDescriptor const &keyDescriptor,
00064         TupleData const &searchKey,
00065         DuplicateSeek dupSeek,
00066         bool leastUpper,
00067         TupleData &scratchKey,
00068         bool &found)
00069     {
00070         uint probe = 0;
00071         uint base = probe;
00072         found = false;
00073         int nKeys = NodeAccessor::getKeyCount(node);
00074         while (nKeys > 0) {
00075             uint split = nKeys >> 1;
00076             probe = base + split;
00077             accessTupleInline(node,probe);
00078             pKeyAccessor->unmarshal(scratchKey);
00079             int j = keyDescriptor.compareTuples(
00080                 searchKey,scratchKey);
00081             if (j == 0) {
00082                 found = true;
00083                 switch (dupSeek) {
00084                 case DUP_SEEK_ANY:
00085                     return probe;
00086                 case DUP_SEEK_BEGIN:
00087                     j = -1;
00088                     break;
00089                 case DUP_SEEK_END:
00090                     j = 1;
00091                     break;
00092                 default:
00093                     permAssert(false);
00094                 }
00095             }
00096             if (j < 0) {
00097                 nKeys = split;
00098             } else {
00099                 base = probe + 1;
00100                 nKeys -= (split + 1);
00101             }
00102         }
00103         if (!found && !leastUpper && (base > 0)) {
00104             base--;
00105         }
00106         if (((base != probe) && (base < node.nEntries)) ||
00107             ((node.nEntries == 1) && (node.height != 0)))
00108         {
00109             // one entry: +infinity
00110             accessTupleInline(node,base);
00111         }
00112         return base;
00113     }
00114 
00115     virtual int compareFirstKey(
00116         BTreeNode const &node,
00117         TupleDescriptor const &keyDescriptor,
00118         TupleData const &searchKey,
00119         TupleData &scratchKey)
00120     {
00121         int nKeys = NodeAccessor::getKeyCount(node);
00122         if (nKeys == 0) {
00123             return -1;
00124         }
00125         accessTupleInline(node, 0);
00126         pKeyAccessor->unmarshal(scratchKey);
00127         int compareResult = keyDescriptor.compareTuples(searchKey, scratchKey);
00128         return compareResult;
00129     }
00130 
00131     virtual PConstBuffer getEntryForRead(BTreeNode const &node,uint iEntry)
00132     {
00133         return NodeAccessor::getEntryForReadInline(node,iEntry);
00134     }
00135 };
00136 
00137 FENNEL_END_NAMESPACE
00138 
00139 #endif
00140 
00141 // End BTreeKeyedNodeAccessor.h

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