Sirikata
|
00001 /* Sirikata 00002 * FCache.hpp 00003 * 00004 * Copyright (c) 2010, Behram Mistree 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions are 00009 * met: 00010 * * Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * * Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in 00014 * the documentation and/or other materials provided with the 00015 * distribution. 00016 * * Neither the name of Sirikata nor the names of its contributors may 00017 * be used to endorse or promote products derived from this software 00018 * without specific prior written permission. 00019 * 00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 00021 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 00022 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00023 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 00024 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00025 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00026 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00027 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00028 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00029 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 */ 00032 00033 #include <iostream> 00034 #include <iomanip> 00035 #include "Cache.hpp" 00036 #include <map> 00037 #include <vector> 00038 #include <string> 00039 #include "CacheRecords.hpp" 00040 00041 #ifndef __FCACHE_HPP__ 00042 #define __FCACHE_HPP__ 00043 00044 00045 namespace Sirikata 00046 { 00047 00048 00049 typedef double (*FCacheScoreFunc) (const FCacheRecord* a); 00050 typedef double (*FCacheScoreFuncPrint) (const FCacheRecord* a,bool toPrint); 00051 00052 00053 class FCache : public Cache 00054 { 00055 private: 00056 00057 typedef std::map<UUID, FCacheRecord*> FCacheDataMap; 00058 typedef std::pair<UUID,FCacheRecord*> FCacheInsertionPair; 00059 typedef std::vector<UUID> FCacheObjVec; 00060 typedef std::map<int,bool> FCacheValidMap; 00061 00062 class FCachePropDataStruct 00063 { 00064 private: 00065 FCacheDataMap cacheProper; 00066 FCacheObjVec cPropVec; 00067 FCacheValidMap invalidMap; 00068 int mMaxSize; 00069 FCacheRecord* allRecordMem; 00070 SpaceContext* ctx; 00071 00072 const static int MAX_NUM_ITERATIONS_GET_RANDOM = 20; 00073 00074 void sanityCheck(); 00075 void printInvalidMap(); 00076 void printCPropVec(); 00077 00078 public: 00079 00080 FCachePropDataStruct(SpaceContext* spctx, int max_size); 00081 ~FCachePropDataStruct(); 00082 FCacheRecord* getRandom(); 00083 bool insert(const UUID& oid, ServerID bid,int globEvNum,double weight, double distance, double radius,double lookupWeight,double scalingUnits,double vMag); 00084 bool remove(FCacheRecord* rec); 00085 int getSize(); 00086 bool hasRoom(); 00087 FCacheRecord* getRecord(const UUID& oid); 00088 FCacheRecord* popFirst(); 00089 bool empty(); 00090 void clear(); 00091 void printCacheProper(); 00092 }; 00093 00094 00095 static const int FCACHE_RAND_SELECT_NUMBER = 15; 00096 static const int FCACHE_KEEP_NUMBER = 3; 00097 00098 00099 00100 private: 00101 double ewmaRecPopPar; 00102 std::string mName; 00103 00104 //score function 00105 FCacheScoreFunc mScoreFunc; 00106 FCacheScoreFuncPrint mScoreFuncPrint; 00107 00108 public: 00109 SpaceContext* ctx; 00110 OSegEntry mCraqEntry; 00111 00112 private: 00113 unsigned int MAX_SIZE_CACHE_PROPER; 00114 FCachePropDataStruct cacheProper; 00115 00116 std::vector<FCacheRecord*>worstRecords; 00117 00118 00119 void updatePopAndIncrement(FCacheRecord* ); 00120 void updatePopNoIncrement(FCacheRecord* ); 00121 void updatePopNoIncrement(std::vector<FCacheRecord*>&); 00122 void updateRecord(FCacheRecord* rec, ServerID bid,double weight,double distance, double radius, double vMag); 00123 00124 void generateRandomCacheProper(int numToSelect,std::vector<FCacheRecord*>&randRecs);//generates from cacheProper 00125 00126 00127 //debugging sanity checks. 00128 00129 void printCacheRecordVec(const std::vector<FCacheRecord*>& vec); 00130 void checkStaticTimeSanity(); 00131 void checkCacheProperSanity(); 00132 00133 void printCacheProper(); 00134 void removeCacheProper(FCacheRecord* rec); 00135 void printWorstRecords(); 00136 00137 void killRecord(); 00138 00139 public: 00140 00141 FCache(double avgPopPar,std::string cacheName,FCacheScoreFunc scoreFunc, FCacheScoreFuncPrint scoreFuncPrint, SpaceContext* contex); 00142 00143 FCache(double avgPopPar,std::string cacheName,FCacheScoreFunc scoreFunc, FCacheScoreFuncPrint scoreFuncPrint, SpaceContext* context,int CACHE_SIZE); 00144 00145 00146 00147 virtual ~FCache(); 00148 00149 virtual void insert(const UUID& toInsert, ServerID bid, CacheTimeMS tms, double vMag, double weight, double distance, double radius,double lookupWeight,double unitsScaling); 00150 00151 virtual const OSegEntry& lookup(const UUID& lookingFor); 00152 virtual std::string getCacheName(); 00153 virtual void remove(const UUID& toRemove); 00154 00155 static void checkThisRecord(const FCacheRecord* bcr); 00156 static void printVecRecs(const std::vector<FCacheRecord*> &recs); 00157 }; 00158 00159 00160 //for scoring/ordering cache records. 00161 void findMins(std::vector<FCacheRecord*>&vecRecs, FCacheScoreFunc scoreFunc); 00162 } 00163 00164 #endif