]> 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   int readEquilibratedConfigs;
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
53  private:
54   ostream* log;
55   infiledesc openFileDesc;
56   void finishOutFile();
57   int getParIndex(const string& parid);
58   int NEQUI;
59   int NSKIP;
60   string DATADIR;
61   string CACHEID;
62   int MODE;
63   string getFileId(int actnequi, const bool& shortid=false);
64
65   ofstream outFile;
66   ifstream inFile;
67
68   stringstream outFileName;
69
70   int readnum;
71
72   boost::iostreams::filtering_istreambuf *inBuffer;
73   boost::iostreams::filtering_ostreambuf *outBuffer;
74
75   int inSize;
76
77   int configSize;
78   char *configMem;
79   char *tmpConfig;
80
81   bool refetchDataFiles;
82
83   void fetchDataFiles();
84   
85   bool isValidInFile(const string& infile, infiledesc *filedesc);
86
87   vector<infiledesc> inFiles;
88
89   vector<parameter> Paras;
90
91   int readDataToMem(char *tmpData, long unsigned int dataSize);
92
93   void openOutFile(int actnequi);
94
95   int readHeader();
96
97   bool headerWritten;
98
99   int readFullBlock(char *tmpData, long unsigned int dataSize);
100
101   vector< pair<unsigned long, void *> > headerStore;
102
103   void deleteHeaderStore();
104
105   bool readAllHeaders();
106
107   vector<infiledesc>::iterator getNextInfile(vector<unsigned long> *excludeFileHashes);
108 };
109
110 #endif