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;
76 if( infile.size() < 4 ) return false;
78 if( infile.substr(infile.size()-4) == ".dat" )
79 filedesc->extended = false;
80 else if( infile.substr(infile.size()-4) == "edat" )
81 filedesc->extended = true;
85 inchar = new char [infile.size()+1];
86 strcpy (inchar, infile.c_str());
88 inParts = strtok( inchar, "_" );
89 for(int iPart=0; inParts!=NULL; iPart++)
91 if( iPart>3 ) { truncIn += "_"; truncIn += inParts; }
95 case 1: if(inParts != CACHEID)
99 filedesc->nequi = atoi(inParts);
102 if(atoi(inParts) != NSKIP)
104 filedesc->nskip = atoi(inParts);
107 inParts = strtok( NULL, "_");
109 truncIn = truncIn.substr(0, truncIn.size()-4);
113 if( truncIn.find( getFileId(NEQUI, true) + "_" ) == string::npos ) return false;
118 int configcache::readHeader()
120 long unsigned int headersize;
122 if( readDataToMem((char *)&headersize, sizeof(long unsigned int)) == sizeof(long unsigned int) && inFile.is_open() ) {
123 if ( headersize == 0 )
124 return HEADER_READLAST;
126 pair<unsigned long, void *> newHeader;
128 if( readDataToMem((char *)&newHeader.first, sizeof(unsigned long)) == sizeof(unsigned long) && inFile.is_open() ) {
129 newHeader.second = malloc(headersize);
131 if( readDataToMem((char *)newHeader.second, headersize) == headersize && inFile.is_open() ) {
132 headerStore.push_back(newHeader);
133 return HEADER_READOK;
136 if(log) *log << "CCACHE: Could not read heade-data! Closing dat-file: " << openFileDesc.filename << endl << flush;
138 return HEADER_READERR;
142 if(log) *log << "CCACHE: Could not read headerid-hash! Closing dat-file: " << openFileDesc.filename << endl << flush;
144 return HEADER_READERR;
148 if(log) *log << "CCACHE: Could not read header size. Closing dat-file: " << openFileDesc.filename << endl << flush;
150 return HEADER_READERR;
154 bool configcache::readAllHeaders()
156 int readHeaderStatus;
161 readHeaderStatus = readHeader();
163 while ( readHeaderStatus == HEADER_READOK );
165 if ( readHeaderStatus == HEADER_READLAST ) return true;
166 else if ( readHeaderStatus == HEADER_READERR ) return false;
169 void * configcache::getHeader(const string& headerid) {
170 for (vector< pair<unsigned long, void *> >::iterator headerStoreIt = headerStore.begin(); headerStoreIt != headerStore.end(); ++headerStoreIt)
171 if ( headerStoreIt->first == hash(headerid) )
172 return headerStoreIt->second;
177 /* returns number of equilibration-steps left with new read configuration
178 or zero if no new configuration was read */
179 void configcache::readConfig(bool& readnewconfig, int& nequileft, vector<unsigned long> *excludeFileHashes)
181 readnewconfig = false;
183 if(DATADIR == "" || MODE == CACHE_MODE_DISABLED) return;
185 if(refetchDataFiles){
186 refetchDataFiles = false;
192 vector<infiledesc>::iterator inFileIt = getNextInfile(excludeFileHashes);
193 int iDidVirtualSkips;
195 if( (!inFile.is_open()) && inFileIt == inFiles.end() ) {
197 nequileft = nequileft_internal;
201 while( (!inFile.is_open()) && inFiles.size() > 0 ) {
202 openFileDesc = *inFileIt;
204 if (openFileDesc.nequi < NEQUI)
205 doVirtualEquilibration = true;
207 doVirtualEquilibration = false;
209 firstUsedConfig = true;
211 if(log) *log << "CCACHE: Opening dat-file: " << inFileIt->filename << endl << flush;
212 inFile.open( (DATADIR + "/" + inFileIt->filename).c_str(), std::ios::binary );
214 inFiles.erase(inFileIt);
216 if( !inFile.is_open() ) continue;
218 inBuffer = new boost::iostreams::filtering_istreambuf;
219 inBuffer->push( boost::iostreams::bzip2_decompressor() );
220 inBuffer->push(inFile);
223 if( inFile.is_open() )
225 if (doVirtualEquilibration) {
226 if(log) *log << "CCACHE: Trying virtual equilibration." << endl << flush;
227 doVirtualEquilibration = false;
228 for (iDidVirtualSkips=0; iDidVirtualSkips < (NEQUI-openFileDesc.nequi)/openFileDesc.nskip; iDidVirtualSkips++) {
229 if( readFullBlock(tmpConfig, configSize) != configSize || ! inFile.is_open() )
231 else if ( (NEQUI-openFileDesc.nequi) - (iDidVirtualSkips+1)*openFileDesc.nskip < nequileft_internal ) {
232 memcpy(configMem, tmpConfig, configSize);
233 nequileft_internal = NEQUI - openFileDesc.nequi - (iDidVirtualSkips+1)*openFileDesc.nskip;
234 readnewconfig = true;
235 firstUsedConfig = false;
240 if( readFullBlock(tmpConfig, configSize) == configSize && inFile.is_open() )
242 memcpy(configMem, tmpConfig, configSize);
243 readnewconfig = true;
244 if (firstUsedConfig) {
245 firstUsedConfig = false;
246 if (openFileDesc.nequi < NEQUI)
247 nequileft_internal = NEQUI - openFileDesc.nequi - iDidVirtualSkips*openFileDesc.nskip;
249 nequileft_internal = NEQUI - openFileDesc.nequi;
251 nequileft_internal -= openFileDesc.nskip;
252 nequileft = nequileft_internal;
256 if(log) *log << "CCACHE: Could not read configuration. Closing dat-file: " << openFileDesc.filename << endl << flush;
263 void configcache::openOutFile(int actnequi)
265 time_t secstamp = time(NULL);
268 outFileName << DATADIR << "/" << secstamp << "_" << getFileId(actnequi) << "_.edat.tmp";
270 outFile.open( outFileName.str().c_str(), std::ios::binary );
272 outBuffer = new boost::iostreams::filtering_ostreambuf;
273 outBuffer->push(boost::iostreams::bzip2_compressor());
274 outBuffer->push(outFile);
277 void configcache::writeHeader(const string& headerid, const char *header, long unsigned int size, int actnequi) {
278 unsigned long headeridhash;
280 if( DATADIR == "" || MODE < 2 ) return;
282 if(!outFile.is_open())
283 openOutFile(actnequi);
285 headeridhash = hash(headerid);
287 boost::iostreams::write(*outBuffer, (char*)&size, sizeof(long unsigned int));
288 boost::iostreams::write(*outBuffer, (char*)&headeridhash, sizeof(unsigned long));
289 boost::iostreams::write(*outBuffer, header, size);
292 void configcache::writeConfig(int actnequi)
294 long unsigned int zeroheader=0;
296 if ( DATADIR == "" || MODE < 2 ) return;
298 if ( ! outFile.is_open() )
299 openOutFile(actnequi);
301 boost::iostreams::write(*outBuffer, (char*)&zeroheader, sizeof(long unsigned int));
303 boost::iostreams::write(*outBuffer, configMem, configSize);
306 void configcache::addPara(const string& parid, const double& val){
310 Paras.push_back(newPara);
313 int configcache::getParIndex(const string& parid){
314 for(int ipara=0; ipara<Paras.size(); ipara++)
315 if(Paras[ipara].id == parid) return ipara;
318 void configcache::setPara(const string& parid, const double& value){
319 Paras[getParIndex(parid)].val = value;
321 if(inBuffer != NULL) { delete inBuffer; inBuffer=NULL; }
325 refetchDataFiles = true;
326 nequileft_internal = NEQUI;
329 configcache::~configcache()
336 void configcache::finishOutFile()
338 if( outBuffer != NULL )
344 if( outFile.is_open() )
347 rename( outFileName.str().c_str(), outFileName.str().substr(0, outFileName.str().size()-4).c_str() );
351 int configcache::readFullBlock(char *tmpData, long unsigned int dataSize)
353 /* try to read header */
354 if ( openFileDesc.extended )
355 if ( ! readAllHeaders() )
359 return readDataToMem(tmpData, dataSize);
362 int configcache::readDataToMem(char *tmpData, long unsigned int dataSize)
366 if ( dataSize == 0 ) return 0;
368 try { readturn = boost::iostreams::read(*inBuffer, tmpData, dataSize); }
369 catch(boost::iostreams::bzip2_error& error) {
370 if(log) *log << "CCACHE: Caught bzip2 exception with error code: " << error.error() << endl << flush;
373 catch (std::exception const& ex) {
374 if(log) *log << "CCACHE: Caught exception: " << ex.what() << endl << flush;
378 if(log) *log << "CCACHE: Caught unknown exception while reading." << endl << flush;
385 unsigned long configcache::hash(const string& str)
387 unsigned long hash = 5381;
389 for(string::const_iterator it=str.begin();it!=str.end();it++)
390 hash = ((hash << 5) + hash) + *it; /* hash * 33 + character */
395 void configcache::deleteHeaderStore()
397 while ( headerStore.size() > 0 ) {
398 free(headerStore.back().second);
399 headerStore.pop_back();
403 vector<infiledesc>::iterator configcache::getNextInfile(vector<unsigned long> *excludeFileHashes) {
404 for (vector<infiledesc>::iterator init = inFiles.begin(); init != inFiles.end(); ++init) {
405 if (excludeFileHashes != NULL) {
406 bool excludethisfile = false;
408 for (vector<unsigned long>::iterator exit = excludeFileHashes->begin(); exit != excludeFileHashes->end(); ++exit)
409 if ( *exit == hash(init->filename) ) {
410 excludethisfile = true;
419 return inFiles.end();