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 if ( cacheid.find("_") != -1 ) {
22 if(log) *log << "CCACHE: Invalid cacheid \"" << cacheid << "\" given. Cacheids must not contain underscores!" << endl << flush;
26 configMem = (char*)malloc(configMemSize);
27 tmpConfig = (char*)malloc(configMemSize);
29 *configmem = configMem;
30 configSize = configMemSize;
37 refetchDataFiles = false;
40 string configcache::getFileId(int actnequi, const bool& shortid)
44 if(!shortid) fileid << CACHEID << "_" << actnequi << "_" << NSKIP;
45 for(int ipara=0; ipara<Paras.size(); ipara++)
46 fileid << "_" << Paras[ipara].id << Paras[ipara].val;
51 void configcache::fetchDataFiles()
53 struct dirent *de=NULL;
55 static infiledesc filedesc;
57 d=opendir(DATADIR.c_str());
59 while(de = readdir(d)){
60 string filename = de->d_name;
61 if(isValidInFile(filename, &filedesc))
63 inFiles.push_back(filedesc);
69 bool configcache::isValidInFile(const string& infile, infiledesc *filedesc)
71 char *inchar, *inParts;
72 string truncIn, truncOut;
74 filedesc->filename = infile;
75 filedesc->doVirtualEquilibration = false;
76 filedesc->firstUsedConfig = true;
78 if( infile.size() < 4 ) return false;
80 if( infile.substr(infile.size()-4) == ".dat" )
81 filedesc->extended = false;
82 else if( infile.substr(infile.size()-4) == "edat" )
83 filedesc->extended = true;
87 inchar = new char [infile.size()+1];
88 strcpy (inchar, infile.c_str());
90 inParts = strtok( inchar, "_" );
91 for(int iPart=0; inParts!=NULL; iPart++)
93 if( iPart>3 ) { truncIn += "_"; truncIn += inParts; }
97 case 1: if(inParts != CACHEID)
101 if (atoi(inParts) < NEQUI)
102 filedesc->doVirtualEquilibration = true;
103 filedesc->nequi = atoi(inParts);
106 if(atoi(inParts) != NSKIP)
108 filedesc->nskip = atoi(inParts);
111 inParts = strtok( NULL, "_");
113 truncIn = truncIn.substr(0, truncIn.size()-4);
117 if( truncIn.find( getFileId(NEQUI, true) + "_" ) == string::npos ) return false;
122 int configcache::readHeader()
124 long unsigned int headersize;
126 if( readDataToMem((char *)&headersize, sizeof(long unsigned int)) == sizeof(long unsigned int) && inFile.is_open() ) {
127 if ( headersize == 0 )
128 return HEADER_READLAST;
130 pair<unsigned long, void *> newHeader;
132 if( readDataToMem((char *)&newHeader.first, sizeof(unsigned long)) == sizeof(unsigned long) && inFile.is_open() ) {
133 newHeader.second = malloc(headersize);
135 if( readDataToMem((char *)newHeader.second, headersize) == headersize && inFile.is_open() ) {
136 headerStore.push_back(newHeader);
137 return HEADER_READOK;
140 if(log) *log << "CCACHE: Could not read heade-data! Closing dat-file: " << openFileDesc.filename << endl << flush;
142 return HEADER_READERR;
146 if(log) *log << "CCACHE: Could not read headerid-hash! Closing dat-file: " << openFileDesc.filename << endl << flush;
148 return HEADER_READERR;
152 if(log) *log << "CCACHE: Could not read header size. Closing dat-file: " << openFileDesc.filename << endl << flush;
154 return HEADER_READERR;
158 bool configcache::readAllHeaders()
160 int readHeaderStatus;
165 readHeaderStatus = readHeader();
167 while ( readHeaderStatus == HEADER_READOK );
169 if ( readHeaderStatus == HEADER_READLAST ) return true;
170 else if ( readHeaderStatus == HEADER_READERR ) return false;
173 void * configcache::getHeader(const string& headerid) {
174 for (vector< pair<unsigned long, void *> >::iterator headerStoreIt = headerStore.begin(); headerStoreIt != headerStore.end(); ++headerStoreIt)
175 if ( headerStoreIt->first == hash(headerid) )
176 return headerStoreIt->second;
181 /* returns number of equilibration-steps left with new read configuration
182 or zero if no new configuration was read */
183 void configcache::readConfig(bool& readnewconfig, int& nequileft, vector<unsigned long> *excludeFileHashes)
185 readnewconfig = false;
187 if(DATADIR == "" || MODE == CACHE_MODE_DISABLED) return;
189 if(refetchDataFiles){
190 refetchDataFiles = false;
196 vector<infiledesc>::iterator inFileIt = getNextInfile(excludeFileHashes);
197 int iDidVirtualSkips;
199 if( (!inFile.is_open()) && inFileIt == inFiles.end() ) {
200 cout << "RETURNING..." << endl;
201 nequileft = nequileft_internal;
205 while( (!inFile.is_open()) && inFiles.size() > 0 ) {
206 openFileDesc = *inFileIt;
208 if(log) *log << "CCACHE: Opening dat-file: " << inFileIt->filename << endl << flush;
209 inFile.open( (DATADIR + "/" + inFileIt->filename).c_str(), std::ios::binary );
211 inFiles.erase(inFileIt);
213 if( !inFile.is_open() ) continue;
215 inBuffer = new boost::iostreams::filtering_istreambuf;
216 inBuffer->push( boost::iostreams::bzip2_decompressor() );
217 inBuffer->push(inFile);
220 if( inFile.is_open() )
222 if (openFileDesc.doVirtualEquilibration) {
223 if(log) *log << "CCACHE: Trying virtual equilibration." << endl << flush;
224 openFileDesc.doVirtualEquilibration = false;
225 for (iDidVirtualSkips=0; iDidVirtualSkips < (NEQUI-openFileDesc.nequi)/openFileDesc.nskip; iDidVirtualSkips++) {
226 if( readFullBlock(tmpConfig, configSize) != configSize || ! inFile.is_open() )
228 else if ( (NEQUI-openFileDesc.nequi) - (iDidVirtualSkips+1)*openFileDesc.nskip < nequileft_internal ) {
229 memcpy(configMem, tmpConfig, configSize);
230 nequileft_internal = NEQUI - openFileDesc.nequi - (iDidVirtualSkips+1)*openFileDesc.nskip;
231 cout << "blabla:" << nequileft_internal << endl;
232 readnewconfig = true;
233 openFileDesc.firstUsedConfig = false;
238 if( readFullBlock(tmpConfig, configSize) == configSize && inFile.is_open() )
240 memcpy(configMem, tmpConfig, configSize);
241 readnewconfig = true;
242 if (openFileDesc.firstUsedConfig) {
243 openFileDesc.firstUsedConfig = false;
244 if (NEQUI < openFileDesc.nequi)
245 nequileft_internal = NEQUI - openFileDesc.nequi - iDidVirtualSkips*openFileDesc.nskip;
247 nequileft_internal = NEQUI - openFileDesc.nequi;
249 nequileft_internal -= openFileDesc.nskip;
250 cout << "sadasd:" << nequileft_internal << endl;
251 nequileft = nequileft_internal;
255 if(log) *log << "CCACHE: Could not read configuration. Closing dat-file: " << openFileDesc.filename << endl << flush;
262 void configcache::openOutFile(int actnequi)
264 time_t secstamp = time(NULL);
267 outFileName << DATADIR << "/" << secstamp << "_" << getFileId(actnequi) << "_.edat.tmp";
269 outFile.open( outFileName.str().c_str(), std::ios::binary );
271 outBuffer = new boost::iostreams::filtering_ostreambuf;
272 outBuffer->push(boost::iostreams::bzip2_compressor());
273 outBuffer->push(outFile);
276 void configcache::writeHeader(const string& headerid, const char *header, long unsigned int size, int actnequi) {
277 unsigned long headeridhash;
279 if( DATADIR == "" || MODE < 2 ) return;
281 if(!outFile.is_open())
282 openOutFile(actnequi);
284 headeridhash = hash(headerid);
286 boost::iostreams::write(*outBuffer, (char*)&size, sizeof(long unsigned int));
287 boost::iostreams::write(*outBuffer, (char*)&headeridhash, sizeof(unsigned long));
288 boost::iostreams::write(*outBuffer, header, size);
291 void configcache::writeConfig(int actnequi)
293 long unsigned int zeroheader=0;
295 if ( DATADIR == "" || MODE < 2 ) return;
297 if ( ! outFile.is_open() )
298 openOutFile(actnequi);
300 boost::iostreams::write(*outBuffer, (char*)&zeroheader, sizeof(long unsigned int));
302 boost::iostreams::write(*outBuffer, configMem, configSize);
305 void configcache::addPara(const string& parid, const double& val){
309 Paras.push_back(newPara);
312 int configcache::getParIndex(const string& parid){
313 for(int ipara=0; ipara<Paras.size(); ipara++)
314 if(Paras[ipara].id == parid) return ipara;
317 void configcache::setPara(const string& parid, const double& value){
318 Paras[getParIndex(parid)].val = value;
320 if(inBuffer != NULL) { delete inBuffer; inBuffer=NULL; }
324 refetchDataFiles = true;
327 configcache::~configcache()
334 void configcache::finishOutFile()
336 if( outBuffer != NULL )
342 if( outFile.is_open() )
345 rename( outFileName.str().c_str(), outFileName.str().substr(0, outFileName.str().size()-4).c_str() );
349 int configcache::readFullBlock(char *tmpData, long unsigned int dataSize)
351 /* try to read header */
352 if ( openFileDesc.extended )
353 if ( ! readAllHeaders() )
357 return readDataToMem(tmpData, dataSize);
360 int configcache::readDataToMem(char *tmpData, long unsigned int dataSize)
364 if ( dataSize == 0 ) return 0;
366 try { readturn = boost::iostreams::read(*inBuffer, tmpData, dataSize); }
367 catch(boost::iostreams::bzip2_error& error) {
368 if(log) *log << "CCACHE: Caught bzip2 exception with error code: " << error.error() << endl << flush;
371 catch (std::exception const& ex) {
372 if(log) *log << "CCACHE: Caught exception: " << ex.what() << endl << flush;
376 if(log) *log << "CCACHE: Caught unknown exception while reading." << endl << flush;
383 unsigned long configcache::hash(const string& str)
385 unsigned long hash = 5381;
387 for(string::const_iterator it=str.begin();it!=str.end();it++)
388 hash = ((hash << 5) + hash) + *it; /* hash * 33 + character */
393 void configcache::deleteHeaderStore()
395 while ( headerStore.size() > 0 ) {
396 free(headerStore.back().second);
397 headerStore.pop_back();
401 vector<infiledesc>::iterator configcache::getNextInfile(vector<unsigned long> *excludeFileHashes) {
402 for (vector<infiledesc>::iterator init = inFiles.begin(); init != inFiles.end(); ++init) {
403 if (excludeFileHashes != NULL) {
404 bool excludethisfile = false;
406 for (vector<unsigned long>::iterator exit = excludeFileHashes->begin(); exit != excludeFileHashes->end(); ++exit)
407 if ( *exit == hash(init->filename) ) {
408 excludethisfile = true;
417 return inFiles.end();