summaryrefslogtreecommitdiff
authoreilers <eilers>2004-08-29 12:50:18 (UTC)
committer eilers <eilers>2004-08-29 12:50:18 (UTC)
commit679d9fef2673eea18fe5d9c85df2b10b09a8a250 (patch) (side-by-side diff)
tree8b3d6dbef45568be6f5daac31094d6e599fdefdf
parent45327ef3c0f093fc227682f79158632afc09e6d1 (diff)
downloadopie-679d9fef2673eea18fe5d9c85df2b10b09a8a250.zip
opie-679d9fef2673eea18fe5d9c85df2b10b09a8a250.tar.gz
opie-679d9fef2673eea18fe5d9c85df2b10b09a8a250.tar.bz2
Added lookahead caching for addressbook (SQL-only) for speed improvement.
Diffstat (more/less context) (show whitespace changes)
-rw-r--r--libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp128
-rw-r--r--libopie2/opiepim/backend/ocontactaccessbackend_sql.h5
-rw-r--r--libopie2/opiepim/backend/opimaccessbackend.h20
-rw-r--r--libopie2/opiepim/core/opimaccesstemplate.h34
-rw-r--r--libopie2/opiepim/core/opimtemplatebase.h16
5 files changed, 176 insertions, 27 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,6 +302,4 @@ namespace {
QString FindQuery::query()const{
-// if ( m_uids.count() == 0 )
+ if ( m_uids.count() == 0 )
return single();
- }
- /*
else
@@ -309,11 +307,14 @@ namespace {
}
+
QString FindQuery::multi()const {
- QString qu = "select uid, type, value from addressbook where";
+ QString qu = "select * from addressbook where";
for (uint i = 0; i < m_uids.count(); i++ ) {
- qu += " UID = " + QString::number( m_uids[i] ) + " OR";
+ 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{
@@ -475,3 +476,3 @@ OPimContact OPimContactAccessBackend_SQL::find ( int uid ) const
{
- odebug << "OPimContactAccessBackend_SQL::find()" << oendl;
+ odebug << "OPimContactAccessBackend_SQL::find(" << uid << ")" << oendl;
QTime t;
@@ -486,2 +487,43 @@ OPimContact OPimContactAccessBackend_SQL::find ( int uid ) const
+OPimContact OPimContactAccessBackend_SQL::find( int uid, const QArray<int>& 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<int> 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;
+}
@@ -770,4 +812,2 @@ QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) co
- QMap<int, QString> nonCustomMap;
-
int t2needed = 0;
@@ -782,4 +822,63 @@ QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) co
+ QMap<int, QString> 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<int>& 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<int> 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<int, QString> 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<int, QString> OPimContactAccessBackend_SQL::fillNonCustomMap( const OSQLResultItem& resultItem ) const
+{
+ QMap<int, QString> nonCustomMap;
+
// Now loop through all columns
@@ -792,3 +891,3 @@ QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) co
int id = translate[*it];
- QString value = resItem.data( (*it) );
+ QString value = resultItem.data( (*it) );
@@ -818,9 +917,3 @@ QMap<int, QString> 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" ) );
@@ -829,2 +922,3 @@ QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) co
+
QMap<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 {
OPimContact find ( int uid ) const;
- // FIXME: Add lookahead-cache support !
- //OPimContact find(int uid, const QArray<int>&, uint cur, Frontend::CacheDirection )const;
+ OPimContact find( int uid, const QArray<int>&, uint cur, Frontend::CacheDirection ) const;
@@ -100,2 +99,4 @@ class OPimContactAccessBackend_SQL : public OPimContactAccessBackend {
QMap<QString, QString> requestCustom( int uid ) const;
+ QMap<int, QString> fillNonCustomMap( const Opie::DB::OSQLResultItem& resultItem ) const;
+ OPimContact requestContactsAndCache( int uid, const QArray<int>& cachelist ) const;
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
@@ -130,2 +130,3 @@ protected:
int access()const;
+
void cache( const T& t )const;
@@ -133,2 +134,9 @@ protected:
/**
+ * Returns the element with given uid out of the cache.
+ * Returns empty element if nothing was found.
+ * <b>Attention:</b> This just works if we have a frontend which contains the cache !
+ */
+ T cacheFind( int uid ) const;
+
+ /**
* use a prime number here!
@@ -166,2 +174,13 @@ void OPimAccessBackend<T>::cache( const T& t )const {
}
+
+template <class T>
+T OPimAccessBackend<T>::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 <class T>
@@ -174,2 +193,3 @@ T OPimAccessBackend<T>::find( int uid, const QArray<int>&,
uint, typename Frontend::CacheDirection )const {
+ qDebug( "*** Lookahead feature not supported. Fallback to default find!" );
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 @@
-#include <opie2/opimcache.h>
#include <opie2/opimtemplatebase.h>
@@ -130,2 +129,3 @@ public:
+
/* invalidate cache here */
@@ -142,8 +142,10 @@ public:
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* );
@@ -178,2 +180,3 @@ public:
*/
+ virtual T cacheFind( int uid )const;
void cache( const T& )const;
@@ -194,2 +197,3 @@ protected:
BackEnd* m_backEnd;
+
Cache m_cache;
@@ -220,3 +224,3 @@ template <class T>
bool OPimAccessTemplate<T>::reload() {
- invalidateCache(); // zecke: I think this should be added (se)
+ invalidateCache();
return m_backEnd->reload();
@@ -253,6 +257,18 @@ template <class T>
T OPimAccessTemplate<T>::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 <class T>
+T OPimAccessTemplate<T>::cacheFind( int uid ) const
+{
+ return m_cache.find( uid );
+}
+
template <class T>
@@ -292,2 +308,4 @@ bool OPimAccessTemplate<T>::add( const OPimRecord& rec) {
return add(t);
+ } else {
+ owarn << "Adding not possible: Objecttype mismatch" << oendl;
}
@@ -303,2 +321,4 @@ bool OPimAccessTemplate<T>::add( const OPimRecord* rec) {
return add( *t );
+ } else {
+ owarn << "Adding not possible: Objecttype mismatch" << oendl;
}
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 @@
#include <opie2/opimrecord.h>
+#include <opie2/opimcache.h>
@@ -72,3 +73,3 @@ private:
* internal template base
- * T needs to implement the copy c'tor!!!
+ * Attention: T needs to implement the copy c'tor!!!
*/
@@ -78,3 +79,5 @@ class OTemplateBase : public OPimBase {
public:
+ /** Look ahead direction of cache */
enum CacheDirection { Forward=0, Reverse };
+
OTemplateBase() {
@@ -90,2 +93,12 @@ public:
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;
@@ -97,2 +110,3 @@ public:
+
private: