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/tests/cache.cpp | 125 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 100 insertions(+), 25 deletions(-) (limited to 'src/tests/cache.cpp') 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; } -- cgit v1.2.3