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;
46 static infiledesc filedesc;
48 d=opendir(DATADIR.c_str());
50 while(de = readdir(d)){
51 string filename = de->d_name;
52 if(isValidInFile(filename, &filedesc))
54 inFiles.push_back(filedesc);
60 bool configcache::isValidInFile(const string& infile, infiledesc *filedesc)
62 char *inchar, *inParts;
63 string truncIn, truncOut;
65 filedesc->filename = infile;
66 filedesc->doVirtualEquilibration = false;
68 if( infile.size() < 4 ) return false;
70 if( infile.substr(infile.size()-3) != "dat" ) return false;
72 inchar = new char [infile.size()+1];
73 strcpy (inchar, infile.c_str());
75 inParts = strtok( inchar, "_" );
76 for(int iPart=0; inParts!=NULL; iPart++)
78 if( iPart>3 ) { truncIn += "_"; truncIn += inParts; }
82 case 1: if(inParts != CACHEID) return false; break;
84 if (atoi(inParts) > NEQUI)
86 else if (atoi(inParts) < NEQUI)
87 filedesc->doVirtualEquilibration = true;
88 filedesc->nequi = atoi(inParts);
91 if(atoi(inParts) != NSKIP)
93 filedesc->nskip = atoi(inParts);
96 inParts = strtok( NULL, "_");
98 truncIn = truncIn.substr(0, truncIn.size()-4);
102 if( truncIn.find( getFileId(true) + "_" ) == string::npos ) return false;
107 bool configcache::readConfig()
109 if(DATADIR == "" || MODE == CACHE_MODE_DISABLED) return false;
111 if(refetchDataFiles){
112 refetchDataFiles = false;
118 if( (!inFile.is_open()) && inFiles.size() == 0 ) return false;
120 while( (!inFile.is_open()) && inFiles.size() > 0 )
122 if(out) *out->log << "CCACHE: Opening dat-file: " << inFiles.back().filename << endl << flush;
124 openFileDesc = inFiles.back();
125 inFile.open( (DATADIR + "/" + inFiles.back().filename).c_str(), std::ios::binary );
128 if( !inFile.is_open() ) continue;
130 inBuffer = new boost::iostreams::filtering_istreambuf;
131 inBuffer->push( boost::iostreams::bzip2_decompressor() );
132 inBuffer->push(inFile);
135 if( inFile.is_open() )
137 if (openFileDesc.doVirtualEquilibration) {
138 if(out) *out->log << "CCACHE: Trying virtual equilibration." << endl << flush;
139 openFileDesc.doVirtualEquilibration = false;
140 for (int iskip=0; iskip < (NEQUI-openFileDesc.nequi)/openFileDesc.nskip; iskip++) {
141 if( readConfigToMem(tmpConfig) != configSize || ! inFile.is_open() )
146 if( readConfigToMem(tmpConfig) == configSize && inFile.is_open() )
148 memcpy(configMem, tmpConfig, configSize);
152 if(out) *out->log << "CCACHE: Closing dat-file: " << openFileDesc.filename << endl << flush;
159 void configcache::writeConfig()
161 if( DATADIR == "" || MODE < 2 ) return;
163 if(!outFile.is_open()){
164 time_t secstamp = time(NULL);
167 outFileName << DATADIR << "/" << secstamp << "_" << getFileId() << "_.dat.tmp";
168 outFile.open( outFileName.str().c_str(), std::ios::binary );
170 outBuffer = new boost::iostreams::filtering_ostreambuf;
171 outBuffer->push(boost::iostreams::bzip2_compressor());
172 outBuffer->push(outFile);
175 boost::iostreams::write(*outBuffer, configMem, configSize);
178 void configcache::addPara(const string& parid, const double& val){
182 Paras.push_back(newPara);
185 int configcache::getParIndex(const string& parid){
186 for(int ipara=0; ipara<Paras.size(); ipara++)
187 if(Paras[ipara].id == parid) return ipara;
190 void configcache::setPara(const string& parid, const double& value){
191 Paras[getParIndex(parid)].val = value;
193 if(inBuffer != NULL) { delete inBuffer; inBuffer=NULL; }
197 refetchDataFiles = true;
200 configcache::~configcache()
207 void configcache::finishOutFile()
209 if( outBuffer != NULL )
215 if( outFile.is_open() )
218 rename( outFileName.str().c_str(), outFileName.str().substr(0, outFileName.str().size()-4).c_str() );
222 int configcache::readConfigToMem(char *tmpConfig)
226 try { readturn = boost::iostreams::read(*inBuffer, tmpConfig, configSize); }
227 catch(boost::iostreams::bzip2_error& error) {
228 if(out) *out->log << "CCACHE: Caught bzip2 exception with error code: " << error.error() << endl << flush;
231 catch (std::exception const& ex) {
232 if(out) *out->log << "CCACHE: Caught exception: " << ex.what() << endl << flush;
236 if(out) *out->log << "CCACHE: Caught unknown exception while reading." << endl << flush;