VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gvcache.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file gvcache.h
19 ///
20 
21 #ifndef GV_CACHE__H
22 #define GV_CACHE__H
23 
24 #include "gdef.h"
25 #include "grimagebuffer.h"
26 #include "gexception.h"
27 #include "gpath.h"
28 #include <vector>
29 #include <string>
30 
31 namespace Gv
32 {
33  class Cache ;
34 }
35 
36 /// \class Gv::Cache
37 /// A queue of recent images that can be flushed to the filesystem on demand.
38 ///
39 /// The implementation keeps a large set of cache files open and re-writes them
40 /// on a least-recently used basis. When a commit is requested the files are
41 /// closed and moved into their proper place (see ImageOutput). By keeping the
42 /// files open the images live in the kernel's buffer cache and do not need to
43 /// hit the filesystem at all.
44 ///
45 class Gv::Cache
46 {
47 public:
48  G_EXCEPTION( Error , "cache error" ) ;
49 
50  explicit Cache( const G::Path & base_dir , const std::string & name , size_t size ) ;
51  ///< Constructor. Paths are like "<base-dir>/.cache/<name>.123".
52 
53  ~Cache() ;
54  ///< Destructor.
55 
56  void store( const Gr::ImageBuffer & , const std::string & commit_path ,
57  const std::string & commit_path_other = std::string() ,
58  const std::string & same_as = std::string() ) ;
59  ///< Stores an image in the cache.
60  ///<
61  ///< An alternate path for the committed file can be supplied,
62  ///< with the choice dictated by a parameter passed to commit().
63  ///<
64  ///< The "same-as" path indicates where the image has already
65  ///< been saved to the filesystem, so a commit() just moves
66  ///< the already-saved file into place and discards the
67  ///< cached version. (In practice this avoids duplicates
68  ///< when recording in slow mode and then switching to fast
69  ///< mode with an associated flush of the cache.)
70 
71  void commit( bool other = false ) ;
72  ///< Commits all cached images to their non-cache location.
73 
74  std::string base() const ;
75  ///< Returns the base directory, as passed to the constructor.
76 
77 private:
78  struct Entry
79  {
80  Entry( int fd_ = -1 , const std::string & cache_path_ = std::string() , const std::string & commit_path_ = std::string() ) :
81  fd(fd_) ,
82  cache_path(cache_path_) ,
83  commit_path(commit_path_)
84  {
85  }
86  int fd ;
87  std::string cache_path ;
88  std::string commit_path ;
89  std::string commit_path_other ;
90  std::string same_as_path ;
91  } ;
92 
93 private:
94  Cache( const Cache & ) ;
95  void operator=( const Cache & ) ;
96  std::string commitPath( const std::string & , G::EpochTime ) const ;
97  void fail( const char * where ) ;
98  void open( Entry & e ) ;
99  void write( Entry & e , const Gr::ImageBuffer & ) ;
100  void commit( Entry & e , bool ) ;
101  void close( Entry & e ) ;
102  bool move( const std::string & src , const std::string & dst ) ;
103  void mkdirs( const G::Path & path ) ;
104 
105 private:
106  typedef std::vector<Entry> List ;
107  G::Path m_base_dir ;
108  std::string m_prefix ;
109  G::Path m_cache_dir ;
110  List m_list ;
111  List::iterator m_p ;
112  bool m_ok ;
113 } ;
114 
115 #endif
A subsecond-resolution timestamp based on a time_t.
Definition: gdatetime.h:39
Vectors ImageBuffer
An ImageBuffer is used to hold raw image data, typically in more than one chunk.
Definition: grimagebuffer.h:47
~Cache()
Destructor.
Definition: gvcache.cpp:60
Cache(const G::Path &base_dir, const std::string &name, size_t size)
Constructor. Paths are like "<base-dir>/.cache/<name>.123".
Definition: gvcache.cpp:37
void store(const Gr::ImageBuffer &, const std::string &commit_path, const std::string &commit_path_other=std::string(), const std::string &same_as=std::string())
Stores an image in the cache.
Definition: gvcache.cpp:83
void commit(bool other=false)
Commits all cached images to their non-cache location.
Definition: gvcache.cpp:130
std::string base() const
Returns the base directory, as passed to the constructor.
Definition: gvcache.cpp:71
A queue of recent images that can be flushed to the filesystem on demand.
Definition: gvcache.h:45
A Path object represents a file system path.
Definition: gpath.h:72