diff options
Diffstat (limited to 'src/unstable')
-rw-r--r-- | src/unstable/cachebase.cpp | 8 | ||||
-rw-r--r-- | src/unstable/cachebase.h | 514 | ||||
-rw-r--r-- | src/unstable/cacheobject.cpp | 8 | ||||
-rw-r--r-- | src/unstable/cacheobject.h | 84 | ||||
-rw-r--r-- | src/unstable/myriadcache.cpp | 8 | ||||
-rw-r--r-- | src/unstable/myriadcache.h | 126 |
6 files changed, 748 insertions, 0 deletions
diff --git a/src/unstable/cachebase.cpp b/src/unstable/cachebase.cpp new file mode 100644 index 0000000..69b378e --- /dev/null +++ b/src/unstable/cachebase.cpp | |||
@@ -0,0 +1,8 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2013 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #include "bu/cachebase.h" | ||
diff --git a/src/unstable/cachebase.h b/src/unstable/cachebase.h new file mode 100644 index 0000000..807adf0 --- /dev/null +++ b/src/unstable/cachebase.h | |||
@@ -0,0 +1,514 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2013 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #ifndef BU_CACHE_BASE_H | ||
9 | #define BU_CACHE_BASE_H | ||
10 | |||
11 | #include "bu/string.h" | ||
12 | #include "bu/archive.h" | ||
13 | #include "bu/hash.h" | ||
14 | #include "bu/readwritemutex.h" | ||
15 | #include "bu/mutexlocker.h" | ||
16 | #include "bu/cacheobject.h" | ||
17 | |||
18 | namespace Bu | ||
19 | { | ||
20 | template<typename keytype, typename obtype> class CacheBase; | ||
21 | template<typename keytype, typename obtype> class CacheEntry; | ||
22 | |||
23 | template<typename keytype, typename obtype> | ||
24 | class CacheEntry | ||
25 | { | ||
26 | friend class CacheBase<keytype, obtype>; | ||
27 | private: | ||
28 | CacheEntry( obtype *pObject ) : | ||
29 | iRefCount( 0 ), | ||
30 | pObject( pObject ) | ||
31 | { | ||
32 | } | ||
33 | |||
34 | public: | ||
35 | int getRefCount() const | ||
36 | { | ||
37 | mEntry.lock(); | ||
38 | int ret = iRefCount; | ||
39 | mEntry.unlock(); | ||
40 | return ret; | ||
41 | } | ||
42 | |||
43 | obtype *getPtr() const | ||
44 | { | ||
45 | return pObject; | ||
46 | } | ||
47 | |||
48 | void lock() const | ||
49 | { | ||
50 | mObject.lock(); | ||
51 | } | ||
52 | |||
53 | void unlock() const | ||
54 | { | ||
55 | mObject.unlock(); | ||
56 | } | ||
57 | |||
58 | Bu::Mutex &getMutex() | ||
59 | { | ||
60 | return mObject; | ||
61 | } | ||
62 | |||
63 | void incRef() | ||
64 | { | ||
65 | mEntry.lock(); | ||
66 | iRefCount++; | ||
67 | mEntry.unlock(); | ||
68 | } | ||
69 | |||
70 | bool decRef() | ||
71 | { | ||
72 | mEntry.lock(); | ||
73 | iRefCount--; | ||
74 | bool bRet = iRefCount > 0; | ||
75 | mEntry.unlock(); | ||
76 | return bRet; | ||
77 | } | ||
78 | |||
79 | private: | ||
80 | mutable Bu::Mutex mEntry; | ||
81 | mutable Bu::Mutex mObject; | ||
82 | int iRefCount; | ||
83 | obtype *pObject; | ||
84 | }; | ||
85 | |||
86 | template<typename keytype, typename basetype> | ||
87 | class CachePtrInterface | ||
88 | { | ||
89 | protected: | ||
90 | CachePtrInterface() | ||
91 | { | ||
92 | } | ||
93 | |||
94 | virtual ~CachePtrInterface() | ||
95 | { | ||
96 | } | ||
97 | |||
98 | template<typename obtype> | ||
99 | void checkRef( CacheBase<keytype, basetype> *pCache, | ||
100 | const keytype &kId, | ||
101 | CacheEntry<keytype, basetype> * &rpEnt, | ||
102 | obtype * &rpData ) | ||
103 | { | ||
104 | if( pCache == NULL ) | ||
105 | throw Bu::ExceptionBase("Invalid pointer"); | ||
106 | |||
107 | if( !rpData ) | ||
108 | { | ||
109 | rpEnt = pCache->getRef( kId ); | ||
110 | rpEnt->incRef(); | ||
111 | rpData = dynamic_cast<obtype *>(rpEnt->getPtr()); | ||
112 | if( rpData == NULL ) | ||
113 | { | ||
114 | rpEnt->decRef(); | ||
115 | rpEnt = NULL; | ||
116 | throw std::bad_cast(); | ||
117 | } | ||
118 | } | ||
119 | } | ||
120 | |||
121 | template<typename obtype> | ||
122 | void releaseRef( CacheBase<keytype, basetype> *pCache, | ||
123 | CacheEntry<keytype, basetype> * &rpEnt, | ||
124 | obtype * &rpData ) | ||
125 | { | ||
126 | if( pCache == NULL ) | ||
127 | return; | ||
128 | |||
129 | if( rpData == NULL ) | ||
130 | return; | ||
131 | |||
132 | rpData = NULL; | ||
133 | rpEnt->decRef(); | ||
134 | pCache->releaseRef( rpEnt ); | ||
135 | rpEnt = NULL; | ||
136 | } | ||
137 | }; | ||
138 | |||
139 | template<typename keytype, typename obtype, typename basetype=obtype> | ||
140 | class CachePtr : protected CachePtrInterface<keytype, basetype> | ||
141 | { | ||
142 | friend class CacheBase<keytype, basetype>; | ||
143 | private: | ||
144 | typedef CachePtr<keytype, obtype, basetype> MyType; | ||
145 | |||
146 | CachePtr( CacheBase<keytype, basetype> *pCache, | ||
147 | CacheEntry<keytype, basetype> *pEnt, | ||
148 | const keytype &kId ) : | ||
149 | pCache( pCache ), | ||
150 | kId( kId ), | ||
151 | pEnt( pEnt ), | ||
152 | pData( NULL ) | ||
153 | { | ||
154 | pEnt->incRef(); | ||
155 | pData = dynamic_cast<obtype *>( pEnt->getPtr() ); | ||
156 | if( pData == NULL ) | ||
157 | { | ||
158 | pEnt->decRef(); | ||
159 | throw std::bad_cast(); | ||
160 | } | ||
161 | } | ||
162 | |||
163 | CachePtr( CacheBase<keytype, basetype> *pCache, const keytype &kId ) : | ||
164 | pCache( pCache ), | ||
165 | kId( kId ), | ||
166 | pEnt( NULL ), | ||
167 | pData( NULL ) | ||
168 | { | ||
169 | } | ||
170 | |||
171 | public: | ||
172 | CachePtr() : | ||
173 | pCache( NULL ), | ||
174 | pEnt( NULL ), | ||
175 | pData( NULL ) | ||
176 | { | ||
177 | } | ||
178 | |||
179 | CachePtr( const MyType &rhs ) : | ||
180 | pCache( rhs.pCache ), | ||
181 | kId( rhs.kId ), | ||
182 | pEnt( rhs.pEnt ), | ||
183 | pData( rhs.pData ) | ||
184 | { | ||
185 | pEnt->incRef(); | ||
186 | } | ||
187 | |||
188 | virtual ~CachePtr() | ||
189 | { | ||
190 | unbind(); | ||
191 | } | ||
192 | |||
193 | const keytype &getKey() const | ||
194 | { | ||
195 | return kId; | ||
196 | } | ||
197 | |||
198 | obtype &operator*() | ||
199 | { | ||
200 | bind(); | ||
201 | return pData; | ||
202 | } | ||
203 | |||
204 | const obtype &operator*() const | ||
205 | { | ||
206 | bind(); | ||
207 | return pData; | ||
208 | } | ||
209 | |||
210 | obtype *operator->() | ||
211 | { | ||
212 | bind(); | ||
213 | return pData; | ||
214 | } | ||
215 | |||
216 | const obtype *operator->() const | ||
217 | { | ||
218 | bind(); | ||
219 | return pData; | ||
220 | } | ||
221 | |||
222 | MyType operator=( const MyType &rhs ) | ||
223 | { | ||
224 | unbind(); | ||
225 | pCache = rhs.pCache; | ||
226 | kId = rhs.kId; | ||
227 | pEnt = rhs.pEnt; | ||
228 | pData = rhs.pData; | ||
229 | pEnt->incRef(); | ||
230 | |||
231 | return *this; | ||
232 | } | ||
233 | |||
234 | template<typename castto> | ||
235 | CachePtr<keytype, castto, basetype> cast() | ||
236 | { | ||
237 | return pCache->cast<obtype, castto>( *this ); | ||
238 | } | ||
239 | |||
240 | bool operator==( const MyType &rhs ) const | ||
241 | { | ||
242 | return pCache == rhs.pCache && | ||
243 | kId == rhs.kId; | ||
244 | } | ||
245 | |||
246 | bool operator!=( const MyType &rhs ) const | ||
247 | { | ||
248 | return pCache != rhs.pCache || | ||
249 | kId != rhs.kId; | ||
250 | } | ||
251 | |||
252 | void bind() | ||
253 | { | ||
254 | CachePtrInterface<keytype, basetype>::checkRef( pCache, kId, pEnt, pData ); | ||
255 | } | ||
256 | |||
257 | void unbind() | ||
258 | { | ||
259 | CachePtrInterface<keytype, basetype>::releaseRef( pCache, pEnt, pData ); | ||
260 | } | ||
261 | |||
262 | void lock() | ||
263 | { | ||
264 | bind(); | ||
265 | if( pEnt ) | ||
266 | pEnt->lock(); | ||
267 | } | ||
268 | |||
269 | void unlock() | ||
270 | { | ||
271 | bind(); | ||
272 | if( pEnt ) | ||
273 | pEnt->unlock(); | ||
274 | } | ||
275 | |||
276 | class Locker | ||
277 | { | ||
278 | public: | ||
279 | Locker( MyType &rPtr ) : | ||
280 | rPtr( rPtr ), | ||
281 | bLocked( true ) | ||
282 | { | ||
283 | rPtr.lock(); | ||
284 | } | ||
285 | |||
286 | ~Locker() | ||
287 | { | ||
288 | unlock(); | ||
289 | } | ||
290 | |||
291 | void unlock() | ||
292 | { | ||
293 | if( !bLocked ) | ||
294 | return; | ||
295 | rPtr.unlock(); | ||
296 | bLocked = false; | ||
297 | } | ||
298 | |||
299 | private: | ||
300 | MyType &rPtr; | ||
301 | bool bLocked; | ||
302 | }; | ||
303 | |||
304 | private: | ||
305 | CacheBase<keytype, basetype> *pCache; | ||
306 | mutable keytype kId; | ||
307 | mutable CacheEntry<keytype, basetype> *pEnt; | ||
308 | mutable obtype *pData; | ||
309 | }; | ||
310 | |||
311 | template<typename keytype, typename obtype> | ||
312 | class CacheBase | ||
313 | { | ||
314 | friend class CachePtrInterface<keytype, obtype>; | ||
315 | friend class CacheObject<keytype, obtype>; | ||
316 | public: | ||
317 | CacheBase() | ||
318 | { | ||
319 | } | ||
320 | |||
321 | virtual ~CacheBase() | ||
322 | { | ||
323 | Bu::ReadWriteMutex::WriteLocker wl( mCacheEntry ); | ||
324 | syncChanges(); | ||
325 | } | ||
326 | |||
327 | typedef CacheEntry<keytype, obtype> Entry; | ||
328 | typedef Bu::List<keytype> KeyList; | ||
329 | |||
330 | CachePtr<keytype, obtype> insert( obtype *pObject ) | ||
331 | { | ||
332 | Entry *pEnt = addEntry( pObject ); | ||
333 | |||
334 | return CachePtr<keytype, obtype>( | ||
335 | this, pEnt, pObject->getKey() | ||
336 | ); | ||
337 | } | ||
338 | |||
339 | template<typename supertype> | ||
340 | CachePtr<keytype, supertype, obtype> insert( supertype *pObject ) | ||
341 | { | ||
342 | obtype *pCast = dynamic_cast<obtype *>( pObject ); | ||
343 | if( pCast == NULL ) | ||
344 | throw std::bad_cast(); | ||
345 | |||
346 | Entry *pEnt = addEntry( pCast ); | ||
347 | |||
348 | return CachePtr<keytype, supertype, obtype>( | ||
349 | this, pEnt, pObject->getKey() | ||
350 | ); | ||
351 | } | ||
352 | |||
353 | CachePtr<keytype, obtype> get( const keytype &key ) | ||
354 | { | ||
355 | Entry *pEnt = getEntry( key ); | ||
356 | return CachePtr<keytype, obtype>( this, pEnt, key ); | ||
357 | } | ||
358 | |||
359 | template<typename supertype> | ||
360 | CachePtr<keytype, supertype, obtype> get( const keytype &key ) | ||
361 | { | ||
362 | Entry *pEnt = getEntry( key ); | ||
363 | return CachePtr<keytype, supertype, obtype>( this, pEnt, key ); | ||
364 | } | ||
365 | |||
366 | CachePtr<keytype, obtype> getLazy( const keytype &key ) | ||
367 | { | ||
368 | return CachePtr<keytype, obtype>( this, key ); | ||
369 | } | ||
370 | |||
371 | template<typename supertype> | ||
372 | CachePtr<keytype, supertype, obtype> getLazy( const keytype &key ) | ||
373 | { | ||
374 | return CachePtr<keytype, supertype, obtype>( this, key ); | ||
375 | } | ||
376 | |||
377 | template<typename supertype, typename castto> | ||
378 | CachePtr<keytype, castto, obtype> cast( CachePtr<keytype, supertype, obtype> &ptr ) | ||
379 | { | ||
380 | if( ptr.pEnt ) | ||
381 | return CachePtr<keytype, castto, obtype>( this, ptr.pEnt, ptr.kId ); | ||
382 | else | ||
383 | return CachePtr<keytype, castto, obtype>( this, ptr.kId ); | ||
384 | } | ||
385 | |||
386 | void erase( const keytype &key ) | ||
387 | { | ||
388 | Bu::ReadWriteMutex::WriteLocker wl( mCacheEntry ); | ||
389 | if( hCacheEntry.has( key ) ) | ||
390 | { | ||
391 | Entry *pEnt = hCacheEntry.get( key ); | ||
392 | pEnt->mEntry.lock(); | ||
393 | if( pEnt->iRefCount > 0 ) | ||
394 | { | ||
395 | int iCount = pEnt->iRefCount; | ||
396 | pEnt->mEntry.unlock(); | ||
397 | throw Bu::ExceptionBase( Bu::String("Cache entry %1 cannot be erased, there are %2 active references.").arg( key ).arg( iCount ).end().getStr() ); | ||
398 | } | ||
399 | delete pEnt->pObject; | ||
400 | delete pEnt; | ||
401 | hCacheEntry.erase( key ); | ||
402 | } | ||
403 | _erase( key ); | ||
404 | } | ||
405 | |||
406 | virtual KeyList getKeys() const=0; | ||
407 | virtual int getSize() const=0; | ||
408 | |||
409 | void sync() | ||
410 | { | ||
411 | Bu::ReadWriteMutex::WriteLocker wl( mCacheEntry ); | ||
412 | _sync(); | ||
413 | syncChanges(); | ||
414 | } | ||
415 | |||
416 | protected: | ||
417 | Entry *getRef( const keytype &k ) | ||
418 | { | ||
419 | Entry *pEnt = getEntry( k ); | ||
420 | return pEnt; | ||
421 | } | ||
422 | |||
423 | void releaseRef( Entry * )//pEnt ) | ||
424 | { | ||
425 | } | ||
426 | |||
427 | void objectChanged( const keytype &k ) | ||
428 | { | ||
429 | Bu::ReadWriteMutex::WriteLocker wl( mCacheEntry ); | ||
430 | hChanged.insert( k, true ); | ||
431 | } | ||
432 | |||
433 | protected: | ||
434 | virtual void _create( const obtype *o )=0; | ||
435 | virtual void _erase( const keytype &k )=0; | ||
436 | |||
437 | virtual obtype *_load( const keytype &k )=0; | ||
438 | virtual void _save( const obtype *o )=0; | ||
439 | |||
440 | virtual void _sync()=0; | ||
441 | |||
442 | private: | ||
443 | Entry *addEntry( obtype *pObject ) | ||
444 | { | ||
445 | Entry *pEnt = new Entry( pObject ); | ||
446 | mCacheEntry.lockWrite(); | ||
447 | hCacheEntry.insert( pObject->getKey(), pEnt ); | ||
448 | mCacheEntry.unlockWrite(); | ||
449 | _create( pObject ); | ||
450 | pObject->setCache( this, pEnt ); | ||
451 | |||
452 | return pEnt; | ||
453 | } | ||
454 | |||
455 | Entry *getEntry( const keytype &k ) | ||
456 | { | ||
457 | Entry *pEnt = NULL; | ||
458 | try | ||
459 | { | ||
460 | Bu::ReadWriteMutex::ReadLocker rl( mCacheEntry ); | ||
461 | pEnt = hCacheEntry.get( k ); | ||
462 | } | ||
463 | catch(...) | ||
464 | { | ||
465 | // try to load the object from the backing store | ||
466 | obtype *pObject = _load( k ); | ||
467 | pEnt = new Entry( pObject ); | ||
468 | pObject->setCache( this, pEnt ); | ||
469 | Bu::ReadWriteMutex::WriteLocker wl( mCacheEntry ); | ||
470 | hCacheEntry.insert( k, pEnt ); | ||
471 | } | ||
472 | return pEnt; | ||
473 | } | ||
474 | |||
475 | void syncChanges() | ||
476 | { | ||
477 | if( !hChanged.isEmpty() ) | ||
478 | { | ||
479 | for( typename CacheKeySet::iterator i = hChanged.begin(); i; i++ ) | ||
480 | { | ||
481 | Entry *pEnt = hCacheEntry.get( i.getKey() ); | ||
482 | Bu::MutexLocker ml( pEnt->getMutex() ); | ||
483 | _save( pEnt->getPtr() ); | ||
484 | } | ||
485 | hChanged.clear(); | ||
486 | } | ||
487 | } | ||
488 | |||
489 | private: | ||
490 | typedef Bu::Hash<keytype, Entry *> CacheEntryHash; | ||
491 | typedef Bu::Hash<keytype, bool> CacheKeySet; | ||
492 | CacheEntryHash hCacheEntry; | ||
493 | CacheKeySet hChanged; | ||
494 | Bu::ReadWriteMutex mCacheEntry; | ||
495 | }; | ||
496 | |||
497 | template<typename obtype> | ||
498 | void _cacheObjectSave( Bu::Stream &s, obtype *pObject ) | ||
499 | { | ||
500 | Bu::Archive ar( s, Bu::Archive::save ); | ||
501 | ar << *pObject; | ||
502 | } | ||
503 | |||
504 | template<typename obtype> | ||
505 | obtype *_cacheObjectLoad( Bu::Stream &s ) | ||
506 | { | ||
507 | Bu::Archive ar( s, Bu::Archive::load ); | ||
508 | obtype *ret = new obtype(); | ||
509 | ar >> *ret; | ||
510 | return ret; | ||
511 | } | ||
512 | } | ||
513 | |||
514 | #endif | ||
diff --git a/src/unstable/cacheobject.cpp b/src/unstable/cacheobject.cpp new file mode 100644 index 0000000..eedb645 --- /dev/null +++ b/src/unstable/cacheobject.cpp | |||
@@ -0,0 +1,8 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2013 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #include "bu/cacheobject.h" | ||
diff --git a/src/unstable/cacheobject.h b/src/unstable/cacheobject.h new file mode 100644 index 0000000..0088685 --- /dev/null +++ b/src/unstable/cacheobject.h | |||
@@ -0,0 +1,84 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2013 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #ifndef BU_CACHE_OBJECT_H | ||
9 | #define BU_CACHE_OBJECT_H | ||
10 | |||
11 | #ifndef NULL | ||
12 | #define NULL 0 | ||
13 | #endif | ||
14 | |||
15 | namespace Bu | ||
16 | { | ||
17 | template<typename keytype, typename obtype> class CacheBase; | ||
18 | template<typename keytype, typename obtype> class CacheEntry; | ||
19 | |||
20 | template<typename keytype, typename obtype> | ||
21 | class CacheObject | ||
22 | { | ||
23 | friend class CacheBase<keytype, obtype>; | ||
24 | public: | ||
25 | CacheObject() : | ||
26 | pCache( NULL ), | ||
27 | bChanged( false ) | ||
28 | { | ||
29 | } | ||
30 | |||
31 | virtual ~CacheObject() | ||
32 | { | ||
33 | } | ||
34 | |||
35 | typedef CacheBase<keytype, obtype> CacheType; | ||
36 | |||
37 | virtual keytype getKey() const=0; | ||
38 | virtual int getPersistenceScore() const { return 0; } | ||
39 | |||
40 | void lock() | ||
41 | { | ||
42 | pEntry->lock(); | ||
43 | } | ||
44 | |||
45 | void unlock() | ||
46 | { | ||
47 | pEntry->unlock(); | ||
48 | } | ||
49 | |||
50 | int getRefCount() const | ||
51 | { | ||
52 | return pEntry->getRefCount(); | ||
53 | } | ||
54 | |||
55 | bool hasChanged() const | ||
56 | { | ||
57 | return bChanged; | ||
58 | } | ||
59 | |||
60 | protected: | ||
61 | void changed( bool bChanged=true ) | ||
62 | { | ||
63 | if( this->bChanged == false && bChanged == true ) | ||
64 | pCache->objectChanged( getKey() ); | ||
65 | |||
66 | this->bChanged = bChanged; | ||
67 | } | ||
68 | |||
69 | private: | ||
70 | typedef CacheEntry<keytype, obtype> Entry; | ||
71 | void setCache( CacheType *pCache, Entry *pEntry ) | ||
72 | { | ||
73 | this->pCache = pCache; | ||
74 | this->pEntry = pEntry; | ||
75 | } | ||
76 | |||
77 | private: | ||
78 | CacheType *pCache; | ||
79 | Entry *pEntry; | ||
80 | bool bChanged; | ||
81 | }; | ||
82 | } | ||
83 | |||
84 | #endif | ||
diff --git a/src/unstable/myriadcache.cpp b/src/unstable/myriadcache.cpp new file mode 100644 index 0000000..0a997f8 --- /dev/null +++ b/src/unstable/myriadcache.cpp | |||
@@ -0,0 +1,8 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2013 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #include "bu/myriadcache.h" | ||
diff --git a/src/unstable/myriadcache.h b/src/unstable/myriadcache.h new file mode 100644 index 0000000..f8c0e00 --- /dev/null +++ b/src/unstable/myriadcache.h | |||
@@ -0,0 +1,126 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2013 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #ifndef BU_MYRIAD_CACHE_H | ||
9 | #define BU_MYRIAD_CACHE_H | ||
10 | |||
11 | #include "bu/cachebase.h" | ||
12 | #include "bu/myriad.h" | ||
13 | #include "bu/myriadstream.h" | ||
14 | #include "bu/file.h" | ||
15 | #include "bu/streamstack.h" | ||
16 | |||
17 | namespace Bu | ||
18 | { | ||
19 | template<typename keytype, typename obtype> | ||
20 | class MyriadCache : public Bu::CacheBase<keytype, obtype> | ||
21 | { | ||
22 | public: | ||
23 | MyriadCache( Bu::Stream &sStore, int iBlockSize=512, int iPreallocate=8 ) : | ||
24 | sStore( sStore ), | ||
25 | mStore( sStore, iBlockSize, iPreallocate ), | ||
26 | bStructureChanged( false ) | ||
27 | { | ||
28 | try | ||
29 | { | ||
30 | Bu::ReadWriteMutex::ReadLocker l( rwStore ); | ||
31 | Bu::MyriadStream ms = mStore.openStream( 1 ); | ||
32 | Bu::Archive ar( ms, Bu::Archive::load ); | ||
33 | uint8_t uVer; | ||
34 | ar >> uVer; | ||
35 | switch( uVer ) | ||
36 | { | ||
37 | case 0: | ||
38 | ar >> hIndex; | ||
39 | break; | ||
40 | } | ||
41 | } | ||
42 | catch(...) | ||
43 | { | ||
44 | if( mStore.createStreamWithId( 1 ) != 1 ) | ||
45 | throw Bu::ExceptionBase("Error creating index stream."); | ||
46 | |||
47 | _sync(); | ||
48 | } | ||
49 | } | ||
50 | |||
51 | virtual ~MyriadCache() | ||
52 | { | ||
53 | _sync(); | ||
54 | } | ||
55 | |||
56 | using typename Bu::CacheBase<keytype,obtype>::KeyList; | ||
57 | |||
58 | virtual KeyList getKeys() const | ||
59 | { | ||
60 | Bu::ReadWriteMutex::ReadLocker rl( rwStore ); | ||
61 | return hIndex.getKeys(); | ||
62 | } | ||
63 | |||
64 | virtual int getSize() const | ||
65 | { | ||
66 | Bu::ReadWriteMutex::ReadLocker rl( rwStore ); | ||
67 | return hIndex.getSize(); | ||
68 | } | ||
69 | |||
70 | protected: | ||
71 | virtual void _create( const obtype *o ) | ||
72 | { | ||
73 | Bu::ReadWriteMutex::WriteLocker wl( rwStore ); | ||
74 | hIndex.insert( o->getKey(), mStore.createStream() ); | ||
75 | _save( o ); | ||
76 | |||
77 | bStructureChanged = true; | ||
78 | } | ||
79 | |||
80 | virtual void _erase( const keytype &k ) | ||
81 | { | ||
82 | Bu::ReadWriteMutex::WriteLocker wl( rwStore ); | ||
83 | mStore.deleteStream( hIndex.get( k ) ); | ||
84 | hIndex.erase( k ); | ||
85 | |||
86 | bStructureChanged = true; | ||
87 | } | ||
88 | |||
89 | virtual obtype *_load( const keytype &k ) | ||
90 | { | ||
91 | Bu::MyriadStream ms = mStore.openStream( hIndex.get( k ) ); | ||
92 | return _cacheObjectLoad<obtype>( ms ); | ||
93 | } | ||
94 | |||
95 | virtual void _save( const obtype *o ) | ||
96 | { | ||
97 | Bu::MyriadStream ms = mStore.openStream( hIndex.get( o->getKey() ) ); | ||
98 | _cacheObjectSave( ms, o ); | ||
99 | ms.setSize( ms.tell() ); | ||
100 | } | ||
101 | |||
102 | virtual void _sync() | ||
103 | { | ||
104 | Bu::ReadWriteMutex::ReadLocker wl( rwStore ); | ||
105 | if( !bStructureChanged ) | ||
106 | return; | ||
107 | |||
108 | Bu::MyriadStream ms = mStore.openStream( 1 ); | ||
109 | Bu::Archive ar( ms, Bu::Archive::save ); | ||
110 | ar << (uint8_t)0 << hIndex; | ||
111 | ar.close(); | ||
112 | ms.setSize( ms.tell() ); | ||
113 | |||
114 | bStructureChanged = false; | ||
115 | } | ||
116 | |||
117 | private: | ||
118 | Bu::Stream &sStore; | ||
119 | Bu::Myriad mStore; | ||
120 | Bu::Hash<keytype, int> hIndex; | ||
121 | mutable Bu::ReadWriteMutex rwStore; | ||
122 | bool bStructureChanged; | ||
123 | }; | ||
124 | } | ||
125 | |||
126 | #endif | ||