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){
14 configMem = (char*)malloc(configMemSize);
15 tmpConfig = (char*)malloc(configMemSize);
17 *configmem = configMem;
18 configSize = configMemSize;
25 refetchDataFiles = false;
28 string configcache::getFileId(const bool& shortid)
32 if(!shortid) fileid << CACHEID << "_" << NEQUI << "_" << NSKIP;
33 for(int ipara=0; ipara<Paras.size(); ipara++)
34 fileid << "_" << Paras[ipara].id << Paras[ipara].val;
39 void configcache::fetchDataFiles()
41 struct dirent *de=NULL;
44 d=opendir(DATADIR.c_str());
46 while(de = readdir(d)){
47 string filename = de->d_name;
48 if(isValidInFile(filename))
50 inFiles.push_back(filename);
56 bool configcache::isValidInFile(const string& infile)
58 char *inchar, *inParts;
59 string truncIn, truncOut;
61 if( infile.size() < 4 ) return false;
63 if( infile.substr(infile.size()-3) != "dat" ) return false;
65 inchar = new char [infile.size()+1];
66 strcpy (inchar, infile.c_str());
68 inParts = strtok( inchar, "_" );
69 for(int iPart=0; inParts!=NULL; iPart++)
71 if( iPart>3 ) { truncIn += "_"; truncIn += inParts; }
75 case 1: if(inParts != CACHEID) return false; break;
76 case 2: if(atoi(inParts) < NEQUI) return false; break;
77 case 3: if(atoi(inParts) < NSKIP) return false; break;
79 inParts = strtok( NULL, "_");
81 truncIn = truncIn.substr(0, truncIn.size()-4);
85 if( truncIn.find( getFileId(true) + "_" ) == string::npos ) return false;
90 bool configcache::readConfig()
92 if(DATADIR == "" || MODE == CACHE_MODE_DISABLED) return false;
95 refetchDataFiles = false;
101 if( (!inFile.is_open()) && inFiles.size() == 0 ) return false;
103 while( (!inFile.is_open()) && inFiles.size() > 0 )
105 inFile.open( (DATADIR + "/" + inFiles.back()).c_str(), std::ios::binary );
108 if( !inFile.is_open() ) continue;
110 inBuffer = new boost::iostreams::filtering_istreambuf;
111 inBuffer->push( boost::iostreams::bzip2_decompressor() );
112 inBuffer->push(inFile);
115 if( inFile.is_open() )
117 if( boost::iostreams::read(*inBuffer, tmpConfig, configSize) == configSize )
119 memcpy(configMem, tmpConfig, configSize);
127 void configcache::writeConfig()
129 if( DATADIR == "" || MODE < 2 ) return;
131 if(!outFile.is_open()){
132 time_t secstamp = time(NULL);
135 outFileName << DATADIR << "/" << secstamp << "_" << getFileId() << "_.dat.tmp";
136 outFile.open( outFileName.str().c_str(), std::ios::binary );
138 outBuffer = new boost::iostreams::filtering_ostreambuf;
139 outBuffer->push(boost::iostreams::bzip2_compressor());
140 outBuffer->push(outFile);
143 boost::iostreams::write(*outBuffer, configMem, configSize);
146 void configcache::addPara(const string& parid, const double& val){
150 Paras.push_back(newPara);
153 int configcache::getParIndex(const string& parid){
154 for(int ipara=0; ipara<Paras.size(); ipara++)
155 if(Paras[ipara].id == parid) return ipara;
158 void configcache::setPara(const string& parid, const double& value){
159 Paras[getParIndex(parid)].val = value;
161 if(inBuffer != NULL) { delete inBuffer; inBuffer=NULL; }
165 refetchDataFiles = true;
168 configcache::~configcache()
175 void configcache::finishOutFile()
177 if( outBuffer != NULL )
183 if( outFile.is_open() )
186 rename( outFileName.str().c_str(), outFileName.str().substr(0, outFileName.str().size()-4).c_str() );