diff --git a/source/shared_lib/include/platform/common/cache_manager.h b/source/shared_lib/include/platform/common/cache_manager.h index be5eafb1c..f926851dc 100644 --- a/source/shared_lib/include/platform/common/cache_manager.h +++ b/source/shared_lib/include/platform/common/cache_manager.h @@ -14,7 +14,11 @@ #define _SHARED_PLATFORMCOMMON_CACHEMANAGER_H_ #include "thread.h" +#include +#include +#include +using namespace std; using namespace Shared::Platform; namespace Shared { namespace PlatformCommon { @@ -26,9 +30,8 @@ namespace Shared { namespace PlatformCommon { class CacheManager { protected: - Mutex mutexCache; - - static std::map masterCacheList; + static Mutex mutexCache; + static std::map masterCacheList; typedef enum { cacheItemGet, @@ -36,12 +39,23 @@ protected: } CacheAccessorType; template - static T manageCachedItem(string cacheKey, T *value,CacheAccessorType accessor) { - static std::map itemCache; + static T & manageCachedItem(string cacheKey, T *value,CacheAccessorType accessor) { + static std::map itemCache; if(accessor == cacheItemSet) { + if(value == NULL) { + throw runtime_error("Unexpected NULL object setting cache value!"); + } 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]; } @@ -56,11 +70,11 @@ public: } template - static T setCachedItem(string cacheKey, T value) { + static void setCachedItem(string cacheKey, const T value) { manageCachedItem(cacheKey,value,cacheItemSet); } template - static T getCachedItem(string cacheKey) { + static T & getCachedItem(string cacheKey) { return manageCachedItem(cacheKey,NULL,cacheItemGet); } diff --git a/source/shared_lib/sources/platform/common/cache_manager.cpp b/source/shared_lib/sources/platform/common/cache_manager.cpp index a7562aaf1..10b63e921 100644 --- a/source/shared_lib/sources/platform/common/cache_manager.cpp +++ b/source/shared_lib/sources/platform/common/cache_manager.cpp @@ -9,3 +9,11 @@ // by the Free Software Foundation; either version 2 of the // License, or (at your option) any later version // ============================================================== +#include "cache_manager.h" + +namespace Shared { namespace PlatformCommon { + +Mutex CacheManager::mutexCache; +std::map CacheManager::masterCacheList; + +}}//end namespace diff --git a/source/shared_lib/sources/platform/common/platform_common.cpp b/source/shared_lib/sources/platform/common/platform_common.cpp index 88a8788dc..2202a1b0e 100644 --- a/source/shared_lib/sources/platform/common/platform_common.cpp +++ b/source/shared_lib/sources/platform/common/platform_common.cpp @@ -290,9 +290,10 @@ int32 getFolderTreeContentsCheckSumRecursively(vector paths, string path SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - static std::map crcTreeCache; - //string cacheLookupId = string(__FILE__) + intToStr(__LINE__); - //std::map &crcTreeCache = CacheManager::getCachedItem(cacheLookupId); + // !!! OLD local CACHE + //static std::map crcTreeCache; + string cacheLookupId = string(__FILE__) + intToStr(__LINE__); + std::map &crcTreeCache = CacheManager::getCachedItem< std::map >(cacheLookupId); string cacheKey = ""; size_t count = paths.size(); @@ -328,8 +329,12 @@ int32 getFolderTreeContentsCheckSumRecursively(vector paths, string path //finds all filenames like path and gets their checksum of all files combined int32 getFolderTreeContentsCheckSumRecursively(const string &path, const string &filterFileExt, Checksum *recursiveChecksum) { + // !!! OLD local CACHE + //static std::map crcTreeCache; + string cacheLookupId = string(__FILE__) + intToStr(__LINE__); + std::map &crcTreeCache = CacheManager::getCachedItem< std::map >(cacheLookupId); + string cacheKey = path + "_" + filterFileExt; - static std::map crcTreeCache; 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()); return crcTreeCache[cacheKey]; @@ -428,7 +433,9 @@ int32 getFolderTreeContentsCheckSumRecursively(const string &path, const string vector > getFolderTreeContentsCheckSumListRecursively(vector paths, string pathSearchString, string filterFileExt, vector > *recursiveMap) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - static std::map > > crcTreeCache; + //static std::map > > crcTreeCache; + string cacheLookupId = string(__FILE__) + intToStr(__LINE__); + std::map > > &crcTreeCache = CacheManager::getCachedItem< std::map > > >(cacheLookupId); string cacheKey = ""; size_t count = paths.size(); @@ -457,8 +464,11 @@ vector > getFolderTreeContentsCheckSumListRecursively(ve //finds all filenames like path and gets the checksum of each file vector > getFolderTreeContentsCheckSumListRecursively(const string &path, const string &filterFileExt, vector > *recursiveMap) { + //static std::map > > crcTreeCache; + string cacheLookupId = string(__FILE__) + intToStr(__LINE__); + std::map > > &crcTreeCache = CacheManager::getCachedItem< std::map > > >(cacheLookupId); + string cacheKey = path + "_" + filterFileExt; - static std::map > > crcTreeCache; 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()); return crcTreeCache[cacheKey]; diff --git a/source/shared_lib/sources/platform/common/simple_threads.cpp b/source/shared_lib/sources/platform/common/simple_threads.cpp index b8ca1c07b..5e8254a31 100644 --- a/source/shared_lib/sources/platform/common/simple_threads.cpp +++ b/source/shared_lib/sources/platform/common/simple_threads.cpp @@ -34,24 +34,17 @@ void FileCRCPreCacheThread::execute() { findDirs(techDataPaths, techPaths); if(techPaths.empty() == false) { for(unsigned int idx = 0; idx < techPaths.size(); idx++) { - string &techPath = techPaths[idx]; - for(unsigned int idx2 = 0; idx2 < techPaths.size(); idx2++) { - string techName = techPaths[idx2]; + string techName = techPaths[idx]; - printf("In [%s::%s Line: %d] caching CRC value for Tech [%s]\n",__FILE__,__FUNCTION__,__LINE__,techName.c_str()); - 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] 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); + 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) { SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); break; } + sleep( 100 ); } } }