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/stable/stdstream.cpp | 4 ++-- src/tests/cache.cpp | 6 +++--- src/unstable/cachebase.h | 28 +++++++++++++++++++++------- src/unstable/cacheobject.h | 34 +++++++++++++++++++++++++++++++--- src/unstable/myriadcache.h | 8 ++++++-- 5 files changed, 63 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/stable/stdstream.cpp b/src/stable/stdstream.cpp index a622559..5bcc643 100644 --- a/src/stable/stdstream.cpp +++ b/src/stable/stdstream.cpp @@ -50,12 +50,12 @@ void Bu::StdStream::setPosEnd( Bu::size ) bool Bu::StdStream::isEos() { - return false; + return feof( stdin ) != 0; } bool Bu::StdStream::isOpen() { - return true; + return feof( stdin ) != 0; } void Bu::StdStream::flush() diff --git a/src/tests/cache.cpp b/src/tests/cache.cpp index 7982b2d..4751559 100644 --- a/src/tests/cache.cpp +++ b/src/tests/cache.cpp @@ -145,7 +145,7 @@ namespace Bu } template<> - Something *_cacheObjectLoad( Bu::Stream &s ) + Something *_cacheObjectLoad( Bu::CacheObject::Initializer &initObj, const Bu::Uuid &rKey, Bu::Stream &s ) { Bu::Archive ar( s, Bu::Archive::load ); uint8_t uType; @@ -154,7 +154,7 @@ namespace Bu { case 1: { - SubSomethingA *ret = new SubSomethingA(); + SubSomethingA *ret = initObj(new SubSomethingA()); ar >> *ret; return ret; } @@ -162,7 +162,7 @@ namespace Bu case 2: { - SubSomethingB *ret = new SubSomethingB(); + SubSomethingB *ret = initObj(new SubSomethingB()); ar >> *ret; return ret; } 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