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& superextended, const bool& shortid)
69 if(!shortid) fileid << CACHEID << "_" << actnequi << "_" << NSKIP;
72 fileid << "_" << hash( paraString() );
74 fileid << "_" << paraString();
79 void configcache::fetchDataFiles()
81 struct dirent *de=NULL;
83 static infiledesc filedesc;
85 d=opendir(DATADIR.c_str());
87 while(de = readdir(d)){
88 string filename = de->d_name;
89 if(isValidInFile(filename, &filedesc))
91 inFiles.push_back(filedesc);
97 bool configcache::isValidInFile(const string& infile, infiledesc *filedesc)
99 char *inchar, *inParts;
100 string truncIn, truncOut;
102 filedesc->filename = infile;
104 if( infile.size() < 4 ) return false;
106 if( infile.substr(infile.size()-4) == ".dat" ) {
107 filedesc->extended = false;
108 filedesc->superextended = false;
110 else if( infile.substr(infile.size()-4) == "edat" ) {
111 filedesc->extended = true;
112 filedesc->superextended = false;
114 else if( infile.substr(infile.size()-4) == "sdat" ) {
115 filedesc->extended = true;
116 filedesc->superextended = true;
121 inchar = new char [infile.size()+1];
122 strcpy (inchar, infile.c_str());
124 inParts = strtok( inchar, "_" );
125 for(int iPart=0; inParts!=NULL; iPart++)
127 if( iPart>3 ) { truncIn += "_"; truncIn += inParts; }
131 case 1: if(inParts != CACHEID)
135 filedesc->nequi = atoi(inParts);
138 if(atoi(inParts) != NSKIP)
140 filedesc->nskip = atoi(inParts);
143 inParts = strtok( NULL, "_");
145 truncIn = truncIn.substr(0, truncIn.size()-4);
149 if( truncIn.find( getFileId(NEQUI, filedesc->superextended, true) + "_" ) == string::npos ) return false;
154 int configcache::readHeader()
156 long unsigned int headersize;
158 if( readDataToMem((char *)&headersize, sizeof(long unsigned int)) == sizeof(long unsigned int) && inFile.is_open() ) {
159 if ( headersize == 0 )
160 return HEADER_READLAST;
162 pair<unsigned long, void *> newHeader;
164 if( readDataToMem((char *)&newHeader.first, sizeof(unsigned long)) == sizeof(unsigned long) && inFile.is_open() ) {
165 newHeader.second = malloc(headersize);
167 if( readDataToMem((char *)newHeader.second, headersize) == headersize && inFile.is_open() ) {
168 headerStore.push_back(newHeader);
169 return HEADER_READOK;
172 if(log) *log << "CCACHE: Could not read heade-data! Closing dat-file: " << openFileDesc.filename << endl << flush;
174 return HEADER_READERR;
178 if(log) *log << "CCACHE: Could not read headerid-hash! Closing dat-file: " << openFileDesc.filename << endl << flush;
180 return HEADER_READERR;
184 if(log) *log << "CCACHE: Could not read header size. Closing dat-file: " << openFileDesc.filename << endl << flush;
186 return HEADER_READERR;
190 bool configcache::readAllHeaders()
192 int readHeaderStatus;
197 readHeaderStatus = readHeader();
199 while ( readHeaderStatus == HEADER_READOK );
201 if ( readHeaderStatus == HEADER_READLAST ) return true;
202 else if ( readHeaderStatus == HEADER_READERR ) return false;
205 void * configcache::getHeader(const string& headerid) {
206 for (vector< pair<unsigned long, void *> >::iterator headerStoreIt = headerStore.begin(); headerStoreIt != headerStore.end(); ++headerStoreIt)
207 if ( headerStoreIt->first == hash(headerid) )
208 return headerStoreIt->second;
213 void configcache::readConfig(bool *readnewconfig, int *nequileft, vector<unsigned long> *excludeFileHashes)
215 *readnewconfig = false;
217 if( DATADIR == "" || !(MODE==CACHE_MODE_RO||MODE==CACHE_MODE_RW) ) return;
219 if(refetchDataFiles){
220 refetchDataFiles = false;
226 vector<infiledesc>::iterator inFileIt = getNextInfile(excludeFileHashes);
227 int iDidVirtualSkips;
229 if( (!inFile.is_open()) && inFileIt == inFiles.end() ) {
231 *nequileft = nequileft_internal;
235 while( (!inFile.is_open()) && inFiles.size() > 0 ) {
236 string inFileParaString;
238 openFileDesc = *inFileIt;
240 if (openFileDesc.nequi < NEQUI)
241 doVirtualEquilibration = true;
243 doVirtualEquilibration = false;
245 firstUsedConfig = true;
247 if(log) *log << "CCACHE: Opening dat-file: " << inFileIt->filename << endl << flush;
248 inFile.open( (DATADIR + "/" + inFileIt->filename).c_str(), std::ios::binary );
250 if( openFileDesc.superextended ) {
251 getline( inFile, inFileParaString );
252 if( inFileParaString != paraString() ) {
253 if(log) *log << "CCACHE: Parastring does not match. Closing dat-file..." << endl << flush;
258 inFiles.erase(inFileIt);
260 if( !inFile.is_open() ) continue;
262 ioBuffers->in = new boost::iostreams::filtering_istreambuf;
263 ioBuffers->in->push( boost::iostreams::bzip2_decompressor() );
264 ioBuffers->in->push(inFile);
267 if( inFile.is_open() )
269 if (doVirtualEquilibration) {
270 if(log) *log << "CCACHE: Trying virtual equilibration." << endl << flush;
271 doVirtualEquilibration = false;
272 for (iDidVirtualSkips=0; iDidVirtualSkips < (NEQUI-openFileDesc.nequi)/openFileDesc.nskip; iDidVirtualSkips++) {
273 if( readFullBlock(tmpConfig, configSize) != configSize || ! inFile.is_open() )
275 else if ( (NEQUI-openFileDesc.nequi) - (iDidVirtualSkips+1)*openFileDesc.nskip < nequileft_internal ) {
276 memcpy(configMem, tmpConfig, configSize);
277 nequileft_internal = NEQUI - openFileDesc.nequi - (iDidVirtualSkips+1)*openFileDesc.nskip;
278 *readnewconfig = true;
279 firstUsedConfig = false;
284 if( readFullBlock(tmpConfig, configSize) == configSize && inFile.is_open() )
286 memcpy(configMem, tmpConfig, configSize);
287 *readnewconfig = true;
288 if (firstUsedConfig) {
289 firstUsedConfig = false;
290 if (openFileDesc.nequi < NEQUI)
291 nequileft_internal = NEQUI - openFileDesc.nequi - iDidVirtualSkips*openFileDesc.nskip;
293 nequileft_internal = NEQUI - openFileDesc.nequi;
295 nequileft_internal -= openFileDesc.nskip;
296 *nequileft = nequileft_internal;
300 if(log) *log << "CCACHE: Could not read configuration. Closing dat-file: " << openFileDesc.filename << endl << flush;
307 void configcache::openOutFile(int actnequi)
309 time_t secstamp = time(NULL);
314 outFileName << DATADIR << "/" << secstamp << "." << iseq << "_" << getFileId(actnequi, true, false) << "_.sdat.tmp";
316 int tmpfd = open(outFileName.str().c_str(), O_CREAT | O_EXCL, 0644);
322 else if ( errno != EEXIST ) {
323 if(log) *log << "CCACHE: Could not create cachefile!" << endl << flush;
330 outFile.open( outFileName.str().c_str(), std::ios::binary );
332 outFile << paraString() << endl;
334 ioBuffers->out = new boost::iostreams::filtering_ostreambuf;
335 ioBuffers->out->push(boost::iostreams::bzip2_compressor());
336 ioBuffers->out->push(outFile);
339 void configcache::writeHeader(const string& headerid, const char *header, long unsigned int size, int actnequi) {
340 unsigned long headeridhash;
342 if( DATADIR == "" || !(MODE==CACHE_MODE_WO||MODE==CACHE_MODE_RW) ) return;
344 if(!outFile.is_open())
345 openOutFile(actnequi);
347 headeridhash = hash(headerid);
349 boost::iostreams::write(*ioBuffers->out, (char*)&size, sizeof(long unsigned int));
350 boost::iostreams::write(*ioBuffers->out, (char*)&headeridhash, sizeof(unsigned long));
351 boost::iostreams::write(*ioBuffers->out, header, size);
354 void configcache::writeConfig(int actnequi)
356 long unsigned int zeroheader=0;
358 if ( DATADIR == "" || !(MODE==CACHE_MODE_WO||MODE==CACHE_MODE_RW) ) return;
360 if ( ! outFile.is_open() )
361 openOutFile(actnequi);
363 boost::iostreams::write(*ioBuffers->out, (char*)&zeroheader, sizeof(long unsigned int));
365 boost::iostreams::write(*ioBuffers->out, configMem, configSize);
368 void configcache::addPara(const string& parid, const double& val){
372 Paras.push_back(newPara);
375 int configcache::getParIndex(const string& parid){
376 for(int ipara=0; ipara<Paras.size(); ipara++)
377 if(Paras[ipara].id == parid) return ipara;
380 void configcache::setPara(const string& parid, const double& value){
381 Paras[getParIndex(parid)].val = value;
384 if(ioBuffers->in != NULL) { delete ioBuffers->in; ioBuffers->in=NULL; }
388 refetchDataFiles = true;
389 nequileft_internal = NEQUI;
392 configcache::~configcache()
395 delete ioBuffers->in;
396 ioBuffers->in = NULL;
399 void configcache::finishOutFile()
401 if( ioBuffers->out != NULL )
403 delete ioBuffers->out;
404 ioBuffers->out = NULL;
407 if( outFile.is_open() )
410 rename( outFileName.str().c_str(), outFileName.str().substr(0, outFileName.str().size()-4).c_str() );
414 int configcache::readFullBlock(char *tmpData, long unsigned int dataSize)
416 /* try to read header */
417 if ( openFileDesc.extended )
418 if ( ! readAllHeaders() )
422 return readDataToMem(tmpData, dataSize);
425 int configcache::readDataToMem(char *tmpData, long unsigned int dataSize)
429 if ( dataSize == 0 ) return 0;
431 try { readturn = boost::iostreams::read(*ioBuffers->in, tmpData, dataSize); }
432 catch(boost::iostreams::bzip2_error& error) {
433 if(log) *log << "CCACHE: Caught bzip2 exception with error code: " << error.error() << endl << flush;
436 catch (std::exception const& ex) {
437 if(log) *log << "CCACHE: Caught exception: " << ex.what() << endl << flush;
441 if(log) *log << "CCACHE: Caught unknown exception while reading." << endl << flush;
448 unsigned long configcache::hash(const string& str)
450 unsigned long hash = 5381;
452 for(string::const_iterator it=str.begin();it!=str.end();it++)
453 hash = ((hash << 5) + hash) + *it; /* hash * 33 + character */
458 void configcache::deleteHeaderStore()
460 while ( headerStore.size() > 0 ) {
461 free(headerStore.back().second);
462 headerStore.pop_back();
466 vector<infiledesc>::iterator configcache::getNextInfile(vector<unsigned long> *excludeFileHashes) {
467 for (vector<infiledesc>::iterator init = inFiles.begin(); init != inFiles.end(); ++init) {
468 if (excludeFileHashes != NULL) {
469 bool excludethisfile = false;
471 for (vector<unsigned long>::iterator exit = excludeFileHashes->begin(); exit != excludeFileHashes->end(); ++exit)
472 if ( *exit == hash(init->filename) ) {
473 excludethisfile = true;
482 return inFiles.end();