From 180bee907a63817196b9e3ec8472603c8b5b8756 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Sat, 23 Feb 2013 06:27:52 +0000 Subject: Just made all tests include libpthread, it makes sense these days. Creating an experimental new cache system that's controlled primarily with a subclass. This should cut down significantly on the amount of code needed to use the cache. The new one also features a required (for now) base class for objects that use the cache. This simplifies so many different things. --- src/tests/cache.cpp | 204 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 204 insertions(+) create mode 100644 src/tests/cache.cpp (limited to 'src/tests') diff --git a/src/tests/cache.cpp b/src/tests/cache.cpp new file mode 100644 index 0000000..911b829 --- /dev/null +++ b/src/tests/cache.cpp @@ -0,0 +1,204 @@ +#include +#include +#include +#include +#include +#include +#include + +// +// New Cache Implementation +// + +template class CacheBase; + +template +class CacheObject +{ +friend class CacheBase; +public: + CacheObject() : + pCache( NULL ), + bChanged( false ) + { + } + + virtual ~CacheObject() + { + } + + typedef CacheBase CacheType; + + virtual const keytype getKey() const=0; + + void changed( bool bChanged=true ) + { + if( this->bChanged == false && bChanged == true ) + pCache->objectChanged( getKey() ); + + this->bChanged = bChanged; + } + +private: + void setCache( CacheType *pCache ) + { + this->pCache = pCache; + } + +private: + CacheType *pCache; + bool bChanged; +}; + +template +class CachePtr +{ +friend class CacheBase; +public: + CachePtr() + { + } + + virtual ~CachePtr() + { + } +}; + +template +class CacheBase +{ +public: + CacheBase() + { + } + + virtual ~CacheBase() + { + } + +protected: + virtual void _create( obtype *o )=0; + virtual void _erase( const keytype &k )=0; + + virtual obtype *_load( const keytype &k )=0; + virtual void _save( obtype *o )=0; +}; + +template +class MyriadCache : public CacheBase +{ +public: + MyriadCache( Bu::Stream &sStore, int iBlockSize=512, int iPreallocate=8 ) : + sStore( sStore ), + mStore( sStore, iBlockSize, iPreallocate ) + { + try + { + Bu::MyriadStream ms = mStore.openStream( 1 ); + Bu::Archive ar( ms, Bu::Archive::load ); + ar >> hIndex; + } + catch(...) + { + if( mStore.createStreamWithId( 1 ) != 1 ) + throw Bu::ExceptionBase("Error creating index stream."); + + Bu::MyriadStream ms = mStore.openStream( 1 ); + Bu::Archive ar( ms, Bu::Archive::save ); + ar << hIndex; + } + } + + virtual ~MyriadCache() + { + } + +protected: + virtual void _create( obtype *o ) + { + hIndex.insert( o->getKey(), mStore.createStream() ); + _save( o ); + } + + virtual void _erase( const keytype &k ) + { + mStore.deleteStream( hIndex.get( k ) ); + hIndex.erase( k ); + } + + virtual obtype *_load( const keytype &k ) + { + Bu::MyriadStream ms = mStore.openStream( hIndex.get( k ) ); + Bu::Archive ar( ms, Bu::Archive::load ); + obtype *ret = new obtype(); + ar >> *ret; + return ret; + } + + virtual void _save( obtype *o ) + { + Bu::MyriadStream ms = mStore.openStream( hIndex.get( o->getKey() ) ); + { + Bu::Archive ar( ms, Bu::Archive::save ); + ar << *o; + } + ms.setSize( ms.tell() ); + } + +private: + Bu::Stream &sStore; + Bu::Myriad mStore; + Bu::Hash hIndex; +}; + +// +// Test +// + +class Something : public CacheObject +{ +friend Bu::ArchiveBase &operator>>( Bu::ArchiveBase &ar, Something &s ); +friend Bu::ArchiveBase &operator<<( Bu::ArchiveBase &ar, const Something &s ); +public: + Something() + { + } + + Something( const Bu::String &sName ) : + uId( Bu::Uuid::generate() ), + sName( sName ) + { + } + + virtual ~Something() + { + } + + virtual const Bu::Uuid getKey() const + { + return uId; + } + +private: + Bu::Uuid uId; + Bu::String sName; +}; + +Bu::ArchiveBase &operator>>( Bu::ArchiveBase &ar, Something &s ) +{ + return ar >> s.uId >> s.sName; +} + +Bu::ArchiveBase &operator<<( Bu::ArchiveBase &ar, const Something &s ) +{ + return ar << s.uId << s.sName; +} + +int main() +{ + Bu::File fStore("test.myr", Bu::File::Create|Bu::File::ReadWrite); + MyriadCache c( fStore ); + + return 0; +} + -- cgit v1.2.3