summaryrefslogtreecommitdiff
path: root/libopie2/opiepim
authoreilers <eilers>2004-08-29 12:50:18 (UTC)
committer eilers <eilers>2004-08-29 12:50:18 (UTC)
commit679d9fef2673eea18fe5d9c85df2b10b09a8a250 (patch) (unidiff)
tree8b3d6dbef45568be6f5daac31094d6e599fdefdf /libopie2/opiepim
parent45327ef3c0f093fc227682f79158632afc09e6d1 (diff)
downloadopie-679d9fef2673eea18fe5d9c85df2b10b09a8a250.zip
opie-679d9fef2673eea18fe5d9c85df2b10b09a8a250.tar.gz
opie-679d9fef2673eea18fe5d9c85df2b10b09a8a250.tar.bz2
Added lookahead caching for addressbook (SQL-only) for speed improvement.
Diffstat (limited to 'libopie2/opiepim') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp154
-rw-r--r--libopie2/opiepim/backend/ocontactaccessbackend_sql.h5
-rw-r--r--libopie2/opiepim/backend/opimaccessbackend.h28
-rw-r--r--libopie2/opiepim/core/opimaccesstemplate.h40
-rw-r--r--libopie2/opiepim/core/opimtemplatebase.h16
5 files changed, 196 insertions, 47 deletions
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
@@ -302,24 +302,25 @@ namespace {
302 QString FindQuery::query()const{ 302 QString FindQuery::query()const{
303// if ( m_uids.count() == 0 ) 303 if ( m_uids.count() == 0 )
304 return single(); 304 return single();
305 else
306 return multi();
305 } 307 }
306 /* 308
307 else 309 QString FindQuery::multi()const {
308 return multi(); 310 QString qu = "select * from addressbook where";
309 } 311 for (uint i = 0; i < m_uids.count(); i++ ) {
310 QString FindQuery::multi()const { 312 qu += " uid = " + QString::number( m_uids[i] ) + " OR";
311 QString qu = "select uid, type, value from addressbook where"; 313 }
312 for (uint i = 0; i < m_uids.count(); i++ ) { 314 qu.remove( qu.length()-2, 2 ); // Hmmmm..
313 qu += " UID = " + QString::number( m_uids[i] ) + " OR"; 315
314 } 316 odebug << "find query: " << qu << "" << oendl;
315 qu.remove( qu.length()-2, 2 ); // Hmmmm.. 317 return qu;
316 return qu;
317 } 318 }
318 */
319 QString FindQuery::single()const{
320 QString qu = "select *";
321 qu += " from addressbook where uid = " + QString::number(m_uid);
322 319
323 // owarn << "find query: " << qu << "" << oendl; 320 QString FindQuery::single()const{
324 return qu; 321 QString qu = "select *";
322 qu += " from addressbook where uid = " + QString::number(m_uid);
323
324 // owarn << "find query: " << qu << "" << oendl;
325 return qu;
325 } 326 }
@@ -475,3 +476,3 @@ OPimContact OPimContactAccessBackend_SQL::find ( int uid ) const
475{ 476{
476 odebug << "OPimContactAccessBackend_SQL::find()" << oendl; 477 odebug << "OPimContactAccessBackend_SQL::find(" << uid << ")" << oendl;
477 QTime t; 478 QTime t;
@@ -486,2 +487,43 @@ OPimContact OPimContactAccessBackend_SQL::find ( int uid ) const
486 487
488OPimContact OPimContactAccessBackend_SQL::find( int uid, const QArray<int>& queryUids, uint current, Frontend::CacheDirection direction ) const
489{
490 odebug << "OPimContactAccessBackend_SQL::find( ..multi.. )" << oendl;
491 odebug << "searching for " << uid << "" << oendl;
492
493 QTime t;
494 t.start();
495
496 uint numReadAhead = readAhead();
497 QArray<int> searchList( numReadAhead );
498
499 uint size =0;
500
501 // Build an array with all elements which should be requested and cached
502 // We will just request "numReadAhead" elements, starting from "current" position in
503 // the list of many uids !
504 switch( direction ) {
505 /* forward */
506 case Frontend::Forward:
507 for ( uint i = current; i < queryUids.count() && size < numReadAhead; i++ ) {
508 searchList[size] = queryUids[i];
509 size++;
510 }
511 break;
512 /* reverse */
513 case Frontend::Reverse:
514 for ( uint i = current; i != 0 && size < numReadAhead; i-- ) {
515 searchList[size] = queryUids[i];
516 size++;
517 }
518 break;
519 }
520
521 //Shrink to real size..
522 searchList.resize( size );
523
524 OPimContact retContact( requestContactsAndCache( uid, searchList ) );
525
526 odebug << "OPimContactAccessBackend_SQL::find( ..multi.. ) needed: " << t.elapsed() << " ms" << oendl;
527 return retContact;
528}
487 529
@@ -770,4 +812,2 @@ QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) co
770 812
771 QMap<int, QString> nonCustomMap;
772
773 int t2needed = 0; 813 int t2needed = 0;
@@ -782,4 +822,63 @@ QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) co
782 822
823 QMap<int, QString> nonCustomMap;
783 QTime t3; 824 QTime t3;
784 t3.start(); 825 t3.start();
826 nonCustomMap = fillNonCustomMap( resItem );
827 t3needed = t3.elapsed();
828
829
830 // odebug << "Adding UID: " << resItem.data( "uid" ) << "" << oendl;
831 odebug << "RequestNonCustom needed: insg.:" << t.elapsed() << " ms, query: " << t2needed
832 << " ms, mapping: " << t3needed << " ms" << oendl;
833
834 return nonCustomMap;
835}
836
837/* Returns contact requested by uid and fills cache with contacts requested by uids in the cachelist */
838OPimContact OPimContactAccessBackend_SQL::requestContactsAndCache( int uid, const QArray<int>& uidlist )const
839{
840 // We want to get all contacts with one query.
841 // We don't have to add the given uid to the uidlist, it is expected to be there already (see opimrecordlist.h).
842 // All contacts will be stored in the cache, afterwards the contact with the user id "uid" will be returned
843 // by using the cache..
844 QArray<int> cachelist = uidlist;
845
846 odebug << "Reqest and cache" << cachelist.size() << "elements !" << oendl;
847
848 QTime t;
849 t.start();
850
851 int t2needed = 0;
852 int t3needed = 0;
853 QTime t2;
854 t2.start();
855 FindQuery query( cachelist );
856 OSQLResult res_noncustom = m_driver->query( &query );
857 t2needed = t2.elapsed();
858
859 QMap<int, QString> nonCustomMap;
860 QTime t3;
861 t3.start();
862 OSQLResultItem resItem = res_noncustom.first();
863 do {
864 OPimContact contact( fillNonCustomMap( resItem ) );
865 contact.setExtraMap( requestCustom( contact.uid() ) );
866 odebug << "Caching uid: " << contact.uid() << oendl;
867 cache( contact );
868 resItem = res_noncustom.next();
869 } while ( ! res_noncustom.atEnd() ); //atEnd() is true if we are past(!) the list !!
870 t3needed = t3.elapsed();
871
872
873 // odebug << "Adding UID: " << resItem.data( "uid" ) << "" << oendl;
874 odebug << "RequestContactsAndCache needed: insg.:" << t.elapsed() << " ms, query: " << t2needed
875 << " ms, mapping: " << t3needed << " ms" << oendl;
876
877 return cacheFind( uid );
878}
879
880QMap<int, QString> OPimContactAccessBackend_SQL::fillNonCustomMap( const OSQLResultItem& resultItem ) const
881{
882 QMap<int, QString> nonCustomMap;
883
785 // Now loop through all columns 884 // Now loop through all columns
@@ -792,3 +891,3 @@ QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) co
792 int id = translate[*it]; 891 int id = translate[*it];
793 QString value = resItem.data( (*it) ); 892 QString value = resultItem.data( (*it) );
794 893
@@ -818,9 +917,3 @@ QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) co
818 917
819 // First insert uid 918 nonCustomMap.insert( Qtopia::AddressUid, resultItem.data( "uid" ) );
820 nonCustomMap.insert( Qtopia::AddressUid, resItem.data( "uid" ) );
821 t3needed = t3.elapsed();
822
823 // odebug << "Adding UID: " << resItem.data( "uid" ) << "" << oendl;
824 odebug << "RequestNonCustom needed: insg.:" << t.elapsed() << " ms, query: " << t2needed
825 << " ms, mapping: " << t3needed << " ms" << oendl;
826 919
@@ -829,2 +922,3 @@ QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) co
829 922
923
830QMap<QString, QString> OPimContactAccessBackend_SQL::requestCustom( int uid ) const 924QMap<QString, QString> OPimContactAccessBackend_SQL::requestCustom( int uid ) const
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
@@ -75,4 +75,3 @@ class OPimContactAccessBackend_SQL : public OPimContactAccessBackend {
75 OPimContact find ( int uid ) const; 75 OPimContact find ( int uid ) const;
76 // FIXME: Add lookahead-cache support ! 76 OPimContact find( int uid, const QArray<int>&, uint cur, Frontend::CacheDirection ) const;
77 //OPimContact find(int uid, const QArray<int>&, uint cur, Frontend::CacheDirection )const;
78 77
@@ -100,2 +99,4 @@ class OPimContactAccessBackend_SQL : public OPimContactAccessBackend {
100 QMap<QString, QString> requestCustom( int uid ) const; 99 QMap<QString, QString> requestCustom( int uid ) const;
100 QMap<int, QString> fillNonCustomMap( const Opie::DB::OSQLResultItem& resultItem ) const;
101 OPimContact requestContactsAndCache( int uid, const QArray<int>& cachelist ) const;
101 void update(); 102 void update();
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
@@ -98,3 +98,3 @@ public:
98 virtual T find(int uid, const QArray<int>& items, 98 virtual T find(int uid, const QArray<int>& items,
99 uint current, typename Frontend::CacheDirection )const ; 99 uint current, typename Frontend::CacheDirection ) const;
100 /** 100 /**
@@ -130,4 +130,12 @@ protected:
130 int access()const; 130 int access()const;
131
131 void cache( const T& t )const; 132 void cache( const T& t )const;
132 133
134 /**
135 * Returns the element with given uid out of the cache.
136 * Returns empty element if nothing was found.
137 * <b>Attention:</b> This just works if we have a frontend which contains the cache !
138 */
139 T cacheFind( int uid ) const;
140
133 /** 141 /**
@@ -163,8 +171,19 @@ template <class T>
163void OPimAccessBackend<T>::cache( const T& t )const { 171void OPimAccessBackend<T>::cache( const T& t )const {
164 if (m_front ) 172 if ( m_front )
165 m_front->cache( t ); 173 m_front->cache( t );
166} 174}
175
176template <class T>
177T OPimAccessBackend<T>::cacheFind( int uid )const {
178 if ( ! m_front ){
179 qWarning ( "No frontend assigned ! Therefore we cannot access the cache to return the right element!" );
180 return T();
181 }
182
183 return m_front->cacheFind( uid );
184}
185
167template <class T> 186template <class T>
168void OPimAccessBackend<T>::setSaneCacheSize( int size) { 187void OPimAccessBackend<T>::setSaneCacheSize( int size) {
169 if (m_front ) 188 if ( m_front )
170 m_front->setSaneCacheSize( size ); 189 m_front->setSaneCacheSize( size );
@@ -173,3 +192,4 @@ template <class T>
173T OPimAccessBackend<T>::find( int uid, const QArray<int>&, 192T OPimAccessBackend<T>::find( int uid, const QArray<int>&,
174 uint, typename Frontend::CacheDirection )const { 193 uint, typename Frontend::CacheDirection ) const{
194 qDebug( "*** Lookahead feature not supported. Fallback to default find!" );
175 return find( uid ); 195 return find( uid );
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
@@ -37,3 +37,2 @@
37 37
38#include <opie2/opimcache.h>
39#include <opie2/opimtemplatebase.h> 38#include <opie2/opimtemplatebase.h>
@@ -130,2 +129,3 @@ public:
130 129
130
131 /* invalidate cache here */ 131 /* invalidate cache here */
@@ -141,9 +141,11 @@ public:
141 */ 141 */
142 virtual bool add( const T& t ) ; 142 virtual bool add( const T& t ) ;
143
143 bool add( const OPimRecord& ); 144 bool add( const OPimRecord& );
144 // Needed for real generic access (eilers) 145 /**
145 // Info: Take this if you are working with OPimRecord, which is a generic base class, and 146 * Add an Opie PimRecord.
146 // you need to add it into any database, you cannot generate a reference to 147 * Info: Take this if you are working with OPimRecords and you need to add it into any database.
147 // it and casting may be not approriate, too. 148 * But take care that the accessing database is compatible to the real type of OPimRecord !!
148 // But take care that the accessing database is compatible to the real type of OPimRecord !! 149 * Otherwise this access will be rejected !
150 */
149 bool add( const OPimRecord* ); 151 bool add( const OPimRecord* );
@@ -178,2 +180,3 @@ public:
178 */ 180 */
181 virtual T cacheFind( int uid )const;
179 void cache( const T& )const; 182 void cache( const T& )const;
@@ -194,2 +197,3 @@ protected:
194 BackEnd* m_backEnd; 197 BackEnd* m_backEnd;
198
195 Cache m_cache; 199 Cache m_cache;
@@ -220,3 +224,3 @@ template <class T>
220bool OPimAccessTemplate<T>::reload() { 224bool OPimAccessTemplate<T>::reload() {
221 invalidateCache(); // zecke: I think this should be added (se) 225 invalidateCache();
222 return m_backEnd->reload(); 226 return m_backEnd->reload();
@@ -253,6 +257,18 @@ template <class T>
253T OPimAccessTemplate<T>::find( int uid ) const{ 257T OPimAccessTemplate<T>::find( int uid ) const{
258 // First search in cache..
259 if ( m_cache.contains( uid ) )
260 return m_cache.find( uid );
261
254 T t = m_backEnd->find( uid ); 262 T t = m_backEnd->find( uid );
255 cache( t ); 263 cache( t );
264
256 return t; 265 return t;
257} 266}
267
268template <class T>
269T OPimAccessTemplate<T>::cacheFind( int uid ) const
270{
271 return m_cache.find( uid );
272}
273
258template <class T> 274template <class T>
@@ -266,3 +282,3 @@ T OPimAccessTemplate<T>::find( int uid, const QArray<int>& ar,
266 // owarn << "find it now " << uid << oendl; 282 // owarn << "find it now " << uid << oendl;
267 if (m_cache.contains( uid ) ) { 283 if ( m_cache.contains( uid ) ) {
268 return m_cache.find( uid ); 284 return m_cache.find( uid );
@@ -286,3 +302,3 @@ bool OPimAccessTemplate<T>::add( const T& t ) {
286template <class T> 302template <class T>
287bool OPimAccessTemplate<T>::add( const OPimRecord& rec) { 303bool OPimAccessTemplate<T>::add( const OPimRecord& rec ) {
288 /* same type */ 304 /* same type */
@@ -292,2 +308,4 @@ bool OPimAccessTemplate<T>::add( const OPimRecord& rec) {
292 return add(t); 308 return add(t);
309 } else {
310 owarn << "Adding not possible: Objecttype mismatch" << oendl;
293 } 311 }
@@ -303,2 +321,4 @@ bool OPimAccessTemplate<T>::add( const OPimRecord* rec) {
303 return add( *t ); 321 return add( *t );
322 } else {
323 owarn << "Adding not possible: Objecttype mismatch" << oendl;
304 } 324 }
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
@@ -33,2 +33,3 @@
33#include <opie2/opimrecord.h> 33#include <opie2/opimrecord.h>
34#include <opie2/opimcache.h>
34 35
@@ -72,3 +73,3 @@ private:
72 * internal template base 73 * internal template base
73 * T needs to implement the copy c'tor!!! 74 * Attention: T needs to implement the copy c'tor!!!
74 */ 75 */
@@ -78,3 +79,5 @@ class OTemplateBase : public OPimBase {
78public: 79public:
80 /** Look ahead direction of cache */
79 enum CacheDirection { Forward=0, Reverse }; 81 enum CacheDirection { Forward=0, Reverse };
82
80 OTemplateBase() { 83 OTemplateBase() {
@@ -90,2 +93,12 @@ public:
90 uint current, CacheDirection dir = Forward )const = 0; 93 uint current, CacheDirection dir = Forward )const = 0;
94
95 /**
96 * Find in Cache..
97 * Returns empty object if nothing found.
98 */
99 virtual T cacheFind( int uid )const = 0;
100
101 /**
102 * Put element into Cache
103 */
91 virtual void cache( const T& )const = 0; 104 virtual void cache( const T& )const = 0;
@@ -96,2 +109,3 @@ public:
96 static T* rec(); 109 static T* rec();
110
97 111