1 #include "configcache.h"
 
   8 #include <boost/iostreams/filtering_streambuf.hpp>
 
   9 #include <boost/iostreams/stream.hpp>
 
  10 #include <boost/iostreams/filter/bzip2.hpp>
 
  11 #include <boost/iostreams/device/array.hpp>
 
  12 #include <boost/iostreams/copy.hpp>
 
  14 #define HEADER_READOK   0
 
  15 #define HEADER_READERR  1
 
  16 #define HEADER_READLAST 2
 
  18 struct configcache::iobuffers
 
  20   boost::iostreams::filtering_istreambuf *in;
 
  21   boost::iostreams::filtering_ostreambuf *out;
 
  24 configcache::configcache(const string& cacheid, const int& nequi, const int& nskip, const string& datadir, char **configmem, const int& configMemSize, const int& cachemode,
 
  33   if ( cacheid.find("_") != -1 ) {
 
  34     if(log) *log << "CCACHE: Invalid cacheid \"" << cacheid << "\" given. Cacheids must not contain underscores!" << endl << flush;
 
  38   configMem = (char*)malloc(configMemSize);
 
  39   tmpConfig = (char*)malloc(configMemSize);
 
  41   *configmem = configMem;
 
  42   configSize = configMemSize;
 
  44   ioBuffers = new iobuffers;
 
  46   ioBuffers->out = NULL;
 
  50   refetchDataFiles = false;
 
  53 string configcache::getFileId(int actnequi, const bool& shortid)
 
  57   if(!shortid) fileid << CACHEID << "_" << actnequi << "_" << NSKIP;
 
  58   for(int ipara=0; ipara<Paras.size(); ipara++)
 
  59     fileid << "_" << Paras[ipara].id << Paras[ipara].val;
 
  64 void configcache::fetchDataFiles()
 
  66   struct dirent *de=NULL;
 
  68   static infiledesc filedesc;
 
  70   d=opendir(DATADIR.c_str());
 
  72     while(de = readdir(d)){
 
  73       string filename = de->d_name;
 
  74       if(isValidInFile(filename, &filedesc)) 
 
  76           inFiles.push_back(filedesc);
 
  82 bool configcache::isValidInFile(const string& infile, infiledesc *filedesc)
 
  84   char *inchar, *inParts;
 
  85   string truncIn, truncOut;
 
  87   filedesc->filename = infile;
 
  89   if( infile.size() < 4 ) return false;
 
  91   if( infile.substr(infile.size()-4) == ".dat" )
 
  92     filedesc->extended = false;
 
  93   else if( infile.substr(infile.size()-4) == "edat" )
 
  94     filedesc->extended = true;
 
  98   inchar = new char [infile.size()+1];
 
  99   strcpy (inchar, infile.c_str());
 
 101   inParts = strtok( inchar, "_" );
 
 102   for(int iPart=0; inParts!=NULL; iPart++)
 
 104       if( iPart>3 ) { truncIn += "_"; truncIn += inParts; }
 
 108         case 1: if(inParts != CACHEID)
 
 112           filedesc->nequi = atoi(inParts);
 
 115           if(atoi(inParts) != NSKIP) 
 
 117           filedesc->nskip = atoi(inParts);
 
 120       inParts = strtok( NULL, "_");
 
 122   truncIn = truncIn.substr(0, truncIn.size()-4);
 
 126   if( truncIn.find( getFileId(NEQUI, true) + "_" ) == string::npos ) return false;
 
 131 int configcache::readHeader()
 
 133   long unsigned int headersize;
 
 135   if( readDataToMem((char *)&headersize, sizeof(long unsigned int)) == sizeof(long unsigned int) && inFile.is_open() ) {
 
 136     if ( headersize == 0 )
 
 137       return HEADER_READLAST;
 
 139     pair<unsigned long, void *> newHeader;
 
 141     if( readDataToMem((char *)&newHeader.first, sizeof(unsigned long)) == sizeof(unsigned long) && inFile.is_open() ) {
 
 142       newHeader.second = malloc(headersize);
 
 144       if( readDataToMem((char *)newHeader.second, headersize) == headersize && inFile.is_open() ) {
 
 145         headerStore.push_back(newHeader);
 
 146         return HEADER_READOK;
 
 149         if(log) *log << "CCACHE: Could not read heade-data! Closing dat-file: " << openFileDesc.filename << endl << flush;
 
 151         return HEADER_READERR;
 
 155       if(log) *log << "CCACHE: Could not read headerid-hash! Closing dat-file: " << openFileDesc.filename << endl << flush;
 
 157       return HEADER_READERR;
 
 161     if(log) *log << "CCACHE: Could not read header size. Closing dat-file: " << openFileDesc.filename << endl << flush;
 
 163     return HEADER_READERR;
 
 167 bool configcache::readAllHeaders()
 
 169   int readHeaderStatus;
 
 174     readHeaderStatus = readHeader();
 
 176   while ( readHeaderStatus == HEADER_READOK );
 
 178   if ( readHeaderStatus == HEADER_READLAST ) return true;
 
 179   else if ( readHeaderStatus == HEADER_READERR ) return false;
 
 182 void * configcache::getHeader(const string& headerid) {
 
 183   for (vector< pair<unsigned long, void *> >::iterator headerStoreIt = headerStore.begin(); headerStoreIt != headerStore.end(); ++headerStoreIt)
 
 184     if ( headerStoreIt->first == hash(headerid) )
 
 185       return headerStoreIt->second;
 
 190 void configcache::readConfig(bool *readnewconfig, int *nequileft, vector<unsigned long> *excludeFileHashes)
 
 192   *readnewconfig = false;
 
 194   if(DATADIR == "" || MODE == CACHE_MODE_DISABLED) return;
 
 196   if(refetchDataFiles){
 
 197     refetchDataFiles = false;
 
 203       vector<infiledesc>::iterator inFileIt = getNextInfile(excludeFileHashes);
 
 204       int iDidVirtualSkips;
 
 206       if( (!inFile.is_open()) && inFileIt == inFiles.end() ) {
 
 208           *nequileft = nequileft_internal;
 
 212       while( (!inFile.is_open()) && inFiles.size() > 0 ) {
 
 213         openFileDesc = *inFileIt;
 
 215         if (openFileDesc.nequi < NEQUI)
 
 216           doVirtualEquilibration = true;
 
 218           doVirtualEquilibration = false;
 
 220         firstUsedConfig = true;
 
 222         if(log) *log << "CCACHE: Opening dat-file: " << inFileIt->filename << endl << flush;
 
 223         inFile.open( (DATADIR + "/" + inFileIt->filename).c_str(), std::ios::binary );
 
 225         inFiles.erase(inFileIt);
 
 227         if( !inFile.is_open() ) continue;
 
 229         ioBuffers->in = new boost::iostreams::filtering_istreambuf;
 
 230         ioBuffers->in->push( boost::iostreams::bzip2_decompressor() );
 
 231         ioBuffers->in->push(inFile);
 
 234       if( inFile.is_open() ) 
 
 236           if (doVirtualEquilibration) {
 
 237             if(log) *log << "CCACHE: Trying virtual equilibration." << endl << flush;
 
 238             doVirtualEquilibration = false;
 
 239             for (iDidVirtualSkips=0; iDidVirtualSkips < (NEQUI-openFileDesc.nequi)/openFileDesc.nskip; iDidVirtualSkips++) {
 
 240               if( readFullBlock(tmpConfig, configSize) != configSize || ! inFile.is_open() )
 
 242               else if ( (NEQUI-openFileDesc.nequi) - (iDidVirtualSkips+1)*openFileDesc.nskip < nequileft_internal ) {
 
 243                 memcpy(configMem, tmpConfig, configSize);
 
 244                 nequileft_internal = NEQUI - openFileDesc.nequi - (iDidVirtualSkips+1)*openFileDesc.nskip;
 
 245                 *readnewconfig = true;
 
 246                 firstUsedConfig = false;
 
 251           if( readFullBlock(tmpConfig, configSize) == configSize && inFile.is_open() )
 
 253               memcpy(configMem, tmpConfig, configSize);
 
 254               *readnewconfig = true;
 
 255               if (firstUsedConfig) {
 
 256                 firstUsedConfig = false;
 
 257                 if (openFileDesc.nequi < NEQUI)
 
 258                   nequileft_internal = NEQUI - openFileDesc.nequi - iDidVirtualSkips*openFileDesc.nskip;
 
 260                   nequileft_internal = NEQUI - openFileDesc.nequi;
 
 262               nequileft_internal -= openFileDesc.nskip;
 
 263               *nequileft = nequileft_internal;
 
 267             if(log) *log << "CCACHE: Could not read configuration. Closing dat-file: " << openFileDesc.filename << endl << flush;
 
 274 void configcache::openOutFile(int actnequi)
 
 276   time_t secstamp = time(NULL);
 
 279   outFileName << DATADIR << "/" << secstamp << "_" << getFileId(actnequi) << "_.edat.tmp";
 
 281   outFile.open( outFileName.str().c_str(), std::ios::binary );
 
 283   ioBuffers->out = new boost::iostreams::filtering_ostreambuf;
 
 284   ioBuffers->out->push(boost::iostreams::bzip2_compressor());
 
 285   ioBuffers->out->push(outFile);
 
 288 void configcache::writeHeader(const string& headerid, const char *header, long unsigned int size, int actnequi) {
 
 289   unsigned long headeridhash;
 
 291   if( DATADIR == "" || MODE < 2 ) return;
 
 293   if(!outFile.is_open())
 
 294     openOutFile(actnequi);
 
 296   headeridhash = hash(headerid);
 
 298   boost::iostreams::write(*ioBuffers->out, (char*)&size, sizeof(long unsigned int));
 
 299   boost::iostreams::write(*ioBuffers->out, (char*)&headeridhash, sizeof(unsigned long));
 
 300   boost::iostreams::write(*ioBuffers->out, header, size);
 
 303 void configcache::writeConfig(int actnequi)
 
 305   long unsigned int zeroheader=0;
 
 307   if ( DATADIR == "" || MODE < 2 ) return;
 
 309   if ( ! outFile.is_open() )
 
 310     openOutFile(actnequi);
 
 312   boost::iostreams::write(*ioBuffers->out, (char*)&zeroheader, sizeof(long unsigned int));
 
 314   boost::iostreams::write(*ioBuffers->out, configMem, configSize);
 
 317 void configcache::addPara(const string& parid, const double& val){
 
 321   Paras.push_back(newPara);
 
 324 int configcache::getParIndex(const string& parid){
 
 325   for(int ipara=0; ipara<Paras.size(); ipara++)
 
 326     if(Paras[ipara].id == parid) return ipara;
 
 329 void configcache::setPara(const string& parid, const double& value){
 
 330   Paras[getParIndex(parid)].val = value;
 
 333   if(ioBuffers->in != NULL) { delete ioBuffers->in; ioBuffers->in=NULL; } 
 
 337   refetchDataFiles = true;
 
 338   nequileft_internal = NEQUI;
 
 341 configcache::~configcache()
 
 344   delete ioBuffers->in;
 
 345   ioBuffers->in = NULL;
 
 348 void configcache::finishOutFile()
 
 350   if( ioBuffers->out != NULL )
 
 352       delete ioBuffers->out;
 
 353       ioBuffers->out = NULL;
 
 356   if( outFile.is_open() )
 
 359       rename( outFileName.str().c_str(), outFileName.str().substr(0, outFileName.str().size()-4).c_str() );
 
 363 int configcache::readFullBlock(char *tmpData, long unsigned int dataSize)
 
 365   /* try to read header */
 
 366   if ( openFileDesc.extended )
 
 367     if ( ! readAllHeaders() ) 
 
 371   return readDataToMem(tmpData, dataSize);
 
 374 int configcache::readDataToMem(char *tmpData, long unsigned int dataSize)
 
 378   if ( dataSize == 0 ) return 0;
 
 380   try { readturn = boost::iostreams::read(*ioBuffers->in, tmpData, dataSize); }
 
 381   catch(boost::iostreams::bzip2_error& error) { 
 
 382     if(log) *log << "CCACHE: Caught bzip2 exception with error code: " << error.error() << endl << flush;
 
 385   catch (std::exception const& ex) {
 
 386     if(log) *log << "CCACHE: Caught exception: " << ex.what() << endl << flush;
 
 390     if(log) *log << "CCACHE: Caught unknown exception while reading." << endl << flush;
 
 397 unsigned long configcache::hash(const string& str)
 
 399   unsigned long hash = 5381;
 
 401   for(string::const_iterator it=str.begin();it!=str.end();it++) 
 
 402     hash = ((hash << 5) + hash) + *it; /* hash * 33 + character */
 
 407 void configcache::deleteHeaderStore()
 
 409   while ( headerStore.size() > 0 ) {
 
 410     free(headerStore.back().second);
 
 411     headerStore.pop_back();
 
 415 vector<infiledesc>::iterator configcache::getNextInfile(vector<unsigned long> *excludeFileHashes) {
 
 416   for (vector<infiledesc>::iterator init = inFiles.begin(); init != inFiles.end(); ++init) {
 
 417     if (excludeFileHashes != NULL) {
 
 418       bool excludethisfile = false;
 
 420       for (vector<unsigned long>::iterator exit = excludeFileHashes->begin(); exit != excludeFileHashes->end(); ++exit)
 
 421         if ( *exit == hash(init->filename) ) {
 
 422           excludethisfile = true;
 
 431   return inFiles.end();