HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_LocalDiskCache.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_LocalDiskCache.h ( UT Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __UT_LocalDiskCache__
12 #define __UT_LocalDiskCache__
13 
14 #include "UT_API.h"
15 #include "UT_CappedCache.h"
16 #include "UT_ConcurrentQueue.h"
17 #include "UT_String.h"
18 #include <stdio.h>
19 
20 /// @brief Class used to lookup cache items
22 {
23 public:
25  : UT_CappedKey()
26  {}
27  virtual ~UT_LocalDiskCacheKey();
28 
29  /// @{
30  /// Pure virtual methods which need to be implemented in the subclass
31  virtual UT_CappedKey *duplicate() const = 0;
32  virtual unsigned getHash() const = 0;
33  virtual bool isEqual(const UT_CappedKey &key) const=0;
34  /// @}
35 
36 private:
37  /// The getTotalMemory() method should @b not be implemented in the
38  /// sub-class. It is important that this always return "1".
39  virtual int64 getTotalMemory() const;
40 };
41 
42 /// @brief A disk cache to store expensive data on a local disk
43 ///
44 /// In some cases, accessing data can be expensive:
45 /// - Loading over a saturated network
46 /// - An expensive computation
47 /// This cache provides a means to store this data on disk. The disk file is
48 /// automatically cleaned up when the program completes.
49 ///
50 /// This cache makes a reasonable secondary cache.
52 {
53 public:
54  /// The cache is used to store data in a temporary disk file
55  /// @c max_item_size is the maximum size for each item put into the cache
56  /// @c max_item_count is the maximum number of items to be stored in the
57  /// cache
58  /// It's fairly obvious that the maximum size for the disk file will
59  /// be @c max_item_size*max_item_count.
60  UT_LocalDiskCache(const char *cache_name,
61  int64 max_item_size,
62  int64 max_item_count);
64 
65  /// Clear entire disk cache. This is not thread safe and should only be
66  /// called when the cache isn't under contention.
67  void clearCache(bool reset_file=true);
68 
69  /// Extract from the disk cache. Once extracted, the item will no longer
70  /// exist in the cache.
71  ///
72  /// The @remove_from_cache flag determines whether the object will be
73  /// removed from the disk cache after extraction. Since this cache is
74  /// usually used as a secondary cache, it's usually the primary cache which
75  /// extracts the data from the disk cache (keeping the object in the
76  /// primary cache after).
77  bool extractFromCache(const UT_LocalDiskCacheKey &key,
78  void *buffer, int64 &buffer_size,
79  bool remove_from_cache = true);
80 
81  /// Store in the cache
82  void storeInCache(const UT_LocalDiskCacheKey &key,
83  const void *buffer, int64 buffer_size);
84 
85  /// @private Used internally
86  void itemFreed(int64 o, int64 s);
87 
88  /// Get memory used
89  int64 getMemoryUsed() const;
90 
91  /// Get the file size
92  int64 getFileSize() const { return myFileSize; }
93 
94  /// Traverse the cache
95  template <typename T> void traverseCache(T &task)
96  {
97  myCache.threadSafeTraversal(task);
98  }
99 
100  /// Const access to the cache
101  const UT_CappedCache &getCache() const { return myCache; }
102 
103 private:
104  bool lockAndLoad(int64 offset, void *data, int64 size);
105  bool lockAndSave(int64 offset, const void *data, int64 size);
106  void growFile(int64 size);
107  void initQueue();
108 
109  UT_CappedCache myCache;
110  UT_ConcurrentQueue<int64> myQueue;
111  FILE *myFp;
112  UT_Lock myLock;
113  int64 myMaxItemSize, myMaxItemCount;
114  int64 myFileSize;
115 };
116 
117 #endif
int64 getFileSize() const
Get the file size.
void traverseCache(T &task)
Traverse the cache.
Base class for search keys for UT_CappedCache.
virtual bool isEqual(const UT_CappedKey &key) const =0
Test equality.
GT_API const UT_StringHolder cache_name
#define UT_API
Definition: UT_API.h:13
GLuint buffer
Definition: glcorearb.h:659
GLsizeiptr size
Definition: glcorearb.h:663
long long int64
Definition: SYS_Types.h:107
GLintptr offset
Definition: glcorearb.h:664
virtual unsigned int getHash() const =0
Return a hash for the key.
GLboolean * data
Definition: glcorearb.h:130
Class used to lookup cache items.
virtual UT_CappedKey * duplicate() const =0
The duplicate() method should return a copy of the key.
png_infop png_bytep png_size_t buffer_size
Definition: png.h:2124
const UT_CappedCache & getCache() const
Const access to the cache.
A disk cache to store expensive data on a local disk.