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 bool configcache::readConfig(vector<unsigned long> *excludeFileHashes)
177 if(DATADIR == "" || MODE == CACHE_MODE_DISABLED) return false;
179 if(refetchDataFiles){
180 refetchDataFiles = false;
186 if( (!inFile.is_open()) && inFiles.size() == 0 ) return false;
188 while( (!inFile.is_open()) && inFiles.size() > 0 ) {
189 bool excludethisfile=false;
191 openFileDesc = inFiles.back();
193 if (excludeFileHashes != NULL)
194 for (vector<unsigned long>::iterator exit = excludeFileHashes->begin(); exit != excludeFileHashes->end(); ++exit)
195 if ( *exit == hash(inFiles.back().filename) ) {
196 excludethisfile = true;
200 if ( ! excludethisfile ) {
201 if(out) *out->log << "CCACHE: Opening dat-file: " << inFiles.back().filename << endl << flush;
202 inFile.open( (DATADIR + "/" + inFiles.back().filename).c_str(), std::ios::binary );
205 if(out) *out->log << "CCACHE: Excluded dat-file: " << inFiles.back().filename << endl << flush;
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(out) *out->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() )
227 if( readFullBlock(tmpConfig, configSize) == configSize && inFile.is_open() )
229 memcpy(configMem, tmpConfig, configSize);
233 if(out) *out->log << "CCACHE: Could not read configuration. Closing dat-file: " << openFileDesc.filename << endl << flush;
240 void configcache::openOutFile()
242 time_t secstamp = time(NULL);
245 outFileName << DATADIR << "/" << secstamp << "_" << getFileId() << "_.edat.tmp";
247 outFile.open( outFileName.str().c_str(), std::ios::binary );
249 outBuffer = new boost::iostreams::filtering_ostreambuf;
250 outBuffer->push(boost::iostreams::bzip2_compressor());
251 outBuffer->push(outFile);
254 void configcache::writeHeader(const string& headerid, const char *header, long unsigned int size) {
255 unsigned long headeridhash;
257 if( DATADIR == "" || MODE < 2 ) return;
259 if(!outFile.is_open())
262 headeridhash = hash(headerid);
264 boost::iostreams::write(*outBuffer, (char*)&size, sizeof(long unsigned int));
265 boost::iostreams::write(*outBuffer, (char*)&headeridhash, sizeof(unsigned long));
266 boost::iostreams::write(*outBuffer, header, size);
269 void configcache::writeConfig()
271 long unsigned int zeroheader=0;
273 if ( DATADIR == "" || MODE < 2 ) return;
275 if ( ! outFile.is_open() )
278 boost::iostreams::write(*outBuffer, (char*)&zeroheader, sizeof(long unsigned int));
280 boost::iostreams::write(*outBuffer, configMem, configSize);
283 void configcache::addPara(const string& parid, const double& val){
287 Paras.push_back(newPara);
290 int configcache::getParIndex(const string& parid){
291 for(int ipara=0; ipara<Paras.size(); ipara++)
292 if(Paras[ipara].id == parid) return ipara;
295 void configcache::setPara(const string& parid, const double& value){
296 Paras[getParIndex(parid)].val = value;
298 if(inBuffer != NULL) { delete inBuffer; inBuffer=NULL; }
302 refetchDataFiles = true;
305 configcache::~configcache()
312 void configcache::finishOutFile()
314 if( outBuffer != NULL )
320 if( outFile.is_open() )
323 rename( outFileName.str().c_str(), outFileName.str().substr(0, outFileName.str().size()-4).c_str() );
327 int configcache::readFullBlock(char *tmpData, long unsigned int dataSize)
329 /* try to read header */
330 if ( openFileDesc.extended )
331 if ( ! readAllHeaders() )
335 return readDataToMem(tmpData, dataSize);
338 int configcache::readDataToMem(char *tmpData, long unsigned int dataSize)
342 if ( dataSize == 0 ) return 0;
344 try { readturn = boost::iostreams::read(*inBuffer, tmpData, dataSize); }
345 catch(boost::iostreams::bzip2_error& error) {
346 if(out) *out->log << "CCACHE: Caught bzip2 exception with error code: " << error.error() << endl << flush;
349 catch (std::exception const& ex) {
350 if(out) *out->log << "CCACHE: Caught exception: " << ex.what() << endl << flush;
354 if(out) *out->log << "CCACHE: Caught unknown exception while reading." << endl << flush;
361 unsigned long configcache::hash(const string& str)
363 unsigned long hash = 5381;
365 for(string::const_iterator it=str.begin();it!=str.end();it++)
366 hash = ((hash << 5) + hash) + *it; /* hash * 33 + character */
371 void configcache::deleteHeaderStore()
373 while ( headerStore.size() > 0 ) {
374 free(headerStore.back().second);
375 headerStore.pop_back();