Disk ARchive
2.3.11
|
00001 /*********************************************************************/ 00002 // dar - disk archive - a backup/restoration program 00003 // Copyright (C) 2002-2052 Denis Corbin 00004 // 00005 // This program is free software; you can redistribute it and/or 00006 // modify it under the terms of the GNU General Public License 00007 // as published by the Free Software Foundation; either version 2 00008 // of the License, or (at your option) any later version. 00009 // 00010 // This program is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with this program; if not, write to the Free Software 00017 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00018 // 00019 // to contact the author : dar.linux@free.fr 00020 /*********************************************************************/ 00021 // $Id: cache.hpp,v 1.10.2.2 2009/04/07 08:45:29 edrusb Rel $ 00022 // 00023 /*********************************************************************/ 00024 00028 00029 #ifndef CACHE_HPP 00030 #define CACHE_HPP 00031 00032 #include "../my_config.h" 00033 #include "infinint.hpp" 00034 #include "generic_file.hpp" 00035 00036 namespace libdar 00037 { 00039 00050 class cache : public generic_file 00051 { 00052 public: 00053 cache(user_interaction & dialog, 00054 generic_file & hidden, 00055 U_I initial_size = 10240, 00056 U_I unused_read_ratio = 10, U_I observation_read_number = 100, U_I max_size_hit_read_ratio = 50, 00057 U_I unused_write_ratio = 10, U_I observation_write_number = 100, U_I max_size_hit_write_ratio = 50); 00058 // *** hidden: is the file to cache, it is never deleted by the cache object, 00059 // *** intial_size: is the initial size of the cache for reading (read this amount of data at a time) 00060 // unused_read_ratio: is the ratio of cached data effectively asked for reading below which the cache size is divided by two 00061 // in other words, if during observation_read_number fullfilment of the cache, less than unused_read_ratio percent of data has been 00062 // effectively asked for reading, then the cache size is divided by two 00063 // *** max_size_hit_read_ratio: is the ratio above which the cache size is doubled. In other words, if during observation_read_number 00064 // times of cache fullfilment, more than max_size_hit_read_ratio percent time all the cached data has been asked for reading 00065 // the cache size is doubled. 00066 // to have fixed size read caching, you can set unused_read_ratio to zero and max_size_hit_read_ratio to 101 or above. 00067 // *** unused_write_ratio: cache is flushed before being fullfilled if a seek operation has been asked. If after observation_write_number 00068 // of cache flushing, less than unused_write_ratio percent of the time the cache was full, its size is divided by 2 00069 // *** max_size_hit_write_ratio: if after observation_write_number of cache flushing, max_size_hit_write_ratio percent of the time 00070 // the cache was fulled, its size get doubled. 00071 // to have a fixed size write caching , you can set unused_write_ratio to zero and max_size_hit_write_ratio to 101 or above 00072 ~cache() { flush_write(); }; 00073 00074 00075 // inherited from generic_file 00076 bool skip(const infinint & pos) { flush_write(); clear_read(); return ref->skip(pos); }; 00077 bool skip_to_eof() { flush_write(); clear_read(); return ref->skip_to_eof(); }; 00078 bool skip_relative(S_I x) { flush_write(); clear_read(); return ref->skip_relative(x); }; 00079 infinint get_position() { return ref->get_position(); }; 00080 00081 protected: 00082 // inherited from generic_file 00083 S_I inherited_read(char *a, size_t size); 00084 S_I inherited_write(const char *a, size_t size); 00085 00086 private: 00087 struct buf 00088 { 00089 char *buffer; 00090 U_I size; // allocated size 00091 U_I next; // next to read or next place to write to 00092 U_I last; // first to not read in the cache 00093 00094 buf() { buffer = NULL; size = next = last = 0; }; 00095 ~buf() { if(buffer != NULL) delete [] buffer; }; 00096 }; 00097 00098 generic_file *ref; 00099 00100 struct buf buffer_cache; 00101 bool read_mode; // true if in read mode, false if in write mode 00102 00103 U_I read_obs; 00104 U_I read_unused_rate; 00105 U_I read_overused_rate; 00106 00107 U_I write_obs; 00108 U_I write_unused_rate; 00109 U_I write_overused_rate; 00110 00111 U_I stat_read_unused; 00112 U_I stat_read_overused; 00113 U_I stat_read_counter; 00114 00115 U_I stat_write_overused; 00116 U_I stat_write_counter; 00117 00118 void flush_write(); 00119 void fulfill_read(); 00120 void clear_read() { if(read_mode) { buffer_cache.next = buffer_cache.last = 0; } }; 00121 }; 00122 00123 } // end of namespace 00124 00125 #endif 00126