EMAN2
hdf_filecache.h
Go to the documentation of this file.
1/*
2 * Author: Grant Tang, 05/01/2012 (gtang@bcm.edu)
3 * Copyright (c) 2000-2006 Baylor College of Medicine
4 *
5 * This software is issued under a joint BSD/GNU license. You may use the
6 * source code in this file under either license. However, note that the
7 * complete EMAN2 and SPARX software packages have some GPL dependencies,
8 * so you are responsible for compliance with the licenses of these packages
9 * if you opt to use BSD licensing. The warranty disclaimer below holds
10 * in either instance.
11 *
12 * This complete copyright notice must be included in any revised version of the
13 * source code. Additional authorship citations may be added, but existing
14 * author citations must be preserved.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 *
30 * */
31
32#ifndef eman__hdf_filecache__h__
33#define eman__hdf_filecache__h__ 1
34
35#ifdef HDFIO_CACHE
36
37#include <string>
38#include <cstdio>
39#include <ctime>
40#include <functional>
41
42#include "imageio.h"
43
44using std::string;
45using std::binary_function;
46
47const static int CHECK_INTERVAL = 5; //seconds for thread to check if the opened file need been closed
48const static int ACCESS_TIME_THRESHOLD = 30; //seconds since the last access to close HDF image file
49const static int ACCESS_WRITE_TIME_THRESHOLD = 5; //seconds since the last access to close HDF image file
50
51namespace EMAN
52{
53
59class FileItem {
60public:
61 FileItem();
62 FileItem(const string& fpath, ImageIO* const fimageio, const time_t& ftimestamp, bool freadonly);
63 ~FileItem();
64
65 int set_path(const string& fpath);
66 string get_path() const;
67
68 int set_imgio(ImageIO* const fimageio);
69 ImageIO * get_imgio() const;
70
71 int set_timestamp(const time_t& ftimestamp);
72 time_t get_timestamp() const;
73
74 int set_readonly(bool freadonly);
75 bool get_readonly() const;
76
77private:
78 string _path; //full path name to the opened file
79 ImageIO * _imgio; //image I/O object to the opened file
80 time_t _timestamp; //last access time
81 bool _readonly; //Is the file is opened in read only mode
82};
83
86class HDFCache {
87public:
88 static HDFCache *instance();
89
90 //file name is a full path name
91 FileItem * get_file(const string& filename);
92
93 int add_file(FileItem * newfile);
94
95private:
96 HDFCache();
97 HDFCache(const HDFCache&);
98 ~HDFCache();
99
100 static HDFCache * _instance;
101
102 //Primary file handle container for fast accessing by file name
103 typedef boost::unordered_map<string, FileItem *> MyHashtable;
104 MyHashtable file_pool;
105
106 //Secondary file handle container for fast sorting to close those files expired
107 vector<FileItem *> file_pool2;
108
109 boost::thread _thread;
110 boost::mutex m_mutex;
111
112 size_t CACHESIZE; //size limit for the HDF file cache, half of the file descriptor limit per process
113
114 //These functions are running in the thread
115 int purge_file(); //purge the file has last access time >= threshold
116 int force_clean(); //close some files when the cache reach it's size limit
117 void cache_routine(); //function for background thread
118 int close_file(const string& filename);
119
120 //functor for sorting file pointer by access time
121 struct least_access : public binary_function<FileItem *, FileItem *, bool>{
122 bool operator()(FileItem * x, FileItem * y) const {
123 return x->get_timestamp() < y->get_timestamp();
124 }
125 };
126
127 //functor for finding those files have access time over the threshold
128 struct access_time_cmp : public binary_function<FileItem *, int, bool>{
129 bool operator()(FileItem * f, int interval) const {
130 return f->get_timestamp() < time(0)-ACCESS_TIME_THRESHOLD;
131 }
132 };
133};
134
135}
136
137#endif //HDFIO_CACHE
138
139#endif //eman__hdf_filecache__h__
float & operator()(const int ix, const int iy, const int iz) const
Overload operator() for array indexing.
Definition: emdata_core.h:727
void set_path(const string &new_path)
Set the path.
string get_path() const
E2Exception class.
Definition: aligner.h:40
#define y(i, j)
Definition: projector.cpp:1516
#define x(i)
Definition: projector.cpp:1517