bug fixes for CRC caching and working version of cache manager

This commit is contained in:
Mark Vejvoda
2010-05-01 00:53:43 +00:00
parent 5cf84e8a2a
commit 33084f8dbe
4 changed files with 51 additions and 26 deletions

View File

@@ -14,7 +14,11 @@
#define _SHARED_PLATFORMCOMMON_CACHEMANAGER_H_ #define _SHARED_PLATFORMCOMMON_CACHEMANAGER_H_
#include "thread.h" #include "thread.h"
#include <map>
#include <string>
#include <stdexcept>
using namespace std;
using namespace Shared::Platform; using namespace Shared::Platform;
namespace Shared { namespace PlatformCommon { namespace Shared { namespace PlatformCommon {
@@ -26,9 +30,8 @@ namespace Shared { namespace PlatformCommon {
class CacheManager class CacheManager
{ {
protected: protected:
Mutex mutexCache; static Mutex mutexCache;
static std::map<std::string, bool> masterCacheList;
static std::map<string, bool> masterCacheList;
typedef enum { typedef enum {
cacheItemGet, cacheItemGet,
@@ -36,12 +39,23 @@ protected:
} CacheAccessorType; } CacheAccessorType;
template <typename T> template <typename T>
static T manageCachedItem(string cacheKey, T *value,CacheAccessorType accessor) { static T & manageCachedItem(string cacheKey, T *value,CacheAccessorType accessor) {
static std::map<string, T> itemCache; static std::map<string, T > itemCache;
if(accessor == cacheItemSet) { if(accessor == cacheItemSet) {
if(value == NULL) {
throw runtime_error("Unexpected NULL object setting cache value!");
}
masterCacheList[cacheKey] = true; masterCacheList[cacheKey] = true;
itemCache[cacheKey] = *value; try {
mutexCache.p();
itemCache[cacheKey] = *value;
mutexCache.v();
}
catch(const std::exception &ex) {
mutexCache.v();
throw runtime_error(ex.what());
}
} }
return itemCache[cacheKey]; return itemCache[cacheKey];
} }
@@ -56,11 +70,11 @@ public:
} }
template <typename T> template <typename T>
static T setCachedItem(string cacheKey, T value) { static void setCachedItem(string cacheKey, const T value) {
manageCachedItem<T>(cacheKey,value,cacheItemSet); manageCachedItem<T>(cacheKey,value,cacheItemSet);
} }
template <typename T> template <typename T>
static T getCachedItem(string cacheKey) { static T & getCachedItem(string cacheKey) {
return manageCachedItem<T>(cacheKey,NULL,cacheItemGet); return manageCachedItem<T>(cacheKey,NULL,cacheItemGet);
} }

View File

@@ -9,3 +9,11 @@
// by the Free Software Foundation; either version 2 of the // by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version // License, or (at your option) any later version
// ============================================================== // ==============================================================
#include "cache_manager.h"
namespace Shared { namespace PlatformCommon {
Mutex CacheManager::mutexCache;
std::map<string, bool> CacheManager::masterCacheList;
}}//end namespace

View File

@@ -290,9 +290,10 @@ int32 getFolderTreeContentsCheckSumRecursively(vector<string> paths, string path
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
static std::map<string,int32> crcTreeCache; // !!! OLD local CACHE
//string cacheLookupId = string(__FILE__) + intToStr(__LINE__); //static std::map<string,int32> crcTreeCache;
//std::map<string,int32> &crcTreeCache = CacheManager::getCachedItem(cacheLookupId); string cacheLookupId = string(__FILE__) + intToStr(__LINE__);
std::map<string,int32> &crcTreeCache = CacheManager::getCachedItem< std::map<string,int32> >(cacheLookupId);
string cacheKey = ""; string cacheKey = "";
size_t count = paths.size(); size_t count = paths.size();
@@ -328,8 +329,12 @@ int32 getFolderTreeContentsCheckSumRecursively(vector<string> paths, string path
//finds all filenames like path and gets their checksum of all files combined //finds all filenames like path and gets their checksum of all files combined
int32 getFolderTreeContentsCheckSumRecursively(const string &path, const string &filterFileExt, Checksum *recursiveChecksum) { int32 getFolderTreeContentsCheckSumRecursively(const string &path, const string &filterFileExt, Checksum *recursiveChecksum) {
// !!! OLD local CACHE
//static std::map<string,int32> crcTreeCache;
string cacheLookupId = string(__FILE__) + intToStr(__LINE__);
std::map<string,int32> &crcTreeCache = CacheManager::getCachedItem< std::map<string,int32> >(cacheLookupId);
string cacheKey = path + "_" + filterFileExt; string cacheKey = path + "_" + filterFileExt;
static std::map<string,int32> crcTreeCache;
if(crcTreeCache.find(cacheKey) != crcTreeCache.end()) { if(crcTreeCache.find(cacheKey) != crcTreeCache.end()) {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] scanning [%s] found CACHED checksum = %d for cacheKey [%s]\n",__FILE__,__FUNCTION__,path.c_str(),crcTreeCache[cacheKey],cacheKey.c_str()); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] scanning [%s] found CACHED checksum = %d for cacheKey [%s]\n",__FILE__,__FUNCTION__,path.c_str(),crcTreeCache[cacheKey],cacheKey.c_str());
return crcTreeCache[cacheKey]; return crcTreeCache[cacheKey];
@@ -428,7 +433,9 @@ int32 getFolderTreeContentsCheckSumRecursively(const string &path, const string
vector<std::pair<string,int32> > getFolderTreeContentsCheckSumListRecursively(vector<string> paths, string pathSearchString, string filterFileExt, vector<std::pair<string,int32> > *recursiveMap) { vector<std::pair<string,int32> > getFolderTreeContentsCheckSumListRecursively(vector<string> paths, string pathSearchString, string filterFileExt, vector<std::pair<string,int32> > *recursiveMap) {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
static std::map<string,vector<std::pair<string,int32> > > crcTreeCache; //static std::map<string,vector<std::pair<string,int32> > > crcTreeCache;
string cacheLookupId = string(__FILE__) + intToStr(__LINE__);
std::map<string,vector<std::pair<string,int32> > > &crcTreeCache = CacheManager::getCachedItem< std::map<string,vector<std::pair<string,int32> > > >(cacheLookupId);
string cacheKey = ""; string cacheKey = "";
size_t count = paths.size(); size_t count = paths.size();
@@ -457,8 +464,11 @@ vector<std::pair<string,int32> > getFolderTreeContentsCheckSumListRecursively(ve
//finds all filenames like path and gets the checksum of each file //finds all filenames like path and gets the checksum of each file
vector<std::pair<string,int32> > getFolderTreeContentsCheckSumListRecursively(const string &path, const string &filterFileExt, vector<std::pair<string,int32> > *recursiveMap) { vector<std::pair<string,int32> > getFolderTreeContentsCheckSumListRecursively(const string &path, const string &filterFileExt, vector<std::pair<string,int32> > *recursiveMap) {
//static std::map<string,vector<std::pair<string,int32> > > crcTreeCache;
string cacheLookupId = string(__FILE__) + intToStr(__LINE__);
std::map<string,vector<std::pair<string,int32> > > &crcTreeCache = CacheManager::getCachedItem< std::map<string,vector<std::pair<string,int32> > > >(cacheLookupId);
string cacheKey = path + "_" + filterFileExt; string cacheKey = path + "_" + filterFileExt;
static std::map<string,vector<std::pair<string,int32> > > crcTreeCache;
if(crcTreeCache.find(cacheKey) != crcTreeCache.end()) { if(crcTreeCache.find(cacheKey) != crcTreeCache.end()) {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] scanning [%s] FOUND CACHED result for cacheKey [%s]\n",__FILE__,__FUNCTION__,path.c_str(),cacheKey.c_str()); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] scanning [%s] FOUND CACHED result for cacheKey [%s]\n",__FILE__,__FUNCTION__,path.c_str(),cacheKey.c_str());
return crcTreeCache[cacheKey]; return crcTreeCache[cacheKey];

View File

@@ -34,24 +34,17 @@ void FileCRCPreCacheThread::execute() {
findDirs(techDataPaths, techPaths); findDirs(techDataPaths, techPaths);
if(techPaths.empty() == false) { if(techPaths.empty() == false) {
for(unsigned int idx = 0; idx < techPaths.size(); idx++) { for(unsigned int idx = 0; idx < techPaths.size(); idx++) {
string &techPath = techPaths[idx]; string techName = techPaths[idx];
for(unsigned int idx2 = 0; idx2 < techPaths.size(); idx2++) {
string techName = techPaths[idx2];
printf("In [%s::%s Line: %d] caching CRC value for Tech [%s]\n",__FILE__,__FUNCTION__,__LINE__,techName.c_str()); printf("In [%s::%s Line: %d] caching CRC value for Tech [%s] [%d of %d]\n",__FILE__,__FUNCTION__,__LINE__,techName.c_str(),idx+1,techPaths.size());
int32 techCRC = getFolderTreeContentsCheckSumRecursively(techDataPaths, string("/") + techName + string("/*"), ".xml", NULL); int32 techCRC = getFolderTreeContentsCheckSumRecursively(techDataPaths, string("/") + techName + string("/*"), ".xml", NULL);
printf("In [%s::%s Line: %d] cached CRC value for Tech [%s] is [%d]\n",__FILE__,__FUNCTION__,__LINE__,techName.c_str(),techCRC); printf("In [%s::%s Line: %d] cached CRC value for Tech [%s] is [%d] [%d of %d]\n",__FILE__,__FUNCTION__,__LINE__,techName.c_str(),techCRC,idx+1,techPaths.size());
if(getQuitStatus() == true) {
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
break;
}
sleep( 100 );
}
if(getQuitStatus() == true) { if(getQuitStatus() == true) {
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
break; break;
} }
sleep( 100 );
} }
} }
} }