00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef __UT_LocalDiskCache__
00019 #define __UT_LocalDiskCache__
00020
00021 #include "UT_API.h"
00022 #include "UT_LimitedCache.h"
00023 #include "UT_ConcurrentQueue.h"
00024 #include "UT_String.h"
00025 #include <stdio.h>
00026
00027
00028 class UT_API UT_LocalDiskCacheKey : public UT_LimitedCacheKey
00029 {
00030 public:
00031 UT_LocalDiskCacheKey()
00032 : UT_LimitedCacheKey()
00033 {}
00034 virtual ~UT_LocalDiskCacheKey();
00035
00036
00037
00038 virtual UT_LimitedCacheKey *duplicate() const = 0;
00039 virtual unsigned getHash() const = 0;
00040 virtual bool isEqual(const UT_LimitedCacheKey &key) const=0;
00041
00042
00043 private:
00044
00045
00046 virtual int64 getTotalMemory() const;
00047 };
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 class UT_API UT_LocalDiskCache
00059 {
00060 public:
00061
00062
00063
00064
00065
00066
00067 UT_LocalDiskCache(const char *cache_name,
00068 int64 max_item_size,
00069 int64 max_item_count);
00070 ~UT_LocalDiskCache();
00071
00072
00073
00074 void clearCache(bool reset_file=true);
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 bool extractFromCache(const UT_LocalDiskCacheKey &key,
00085 void *buffer, int64 &buffer_size,
00086 bool remove_from_cache = true);
00087
00088
00089 void storeInCache(const UT_LocalDiskCacheKey &key,
00090 const void *buffer, int64 buffer_size);
00091
00092
00093 void itemFreed(int64 o, int64 s);
00094
00095
00096 int64 getMemoryUsed() const;
00097
00098
00099 int64 getFileSize() const { return myFileSize; }
00100
00101
00102
00103 typedef UT_LimitedCache::unsafe_traverser unsafe_traverser;
00104 unsafe_traverser unsafe_begin() const
00105 { return myCache.unsafe_begin(); }
00106 unsafe_traverser unsafe_end() const
00107 { return myCache.unsafe_end(); }
00108
00109
00110 private:
00111 bool lockAndLoad(int64 offset, void *data, int64 size);
00112 bool lockAndSave(int64 offset, const void *data, int64 size);
00113 void growFile(int64 size);
00114 void initQueue();
00115
00116 UT_LimitedCache myCache;
00117 UT_ConcurrentQueue<int64> myQueue;
00118 FILE *myFp;
00119 UT_Lock myLock;
00120 int64 myMaxItemSize, myMaxItemCount;
00121 int64 myFileSize;
00122 };
00123
00124 #endif