author | eilers <eilers> | 2004-08-29 12:50:18 (UTC) |
---|---|---|
committer | eilers <eilers> | 2004-08-29 12:50:18 (UTC) |
commit | 679d9fef2673eea18fe5d9c85df2b10b09a8a250 (patch) (unidiff) | |
tree | 8b3d6dbef45568be6f5daac31094d6e599fdefdf | |
parent | 45327ef3c0f093fc227682f79158632afc09e6d1 (diff) | |
download | opie-679d9fef2673eea18fe5d9c85df2b10b09a8a250.zip opie-679d9fef2673eea18fe5d9c85df2b10b09a8a250.tar.gz opie-679d9fef2673eea18fe5d9c85df2b10b09a8a250.tar.bz2 |
Added lookahead caching for addressbook (SQL-only) for speed improvement.
-rw-r--r-- | libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp | 154 | ||||
-rw-r--r-- | libopie2/opiepim/backend/ocontactaccessbackend_sql.h | 5 | ||||
-rw-r--r-- | libopie2/opiepim/backend/opimaccessbackend.h | 28 | ||||
-rw-r--r-- | libopie2/opiepim/core/opimaccesstemplate.h | 40 | ||||
-rw-r--r-- | libopie2/opiepim/core/opimtemplatebase.h | 16 |
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 | |||
@@ -297,34 +297,35 @@ namespace { | |||
297 | FindQuery::FindQuery(const QArray<int>& ints) | 297 | FindQuery::FindQuery(const QArray<int>& ints) |
298 | : OSQLQuery(), m_uids( ints ){ | 298 | : OSQLQuery(), m_uids( ints ){ |
299 | } | 299 | } |
300 | FindQuery::~FindQuery() { | 300 | FindQuery::~FindQuery() { |
301 | } | 301 | } |
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 | } |
326 | 327 | ||
327 | 328 | ||
328 | FindCustomQuery::FindCustomQuery(int uid) | 329 | FindCustomQuery::FindCustomQuery(int uid) |
329 | : OSQLQuery(), m_uid( uid ) { | 330 | : OSQLQuery(), m_uid( uid ) { |
330 | } | 331 | } |
@@ -470,23 +471,64 @@ bool OPimContactAccessBackend_SQL::replace ( const OPimContact &contact ) | |||
470 | return add( contact ); | 471 | return add( contact ); |
471 | } | 472 | } |
472 | 473 | ||
473 | 474 | ||
474 | OPimContact OPimContactAccessBackend_SQL::find ( int uid ) const | 475 | 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; |
478 | t.start(); | 479 | t.start(); |
479 | 480 | ||
480 | OPimContact retContact( requestNonCustom( uid ) ); | 481 | OPimContact retContact( requestNonCustom( uid ) ); |
481 | retContact.setExtraMap( requestCustom( uid ) ); | 482 | retContact.setExtraMap( requestCustom( uid ) ); |
482 | 483 | ||
483 | odebug << "OPimContactAccessBackend_SQL::find() needed: " << t.elapsed() << " ms" << oendl; | 484 | odebug << "OPimContactAccessBackend_SQL::find() needed: " << t.elapsed() << " ms" << oendl; |
484 | return retContact; | 485 | return retContact; |
485 | } | 486 | } |
486 | 487 | ||
488 | OPimContact 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 | ||
488 | 530 | ||
489 | QArray<int> OPimContactAccessBackend_SQL::queryByExample ( const OPimContact &query, int settings, const QDateTime& qd ) | 531 | QArray<int> OPimContactAccessBackend_SQL::queryByExample ( const OPimContact &query, int settings, const QDateTime& qd ) |
490 | { | 532 | { |
491 | QString qu = "SELECT uid FROM addressbook WHERE"; | 533 | QString qu = "SELECT uid FROM addressbook WHERE"; |
492 | QString searchQuery =""; | 534 | QString searchQuery =""; |
@@ -765,35 +807,92 @@ QArray<int> OPimContactAccessBackend_SQL::extractUids( OSQLResult& res ) const | |||
765 | 807 | ||
766 | QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) const | 808 | QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) const |
767 | { | 809 | { |
768 | QTime t; | 810 | QTime t; |
769 | t.start(); | 811 | t.start(); |
770 | 812 | ||
771 | QMap<int, QString> nonCustomMap; | ||
772 | |||
773 | int t2needed = 0; | 813 | int t2needed = 0; |
774 | int t3needed = 0; | 814 | int t3needed = 0; |
775 | QTime t2; | 815 | QTime t2; |
776 | t2.start(); | 816 | t2.start(); |
777 | FindQuery query( uid ); | 817 | FindQuery query( uid ); |
778 | OSQLResult res_noncustom = m_driver->query( &query ); | 818 | OSQLResult res_noncustom = m_driver->query( &query ); |
779 | t2needed = t2.elapsed(); | 819 | t2needed = t2.elapsed(); |
780 | 820 | ||
781 | OSQLResultItem resItem = res_noncustom.first(); | 821 | OSQLResultItem resItem = res_noncustom.first(); |
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 */ | ||
838 | OPimContact 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 | |||
880 | QMap<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 |
786 | QStringList fieldList = OPimContactFields::untrfields( false ); | 885 | QStringList fieldList = OPimContactFields::untrfields( false ); |
787 | QMap<QString, int> translate = OPimContactFields::untrFieldsToId(); | 886 | QMap<QString, int> translate = OPimContactFields::untrFieldsToId(); |
788 | for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){ | 887 | for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){ |
789 | // Get data for the selected column and store it with the | 888 | // Get data for the selected column and store it with the |
790 | // corresponding id into the map.. | 889 | // corresponding id into the map.. |
791 | 890 | ||
792 | int id = translate[*it]; | 891 | int id = translate[*it]; |
793 | QString value = resItem.data( (*it) ); | 892 | QString value = resultItem.data( (*it) ); |
794 | 893 | ||
795 | // odebug << "Reading " << (*it) << "... found: " << value << "" << oendl; | 894 | // odebug << "Reading " << (*it) << "... found: " << value << "" << oendl; |
796 | 895 | ||
797 | switch( id ){ | 896 | switch( id ){ |
798 | case Qtopia::Birthday: | 897 | case Qtopia::Birthday: |
799 | case Qtopia::Anniversary:{ | 898 | case Qtopia::Anniversary:{ |
@@ -813,23 +912,18 @@ QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) co | |||
813 | odebug << "Category is: " << value << "" << oendl; | 912 | odebug << "Category is: " << value << "" << oendl; |
814 | default: | 913 | default: |
815 | nonCustomMap.insert( id, value ); | 914 | nonCustomMap.insert( id, value ); |
816 | } | 915 | } |
817 | } | 916 | } |
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 | ||
827 | return nonCustomMap; | 920 | return nonCustomMap; |
828 | } | 921 | } |
829 | 922 | ||
923 | |||
830 | QMap<QString, QString> OPimContactAccessBackend_SQL::requestCustom( int uid ) const | 924 | QMap<QString, QString> OPimContactAccessBackend_SQL::requestCustom( int uid ) const |
831 | { | 925 | { |
832 | QTime t; | 926 | QTime t; |
833 | t.start(); | 927 | t.start(); |
834 | 928 | ||
835 | QMap<QString, QString> customMap; | 929 | QMap<QString, QString> customMap; |
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 | |||
@@ -70,14 +70,13 @@ class OPimContactAccessBackend_SQL : public OPimContactAccessBackend { | |||
70 | 70 | ||
71 | bool wasChangedExternally(); | 71 | bool wasChangedExternally(); |
72 | 72 | ||
73 | QArray<int> allRecords() const; | 73 | QArray<int> allRecords() const; |
74 | 74 | ||
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 | ||
79 | QArray<int> queryByExample ( const OPimContact &query, int settings, | 78 | QArray<int> queryByExample ( const OPimContact &query, int settings, |
80 | const QDateTime& d ); | 79 | const QDateTime& d ); |
81 | 80 | ||
82 | QArray<int> matchRegexp( const QRegExp &r ) const; | 81 | QArray<int> matchRegexp( const QRegExp &r ) const; |
83 | 82 | ||
@@ -95,12 +94,14 @@ class OPimContactAccessBackend_SQL : public OPimContactAccessBackend { | |||
95 | bool reload(); | 94 | bool reload(); |
96 | 95 | ||
97 | private: | 96 | private: |
98 | QArray<int> extractUids( Opie::DB::OSQLResult& res ) const; | 97 | QArray<int> extractUids( Opie::DB::OSQLResult& res ) const; |
99 | QMap<int, QString> requestNonCustom( int uid ) const; | 98 | QMap<int, QString> requestNonCustom( int uid ) const; |
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(); |
102 | 103 | ||
103 | protected: | 104 | protected: |
104 | bool m_changed; | 105 | bool m_changed; |
105 | QString m_fileName; | 106 | QString m_fileName; |
106 | QArray<int> m_uids; | 107 | QArray<int> m_uids; |
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 | |||
@@ -93,13 +93,13 @@ public: | |||
93 | * find the OPimRecord with uid @param uid | 93 | * find the OPimRecord with uid @param uid |
94 | * returns T and T.isEmpty() if nothing was found | 94 | * returns T and T.isEmpty() if nothing was found |
95 | */ | 95 | */ |
96 | virtual T find(int uid )const = 0; | 96 | virtual T find(int uid )const = 0; |
97 | 97 | ||
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 | /** |
101 | * clear the back end | 101 | * clear the back end |
102 | */ | 102 | */ |
103 | virtual void clear() = 0; | 103 | virtual void clear() = 0; |
104 | 104 | ||
105 | /** | 105 | /** |
@@ -125,14 +125,22 @@ public: | |||
125 | /** | 125 | /** |
126 | * set the read ahead count | 126 | * set the read ahead count |
127 | */ | 127 | */ |
128 | void setReadAhead( uint count ); | 128 | void setReadAhead( uint count ); |
129 | protected: | 129 | 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 | /** |
134 | * use a prime number here! | 142 | * use a prime number here! |
135 | */ | 143 | */ |
136 | void setSaneCacheSize( int ); | 144 | void setSaneCacheSize( int ); |
137 | 145 | ||
138 | uint readAhead()const; | 146 | uint readAhead()const; |
@@ -158,23 +166,35 @@ OPimAccessBackend<T>::~OPimAccessBackend() { | |||
158 | template <class T> | 166 | template <class T> |
159 | void OPimAccessBackend<T>::setFrontend( Frontend* fr ) { | 167 | void OPimAccessBackend<T>::setFrontend( Frontend* fr ) { |
160 | m_front = fr; | 168 | m_front = fr; |
161 | } | 169 | } |
162 | template <class T> | 170 | template <class T> |
163 | void OPimAccessBackend<T>::cache( const T& t )const { | 171 | void 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 | |||
176 | template <class T> | ||
177 | T 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 | |||
167 | template <class T> | 186 | template <class T> |
168 | void OPimAccessBackend<T>::setSaneCacheSize( int size) { | 187 | void OPimAccessBackend<T>::setSaneCacheSize( int size) { |
169 | if (m_front ) | 188 | if ( m_front ) |
170 | m_front->setSaneCacheSize( size ); | 189 | m_front->setSaneCacheSize( size ); |
171 | } | 190 | } |
172 | template <class T> | 191 | template <class T> |
173 | T OPimAccessBackend<T>::find( int uid, const QArray<int>&, | 192 | T 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 ); |
176 | } | 196 | } |
177 | template <class T> | 197 | template <class T> |
178 | void OPimAccessBackend<T>::setReadAhead( uint count ) { | 198 | void OPimAccessBackend<T>::setReadAhead( uint count ) { |
179 | m_read = count; | 199 | m_read = count; |
180 | } | 200 | } |
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 | |||
@@ -32,13 +32,12 @@ | |||
32 | 32 | ||
33 | /* OPIE */ | 33 | /* OPIE */ |
34 | #include <opie2/opimrecord.h> | 34 | #include <opie2/opimrecord.h> |
35 | #include <opie2/opimaccessbackend.h> | 35 | #include <opie2/opimaccessbackend.h> |
36 | #include <opie2/opimrecordlist.h> | 36 | #include <opie2/opimrecordlist.h> |
37 | 37 | ||
38 | #include <opie2/opimcache.h> | ||
39 | #include <opie2/opimtemplatebase.h> | 38 | #include <opie2/opimtemplatebase.h> |
40 | #include <opie2/odebug.h> | 39 | #include <opie2/odebug.h> |
41 | 40 | ||
42 | /* QT */ | 41 | /* QT */ |
43 | #include <qarray.h> | 42 | #include <qarray.h> |
44 | 43 | ||
@@ -125,30 +124,33 @@ public: | |||
125 | /** | 124 | /** |
126 | * read ahead cache find method ;) | 125 | * read ahead cache find method ;) |
127 | */ | 126 | */ |
128 | virtual T find( int uid, const QArray<int>&, | 127 | virtual T find( int uid, const QArray<int>&, |
129 | uint current, typename OTemplateBase<T>::CacheDirection dir = OTemplateBase<T>::Forward )const; | 128 | uint current, typename OTemplateBase<T>::CacheDirection dir = OTemplateBase<T>::Forward )const; |
130 | 129 | ||
130 | |||
131 | /* invalidate cache here */ | 131 | /* invalidate cache here */ |
132 | /** | 132 | /** |
133 | * clears the backend and invalidates the backend | 133 | * clears the backend and invalidates the backend |
134 | */ | 134 | */ |
135 | void clear() ; | 135 | void clear() ; |
136 | 136 | ||
137 | /** | 137 | /** |
138 | * add T to the backend | 138 | * add T to the backend |
139 | * @param t The item to add. | 139 | * @param t The item to add. |
140 | * @return <i>true</i> if added successfully. | 140 | * @return <i>true</i> if added successfully. |
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* ); |
150 | 152 | ||
151 | 153 | ||
152 | /* only the uid matters */ | 154 | /* only the uid matters */ |
153 | /** | 155 | /** |
154 | * remove T from the backend | 156 | * remove T from the backend |
@@ -173,12 +175,13 @@ public: | |||
173 | virtual bool replace( const T& t) ; | 175 | virtual bool replace( const T& t) ; |
174 | 176 | ||
175 | void setReadAhead( uint count ); | 177 | void setReadAhead( uint count ); |
176 | /** | 178 | /** |
177 | * @internal | 179 | * @internal |
178 | */ | 180 | */ |
181 | virtual T cacheFind( int uid )const; | ||
179 | void cache( const T& )const; | 182 | void cache( const T& )const; |
180 | void setSaneCacheSize( int ); | 183 | void setSaneCacheSize( int ); |
181 | 184 | ||
182 | QArray<int> records()const; | 185 | QArray<int> records()const; |
183 | protected: | 186 | protected: |
184 | /** | 187 | /** |
@@ -189,12 +192,13 @@ protected: | |||
189 | void setBackEnd( BackEnd* end ); | 192 | void setBackEnd( BackEnd* end ); |
190 | /** | 193 | /** |
191 | * returns the backend | 194 | * returns the backend |
192 | */ | 195 | */ |
193 | BackEnd* backEnd(); | 196 | BackEnd* backEnd(); |
194 | BackEnd* m_backEnd; | 197 | BackEnd* m_backEnd; |
198 | |||
195 | Cache m_cache; | 199 | Cache m_cache; |
196 | 200 | ||
197 | private: | 201 | private: |
198 | OPimAccessTemplatePrivate *d; | 202 | OPimAccessTemplatePrivate *d; |
199 | 203 | ||
200 | }; | 204 | }; |
@@ -215,13 +219,13 @@ template <class T> | |||
215 | bool OPimAccessTemplate<T>::load() { | 219 | bool OPimAccessTemplate<T>::load() { |
216 | invalidateCache(); | 220 | invalidateCache(); |
217 | return m_backEnd->load(); | 221 | return m_backEnd->load(); |
218 | } | 222 | } |
219 | template <class T> | 223 | template <class T> |
220 | bool OPimAccessTemplate<T>::reload() { | 224 | bool 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(); |
223 | } | 227 | } |
224 | template <class T> | 228 | template <class T> |
225 | bool OPimAccessTemplate<T>::save() { | 229 | bool OPimAccessTemplate<T>::save() { |
226 | return m_backEnd->save(); | 230 | return m_backEnd->save(); |
227 | } | 231 | } |
@@ -248,26 +252,38 @@ OPimAccessTemplate<T>::queryByExample( const T& t, int settings, const QDateTime | |||
248 | 252 | ||
249 | List lis(ints, this ); | 253 | List lis(ints, this ); |
250 | return lis; | 254 | return lis; |
251 | } | 255 | } |
252 | template <class T> | 256 | template <class T> |
253 | T OPimAccessTemplate<T>::find( int uid ) const{ | 257 | T 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 | |||
268 | template <class T> | ||
269 | T OPimAccessTemplate<T>::cacheFind( int uid ) const | ||
270 | { | ||
271 | return m_cache.find( uid ); | ||
272 | } | ||
273 | |||
258 | template <class T> | 274 | template <class T> |
259 | T OPimAccessTemplate<T>::find( int uid, const QArray<int>& ar, | 275 | T OPimAccessTemplate<T>::find( int uid, const QArray<int>& ar, |
260 | uint current, typename OTemplateBase<T>::CacheDirection dir )const { | 276 | uint current, typename OTemplateBase<T>::CacheDirection dir )const { |
261 | /* | 277 | /* |
262 | * better do T.isEmpty() | 278 | * better do T.isEmpty() |
263 | * after a find this way we would | 279 | * after a find this way we would |
264 | * avoid two finds in QCache... | 280 | * avoid two finds in QCache... |
265 | */ | 281 | */ |
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 ); |
269 | } | 285 | } |
270 | 286 | ||
271 | T t = m_backEnd->find( uid, ar, current, dir ); | 287 | T t = m_backEnd->find( uid, ar, current, dir ); |
272 | cache( t ); | 288 | cache( t ); |
273 | return t; | 289 | return t; |
@@ -281,29 +297,33 @@ template <class T> | |||
281 | bool OPimAccessTemplate<T>::add( const T& t ) { | 297 | bool OPimAccessTemplate<T>::add( const T& t ) { |
282 | cache( t ); | 298 | cache( t ); |
283 | return m_backEnd->add( t ); | 299 | return m_backEnd->add( t ); |
284 | } | 300 | } |
285 | 301 | ||
286 | template <class T> | 302 | template <class T> |
287 | bool OPimAccessTemplate<T>::add( const OPimRecord& rec) { | 303 | bool OPimAccessTemplate<T>::add( const OPimRecord& rec ) { |
288 | /* same type */ | 304 | /* same type */ |
289 | T tempInstance; | 305 | T tempInstance; |
290 | if ( rec.rtti() == tempInstance.rtti() ) { | 306 | if ( rec.rtti() == tempInstance.rtti() ) { |
291 | const T& t = static_cast<const T&>(rec); | 307 | const T& t = static_cast<const T&>(rec); |
292 | return add(t); | 308 | return add(t); |
309 | } else { | ||
310 | owarn << "Adding not possible: Objecttype mismatch" << oendl; | ||
293 | } | 311 | } |
294 | return false; | 312 | return false; |
295 | } | 313 | } |
296 | 314 | ||
297 | template <class T> | 315 | template <class T> |
298 | bool OPimAccessTemplate<T>::add( const OPimRecord* rec) { | 316 | bool OPimAccessTemplate<T>::add( const OPimRecord* rec) { |
299 | /* same type, but pointer */ | 317 | /* same type, but pointer */ |
300 | T tempInstance; | 318 | T tempInstance; |
301 | if ( rec -> rtti() == tempInstance.rtti() ) { | 319 | if ( rec -> rtti() == tempInstance.rtti() ) { |
302 | const T* t = static_cast<const T*>(rec); | 320 | const T* t = static_cast<const T*>(rec); |
303 | return add( *t ); | 321 | return add( *t ); |
322 | } else { | ||
323 | owarn << "Adding not possible: Objecttype mismatch" << oendl; | ||
304 | } | 324 | } |
305 | return false; | 325 | return false; |
306 | } | 326 | } |
307 | 327 | ||
308 | template <class T> | 328 | template <class T> |
309 | bool OPimAccessTemplate<T>::remove( const T& t ) { | 329 | bool OPimAccessTemplate<T>::remove( const T& t ) { |
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 | |||
@@ -28,12 +28,13 @@ | |||
28 | */ | 28 | */ |
29 | #ifndef OTEMPLATEBASE_H | 29 | #ifndef OTEMPLATEBASE_H |
30 | #define OTEMPLATEBASE_H | 30 | #define OTEMPLATEBASE_H |
31 | 31 | ||
32 | /* OPIE */ | 32 | /* OPIE */ |
33 | #include <opie2/opimrecord.h> | 33 | #include <opie2/opimrecord.h> |
34 | #include <opie2/opimcache.h> | ||
34 | 35 | ||
35 | /* QT */ | 36 | /* QT */ |
36 | #include <qarray.h> | 37 | #include <qarray.h> |
37 | 38 | ||
38 | namespace Opie { | 39 | namespace Opie { |
39 | /** | 40 | /** |
@@ -67,36 +68,49 @@ struct OPimBase { | |||
67 | private: | 68 | private: |
68 | OPimBasePrivate* d; | 69 | OPimBasePrivate* d; |
69 | 70 | ||
70 | }; | 71 | }; |
71 | /** | 72 | /** |
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 | */ |
75 | class OTemplateBasePrivate; | 76 | class OTemplateBasePrivate; |
76 | template <class T = OPimRecord> | 77 | template <class T = OPimRecord> |
77 | class OTemplateBase : public OPimBase { | 78 | class OTemplateBase : public OPimBase { |
78 | public: | 79 | public: |
80 | /** Look ahead direction of cache */ | ||
79 | enum CacheDirection { Forward=0, Reverse }; | 81 | enum CacheDirection { Forward=0, Reverse }; |
82 | |||
80 | OTemplateBase() { | 83 | OTemplateBase() { |
81 | }; | 84 | }; |
82 | virtual ~OTemplateBase() { | 85 | virtual ~OTemplateBase() { |
83 | } | 86 | } |
84 | virtual T find( int uid )const = 0; | 87 | virtual T find( int uid )const = 0; |
85 | 88 | ||
86 | /** | 89 | /** |
87 | * read ahead find | 90 | * read ahead find |
88 | */ | 91 | */ |
89 | virtual T find( int uid, const QArray<int>& items, | 92 | virtual T find( int uid, const QArray<int>& items, |
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; |
92 | virtual void setSaneCacheSize( int ) = 0; | 105 | virtual void setSaneCacheSize( int ) = 0; |
93 | 106 | ||
94 | OPimRecord* record()const; | 107 | OPimRecord* record()const; |
95 | OPimRecord* record(int uid )const; | 108 | OPimRecord* record(int uid )const; |
96 | static T* rec(); | 109 | static T* rec(); |
110 | |||
97 | 111 | ||
98 | private: | 112 | private: |
99 | OTemplateBasePrivate *d; | 113 | OTemplateBasePrivate *d; |
100 | }; | 114 | }; |
101 | 115 | ||
102 | 116 | ||