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 configMem = (char*)malloc(configMemSize);
22 tmpConfig = (char*)malloc(configMemSize);
24 *configmem = configMem;
25 configSize = configMemSize;
32 refetchDataFiles = false;
35 string configcache::getFileId(const bool& shortid)
39 if(!shortid) fileid << CACHEID << "_" << NEQUI << "_" << NSKIP;
40 for(int ipara=0; ipara<Paras.size(); ipara++)
41 fileid << "_" << Paras[ipara].id << Paras[ipara].val;
46 void configcache::fetchDataFiles()
48 struct dirent *de=NULL;
50 static infiledesc filedesc;
52 d=opendir(DATADIR.c_str());
54 while(de = readdir(d)){
55 string filename = de->d_name;
56 if(isValidInFile(filename, &filedesc))
58 inFiles.push_back(filedesc);
64 bool configcache::isValidInFile(const string& infile, infiledesc *filedesc)
66 char *inchar, *inParts;
67 string truncIn, truncOut;
69 filedesc->filename = infile;
70 filedesc->doVirtualEquilibration = false;
72 if( infile.size() < 4 ) return false;
74 if( infile.substr(infile.size()-4) == ".dat" )
75 filedesc->extended = false;
76 else if( infile.substr(infile.size()-4) == "edat" )
77 filedesc->extended = true;
81 inchar = new char [infile.size()+1];
82 strcpy (inchar, infile.c_str());
84 inParts = strtok( inchar, "_" );
85 for(int iPart=0; inParts!=NULL; iPart++)
87 if( iPart>3 ) { truncIn += "_"; truncIn += inParts; }
91 case 1: if(inParts != CACHEID) return false; break;
93 if (atoi(inParts) > NEQUI)
95 else if (atoi(inParts) < NEQUI)
96 filedesc->doVirtualEquilibration = true;
97 filedesc->nequi = atoi(inParts);
100 if(atoi(inParts) != NSKIP)
102 filedesc->nskip = atoi(inParts);
105 inParts = strtok( NULL, "_");
107 truncIn = truncIn.substr(0, truncIn.size()-4);
111 if( truncIn.find( getFileId(true) + "_" ) == string::npos ) return false;
116 int configcache::readHeader()
118 long unsigned int headersize;
120 if( readDataToMem((char *)&headersize, sizeof(long unsigned int)) == sizeof(long unsigned int) && inFile.is_open() ) {
121 if ( headersize == 0 )
122 return HEADER_READLAST;
124 pair<unsigned long, void *> newHeader;
126 if( readDataToMem((char *)&newHeader.first, sizeof(unsigned long)) == sizeof(unsigned long) && inFile.is_open() ) {
127 newHeader.second = malloc(headersize);
129 if( readDataToMem((char *)newHeader.second, headersize) == headersize && inFile.is_open() ) {
130 headerStore.push_back(newHeader);
131 return HEADER_READOK;
134 if(out) *out->log << "CCACHE: Could not read heade-data! Closing dat-file: " << openFileDesc.filename << endl << flush;
136 return HEADER_READERR;
140 if(out) *out->log << "CCACHE: Could not read headerid-hash! Closing dat-file: " << openFileDesc.filename << endl << flush;
142 return HEADER_READERR;
146 if(out) *out->log << "CCACHE: Could not read header size. Closing dat-file: " << openFileDesc.filename << endl << flush;
148 return HEADER_READERR;
152 bool configcache::readAllHeaders()
154 int readHeaderStatus;
159 readHeaderStatus = readHeader();
161 while ( readHeaderStatus == HEADER_READOK );
163 if ( readHeaderStatus == HEADER_READLAST ) return true;
164 else if ( readHeaderStatus == HEADER_READERR ) return false;
167 void * configcache::getHeader(const string& headerid) {
168 for (vector< pair<unsigned long, void *> >::iterator headerStoreIt = headerStore.begin(); headerStoreIt != headerStore.end(); ++headerStoreIt)
169 if ( headerStoreIt->first == hash(headerid) )
170 return headerStoreIt->second;
175 /* returns number of equilibration-steps left */
176 int configcache::readConfig(vector<unsigned long> *excludeFileHashes)
178 int nequileft = NEQUI;
180 if(DATADIR == "" || MODE == CACHE_MODE_DISABLED) return nequileft;
182 if(refetchDataFiles){
183 refetchDataFiles = false;
189 if( (!inFile.is_open()) && inFiles.size() == 0 ) return nequileft;
191 while( (!inFile.is_open()) && inFiles.size() > 0 ) {
192 bool excludethisfile=false;
194 openFileDesc = inFiles.back();
196 if (excludeFileHashes != NULL)
197 for (vector<unsigned long>::iterator exit = excludeFileHashes->begin(); exit != excludeFileHashes->end(); ++exit)
198 if ( *exit == hash(inFiles.back().filename) ) {
199 excludethisfile = true;
203 if ( ! excludethisfile ) {
204 if(out) *out->log << "CCACHE: Opening dat-file: " << inFiles.back().filename << endl << flush;
205 inFile.open( (DATADIR + "/" + inFiles.back().filename).c_str(), std::ios::binary );
208 if(out) *out->log << "CCACHE: Excluded dat-file: " << inFiles.back().filename << endl << flush;
212 if( !inFile.is_open() ) continue;
214 inBuffer = new boost::iostreams::filtering_istreambuf;
215 inBuffer->push( boost::iostreams::bzip2_decompressor() );
216 inBuffer->push(inFile);
219 if( inFile.is_open() )
221 if (openFileDesc.doVirtualEquilibration) {
222 if(out) *out->log << "CCACHE: Trying virtual equilibration." << endl << flush;
223 openFileDesc.doVirtualEquilibration = false;
224 for (int iskip=0; iskip < (NEQUI-openFileDesc.nequi)/openFileDesc.nskip; iskip++) {
225 if( readFullBlock(tmpConfig, configSize) != configSize || ! inFile.is_open() )
227 else if ( (NEQUI-openFileDesc.nequi) - (iskip+1)*openFileDesc.nskip < nequileft ) {
228 memcpy(configMem, tmpConfig, configSize);
229 nequileft = (NEQUI-openFileDesc.nequi) - (iskip+1)*openFileDesc.nskip;
234 if( readFullBlock(tmpConfig, configSize) == configSize && inFile.is_open() )
236 memcpy(configMem, tmpConfig, configSize);
240 if(out) *out->log << "CCACHE: Could not read configuration. Closing dat-file: " << openFileDesc.filename << endl << flush;
247 void configcache::openOutFile()
249 time_t secstamp = time(NULL);
252 outFileName << DATADIR << "/" << secstamp << "_" << getFileId() << "_.edat.tmp";
254 outFile.open( outFileName.str().c_str(), std::ios::binary );
256 outBuffer = new boost::iostreams::filtering_ostreambuf;
257 outBuffer->push(boost::iostreams::bzip2_compressor());
258 outBuffer->push(outFile);
261 void configcache::writeHeader(const string& headerid, const char *header, long unsigned int size) {
262 unsigned long headeridhash;
264 if( DATADIR == "" || MODE < 2 ) return;
266 if(!outFile.is_open())
269 headeridhash = hash(headerid);
271 boost::iostreams::write(*outBuffer, (char*)&size, sizeof(long unsigned int));
272 boost::iostreams::write(*outBuffer, (char*)&headeridhash, sizeof(unsigned long));
273 boost::iostreams::write(*outBuffer, header, size);
276 void configcache::writeConfig()
278 long unsigned int zeroheader=0;
280 if ( DATADIR == "" || MODE < 2 ) return;
282 if ( ! outFile.is_open() )
285 boost::iostreams::write(*outBuffer, (char*)&zeroheader, sizeof(long unsigned int));
287 boost::iostreams::write(*outBuffer, configMem, configSize);
290 void configcache::addPara(const string& parid, const double& val){
294 Paras.push_back(newPara);
297 int configcache::getParIndex(const string& parid){
298 for(int ipara=0; ipara<Paras.size(); ipara++)
299 if(Paras[ipara].id == parid) return ipara;
302 void configcache::setPara(const string& parid, const double& value){
303 Paras[getParIndex(parid)].val = value;
305 if(inBuffer != NULL) { delete inBuffer; inBuffer=NULL; }
309 refetchDataFiles = true;
312 configcache::~configcache()
319 void configcache::finishOutFile()
321 if( outBuffer != NULL )
327 if( outFile.is_open() )
330 rename( outFileName.str().c_str(), outFileName.str().substr(0, outFileName.str().size()-4).c_str() );
334 int configcache::readFullBlock(char *tmpData, long unsigned int dataSize)
336 /* try to read header */
337 if ( openFileDesc.extended )
338 if ( ! readAllHeaders() )
342 return readDataToMem(tmpData, dataSize);
345 int configcache::readDataToMem(char *tmpData, long unsigned int dataSize)
349 if ( dataSize == 0 ) return 0;
351 try { readturn = boost::iostreams::read(*inBuffer, tmpData, dataSize); }
352 catch(boost::iostreams::bzip2_error& error) {
353 if(out) *out->log << "CCACHE: Caught bzip2 exception with error code: " << error.error() << endl << flush;
356 catch (std::exception const& ex) {
357 if(out) *out->log << "CCACHE: Caught exception: " << ex.what() << endl << flush;
361 if(out) *out->log << "CCACHE: Caught unknown exception while reading." << endl << flush;
368 unsigned long configcache::hash(const string& str)
370 unsigned long hash = 5381;
372 for(string::const_iterator it=str.begin();it!=str.end();it++)
373 hash = ((hash << 5) + hash) + *it; /* hash * 33 + character */
378 void configcache::deleteHeaderStore()
380 while ( headerStore.size() > 0 ) {
381 free(headerStore.back().second);
382 headerStore.pop_back();