From 90a51615bec786fac6750616e82570cf8727db06 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 16 Jan 2009 00:30:59 +0000 Subject: Whoa, Bu::CacheStoreNids totally works now, it's even tested and everything. Isn't that great? --- src/cachestorenids.h | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) (limited to 'src/cachestorenids.h') diff --git a/src/cachestorenids.h b/src/cachestorenids.h index e69de29..f488117 100644 --- a/src/cachestorenids.h +++ b/src/cachestorenids.h @@ -0,0 +1,100 @@ +#ifndef BU_CACHE_STORE_NIDS_H +#define BU_CACHE_STORE_NIDS_H + +#include "bu/fstring.h" +#include "bu/stream.h" +#include "bu/nids.h" +#include "bu/nidsstream.h" +#include "bu/cachestore.h" + +namespace Bu +{ + template + class CacheStoreNids : public CacheStore + { + public: + CacheStoreNids( Bu::Stream &sArch, + int iBlockSize=1024, int iPreAllocate=1 ) : + nStore( sArch ) + { + try + { + nStore.initialize(); + NidsStream ns = nStore.openStream( 0 ); + Bu::Archive ar( ns, Bu::Archive::load ); + ar >> hId; + } + catch( Bu::NidsException &e ) + { + nStore.initialize( iBlockSize, iPreAllocate ); + int iStream = nStore.createStream(); + if( iStream != 0 ) + printf("That's...horrible...id = %d.\n\n", iStream ); + NidsStream ns = nStore.openStream( 0 ); + Bu::Archive ar( ns, Bu::Archive::save ); + ar << hId; + } + } + + virtual ~CacheStoreNids() + { + NidsStream ns = nStore.openStream( 0 ); + Bu::Archive ar( ns, Bu::Archive::save ); + ar << hId; + } + + virtual obtype *load( const keytype &key ) + { + int iStream = hId.get( key ); + NidsStream ns = nStore.openStream( iStream ); + Bu::Archive ar( ns, Bu::Archive::load ); + obtype *pOb = new obtype(); + ar >> (*pOb); + return pOb; + } + + virtual void unload( obtype *pObj, const keytype &key ) + { + int iStream = hId.get( key ); + NidsStream ns = nStore.openStream( iStream ); + Bu::Archive ar( ns, Bu::Archive::save ); + ar << (*pObj); + delete pObj; + } + + virtual keytype getKey( obtype *pSrc )=0; + + virtual keytype create( obtype *pSrc ) + { + keytype key = getKey( pSrc ); + int iStream = nStore.createStream(); + hId.insert( key, iStream ); + printf("Creating stream: %d\n", iStream ); + NidsStream ns = nStore.openStream( iStream ); + Bu::Archive ar( ns, Bu::Archive::save ); + obtype *pOb = new obtype(); + ar << (*pOb); + return key; + } + + virtual void destroy( obtype *pObj, const keytype &key ) + { + int iStream = hId.get( key ); + nStore.deleteStream( iStream ); + hId.erase( key ); + delete pObj; + } + + Bu::List getKeys() + { + return hId.getKeys(); + } + + private: + Nids nStore; + typedef Bu::Hash NidHash; + NidHash hId; + }; +}; + +#endif -- cgit v1.2.3