From 1dcc1b1fc9fd35d959255452c8b5be1269ca4f44 Mon Sep 17 00:00:00 2001 From: zecke Date: Thu, 10 Oct 2002 17:08:58 +0000 Subject: The Cache is finally in place I tested it with my todolist and it 'works' for 10.000 todos the hits are awesome ;) The read ahead functionality does not make sense for XMLs backends because most of the stuff is already in memory. While using readahead on SQL makes things a lot faster.... I still have to fully implement read ahead This change is bic but sc --- (limited to 'libopie/pim/opimaccesstemplate.h') diff --git a/libopie/pim/opimaccesstemplate.h b/libopie/pim/opimaccesstemplate.h index 31ab516..92d7192 100644 --- a/libopie/pim/opimaccesstemplate.h +++ b/libopie/pim/opimaccesstemplate.h @@ -7,6 +7,7 @@ #include #include +#include "opimcache.h" #include "otemplatebase.h" /** @@ -23,6 +24,7 @@ class OPimAccessTemplate : public OTemplateBase { public: typedef ORecordList List; typedef OPimAccessBackend BackEnd; + typedef OPimCache Cache; /** * our sort order @@ -73,6 +75,12 @@ public: */ virtual T find( int uid )const; + /** + * read ahead cache find method ;) + */ + virtual T find( int uid, const QArray&, + uint current, CacheDirection dir = Forward )const; + /* invalidate cache here */ /** * clears the backend and invalidates the backend @@ -99,6 +107,12 @@ public: * replace T from backend */ virtual bool replace( const T& t) ; + + /** + * @internal + */ + void cache( const T& )const; + void setSaneCacheSize( int ); protected: /** * invalidate the cache @@ -111,6 +125,7 @@ protected: */ BackEnd* backEnd(); BackEnd* m_backEnd; + Cache m_cache; }; @@ -118,7 +133,8 @@ template OPimAccessTemplate::OPimAccessTemplate( BackEnd* end ) : OTemplateBase(), m_backEnd( end ) { - + if (end ) + end->setFrontend( this ); } template OPimAccessTemplate::~OPimAccessTemplate() { @@ -127,6 +143,7 @@ OPimAccessTemplate::~OPimAccessTemplate() { } template bool OPimAccessTemplate::load() { + invalidateCache(); return m_backEnd->load(); } template @@ -154,6 +171,26 @@ OPimAccessTemplate::queryByExample( const T& t, int sortOrder ) { template T OPimAccessTemplate::find( int uid ) const{ T t = m_backEnd->find( uid ); + cache( t ); + return t; +} +template +T OPimAccessTemplate::find( int uid, const QArray& ar, + uint current, CacheDirection dir )const { + /* + * better do T.isEmpty() + * after a find this way we would + * avoid two finds in QCache... + */ + // qWarning("find it now %d", uid ); + if (m_cache.contains( uid ) ) { + qWarning("m cache contains %d", uid); + return m_cache.find( uid ); + } + + T t = m_backEnd->find( uid, ar, current, dir ); + qWarning("found it and cache it now %d", uid); + cache( t ); return t; } template @@ -163,23 +200,26 @@ void OPimAccessTemplate::clear() { } template bool OPimAccessTemplate::add( const T& t ) { + cache( t ); return m_backEnd->add( t ); } template bool OPimAccessTemplate::remove( const T& t ) { - return m_backEnd->remove( t.uid() ); + return remove( t.uid() ); } template bool OPimAccessTemplate::remove( int uid ) { + m_cache.remove( uid ); return m_backEnd->remove( uid ); } template bool OPimAccessTemplate::replace( const T& t ) { + m_cache.replace( t ); return m_backEnd->replace( t ); } template void OPimAccessTemplate::invalidateCache() { - + m_cache.invalidate(); } template OPimAccessTemplate::BackEnd* OPimAccessTemplate::backEnd() { @@ -192,5 +232,16 @@ bool OPimAccessTemplate::wasChangedExternally()const { template void OPimAccessTemplate::setBackEnd( BackEnd* end ) { m_backEnd = end; + if (m_backEnd ) + m_backEnd->setFrontend( this ); +} +template +void OPimAccessTemplate::cache( const T& t ) const{ + /* hacky we need to work around the const*/ + ((OPimAccessTemplate*)this)->m_cache.add( t ); +} +template +void OPimAccessTemplate::setSaneCacheSize( int size ) { + m_cache.setSize( size ); } #endif -- cgit v0.9.0.2