From 88b3a5acf78aa9a2d73f2462e45988f5afb9d2c5 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 24 Feb 2009 01:23:11 +0000 Subject: Woot, the cache system now supports intellegent sync'ing. It will ask the cache about each object that it has and weather or not to sync it. This will probably be made optional in the future. --- src/cache.h | 25 +++++++++++++++++-------- src/cachecalc.h | 4 ++++ src/tests/cache.cpp | 17 +++++++++++++++++ 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/cache.h b/src/cache.h index 036ceb4..4ae1aa8 100644 --- a/src/cache.h +++ b/src/cache.h @@ -143,6 +143,7 @@ namespace Bu { obtype *pData; int iRefs; + time_t tLastSync; } CacheEntry; typedef Bu::Hash CidHash; @@ -184,7 +185,7 @@ namespace Bu Ptr insert( obtype *pData ) { TRACE( pData ); - CacheEntry e = {pData, 0}; + CacheEntry e = {pData, 0, 0}; keytype k = pStore->create( pData ); hEnt.insert( k, e ); @@ -205,7 +206,7 @@ namespace Bu return Ptr( this, hEnt.get( cId ).pData, cId ); } catch( Bu::HashException &e ) { - CacheEntry e = {pStore->load( cId ), 0}; + CacheEntry e = {pStore->load( cId ), 0, time( NULL )}; pCalc->onLoad( e.pData, cId ); hEnt.insert( cId, e ); return Ptr( this, e.pData, cId ); @@ -285,11 +286,19 @@ namespace Bu { if( i.getValue().iRefs == 0 ) { - pStore->sync( - i.getValue().pData, - i.getKey() - ); - iSynced++; + if( pCalc->shouldSync( + i.getValue().pData, + i.getKey(), + i.getValue().tLastSync + ) ) + { + pStore->sync( + i.getValue().pData, + i.getKey() + ); + iSynced++; + i.getValue().tLastSync = time( NULL ); + } } } if( iSynced > 0 ) @@ -319,7 +328,7 @@ namespace Bu return hEnt.get( cId ).pData; } catch( Bu::HashException &e ) { - CacheEntry e = {pStore->load( cId ), 0}; + CacheEntry e = {pStore->load( cId ), 0, time( NULL )}; pCalc->onLoad( e.pData, cId ); hEnt.insert( cId, e ); return e.pData; diff --git a/src/cachecalc.h b/src/cachecalc.h index dd9712f..7fdb16e 100644 --- a/src/cachecalc.h +++ b/src/cachecalc.h @@ -3,6 +3,8 @@ #include "bu/trace.h" +#include + namespace Bu { template class Cache; @@ -27,6 +29,8 @@ namespace Bu virtual void onLoad( obtype *pSrc, const keytype &key )=0; virtual void onUnload( obtype *pSrc, const keytype &key )=0; + virtual bool shouldSync( obtype *pSrc, const keytype &key, + time_t tLastSync )=0; virtual void onTick() { }; protected: diff --git a/src/tests/cache.cpp b/src/tests/cache.cpp index 323edda..8d95a9d 100644 --- a/src/tests/cache.cpp +++ b/src/tests/cache.cpp @@ -96,6 +96,19 @@ public: f.write( s ); } + virtual void sync( Bob *, const long & ) + { + } + + virtual void sync() + { + } + + virtual bool has( const long & ) + { + return true; + } + virtual Bob *load( const long &key ) { TRACE( key ); @@ -156,6 +169,10 @@ public: { } + virtual bool shouldSync( Bob *, const long &, time_t ) + { + } + private: }; -- cgit v1.2.3