37#include <sys/resource.h>
43 _path(
""), _imgio(0), _timestamp(0), _readonly(true)
47FileItem::FileItem(
const string& fpath,
ImageIO*
const fimageio,
const time_t& ftimestamp,
bool freadonly) :
48 _path(fpath), _imgio(fimageio), _timestamp(ftimestamp), _readonly(freadonly)
68int FileItem::set_imgio(
ImageIO*
const fimageio)
74ImageIO * FileItem::get_imgio()
const
79int FileItem::set_timestamp(
const time_t& ftimestamp)
81 _timestamp = ftimestamp;
85time_t FileItem::get_timestamp()
const
90int FileItem::set_readonly(
bool freadonly)
92 _readonly = freadonly;
96bool FileItem::get_readonly()
const
102HDFCache * HDFCache::_instance = 0;
106 _thread = boost::thread(&HDFCache::cache_routine,
this);
109 getrlimit(RLIMIT_NOFILE, &rl);
110 CACHESIZE = rl.rlim_cur/2;
114HDFCache * HDFCache::instance()
117 _instance =
new HDFCache();
123void HDFCache::cache_routine()
125 boost::posix_time::seconds intervalTime(CHECK_INTERVAL);
131 boost::this_thread::sleep(intervalTime);
133 catch (std::exception& exc) {
134 std::cerr <<
"Very Bad Thing Happen in HDFCache routine: " << exc.what() << std::endl;
139FileItem * HDFCache::get_file(
const string& filename)
141 boost::mutex::scoped_lock l(m_mutex);
143 if(file_pool.find(filename) != file_pool.end()) {
144 FileItem * hdf_item = file_pool[filename];
145 hdf_item->set_timestamp(time(0));
153int HDFCache::add_file(FileItem * newfile)
155 if(file_pool.size() >= CACHESIZE) {
159 boost::mutex::scoped_lock l(m_mutex);
160 file_pool[newfile->get_path()] = newfile;
161 file_pool2.push_back(newfile);
168int HDFCache::close_file(
const string& filename)
170 FileItem * hdfitem = file_pool[filename];
172 ImageIO * hdfio = hdfitem->get_imgio();
174 file_pool[filename]->set_imgio(0);
175 file_pool.erase(filename);
177 vector<FileItem *>::iterator it = find(file_pool2.begin(), file_pool2.end(), hdfitem);
178 file_pool2.erase(it);
183int HDFCache::purge_file()
185 boost::mutex::scoped_lock l(m_mutex);
188 sort(file_pool2.begin(), file_pool2.end(), least_access());
190 vector<FileItem *>::iterator it2 = lower_bound(file_pool2.begin(), file_pool2.end(), ACCESS_TIME_THRESHOLD, access_time_cmp());
192 if(it2 != file_pool2.begin()) {
193 list<FileItem *> expired_file;
194 copy(file_pool2.begin(), it2, back_inserter(expired_file));
195 list<FileItem *>::const_iterator lit;
196 for(lit = expired_file.begin(); lit!=expired_file.end(); ++lit) {
197 string filename = (*lit)->get_path();
198 close_file(filename);
205int HDFCache::force_clean()
207 boost::mutex::scoped_lock l(m_mutex);
210 if(file_pool2.size() >= CACHESIZE) {
211 sort(file_pool2.begin(), file_pool2.end(), least_access());
212 for(
size_t i=0; i<=CACHESIZE/10; ++i) {
213 close_file(file_pool2.front()->get_path());
ImageIO classes are designed for reading/writing various electron micrography image formats,...
EMData * copy() const
This file is a part of "emdata.h", to use functions in this file, you should "#include "emdata....