1 #include "configcache.h"
8 configcache::configcache(const string& cacheid, const int& nequi, const int& nskip, const string& datadir, char **configmem, const int& configMemSize, const int& cachemode,
17 configMem = (char*)malloc(configMemSize);
18 tmpConfig = (char*)malloc(configMemSize);
20 *configmem = configMem;
21 configSize = configMemSize;
28 refetchDataFiles = false;
31 string configcache::getFileId(const bool& shortid)
35 if(!shortid) fileid << CACHEID << "_" << NEQUI << "_" << NSKIP;
36 for(int ipara=0; ipara<Paras.size(); ipara++)
37 fileid << "_" << Paras[ipara].id << Paras[ipara].val;
42 void configcache::fetchDataFiles()
44 struct dirent *de=NULL;
47 d=opendir(DATADIR.c_str());
49 while(de = readdir(d)){
50 string filename = de->d_name;
51 if(isValidInFile(filename))
53 inFiles.push_back(filename);
59 bool configcache::isValidInFile(const string& infile)
61 char *inchar, *inParts;
62 string truncIn, truncOut;
64 if( infile.size() < 4 ) return false;
66 if( infile.substr(infile.size()-3) != "dat" ) return false;
68 inchar = new char [infile.size()+1];
69 strcpy (inchar, infile.c_str());
71 inParts = strtok( inchar, "_" );
72 for(int iPart=0; inParts!=NULL; iPart++)
74 if( iPart>3 ) { truncIn += "_"; truncIn += inParts; }
78 case 1: if(inParts != CACHEID) return false; break;
79 case 2: if(atoi(inParts) != NEQUI) return false; break;
80 case 3: if(atoi(inParts) != NSKIP) return false; break;
82 inParts = strtok( NULL, "_");
84 truncIn = truncIn.substr(0, truncIn.size()-4);
88 if( truncIn.find( getFileId(true) + "_" ) == string::npos ) return false;
93 bool configcache::readConfig()
95 if(DATADIR == "" || MODE == CACHE_MODE_DISABLED) return false;
98 refetchDataFiles = false;
104 if( (!inFile.is_open()) && inFiles.size() == 0 ) return false;
106 while( (!inFile.is_open()) && inFiles.size() > 0 )
108 if(out) *out->log << "CCACHE: Opening dat-file: " << inFiles.back() << endl << flush;
110 inFile.open( (DATADIR + "/" + inFiles.back()).c_str(), std::ios::binary );
113 if( !inFile.is_open() ) continue;
115 inBuffer = new boost::iostreams::filtering_istreambuf;
116 inBuffer->push( boost::iostreams::bzip2_decompressor() );
117 inBuffer->push(inFile);
120 if( inFile.is_open() )
123 try { readturn = boost::iostreams::read(*inBuffer, tmpConfig, configSize); }
124 catch (std::exception const& ex) {
125 if(out) *out->log << "CCACHE: Caught exception: " << ex.what() << endl << flush;
129 if( readturn == configSize && inFile.is_open() )
131 memcpy(configMem, tmpConfig, configSize);
139 void configcache::writeConfig()
141 if( DATADIR == "" || MODE < 2 ) return;
143 if(!outFile.is_open()){
144 time_t secstamp = time(NULL);
147 outFileName << DATADIR << "/" << secstamp << "_" << getFileId() << "_.dat.tmp";
148 outFile.open( outFileName.str().c_str(), std::ios::binary );
150 outBuffer = new boost::iostreams::filtering_ostreambuf;
151 outBuffer->push(boost::iostreams::bzip2_compressor());
152 outBuffer->push(outFile);
155 boost::iostreams::write(*outBuffer, configMem, configSize);
158 void configcache::addPara(const string& parid, const double& val){
162 Paras.push_back(newPara);
165 int configcache::getParIndex(const string& parid){
166 for(int ipara=0; ipara<Paras.size(); ipara++)
167 if(Paras[ipara].id == parid) return ipara;
170 void configcache::setPara(const string& parid, const double& value){
171 Paras[getParIndex(parid)].val = value;
173 if(inBuffer != NULL) { delete inBuffer; inBuffer=NULL; }
177 refetchDataFiles = true;
180 configcache::~configcache()
187 void configcache::finishOutFile()
189 if( outBuffer != NULL )
195 if( outFile.is_open() )
198 rename( outFileName.str().c_str(), outFileName.str().substr(0, outFileName.str().size()-4).c_str() );