summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoraunsane <aunsane@gmail.com>2018-09-27 23:48:00 +0300
committeraunsane <aunsane@gmail.com>2018-09-27 23:48:00 +0300
commitfe0958f789d45915c317db89dce533311c79d016 (patch)
tree97849275b8df8ae9562b4f6eda722feb85959f91
parent39fdc50b018c989f22c5392aca11da5fc5a482aa (diff)
dbx_sqlite: simple events cache
-rw-r--r--plugins/Dbx_sqlite/src/dbcontacts.cpp7
-rw-r--r--plugins/Dbx_sqlite/src/dbevents.cpp79
-rw-r--r--plugins/Dbx_sqlite/src/dbintf.h16
-rw-r--r--plugins/Dbx_sqlite/src/stdafx.h1
4 files changed, 83 insertions, 20 deletions
diff --git a/plugins/Dbx_sqlite/src/dbcontacts.cpp b/plugins/Dbx_sqlite/src/dbcontacts.cpp
index 2bcc0027c8..6a8568e2cb 100644
--- a/plugins/Dbx_sqlite/src/dbcontacts.cpp
+++ b/plugins/Dbx_sqlite/src/dbcontacts.cpp
@@ -24,10 +24,13 @@ void CDbxSQLite::InitContacts()
//m_cache->AddContactToCache(0);
sqlite3_stmt *stmt = nullptr;
- sqlite3_prepare_v2(m_db, "select id from contacts;", -1, &stmt, nullptr);
+ sqlite3_prepare_v2(m_db, "select contact_events.contactid, count(eventid) from contact_events join contacts on contacts.id = contact_events.contactid group by contact_events.contactid;", -1, &stmt, nullptr);
while (sqlite3_step(stmt) == SQLITE_ROW) {
MCONTACT hContact = sqlite3_column_int64(stmt, 0);
- DBCachedContact *cc = m_cache->AddContactToCache(hContact);
+ DBCachedContact *cc = (hContact)
+ ? m_cache->AddContactToCache(hContact)
+ : &m_system;
+ cc->count = sqlite3_column_int64(stmt, 1);
DBVARIANT dbv = { DBVT_DWORD };
cc->nSubs = (0 != GetContactSetting(cc->contactID, META_PROTO, "NumContacts", &dbv)) ? -1 : dbv.dVal;
diff --git a/plugins/Dbx_sqlite/src/dbevents.cpp b/plugins/Dbx_sqlite/src/dbevents.cpp
index 0cc5208009..b02d49a929 100644
--- a/plugins/Dbx_sqlite/src/dbevents.cpp
+++ b/plugins/Dbx_sqlite/src/dbevents.cpp
@@ -61,6 +61,13 @@ void CDbxSQLite::UninitEvents()
LONG CDbxSQLite::GetEventCount(MCONTACT hContact)
{
+ DBCachedContact *cc = (hContact)
+ ? m_cache->GetCachedContact(hContact)
+ : &m_system;
+
+ if (cc->count)
+ return cc->count;
+
mir_cslock lock(m_csDbAccess);
sqlite3_stmt *stmt = evt_stmts_prep[SQL_EVT_STMT_COUNT];
sqlite3_bind_int64(stmt, 1, hContact);
@@ -69,9 +76,9 @@ LONG CDbxSQLite::GetEventCount(MCONTACT hContact)
sqlite3_reset(stmt);
return 0;
}
- LONG count = sqlite3_column_int64(stmt, 0);
+ cc->count = sqlite3_column_int64(stmt, 0);
sqlite3_reset(stmt);
- return count;
+ return cc->count;
}
MEVENT CDbxSQLite::AddEvent(MCONTACT hContact, DBEVENTINFO *dbei)
@@ -82,7 +89,9 @@ MEVENT CDbxSQLite::AddEvent(MCONTACT hContact, DBEVENTINFO *dbei)
if (dbei->timestamp == 0)
return 0;
- DBCachedContact *cc = m_cache->GetCachedContact(hContact);
+ DBCachedContact *cc = (hContact)
+ ? m_cache->GetCachedContact(hContact)
+ : &m_system;
if (cc == nullptr)
return 0;
@@ -121,6 +130,11 @@ MEVENT CDbxSQLite::AddEvent(MCONTACT hContact, DBEVENTINFO *dbei)
}
sqlite3_exec(m_db, "commit;", nullptr, nullptr, nullptr);
+
+ cc->count++;
+ cc->first = cc->last = 0;
+ if (!(dbei->flags & (DBEF_READ | DBEF_SENT)))
+ cc->unread = 0;
}
bool neednotify = false;
@@ -140,7 +154,9 @@ BOOL CDbxSQLite::DeleteEvent(MCONTACT hContact, MEVENT hDbEvent)
if (hDbEvent == 0)
return 1;
- DBCachedContact *cc = m_cache->GetCachedContact(hContact);
+ DBCachedContact *cc = (hContact)
+ ? m_cache->GetCachedContact(hContact)
+ : &m_system;
if (cc == nullptr)
return 1;
@@ -153,6 +169,11 @@ BOOL CDbxSQLite::DeleteEvent(MCONTACT hContact, MEVENT hDbEvent)
sqlite3_reset(stmt);
if (rc != SQLITE_DONE)
return 1;
+
+ if (cc->count > 0)
+ cc->count--;
+
+ cc->first = cc->unread = cc->last = 0;
}
NotifyEventHooks(hEventDeletedEvent, hContact, (LPARAM)hDbEvent);
@@ -168,7 +189,9 @@ BOOL CDbxSQLite::EditEvent(MCONTACT hContact, MEVENT hDbEvent, DBEVENTINFO *dbei
if (dbei->timestamp == 0)
return 1;
- DBCachedContact *cc = m_cache->GetCachedContact(hContact);
+ DBCachedContact *cc = (hContact)
+ ? m_cache->GetCachedContact(hContact)
+ : &m_system;
if (cc == nullptr)
return 1;
@@ -182,8 +205,10 @@ BOOL CDbxSQLite::EditEvent(MCONTACT hContact, MEVENT hDbEvent, DBEVENTINFO *dbei
sqlite3_bind_blob(stmt, 6, dbei->pBlob, dbei->cbBlob, nullptr);
sqlite3_bind_int64(stmt, 7, hDbEvent);
int rc = sqlite3_step(stmt);
- if (rc == SQLITE_DONE)
+ if (rc == SQLITE_DONE) {
+ cc->first = cc->unread = cc->last = 0;
NotifyEventHooks(hEventEditedEvent, hContact, (LPARAM)hDbEvent);
+ }
sqlite3_reset(stmt);
return (rc != SQLITE_DONE);
}
@@ -248,7 +273,9 @@ BOOL CDbxSQLite::MarkEventRead(MCONTACT hContact, MEVENT hDbEvent)
if (hDbEvent == 0)
return -1;
- DBCachedContact *cc = m_cache->GetCachedContact(hContact);
+ DBCachedContact *cc = (hContact)
+ ? m_cache->GetCachedContact(hContact)
+ : &m_system;
if (cc == nullptr)
return -1;
@@ -279,6 +306,8 @@ BOOL CDbxSQLite::MarkEventRead(MCONTACT hContact, MEVENT hDbEvent)
sqlite3_reset(stmt);
if (rc != SQLITE_DONE)
return -1;
+ if (cc->unread = hDbEvent)
+ cc->unread = 0;
}
NotifyEventHooks(hEventMarkedRead, hContact, (LPARAM)hDbEvent);
@@ -306,10 +335,15 @@ MCONTACT CDbxSQLite::GetEventContact(MEVENT hDbEvent)
MEVENT CDbxSQLite::FindFirstEvent(MCONTACT hContact)
{
- DBCachedContact *cc = m_cache->GetCachedContact(hContact);
+ DBCachedContact *cc = (hContact)
+ ? m_cache->GetCachedContact(hContact)
+ : &m_system;
if (cc == nullptr)
return 0;
+ if (cc->first)
+ return cc->first;
+
mir_cslock lock(m_csDbAccess);
sqlite3_stmt *stmt = evt_stmts_prep[SQL_EVT_STMT_FINDFIRST];
sqlite3_bind_int64(stmt, 1, hContact);
@@ -318,17 +352,22 @@ MEVENT CDbxSQLite::FindFirstEvent(MCONTACT hContact)
sqlite3_reset(stmt);
return 0;
}
- MEVENT hDbEvent = sqlite3_column_int64(stmt, 0);
+ cc->first = sqlite3_column_int64(stmt, 0);
sqlite3_reset(stmt);
- return hDbEvent;
+ return cc->first;
}
MEVENT CDbxSQLite::FindFirstUnreadEvent(MCONTACT hContact)
{
- DBCachedContact *cc = m_cache->GetCachedContact(hContact);
+ DBCachedContact *cc = (hContact)
+ ? m_cache->GetCachedContact(hContact)
+ : &m_system;
if (cc == nullptr)
return 0;
+ if (cc->unread)
+ return cc->unread;
+
mir_cslock lock(m_csDbAccess);
sqlite3_stmt *stmt = evt_stmts_prep[SQL_EVT_STMT_FINDFIRSTUNREAD];
sqlite3_bind_int64(stmt, 1, hContact);
@@ -337,17 +376,22 @@ MEVENT CDbxSQLite::FindFirstUnreadEvent(MCONTACT hContact)
sqlite3_reset(stmt);
return 0;
}
- MEVENT hDbEvent = sqlite3_column_int64(stmt, 0);
+ cc->unread = sqlite3_column_int64(stmt, 0);
sqlite3_reset(stmt);
- return hDbEvent;
+ return cc->unread;
}
MEVENT CDbxSQLite::FindLastEvent(MCONTACT hContact)
{
- DBCachedContact *cc = m_cache->GetCachedContact(hContact);
+ DBCachedContact *cc = (hContact)
+ ? m_cache->GetCachedContact(hContact)
+ : &m_system;
if (cc == nullptr)
return 0;
+ if (cc->last)
+ return cc->last;
+
mir_cslock lock(m_csDbAccess);
sqlite3_stmt *stmt = evt_stmts_prep[SQL_EVT_STMT_FINDLAST];
sqlite3_bind_int64(stmt, 1, hContact);
@@ -356,9 +400,9 @@ MEVENT CDbxSQLite::FindLastEvent(MCONTACT hContact)
sqlite3_reset(stmt);
return 0;
}
- MEVENT hDbEvent = sqlite3_column_int64(stmt, 0);
+ cc->last = sqlite3_column_int64(stmt, 0);
sqlite3_reset(stmt);
- return hDbEvent;
+ return cc->last;
}
MEVENT CDbxSQLite::FindNextEvent(MCONTACT hContact, MEVENT hDbEvent)
@@ -440,7 +484,6 @@ BOOL CDbxSQLite::SetEventId(LPCSTR, MEVENT hDbEvent, LPCSTR szId)
return (rc != SQLITE_DONE);
}
-
BOOL CDbxSQLite::MetaMergeHistory(DBCachedContact *ccMeta, DBCachedContact *ccSub)
{
mir_cslock lock(m_csDbAccess);
@@ -449,6 +492,7 @@ BOOL CDbxSQLite::MetaMergeHistory(DBCachedContact *ccMeta, DBCachedContact *ccSu
sqlite3_bind_int64(stmt, 2, ccSub->contactID);
int rc = sqlite3_step(stmt);
sqlite3_reset(stmt);
+ ccMeta->first = ccMeta->unread = ccMeta->last = 0;
return (rc != SQLITE_DONE);
}
@@ -460,5 +504,6 @@ BOOL CDbxSQLite::MetaSplitHistory(DBCachedContact *ccMeta, DBCachedContact *ccSu
sqlite3_bind_int64(stmt, 2, ccSub->contactID);
int rc = sqlite3_step(stmt);
sqlite3_reset(stmt);
+ ccMeta->first = ccMeta->unread = ccMeta->last = 0;
return (rc != SQLITE_DONE);
} \ No newline at end of file
diff --git a/plugins/Dbx_sqlite/src/dbintf.h b/plugins/Dbx_sqlite/src/dbintf.h
index 190745d41f..4d919212ec 100644
--- a/plugins/Dbx_sqlite/src/dbintf.h
+++ b/plugins/Dbx_sqlite/src/dbintf.h
@@ -1,5 +1,19 @@
#pragma once
+#define OWN_CACHED_CONTACT
+
+#include <m_db_int.h>
+
+struct DBCachedContact : public DBCachedContactBase
+{
+ uint32_t count;
+ MEVENT first;
+ MEVENT unread;
+ MEVENT last;
+
+ DBCachedContact() { count = first = unread = last = 0; }
+};
+
struct CDbxSQLite : public MDatabaseCommon, public MZeroedObject
{
private:
@@ -14,6 +28,8 @@ private:
HANDLE hEventMarkedRead;
HANDLE hSettingChangeEvent;
+ DBCachedContact m_system;
+
CDbxSQLite(sqlite3 *database);
void InitContacts();
diff --git a/plugins/Dbx_sqlite/src/stdafx.h b/plugins/Dbx_sqlite/src/stdafx.h
index bcf4e9811f..80384e86da 100644
--- a/plugins/Dbx_sqlite/src/stdafx.h
+++ b/plugins/Dbx_sqlite/src/stdafx.h
@@ -11,7 +11,6 @@
#include <m_core.h>
#include <m_system.h>
#include <m_database.h>
-#include <m_db_int.h>
#include <m_protocols.h>
#include <m_metacontacts.h>