From 0bd8b8cf19687229b53e37468dfe36c4217fbbf1 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 3 Dec 2008 17:05:26 +0000 Subject: Alright, the caching system now passes the basic CRUD tests with arbitrary keytypes. It doesn't yet use the stackable CacheStore concept, but the code is all in place to make it easy to switch to. There also needs to be some more accounting code in place, so that we can actually use the Schedulers, whatever they happen to be called in the future. A whacky side note, it turns out that we totally need to ensure an object is loaded from the cache in order to delete it, we can't ensure that any references to other objects that are in the cache will be loaded otherwise. --- src/cache.cpp | 1 + src/cache.h | 58 ++++++++++++++++++++---- src/cachestore.h | 6 +-- src/cptr.h | 11 ++++- src/tests/cache.cpp | 125 +++++++++++++++++++++++++++++++++++++++++----------- src/trace.cpp | 12 ++++- src/trace.h | 37 +++++++++++----- 7 files changed, 198 insertions(+), 52 deletions(-) diff --git a/src/cache.cpp b/src/cache.cpp index e69de29..fd1082b 100644 --- a/src/cache.cpp +++ b/src/cache.cpp @@ -0,0 +1 @@ +#include "bu/cache.h" diff --git a/src/cache.h b/src/cache.h index 0c3994c..9488044 100644 --- a/src/cache.h +++ b/src/cache.h @@ -38,6 +38,20 @@ namespace Bu virtual ~Cache() { TRACE(); + for( typename CidHash::iterator i = hEnt.begin(); + i != hEnt.end(); i++ ) + { + if( i.getValue().iRefs > 0 ) + { + printf("Error? iRefs=%d for key ", i.getValue().iRefs ); + __tracer_format( i.getKey() ); + printf("!\n"); + } + lStore.first()->unload( + i.getValue().pData, + i.getKey() + ); + } for( typename StoreList::iterator i = lStore.begin(); i != lStore.end(); i++ ) { @@ -47,44 +61,72 @@ namespace Bu void appendStore( Store *pHand ) { + TRACE(); lStore.append( pHand ); } void prependStore( Store *pHand ) { + TRACE(); lStore.prepend( pHand ); } Ptr insert( obtype *pData ) { - TRACE(); + TRACE( pData ); CacheEntry e = {pData, 0}; - hEnt.insert( 0 , e ); - return Ptr( *this, pData ); + keytype k = lStore.first()->create( pData ); + hEnt.insert( k, e ); + + return Ptr( *this, pData, k ); } Ptr get( keytype cId ) { - TRACE(); - return Ptr( *this, hEnt.get( cId ).pData ); + TRACE( cId ); + try { + return Ptr( *this, hEnt.get( cId ).pData, cId ); + } + catch( Bu::HashException &e ) { + CacheEntry e = {lStore.first()->load( cId ), 0}; + hEnt.insert( cId, e ); + return Ptr( *this, e.pData, cId ); + } + } + + void erase( keytype cId ) + { + TRACE( cId ); + try { + if( hEnt.get( cId ).iRefs > 0 ) + { + printf("Shouldn't delete, references still exist!\n"); + return; + } + } + catch( Bu::HashException &e ) { + get( cId ); + } + lStore.first()->destroy( hEnt.get( cId ).pData, cId ); + hEnt.erase( cId ); } int getRefCnt( keytype cId ) { - TRACE(); + TRACE( cId ); return hEnt.get( cId ).iRefs; } private: void incRef( keytype cId ) { - TRACE(); + TRACE( cId ); hEnt.get( cId ).iRefs++; } void decRef( keytype cId ) { - TRACE(); + TRACE( cId ); CacheEntry &e = hEnt.get( cId ); e.iRefs--; } diff --git a/src/cachestore.h b/src/cachestore.h index 3211b6a..5b52359 100644 --- a/src/cachestore.h +++ b/src/cachestore.h @@ -1,8 +1,6 @@ #ifndef BU_CACHE_STORE_H #define BU_CACHE_STORE_H -#include "bu/cptr.h" - namespace Bu { /** @@ -21,10 +19,8 @@ namespace Bu { } - typedef Bu::CPtr Ptr; - virtual obtype *load( const keytype &key )=0; - virtual void unload( obtype *pObj )=0; + virtual void unload( obtype *pObj, const keytype &key )=0; virtual keytype create( obtype *pSrc )=0; virtual void destroy( obtype *pObj, const keytype &key )=0; diff --git a/src/cptr.h b/src/cptr.h index 8f35a03..97f5e17 100644 --- a/src/cptr.h +++ b/src/cptr.h @@ -15,9 +15,11 @@ namespace Bu { friend class Bu::Cache; private: - CPtr( Cache &rCache, obtype *pData ) : + CPtr( Cache &rCache, obtype *pData, + const keytype &kId ) : rCache( rCache ), - pData( pData ) + pData( pData ), + kId( kId ) { rCache.incRef( kId ); } @@ -38,6 +40,11 @@ namespace Bu return pData; } + const keytype &getKey() + { + return kId; + } + private: Bu::Cache &rCache; obtype *pData; diff --git a/src/tests/cache.cpp b/src/tests/cache.cpp index 1cc008a..18e6a95 100644 --- a/src/tests/cache.cpp +++ b/src/tests/cache.cpp @@ -1,9 +1,12 @@ #include +#include #include #include #include #include "bu/cache.h" +#include "bu/file.h" +#include "bu/fstring.h" class Bob { @@ -13,6 +16,12 @@ public: TRACE(); } + Bob( int i ) : + iInt( i ) + { + TRACE( i ); + } + virtual ~Bob() { TRACE(); @@ -20,7 +29,7 @@ public: void setInt( int i ) { - TRACE(); + TRACE( i ); iInt = i; } @@ -29,15 +38,17 @@ public: return iInt; } - long getCacheId() const - { - TRACE(); - return 0; - } - int iInt; }; +namespace Bu { + template<> + void __tracer_format( Bob* const &c ) + { + printf("%08X=%d", (uint32_t)c, c->getInt() ); + } +} + class BobStore : public Bu::CacheStore { public: @@ -45,34 +56,79 @@ public: cLastId( 0 ) { TRACE(); + if( access( "bobcache/last", R_OK|W_OK ) ) + { + mkdir("bobcache", 0755 ); + Bu::File f("bobcache/last", Bu::File::Write|Bu::File::Create ); + f.write("0", 1); + printf("Initializing cache: %s\n", strerror( errno ) ); + } + else + { + cLastId = readNum("bobcache/last"); + } } ~BobStore() { TRACE(); + writeNum("bobcache/last", cLastId ); + } + + long readNum( const Bu::FString &sFile ) + { + TRACE( sFile ); + Bu::File f( sFile, Bu::File::Read ); + char buf[80]; + buf[f.read( buf, 80 )] = '\0'; + return strtol( buf, NULL, 0 ); + } + + void writeNum( const Bu::FString &sFile, long num ) + { + TRACE( sFile, num ); + Bu::File f( sFile, + Bu::File::Write|Bu::File::Create|Bu::File::Truncate + ); + Bu::FString s; + s.format("%d", num ); + f.write( s ); } virtual Bob *load( const long &key ) { - TRACE(); - return NULL; + TRACE( key ); + Bu::FString sDest; + sDest.format("bobcache/%d", key ); + return new Bob( readNum( sDest ) ); } - virtual void unload( Bob *pObj ) + virtual void unload( Bob *pObj, const long &key ) { - TRACE(); + TRACE( pObj, key ); + Bu::FString sDest; + sDest.format("bobcache/%d", key ); + writeNum( sDest, pObj->getInt() ); delete pObj; } virtual long create( Bob *rSrc ) { - TRACE(); - return ++cLastId; + TRACE( rSrc ); + long id = ++cLastId; + Bu::FString sDest; + sDest.format("bobcache/%d", id ); + writeNum( sDest, rSrc->getInt() ); + return id; } virtual void destroy( Bob *pObj, const long &key ) { - TRACE(); + TRACE( pObj, key ); + Bu::FString sDest; + sDest.format("bobcache/%d", key ); + if( !access( sDest.getStr(), F_OK ) ) + unlink( sDest.getStr() ); delete pObj; } @@ -82,28 +138,47 @@ private: int main( int argc, char *argv[] ) { - TRACE(); + TRACE( argc, argv ); + typedef Bu::Cache BobCache; + typedef BobCache::Ptr BobPtr; + if( argc < 3 ) { - printf("Try: %s [icufd] []\n\n", argv[0] ); + printf("Try: %s [crud] []\n\n", argv[0] ); return 0; } + BobCache cBob; + cBob.appendStore( new BobStore() ); 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::Ptr pBob = cBob.insert( + new Bob( strtol( argv[2], NULL, 0 ) ) + ); + printf("Key = %ld\n", pBob.getKey() ); + } + return 0; - BobCache cBob; + case 'r': + { + BobCache::Ptr pBob = cBob.get( strtol( argv[2], NULL, 0 ) ); + printf("Value = %d\n", pBob->getInt() ); + } + return 0; - cBob.appendStore( new BobStore() ); + case 'u': + { + BobCache::Ptr pBob = cBob.get( strtol( argv[2], NULL, 0 ) ); + pBob->setInt( strtol( argv[3], NULL, 0 ) ); + } + return 0; + case 'd': + { + cBob.erase( strtol( argv[2], NULL, 0 ) ); + } return 0; } diff --git a/src/trace.cpp b/src/trace.cpp index dab53d6..242d110 100644 --- a/src/trace.cpp +++ b/src/trace.cpp @@ -58,7 +58,7 @@ template<> void Bu::__tracer_format( const char &v ) { printf("%hhd", v ); } -/* + template<> void Bu::__tracer_format( const long &v ) { printf("%ld", v ); @@ -67,7 +67,7 @@ template<> void Bu::__tracer_format( const long &v ) template<> void Bu::__tracer_format( const unsigned long &v ) { printf("%lu", v ); -}*/ +} template<> void Bu::__tracer_format( const float &v ) { @@ -93,7 +93,11 @@ template<> void Bu::__tracer_format( char ** const &v ) { printf("["); for( int j = 0; v[j]; j++ ) + { + if( j > 0 ) + printf(", "); printf("\"%s\"", v[j] ); + } printf("]"); } @@ -111,6 +115,10 @@ template<> void Bu::__tracer_format( char const ** const &v ) { printf("["); for( int j = 0; v[j]; j++ ) + { + if( j > 0 ) + printf(", "); printf("\"%s\"", v[j] ); + } printf("]"); } diff --git a/src/trace.h b/src/trace.h index 82399c2..049d138 100644 --- a/src/trace.h +++ b/src/trace.h @@ -11,6 +11,7 @@ #include #include #include +#include namespace Bu { @@ -26,7 +27,12 @@ namespace Bu #define looper( vv ) \ for( ; *n; n++ ) \ { \ - if( *n == ',' || *n == ')' ) \ + if( bInBracket == true ) \ + { \ + if( *n == '>' ) \ + bInBracket = false; \ + } \ + else if( *n == ',' || *n == ')' ) \ { \ fwrite( s, (ptrdiff_t)n-(ptrdiff_t)s, 1, stdout ); \ fwrite("=", 1, 1, stdout); \ @@ -35,6 +41,10 @@ namespace Bu n++; \ break; \ } \ + else if( *n == '<' ) \ + { \ + bInBracket = true; \ + } \ } (void)0 template void __tracer( const char *pf, t1 &v1 ) @@ -42,8 +52,9 @@ namespace Bu printf("trace: "); const char *s = pf; const char *n = pf; + bool bInBracket = false; looper( v1 ); - fwrite( s, (ptrdiff_t)n-(ptrdiff_t)s, 1, stdout ); + fwrite( s, (ptrdiff_t)n-(ptrdiff_t)s, strlen(s), stdout ); fwrite( "\n", 1, 1, stdout ); fflush( stdout ); } @@ -54,9 +65,10 @@ namespace Bu printf("trace: "); const char *s = pf; const char *n = pf; + bool bInBracket = false; looper( v1 ); looper( v2 ); - fwrite( s, (ptrdiff_t)n-(ptrdiff_t)s, 1, stdout ); + fwrite( s, (ptrdiff_t)n-(ptrdiff_t)s, strlen(s), stdout ); fwrite( "\n", 1, 1, stdout ); fflush( stdout ); } @@ -67,10 +79,11 @@ namespace Bu printf("trace: "); const char *s = pf; const char *n = pf; + bool bInBracket = false; looper( v1 ); looper( v2 ); looper( v3 ); - fwrite( s, (ptrdiff_t)n-(ptrdiff_t)s, 1, stdout ); + fwrite( s, (ptrdiff_t)n-(ptrdiff_t)s, strlen(s), stdout ); fwrite( "\n", 1, 1, stdout ); fflush( stdout ); } @@ -81,11 +94,12 @@ namespace Bu printf("trace: "); const char *s = pf; const char *n = pf; + bool bInBracket = false; looper( v1 ); looper( v2 ); looper( v3 ); looper( v4 ); - fwrite( s, (ptrdiff_t)n-(ptrdiff_t)s, 1, stdout ); + fwrite( s, (ptrdiff_t)n-(ptrdiff_t)s, strlen(s), stdout ); fwrite( "\n", 1, 1, stdout ); fflush( stdout ); } @@ -96,12 +110,13 @@ namespace Bu printf("trace: "); const char *s = pf; const char *n = pf; + bool bInBracket = false; looper( v1 ); looper( v2 ); looper( v3 ); looper( v4 ); looper( v5 ); - fwrite( s, (ptrdiff_t)n-(ptrdiff_t)s, 1, stdout ); + fwrite( s, (ptrdiff_t)n-(ptrdiff_t)s, strlen(s), stdout ); fwrite( "\n", 1, 1, stdout ); fflush( stdout ); } @@ -114,13 +129,14 @@ namespace Bu printf("trace: "); const char *s = pf; const char *n = pf; + bool bInBracket = false; looper( v1 ); looper( v2 ); looper( v3 ); looper( v4 ); looper( v5 ); looper( v6 ); - fwrite( s, (ptrdiff_t)n-(ptrdiff_t)s, 1, stdout ); + fwrite( s, (ptrdiff_t)n-(ptrdiff_t)s, strlen(s), stdout ); fwrite( "\n", 1, 1, stdout ); fflush( stdout ); } @@ -133,6 +149,7 @@ namespace Bu printf("trace: "); const char *s = pf; const char *n = pf; + bool bInBracket = false; looper( v1 ); looper( v2 ); looper( v3 ); @@ -140,7 +157,7 @@ namespace Bu looper( v5 ); looper( v6 ); looper( v7 ); - fwrite( s, (ptrdiff_t)n-(ptrdiff_t)s, 1, stdout ); + fwrite( s, (ptrdiff_t)n-(ptrdiff_t)s, strlen(s), stdout ); fwrite( "\n", 1, 1, stdout ); fflush( stdout ); } @@ -156,8 +173,8 @@ namespace Bu template<> void __tracer_format( const uint64_t &v ); template<> void __tracer_format( const bool &v ); template<> void __tracer_format( const char &v ); - //template<> void __tracer_format( const long &v ); - //template<> void __tracer_format( const unsigned long &v ); + template<> void __tracer_format( const long &v ); + template<> void __tracer_format( const unsigned long &v ); template<> void __tracer_format( const float &v ); template<> void __tracer_format( const double &v ); template<> void __tracer_format( void * const &v ); -- cgit v1.2.3