From 6f3b85c5af1855e1695885fb28220c34f6a0673f Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 1 Dec 2008 23:10:44 +0000 Subject: Wow, that's a lot of changes. You can use anything as a key now, as long as it can be hashed. And we're about to test actually loading and saving persistant cache items. Fun. --- src/cache.h | 57 ++++++++++++++++++++--------------------- src/cachehandler.cpp | 2 -- src/cachehandler.h | 34 ------------------------- src/cachehandlernids.cpp | 0 src/cachehandlernids.h | 0 src/cachestore.cpp | 2 ++ src/cachestore.h | 35 +++++++++++++++++++++++++ src/cachestorenids.cpp | 0 src/cachestorenids.h | 0 src/cptr.cpp | 1 + src/cptr.h | 18 ++++++------- src/tests/cache.cpp | 66 ++++++++++++++++++++++++++++++++---------------- 12 files changed, 118 insertions(+), 97 deletions(-) delete mode 100644 src/cachehandler.cpp delete mode 100644 src/cachehandler.h delete mode 100644 src/cachehandlernids.cpp delete mode 100644 src/cachehandlernids.h create mode 100644 src/cachestore.cpp create mode 100644 src/cachestore.h create mode 100644 src/cachestorenids.cpp create mode 100644 src/cachestorenids.h diff --git a/src/cache.h b/src/cache.h index f7b71ce..0c3994c 100644 --- a/src/cache.h +++ b/src/cache.h @@ -4,22 +4,30 @@ #include "bu/cptr.h" #include "bu/hash.h" #include "bu/list.h" -#include "bu/cachehandler.h" +#include "bu/cachestore.h" #define BU_TRACE #include "bu/trace.h" namespace Bu { - template + template class Cache { - friend class Bu::CPtr; - typedef Bu::CPtr Ptr; - typedef Bu::CacheHandler Handler; - typedef Bu::List HandlerList; + friend class Bu::CPtr; public: - typedef long cid_t; /**< Cache ID type. Unique cache entry ID. */ + typedef Bu::CPtr Ptr; + private: + typedef Bu::CacheStore Store; + typedef Bu::List StoreList; + + typedef struct CacheEntry + { + obtype *pData; + int iRefs; + } CacheEntry; + + typedef Bu::Hash CidHash; public: Cache() @@ -30,51 +38,51 @@ namespace Bu virtual ~Cache() { TRACE(); -/* for( HandlerList::iterator i = lHandler.begin(); - i != lHandler.end(); i++ ) + for( typename StoreList::iterator i = lStore.begin(); + i != lStore.end(); i++ ) { delete *i; } -*/ } + } - void appendHandler( Handler *pHand ) + void appendStore( Store *pHand ) { - lHandler.append( pHand ); + lStore.append( pHand ); } - void prependHandler( Handler *pHand ) + void prependStore( Store *pHand ) { - lHandler.prepend( pHand ); + lStore.prepend( pHand ); } Ptr insert( obtype *pData ) { TRACE(); CacheEntry e = {pData, 0}; - hEnt.insert( /*pData*/ 0 , e ); + hEnt.insert( 0 , e ); return Ptr( *this, pData ); } - Ptr get( cid_t cId ) + Ptr get( keytype cId ) { TRACE(); return Ptr( *this, hEnt.get( cId ).pData ); } - int getRefCnt( cid_t cId ) + int getRefCnt( keytype cId ) { TRACE(); return hEnt.get( cId ).iRefs; } private: - void incRef( cid_t cId ) + void incRef( keytype cId ) { TRACE(); hEnt.get( cId ).iRefs++; } - void decRef( cid_t cId ) + void decRef( keytype cId ) { TRACE(); CacheEntry &e = hEnt.get( cId ); @@ -82,17 +90,8 @@ namespace Bu } private: - typedef struct CacheEntry - { - obtype *pData; - int iRefs; - } CacheEntry; - - //typedef Bu::Hash RefHash; - typedef Bu::Hash CidHash; - //RefHash hRefs; CidHash hEnt; - HandlerList lHandler; + StoreList lStore; }; }; diff --git a/src/cachehandler.cpp b/src/cachehandler.cpp deleted file mode 100644 index d208e98..0000000 --- a/src/cachehandler.cpp +++ /dev/null @@ -1,2 +0,0 @@ -#include "bu/cachehandler.h" - diff --git a/src/cachehandler.h b/src/cachehandler.h deleted file mode 100644 index 3b26970..0000000 --- a/src/cachehandler.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef BU_CACHE_HANDLER_H -#define BU_CACHE_HANDLER_H - -#include "bu/cptr.h" - -namespace Bu -{ - /** - * Handles I/O for data in the cache. This also assigns ID's to the newly - * created objects that are requested through this system. - */ - template - class CacheHandler - { - public: - CacheHandler() - { - } - - virtual ~CacheHandler() - { - } - - virtual Bu::CPtr load()=0; - virtual void unload( Bu::CPtr pObj )=0; - virtual Bu::CPtr create()=0; - virtual Bu::CPtr create( T &rSrc )=0; - virtual void destroy( Bu::CPtr pObj )=0; - - private: - }; -}; - -#endif diff --git a/src/cachehandlernids.cpp b/src/cachehandlernids.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/src/cachehandlernids.h b/src/cachehandlernids.h deleted file mode 100644 index e69de29..0000000 diff --git a/src/cachestore.cpp b/src/cachestore.cpp new file mode 100644 index 0000000..7f26f17 --- /dev/null +++ b/src/cachestore.cpp @@ -0,0 +1,2 @@ +#include "bu/cachestore.h" + diff --git a/src/cachestore.h b/src/cachestore.h new file mode 100644 index 0000000..3211b6a --- /dev/null +++ b/src/cachestore.h @@ -0,0 +1,35 @@ +#ifndef BU_CACHE_STORE_H +#define BU_CACHE_STORE_H + +#include "bu/cptr.h" + +namespace Bu +{ + /** + * Handles I/O for data in the cache. This also assigns ID's to the newly + * created objects that are requested through this system. + */ + template + class CacheStore + { + public: + CacheStore() + { + } + + virtual ~CacheStore() + { + } + + typedef Bu::CPtr Ptr; + + virtual obtype *load( const keytype &key )=0; + virtual void unload( obtype *pObj )=0; + virtual keytype create( obtype *pSrc )=0; + virtual void destroy( obtype *pObj, const keytype &key )=0; + + private: + }; +}; + +#endif diff --git a/src/cachestorenids.cpp b/src/cachestorenids.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/cachestorenids.h b/src/cachestorenids.h new file mode 100644 index 0000000..e69de29 diff --git a/src/cptr.cpp b/src/cptr.cpp index e69de29..65d668b 100644 --- a/src/cptr.cpp +++ b/src/cptr.cpp @@ -0,0 +1 @@ +#include "bu/cptr.h" diff --git a/src/cptr.h b/src/cptr.h index 6e54fdd..8f35a03 100644 --- a/src/cptr.h +++ b/src/cptr.h @@ -3,31 +3,29 @@ namespace Bu { - template class Cache; + template class Cache; /** * Cache Pointer - Provides access to data that is held within the cache. * This provides safe, refcounting access to data stored in the cache, with * support for lazy loading. */ - template + template class CPtr { - friend class Bu::Cache; - public: - typedef long cid_t; /**< Cache ID type. Unique cache entry ID. */ + friend class Bu::Cache; private: - CPtr( Cache &rCache, obtype *pData ) : + CPtr( Cache &rCache, obtype *pData ) : rCache( rCache ), pData( pData ) { - rCache.incRef( cId ); + rCache.incRef( kId ); } public: virtual ~CPtr() { - rCache.decRef( cId ); + rCache.decRef( kId ); } obtype &operator*() @@ -41,9 +39,9 @@ namespace Bu } private: - Bu::Cache &rCache; + Bu::Cache &rCache; obtype *pData; - cid_t cId; + keytype kId; }; }; diff --git a/src/tests/cache.cpp b/src/tests/cache.cpp index 55ec5be..1cc008a 100644 --- a/src/tests/cache.cpp +++ b/src/tests/cache.cpp @@ -1,4 +1,7 @@ #include +#include +#include +#include #include "bu/cache.h" @@ -35,55 +38,74 @@ public: int iInt; }; -class BobHandler : public Bu::CacheHandler +class BobStore : public Bu::CacheStore { public: - BobHandler() : + BobStore() : cLastId( 0 ) { + TRACE(); } - ~BobHandler() - { - } - - virtual Bu::CPtr load() + ~BobStore() { + TRACE(); } - virtual void unload( Bu::CPtr pObj ) + virtual Bob *load( const long &key ) { + TRACE(); + return NULL; } - virtual Bu::CPtr create() + virtual void unload( Bob *pObj ) { + TRACE(); + delete pObj; } - virtual Bu::CPtr create( Bob &rSrc ) + virtual long create( Bob *rSrc ) { + TRACE(); + return ++cLastId; } - virtual void destroy( Bu::CPtr pObj ) + virtual void destroy( Bob *pObj, const long &key ) { + TRACE(); + delete pObj; } private: - Bu::Cache::cid_t cLastId; + long cLastId; }; -int main() +int main( int argc, char *argv[] ) { - typedef Bu::Cache BobCache; - typedef Bu::CPtr BobPtr; + TRACE(); + if( argc < 3 ) + { + printf("Try: %s [icufd] []\n\n", argv[0] ); + return 0; + } + + switch( argv[1][0] ) + { + case 'i': + mkdir("bobcache", 0755 ); + printf("Initialized cache: %s\n", strerror( errno ) ); + return 0; + + case 'c': + typedef Bu::Cache BobCache; + typedef BobCache::Ptr BobPtr; - BobCache bobCache; + BobCache cBob; - BobPtr pB1 = bobCache.insert( new Bob() ); - (*pB1).setInt( 44 ); - printf("RefCnt = %d\n", bobCache.getRefCnt( 0 ) ); + cBob.appendStore( new BobStore() ); + + return 0; + } - BobPtr pB2 = bobCache.get( 0 ); - printf("RefCnt = %d\n", bobCache.getRefCnt( 0 ) ); - printf("Int = %d\n", pB2->getInt() ); } -- cgit v1.2.3