diff options
Diffstat (limited to '')
-rw-r--r-- | src/cache.h | 81 |
1 files changed, 68 insertions, 13 deletions
diff --git a/src/cache.h b/src/cache.h index 259278c..ccc4966 100644 --- a/src/cache.h +++ b/src/cache.h | |||
@@ -11,19 +11,18 @@ | |||
11 | 11 | ||
12 | namespace Bu | 12 | namespace Bu |
13 | { | 13 | { |
14 | // template<class obtype, class keytype> | ||
15 | // keytype __cacheGetKey( obtype *&pObj ); | ||
16 | |||
14 | template<class obtype, class keytype> | 17 | template<class obtype, class keytype> |
15 | class Cache | 18 | class Cache |
16 | { | 19 | { |
17 | //friend class Bu::CPtr<obtype, keytype>; | ||
18 | public: | 20 | public: |
19 | // typedef Bu::CPtr<obtype, keytype> Ptr; | ||
20 | |||
21 | /** | 21 | /** |
22 | * Cache Pointer - Provides access to data that is held within the | 22 | * Cache Pointer - Provides access to data that is held within the |
23 | * cache. This provides safe, refcounting access to data stored in | 23 | * cache. This provides safe, refcounting access to data stored in |
24 | * the cache, with support for lazy loading. | 24 | * the cache, with support for lazy loading. |
25 | */ | 25 | */ |
26 | //template<class obtype, class keytype> | ||
27 | class Ptr | 26 | class Ptr |
28 | { | 27 | { |
29 | friend class Bu::Cache<obtype, keytype>; | 28 | friend class Bu::Cache<obtype, keytype>; |
@@ -37,6 +36,13 @@ namespace Bu | |||
37 | if( pCache ) | 36 | if( pCache ) |
38 | pCache->incRef( kId ); | 37 | pCache->incRef( kId ); |
39 | } | 38 | } |
39 | |||
40 | Ptr( Cache<obtype, keytype> *pCache, const keytype &kId ) : | ||
41 | pCache( pCache ), | ||
42 | pData( NULL ), | ||
43 | kId( kId ) | ||
44 | { | ||
45 | } | ||
40 | 46 | ||
41 | public: | 47 | public: |
42 | Ptr( const Ptr &rSrc ) : | 48 | Ptr( const Ptr &rSrc ) : |
@@ -44,7 +50,7 @@ namespace Bu | |||
44 | pData( rSrc.pData ), | 50 | pData( rSrc.pData ), |
45 | kId( rSrc.kId ) | 51 | kId( rSrc.kId ) |
46 | { | 52 | { |
47 | if( pCache ) | 53 | if( pCache && pData ) |
48 | pCache->incRef( kId ); | 54 | pCache->incRef( kId ); |
49 | } | 55 | } |
50 | 56 | ||
@@ -56,40 +62,69 @@ namespace Bu | |||
56 | 62 | ||
57 | virtual ~Ptr() | 63 | virtual ~Ptr() |
58 | { | 64 | { |
59 | if( pCache ) | 65 | if( pCache && pData ) |
60 | pCache->decRef( kId ); | 66 | pCache->decRef( kId ); |
61 | } | 67 | } |
62 | 68 | ||
63 | obtype &operator*() | 69 | obtype &operator*() |
64 | { | 70 | { |
71 | checkPtr(); | ||
72 | return *pData; | ||
73 | } | ||
74 | |||
75 | const obtype &operator*() const | ||
76 | { | ||
77 | checkPtr(); | ||
65 | return *pData; | 78 | return *pData; |
66 | } | 79 | } |
67 | 80 | ||
68 | obtype *operator->() | 81 | obtype *operator->() |
69 | { | 82 | { |
83 | checkPtr(); | ||
84 | return pData; | ||
85 | } | ||
86 | |||
87 | const obtype *operator->() const | ||
88 | { | ||
89 | checkPtr(); | ||
70 | return pData; | 90 | return pData; |
71 | } | 91 | } |
72 | 92 | ||
73 | const keytype &getKey() | 93 | bool isLoaded() const |
94 | { | ||
95 | return pData != NULL; | ||
96 | } | ||
97 | |||
98 | const keytype &getKey() const | ||
74 | { | 99 | { |
75 | return kId; | 100 | return kId; |
76 | } | 101 | } |
77 | 102 | ||
78 | Ptr &operator=( const Ptr &rRhs ) | 103 | Ptr &operator=( const Ptr &rRhs ) |
79 | { | 104 | { |
80 | if( pCache ) | 105 | if( pCache && pData ) |
81 | pCache->decRef( kId ); | 106 | pCache->decRef( kId ); |
82 | pCache = rRhs.pCache; | 107 | pCache = rRhs.pCache; |
83 | pData = rRhs.pData; | 108 | pData = rRhs.pData; |
84 | kId = rRhs.kId; | 109 | kId = rRhs.kId; |
85 | if( pCache ) | 110 | if( pCache && pData ) |
86 | pCache->incRef( kId ); | 111 | pCache->incRef( kId ); |
87 | } | 112 | } |
88 | 113 | ||
89 | private: | 114 | private: |
115 | void checkPtr() const | ||
116 | { | ||
117 | if( pCache && !pData ) | ||
118 | { | ||
119 | pData = pCache->getRaw( kId ); | ||
120 | pCache->incRef( kId ); | ||
121 | } | ||
122 | } | ||
123 | |||
124 | private: | ||
90 | Bu::Cache<obtype, keytype> *pCache; | 125 | Bu::Cache<obtype, keytype> *pCache; |
91 | obtype *pData; | 126 | mutable obtype *pData; |
92 | keytype kId; | 127 | mutable keytype kId; |
93 | }; | 128 | }; |
94 | 129 | ||
95 | private: | 130 | private: |
@@ -165,6 +200,12 @@ namespace Bu | |||
165 | } | 200 | } |
166 | } | 201 | } |
167 | 202 | ||
203 | Ptr getLazy( const keytype &cId ) | ||
204 | { | ||
205 | TRACE( cId ); | ||
206 | return Ptr( this, cId ); | ||
207 | } | ||
208 | |||
168 | int getRefCount( const keytype &cId ) | 209 | int getRefCount( const keytype &cId ) |
169 | { | 210 | { |
170 | TRACE( cId ); | 211 | TRACE( cId ); |
@@ -220,19 +261,33 @@ namespace Bu | |||
220 | } | 261 | } |
221 | 262 | ||
222 | private: | 263 | private: |
223 | void incRef( keytype cId ) | 264 | void incRef( const keytype &cId ) |
224 | { | 265 | { |
225 | TRACE( cId ); | 266 | TRACE( cId ); |
226 | hEnt.get( cId ).iRefs++; | 267 | hEnt.get( cId ).iRefs++; |
227 | } | 268 | } |
228 | 269 | ||
229 | void decRef( keytype cId ) | 270 | void decRef( const keytype &cId ) |
230 | { | 271 | { |
231 | TRACE( cId ); | 272 | TRACE( cId ); |
232 | CacheEntry &e = hEnt.get( cId ); | 273 | CacheEntry &e = hEnt.get( cId ); |
233 | e.iRefs--; | 274 | e.iRefs--; |
234 | } | 275 | } |
235 | 276 | ||
277 | obtype *getRaw( const keytype &cId ) | ||
278 | { | ||
279 | TRACE( cId ); | ||
280 | try { | ||
281 | return hEnt.get( cId ).pData; | ||
282 | } | ||
283 | catch( Bu::HashException &e ) { | ||
284 | CacheEntry e = {pStore->load( cId ), 0}; | ||
285 | pCalc->onLoad( e.pData, cId ); | ||
286 | hEnt.insert( cId, e ); | ||
287 | return e.pData; | ||
288 | } | ||
289 | } | ||
290 | |||
236 | private: | 291 | private: |
237 | CidHash hEnt; | 292 | CidHash hEnt; |
238 | Calc *pCalc; | 293 | Calc *pCalc; |