Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

hdf_filecache.h

Go to the documentation of this file.
00001 
00005 /*
00006  * Author: Grant Tang, 05/01/2012 (gtang@bcm.edu)
00007  * Copyright (c) 2000-2006 Baylor College of Medicine
00008  *
00009  * This software is issued under a joint BSD/GNU license. You may use the
00010  * source code in this file under either license. However, note that the
00011  * complete EMAN2 and SPARX software packages have some GPL dependencies,
00012  * so you are responsible for compliance with the licenses of these packages
00013  * if you opt to use BSD licensing. The warranty disclaimer below holds
00014  * in either instance.
00015  *
00016  * This complete copyright notice must be included in any revised version of the
00017  * source code. Additional authorship citations may be added, but existing
00018  * author citations must be preserved.
00019  *
00020  * This program is free software; you can redistribute it and/or modify
00021  * it under the terms of the GNU General Public License as published by
00022  * the Free Software Foundation; either version 2 of the License, or
00023  * (at your option) any later version.
00024  *
00025  * This program is distributed in the hope that it will be useful,
00026  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00027  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00028  * GNU General Public License for more details.
00029  *
00030  * You should have received a copy of the GNU General Public License
00031  * along with this program; if not, write to the Free Software
00032  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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;   //seconds for thread to check if the opened file need been closed
00055 const static int ACCESS_TIME_THRESHOLD = 30;    //seconds since the last access to close HDF image file
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;          //full path name to the opened file
00085         ImageIO *       _imgio;         //image I/O object to the opened file
00086         time_t          _timestamp;     //last access time
00087         bool            _readonly;      //Is the file is opened in read only mode
00088 };
00089 
00092 class HDFCache {
00093 public:
00094         static HDFCache *instance();
00095 
00096         //file name is a full path name
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         //Primary file handle container for fast accessing by file name
00109         typedef boost::unordered_map<string, FileItem *> MyHashtable;
00110         MyHashtable file_pool;
00111 
00112         //Secondary file handle container for fast sorting to close those files expired
00113         vector<FileItem *> file_pool2;
00114 
00115         boost::thread   _thread;
00116         boost::mutex    m_mutex;
00117 
00118         size_t CACHESIZE;               //size limit for the HDF file cache, half of the file descriptor limit per process
00119 
00120         //These functions are running in the thread
00121         int purge_file();               //purge the file has last access time >= threshold
00122         int force_clean();              //close some files when the cache reach it's size limit
00123         void cache_routine();   //function for background thread
00124         int close_file(const string& filename);
00125 
00126         //functor for sorting file pointer by access time
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         //functor for finding those files have access time over the threshold
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__

Generated on Tue Jun 11 13:46:15 2013 for EMAN2 by  doxygen 1.3.9.1