Disk ARchive  2.4.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
cache.hpp
Go to the documentation of this file.
1 /*********************************************************************/
2 // dar - disk archive - a backup/restoration program
3 // Copyright (C) 2002-2052 Denis Corbin
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 //
19 // to contact the author : http://dar.linux.free.fr/email.html
20 /*********************************************************************/
21 
25 
26 #ifndef CACHE_HPP
27 #define CACHE_HPP
28 
29 #include "../my_config.h"
30 #include "infinint.hpp"
31 #include "generic_file.hpp"
32 
33 namespace libdar
34 {
35 
38 
40 
50  class cache : public generic_file
51  {
52  public:
53  cache(generic_file & hidden, //< is the file to cache, it is never deleted by the cache object,
54  bool shift_mode, //< if true, when all cached data has been read, half of the data is flushed from the cache, the other half is shifted and new data take place to fill the cache. This is necessary for sequential reading, but has some CPU overhead.
55  U_I initial_size = 10240, //< is the initial size of the cache for reading (read this amount of data at a time)
56  U_I unused_read_ratio = 10, //< is the ratio of cached data effectively asked for reading below which the cache size is divided by two in other words, if during observation_read_number fullfilment of the cache, less than unused_read_ratio percent of data has been effectively asked for reading, then the cache size is divided by two
57  U_I observation_read_number = 100, //< period of cache size consideration
58  U_I max_size_hit_read_ratio = 50, //< is the ratio above which the cache size is doubled. In other words, if during observation_read_number times of cache fullfilment, more than max_size_hit_read_ratio percent time all the cached data has been asked for reading the cache size is doubled. To have fixed size read caching, you can set unused_read_ratio to zero and max_size_hit_read_ratio to 101 or above.
59  U_I unused_write_ratio = 10, //< same as unused_read_ratio but for writing operations
60  U_I observation_write_number = 100, //< same as observation_read_number but for writing operations
61  U_I max_size_hit_write_ratio = 50); //< same as max_size_hit_read_ratio but for writing operations
62  cache(const cache & ref) : generic_file(gf_read_only) { throw SRC_BUG; };
63  const cache & operator = (const cache & ref) { throw SRC_BUG; };
64 
65  ~cache();
66 
67 
68  // inherited from generic_file
69  bool skip(const infinint & pos);
70  bool skip_to_eof();
71  bool skip_relative(S_I x);
72  infinint get_position() { return current_position; };
73 
74  protected:
75  // inherited from generic_file
76  U_I inherited_read(char *a, U_I size);
77  void inherited_write(const char *a, U_I size);
78  void inherited_sync_write() { flush_write(); };
79  void inherited_terminate() { flush_write(); };
80  private:
81  struct buf
82  {
83  char *buffer;
84  U_I size; // allocated size
85  U_I next; // next to read or next place to write to
86  U_I last; // first to not read in the cache
87 
88  buf() { buffer = NULL; size = next = last = 0; };
89  buf(const buf &ref) { throw SRC_BUG; };
90  ~buf() { if(buffer != NULL) delete [] buffer; };
91  void resize(U_I newsize);
92  void shift_by_half();
93  void clear() { next = last = 0; };
94  };
95 
96  generic_file *ref; //< underlying file, (not owned by "this', not to be delete by "this")
97 
98  struct buf buffer_cache; //< where is stored cached data
99  infinint current_position; //< current offset in file to read from or write to
100  bool read_mode; //< true if in read mode, false if in write mode
101  bool shifted_mode; //< whether to half flush and shift or totally flush data
102  bool failed_increase; //< whether we failed increasing the cache size
103  U_I max_alloc_size; //< if set to non zero value, retains the maximum size not to exceed for block allocation
104 
105  U_I read_obs;
106  U_I read_unused_rate;
107  U_I read_overused_rate;
108 
109  U_I write_obs;
110  U_I write_unused_rate;
111  U_I write_overused_rate;
112 
113  U_I stat_read_unused;
114  U_I stat_read_overused;
115  U_I stat_read_counter;
116 
117  U_I stat_write_overused;
118  U_I stat_write_counter;
119 
120  void flush_write();
121  void fulfill_read();
122  void clear_read() { if(read_mode) buffer_cache.clear(); };
123  };
124 
126 
127 } // end of namespace
128 
129 #endif
130 
infinint get_position()
get the current read/write position
Definition: cache.hpp:72
bool skip(const infinint &pos)
skip at the absolute position
class generic_file is defined here as well as class fichierthe generic_file interface is widely used ...
U_I inherited_read(char *a, U_I size)
implementation of read() operation
void inherited_write(const char *a, U_I size)
implementation of the write() operation
bool skip_relative(S_I x)
skip relatively to the current position
void inherited_sync_write()
write down any pending data
Definition: cache.hpp:78
void inherited_terminate()
destructor-like call, except that it is allowed to throw exceptions
Definition: cache.hpp:79
generic_file(gf_mode m)
main constructor
the cache class implements a very basic read/write caching mechanism
Definition: cache.hpp:50
switch module to limitint (32 ou 64 bits integers) or infinint
this is the interface class from which all other data transfer classes inherit
the arbitrary large positive integer class
bool skip_to_eof()
skip to the end of file
read only access