1 #include "configcache.h"
11 #include <boost/iostreams/filtering_streambuf.hpp>
12 #include <boost/iostreams/stream.hpp>
13 #include <boost/iostreams/filter/bzip2.hpp>
14 #include <boost/iostreams/device/array.hpp>
15 #include <boost/iostreams/copy.hpp>
17 #define HEADER_READOK 0
18 #define HEADER_READERR 1
19 #define HEADER_READLAST 2
21 struct configcache::iobuffers
23 boost::iostreams::filtering_ostreambuf *out;
26 configcache::configcache(const string& cacheid, const int& nequi, const int& nskip, const string& datadir, char **configmem, const int& configMemSize, const int& cachemode,
30 dataReader = new datread(configMemSize, log);
37 if ( cacheid.find("_") != -1 ) {
38 if(log) *log << "CCACHE: Invalid cacheid \"" << cacheid << "\" given. Cacheids must not contain underscores!" << endl << flush;
42 configMem = (char*)malloc(configMemSize);
43 tmpConfig = (char*)malloc(configMemSize);
45 *configmem = configMem;
46 configSize = configMemSize;
48 ioBuffers = new iobuffers;
49 ioBuffers->out = NULL;
53 refetchDataFiles = false;
56 string configcache::paraString() {
57 stringstream parastring;
59 for(int ipara=0; ipara<Paras.size(); ipara++)
60 parastring << "_" << Paras[ipara].id << Paras[ipara].val;
62 return parastring.str();
65 string configcache::getFileId(int actnequi, const bool& superextended, const bool& shortid)
69 if(!shortid) fileid << CACHEID << "_" << actnequi << "_" << NSKIP;
72 fileid << "_" << hash( paraString() );
74 fileid << paraString();
79 void configcache::fetchDataFiles()
81 struct dirent *de=NULL;
83 static infiledesc filedesc;
85 d=opendir(DATADIR.c_str());
87 while(de = readdir(d)){
88 string filename = de->d_name;
89 if(isValidInFile(filename, &filedesc))
91 inFiles.push_back(filedesc);
97 bool configcache::isValidInFile(const string& infile, infiledesc *filedesc)
99 char *inchar, *inParts;
100 string truncIn, truncOut;
102 filedesc->filename = infile;
104 if( infile.size() < 4 ) return false;
106 if( infile.substr(infile.size()-4) == ".dat" ) {
107 filedesc->extended = false;
108 filedesc->superextended = false;
110 else if( infile.substr(infile.size()-4) == "edat" ) {
111 filedesc->extended = true;
112 filedesc->superextended = false;
114 else if( infile.substr(infile.size()-4) == "sdat" ) {
115 filedesc->extended = true;
116 filedesc->superextended = true;
121 inchar = new char [infile.size()+1];
122 strcpy (inchar, infile.c_str());
124 inParts = strtok( inchar, "_" );
125 for(int iPart=0; inParts!=NULL; iPart++)
127 if( iPart>3 ) { truncIn += "_"; truncIn += inParts; }
131 case 1: if(inParts != CACHEID)
135 filedesc->nequi = atoi(inParts);
138 if(atoi(inParts) != NSKIP)
140 filedesc->nskip = atoi(inParts);
143 inParts = strtok( NULL, "_");
145 truncIn = truncIn.substr(0, truncIn.size()-4);
149 if( truncIn.find( getFileId(NEQUI, filedesc->superextended, true) + "_" ) == string::npos ) return false;
154 void * configcache::getHeader(const string& headerid) {
155 return dataReader->getHeader(headerid);
158 void configcache::readConfig(bool *readnewconfig, int *nequileft, vector<unsigned long> *excludeFileHashes)
160 *readnewconfig = false;
162 if( DATADIR == "" || !(MODE==CACHE_MODE_RO||MODE==CACHE_MODE_RW) ) return;
164 if(refetchDataFiles){
165 refetchDataFiles = false;
171 vector<infiledesc>::iterator inFileIt = getNextInfile(excludeFileHashes);
172 int iDidVirtualSkips;
174 if( (!dataReader->fisopen()) && inFileIt == inFiles.end() ) {
176 *nequileft = nequileft_internal;
180 while( (!dataReader->fisopen()) && inFiles.size() > 0 ) {
181 string inFileParaString;
183 openFileDesc = *inFileIt;
185 if (openFileDesc.nequi < NEQUI)
186 doVirtualEquilibration = true;
188 doVirtualEquilibration = false;
190 firstUsedConfig = true;
192 if(log) *log << "CCACHE: Opening dat-file: " << inFileIt->filename << endl << flush;
194 dataReader->openFile( DATADIR + "/" + inFileIt->filename );
196 if( openFileDesc.superextended ) {
197 if( dataReader->getParaString() != paraString() ) {
198 if(log) *log << "CCACHE: Parastring does not match. Closing dat-file..." << endl << flush;
199 dataReader->closeFile();
203 inFiles.erase(inFileIt);
205 if( !dataReader->fisopen() ) continue;
208 if( dataReader->fisopen() )
210 if (doVirtualEquilibration) {
211 if(log) *log << "CCACHE: Trying virtual equilibration." << endl << flush;
212 doVirtualEquilibration = false;
213 for (iDidVirtualSkips=0; iDidVirtualSkips < (NEQUI-openFileDesc.nequi)/openFileDesc.nskip; iDidVirtualSkips++) {
214 if( dataReader->readFullBlock(tmpConfig) < 0 )
216 else if ( (NEQUI-openFileDesc.nequi) - (iDidVirtualSkips+1)*openFileDesc.nskip < nequileft_internal ) {
217 memcpy(configMem, tmpConfig, configSize);
218 nequileft_internal = NEQUI - openFileDesc.nequi - (iDidVirtualSkips+1)*openFileDesc.nskip;
219 *readnewconfig = true;
220 firstUsedConfig = false;
225 if( dataReader->readFullBlock(tmpConfig) >= 0 )
227 memcpy(configMem, tmpConfig, configSize);
228 *readnewconfig = true;
229 if (firstUsedConfig) {
230 firstUsedConfig = false;
231 if (openFileDesc.nequi < NEQUI)
232 nequileft_internal = NEQUI - openFileDesc.nequi - iDidVirtualSkips*openFileDesc.nskip;
234 nequileft_internal = NEQUI - openFileDesc.nequi;
236 nequileft_internal -= openFileDesc.nskip;
237 *nequileft = nequileft_internal;
244 void configcache::openOutFile(int actnequi)
246 time_t secstamp = time(NULL);
251 outFileName << DATADIR << "/" << secstamp << "." << iseq << "_" << getFileId(actnequi, true, false) << "_.sdat.tmp";
253 int tmpfd = open(outFileName.str().c_str(), O_CREAT | O_EXCL, 0644);
259 else if ( errno != EEXIST ) {
260 if(log) *log << "CCACHE: Could not create cachefile!" << endl << flush;
267 outFile.open( outFileName.str().c_str(), std::ios::binary );
269 outFile << paraString() << endl;
271 ioBuffers->out = new boost::iostreams::filtering_ostreambuf;
272 ioBuffers->out->push(boost::iostreams::bzip2_compressor());
273 ioBuffers->out->push(outFile);
276 void configcache::writeHeader(const string& headerid, const char *header, long unsigned int size, int actnequi) {
277 unsigned long headeridhash;
279 if( DATADIR == "" || !(MODE==CACHE_MODE_WO||MODE==CACHE_MODE_RW) ) return;
281 if(!outFile.is_open())
282 openOutFile(actnequi);
284 headeridhash = hash(headerid);
286 boost::iostreams::write(*ioBuffers->out, (char*)&size, sizeof(long unsigned int));
287 boost::iostreams::write(*ioBuffers->out, (char*)&headeridhash, sizeof(unsigned long));
288 boost::iostreams::write(*ioBuffers->out, header, size);
291 void configcache::writeConfig(int actnequi)
293 long unsigned int zeroheader=0;
295 if ( DATADIR == "" || !(MODE==CACHE_MODE_WO||MODE==CACHE_MODE_RW) ) return;
297 if ( ! outFile.is_open() )
298 openOutFile(actnequi);
300 boost::iostreams::write(*ioBuffers->out, (char*)&zeroheader, sizeof(long unsigned int));
302 boost::iostreams::write(*ioBuffers->out, configMem, configSize);
305 void configcache::addPara(const string& parid, const double& val){
309 Paras.push_back(newPara);
312 int configcache::getParIndex(const string& parid){
313 for(int ipara=0; ipara<Paras.size(); ipara++)
314 if(Paras[ipara].id == parid) return ipara;
317 void configcache::setPara(const string& parid, const double& value){
318 Paras[getParIndex(parid)].val = value;
321 dataReader->closeFile();
324 refetchDataFiles = true;
325 nequileft_internal = NEQUI;
328 configcache::~configcache()
334 void configcache::finishOutFile()
336 if( ioBuffers->out != NULL )
338 delete ioBuffers->out;
339 ioBuffers->out = NULL;
342 if( outFile.is_open() )
345 rename( outFileName.str().c_str(), outFileName.str().substr(0, outFileName.str().size()-4).c_str() );
349 unsigned long configcache::hash(const string& str)
351 unsigned long hash = 5381;
353 for(string::const_iterator it=str.begin();it!=str.end();it++)
354 hash = ((hash << 5) + hash) + *it; /* hash * 33 + character */
359 vector<infiledesc>::iterator configcache::getNextInfile(vector<unsigned long> *excludeFileHashes) {
360 for (vector<infiledesc>::iterator init = inFiles.begin(); init != inFiles.end(); ++init) {
361 if (excludeFileHashes != NULL) {
362 bool excludethisfile = false;
364 for (vector<unsigned long>::iterator exit = excludeFileHashes->begin(); exit != excludeFileHashes->end(); ++exit)
365 if ( *exit == hash(init->filename) ) {
366 excludethisfile = true;
375 return inFiles.end();