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::paraString() {
57 stringstream parastring;
59 for(int ipara=0; ipara<Paras.size(); ipara++)
60 parastring << "_" << Paras[ipara].id << Paras[ipara].val;
62 return parastring.str();
65 string configcache::getFileId(int actnequi, const bool& shortid)
69 if(!shortid) fileid << CACHEID << "_" << actnequi << "_" << NSKIP;
71 fileid << "_" << hash( paraString() );
76 void configcache::fetchDataFiles()
78 struct dirent *de=NULL;
80 static infiledesc filedesc;
82 d=opendir(DATADIR.c_str());
84 while(de = readdir(d)){
85 string filename = de->d_name;
86 if(isValidInFile(filename, &filedesc))
88 inFiles.push_back(filedesc);
94 bool configcache::isValidInFile(const string& infile, infiledesc *filedesc)
96 char *inchar, *inParts;
97 string truncIn, truncOut;
99 filedesc->filename = infile;
101 if( infile.size() < 4 ) return false;
103 if( infile.substr(infile.size()-4) == ".dat" )
104 filedesc->extended = false;
105 else if( infile.substr(infile.size()-4) == "edat" )
106 filedesc->extended = true;
110 inchar = new char [infile.size()+1];
111 strcpy (inchar, infile.c_str());
113 inParts = strtok( inchar, "_" );
114 for(int iPart=0; inParts!=NULL; iPart++)
116 if( iPart>3 ) { truncIn += "_"; truncIn += inParts; }
120 case 1: if(inParts != CACHEID)
124 filedesc->nequi = atoi(inParts);
127 if(atoi(inParts) != NSKIP)
129 filedesc->nskip = atoi(inParts);
132 inParts = strtok( NULL, "_");
134 truncIn = truncIn.substr(0, truncIn.size()-4);
138 if( truncIn.find( getFileId(NEQUI, true) + "_" ) == string::npos ) return false;
143 int configcache::readHeader()
145 long unsigned int headersize;
147 if( readDataToMem((char *)&headersize, sizeof(long unsigned int)) == sizeof(long unsigned int) && inFile.is_open() ) {
148 if ( headersize == 0 )
149 return HEADER_READLAST;
151 pair<unsigned long, void *> newHeader;
153 if( readDataToMem((char *)&newHeader.first, sizeof(unsigned long)) == sizeof(unsigned long) && inFile.is_open() ) {
154 newHeader.second = malloc(headersize);
156 if( readDataToMem((char *)newHeader.second, headersize) == headersize && inFile.is_open() ) {
157 headerStore.push_back(newHeader);
158 return HEADER_READOK;
161 if(log) *log << "CCACHE: Could not read heade-data! Closing dat-file: " << openFileDesc.filename << endl << flush;
163 return HEADER_READERR;
167 if(log) *log << "CCACHE: Could not read headerid-hash! Closing dat-file: " << openFileDesc.filename << endl << flush;
169 return HEADER_READERR;
173 if(log) *log << "CCACHE: Could not read header size. Closing dat-file: " << openFileDesc.filename << endl << flush;
175 return HEADER_READERR;
179 bool configcache::readAllHeaders()
181 int readHeaderStatus;
186 readHeaderStatus = readHeader();
188 while ( readHeaderStatus == HEADER_READOK );
190 if ( readHeaderStatus == HEADER_READLAST ) return true;
191 else if ( readHeaderStatus == HEADER_READERR ) return false;
194 void * configcache::getHeader(const string& headerid) {
195 for (vector< pair<unsigned long, void *> >::iterator headerStoreIt = headerStore.begin(); headerStoreIt != headerStore.end(); ++headerStoreIt)
196 if ( headerStoreIt->first == hash(headerid) )
197 return headerStoreIt->second;
202 void configcache::readConfig(bool *readnewconfig, int *nequileft, vector<unsigned long> *excludeFileHashes)
204 *readnewconfig = false;
206 if( DATADIR == "" || !(MODE==CACHE_MODE_RO||MODE==CACHE_MODE_RW) ) return;
208 if(refetchDataFiles){
209 refetchDataFiles = false;
215 vector<infiledesc>::iterator inFileIt = getNextInfile(excludeFileHashes);
216 int iDidVirtualSkips;
218 if( (!inFile.is_open()) && inFileIt == inFiles.end() ) {
220 *nequileft = nequileft_internal;
224 while( (!inFile.is_open()) && inFiles.size() > 0 ) {
225 string inFileParaString;
227 openFileDesc = *inFileIt;
229 if (openFileDesc.nequi < NEQUI)
230 doVirtualEquilibration = true;
232 doVirtualEquilibration = false;
234 firstUsedConfig = true;
236 if(log) *log << "CCACHE: Opening dat-file: " << inFileIt->filename << endl << flush;
237 inFile.open( (DATADIR + "/" + inFileIt->filename).c_str(), std::ios::binary );
239 getline( inFile, inFileParaString );
240 if( inFileParaString != paraString() ) {
241 if(log) *log << "CCACHE: Parastring does not match. Closing dat-file..." << endl << flush;
245 inFiles.erase(inFileIt);
247 if( !inFile.is_open() ) continue;
249 ioBuffers->in = new boost::iostreams::filtering_istreambuf;
250 ioBuffers->in->push( boost::iostreams::bzip2_decompressor() );
251 ioBuffers->in->push(inFile);
254 if( inFile.is_open() )
256 if (doVirtualEquilibration) {
257 if(log) *log << "CCACHE: Trying virtual equilibration." << endl << flush;
258 doVirtualEquilibration = false;
259 for (iDidVirtualSkips=0; iDidVirtualSkips < (NEQUI-openFileDesc.nequi)/openFileDesc.nskip; iDidVirtualSkips++) {
260 if( readFullBlock(tmpConfig, configSize) != configSize || ! inFile.is_open() )
262 else if ( (NEQUI-openFileDesc.nequi) - (iDidVirtualSkips+1)*openFileDesc.nskip < nequileft_internal ) {
263 memcpy(configMem, tmpConfig, configSize);
264 nequileft_internal = NEQUI - openFileDesc.nequi - (iDidVirtualSkips+1)*openFileDesc.nskip;
265 *readnewconfig = true;
266 firstUsedConfig = false;
271 if( readFullBlock(tmpConfig, configSize) == configSize && inFile.is_open() )
273 memcpy(configMem, tmpConfig, configSize);
274 *readnewconfig = true;
275 if (firstUsedConfig) {
276 firstUsedConfig = false;
277 if (openFileDesc.nequi < NEQUI)
278 nequileft_internal = NEQUI - openFileDesc.nequi - iDidVirtualSkips*openFileDesc.nskip;
280 nequileft_internal = NEQUI - openFileDesc.nequi;
282 nequileft_internal -= openFileDesc.nskip;
283 *nequileft = nequileft_internal;
287 if(log) *log << "CCACHE: Could not read configuration. Closing dat-file: " << openFileDesc.filename << endl << flush;
294 void configcache::openOutFile(int actnequi)
296 time_t secstamp = time(NULL);
301 outFileName << DATADIR << "/" << secstamp << "." << iseq << "_" << getFileId(actnequi) << "_.edat.tmp";
303 int tmpfd = open(outFileName.str().c_str(), O_CREAT | O_EXCL, 0644);
309 else if ( errno != EEXIST ) {
310 if(log) *log << "CCACHE: Could not create cachefile!" << endl << flush;
317 outFile.open( outFileName.str().c_str(), std::ios::binary );
319 outFile << paraString() << endl;
321 ioBuffers->out = new boost::iostreams::filtering_ostreambuf;
322 ioBuffers->out->push(boost::iostreams::bzip2_compressor());
323 ioBuffers->out->push(outFile);
326 void configcache::writeHeader(const string& headerid, const char *header, long unsigned int size, int actnequi) {
327 unsigned long headeridhash;
329 if( DATADIR == "" || !(MODE==CACHE_MODE_WO||MODE==CACHE_MODE_RW) ) return;
331 if(!outFile.is_open())
332 openOutFile(actnequi);
334 headeridhash = hash(headerid);
336 boost::iostreams::write(*ioBuffers->out, (char*)&size, sizeof(long unsigned int));
337 boost::iostreams::write(*ioBuffers->out, (char*)&headeridhash, sizeof(unsigned long));
338 boost::iostreams::write(*ioBuffers->out, header, size);
341 void configcache::writeConfig(int actnequi)
343 long unsigned int zeroheader=0;
345 if ( DATADIR == "" || !(MODE==CACHE_MODE_WO||MODE==CACHE_MODE_RW) ) return;
347 if ( ! outFile.is_open() )
348 openOutFile(actnequi);
350 boost::iostreams::write(*ioBuffers->out, (char*)&zeroheader, sizeof(long unsigned int));
352 boost::iostreams::write(*ioBuffers->out, configMem, configSize);
355 void configcache::addPara(const string& parid, const double& val){
359 Paras.push_back(newPara);
362 int configcache::getParIndex(const string& parid){
363 for(int ipara=0; ipara<Paras.size(); ipara++)
364 if(Paras[ipara].id == parid) return ipara;
367 void configcache::setPara(const string& parid, const double& value){
368 Paras[getParIndex(parid)].val = value;
371 if(ioBuffers->in != NULL) { delete ioBuffers->in; ioBuffers->in=NULL; }
375 refetchDataFiles = true;
376 nequileft_internal = NEQUI;
379 configcache::~configcache()
382 delete ioBuffers->in;
383 ioBuffers->in = NULL;
386 void configcache::finishOutFile()
388 if( ioBuffers->out != NULL )
390 delete ioBuffers->out;
391 ioBuffers->out = NULL;
394 if( outFile.is_open() )
397 rename( outFileName.str().c_str(), outFileName.str().substr(0, outFileName.str().size()-4).c_str() );
401 int configcache::readFullBlock(char *tmpData, long unsigned int dataSize)
403 /* try to read header */
404 if ( openFileDesc.extended )
405 if ( ! readAllHeaders() )
409 return readDataToMem(tmpData, dataSize);
412 int configcache::readDataToMem(char *tmpData, long unsigned int dataSize)
416 if ( dataSize == 0 ) return 0;
418 try { readturn = boost::iostreams::read(*ioBuffers->in, tmpData, dataSize); }
419 catch(boost::iostreams::bzip2_error& error) {
420 if(log) *log << "CCACHE: Caught bzip2 exception with error code: " << error.error() << endl << flush;
423 catch (std::exception const& ex) {
424 if(log) *log << "CCACHE: Caught exception: " << ex.what() << endl << flush;
428 if(log) *log << "CCACHE: Caught unknown exception while reading." << endl << flush;
435 unsigned long configcache::hash(const string& str)
437 unsigned long hash = 5381;
439 for(string::const_iterator it=str.begin();it!=str.end();it++)
440 hash = ((hash << 5) + hash) + *it; /* hash * 33 + character */
445 void configcache::deleteHeaderStore()
447 while ( headerStore.size() > 0 ) {
448 free(headerStore.back().second);
449 headerStore.pop_back();
453 vector<infiledesc>::iterator configcache::getNextInfile(vector<unsigned long> *excludeFileHashes) {
454 for (vector<infiledesc>::iterator init = inFiles.begin(); init != inFiles.end(); ++init) {
455 if (excludeFileHashes != NULL) {
456 bool excludethisfile = false;
458 for (vector<unsigned long>::iterator exit = excludeFileHashes->begin(); exit != excludeFileHashes->end(); ++exit)
459 if ( *exit == hash(init->filename) ) {
460 excludethisfile = true;
469 return inFiles.end();