]> git.treefish.org Git - phys/latlib.git/blob - configcache.h
...
[phys/latlib.git] / configcache.h
1 #ifndef CONFIGCACHE_H
2 #define CONFIGCACHE_H
3
4 #include <vector>
5 #include <string>
6 #include <fstream>
7 #include <sstream>
8 #include <ostream>
9
10 #include <boost/iostreams/filtering_streambuf.hpp>
11 #include <boost/iostreams/stream.hpp>
12 #include <boost/iostreams/filter/bzip2.hpp>
13 #include <boost/iostreams/device/array.hpp>
14 #include <boost/iostreams/copy.hpp>
15
16 #define CACHE_MODE_DISABLED 0
17 #define CACHE_MODE_READ 1
18 #define CACHE_MODE_FULL 2
19
20 using namespace std;
21
22 struct parameter{
23   string id;
24   double val;
25 };
26
27 struct infiledesc {
28   string filename;
29   int nequi;
30   int nskip;
31   bool doVirtualEquilibration;
32   bool extended;
33   bool firstConfig;
34 };
35
36 class configcache{
37  public:
38   ~configcache();
39   configcache(const string& cacheid, const int& nequi, const int& nskip, const string& datadir, char **configmem, const int& configMemSize, 
40               const int& cachemode=CACHE_MODE_FULL, ostream *_log=NULL);
41   void readConfig(bool& readnewconfig, int& nequileft, vector<unsigned long> *excludeFileHashes=NULL);
42   void writeConfig(int actnequi);
43   void addPara(const string& parid, const double& val=0);
44   void setPara(const string& parid, const double& value);
45   void writeHeader(const string& headerid, const char *header, long unsigned int size, int actnequi);
46   void * getHeader(const string& headerid);
47   string getOutFileName() { return outFileName.str(); }
48   string getInFileName() { return DATADIR + "/" + openFileDesc.filename; }
49   static unsigned long hash(const string& str);
50   void closeInFile() { inFile.close(); }
51   int inFilesLeft() { return inFiles.size(); }
52   char* getConfigMem() { return configMem; }
53   int getConfigSize() { return configSize; }
54
55  private:
56   ostream* log;
57   infiledesc openFileDesc;
58   void finishOutFile();
59   int getParIndex(const string& parid);
60   int NEQUI;
61   int NSKIP;
62   string DATADIR;
63   string CACHEID;
64   int MODE;
65   string getFileId(int actnequi, const bool& shortid=false);
66
67   ofstream outFile;
68   ifstream inFile;
69
70   stringstream outFileName;
71
72   int readnum;
73
74   boost::iostreams::filtering_istreambuf *inBuffer;
75   boost::iostreams::filtering_ostreambuf *outBuffer;
76
77   int inSize;
78
79   int configSize;
80   char *configMem;
81   char *tmpConfig;
82
83   bool refetchDataFiles;
84
85   void fetchDataFiles();
86   
87   bool isValidInFile(const string& infile, infiledesc *filedesc);
88
89   vector<infiledesc> inFiles;
90
91   vector<parameter> Paras;
92
93   int readDataToMem(char *tmpData, long unsigned int dataSize);
94
95   void openOutFile(int actnequi);
96
97   int readHeader();
98
99   bool headerWritten;
100
101   int readFullBlock(char *tmpData, long unsigned int dataSize);
102
103   vector< pair<unsigned long, void *> > headerStore;
104
105   void deleteHeaderStore();
106
107   bool readAllHeaders();
108
109   vector<infiledesc>::iterator getNextInfile(vector<unsigned long> *excludeFileHashes);
110
111   int nequileft_internal;
112 };
113
114 #endif