From 668737effd601778fba74edec14d22dd5b87457a Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 19 Apr 2010 15:16:53 +0000 Subject: CacheStoreMyriad is written, it's pretty much a copy of CacheStoreNids since Nids and Myriad pretty much share an API. However, there seems to be a bug in Myriad when a Myriad file is created and filled with data immediately, the header stream is mis-linking one of the blocks again. --- src/cachestoremyriad.h | 156 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 src/cachestoremyriad.h (limited to 'src/cachestoremyriad.h') diff --git a/src/cachestoremyriad.h b/src/cachestoremyriad.h new file mode 100644 index 0000000..a40ffb2 --- /dev/null +++ b/src/cachestoremyriad.h @@ -0,0 +1,156 @@ +/* + * Copyright (C) 2007-2010 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#ifndef BU_CACHE_STORE_MYRIAD_H +#define BU_CACHE_STORE_MYRIAD_H + +#include "bu/fstring.h" +#include "bu/stream.h" +#include "bu/myriad.h" +#include "bu/cachestore.h" +#include "bu/myriadstream.h" + +#include "bu/archive.h" + +#include "bu/sio.h" + +namespace Bu +{ + template + keytype __cacheGetKey( const obtype *pObj ); + + template + obtype *__cacheStoreMyriadAlloc( const keytype &key ) + { + return new obtype(); + } + + template + void __cacheStoreMyriadStore( Bu::Stream &s, obtype &rObj, + const keytype & ) + { + Bu::Archive ar( s, Bu::Archive::save ); + ar << rObj; + } + + template + obtype *__cacheStoreMyriadLoad( Bu::Stream &s, const keytype &key ) + { + obtype *pObj = __cacheStoreMyriadAlloc( key ); + Bu::Archive ar( s, Bu::Archive::load ); + ar >> (*pObj); + return pObj; + } + + template + class CacheStoreMyriad : public CacheStore + { + public: + CacheStoreMyriad( Bu::Stream &sArch, + int iBlockSize=1024, int iPreAllocate=1 ) : + mStore( sArch ) + { + try + { + mStore.initialize(); + MyriadStream ns = mStore.openStream( 1 ); + Bu::Archive ar( ns, Bu::Archive::load ); + ar >> hId; + Bu::sio << hId << Bu::sio.nl; + } + catch( Bu::MyriadException &e ) + { + mStore.initialize( iBlockSize, iPreAllocate ); + int iStream = mStore.createStream(); + if( iStream != 1 ) + throw Bu::ExceptionBase("That's...horrible...id = %d.\n\n", iStream ); + MyriadStream ns = mStore.openStream( 1 ); + Bu::Archive ar( ns, Bu::Archive::save ); + ar << hId; + } + } + + virtual ~CacheStoreMyriad() + { + MyriadStream ns = mStore.openStream( 1 ); + Bu::Archive ar( ns, Bu::Archive::save ); + ar << hId; + } + + virtual obtype *load( const keytype &key ) + { + int iStream = hId.get( key ); + MyriadStream ns = mStore.openStream( iStream ); + obtype *pOb = __cacheStoreMyriadLoad( ns, key ); + return pOb; + } + + virtual void unload( obtype *pObj, const keytype &key ) + { + int iStream = hId.get( key ); + MyriadStream ns = mStore.openStream( iStream ); + __cacheStoreMyriadStore( ns, *pObj, key ); + delete pObj; + } + + virtual keytype create( obtype *pSrc ) + { + keytype key = __cacheGetKey( pSrc ); + int iStream = mStore.createStream(); + hId.insert( key, iStream ); + MyriadStream ns = mStore.openStream( iStream ); + __cacheStoreMyriadStore( ns, *pSrc, key ); + return key; + } + + virtual void sync() + { + MyriadStream ns = mStore.openStream( 1 ); + Bu::Archive ar( ns, Bu::Archive::save ); + ar << hId; + + mStore.sync(); + } + + virtual void sync( obtype *pSrc, const keytype &key ) + { + int iStream = hId.get( key ); + MyriadStream ns = mStore.openStream( iStream ); + __cacheStoreMyriadStore( ns, *pSrc, key ); + } + + virtual void destroy( obtype *pObj, const keytype &key ) + { + int iStream = hId.get( key ); + mStore.deleteStream( iStream ); + hId.erase( key ); + delete pObj; + } + + virtual bool has( const keytype &key ) + { + return hId.has( key ); + } + + virtual Bu::List getKeys() + { + return hId.getKeys(); + } + + virtual int getSize() + { + return hId.getSize(); + } + + private: + Myriad mStore; + typedef Bu::Hash StreamHash; + StreamHash hId; + }; +}; + +#endif -- cgit v1.2.3