RandomVictimPolicy.h

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/cache/RandomVictimPolicy.h#10 $
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_RandomVictimPolicy_Included
00025 #define Fennel_RandomVictimPolicy_Included
00026 
00027 #include <functional>
00028 #include "fennel/synch/NullMutex.h"
00029 
00030 FENNEL_BEGIN_NAMESPACE
00031 
00046 template <class PageT>
00047 class RandomVictimPolicy
00048 {
00052     NullMutex nullMutex;
00053 
00057     std::vector<PageT *> pages;
00058 
00059     std::subtractive_rng randomNumberGenerator;
00060 
00061     friend class PageIterator;
00062 
00063 public:
00064     // for use by CacheImpl when iterating over candidate victims
00065     typedef NullMutexGuard SharedGuard;
00066     typedef NullMutexGuard ExclusiveGuard;
00067     // TODO:  write an STL modulo_iterator
00068     class PageIterator
00069     {
00070         RandomVictimPolicy policy;
00071         uint iPage;
00072 
00073         PageT *getCurrent() const
00074         {
00075             return policy.pages[iPage];
00076         }
00077 
00078     public:
00079         PageIterator(RandomVictimPolicy &policyInit,uint iPageInit)
00080             : policy(policyInit)
00081         {
00082             iPage = iPageInit;
00083         }
00084 
00085         void operator ++ ()
00086         {
00087             iPage++;
00088             if (iPage >= policy.pages.size()) {
00089                 iPage = 0;
00090             }
00091         }
00092 
00093         PageT *operator -> () const
00094         {
00095             return getCurrent();
00096         }
00097 
00098         operator PageT * () const
00099         {
00100             return getCurrent();
00101         }
00102 
00103         PageT &operator * () const
00104         {
00105             return *getCurrent();
00106         }
00107 
00108         bool operator == (PageIterator const &other) const
00109         {
00110             // NOTE:  assume policy object is same
00111             return iPage == other.iPage;
00112         }
00113     };
00114 
00115     typedef PageIterator DirtyPageIterator;
00116 
00117     RandomVictimPolicy()
00118     {
00119     }
00120 
00121     RandomVictimPolicy(const CacheParams &params)
00122     {
00123     }
00124 
00125     void setAllocatedPageCount(uint nCachePages)
00126     {
00127     }
00128 
00129     void registerPage(PageT &page)
00130     {
00131         pages.push_back(&page);
00132     }
00133 
00134     void unregisterPage(PageT &)
00135     {
00136         // TODO: zfong 1/8/08 - Should remove the page from the pages vector.
00137         // Otherwise, unallocated pages will be returned by getVictimRange().
00138     }
00139 
00140     void notifyPageAccess(PageT &, bool)
00141     {
00142     }
00143 
00144     void notifyPageNice(PageT &)
00145     {
00146     }
00147 
00148     void notifyPageMap(PageT &, bool)
00149     {
00150     }
00151 
00152     void notifyPageUnmap(PageT &, bool)
00153     {
00154     }
00155 
00156     void notifyPageUnpin(PageT &page)
00157     {
00158     }
00159 
00160     void notifyPageDirty(PageT &page)
00161     {
00162     }
00163 
00164     void notifyPageClean(PageT &page)
00165     {
00166     }
00167 
00168     void notifyPageDiscard(BlockId blockId)
00169     {
00170     }
00171 
00172     NullMutex &getMutex()
00173     {
00174         return nullMutex;
00175     }
00176 
00177     std::pair<PageIterator,PageIterator> getVictimRange()
00178     {
00179         uint iPage = randomNumberGenerator(pages.size());
00180         uint iPageEnd = iPage ? iPage-1 : pages.size();
00181         return std::pair<PageIterator,PageIterator>(
00182             PageIterator(*this,iPage),
00183             PageIterator(*this,iPageEnd));
00184     }
00185 
00186     std::pair<DirtyPageIterator,DirtyPageIterator> getDirtyVictimRange()
00187     {
00188         return
00189             static_cast<std::pair<DirtyPageIterator,DirtyPageIterator> >(
00190                 getVictimRange());
00191     }
00192 };
00193 
00194 FENNEL_END_NAMESPACE
00195 
00196 #endif
00197 
00198 // End RandomVictimPolicy.h

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