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(const bool& shortid)
44 if(!shortid) fileid << CACHEID << "_" << NEQUI << "_" << 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;
77 if( infile.size() < 4 ) return false;
79 if( infile.substr(infile.size()-4) == ".dat" )
80 filedesc->extended = false;
81 else if( infile.substr(infile.size()-4) == "edat" )
82 filedesc->extended = true;
86 inchar = new char [infile.size()+1];
87 strcpy (inchar, infile.c_str());
89 inParts = strtok( inchar, "_" );
90 for(int iPart=0; inParts!=NULL; iPart++)
92 if( iPart>3 ) { truncIn += "_"; truncIn += inParts; }
96 case 1: if(inParts != CACHEID) {
101 if (atoi(inParts) > NEQUI)
103 else if (atoi(inParts) < NEQUI)
104 filedesc->doVirtualEquilibration = true;
105 filedesc->nequi = atoi(inParts);
108 if(atoi(inParts) != NSKIP)
110 filedesc->nskip = atoi(inParts);
113 inParts = strtok( NULL, "_");
115 truncIn = truncIn.substr(0, truncIn.size()-4);
119 if( truncIn.find( getFileId(true) + "_" ) == string::npos ) return false;
124 int configcache::readHeader()
126 long unsigned int headersize;
128 if( readDataToMem((char *)&headersize, sizeof(long unsigned int)) == sizeof(long unsigned int) && inFile.is_open() ) {
129 if ( headersize == 0 )
130 return HEADER_READLAST;
132 pair<unsigned long, void *> newHeader;
134 if( readDataToMem((char *)&newHeader.first, sizeof(unsigned long)) == sizeof(unsigned long) && inFile.is_open() ) {
135 newHeader.second = malloc(headersize);
137 if( readDataToMem((char *)newHeader.second, headersize) == headersize && inFile.is_open() ) {
138 headerStore.push_back(newHeader);
139 return HEADER_READOK;
142 if(log) *log << "CCACHE: Could not read heade-data! Closing dat-file: " << openFileDesc.filename << endl << flush;
144 return HEADER_READERR;
148 if(log) *log << "CCACHE: Could not read headerid-hash! Closing dat-file: " << openFileDesc.filename << endl << flush;
150 return HEADER_READERR;
154 if(log) *log << "CCACHE: Could not read header size. Closing dat-file: " << openFileDesc.filename << endl << flush;
156 return HEADER_READERR;
160 bool configcache::readAllHeaders()
162 int readHeaderStatus;
167 readHeaderStatus = readHeader();
169 while ( readHeaderStatus == HEADER_READOK );
171 if ( readHeaderStatus == HEADER_READLAST ) return true;
172 else if ( readHeaderStatus == HEADER_READERR ) return false;
175 void * configcache::getHeader(const string& headerid) {
176 for (vector< pair<unsigned long, void *> >::iterator headerStoreIt = headerStore.begin(); headerStoreIt != headerStore.end(); ++headerStoreIt)
177 if ( headerStoreIt->first == hash(headerid) )
178 return headerStoreIt->second;
183 /* returns number of equilibration-steps left */
184 int configcache::readConfig(vector<unsigned long> *excludeFileHashes)
186 int nequileft = NEQUI;
188 if(DATADIR == "" || MODE == CACHE_MODE_DISABLED) return nequileft;
190 if(refetchDataFiles){
191 refetchDataFiles = false;
197 vector<infiledesc>::iterator inFileIt = getNextInfile(excludeFileHashes);
199 if( (!inFile.is_open()) && inFileIt == inFiles.end() ) return nequileft;
201 while( (!inFile.is_open()) && inFiles.size() > 0 ) {
202 openFileDesc = *inFileIt;
204 if(log) *log << "CCACHE: Opening dat-file: " << inFileIt->filename << endl << flush;
205 inFile.open( (DATADIR + "/" + inFileIt->filename).c_str(), std::ios::binary );
207 inFiles.erase(inFileIt);
209 if( !inFile.is_open() ) continue;
211 inBuffer = new boost::iostreams::filtering_istreambuf;
212 inBuffer->push( boost::iostreams::bzip2_decompressor() );
213 inBuffer->push(inFile);
216 if( inFile.is_open() )
218 if (openFileDesc.doVirtualEquilibration) {
219 if(log) *log << "CCACHE: Trying virtual equilibration." << endl << flush;
220 openFileDesc.doVirtualEquilibration = false;
221 for (int iskip=0; iskip < (NEQUI-openFileDesc.nequi)/openFileDesc.nskip; iskip++) {
222 if( readFullBlock(tmpConfig, configSize) != configSize || ! inFile.is_open() )
224 else if ( (NEQUI-openFileDesc.nequi) - (iskip+1)*openFileDesc.nskip < nequileft ) {
225 memcpy(configMem, tmpConfig, configSize);
226 nequileft = (NEQUI-openFileDesc.nequi) - (iskip+1)*openFileDesc.nskip;
231 if( readFullBlock(tmpConfig, configSize) == configSize && inFile.is_open() )
233 memcpy(configMem, tmpConfig, configSize);
237 if(log) *log << "CCACHE: Could not read configuration. Closing dat-file: " << openFileDesc.filename << endl << flush;
244 void configcache::openOutFile()
246 time_t secstamp = time(NULL);
249 outFileName << DATADIR << "/" << secstamp << "_" << getFileId() << "_.edat.tmp";
251 outFile.open( outFileName.str().c_str(), std::ios::binary );
253 outBuffer = new boost::iostreams::filtering_ostreambuf;
254 outBuffer->push(boost::iostreams::bzip2_compressor());
255 outBuffer->push(outFile);
258 void configcache::writeHeader(const string& headerid, const char *header, long unsigned int size) {
259 unsigned long headeridhash;
261 if( DATADIR == "" || MODE < 2 ) return;
263 if(!outFile.is_open())
266 headeridhash = hash(headerid);
268 boost::iostreams::write(*outBuffer, (char*)&size, sizeof(long unsigned int));
269 boost::iostreams::write(*outBuffer, (char*)&headeridhash, sizeof(unsigned long));
270 boost::iostreams::write(*outBuffer, header, size);
273 void configcache::writeConfig()
275 long unsigned int zeroheader=0;
277 if ( DATADIR == "" || MODE < 2 ) return;
279 if ( ! outFile.is_open() )
282 boost::iostreams::write(*outBuffer, (char*)&zeroheader, sizeof(long unsigned int));
284 boost::iostreams::write(*outBuffer, configMem, configSize);
287 void configcache::addPara(const string& parid, const double& val){
291 Paras.push_back(newPara);
294 int configcache::getParIndex(const string& parid){
295 for(int ipara=0; ipara<Paras.size(); ipara++)
296 if(Paras[ipara].id == parid) return ipara;
299 void configcache::setPara(const string& parid, const double& value){
300 Paras[getParIndex(parid)].val = value;
302 if(inBuffer != NULL) { delete inBuffer; inBuffer=NULL; }
306 refetchDataFiles = true;
309 configcache::~configcache()
316 void configcache::finishOutFile()
318 if( outBuffer != NULL )
324 if( outFile.is_open() )
327 rename( outFileName.str().c_str(), outFileName.str().substr(0, outFileName.str().size()-4).c_str() );
331 int configcache::readFullBlock(char *tmpData, long unsigned int dataSize)
333 /* try to read header */
334 if ( openFileDesc.extended )
335 if ( ! readAllHeaders() )
339 return readDataToMem(tmpData, dataSize);
342 int configcache::readDataToMem(char *tmpData, long unsigned int dataSize)
346 if ( dataSize == 0 ) return 0;
348 try { readturn = boost::iostreams::read(*inBuffer, tmpData, dataSize); }
349 catch(boost::iostreams::bzip2_error& error) {
350 if(log) *log << "CCACHE: Caught bzip2 exception with error code: " << error.error() << endl << flush;
353 catch (std::exception const& ex) {
354 if(log) *log << "CCACHE: Caught exception: " << ex.what() << endl << flush;
358 if(log) *log << "CCACHE: Caught unknown exception while reading." << endl << flush;
365 unsigned long configcache::hash(const string& str)
367 unsigned long hash = 5381;
369 for(string::const_iterator it=str.begin();it!=str.end();it++)
370 hash = ((hash << 5) + hash) + *it; /* hash * 33 + character */
375 void configcache::deleteHeaderStore()
377 while ( headerStore.size() > 0 ) {
378 free(headerStore.back().second);
379 headerStore.pop_back();
383 vector<infiledesc>::iterator configcache::getNextInfile(vector<unsigned long> *excludeFileHashes) {
384 for (vector<infiledesc>::iterator init = inFiles.begin(); init != inFiles.end(); ++init) {
385 if (excludeFileHashes != NULL) {
386 bool excludethisfile = false;
388 for (vector<unsigned long>::iterator exit = excludeFileHashes->begin(); exit != excludeFileHashes->end(); ++exit)
389 if ( *exit == hash(init->filename) ) {
390 excludethisfile = true;
399 return inFiles.end();