1 #include "configcache.h"
8 #define HEADER_READOK 0
9 #define HEADER_READERR 1
10 #define HEADER_READLAST 2
12 configcache::configcache(const string& cacheid, const int& nequi, const int& nskip, const string& datadir, char **configmem, const int& configMemSize, const int& cachemode,
21 configMem = (char*)malloc(configMemSize);
22 tmpConfig = (char*)malloc(configMemSize);
24 *configmem = configMem;
25 configSize = configMemSize;
32 refetchDataFiles = false;
35 string configcache::getFileId(const bool& shortid)
39 if(!shortid) fileid << CACHEID << "_" << NEQUI << "_" << NSKIP;
40 for(int ipara=0; ipara<Paras.size(); ipara++)
41 fileid << "_" << Paras[ipara].id << Paras[ipara].val;
46 void configcache::fetchDataFiles()
48 struct dirent *de=NULL;
50 static infiledesc filedesc;
52 d=opendir(DATADIR.c_str());
54 while(de = readdir(d)){
55 string filename = de->d_name;
56 if(isValidInFile(filename, &filedesc))
58 inFiles.push_back(filedesc);
64 bool configcache::isValidInFile(const string& infile, infiledesc *filedesc)
66 char *inchar, *inParts;
67 string truncIn, truncOut;
69 filedesc->filename = infile;
70 filedesc->doVirtualEquilibration = false;
72 if( infile.size() < 4 ) return false;
74 if( infile.substr(infile.size()-4) == ".dat" )
75 filedesc->extended = false;
76 else if( infile.substr(infile.size()-4) == "edat" )
77 filedesc->extended = true;
81 inchar = new char [infile.size()+1];
82 strcpy (inchar, infile.c_str());
84 inParts = strtok( inchar, "_" );
85 for(int iPart=0; inParts!=NULL; iPart++)
87 if( iPart>3 ) { truncIn += "_"; truncIn += inParts; }
91 case 1: if(inParts != CACHEID) return false; break;
93 if (atoi(inParts) > NEQUI)
95 else if (atoi(inParts) < NEQUI)
96 filedesc->doVirtualEquilibration = true;
97 filedesc->nequi = atoi(inParts);
100 if(atoi(inParts) != NSKIP)
102 filedesc->nskip = atoi(inParts);
105 inParts = strtok( NULL, "_");
107 truncIn = truncIn.substr(0, truncIn.size()-4);
111 if( truncIn.find( getFileId(true) + "_" ) == string::npos ) return false;
116 int configcache::readHeader()
118 long unsigned int headersize;
120 if( readDataToMem((char *)&headersize, sizeof(long unsigned int)) == sizeof(long unsigned int) && inFile.is_open() ) {
121 if ( headersize == 0 )
122 return HEADER_READLAST;
124 pair<unsigned long, void *> newHeader;
126 if( readDataToMem((char *)&newHeader.first, sizeof(unsigned long)) == sizeof(unsigned long) && inFile.is_open() ) {
127 newHeader.second = malloc(headersize);
129 if( readDataToMem((char *)newHeader.second, headersize) == headersize && inFile.is_open() ) {
130 headerStore.push_back(newHeader);
131 return HEADER_READOK;
134 if(log) *log << "CCACHE: Could not read heade-data! Closing dat-file: " << openFileDesc.filename << endl << flush;
136 return HEADER_READERR;
140 if(log) *log << "CCACHE: Could not read headerid-hash! Closing dat-file: " << openFileDesc.filename << endl << flush;
142 return HEADER_READERR;
146 if(log) *log << "CCACHE: Could not read header size. Closing dat-file: " << openFileDesc.filename << endl << flush;
148 return HEADER_READERR;
152 bool configcache::readAllHeaders()
154 int readHeaderStatus;
159 readHeaderStatus = readHeader();
161 while ( readHeaderStatus == HEADER_READOK );
163 if ( readHeaderStatus == HEADER_READLAST ) return true;
164 else if ( readHeaderStatus == HEADER_READERR ) return false;
167 void * configcache::getHeader(const string& headerid) {
168 for (vector< pair<unsigned long, void *> >::iterator headerStoreIt = headerStore.begin(); headerStoreIt != headerStore.end(); ++headerStoreIt)
169 if ( headerStoreIt->first == hash(headerid) )
170 return headerStoreIt->second;
175 /* returns number of equilibration-steps left */
176 int configcache::readConfig(vector<unsigned long> *excludeFileHashes)
178 int nequileft = NEQUI;
180 if(DATADIR == "" || MODE == CACHE_MODE_DISABLED) return nequileft;
182 if(refetchDataFiles){
183 refetchDataFiles = false;
189 vector<infiledesc>::iterator inFileIt = getNextInfile(excludeFileHashes);
191 if( (!inFile.is_open()) && inFileIt == inFiles.end() ) return nequileft;
193 while( (!inFile.is_open()) && inFiles.size() > 0 ) {
194 openFileDesc = *inFileIt;
196 if(log) *log << "CCACHE: Opening dat-file: " << inFileIt->filename << endl << flush;
197 inFile.open( (DATADIR + "/" + inFileIt->filename).c_str(), std::ios::binary );
199 inFiles.erase(inFileIt);
201 if( !inFile.is_open() ) continue;
203 inBuffer = new boost::iostreams::filtering_istreambuf;
204 inBuffer->push( boost::iostreams::bzip2_decompressor() );
205 inBuffer->push(inFile);
208 if( inFile.is_open() )
210 if (openFileDesc.doVirtualEquilibration) {
211 if(log) *log << "CCACHE: Trying virtual equilibration." << endl << flush;
212 openFileDesc.doVirtualEquilibration = false;
213 for (int iskip=0; iskip < (NEQUI-openFileDesc.nequi)/openFileDesc.nskip; iskip++) {
214 if( readFullBlock(tmpConfig, configSize) != configSize || ! inFile.is_open() )
216 else if ( (NEQUI-openFileDesc.nequi) - (iskip+1)*openFileDesc.nskip < nequileft ) {
217 memcpy(configMem, tmpConfig, configSize);
218 nequileft = (NEQUI-openFileDesc.nequi) - (iskip+1)*openFileDesc.nskip;
223 if( readFullBlock(tmpConfig, configSize) == configSize && inFile.is_open() )
225 memcpy(configMem, tmpConfig, configSize);
229 if(log) *log << "CCACHE: Could not read configuration. Closing dat-file: " << openFileDesc.filename << endl << flush;
236 void configcache::openOutFile()
238 time_t secstamp = time(NULL);
241 outFileName << DATADIR << "/" << secstamp << "_" << getFileId() << "_.edat.tmp";
243 outFile.open( outFileName.str().c_str(), std::ios::binary );
245 outBuffer = new boost::iostreams::filtering_ostreambuf;
246 outBuffer->push(boost::iostreams::bzip2_compressor());
247 outBuffer->push(outFile);
250 void configcache::writeHeader(const string& headerid, const char *header, long unsigned int size) {
251 unsigned long headeridhash;
253 if( DATADIR == "" || MODE < 2 ) return;
255 if(!outFile.is_open())
258 headeridhash = hash(headerid);
260 boost::iostreams::write(*outBuffer, (char*)&size, sizeof(long unsigned int));
261 boost::iostreams::write(*outBuffer, (char*)&headeridhash, sizeof(unsigned long));
262 boost::iostreams::write(*outBuffer, header, size);
265 void configcache::writeConfig()
267 long unsigned int zeroheader=0;
269 if ( DATADIR == "" || MODE < 2 ) return;
271 if ( ! outFile.is_open() )
274 boost::iostreams::write(*outBuffer, (char*)&zeroheader, sizeof(long unsigned int));
276 boost::iostreams::write(*outBuffer, configMem, configSize);
279 void configcache::addPara(const string& parid, const double& val){
283 Paras.push_back(newPara);
286 int configcache::getParIndex(const string& parid){
287 for(int ipara=0; ipara<Paras.size(); ipara++)
288 if(Paras[ipara].id == parid) return ipara;
291 void configcache::setPara(const string& parid, const double& value){
292 Paras[getParIndex(parid)].val = value;
294 if(inBuffer != NULL) { delete inBuffer; inBuffer=NULL; }
298 refetchDataFiles = true;
301 configcache::~configcache()
308 void configcache::finishOutFile()
310 if( outBuffer != NULL )
316 if( outFile.is_open() )
319 rename( outFileName.str().c_str(), outFileName.str().substr(0, outFileName.str().size()-4).c_str() );
323 int configcache::readFullBlock(char *tmpData, long unsigned int dataSize)
325 /* try to read header */
326 if ( openFileDesc.extended )
327 if ( ! readAllHeaders() )
331 return readDataToMem(tmpData, dataSize);
334 int configcache::readDataToMem(char *tmpData, long unsigned int dataSize)
338 if ( dataSize == 0 ) return 0;
340 try { readturn = boost::iostreams::read(*inBuffer, tmpData, dataSize); }
341 catch(boost::iostreams::bzip2_error& error) {
342 if(log) *log << "CCACHE: Caught bzip2 exception with error code: " << error.error() << endl << flush;
345 catch (std::exception const& ex) {
346 if(log) *log << "CCACHE: Caught exception: " << ex.what() << endl << flush;
350 if(log) *log << "CCACHE: Caught unknown exception while reading." << endl << flush;
357 unsigned long configcache::hash(const string& str)
359 unsigned long hash = 5381;
361 for(string::const_iterator it=str.begin();it!=str.end();it++)
362 hash = ((hash << 5) + hash) + *it; /* hash * 33 + character */
367 void configcache::deleteHeaderStore()
369 while ( headerStore.size() > 0 ) {
370 free(headerStore.back().second);
371 headerStore.pop_back();
375 vector<infiledesc>::iterator configcache::getNextInfile(vector<unsigned long> *excludeFileHashes) {
376 for (vector<infiledesc>::iterator init = inFiles.begin(); init != inFiles.end(); ++init) {
377 if (excludeFileHashes != NULL) {
378 bool excludethisfile = false;
380 for (vector<unsigned long>::iterator exit = excludeFileHashes->begin(); exit != excludeFileHashes->end(); ++exit)
381 if ( *exit == hash(init->filename) ) {
382 excludethisfile = true;
391 return inFiles.end();