Panda3D

bamCache.h

00001 // Filename: bamCache.h
00002 // Created by:  drose (09Jun06)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #ifndef BAMCACHE_H
00016 #define BAMCACHE_H
00017 
00018 #include "pandabase.h"
00019 #include "bamCacheRecord.h"
00020 #include "pointerTo.h"
00021 #include "filename.h"
00022 #include "pmap.h"
00023 #include "pvector.h"
00024 #include "reMutex.h"
00025 #include "reMutexHolder.h"
00026 
00027 #include <time.h>
00028 
00029 class BamCacheIndex;
00030 
00031 ////////////////////////////////////////////////////////////////////
00032 //       Class : BamCache
00033 // Description : This class maintains a cache of Bam and/or Txo
00034 //               objects generated from model files and texture images
00035 //               (as well as possibly other kinds of loadable objects
00036 //               that can be stored in bam file format).
00037 //
00038 //               This class also maintains a persistent index that
00039 //               lists all of the cached objects (see BamCacheIndex).
00040 //               We go through some considerable effort to make sure
00041 //               this index gets saved correctly to disk, even in the
00042 //               presence of multiple different processes writing to
00043 //               the same index, and without relying too heavily on
00044 //               low-level os-provided file locks (which work poorly
00045 //               with C++ iostreams).
00046 ////////////////////////////////////////////////////////////////////
00047 class EXPCL_PANDA_PUTIL BamCache {
00048 PUBLISHED:
00049   BamCache();
00050   ~BamCache();
00051 
00052   INLINE void set_active(bool flag);
00053   INLINE bool get_active() const;
00054 
00055   INLINE void set_cache_models(bool flag);
00056   INLINE bool get_cache_models() const;
00057 
00058   INLINE void set_cache_textures(bool flag);
00059   INLINE bool get_cache_textures() const;
00060 
00061   INLINE void set_cache_compressed_textures(bool flag);
00062   INLINE bool get_cache_compressed_textures() const;
00063 
00064   void set_root(const Filename &root);
00065   INLINE Filename get_root() const;
00066 
00067   INLINE void set_flush_time(int flush_time);
00068   INLINE int get_flush_time() const;
00069 
00070   INLINE void set_cache_max_kbytes(int max_kbytes);
00071   INLINE int get_cache_max_kbytes() const;
00072 
00073   INLINE void set_read_only(bool ro);
00074   INLINE bool get_read_only() const;
00075 
00076   PT(BamCacheRecord) lookup(const Filename &source_filename, 
00077                             const string &cache_extension);
00078   bool store(BamCacheRecord *record);
00079 
00080   void consider_flush_index();
00081   void flush_index();
00082   
00083   INLINE static BamCache *get_global_ptr();
00084 
00085 private:
00086   void read_index();
00087   bool read_index_pathname(Filename &index_pathname,
00088                            string &index_ref_contents) const;
00089   void merge_index(BamCacheIndex *new_index);
00090   void rebuild_index();
00091   INLINE void mark_index_stale();
00092 
00093   void add_to_index(const BamCacheRecord *record);
00094   void remove_from_index(const Filename &source_filename);
00095 
00096   void check_cache_size();
00097 
00098   void emergency_read_only();
00099   
00100   static BamCacheIndex *do_read_index(const Filename &index_pathname);
00101   static bool do_write_index(const Filename &index_pathname, const BamCacheIndex *index);
00102 
00103   PT(BamCacheRecord) find_and_read_record(const Filename &source_pathname,
00104                                           const Filename &cache_filename);
00105   PT(BamCacheRecord) read_record(const Filename &source_pathname,
00106                                  const Filename &cache_filename,
00107                                  int pass);
00108   static PT(BamCacheRecord) do_read_record(const Filename &cache_pathname, 
00109                                            bool read_data);
00110 
00111   static string hash_filename(const string &filename);
00112   static void make_global();
00113 
00114   bool _active;
00115   bool _cache_models;
00116   bool _cache_textures;
00117   bool _cache_compressed_textures;
00118   bool _read_only;
00119   Filename _root;
00120   int _flush_time;
00121   int _max_kbytes;
00122   static BamCache *_global_ptr;
00123 
00124   BamCacheIndex *_index;
00125   time_t _index_stale_since;
00126 
00127   Filename _index_pathname;
00128   string _index_ref_contents;
00129 
00130   ReMutex _lock;
00131 };
00132 
00133 #include "bamCache.I"
00134 
00135 #endif
 All Classes Functions Variables Enumerations