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()
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 if(out) *out->log << "CCACHE: Opening dat-file: " << inFiles.back().filename << endl << flush;
191 openFileDesc = inFiles.back();
192 inFile.open( (DATADIR + "/" + inFiles.back().filename).c_str(), std::ios::binary );
195 if( !inFile.is_open() ) continue;
197 inBuffer = new boost::iostreams::filtering_istreambuf;
198 inBuffer->push( boost::iostreams::bzip2_decompressor() );
199 inBuffer->push(inFile);
202 if( inFile.is_open() )
204 if (openFileDesc.doVirtualEquilibration) {
205 if(out) *out->log << "CCACHE: Trying virtual equilibration." << endl << flush;
206 openFileDesc.doVirtualEquilibration = false;
207 for (int iskip=0; iskip < (NEQUI-openFileDesc.nequi)/openFileDesc.nskip; iskip++) {
208 if( readFullBlock(tmpConfig, configSize) != configSize || ! inFile.is_open() )
213 if( readFullBlock(tmpConfig, configSize) == configSize && inFile.is_open() )
215 memcpy(configMem, tmpConfig, configSize);
219 if(out) *out->log << "CCACHE: Could not read configuration. Closing dat-file: " << openFileDesc.filename << endl << flush;
226 void configcache::openOutFile()
228 time_t secstamp = time(NULL);
231 outFileName << DATADIR << "/" << secstamp << "_" << getFileId() << "_.edat.tmp";
233 outFile.open( outFileName.str().c_str(), std::ios::binary );
235 outBuffer = new boost::iostreams::filtering_ostreambuf;
236 outBuffer->push(boost::iostreams::bzip2_compressor());
237 outBuffer->push(outFile);
240 void configcache::writeHeader(const string& headerid, char *header, long unsigned int size) {
241 unsigned long headeridhash;
243 if( DATADIR == "" || MODE < 2 ) return;
245 if(!outFile.is_open())
248 headeridhash = hash(headerid);
250 boost::iostreams::write(*outBuffer, (char*)&size, sizeof(long unsigned int));
251 boost::iostreams::write(*outBuffer, (char*)&headeridhash, sizeof(unsigned long));
252 boost::iostreams::write(*outBuffer, header, size);
255 void configcache::writeConfig()
257 long unsigned int zeroheader=0;
259 if ( DATADIR == "" || MODE < 2 ) return;
261 if ( ! outFile.is_open() )
264 boost::iostreams::write(*outBuffer, (char*)&zeroheader, sizeof(long unsigned int));
266 boost::iostreams::write(*outBuffer, configMem, configSize);
269 void configcache::addPara(const string& parid, const double& val){
273 Paras.push_back(newPara);
276 int configcache::getParIndex(const string& parid){
277 for(int ipara=0; ipara<Paras.size(); ipara++)
278 if(Paras[ipara].id == parid) return ipara;
281 void configcache::setPara(const string& parid, const double& value){
282 Paras[getParIndex(parid)].val = value;
284 if(inBuffer != NULL) { delete inBuffer; inBuffer=NULL; }
288 refetchDataFiles = true;
291 configcache::~configcache()
298 void configcache::finishOutFile()
300 if( outBuffer != NULL )
306 if( outFile.is_open() )
309 rename( outFileName.str().c_str(), outFileName.str().substr(0, outFileName.str().size()-4).c_str() );
313 int configcache::readFullBlock(char *tmpData, long unsigned int dataSize)
315 /* try to read header */
316 if ( openFileDesc.extended )
317 if ( ! readAllHeaders() )
321 return readDataToMem(tmpData, dataSize);
324 int configcache::readDataToMem(char *tmpData, long unsigned int dataSize)
328 if ( dataSize == 0 ) return 0;
330 try { readturn = boost::iostreams::read(*inBuffer, tmpData, dataSize); }
331 catch(boost::iostreams::bzip2_error& error) {
332 if(out) *out->log << "CCACHE: Caught bzip2 exception with error code: " << error.error() << endl << flush;
335 catch (std::exception const& ex) {
336 if(out) *out->log << "CCACHE: Caught exception: " << ex.what() << endl << flush;
340 if(out) *out->log << "CCACHE: Caught unknown exception while reading." << endl << flush;
347 unsigned long configcache::hash(const string& str)
349 unsigned long hash = 5381;
351 for(string::const_iterator it=str.begin();it!=str.end();it++)
352 hash = ((hash << 5) + hash) + *it; /* hash * 33 + character */
357 void configcache::deleteHeaderStore()
359 while ( headerStore.size() > 0 ) {
360 free(headerStore.back().second);
361 headerStore.pop_back();