From 679d9fef2673eea18fe5d9c85df2b10b09a8a250 Mon Sep 17 00:00:00 2001 From: eilers Date: Sun, 29 Aug 2004 12:50:18 +0000 Subject: Added lookahead caching for addressbook (SQL-only) for speed improvement. --- (limited to 'libopie2/opiepim') diff --git a/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp b/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp index dda23cc..abfd944 100644 --- a/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp +++ b/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp @@ -300,28 +300,29 @@ namespace { FindQuery::~FindQuery() { } QString FindQuery::query()const{ -// if ( m_uids.count() == 0 ) - return single(); + if ( m_uids.count() == 0 ) + return single(); + else + return multi(); } - /* - else - return multi(); - } - QString FindQuery::multi()const { - QString qu = "select uid, type, value from addressbook where"; - for (uint i = 0; i < m_uids.count(); i++ ) { - qu += " UID = " + QString::number( m_uids[i] ) + " OR"; - } - qu.remove( qu.length()-2, 2 ); // Hmmmm.. - return qu; + + QString FindQuery::multi()const { + QString qu = "select * from addressbook where"; + for (uint i = 0; i < m_uids.count(); i++ ) { + qu += " uid = " + QString::number( m_uids[i] ) + " OR"; + } + qu.remove( qu.length()-2, 2 ); // Hmmmm.. + + odebug << "find query: " << qu << "" << oendl; + return qu; } - */ - QString FindQuery::single()const{ - QString qu = "select *"; - qu += " from addressbook where uid = " + QString::number(m_uid); - // owarn << "find query: " << qu << "" << oendl; - return qu; + QString FindQuery::single()const{ + QString qu = "select *"; + qu += " from addressbook where uid = " + QString::number(m_uid); + + // owarn << "find query: " << qu << "" << oendl; + return qu; } @@ -473,7 +474,7 @@ bool OPimContactAccessBackend_SQL::replace ( const OPimContact &contact ) OPimContact OPimContactAccessBackend_SQL::find ( int uid ) const { - odebug << "OPimContactAccessBackend_SQL::find()" << oendl; + odebug << "OPimContactAccessBackend_SQL::find(" << uid << ")" << oendl; QTime t; t.start(); @@ -484,6 +485,47 @@ OPimContact OPimContactAccessBackend_SQL::find ( int uid ) const return retContact; } +OPimContact OPimContactAccessBackend_SQL::find( int uid, const QArray& queryUids, uint current, Frontend::CacheDirection direction ) const +{ + odebug << "OPimContactAccessBackend_SQL::find( ..multi.. )" << oendl; + odebug << "searching for " << uid << "" << oendl; + + QTime t; + t.start(); + + uint numReadAhead = readAhead(); + QArray searchList( numReadAhead ); + + uint size =0; + + // Build an array with all elements which should be requested and cached + // We will just request "numReadAhead" elements, starting from "current" position in + // the list of many uids ! + switch( direction ) { + /* forward */ + case Frontend::Forward: + for ( uint i = current; i < queryUids.count() && size < numReadAhead; i++ ) { + searchList[size] = queryUids[i]; + size++; + } + break; + /* reverse */ + case Frontend::Reverse: + for ( uint i = current; i != 0 && size < numReadAhead; i-- ) { + searchList[size] = queryUids[i]; + size++; + } + break; + } + + //Shrink to real size.. + searchList.resize( size ); + + OPimContact retContact( requestContactsAndCache( uid, searchList ) ); + + odebug << "OPimContactAccessBackend_SQL::find( ..multi.. ) needed: " << t.elapsed() << " ms" << oendl; + return retContact; +} QArray OPimContactAccessBackend_SQL::queryByExample ( const OPimContact &query, int settings, const QDateTime& qd ) @@ -768,8 +810,6 @@ QMap OPimContactAccessBackend_SQL::requestNonCustom( int uid ) co QTime t; t.start(); - QMap nonCustomMap; - int t2needed = 0; int t3needed = 0; QTime t2; @@ -780,8 +820,67 @@ QMap OPimContactAccessBackend_SQL::requestNonCustom( int uid ) co OSQLResultItem resItem = res_noncustom.first(); + QMap nonCustomMap; QTime t3; t3.start(); + nonCustomMap = fillNonCustomMap( resItem ); + t3needed = t3.elapsed(); + + + // odebug << "Adding UID: " << resItem.data( "uid" ) << "" << oendl; + odebug << "RequestNonCustom needed: insg.:" << t.elapsed() << " ms, query: " << t2needed + << " ms, mapping: " << t3needed << " ms" << oendl; + + return nonCustomMap; +} + +/* Returns contact requested by uid and fills cache with contacts requested by uids in the cachelist */ +OPimContact OPimContactAccessBackend_SQL::requestContactsAndCache( int uid, const QArray& uidlist )const +{ + // We want to get all contacts with one query. + // We don't have to add the given uid to the uidlist, it is expected to be there already (see opimrecordlist.h). + // All contacts will be stored in the cache, afterwards the contact with the user id "uid" will be returned + // by using the cache.. + QArray cachelist = uidlist; + + odebug << "Reqest and cache" << cachelist.size() << "elements !" << oendl; + + QTime t; + t.start(); + + int t2needed = 0; + int t3needed = 0; + QTime t2; + t2.start(); + FindQuery query( cachelist ); + OSQLResult res_noncustom = m_driver->query( &query ); + t2needed = t2.elapsed(); + + QMap nonCustomMap; + QTime t3; + t3.start(); + OSQLResultItem resItem = res_noncustom.first(); + do { + OPimContact contact( fillNonCustomMap( resItem ) ); + contact.setExtraMap( requestCustom( contact.uid() ) ); + odebug << "Caching uid: " << contact.uid() << oendl; + cache( contact ); + resItem = res_noncustom.next(); + } while ( ! res_noncustom.atEnd() ); //atEnd() is true if we are past(!) the list !! + t3needed = t3.elapsed(); + + + // odebug << "Adding UID: " << resItem.data( "uid" ) << "" << oendl; + odebug << "RequestContactsAndCache needed: insg.:" << t.elapsed() << " ms, query: " << t2needed + << " ms, mapping: " << t3needed << " ms" << oendl; + + return cacheFind( uid ); +} + +QMap OPimContactAccessBackend_SQL::fillNonCustomMap( const OSQLResultItem& resultItem ) const +{ + QMap nonCustomMap; + // Now loop through all columns QStringList fieldList = OPimContactFields::untrfields( false ); QMap translate = OPimContactFields::untrFieldsToId(); @@ -790,7 +889,7 @@ QMap OPimContactAccessBackend_SQL::requestNonCustom( int uid ) co // corresponding id into the map.. int id = translate[*it]; - QString value = resItem.data( (*it) ); + QString value = resultItem.data( (*it) ); // odebug << "Reading " << (*it) << "... found: " << value << "" << oendl; @@ -816,17 +915,12 @@ QMap OPimContactAccessBackend_SQL::requestNonCustom( int uid ) co } } - // First insert uid - nonCustomMap.insert( Qtopia::AddressUid, resItem.data( "uid" ) ); - t3needed = t3.elapsed(); - - // odebug << "Adding UID: " << resItem.data( "uid" ) << "" << oendl; - odebug << "RequestNonCustom needed: insg.:" << t.elapsed() << " ms, query: " << t2needed - << " ms, mapping: " << t3needed << " ms" << oendl; + nonCustomMap.insert( Qtopia::AddressUid, resultItem.data( "uid" ) ); return nonCustomMap; } + QMap OPimContactAccessBackend_SQL::requestCustom( int uid ) const { QTime t; diff --git a/libopie2/opiepim/backend/ocontactaccessbackend_sql.h b/libopie2/opiepim/backend/ocontactaccessbackend_sql.h index ba122ec..4f81735 100644 --- a/libopie2/opiepim/backend/ocontactaccessbackend_sql.h +++ b/libopie2/opiepim/backend/ocontactaccessbackend_sql.h @@ -73,8 +73,7 @@ class OPimContactAccessBackend_SQL : public OPimContactAccessBackend { QArray allRecords() const; OPimContact find ( int uid ) const; - // FIXME: Add lookahead-cache support ! - //OPimContact find(int uid, const QArray&, uint cur, Frontend::CacheDirection )const; + OPimContact find( int uid, const QArray&, uint cur, Frontend::CacheDirection ) const; QArray queryByExample ( const OPimContact &query, int settings, const QDateTime& d ); @@ -98,6 +97,8 @@ class OPimContactAccessBackend_SQL : public OPimContactAccessBackend { QArray extractUids( Opie::DB::OSQLResult& res ) const; QMap requestNonCustom( int uid ) const; QMap requestCustom( int uid ) const; + QMap fillNonCustomMap( const Opie::DB::OSQLResultItem& resultItem ) const; + OPimContact requestContactsAndCache( int uid, const QArray& cachelist ) const; void update(); protected: diff --git a/libopie2/opiepim/backend/opimaccessbackend.h b/libopie2/opiepim/backend/opimaccessbackend.h index 0682063..15a7b7f 100644 --- a/libopie2/opiepim/backend/opimaccessbackend.h +++ b/libopie2/opiepim/backend/opimaccessbackend.h @@ -96,7 +96,7 @@ public: virtual T find(int uid )const = 0; virtual T find(int uid, const QArray& items, - uint current, typename Frontend::CacheDirection )const ; + uint current, typename Frontend::CacheDirection ) const; /** * clear the back end */ @@ -128,8 +128,16 @@ public: void setReadAhead( uint count ); protected: int access()const; + void cache( const T& t )const; + /** + * Returns the element with given uid out of the cache. + * Returns empty element if nothing was found. + * Attention: This just works if we have a frontend which contains the cache ! + */ + T cacheFind( int uid ) const; + /** * use a prime number here! */ @@ -161,17 +169,29 @@ void OPimAccessBackend::setFrontend( Frontend* fr ) { } template void OPimAccessBackend::cache( const T& t )const { - if (m_front ) + if ( m_front ) m_front->cache( t ); } + +template +T OPimAccessBackend::cacheFind( int uid )const { + if ( ! m_front ){ + qWarning ( "No frontend assigned ! Therefore we cannot access the cache to return the right element!" ); + return T(); + } + + return m_front->cacheFind( uid ); +} + template void OPimAccessBackend::setSaneCacheSize( int size) { - if (m_front ) + if ( m_front ) m_front->setSaneCacheSize( size ); } template T OPimAccessBackend::find( int uid, const QArray&, - uint, typename Frontend::CacheDirection )const { + uint, typename Frontend::CacheDirection ) const{ + qDebug( "*** Lookahead feature not supported. Fallback to default find!" ); return find( uid ); } template diff --git a/libopie2/opiepim/core/opimaccesstemplate.h b/libopie2/opiepim/core/opimaccesstemplate.h index 55d600a..6f01b46 100644 --- a/libopie2/opiepim/core/opimaccesstemplate.h +++ b/libopie2/opiepim/core/opimaccesstemplate.h @@ -35,7 +35,6 @@ #include #include -#include #include #include @@ -128,6 +127,7 @@ public: virtual T find( int uid, const QArray&, uint current, typename OTemplateBase::CacheDirection dir = OTemplateBase::Forward )const; + /* invalidate cache here */ /** * clears the backend and invalidates the backend @@ -139,13 +139,15 @@ public: * @param t The item to add. * @return true if added successfully. */ - virtual bool add( const T& t ) ; + virtual bool add( const T& t ) ; + bool add( const OPimRecord& ); - // Needed for real generic access (eilers) - // Info: Take this if you are working with OPimRecord, which is a generic base class, and - // you need to add it into any database, you cannot generate a reference to - // it and casting may be not approriate, too. - // But take care that the accessing database is compatible to the real type of OPimRecord !! + /** + * Add an Opie PimRecord. + * Info: Take this if you are working with OPimRecords and you need to add it into any database. + * But take care that the accessing database is compatible to the real type of OPimRecord !! + * Otherwise this access will be rejected ! + */ bool add( const OPimRecord* ); @@ -176,6 +178,7 @@ public: /** * @internal */ + virtual T cacheFind( int uid )const; void cache( const T& )const; void setSaneCacheSize( int ); @@ -192,6 +195,7 @@ protected: */ BackEnd* backEnd(); BackEnd* m_backEnd; + Cache m_cache; private: @@ -218,7 +222,7 @@ bool OPimAccessTemplate::load() { } template bool OPimAccessTemplate::reload() { - invalidateCache(); // zecke: I think this should be added (se) + invalidateCache(); return m_backEnd->reload(); } template @@ -251,10 +255,22 @@ OPimAccessTemplate::queryByExample( const T& t, int settings, const QDateTime } template T OPimAccessTemplate::find( int uid ) const{ + // First search in cache.. + if ( m_cache.contains( uid ) ) + return m_cache.find( uid ); + T t = m_backEnd->find( uid ); cache( t ); + return t; } + +template +T OPimAccessTemplate::cacheFind( int uid ) const +{ + return m_cache.find( uid ); +} + template T OPimAccessTemplate::find( int uid, const QArray& ar, uint current, typename OTemplateBase::CacheDirection dir )const { @@ -264,7 +280,7 @@ T OPimAccessTemplate::find( int uid, const QArray& ar, * avoid two finds in QCache... */ // owarn << "find it now " << uid << oendl; - if (m_cache.contains( uid ) ) { + if ( m_cache.contains( uid ) ) { return m_cache.find( uid ); } @@ -284,12 +300,14 @@ bool OPimAccessTemplate::add( const T& t ) { } template -bool OPimAccessTemplate::add( const OPimRecord& rec) { +bool OPimAccessTemplate::add( const OPimRecord& rec ) { /* same type */ T tempInstance; if ( rec.rtti() == tempInstance.rtti() ) { const T& t = static_cast(rec); return add(t); + } else { + owarn << "Adding not possible: Objecttype mismatch" << oendl; } return false; } @@ -301,6 +319,8 @@ bool OPimAccessTemplate::add( const OPimRecord* rec) { if ( rec -> rtti() == tempInstance.rtti() ) { const T* t = static_cast(rec); return add( *t ); + } else { + owarn << "Adding not possible: Objecttype mismatch" << oendl; } return false; } diff --git a/libopie2/opiepim/core/opimtemplatebase.h b/libopie2/opiepim/core/opimtemplatebase.h index b48dfed..ec9a94e 100644 --- a/libopie2/opiepim/core/opimtemplatebase.h +++ b/libopie2/opiepim/core/opimtemplatebase.h @@ -31,6 +31,7 @@ /* OPIE */ #include +#include /* QT */ #include @@ -70,13 +71,15 @@ private: }; /** * internal template base - * T needs to implement the copy c'tor!!! + * Attention: T needs to implement the copy c'tor!!! */ class OTemplateBasePrivate; template class OTemplateBase : public OPimBase { public: + /** Look ahead direction of cache */ enum CacheDirection { Forward=0, Reverse }; + OTemplateBase() { }; virtual ~OTemplateBase() { @@ -88,12 +91,23 @@ public: */ virtual T find( int uid, const QArray& items, uint current, CacheDirection dir = Forward )const = 0; + + /** + * Find in Cache.. + * Returns empty object if nothing found. + */ + virtual T cacheFind( int uid )const = 0; + + /** + * Put element into Cache + */ virtual void cache( const T& )const = 0; virtual void setSaneCacheSize( int ) = 0; OPimRecord* record()const; OPimRecord* record(int uid )const; static T* rec(); + private: OTemplateBasePrivate *d; -- cgit v0.9.0.2