From 86e37bec7b2101555635201f83352c0e054f1849 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 22 Jan 2014 16:28:46 +0000 Subject: Updated the cache system. It now ensures that objects are initialized with cache information before they are deserialized from storage. This changed the signature of the cache loading template function, but the new function isn't harder to use, and provides the key information as well. --- src/unstable/cachebase.h | 28 +++++++++++++++++++++------- src/unstable/cacheobject.h | 34 +++++++++++++++++++++++++++++++--- src/unstable/myriadcache.h | 8 ++++++-- 3 files changed, 58 insertions(+), 12 deletions(-) (limited to 'src/unstable') diff --git a/src/unstable/cachebase.h b/src/unstable/cachebase.h index af2884c..bb543dd 100644 --- a/src/unstable/cachebase.h +++ b/src/unstable/cachebase.h @@ -328,6 +328,7 @@ namespace Bu typedef CacheEntry Entry; typedef Bu::List KeyList; + typedef CacheObject ObjectType; CachePtr insert( obtype *pObject ) { @@ -466,7 +467,10 @@ namespace Bu virtual void _create( const obtype *o )=0; virtual void _erase( const keytype &k )=0; - virtual obtype *_load( const keytype &k )=0; + virtual obtype *_load( + typename Bu::CacheObject::Initializer &initObj, + const keytype &k + )=0; virtual void _save( const obtype *o )=0; virtual void _sync()=0; @@ -481,7 +485,10 @@ namespace Bu hChanged.insert( pObject->getKey(), true ); mCacheEntry.unlockWrite(); _create( pObject ); - pObject->setCache( this, pEnt ); + // I'm not sold on the ordering here...or the fact we're not + // using th initilizer + pObject->setCache( this ); + pObject->setCacheEntry( pEnt ); return pEnt; } @@ -497,9 +504,12 @@ namespace Bu catch(...) { // try to load the object from the backing store - obtype *pObject = _load( k ); + typename ObjectType::Initializer initObj( + this + ); + obtype *pObject = _load( initObj, k ); pEnt = new Entry( pObject ); - pObject->setCache( this, pEnt ); + pObject->setCacheEntry( pEnt ); Bu::ReadWriteMutex::WriteLocker wl( mCacheEntry ); hCacheEntry.insert( k, pEnt ); } @@ -536,11 +546,15 @@ namespace Bu ar << *pObject; } - template - obtype *_cacheObjectLoad( Bu::Stream &s ) + template + obtype *_cacheObjectLoad( + typename Bu::CacheObject::Initializer &initObject, + const keytype & /*rKey*/, + Bu::Stream &s + ) { Bu::Archive ar( s, Bu::Archive::load ); - obtype *ret = new obtype(); + obtype *ret = initObject(new obtype()); ar >> *ret; return ret; } diff --git a/src/unstable/cacheobject.h b/src/unstable/cacheobject.h index e83f706..ca70bb4 100644 --- a/src/unstable/cacheobject.h +++ b/src/unstable/cacheobject.h @@ -21,6 +21,7 @@ namespace Bu class CacheObject { friend class CacheBase; + //friend class CacheObject::Initializer; public: CacheObject() : pCache( NULL ), @@ -35,6 +36,7 @@ namespace Bu typedef CacheObject MyType; typedef CacheBase CacheType; + typedef CacheEntry EntryType; virtual keytype getKey() const=0; virtual int getPersistenceScore() const { return 0; } @@ -94,18 +96,44 @@ namespace Bu { return pCache; } + + public: + class Initializer + { + friend class CacheBase; + private: + Initializer( CacheBase *pCache ) : + pCache( pCache ) + { + } + + public: + template + subtype *operator()( subtype *pNewObj ) + { + pNewObj->setCache( pCache ); + return pNewObj; + } + + private: + CacheBase *pCache; + }; + private: - typedef CacheEntry Entry; - void setCache( CacheType *pCache, Entry *pEntry ) + void setCache( CacheType *pCache ) { this->pCache = pCache; + } + + void setCacheEntry( EntryType *pEntry ) + { this->pEntry = pEntry; } private: CacheType *pCache; - Entry *pEntry; + EntryType *pEntry; bool bChanged; }; } diff --git a/src/unstable/myriadcache.h b/src/unstable/myriadcache.h index 9bc926f..d19395a 100644 --- a/src/unstable/myriadcache.h +++ b/src/unstable/myriadcache.h @@ -54,6 +54,7 @@ namespace Bu } using typename Bu::CacheBase::KeyList; + using typename Bu::CacheBase::ObjectType; virtual typename Bu::CacheBase::KeyList getKeys() const { @@ -92,10 +93,13 @@ namespace Bu bStructureChanged = true; } - virtual obtype *_load( const keytype &k ) + virtual obtype *_load( + typename Bu::CacheObject::Initializer &initObj, + const keytype &k + ) { Bu::MyriadStream ms = mStore.openStream( hIndex.get( k ) ); - return _cacheObjectLoad( ms ); + return _cacheObjectLoad( initObj, k, ms ); } virtual void _save( const obtype *o ) -- cgit v1.2.3