Panda3D
bamCache.h
1 // Filename: bamCache.h
2 // Created by: drose (09Jun06)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef BAMCACHE_H
16 #define BAMCACHE_H
17 
18 #include "pandabase.h"
19 #include "bamCacheRecord.h"
20 #include "pointerTo.h"
21 #include "filename.h"
22 #include "pmap.h"
23 #include "pvector.h"
24 #include "reMutex.h"
25 #include "reMutexHolder.h"
26 
27 #include <time.h>
28 
29 class BamCacheIndex;
30 
31 ////////////////////////////////////////////////////////////////////
32 // Class : BamCache
33 // Description : This class maintains a cache of Bam and/or Txo
34 // objects generated from model files and texture images
35 // (as well as possibly other kinds of loadable objects
36 // that can be stored in bam file format).
37 //
38 // This class also maintains a persistent index that
39 // lists all of the cached objects (see BamCacheIndex).
40 // We go through some considerable effort to make sure
41 // this index gets saved correctly to disk, even in the
42 // presence of multiple different processes writing to
43 // the same index, and without relying too heavily on
44 // low-level os-provided file locks (which work poorly
45 // with C++ iostreams).
46 ////////////////////////////////////////////////////////////////////
47 class EXPCL_PANDA_PUTIL BamCache {
48 PUBLISHED:
49  BamCache();
50  ~BamCache();
51 
52  INLINE void set_active(bool flag);
53  INLINE bool get_active() const;
54 
55  INLINE void set_cache_models(bool flag);
56  INLINE bool get_cache_models() const;
57 
58  INLINE void set_cache_textures(bool flag);
59  INLINE bool get_cache_textures() const;
60 
61  INLINE void set_cache_compressed_textures(bool flag);
62  INLINE bool get_cache_compressed_textures() const;
63 
64  void set_root(const Filename &root);
65  INLINE Filename get_root() const;
66 
67  INLINE void set_flush_time(int flush_time);
68  INLINE int get_flush_time() const;
69 
70  INLINE void set_cache_max_kbytes(int max_kbytes);
71  INLINE int get_cache_max_kbytes() const;
72 
73  INLINE void set_read_only(bool ro);
74  INLINE bool get_read_only() const;
75 
76  PT(BamCacheRecord) lookup(const Filename &source_filename,
77  const string &cache_extension);
78  bool store(BamCacheRecord *record);
79 
80  void consider_flush_index();
81  void flush_index();
82 
83  void list_index(ostream &out, int indent_level = 0) const;
84 
85  INLINE static BamCache *get_global_ptr();
86  INLINE static void consider_flush_global_index();
87  INLINE static void flush_global_index();
88 
89 private:
90  void read_index();
91  bool read_index_pathname(Filename &index_pathname,
92  string &index_ref_contents) const;
93  void merge_index(BamCacheIndex *new_index);
94  void rebuild_index();
95  INLINE void mark_index_stale();
96 
97  void add_to_index(const BamCacheRecord *record);
98  void remove_from_index(const Filename &source_filename);
99 
100  void check_cache_size();
101 
102  void emergency_read_only();
103 
104  static BamCacheIndex *do_read_index(const Filename &index_pathname);
105  static bool do_write_index(const Filename &index_pathname, const BamCacheIndex *index);
106 
107  PT(BamCacheRecord) find_and_read_record(const Filename &source_pathname,
108  const Filename &cache_filename);
109  PT(BamCacheRecord) read_record(const Filename &source_pathname,
110  const Filename &cache_filename,
111  int pass);
112  static PT(BamCacheRecord) do_read_record(const Filename &cache_pathname,
113  bool read_data);
114 
115  static string hash_filename(const string &filename);
116  static void make_global();
117 
118  bool _active;
119  bool _cache_models;
120  bool _cache_textures;
121  bool _cache_compressed_textures;
122  bool _read_only;
123  Filename _root;
124  int _flush_time;
125  int _max_kbytes;
126  static BamCache *_global_ptr;
127 
128  BamCacheIndex *_index;
129  time_t _index_stale_since;
130 
131  Filename _index_pathname;
132  string _index_ref_contents;
133 
134  ReMutex _lock;
135 };
136 
137 #include "bamCache.I"
138 
139 #endif
This class maintains a cache of Bam and/or Txo objects generated from model files and texture images ...
Definition: bamCache.h:47
This represents the in-memory index that records the list of files stored in the BamCache.
Definition: bamCacheIndex.h:37
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
An instance of this class is written to the front of a Bam or Txo file to make the file a cached inst...
A reentrant mutex.
Definition: reMutex.h:36