From b80a3fda9209144605a6306ba24ec2051d387f50 Mon Sep 17 00:00:00 2001 From: Alex Schmidt Date: Wed, 20 Mar 2013 14:45:11 +0100 Subject: [PATCH] implemented concurrent obscache confcache protection. --- configcache.cpp | 26 +++++++++++++++++------ configcache.h | 11 +++++----- hypercache.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ hypercache.h | 14 +++++++++---- 4 files changed, 91 insertions(+), 15 deletions(-) diff --git a/configcache.cpp b/configcache.cpp index fe7bd78..b375230 100644 --- a/configcache.cpp +++ b/configcache.cpp @@ -172,8 +172,8 @@ void * configcache::getHeader(const string& headerid) { return NULL; } -bool configcache::readConfig() -{ +bool configcache::readConfig(vector *excludefiles) +{ if(DATADIR == "" || MODE == CACHE_MODE_DISABLED) return false; if(refetchDataFiles){ @@ -186,12 +186,26 @@ bool configcache::readConfig() if( (!inFile.is_open()) && inFiles.size() == 0 ) return false; while( (!inFile.is_open()) && inFiles.size() > 0 ) { - if(out) *out->log << "CCACHE: Opening dat-file: " << inFiles.back().filename << endl << flush; + bool excludethisfile=false; openFileDesc = inFiles.back(); - inFile.open( (DATADIR + "/" + inFiles.back().filename).c_str(), std::ios::binary ); - inFiles.pop_back(); + if (excludefiles != NULL) + for (vector::iterator exit = excludefiles->begin(); exit != excludefiles->end(); ++exit) + if ( *exit == inFiles.back().filename ) { + excludethisfile = true; + break; + } + + if ( ! excludethisfile ) { + if(out) *out->log << "CCACHE: Opening dat-file: " << inFiles.back().filename << endl << flush; + inFile.open( (DATADIR + "/" + inFiles.back().filename).c_str(), std::ios::binary ); + } + else + if(out) *out->log << "CCACHE: Excluded dat-file: " << inFiles.back().filename << endl << flush; + + inFiles.pop_back(); + if( !inFile.is_open() ) continue; inBuffer = new boost::iostreams::filtering_istreambuf; @@ -237,7 +251,7 @@ void configcache::openOutFile() outBuffer->push(outFile); } -void configcache::writeHeader(const string& headerid, char *header, long unsigned int size) { +void configcache::writeHeader(const string& headerid, const char *header, long unsigned int size) { unsigned long headeridhash; if( DATADIR == "" || MODE < 2 ) return; diff --git a/configcache.h b/configcache.h index 3c521d9..eaf610a 100644 --- a/configcache.h +++ b/configcache.h @@ -38,13 +38,16 @@ class configcache{ ~configcache(); configcache(const string& cacheid, const int& nequi, const int& nskip, const string& datadir, char **configmem, const int& configMemSize, const int& cachemode=CACHE_MODE_FULL, writeout *out_a=NULL); - bool readConfig(); + bool readConfig(vector *excludefiles=NULL); void writeConfig(); void addPara(const string& parid, const double& val=0); void setPara(const string& parid, const double& value); - void writeHeader(const string& headerid, char *header, long unsigned int size); + void writeHeader(const string& headerid, const char *header, long unsigned int size); void * getHeader(const string& headerid); - + string getOutFileName() { return outFileName.str(); } + string getInFileName() { return DATADIR + "/" + openFileDesc.filename; } + static unsigned long hash(const string& str); + private: infiledesc openFileDesc; void finishOutFile(); @@ -94,8 +97,6 @@ class configcache{ int readFullBlock(char *tmpData, long unsigned int dataSize); - static unsigned long hash(const string& str); - vector< pair > headerStore; void deleteHeaderStore(); diff --git a/hypercache.cpp b/hypercache.cpp index 7e0f0e5..16de2a2 100644 --- a/hypercache.cpp +++ b/hypercache.cpp @@ -4,6 +4,9 @@ configcache *hypercache::O = NULL; configcache *hypercache::C = NULL; vector hypercache::delayedParaAdd; vector hypercache::delayedParaSet; +string hypercache::activeCFile = ""; +vector hypercache::parentConfigs; +writeout *hypercache::out = NULL; void hypercache::initCache(configcache **cache, const string& cacheid, const int& nequi, const int& nskip, const string& datadir, @@ -14,6 +17,9 @@ void hypercache::initCache(configcache **cache, if (O == NULL || C == NULL) allInitBefore = false; + if ( out_a != NULL ) + out = out_a; + *cache = new configcache(cacheid, nequi, nskip, datadir, configmem, configMemSize, cachemode, out_a); if (O != NULL && C != NULL && !allInitBefore) { @@ -51,6 +57,8 @@ void hypercache::setPara(const string& parid, const double& val) { else { O->setPara(parid, val); C->setPara(parid, val); + activeCFile = ""; + parentConfigs.clear(); } } @@ -58,3 +66,50 @@ void hypercache::finalize() { delete C; delete O; } + +string hypercache::fileOfPath(const string& dressedfile) { + return dressedfile.substr(dressedfile.find_last_of("\\/")+1); +} + +bool hypercache::readC() { + bool readret; + + if ( readret = C->readConfig(&parentConfigs) ) + activeCFile = fileOfPath(C->getInFileName()); + else + activeCFile = ""; + + return readret; +} + +void hypercache::writeC() { + C->writeConfig(); + activeCFile = fileOfPath( C->getOutFileName().substr( 0, C->getOutFileName().length()-4 ) ); +} + +void hypercache::writeO() { + if ( activeCFile != "" ) + O->writeHeader("concurrent_cfile", activeCFile.c_str(), (activeCFile.length()+1)*sizeof(char)); + + O->writeConfig(); +} + +bool hypercache::readO() { + bool readret; + + if ( readret = O->readConfig() ) { + char *parentconfig = (char*)O->getHeader("concurrent_cfile"); + if ( parentconfig != NULL ) + addParentConfig(parentconfig); + } + + return readret; +} + +void hypercache::addParentConfig(const char* parentconfig) { + for (vector::iterator parit = parentConfigs.begin(); parit != parentConfigs.end(); ++parit) + if ( *parit == parentconfig ) + return; + + parentConfigs.push_back(parentconfig); +} diff --git a/hypercache.h b/hypercache.h index 6997d82..f37bb0f 100644 --- a/hypercache.h +++ b/hypercache.h @@ -20,10 +20,10 @@ class hypercache { static void addPara(const string& parid, const double& val=0); static void setPara(const string& parid, const double& value); - static bool readO() { return O->readConfig(); } - static bool readC() { return C->readConfig(); } - static void writeO() { O->writeConfig(); } - static void writeC() { C->writeConfig(); } + static bool readO(); + static bool readC(); + static void writeO(); + static void writeC(); static void writeHeaderO(const string& headerid, char *header, long unsigned int size) { O->writeHeader(headerid, header, size); } static void *getHeaderO(const string& headerid) { O->getHeader(headerid); } static void writeHeaderC(const string& headerid, char *header, long unsigned int size) { C->writeHeader(headerid, header, size); } @@ -48,6 +48,12 @@ class hypercache { static configcache *C; static vector delayedParaAdd; static vector delayedParaSet; + static writeout *out; + + static string fileOfPath(const string& dressedfile); + static string activeCFile; + static vector parentConfigs; + static void addParentConfig(const char* parentconfig); }; #endif -- 2.39.5