-rw-r--r-- | libopie/pim/obackendfactory.h | 20 | ||||
-rw-r--r-- | libopie/pim/opimaccessbackend.h | 40 | ||||
-rw-r--r-- | libopie/pim/opimaccesstemplate.h | 57 | ||||
-rw-r--r-- | libopie/pim/opimcache.h | 117 | ||||
-rw-r--r-- | libopie/pim/orecordlist.h | 20 | ||||
-rw-r--r-- | libopie/pim/otemplatebase.h | 11 |
6 files changed, 248 insertions, 17 deletions
diff --git a/libopie/pim/obackendfactory.h b/libopie/pim/obackendfactory.h index f11f029..89b8c58 100644 --- a/libopie/pim/obackendfactory.h +++ b/libopie/pim/obackendfactory.h @@ -18,2 +18,9 @@ * $Log$ + * Revision 1.3 2002/10/10 17:08:58 zecke + * 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 + * * Revision 1.2 2002/10/08 09:27:36 eilers @@ -30,4 +37,4 @@ */ -#ifndef __OPIE_BACKENDFACTORY_H_ -#define __OPIE_BACKENDFACTORY_H_ +#ifndef OPIE_BACKENDFACTORY_H_ +#define OPIE_BACKENDFACTORY_H_ @@ -40,5 +47,6 @@ -#ifdef __USE_SQL +/*#ifdef __USE_SQL #include "otodoaccesssql.h" #endif +*/ @@ -74,9 +82,9 @@ class OBackendFactory case TODO: -#ifdef __USE_SQL +/*#ifdef __USE_SQL if ( backend == "sql" ) return (T*) new OTodoAccessBackendSQL(""); -#else +#else*/ if ( backend == "sql" ) qWarning ("OBackendFactory:: sql Backend not implemented! Using XML instead!"); -#endif +//#endif diff --git a/libopie/pim/opimaccessbackend.h b/libopie/pim/opimaccessbackend.h index c27acbb..27d3cb8 100644 --- a/libopie/pim/opimaccessbackend.h +++ b/libopie/pim/opimaccessbackend.h @@ -5,2 +5,3 @@ +#include <opie/otemplatebase.h> #include <opie/opimrecord.h> @@ -19,2 +20,3 @@ class OPimAccessBackend { public: + typedef OTemplateBase<T> Frontend; OPimAccessBackend(); @@ -56,2 +58,4 @@ public: + virtual T find(int uid, const QArray<int>& items, + uint current, Frontend::CacheDirection )const ; /** @@ -76,2 +80,17 @@ public: + /* + * setTheFrontEnd!!! + */ + void setFrontend( Frontend* front ); + +protected: + void cache( const T& t )const; + + /** + * use a prime number here! + */ + void setSaneCacheSize( int ); + +private: + Frontend* m_front; @@ -81,3 +100,3 @@ template <class T> OPimAccessBackend<T>::OPimAccessBackend() { - + m_front = 0l; } @@ -87,2 +106,21 @@ OPimAccessBackend<T>::~OPimAccessBackend() { } +template <class T> +void OPimAccessBackend<T>::setFrontend( Frontend* fr ) { + m_front = fr; +} +template <class T> +void OPimAccessBackend<T>::cache( const T& t )const { + if (m_front ) + m_front->cache( t ); +} +template <class T> +void OPimAccessBackend<T>::setSaneCacheSize( int size) { + if (m_front ) + m_front->setSaneCacheSize( size ); +} +template <class T> +T OPimAccessBackend<T>::find( int uid, const QArray<int>&, + uint, Frontend::CacheDirection )const { + return find( uid ); +} 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 @@ -9,2 +9,3 @@ +#include "opimcache.h" #include "otemplatebase.h" @@ -25,2 +26,3 @@ public: typedef OPimAccessBackend<T> BackEnd; + typedef OPimCache<T> Cache; @@ -75,2 +77,8 @@ public: + /** + * read ahead cache find method ;) + */ + virtual T find( int uid, const QArray<int>&, + uint current, CacheDirection dir = Forward )const; + /* invalidate cache here */ @@ -101,2 +109,8 @@ public: virtual bool replace( const T& t) ; + + /** + * @internal + */ + void cache( const T& )const; + void setSaneCacheSize( int ); protected: @@ -113,2 +127,3 @@ protected: BackEnd* m_backEnd; + Cache m_cache; @@ -120,3 +135,4 @@ OPimAccessTemplate<T>::OPimAccessTemplate( BackEnd* end ) { - + if (end ) + end->setFrontend( this ); } @@ -129,2 +145,3 @@ template <class T> bool OPimAccessTemplate<T>::load() { + invalidateCache(); return m_backEnd->load(); @@ -156,2 +173,22 @@ T OPimAccessTemplate<T>::find( int uid ) const{ T t = m_backEnd->find( uid ); + cache( t ); + return t; +} +template <class T> +T OPimAccessTemplate<T>::find( int uid, const QArray<int>& 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; @@ -165,2 +202,3 @@ template <class T> bool OPimAccessTemplate<T>::add( const T& t ) { + cache( t ); return m_backEnd->add( t ); @@ -169,3 +207,3 @@ template <class T> bool OPimAccessTemplate<T>::remove( const T& t ) { - return m_backEnd->remove( t.uid() ); + return remove( t.uid() ); } @@ -173,2 +211,3 @@ template <class T> bool OPimAccessTemplate<T>::remove( int uid ) { + m_cache.remove( uid ); return m_backEnd->remove( uid ); @@ -177,2 +216,3 @@ template <class T> bool OPimAccessTemplate<T>::replace( const T& t ) { + m_cache.replace( t ); return m_backEnd->replace( t ); @@ -181,3 +221,3 @@ template <class T> void OPimAccessTemplate<T>::invalidateCache() { - + m_cache.invalidate(); } @@ -194,2 +234,13 @@ void OPimAccessTemplate<T>::setBackEnd( BackEnd* end ) { m_backEnd = end; + if (m_backEnd ) + m_backEnd->setFrontend( this ); +} +template <class T> +void OPimAccessTemplate<T>::cache( const T& t ) const{ + /* hacky we need to work around the const*/ + ((OPimAccessTemplate<T>*)this)->m_cache.add( t ); +} +template <class T> +void OPimAccessTemplate<T>::setSaneCacheSize( int size ) { + m_cache.setSize( size ); } diff --git a/libopie/pim/opimcache.h b/libopie/pim/opimcache.h new file mode 100644 index 0000000..067f6e7 --- a/dev/null +++ b/libopie/pim/opimcache.h @@ -0,0 +1,117 @@ +#ifndef OPIE_PIM_CACHE_H +#define OPIE_PIM_CACHE_H + +#include <qintcache.h> + +#include "opimrecord.h" + +template <class T = OPimRecord> +class OPimCacheItem { +public: + OPimCacheItem( const T& t = T() ); + ~OPimCacheItem(); + + T record()const; + void setRecord( const T& ); +private: + T m_t; +}; + +/** + * OPimCache for caching the items + * We support adding, removing + * and finding + */ +template <class T = OPimRecord> +class OPimCache { +public: + typedef OPimCacheItem<T> Item; + OPimCache(); + ~OPimCache(); + + bool contains(int uid)const; + void invalidate(); + void setSize( int size ); + + T find(int uid )const; + void add( const T& ); + void remove( int uid ); + void replace( const T& ); + +private: + QIntCache<Item> m_cache; +}; + +// Implementation +template <class T> +OPimCacheItem<T>::OPimCacheItem( const T& t ) + : m_t(t) { +} +template <class T> +OPimCacheItem<T>::~OPimCacheItem() { + +} +template <class T> +T OPimCacheItem<T>::record()const { + return m_t; +} +template <class T> +void OPimCacheItem<T>::setRecord( const T& t ) { + m_t = t; +} +// Cache +template <class T> +OPimCache<T>::OPimCache() { + m_cache.setAutoDelete( TRUE ); +} +template <class T> +OPimCache<T>::~OPimCache() { + +} +template <class T> +bool OPimCache<T>::contains(int uid )const { + Item* it = m_cache.find( uid, FALSE ); + if (!it) + return false; + return true; +} +template <class T> +void OPimCache<T>::invalidate() { + m_cache.clear(); +} +template <class T> +void OPimCache<T>::setSize( int size ) { + m_cache.setMaxCost( size ); +} +template <class T> +T OPimCache<T>::find(int uid )const { + Item *it = m_cache.find( uid ); + if (it) + return it->record(); + return T(); +} +template <class T> +void OPimCache<T>::add( const T& t ) { + Item* it = 0l; + it = m_cache.find(t.uid(), FALSE ); + + if (it ) + it->setRecord( t ); + + it = new Item( t ); + if (!m_cache.insert( t.uid(), it ) ) + delete it; +} +template <class T> +void OPimCache<T>::remove( int uid ) { + m_cache.remove( uid ); +} +template <class T> +void OPimCache<T>::replace( const T& t) { + Item *it = m_cache.find( t.uid() ); + if ( it ) { + it->setRecord( t ); + } +} + +#endif diff --git a/libopie/pim/orecordlist.h b/libopie/pim/orecordlist.h index b6fa7fa..08f5c85 100644 --- a/libopie/pim/orecordlist.h +++ b/libopie/pim/orecordlist.h @@ -70,2 +70,3 @@ private: T m_record; + bool m_direction :1; @@ -127,2 +128,4 @@ ORecordListIterator<T>::ORecordListIterator() { m_record = T(); + /* forward */ + m_direction = TRUE; } @@ -141,2 +144,3 @@ ORecordListIterator<T>::ORecordListIterator( const ORecordListIterator<T>& it) { m_record = it.m_record; + m_direction = it.m_direction; } @@ -158,7 +162,5 @@ T ORecordListIterator<T>::operator*() { if (!m_end ) - /* FIXME - * until the cache is in place - * we do the uid match uid check - */ - m_record = m_temp->find( m_uids[m_current] ); + m_record = m_temp->find( m_uids[m_current], m_uids, m_current, + m_direction ? Base::Forward : + Base::Reverse ); else @@ -171,2 +173,3 @@ template <class T> ORecordListIterator<T> &ORecordListIterator<T>::operator++() { + m_direction = true; if (m_current < m_uids.count() ) { @@ -181,2 +184,3 @@ template <class T> ORecordListIterator<T> &ORecordListIterator<T>::operator--() { + m_direction = false; if ( m_current > 0 ) { @@ -209,3 +213,4 @@ ORecordListIterator<T>::ORecordListIterator( const QArray<int> uids, const Base* t ) - : m_uids( uids ), m_current( 0 ), m_temp( t ), m_end( false ) + : m_uids( uids ), m_current( 0 ), m_temp( t ), m_end( false ), + m_direction( false ) { @@ -256,3 +261,4 @@ template <class T> T ORecordList<T>::operator[]( uint i ) { - return m_acc->find( m_ids[i] ); + /* forward */ + return m_acc->find( m_ids[i], m_ids, i ); } diff --git a/libopie/pim/otemplatebase.h b/libopie/pim/otemplatebase.h index f71417b..b855919 100644 --- a/libopie/pim/otemplatebase.h +++ b/libopie/pim/otemplatebase.h @@ -3,2 +3,4 @@ +#include <qarray.h> + #include "opimrecord.h" @@ -11,2 +13,3 @@ class OTemplateBase { public: + enum CacheDirection { Forward=0, Reverse }; OTemplateBase() { @@ -17,2 +20,10 @@ public: + /** + * read ahead find + */ + virtual T find( int uid, const QArray<int>& items, + uint current, CacheDirection dir = Forward )const = 0; + virtual void cache( const T& )const = 0; + virtual void setSaneCacheSize( int ) = 0; + }; |