00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifndef eman__hdf_filecache__h__
00037 #define eman__hdf_filecache__h__ 1
00038
00039 #ifdef HDFIO_CACHE
00040
00041 #include <string>
00042 #include <cstdio>
00043 #include <ctime>
00044 #include <functional>
00045
00046 #include <boost/unordered_map.hpp>
00047 #include <boost/thread.hpp>
00048
00049 #include "imageio.h"
00050
00051 using std::string;
00052 using std::binary_function;
00053
00054 const static int CHECK_INTERVAL = 10;
00055 const static int ACCESS_TIME_THRESHOLD = 30;
00056
00057 namespace EMAN
00058 {
00059
00065 class FileItem {
00066 public:
00067 FileItem();
00068 FileItem(const string& fpath, ImageIO* const fimageio, const time_t& ftimestamp, bool freadonly);
00069 ~FileItem();
00070
00071 int set_path(const string& fpath);
00072 string get_path() const;
00073
00074 int set_imgio(ImageIO* const fimageio);
00075 ImageIO * get_imgio() const;
00076
00077 int set_timestamp(const time_t& ftimestamp);
00078 time_t get_timestamp() const;
00079
00080 int set_readonly(bool freadonly);
00081 bool get_readonly() const;
00082
00083 private:
00084 string _path;
00085 ImageIO * _imgio;
00086 time_t _timestamp;
00087 bool _readonly;
00088 };
00089
00092 class HDFCache {
00093 public:
00094 static HDFCache *instance();
00095
00096
00097 FileItem * get_file(const string& filename);
00098
00099 int add_file(FileItem * newfile);
00100
00101 private:
00102 HDFCache();
00103 HDFCache(const HDFCache&);
00104 ~HDFCache();
00105
00106 static HDFCache * _instance;
00107
00108
00109 typedef boost::unordered_map<string, FileItem *> MyHashtable;
00110 MyHashtable file_pool;
00111
00112
00113 vector<FileItem *> file_pool2;
00114
00115 boost::thread _thread;
00116 boost::mutex m_mutex;
00117
00118 size_t CACHESIZE;
00119
00120
00121 int purge_file();
00122 int force_clean();
00123 void cache_routine();
00124 int close_file(const string& filename);
00125
00126
00127 struct least_access : public binary_function<FileItem *, FileItem *, bool>{
00128 bool operator()(FileItem * x, FileItem * y) const {
00129 return x->get_timestamp() < y->get_timestamp();
00130 }
00131 };
00132
00133
00134 struct access_time_cmp : public binary_function<FileItem *, int, bool>{
00135 bool operator()(FileItem * f, int interval) const {
00136 return f->get_timestamp() < time(0)-ACCESS_TIME_THRESHOLD;
00137 }
00138 };
00139 };
00140
00141 }
00142
00143 #endif //HDFIO_CACHE
00144
00145 #endif //eman__hdf_filecache__h__