1 #include "configcache.h"
8 configcache::configcache(const string& cacheid, const int& nequi, const int& nskip, const string& datadir, char **configmem, const int& configMemSize, const int& cachemode,
17 configMem = (char*)malloc(configMemSize);
18 tmpConfig = (char*)malloc(configMemSize);
20 *configmem = configMem;
21 configSize = configMemSize;
28 refetchDataFiles = false;
30 readHeaderData = NULL;
33 string configcache::getFileId(const bool& shortid)
37 if(!shortid) fileid << CACHEID << "_" << NEQUI << "_" << NSKIP;
38 for(int ipara=0; ipara<Paras.size(); ipara++)
39 fileid << "_" << Paras[ipara].id << Paras[ipara].val;
44 void configcache::fetchDataFiles()
46 struct dirent *de=NULL;
48 static infiledesc filedesc;
50 d=opendir(DATADIR.c_str());
52 while(de = readdir(d)){
53 string filename = de->d_name;
54 if(isValidInFile(filename, &filedesc))
56 inFiles.push_back(filedesc);
62 bool configcache::isValidInFile(const string& infile, infiledesc *filedesc)
64 char *inchar, *inParts;
65 string truncIn, truncOut;
67 filedesc->filename = infile;
68 filedesc->doVirtualEquilibration = false;
70 if( infile.size() < 4 ) return false;
72 if( infile.substr(infile.size()-4) == ".dat" )
73 filedesc->extended = false;
74 else if( infile.substr(infile.size()-4) == "edat" )
75 filedesc->extended = true;
79 inchar = new char [infile.size()+1];
80 strcpy (inchar, infile.c_str());
82 inParts = strtok( inchar, "_" );
83 for(int iPart=0; inParts!=NULL; iPart++)
85 if( iPart>3 ) { truncIn += "_"; truncIn += inParts; }
89 case 1: if(inParts != CACHEID) return false; break;
91 if (atoi(inParts) > NEQUI)
93 else if (atoi(inParts) < NEQUI)
94 filedesc->doVirtualEquilibration = true;
95 filedesc->nequi = atoi(inParts);
98 if(atoi(inParts) != NSKIP)
100 filedesc->nskip = atoi(inParts);
103 inParts = strtok( NULL, "_");
105 truncIn = truncIn.substr(0, truncIn.size()-4);
109 if( truncIn.find( getFileId(true) + "_" ) == string::npos ) return false;
114 bool configcache::readHeader()
116 long unsigned int headersize;
118 if( readDataToMem((char*)&headersize, sizeof(long unsigned int)) == sizeof(long unsigned int) && inFile.is_open() )
120 if( readHeaderData != NULL ) free(readHeaderData);
122 readHeaderData = (char*) malloc(headersize);
124 if( readDataToMem(readHeaderData, headersize) == headersize && inFile.is_open() ) {
128 if(out) *out->log << "CCACHE: Could not read header! Closing dat-file: " << openFileDesc.filename << endl << flush;
134 if(out) *out->log << "CCACHE: Could not read header size. Closing dat-file: " << openFileDesc.filename << endl << flush;
140 void *configcache::getHeader() {
141 return readHeaderData;
144 bool configcache::readConfig()
146 if(DATADIR == "" || MODE == CACHE_MODE_DISABLED) return false;
148 if(refetchDataFiles){
149 refetchDataFiles = false;
155 if( (!inFile.is_open()) && inFiles.size() == 0 ) return false;
157 while( (!inFile.is_open()) && inFiles.size() > 0 ) {
158 if(out) *out->log << "CCACHE: Opening dat-file: " << inFiles.back().filename << endl << flush;
160 openFileDesc = inFiles.back();
161 inFile.open( (DATADIR + "/" + inFiles.back().filename).c_str(), std::ios::binary );
164 if( !inFile.is_open() ) continue;
166 inBuffer = new boost::iostreams::filtering_istreambuf;
167 inBuffer->push( boost::iostreams::bzip2_decompressor() );
168 inBuffer->push(inFile);
171 if( inFile.is_open() )
173 if (openFileDesc.doVirtualEquilibration) {
174 if(out) *out->log << "CCACHE: Trying virtual equilibration." << endl << flush;
175 openFileDesc.doVirtualEquilibration = false;
176 for (int iskip=0; iskip < (NEQUI-openFileDesc.nequi)/openFileDesc.nskip; iskip++) {
177 if( readFullBlock(tmpConfig, configSize) != configSize || ! inFile.is_open() )
182 if( readFullBlock(tmpConfig, configSize) == configSize && inFile.is_open() )
184 memcpy(configMem, tmpConfig, configSize);
188 if(out) *out->log << "CCACHE: Could not read configuration. Closing dat-file: " << openFileDesc.filename << endl << flush;
195 void configcache::openOutFile()
197 time_t secstamp = time(NULL);
200 outFileName << DATADIR << "/" << secstamp << "_" << getFileId() << "_.edat.tmp";
201 outFile.open( outFileName.str().c_str(), std::ios::binary );
203 outBuffer = new boost::iostreams::filtering_ostreambuf;
204 outBuffer->push(boost::iostreams::bzip2_compressor());
205 outBuffer->push(outFile);
207 headerWritten = false;
210 void configcache::writeHeader(char *header, long unsigned int size) {
211 if( DATADIR == "" || MODE < 2 ) return;
213 if(!outFile.is_open())
216 boost::iostreams::write(*outBuffer, (char*)&size, sizeof(long unsigned int));
217 boost::iostreams::write(*outBuffer, header, size);
219 headerWritten = true;
222 void configcache::writeConfig()
224 if ( DATADIR == "" || MODE < 2 ) return;
226 if ( ! outFile.is_open() )
229 if ( ! headerWritten ) {
230 long unsigned int zeroheader=0;
231 boost::iostreams::write(*outBuffer, (char*)&zeroheader, sizeof(long unsigned int));
234 boost::iostreams::write(*outBuffer, configMem, configSize);
235 headerWritten = false;
238 void configcache::addPara(const string& parid, const double& val){
242 Paras.push_back(newPara);
245 int configcache::getParIndex(const string& parid){
246 for(int ipara=0; ipara<Paras.size(); ipara++)
247 if(Paras[ipara].id == parid) return ipara;
250 void configcache::setPara(const string& parid, const double& value){
251 Paras[getParIndex(parid)].val = value;
253 if(inBuffer != NULL) { delete inBuffer; inBuffer=NULL; }
257 refetchDataFiles = true;
260 configcache::~configcache()
267 void configcache::finishOutFile()
269 if( outBuffer != NULL )
275 if( outFile.is_open() )
278 rename( outFileName.str().c_str(), outFileName.str().substr(0, outFileName.str().size()-4).c_str() );
282 int configcache::readFullBlock(char *tmpData, long unsigned int dataSize)
284 /* try to read header */
285 if ( openFileDesc.extended )
286 if ( ! readHeader() )
290 return readDataToMem(tmpData, dataSize);
293 int configcache::readDataToMem(char *tmpData, long unsigned int dataSize)
297 if ( dataSize == 0 ) return 0;
299 try { readturn = boost::iostreams::read(*inBuffer, tmpData, dataSize); }
300 catch(boost::iostreams::bzip2_error& error) {
301 if(out) *out->log << "CCACHE: Caught bzip2 exception with error code: " << error.error() << endl << flush;
304 catch (std::exception const& ex) {
305 if(out) *out->log << "CCACHE: Caught exception: " << ex.what() << endl << flush;
309 if(out) *out->log << "CCACHE: Caught unknown exception while reading." << endl << flush;