summaryrefslogtreecommitdiff
path: root/libopie2/opiepim/backend
Side-by-side diff
Diffstat (limited to 'libopie2/opiepim/backend') (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
3 files changed, 134 insertions, 19 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,28 +297,29 @@ namespace {
FindQuery::FindQuery(const QArray<int>& ints)
: OSQLQuery(), m_uids( ints ){
}
FindQuery::~FindQuery() {
}
QString FindQuery::query()const{
-// if ( m_uids.count() == 0 )
+ if ( m_uids.count() == 0 )
return single();
- }
- /*
else
return multi();
}
+
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{
QString qu = "select *";
qu += " from addressbook where uid = " + QString::number(m_uid);
// owarn << "find query: " << qu << "" << oendl;
return qu;
@@ -470,23 +471,64 @@ bool OPimContactAccessBackend_SQL::replace ( const OPimContact &contact )
return add( contact );
}
OPimContact OPimContactAccessBackend_SQL::find ( int uid ) const
{
- odebug << "OPimContactAccessBackend_SQL::find()" << oendl;
+ odebug << "OPimContactAccessBackend_SQL::find(" << uid << ")" << oendl;
QTime t;
t.start();
OPimContact retContact( requestNonCustom( uid ) );
retContact.setExtraMap( requestCustom( uid ) );
odebug << "OPimContactAccessBackend_SQL::find() needed: " << t.elapsed() << " ms" << oendl;
return retContact;
}
+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;
+}
QArray<int> OPimContactAccessBackend_SQL::queryByExample ( const OPimContact &query, int settings, const QDateTime& qd )
{
QString qu = "SELECT uid FROM addressbook WHERE";
QString searchQuery ="";
@@ -765,35 +807,92 @@ QArray<int> OPimContactAccessBackend_SQL::extractUids( OSQLResult& res ) const
QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) const
{
QTime t;
t.start();
- QMap<int, QString> nonCustomMap;
-
int t2needed = 0;
int t3needed = 0;
QTime t2;
t2.start();
FindQuery query( uid );
OSQLResult res_noncustom = m_driver->query( &query );
t2needed = t2.elapsed();
OSQLResultItem resItem = res_noncustom.first();
+ 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
QStringList fieldList = OPimContactFields::untrfields( false );
QMap<QString, int> translate = OPimContactFields::untrFieldsToId();
for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){
// Get data for the selected column and store it with the
// 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;
switch( id ){
case Qtopia::Birthday:
case Qtopia::Anniversary:{
@@ -813,23 +912,18 @@ QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) co
odebug << "Category is: " << value << "" << oendl;
default:
nonCustomMap.insert( id, value );
}
}
- // 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<QString, QString> OPimContactAccessBackend_SQL::requestCustom( int uid ) const
{
QTime t;
t.start();
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 {
bool wasChangedExternally();
QArray<int> allRecords() const;
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;
QArray<int> queryByExample ( const OPimContact &query, int settings,
const QDateTime& d );
QArray<int> matchRegexp( const QRegExp &r ) const;
@@ -95,12 +94,14 @@ class OPimContactAccessBackend_SQL : public OPimContactAccessBackend {
bool reload();
private:
QArray<int> extractUids( Opie::DB::OSQLResult& res ) const;
QMap<int, QString> requestNonCustom( int uid ) const;
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();
protected:
bool m_changed;
QString m_fileName;
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
@@ -125,15 +125,23 @@ public:
/**
* set the read ahead count
*/
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.
+ * <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!
*/
void setSaneCacheSize( int );
uint readAhead()const;
@@ -161,20 +169,32 @@ void OPimAccessBackend<T>::setFrontend( Frontend* fr ) {
}
template <class T>
void OPimAccessBackend<T>::cache( const T& t )const {
if (m_front )
m_front->cache( t );
}
+
+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>
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, typename Frontend::CacheDirection )const {
+ qDebug( "*** Lookahead feature not supported. Fallback to default find!" );
return find( uid );
}
template <class T>
void OPimAccessBackend<T>::setReadAhead( uint count ) {
m_read = count;
}