1 #include "configcache.h"
11 #include <boost/iostreams/filtering_streambuf.hpp>
12 #include <boost/iostreams/stream.hpp>
13 #include <boost/iostreams/filter/bzip2.hpp>
14 #include <boost/iostreams/device/array.hpp>
15 #include <boost/iostreams/copy.hpp>
17 #define HEADER_READOK 0
18 #define HEADER_READERR 1
19 #define HEADER_READLAST 2
21 struct configcache::iobuffers
23 boost::iostreams::filtering_istreambuf *in;
24 boost::iostreams::filtering_ostreambuf *out;
27 configcache::configcache(const string& cacheid, const int& nequi, const int& nskip, const string& datadir, char **configmem, const int& configMemSize, const int& cachemode,
36 if ( cacheid.find("_") != -1 ) {
37 if(log) *log << "CCACHE: Invalid cacheid \"" << cacheid << "\" given. Cacheids must not contain underscores!" << endl << flush;
41 configMem = (char*)malloc(configMemSize);
42 tmpConfig = (char*)malloc(configMemSize);
44 *configmem = configMem;
45 configSize = configMemSize;
47 ioBuffers = new iobuffers;
49 ioBuffers->out = NULL;
53 refetchDataFiles = false;
56 string configcache::getFileId(int actnequi, const bool& shortid)
60 if(!shortid) fileid << CACHEID << "_" << actnequi << "_" << NSKIP;
61 for(int ipara=0; ipara<Paras.size(); ipara++)
62 fileid << "_" << Paras[ipara].id << Paras[ipara].val;
67 void configcache::fetchDataFiles()
69 struct dirent *de=NULL;
71 static infiledesc filedesc;
73 d=opendir(DATADIR.c_str());
75 while(de = readdir(d)){
76 string filename = de->d_name;
77 if(isValidInFile(filename, &filedesc))
79 inFiles.push_back(filedesc);
85 bool configcache::isValidInFile(const string& infile, infiledesc *filedesc)
87 char *inchar, *inParts;
88 string truncIn, truncOut;
90 filedesc->filename = infile;
92 if( infile.size() < 4 ) return false;
94 if( infile.substr(infile.size()-4) == ".dat" )
95 filedesc->extended = false;
96 else if( infile.substr(infile.size()-4) == "edat" )
97 filedesc->extended = true;
101 inchar = new char [infile.size()+1];
102 strcpy (inchar, infile.c_str());
104 inParts = strtok( inchar, "_" );
105 for(int iPart=0; inParts!=NULL; iPart++)
107 if( iPart>3 ) { truncIn += "_"; truncIn += inParts; }
111 case 1: if(inParts != CACHEID)
115 filedesc->nequi = atoi(inParts);
118 if(atoi(inParts) != NSKIP)
120 filedesc->nskip = atoi(inParts);
123 inParts = strtok( NULL, "_");
125 truncIn = truncIn.substr(0, truncIn.size()-4);
129 if( truncIn.find( getFileId(NEQUI, true) + "_" ) == string::npos ) return false;
134 int configcache::readHeader()
136 long unsigned int headersize;
138 if( readDataToMem((char *)&headersize, sizeof(long unsigned int)) == sizeof(long unsigned int) && inFile.is_open() ) {
139 if ( headersize == 0 )
140 return HEADER_READLAST;
142 pair<unsigned long, void *> newHeader;
144 if( readDataToMem((char *)&newHeader.first, sizeof(unsigned long)) == sizeof(unsigned long) && inFile.is_open() ) {
145 newHeader.second = malloc(headersize);
147 if( readDataToMem((char *)newHeader.second, headersize) == headersize && inFile.is_open() ) {
148 headerStore.push_back(newHeader);
149 return HEADER_READOK;
152 if(log) *log << "CCACHE: Could not read heade-data! Closing dat-file: " << openFileDesc.filename << endl << flush;
154 return HEADER_READERR;
158 if(log) *log << "CCACHE: Could not read headerid-hash! Closing dat-file: " << openFileDesc.filename << endl << flush;
160 return HEADER_READERR;
164 if(log) *log << "CCACHE: Could not read header size. Closing dat-file: " << openFileDesc.filename << endl << flush;
166 return HEADER_READERR;
170 bool configcache::readAllHeaders()
172 int readHeaderStatus;
177 readHeaderStatus = readHeader();
179 while ( readHeaderStatus == HEADER_READOK );
181 if ( readHeaderStatus == HEADER_READLAST ) return true;
182 else if ( readHeaderStatus == HEADER_READERR ) return false;
185 void * configcache::getHeader(const string& headerid) {
186 for (vector< pair<unsigned long, void *> >::iterator headerStoreIt = headerStore.begin(); headerStoreIt != headerStore.end(); ++headerStoreIt)
187 if ( headerStoreIt->first == hash(headerid) )
188 return headerStoreIt->second;
193 void configcache::readConfig(bool *readnewconfig, int *nequileft, vector<unsigned long> *excludeFileHashes)
195 *readnewconfig = false;
197 if( DATADIR == "" || !(MODE==CACHE_MODE_RO||MODE==CACHE_MODE_RW) ) return;
199 if(refetchDataFiles){
200 refetchDataFiles = false;
206 vector<infiledesc>::iterator inFileIt = getNextInfile(excludeFileHashes);
207 int iDidVirtualSkips;
209 if( (!inFile.is_open()) && inFileIt == inFiles.end() ) {
211 *nequileft = nequileft_internal;
215 while( (!inFile.is_open()) && inFiles.size() > 0 ) {
216 openFileDesc = *inFileIt;
218 if (openFileDesc.nequi < NEQUI)
219 doVirtualEquilibration = true;
221 doVirtualEquilibration = false;
223 firstUsedConfig = true;
225 if(log) *log << "CCACHE: Opening dat-file: " << inFileIt->filename << endl << flush;
226 inFile.open( (DATADIR + "/" + inFileIt->filename).c_str(), std::ios::binary );
228 inFiles.erase(inFileIt);
230 if( !inFile.is_open() ) continue;
232 ioBuffers->in = new boost::iostreams::filtering_istreambuf;
233 ioBuffers->in->push( boost::iostreams::bzip2_decompressor() );
234 ioBuffers->in->push(inFile);
237 if( inFile.is_open() )
239 if (doVirtualEquilibration) {
240 if(log) *log << "CCACHE: Trying virtual equilibration." << endl << flush;
241 doVirtualEquilibration = false;
242 for (iDidVirtualSkips=0; iDidVirtualSkips < (NEQUI-openFileDesc.nequi)/openFileDesc.nskip; iDidVirtualSkips++) {
243 if( readFullBlock(tmpConfig, configSize) != configSize || ! inFile.is_open() )
245 else if ( (NEQUI-openFileDesc.nequi) - (iDidVirtualSkips+1)*openFileDesc.nskip < nequileft_internal ) {
246 memcpy(configMem, tmpConfig, configSize);
247 nequileft_internal = NEQUI - openFileDesc.nequi - (iDidVirtualSkips+1)*openFileDesc.nskip;
248 *readnewconfig = true;
249 firstUsedConfig = false;
254 if( readFullBlock(tmpConfig, configSize) == configSize && inFile.is_open() )
256 memcpy(configMem, tmpConfig, configSize);
257 *readnewconfig = true;
258 if (firstUsedConfig) {
259 firstUsedConfig = false;
260 if (openFileDesc.nequi < NEQUI)
261 nequileft_internal = NEQUI - openFileDesc.nequi - iDidVirtualSkips*openFileDesc.nskip;
263 nequileft_internal = NEQUI - openFileDesc.nequi;
265 nequileft_internal -= openFileDesc.nskip;
266 *nequileft = nequileft_internal;
270 if(log) *log << "CCACHE: Could not read configuration. Closing dat-file: " << openFileDesc.filename << endl << flush;
277 void configcache::openOutFile(int actnequi)
279 time_t secstamp = time(NULL);
284 outFileName << DATADIR << "/" << secstamp << "." << iseq << "_" << getFileId(actnequi) << "_.edat.tmp";
286 int tmpfd = open(outFileName.str().c_str(), O_CREAT | O_EXCL, 0644);
292 else if ( errno != EEXIST ) {
293 if(log) *log << "CCACHE: Could not create cachefile!" << endl << flush;
300 outFile.open( outFileName.str().c_str(), std::ios::binary );
302 ioBuffers->out = new boost::iostreams::filtering_ostreambuf;
303 ioBuffers->out->push(boost::iostreams::bzip2_compressor());
304 ioBuffers->out->push(outFile);
307 void configcache::writeHeader(const string& headerid, const char *header, long unsigned int size, int actnequi) {
308 unsigned long headeridhash;
310 if( DATADIR == "" || !(MODE==CACHE_MODE_WO||MODE==CACHE_MODE_RW) ) return;
312 if(!outFile.is_open())
313 openOutFile(actnequi);
315 headeridhash = hash(headerid);
317 boost::iostreams::write(*ioBuffers->out, (char*)&size, sizeof(long unsigned int));
318 boost::iostreams::write(*ioBuffers->out, (char*)&headeridhash, sizeof(unsigned long));
319 boost::iostreams::write(*ioBuffers->out, header, size);
322 void configcache::writeConfig(int actnequi)
324 long unsigned int zeroheader=0;
326 if ( DATADIR == "" || !(MODE==CACHE_MODE_WO||MODE==CACHE_MODE_RW) ) return;
328 if ( ! outFile.is_open() )
329 openOutFile(actnequi);
331 boost::iostreams::write(*ioBuffers->out, (char*)&zeroheader, sizeof(long unsigned int));
333 boost::iostreams::write(*ioBuffers->out, configMem, configSize);
336 void configcache::addPara(const string& parid, const double& val){
340 Paras.push_back(newPara);
343 int configcache::getParIndex(const string& parid){
344 for(int ipara=0; ipara<Paras.size(); ipara++)
345 if(Paras[ipara].id == parid) return ipara;
348 void configcache::setPara(const string& parid, const double& value){
349 Paras[getParIndex(parid)].val = value;
352 if(ioBuffers->in != NULL) { delete ioBuffers->in; ioBuffers->in=NULL; }
356 refetchDataFiles = true;
357 nequileft_internal = NEQUI;
360 configcache::~configcache()
363 delete ioBuffers->in;
364 ioBuffers->in = NULL;
367 void configcache::finishOutFile()
369 if( ioBuffers->out != NULL )
371 delete ioBuffers->out;
372 ioBuffers->out = NULL;
375 if( outFile.is_open() )
378 rename( outFileName.str().c_str(), outFileName.str().substr(0, outFileName.str().size()-4).c_str() );
382 int configcache::readFullBlock(char *tmpData, long unsigned int dataSize)
384 /* try to read header */
385 if ( openFileDesc.extended )
386 if ( ! readAllHeaders() )
390 return readDataToMem(tmpData, dataSize);
393 int configcache::readDataToMem(char *tmpData, long unsigned int dataSize)
397 if ( dataSize == 0 ) return 0;
399 try { readturn = boost::iostreams::read(*ioBuffers->in, tmpData, dataSize); }
400 catch(boost::iostreams::bzip2_error& error) {
401 if(log) *log << "CCACHE: Caught bzip2 exception with error code: " << error.error() << endl << flush;
404 catch (std::exception const& ex) {
405 if(log) *log << "CCACHE: Caught exception: " << ex.what() << endl << flush;
409 if(log) *log << "CCACHE: Caught unknown exception while reading." << endl << flush;
416 unsigned long configcache::hash(const string& str)
418 unsigned long hash = 5381;
420 for(string::const_iterator it=str.begin();it!=str.end();it++)
421 hash = ((hash << 5) + hash) + *it; /* hash * 33 + character */
426 void configcache::deleteHeaderStore()
428 while ( headerStore.size() > 0 ) {
429 free(headerStore.back().second);
430 headerStore.pop_back();
434 vector<infiledesc>::iterator configcache::getNextInfile(vector<unsigned long> *excludeFileHashes) {
435 for (vector<infiledesc>::iterator init = inFiles.begin(); init != inFiles.end(); ++init) {
436 if (excludeFileHashes != NULL) {
437 bool excludethisfile = false;
439 for (vector<unsigned long>::iterator exit = excludeFileHashes->begin(); exit != excludeFileHashes->end(); ++exit)
440 if ( *exit == hash(init->filename) ) {
441 excludethisfile = true;
450 return inFiles.end();