]> git.treefish.org Git - phys/latlib.git/blob - configcache.cpp
556dba720c6465460e3f0df6cb249b2afc6d3b6b
[phys/latlib.git] / configcache.cpp
1 #include "configcache.h"
2
3 #include <stdlib.h>
4 #include <iostream>
5 #include <time.h>
6 #include <dirent.h>
7
8 configcache::configcache(const string& cacheid, const int& nequi, const int& nskip, const string& datadir, char **configmem, const int& configMemSize, const int& cachemode,
9                          writeout *out_a){
10   out = out_a;
11
12   NEQUI = nequi;
13   NSKIP = nskip;
14   DATADIR = datadir;
15   CACHEID = cacheid;
16
17   configMem = (char*)malloc(configMemSize);
18   tmpConfig = (char*)malloc(configMemSize);
19
20   *configmem = configMem;
21   configSize = configMemSize;
22
23   outBuffer = NULL;
24   inBuffer = NULL;
25
26   MODE = cachemode;
27
28   refetchDataFiles = false;
29 }
30
31 string configcache::getFileId(const bool& shortid)
32 {
33   stringstream fileid;
34
35   if(!shortid) fileid << CACHEID << "_" << NEQUI << "_" << NSKIP;
36   for(int ipara=0; ipara<Paras.size(); ipara++)
37     fileid << "_" << Paras[ipara].id << Paras[ipara].val;
38
39   return fileid.str();
40 }
41
42 void configcache::fetchDataFiles()
43 {
44   struct dirent *de=NULL;
45   DIR *d=NULL;
46   
47   d=opendir(DATADIR.c_str());
48   if(d != NULL){
49     while(de = readdir(d)){
50       string filename = de->d_name;
51       if(isValidInFile(filename)) 
52         {
53           inFiles.push_back(filename);
54         }
55     }
56   }
57 }
58
59 bool configcache::isValidInFile(const string& infile)
60 {
61   char *inchar, *inParts;
62   string truncIn, truncOut;
63
64   if( infile.size() < 4 ) return false;
65
66   if( infile.substr(infile.size()-3) != "dat" ) return false;
67
68   inchar = new char [infile.size()+1];
69   strcpy (inchar, infile.c_str());
70   
71   inParts = strtok( inchar, "_" );
72   for(int iPart=0; inParts!=NULL; iPart++)
73     {
74       if( iPart>3 ) { truncIn += "_"; truncIn += inParts; }
75
76       switch(iPart)
77         {
78         case 1: if(inParts != CACHEID) return false; break;
79         case 2: if(atoi(inParts) != NEQUI) return false; break;
80         case 3: if(atoi(inParts) != NSKIP) return false; break;
81         }
82       inParts = strtok( NULL, "_");
83     }
84   truncIn = truncIn.substr(0, truncIn.size()-4);
85
86   delete[] inchar;
87
88   if( truncIn.find( getFileId(true) + "_" ) == string::npos ) return false;
89
90   return true;
91 }
92
93 bool configcache::readConfig()
94 {
95   if(DATADIR == "" || MODE == CACHE_MODE_DISABLED) return false;
96
97   if(refetchDataFiles){
98     refetchDataFiles = false;
99     fetchDataFiles();
100   }
101
102   while(true)
103     {
104       if( (!inFile.is_open()) && inFiles.size() == 0 ) return false;
105
106       while( (!inFile.is_open()) && inFiles.size() > 0 )
107         {
108           if(out) *out->log << "CCACHE: Opening dat-file: " << inFiles.back() << endl << flush;
109
110           openFileName = inFiles.back();
111           inFile.open( (DATADIR + "/" + inFiles.back()).c_str(), std::ios::binary );
112           inFiles.pop_back();
113
114           if( !inFile.is_open() ) continue;
115
116           std::streampos fsize = inFile.tellg();
117           inFile.seekg( 0, std::ios::end );
118           fsize = inFile.tellg() - fsize;
119           if( (int)fsize == 0 ) {
120             *out->log << "CCACHE: dat-file has zero length! Skipping." << endl << flush;
121             inFile.close();
122             continue;
123           }
124           inFile.seekg(0);
125
126           inBuffer = new boost::iostreams::filtering_istreambuf;
127           inBuffer->push( boost::iostreams::bzip2_decompressor() );
128           inBuffer->push(inFile);
129         }
130
131       if( inFile.is_open() ) 
132         {
133           int readturn = -1;
134           try { readturn = boost::iostreams::read(*inBuffer, tmpConfig, configSize); }
135           catch (std::exception const& ex) {
136             if(out) *out->log << "CCACHE: Caught exception: " << ex.what() << endl << flush;
137             inFile.close();
138           }
139           
140           if( readturn == configSize && inFile.is_open() )
141             {
142               memcpy(configMem, tmpConfig, configSize);
143               return true;
144             }
145           else {
146             *out->log << "CCACHE: Closing dat-file: " << openFileName << endl << flush;
147             inFile.close();
148           }
149         }
150     }
151 }
152
153 void configcache::writeConfig()
154 {
155   if( DATADIR == "" || MODE < 2 ) return;
156
157   if(!outFile.is_open()){
158     time_t secstamp = time(NULL);
159
160     outFileName.str("");
161     outFileName << DATADIR << "/" << secstamp << "_" << getFileId() << "_.dat.tmp";    
162     outFile.open( outFileName.str().c_str(), std::ios::binary );
163
164     outBuffer = new boost::iostreams::filtering_ostreambuf;
165     outBuffer->push(boost::iostreams::bzip2_compressor());
166     outBuffer->push(outFile);
167   }
168
169   boost::iostreams::write(*outBuffer, configMem, configSize);
170 }
171
172 void configcache::addPara(const string& parid, const double& val){
173   parameter newPara;
174   newPara.id = parid;
175   newPara.val = val;
176   Paras.push_back(newPara);
177 }
178
179 int configcache::getParIndex(const string& parid){
180   for(int ipara=0; ipara<Paras.size(); ipara++)
181     if(Paras[ipara].id == parid) return ipara;
182 }
183
184 void configcache::setPara(const string& parid, const double& value){
185   Paras[getParIndex(parid)].val = value;
186   finishOutFile();
187   if(inBuffer != NULL) { delete inBuffer; inBuffer=NULL; } 
188   inFile.close();
189   inFiles.clear();
190
191   refetchDataFiles = true;
192 }
193
194 configcache::~configcache()
195 {
196   finishOutFile();
197   delete inBuffer;
198   inBuffer = NULL;
199 }
200
201 void configcache::finishOutFile()
202 {
203   if( outBuffer != NULL )
204     {
205       delete outBuffer;
206       outBuffer = NULL;
207     }
208
209   if( outFile.is_open() )
210     {
211       outFile.close();
212       rename( outFileName.str().c_str(), outFileName.str().substr(0, outFileName.str().size()-4).c_str() );
213     }
214 }