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:
24 
25  /// @{
26  /// Pure virtual methods which need to be implemented in the subclass
27  UT_CappedKey *duplicate() const override = 0;
28  unsigned getHash() const override = 0;
29  bool isEqual(const UT_CappedKey &key) const override = 0;
30  /// @}
31 
32 private:
33  /// The getTotalMemory() method should @b not be implemented in the
34  /// sub-class. It is important that this always return "1".
35  virtual int64 getTotalMemory() const;
36 };
37 
38 /// @brief A disk cache to store expensive data on a local disk
39 ///
40 /// In some cases, accessing data can be expensive:
41 /// - Loading over a saturated network
42 /// - An expensive computation
43 /// This cache provides a means to store this data on disk. The disk file is
44 /// automatically cleaned up when the program completes.
45 ///
46 /// This cache makes a reasonable secondary cache.
48 {
49 public:
50  /// The cache is used to store data in a temporary disk file
51  /// @c max_item_size is the maximum size for each item put into the cache
52  /// @c max_item_count is the maximum number of items to be stored in the
53  /// cache
54  /// It's fairly obvious that the maximum size for the disk file will
55  /// be @c max_item_size*max_item_count.
56  UT_LocalDiskCache(const char *cache_name,
57  int64 max_item_size,
58  int64 max_item_count);
60 
61  /// Clear entire disk cache. This is not thread safe and should only be
62  /// called when the cache isn't under contention.
63  void clearCache(bool reset_file=true);
64 
65  /// Extract from the disk cache. Once extracted, the item will no longer
66  /// exist in the cache.
67  ///
68  /// The @c remove_from_cache flag determines whether the object will be
69  /// removed from the disk cache after extraction. Since this cache is
70  /// usually used as a secondary cache, it's usually the primary cache which
71  /// extracts the data from the disk cache (keeping the object in the
72  /// primary cache after).
73  bool extractFromCache(const UT_LocalDiskCacheKey &key,
74  void *buffer, int64 &buffer_size,
75  bool remove_from_cache = true);
76 
77  /// Store in the cache
78  void storeInCache(const UT_LocalDiskCacheKey &key,
79  const void *buffer, int64 buffer_size);
80 
81  /// @private Used internally
82  void itemFreed(int64 o, int64 s);
83 
84  /// Get memory used
85  int64 getMemoryUsed() const;
86 
87  /// Get the file size
88  int64 getFileSize() const { return myFileSize; }
89 
90  /// Traverse the cache
91  template <typename T> void traverseCache(T &task)
92  {
93  myCache.threadSafeTraversal(task);
94  }
95 
96  /// Const access to the cache
97  const UT_CappedCache &getCache() const { return myCache; }
98 
99 private:
100  bool lockAndLoad(int64 offset, void *data, int64 size);
101  bool lockAndSave(int64 offset, const void *data, int64 size);
102  void growFile(int64 size);
103  void initQueue();
104 
105  UT_CappedCache myCache;
106  UT_ConcurrentQueue<int64> myQueue;
107  FILE *myFp;
108  UT_Lock myLock;
109  int64 myMaxItemSize, myMaxItemCount;
110  int64 myFileSize;
111 };
112 
113 #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
GLdouble s
Definition: glad.h:3009
#define UT_API
Definition: UT_API.h:14
GLintptr offset
Definition: glcorearb.h:665
Definition: core.h:760
long long int64
Definition: SYS_Types.h:116
virtual unsigned int getHash() const =0
Return a hash for the key.
GLsizeiptr size
Definition: glcorearb.h:664
Class used to lookup cache items.
virtual UT_CappedKey * duplicate() const =0
The duplicate() method should return a copy of the key.
const UT_CappedCache & getCache() const
Const access to the cache.
A disk cache to store expensive data on a local disk.
Definition: format.h:895