From ef2935347099967cc7092fa7f472d91d51571468 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 24 Feb 2009 00:06:16 +0000 Subject: Whoa, lots of updates. Md5 is more general, nids, cache, cachestore, and cachestorenids all support synchronizing now. Url is pretty much done. --- src/cache.h | 38 ++++++++++++++++++++++++++++++++++++++ src/cachestore.h | 3 +++ src/cachestorenids.h | 21 +++++++++++++++++++++ src/cryptohash.h | 2 +- src/md5.cpp | 4 +++- src/md5.h | 2 +- src/nids.cpp | 7 +++++++ src/nids.h | 6 ++++++ src/tests/md5.cpp | 6 +++--- src/url.cpp | 29 +++++++++++++++++++++++++++++ src/url.h | 20 +++++++++++--------- 11 files changed, 123 insertions(+), 15 deletions(-) diff --git a/src/cache.h b/src/cache.h index ccc4966..036ceb4 100644 --- a/src/cache.h +++ b/src/cache.h @@ -100,6 +100,13 @@ namespace Bu return kId; } + void unbind() + { + if( pCache && pData ) + pCache->decRef( kId ); + pData = NULL; + } + Ptr &operator=( const Ptr &rRhs ) { if( pCache && pData ) @@ -186,6 +193,11 @@ namespace Bu return Ptr( this, pData, k ); } + bool has( const keytype &cId ) + { + return hEnt.has( cId ) || pStore->has( cId ); + } + Ptr get( const keytype &cId ) { TRACE( cId ); @@ -260,6 +272,32 @@ namespace Bu return pStore->getKeys(); } + /** + * Make sure all currently loaded but not-in-use objects are synced to + * the store. + */ + void sync() + { + TRACE(); + int iSynced = 0; + for( typename CidHash::iterator i = hEnt.begin(); + i != hEnt.end(); i++ ) + { + if( i.getValue().iRefs == 0 ) + { + pStore->sync( + i.getValue().pData, + i.getKey() + ); + iSynced++; + } + } + if( iSynced > 0 ) + { + pStore->sync(); + } + } + private: void incRef( const keytype &cId ) { diff --git a/src/cachestore.h b/src/cachestore.h index 97f02af..437b3d3 100644 --- a/src/cachestore.h +++ b/src/cachestore.h @@ -24,7 +24,10 @@ namespace Bu virtual obtype *load( const keytype &key )=0; virtual void unload( obtype *pObj, const keytype &key )=0; virtual keytype create( obtype *pSrc )=0; + virtual void sync()=0; + virtual void sync( obtype *pObj, const keytype &key )=0; virtual void destroy( obtype *pObj, const keytype &key )=0; + virtual bool has( const keytype &key )=0; virtual Bu::List getKeys() { return Bu::List(); } private: diff --git a/src/cachestorenids.h b/src/cachestorenids.h index 40213cd..c6a00ce 100644 --- a/src/cachestorenids.h +++ b/src/cachestorenids.h @@ -97,6 +97,22 @@ namespace Bu return key; } + virtual void sync() + { + NidsStream ns = nStore.openStream( 0 ); + Bu::Archive ar( ns, Bu::Archive::save ); + ar << hId; + + nStore.sync(); + } + + virtual void sync( obtype *pSrc, const keytype &key ) + { + int iStream = hId.get( key ); + NidsStream ns = nStore.openStream( iStream ); + __cacheStoreNidsStore( ns, *pSrc, key ); + } + virtual void destroy( obtype *pObj, const keytype &key ) { int iStream = hId.get( key ); @@ -105,6 +121,11 @@ namespace Bu delete pObj; } + virtual bool has( const keytype &key ) + { + return hId.has( key ); + } + virtual Bu::List getKeys() { return hId.getKeys(); diff --git a/src/cryptohash.h b/src/cryptohash.h index 01d4634..ed8d9ec 100644 --- a/src/cryptohash.h +++ b/src/cryptohash.h @@ -13,7 +13,7 @@ namespace Bu virtual void reset() = 0; virtual void setSalt( const Bu::FString &sSalt ) = 0; - virtual void addData( const char *sData, int iSize ) = 0; + virtual void addData( const void *sData, int iSize ) = 0; virtual void addData( const Bu::FString &sData ); virtual FString getResult() = 0; }; diff --git a/src/md5.cpp b/src/md5.cpp index aa965ed..44e8519 100644 --- a/src/md5.cpp +++ b/src/md5.cpp @@ -55,8 +55,10 @@ void Bu::Md5::setSalt( const Bu::FString & /*sSalt*/ ) { } -void Bu::Md5::addData( const char *sData, int iSize ) +void Bu::Md5::addData( const void *sVData, int iSize ) { + const char *sData = (const char *)sVData; + int iInPos = 0; for(;;) { diff --git a/src/md5.h b/src/md5.h index c548041..f3f040c 100644 --- a/src/md5.h +++ b/src/md5.h @@ -20,7 +20,7 @@ namespace Bu virtual void reset(); virtual void setSalt( const Bu::FString &sSalt ); - virtual void addData( const char *sData, int iSize ); + virtual void addData( const void *sData, int iSize ); using Bu::CryptoHash::addData; virtual FString getResult(); diff --git a/src/nids.cpp b/src/nids.cpp index 26a7964..0bec952 100644 --- a/src/nids.cpp +++ b/src/nids.cpp @@ -31,6 +31,13 @@ Bu::Nids::~Nids() updateHeader(); } +void Bu::Nids::sync() +{ + updateHeader(); + + // Later, also flush all caches. +} + void Bu::Nids::initialize() { unsigned char buf[4]; diff --git a/src/nids.h b/src/nids.h index 262d9c1..cab4448 100644 --- a/src/nids.h +++ b/src/nids.h @@ -70,6 +70,12 @@ namespace Bu int getBlockStart(); int getBlockOverhead(); + /** + * Syncronize the header data, etc. with the storage stream. It's not + * a bad idea to call this periodically. + */ + void sync(); + private: typedef struct Block { diff --git a/src/tests/md5.cpp b/src/tests/md5.cpp index dea957b..2e9c318 100644 --- a/src/tests/md5.cpp +++ b/src/tests/md5.cpp @@ -12,12 +12,12 @@ int main( int argc, char *argv[] ) Bu::File fIn( *argv, Bu::File::Read ); Bu::Md5 m; - char buf[1000]; + char buf[100000]; for(;;) { - int iRead = fIn.read( buf, 1000 ); + int iRead = fIn.read( buf, 100000 ); m.addData( buf, iRead ); - if( iRead < 1000 ) + if( iRead < 100000 ) break; } diff --git a/src/url.cpp b/src/url.cpp index 7cc4263..e4d2f55 100644 --- a/src/url.cpp +++ b/src/url.cpp @@ -235,3 +235,32 @@ void Bu::Url::clear() iPort.clear(); } +Bu::FString Bu::Url::getFullPath() const +{ + Bu::FString sBuf = sPath; + if( !lParam.isEmpty() ) + { + for( ParamList::const_iterator i = lParam.begin(); i; i++ ) + { + if( i == lParam.begin() ) + sBuf += "?"; + else + sBuf += "&"; + + sBuf += encode( (*i).sName ); + if( !(*i).sValue.isEmpty() ) + { + sBuf += "=" + encode( (*i).sValue ); + } + } + } + + return sBuf; +} + +Bu::FString Bu::Url::getUrl() const +{ + Bu::FString sBuf = sProtocol + "://" + sHost + getFullPath(); + return sBuf; +} + diff --git a/src/url.h b/src/url.h index 861bb9f..8713ed4 100644 --- a/src/url.h +++ b/src/url.h @@ -29,15 +29,17 @@ namespace Bu void parseParams( Bu::FString::const_iterator &i ); void clear(); - Bu::FString getUrl(); + Bu::FString getUrl() const; + Bu::FString getFullPath() const; - const Bu::FString &getProtocol() { return sProtocol; } - const Bu::FString &getUser() { return sUser; } - const Bu::FString &getPass() { return sPass; } - const Bu::FString &getHost() { return sHost; } - const Bu::FString &getPath() { return sPath; } - int getPort() { return iPort; } - ParamList::const_iterator getParamBegin() { return lParam.begin(); } + const Bu::FString &getProtocol() const { return sProtocol; } + const Bu::FString &getUser() const { return sUser; } + const Bu::FString &getPass() const { return sPass; } + const Bu::FString &getHost() const { return sHost; } + const Bu::FString &getPath() const { return sPath; } + int getPort() const { return iPort; } + ParamList::const_iterator getParamBegin() const + { return lParam.begin(); } void setProtocol( const Bu::FString &sNewHost, bool bAutoSetPort=true ); void setUser( const Bu::FString &s ) { sUser = s; } @@ -47,7 +49,7 @@ namespace Bu void setPort( int i ) { iPort = i; } void addParam( const Bu::FString &n, const Bu::FString &v ); - bool hasPort() { return iPort.has(); } + bool hasPort() const { return iPort.has(); } static Bu::FString decode( const Bu::FString &sStr ); static Bu::FString encode( const Bu::FString &sStr ); -- cgit v1.2.3