summaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authorMikalaiR <nikolay.romanovich@narod.ru>2016-07-06 18:44:56 +0000
committerMikalaiR <nikolay.romanovich@narod.ru>2016-07-06 18:44:56 +0000
commit79e4ee10e9ebd6382f819502285685ce21e69ef0 (patch)
tree75d9a769d796bd03d624a43c130e4830a0a9a3fd /plugins
parenta113c5dfbcb862c2cf0a49f3f3e873d7325c0c53 (diff)
dbx_lmdb: code optimization
git-svn-id: http://svn.miranda-ng.org/main/trunk@17069 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'plugins')
-rw-r--r--plugins/Dbx_mdb/src/dbcontacts.cpp20
-rw-r--r--plugins/Dbx_mdb/src/dbcrypt.cpp40
-rw-r--r--plugins/Dbx_mdb/src/dbevents.cpp67
-rw-r--r--plugins/Dbx_mdb/src/dbintf.cpp27
-rw-r--r--plugins/Dbx_mdb/src/dbintf.h71
-rw-r--r--plugins/Dbx_mdb/src/dbmodulechain.cpp10
-rw-r--r--plugins/Dbx_mdb/src/dbsettings.cpp7
-rw-r--r--plugins/Dbx_mdb/src/init.cpp8
-rw-r--r--plugins/Dbx_mdb/src/lmdb/mdb.c1
-rw-r--r--plugins/Dbx_mdb/src/stdafx.h11
10 files changed, 126 insertions, 136 deletions
diff --git a/plugins/Dbx_mdb/src/dbcontacts.cpp b/plugins/Dbx_mdb/src/dbcontacts.cpp
index 629790ea61..48a911c459 100644
--- a/plugins/Dbx_mdb/src/dbcontacts.cpp
+++ b/plugins/Dbx_mdb/src/dbcontacts.cpp
@@ -130,10 +130,6 @@ STDMETHODIMP_(LONG) CDbxMdb::DeleteContact(MCONTACT contactID)
InterlockedDecrement(&m_contactCount);
- m_cache->FreeCachedContact(contactID);
- if (contactID == m_hLastCachedContact)
- m_hLastCachedContact = NULL;
-
return 0;
}
@@ -191,17 +187,15 @@ void CDbxMdb::GatherContactHistory(MCONTACT hContact, LIST<EventItem> &list)
for (int res = mdb_cursor_get(cursor, &key, &data, MDB_SET_RANGE); res == MDB_SUCCESS; res = mdb_cursor_get(cursor, &key, &data, MDB_NEXT))
{
const DBEventSortingKey *pKey = (const DBEventSortingKey*)key.mv_data;
- if (pKey->dwContactId != hContact)
+ if (pKey->hContact != hContact)
return;
- list.insert(new EventItem(pKey->ts, pKey->dwEventId));
+ list.insert(new EventItem(pKey->ts, pKey->hEvent));
}
}
BOOL CDbxMdb::MetaMergeHistory(DBCachedContact *ccMeta, DBCachedContact *ccSub)
{
- mir_cslock lck(m_csDbAccess);
-
LIST<EventItem> list(1000);
GatherContactHistory(ccSub->contactID, list);
@@ -212,7 +206,7 @@ BOOL CDbxMdb::MetaMergeHistory(DBCachedContact *ccMeta, DBCachedContact *ccSub)
{
txn_ptr trnlck(m_pMdbEnv);
DBEventSortingKey insVal = { EI->eventId, EI->ts, ccMeta->contactID };
- MDB_val key = { sizeof(insVal), &insVal }, data = { 1, "" };
+ MDB_val key = { sizeof(insVal), &insVal }, data = { 1, (void*)"" };
mdb_put(trnlck, m_dbEventsSort, &key, &data, 0);
if (trnlck.commit() == MDB_SUCCESS)
break;
@@ -237,8 +231,6 @@ BOOL CDbxMdb::MetaMergeHistory(DBCachedContact *ccMeta, DBCachedContact *ccSub)
BOOL CDbxMdb::MetaSplitHistory(DBCachedContact *ccMeta, DBCachedContact *ccSub)
{
- mir_cslock lck(m_csDbAccess);
-
LIST<EventItem> list(1000);
GatherContactHistory(ccSub->contactID, list);
@@ -248,7 +240,7 @@ BOOL CDbxMdb::MetaSplitHistory(DBCachedContact *ccMeta, DBCachedContact *ccSub)
for (;; Remap()) {
txn_ptr trnlck(m_pMdbEnv);
DBEventSortingKey insVal = { EI->eventId, EI->ts, ccMeta->contactID };
- MDB_val key = { sizeof(insVal), &insVal }, data = { 1, "" };
+ MDB_val key = { sizeof(insVal), &insVal }, data = { 1, (void*)"" };
mdb_del(trnlck, m_dbEventsSort, &key, &data);
if (trnlck.commit() == MDB_SUCCESS)
break;
@@ -271,7 +263,7 @@ BOOL CDbxMdb::MetaSplitHistory(DBCachedContact *ccMeta, DBCachedContact *ccSub)
/////////////////////////////////////////////////////////////////////////////////////////
-void DBCachedContact::Advance(DWORD id, DBEvent &dbe)
+void DBCachedContact::Advance(MEVENT id, DBEvent &dbe)
{
dbc.dwEventCount++;
@@ -280,7 +272,7 @@ void DBCachedContact::Advance(DWORD id, DBEvent &dbe)
if (dbe.timestamp < dbc.tsFirstUnread || dbc.tsFirstUnread == 0) {
dbc.tsFirstUnread = dbe.timestamp;
- dbc.dwFirstUnread = id;
+ dbc.evFirstUnread = id;
}
}
diff --git a/plugins/Dbx_mdb/src/dbcrypt.cpp b/plugins/Dbx_mdb/src/dbcrypt.cpp
index e860a3c86e..0b5d3e6bf2 100644
--- a/plugins/Dbx_mdb/src/dbcrypt.cpp
+++ b/plugins/Dbx_mdb/src/dbcrypt.cpp
@@ -25,9 +25,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
/////////////////////////////////////////////////////////////////////////////////////////
-#define DBKEY_PROVIDER "Provider"
-#define DBKEY_KEY "Key"
-#define DBKEY_IS_ENCRYPTED "EncryptedDB"
+char DBKey_Crypto_Provider [] = "Provider";
+char DBKey_Crypto_Key [] = "Key";
+char DBKey_Crypto_IsEncrypted[] = "EncryptedDB";
CRYPTO_PROVIDER* CDbxMdb::SelectProvider()
{
@@ -53,10 +53,10 @@ CRYPTO_PROVIDER* CDbxMdb::SelectProvider()
{
txn_ptr txn(m_pMdbEnv);
- MDB_val key = { sizeof(DBKEY_PROVIDER), (char*)(DBKEY_PROVIDER) }, value = { mir_strlen(pProv->pszName) + 1, pProv->pszName };
+ MDB_val key = { sizeof(DBKey_Crypto_Provider), DBKey_Crypto_Provider }, value = { mir_strlen(pProv->pszName) + 1, pProv->pszName };
MDB_CHECK(mdb_put(txn, m_dbCrypto, &key, &value, 0), nullptr);
- key.mv_size = sizeof(DBKEY_IS_ENCRYPTED); key.mv_data = (char*)(DBKEY_IS_ENCRYPTED); value.mv_size = sizeof(bool); value.mv_data = &bTotalCrypt;
+ key.mv_size = sizeof(DBKey_Crypto_IsEncrypted); key.mv_data = DBKey_Crypto_IsEncrypted; value.mv_size = sizeof(bool); value.mv_data = &bTotalCrypt;
MDB_CHECK(mdb_put(txn, m_dbCrypto, &key, &value, 0), nullptr);
if (txn.commit() == MDB_SUCCESS)
@@ -72,7 +72,7 @@ int CDbxMdb::InitCrypt()
txn_ptr_ro txn(m_txn);
- MDB_val key = { sizeof(DBKEY_PROVIDER), (char*)(DBKEY_PROVIDER) }, value;
+ MDB_val key = { sizeof(DBKey_Crypto_Provider), DBKey_Crypto_Provider }, value;
if (mdb_get(txn, m_dbCrypto, &key, &value) == MDB_SUCCESS)
{
pProvider = Crypto_GetProvider((const char*)value.mv_data);
@@ -89,7 +89,7 @@ int CDbxMdb::InitCrypt()
if ((m_crypto = pProvider->pFactory()) == nullptr)
return 3;
- key.mv_size = sizeof(DBKEY_KEY); key.mv_data = (char*)(DBKEY_KEY);
+ key.mv_size = sizeof(DBKey_Crypto_Key); key.mv_data = DBKey_Crypto_Key;
if (mdb_get(txn, m_dbCrypto, &key, &value) == MDB_SUCCESS && (value.mv_size == m_crypto->getKeyLength()))
{
if (!m_crypto->setKey((const BYTE*)value.mv_data, value.mv_size))
@@ -118,7 +118,7 @@ int CDbxMdb::InitCrypt()
StoreKey();
}
- key.mv_size = sizeof(DBKEY_IS_ENCRYPTED); key.mv_data = (char*)(DBKEY_IS_ENCRYPTED);
+ key.mv_size = sizeof(DBKey_Crypto_IsEncrypted); key.mv_data = DBKey_Crypto_IsEncrypted;
if (mdb_get(txn, m_dbCrypto, &key, &value) == MDB_SUCCESS)
m_bEncrypted = *(const bool*)value.mv_data;
@@ -138,7 +138,7 @@ void CDbxMdb::StoreKey()
for (;; Remap())
{
txn_ptr txn(m_pMdbEnv);
- MDB_val key = { sizeof(DBKEY_KEY), (char*)(DBKEY_KEY) }, value = { iKeyLength, pKey };
+ MDB_val key = { sizeof(DBKey_Crypto_Key), DBKey_Crypto_Key }, value = { iKeyLength, pKey };
mdb_put(txn, m_dbCrypto, &key, &value, 0);
if (txn.commit() == MDB_SUCCESS)
break;
@@ -168,9 +168,7 @@ int CDbxMdb::EnableEncryption(bool bEncrypted)
if (m_bEncrypted == bEncrypted)
return 0;
- mir_cslock lck(m_csDbAccess);
-#ifdef DEBUG
{
txn_ptr_ro txn(m_txn);
@@ -202,7 +200,7 @@ int CDbxMdb::EnableEncryption(bool bEncrypted)
{
mir_ptr<BYTE> pNewBlob;
size_t nNewBlob;
- DWORD dwNewFlags;
+ uint32_t dwNewFlags;
if (dbEvent->flags & DBEF_ENCRYPTED)
{
@@ -215,21 +213,18 @@ int CDbxMdb::EnableEncryption(bool bEncrypted)
dwNewFlags = dbEvent->flags | DBEF_ENCRYPTED;
}
- DBEvent *pNewEvent = (DBEvent*)_alloca(sizeof(DBEvent));
- memcpy(pNewEvent, dbEvent, sizeof(DBEvent));
- pNewEvent->cbBlob = nNewBlob;
- pNewEvent->flags = dwNewFlags;
-
-
for (;; Remap())
{
txn_ptr txn(m_pMdbEnv);
data.mv_size = sizeof(DBEvent)+nNewBlob;
MDB_CHECK(mdb_put(txn, m_dbEvents, &key, &data, MDB_RESERVE), 1);
- BYTE *pNewDBEvent = (BYTE*)data.mv_data;
- memcpy(pNewDBEvent, pNewEvent, sizeof(DBEvent));
- memcpy(pNewDBEvent + sizeof(DBEvent), pNewBlob, nNewBlob);
+ DBEvent *pNewDBEvent = (DBEvent *)data.mv_data;
+ *pNewDBEvent = *dbEvent;
+ pNewDBEvent->cbBlob = nNewBlob;
+ pNewDBEvent->flags = dwNewFlags;
+ memcpy(pNewDBEvent + 1, pNewBlob, nNewBlob);
+
if (txn.commit() == MDB_SUCCESS)
break;
@@ -237,12 +232,11 @@ int CDbxMdb::EnableEncryption(bool bEncrypted)
}
}
}
-#endif
for (;; Remap())
{
txn_ptr txn(m_pMdbEnv);
- MDB_val key = { sizeof(DBKEY_IS_ENCRYPTED), (char*)(DBKEY_IS_ENCRYPTED) }, value = { sizeof(bool), &bEncrypted };
+ MDB_val key = { sizeof(DBKey_Crypto_IsEncrypted), DBKey_Crypto_IsEncrypted }, value = { sizeof(bool), &bEncrypted };
MDB_CHECK(mdb_put(txn, m_dbCrypto, &key, &value, 0), 1);
if (txn.commit() == MDB_SUCCESS)
break;
diff --git a/plugins/Dbx_mdb/src/dbevents.cpp b/plugins/Dbx_mdb/src/dbevents.cpp
index 48c8993cdd..eb7fd3b568 100644
--- a/plugins/Dbx_mdb/src/dbevents.cpp
+++ b/plugins/Dbx_mdb/src/dbevents.cpp
@@ -36,7 +36,7 @@ STDMETHODIMP_(MEVENT) CDbxMdb::AddEvent(MCONTACT contactID, DBEVENTINFO *dbei)
DBEvent dbe;
dbe.contactID = contactID; // store native or subcontact's id
- dbe.ofsModuleName = GetModuleID(dbei->szModule);
+ dbe.iModuleId = GetModuleID(dbei->szModule);
MCONTACT contactNotifyID = contactID;
DBCachedContact *cc, *ccSub = NULL;
@@ -72,13 +72,13 @@ STDMETHODIMP_(MEVENT) CDbxMdb::AddEvent(MCONTACT contactID, DBEVENTINFO *dbei)
BYTE *pResult = m_crypto->encodeBuffer(pBlob, dbe.cbBlob, &len);
if (pResult != NULL) {
pCryptBlob = pBlob = pResult;
- dbe.cbBlob = (DWORD)len;
+ dbe.cbBlob = (uint16_t)len;
dbe.flags |= DBEF_ENCRYPTED;
}
}
- DWORD dwEventId = InterlockedIncrement(&m_dwMaxEventId);
+ MEVENT dwEventId = InterlockedIncrement(&m_dwMaxEventId);
const auto Snapshot = [&]() { cc->Snapshot(); if (ccSub) ccSub->Snapshot(); };
const auto Revert = [&]() { cc->Revert(); if (ccSub) ccSub->Revert(); };
@@ -89,12 +89,12 @@ STDMETHODIMP_(MEVENT) CDbxMdb::AddEvent(MCONTACT contactID, DBEVENTINFO *dbei)
MDB_val key = { sizeof(int), &dwEventId }, data = { sizeof(DBEvent) + dbe.cbBlob, NULL };
MDB_CHECK(mdb_put(txn, m_dbEvents, &key, &data, MDB_RESERVE), 0);
- BYTE *pDest = (BYTE*)data.mv_data;
- memcpy(pDest, &dbe, sizeof(DBEvent));
- memcpy(pDest + sizeof(DBEvent), pBlob, dbe.cbBlob);
+ DBEvent *pNewEvent = (DBEvent*)data.mv_data;
+ *pNewEvent = dbe;
+ memcpy(pNewEvent + 1, pBlob, dbe.cbBlob);
// add a sorting key
- DBEventSortingKey key2 = { dwEventId, dbe.timestamp, contactID };
+ DBEventSortingKey key2 = { contactID, dwEventId, dbe.timestamp };
key.mv_size = sizeof(key2); key.mv_data = &key2;
data.mv_size = 1; data.mv_data = (char*)("");
MDB_CHECK(mdb_put(txn, m_dbEventsSort, &key, &data, 0), 0);
@@ -105,7 +105,7 @@ STDMETHODIMP_(MEVENT) CDbxMdb::AddEvent(MCONTACT contactID, DBEVENTINFO *dbei)
// insert an event into a sub's history too
if (ccSub != NULL) {
- key2.dwContactId = ccSub->contactID;
+ key2.hContact = ccSub->contactID;
MDB_CHECK(mdb_put(txn, m_dbEventsSort, &key, &data, 0), 0);
ccSub->Advance(dwEventId, dbe);
@@ -137,7 +137,7 @@ STDMETHODIMP_(BOOL) CDbxMdb::DeleteEvent(MCONTACT contactID, MEVENT hDbEvent)
MDB_val key = { sizeof(MEVENT), &hDbEvent }, data;
if (mdb_get(txn, m_dbEvents, &key, &data) != MDB_SUCCESS)
return 1;
- memcpy(&dbe, data.mv_data, sizeof(dbe));
+ dbe = *(DBEvent*)data.mv_data;
}
if (contactID != dbe.contactID)
@@ -150,7 +150,7 @@ STDMETHODIMP_(BOOL) CDbxMdb::DeleteEvent(MCONTACT contactID, MEVENT hDbEvent)
for (Snapshot();; Revert(), Remap())
{
- DBEventSortingKey key2 = { hDbEvent, dbe.timestamp, contactID };
+ DBEventSortingKey key2 = { contactID, hDbEvent, dbe.timestamp };
txn_ptr txn(m_pMdbEnv);
MDB_val key = { sizeof(key2), &key2 }, data;
@@ -160,7 +160,7 @@ STDMETHODIMP_(BOOL) CDbxMdb::DeleteEvent(MCONTACT contactID, MEVENT hDbEvent)
{
key.mv_size = sizeof(MCONTACT); key.mv_data = &contactID;
cc->dbc.dwEventCount--;
- if (cc->dbc.dwFirstUnread == hDbEvent)
+ if (cc->dbc.evFirstUnread == hDbEvent)
FindNextUnread(txn, cc, key2);
data.mv_size = sizeof(DBContact); data.mv_data = &cc->dbc;
@@ -169,12 +169,12 @@ STDMETHODIMP_(BOOL) CDbxMdb::DeleteEvent(MCONTACT contactID, MEVENT hDbEvent)
if (cc2)
{
- key2.dwContactId = dbe.contactID;
+ key2.hContact = dbe.contactID;
MDB_CHECK(mdb_del(txn, m_dbEventsSort, &key, &data), 1);
key.mv_size = sizeof(MCONTACT); key.mv_data = &contactID;
cc2->dbc.dwEventCount--;
- if (cc2->dbc.dwFirstUnread == hDbEvent)
+ if (cc2->dbc.evFirstUnread == hDbEvent)
FindNextUnread(txn, cc2, key2);
data.mv_size = sizeof(DBContact); data.mv_data = &cc2->dbc;
@@ -221,7 +221,7 @@ STDMETHODIMP_(BOOL) CDbxMdb::GetEvent(MEVENT hDbEvent, DBEVENTINFO *dbei)
const DBEvent *dbe = (const DBEvent*)data.mv_data;
- dbei->szModule = GetModuleName(dbe->ofsModuleName);
+ dbei->szModule = GetModuleName(dbe->iModuleId);
dbei->timestamp = dbe->timestamp;
dbei->flags = dbe->flags;
dbei->eventType = dbe->wEventType;
@@ -260,13 +260,13 @@ void CDbxMdb::FindNextUnread(const txn_ptr &txn, DBCachedContact *cc, DBEventSor
if (dbe->contactID != cc->contactID)
break;
if (!dbe->markedRead()) {
- cc->dbc.dwFirstUnread = key2.dwEventId;
+ cc->dbc.evFirstUnread = key2.hEvent;
cc->dbc.tsFirstUnread = key2.ts;
return;
}
}
- cc->dbc.dwFirstUnread = cc->dbc.tsFirstUnread = 0;
+ cc->dbc.evFirstUnread = cc->dbc.tsFirstUnread = 0;
}
STDMETHODIMP_(BOOL) CDbxMdb::MarkEventRead(MCONTACT contactID, MEVENT hDbEvent)
@@ -277,7 +277,7 @@ STDMETHODIMP_(BOOL) CDbxMdb::MarkEventRead(MCONTACT contactID, MEVENT hDbEvent)
if (cc == NULL)
return -1;
- DWORD wRetVal = -1;
+ uint32_t wRetVal = -1;
for (cc->Snapshot();; cc->Revert(), Remap())
{
@@ -291,14 +291,16 @@ STDMETHODIMP_(BOOL) CDbxMdb::MarkEventRead(MCONTACT contactID, MEVENT hDbEvent)
if (cdbe->markedRead())
return cdbe->flags;
- DBEventSortingKey key2 = { hDbEvent, cdbe->timestamp, contactID };
+ DBEventSortingKey keyVal = { contactID, hDbEvent, cdbe->timestamp };
MDB_CHECK(mdb_put(txn, m_dbEvents, &key, &data, MDB_RESERVE), -1);
- memcpy(data.mv_data, cdbe, data.mv_size);
- wRetVal = (((DBEvent*)data.mv_data)->flags |= DBEF_READ);
+ DBEvent *pNewEvent = (DBEvent*)data.mv_data;
+ *pNewEvent = *cdbe;
- FindNextUnread(txn, cc, key2);
+ wRetVal = (pNewEvent->flags |= DBEF_READ);
+
+ FindNextUnread(txn, cc, keyVal);
key.mv_size = sizeof(MCONTACT); key.mv_data = &contactID;
data.mv_data = &cc->dbc; data.mv_size = sizeof(cc->dbc);
MDB_CHECK(mdb_put(txn, m_dbContacts, &key, &data, 0), -1);
@@ -322,16 +324,15 @@ STDMETHODIMP_(MCONTACT) CDbxMdb::GetEventContact(MEVENT hDbEvent)
if (mdb_get(txn, m_dbEvents, &key, &data) != MDB_SUCCESS)
return INVALID_CONTACT_ID;
- const DBEvent *dbe = (const DBEvent*)data.mv_data;
- return dbe->contactID;
+ return ((const DBEvent*)data.mv_data)->contactID;
}
-thread_local DWORD t_tsLast = 0;
+thread_local uint64_t t_tsLast = 0;
thread_local MEVENT t_evLast = 0;
STDMETHODIMP_(MEVENT) CDbxMdb::FindFirstEvent(MCONTACT contactID)
{
- DBEventSortingKey keyVal = { 0, 0, contactID };
+ DBEventSortingKey keyVal = { contactID, 0, 0 };
MDB_val key = { sizeof(keyVal), &keyVal }, data;
txn_ptr_ro txn(m_txn);
@@ -342,18 +343,18 @@ STDMETHODIMP_(MEVENT) CDbxMdb::FindFirstEvent(MCONTACT contactID)
const DBEventSortingKey *pKey = (const DBEventSortingKey*)key.mv_data;
t_tsLast = pKey->ts;
- return t_evLast = (pKey->dwContactId == contactID) ? pKey->dwEventId : 0;
+ return t_evLast = (pKey->hContact == contactID) ? pKey->hEvent : 0;
}
STDMETHODIMP_(MEVENT) CDbxMdb::FindFirstUnreadEvent(MCONTACT contactID)
{
DBCachedContact *cc = m_cache->GetCachedContact(contactID);
- return (cc == NULL) ? 0 : cc->dbc.dwFirstUnread;
+ return (cc == NULL) ? 0 : cc->dbc.evFirstUnread;
}
STDMETHODIMP_(MEVENT) CDbxMdb::FindLastEvent(MCONTACT contactID)
{
- DBEventSortingKey keyVal = { 0xFFFFFFFF, 0xFFFFFFFF, contactID };
+ DBEventSortingKey keyVal = { contactID, 0xFFFFFFFF, 0xFFFFFFFFFFFFFFFF };
MDB_val key = { sizeof(keyVal), &keyVal }, data;
txn_ptr_ro txn(m_txn);
@@ -372,7 +373,7 @@ STDMETHODIMP_(MEVENT) CDbxMdb::FindLastEvent(MCONTACT contactID)
const DBEventSortingKey *pKey = (const DBEventSortingKey*)key.mv_data;
t_tsLast = pKey->ts;
- return t_evLast = (pKey->dwContactId == contactID) ? pKey->dwEventId : 0;
+ return t_evLast = (pKey->hContact == contactID) ? pKey->hEvent : 0;
}
STDMETHODIMP_(MEVENT) CDbxMdb::FindNextEvent(MCONTACT contactID, MEVENT hDbEvent)
@@ -390,7 +391,7 @@ STDMETHODIMP_(MEVENT) CDbxMdb::FindNextEvent(MCONTACT contactID, MEVENT hDbEvent
t_tsLast = ((DBEvent*)data.mv_data)->timestamp;
}
- DBEventSortingKey keyVal = { hDbEvent, t_tsLast, contactID };
+ DBEventSortingKey keyVal = { contactID, hDbEvent, t_tsLast };
MDB_val key = { sizeof(keyVal), &keyVal }, data;
cursor_ptr_ro cursor(m_curEventsSort);
@@ -402,7 +403,7 @@ STDMETHODIMP_(MEVENT) CDbxMdb::FindNextEvent(MCONTACT contactID, MEVENT hDbEvent
const DBEventSortingKey *pKey = (const DBEventSortingKey*)key.mv_data;
t_tsLast = pKey->ts;
- return t_evLast = (pKey->dwContactId == contactID) ? pKey->dwEventId : 0;
+ return t_evLast = (pKey->hContact == contactID) ? pKey->hEvent : 0;
}
STDMETHODIMP_(MEVENT) CDbxMdb::FindPrevEvent(MCONTACT contactID, MEVENT hDbEvent)
@@ -422,7 +423,7 @@ STDMETHODIMP_(MEVENT) CDbxMdb::FindPrevEvent(MCONTACT contactID, MEVENT hDbEvent
t_tsLast = ((DBEvent*)data.mv_data)->timestamp;
}
- DBEventSortingKey keyVal = { hDbEvent, t_tsLast, contactID };
+ DBEventSortingKey keyVal = { contactID, hDbEvent, t_tsLast };
MDB_val key = { sizeof(keyVal), &keyVal };
cursor_ptr_ro cursor(m_curEventsSort);
@@ -434,5 +435,5 @@ STDMETHODIMP_(MEVENT) CDbxMdb::FindPrevEvent(MCONTACT contactID, MEVENT hDbEvent
const DBEventSortingKey *pKey = (const DBEventSortingKey*)key.mv_data;
t_tsLast = pKey->ts;
- return t_evLast = (pKey->dwContactId == contactID) ? pKey->dwEventId : 0;
+ return t_evLast = (pKey->hContact == contactID) ? pKey->hEvent : 0;
}
diff --git a/plugins/Dbx_mdb/src/dbintf.cpp b/plugins/Dbx_mdb/src/dbintf.cpp
index f27e819a4d..35f49aac12 100644
--- a/plugins/Dbx_mdb/src/dbintf.cpp
+++ b/plugins/Dbx_mdb/src/dbintf.cpp
@@ -27,8 +27,8 @@ CDbxMdb::CDbxMdb(const TCHAR *tszFileName, int iMode) :
m_safetyMode(true),
m_bReadOnly((iMode & DBMODE_READONLY) != 0),
m_bShared((iMode & DBMODE_SHARED) != 0),
- m_maxContactId(1),
- m_lResidentSettings(50, strcmp)
+ m_lResidentSettings(50, strcmp),
+ m_maxContactId(1)
{
m_tszProfileName = mir_tstrdup(tszFileName);
InitDbInstance(this);
@@ -36,6 +36,7 @@ CDbxMdb::CDbxMdb(const TCHAR *tszFileName, int iMode) :
mdb_env_create(&m_pMdbEnv);
mdb_env_set_maxdbs(m_pMdbEnv, 10);
mdb_env_set_userctx(m_pMdbEnv, this);
+// mdb_env_set_assert(m_pMdbEnv, LMDB_FailAssert);
m_codePage = Langpack_GetDefaultCodePage();
}
@@ -78,20 +79,24 @@ int CDbxMdb::Load(bool bSkipInit)
mdb_dbi_open(trnlck, "contacts", defFlags | MDB_INTEGERKEY, &m_dbContacts);
mdb_dbi_open(trnlck, "modules", defFlags | MDB_INTEGERKEY, &m_dbModules);
mdb_dbi_open(trnlck, "events", defFlags | MDB_INTEGERKEY, &m_dbEvents);
- mdb_dbi_open(trnlck, "eventsrt", defFlags | MDB_INTEGERKEY, &m_dbEventsSort);
+
+ mdb_dbi_open(trnlck, "eventsrt", defFlags, &m_dbEventsSort);
+ mdb_set_compare(trnlck, m_dbEventsSort, DBEventSortingKey::Compare);
+
mdb_dbi_open(trnlck, "settings", defFlags, &m_dbSettings);
+ mdb_set_compare(trnlck, m_dbSettings, DBSettingKey::Compare);
- DWORD keyVal = 1;
- MDB_val key = { sizeof(DWORD), &keyVal }, data;
+ uint32_t keyVal = 1;
+ MDB_val key = { sizeof(keyVal), &keyVal }, data;
if (mdb_get(trnlck, m_dbGlobal, &key, &data) == MDB_SUCCESS)
{
const DBHeader *hdr = (const DBHeader*)data.mv_data;
if (hdr->dwSignature != DBHEADER_SIGNATURE)
- DatabaseCorruption(NULL);
+ return EGROKPRF_DAMAGED;
if (hdr->dwVersion != DBHEADER_VERSION)
return EGROKPRF_OBSOLETE;
- memcpy(&m_header, data.mv_data, sizeof(m_header));
+ m_header = *hdr;
}
else
{
@@ -158,7 +163,6 @@ int CDbxMdb::Load(bool bSkipInit)
int CDbxMdb::Create(void)
{
- m_dwFileSize = 0;
return (Map() == MDB_SUCCESS) ? 0 : EGROKPRF_CANTREAD;
}
@@ -189,7 +193,6 @@ int CDbxMdb::PrepareCheck(int*)
STDMETHODIMP_(void) CDbxMdb::SetCacheSafetyMode(BOOL bIsSet)
{
- mir_cslock lck(m_csDbAccess);
m_safetyMode = bIsSet != 0;
}
@@ -232,9 +235,9 @@ EXTERN_C void __cdecl dbpanic(void *)
}
-EXTERN_C void LMDB_FailAssert(void *p, const char *text)
+EXTERN_C void LMDB_FailAssert(MDB_env *env, const char *text)
{
- ((CDbxMdb*)p)->DatabaseCorruption(_A2T(text));
+ ((CDbxMdb*)mdb_env_get_userctx(env))->DatabaseCorruption(_A2T(text));
}
EXTERN_C void LMDB_Log(const char *fmt, ...)
@@ -249,7 +252,6 @@ void CDbxMdb::DatabaseCorruption(const TCHAR *text)
{
int kill = 0;
- mir_cslockfull lck(m_csDbAccess);
if (DatabaseCorrupted == 0) {
DatabaseCorrupted++;
kill++;
@@ -262,7 +264,6 @@ void CDbxMdb::DatabaseCorruption(const TCHAR *text)
Sleep(INFINITE);
return;
}
- lck.unlock();
if (kill) {
_beginthread(dbpanic, 0, NULL);
diff --git a/plugins/Dbx_mdb/src/dbintf.h b/plugins/Dbx_mdb/src/dbintf.h
index 51ee91ed64..496a315589 100644
--- a/plugins/Dbx_mdb/src/dbintf.h
+++ b/plugins/Dbx_mdb/src/dbintf.h
@@ -37,30 +37,30 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include <pshpack1.h>
-#define DBHEADER_VERSION MAKELONG(1, 3)
+#define DBHEADER_VERSION MAKELONG(1, 4)
#define DBHEADER_SIGNATURE 0x40DECADEu
struct DBHeader
{
- DWORD dwSignature;
- DWORD dwVersion; // database format version
+ uint32_t dwSignature;
+ uint32_t dwVersion; // database format version
};
struct DBContact
{
- DWORD dwEventCount; // number of events in the chain for this contact
- DWORD tsFirstUnread;
- DWORD dwFirstUnread;
+ uint32_t dwEventCount; // number of events in the chain for this contact
+ MEVENT evFirstUnread;
+ uint64_t tsFirstUnread;
};
struct DBEvent
{
MCONTACT contactID; // a contact this event belongs to
- DWORD ofsModuleName; // offset to a DBModuleName struct of the name of
- DWORD timestamp; // seconds since 00:00:00 01/01/1970
- DWORD flags; // see m_database.h, db/event/add
- WORD wEventType; // module-defined event type
- WORD cbBlob; // number of bytes in the blob
+ uint32_t iModuleId; // offset to a DBModuleName struct of the name of
+ uint64_t timestamp; // seconds since 00:00:00 01/01/1970
+ uint32_t flags; // see m_database.h, db/event/add
+ uint16_t wEventType; // module-defined event type
+ uint16_t cbBlob; // number of bytes in the blob
bool __forceinline markedRead() const
{
@@ -70,14 +70,21 @@ struct DBEvent
struct DBEventSortingKey
{
- DWORD dwEventId, ts, dwContactId;
+ MCONTACT hContact;
+ MEVENT hEvent;
+ uint64_t ts;
+
+ static int Compare(const MDB_val* a, const MDB_val* b);
};
struct DBSettingKey
{
MCONTACT hContact;
- DWORD dwModuleId;
- char szSettingName[];
+ uint32_t dwModuleId;
+ char szSettingName[];
+
+ static int Compare(const MDB_val*, const MDB_val*);
+
};
struct DBSettingValue
@@ -102,7 +109,7 @@ struct DBSettingValue
struct DBCachedContact : public DBCachedContactBase
{
- void Advance(DWORD id, DBEvent &dbe);
+ void Advance(MEVENT id, DBEvent &dbe);
void Snapshot();
void Revert();
DBContact dbc, tmp_dbc;
@@ -110,12 +117,12 @@ struct DBCachedContact : public DBCachedContactBase
struct EventItem
{
- __forceinline EventItem(int _ts, DWORD _id) :
+ __forceinline EventItem(int _ts, MEVENT _id) :
ts(_ts), eventId(_id)
{}
- int ts;
- DWORD eventId;
+ uint64_t ts;
+ MEVENT eventId;
};
struct CDbxMdb : public MIDatabase, public MIDatabaseChecker, public MZeroedObject
@@ -208,15 +215,13 @@ public:
protected:
MDB_env *m_pMdbEnv;
- TXN_RO m_txn;
- DWORD m_dwFileSize;
+ CMDB_txn_ro m_txn;
+
MDB_dbi m_dbGlobal;
DBHeader m_header;
HANDLE hSettingChangeEvent, hContactDeletedEvent, hContactAddedEvent, hEventMarkedRead;
- mir_cs m_csDbAccess;
-
int CheckProto(DBCachedContact *cc, const char *proto);
////////////////////////////////////////////////////////////////////////////
@@ -228,13 +233,15 @@ protected:
int m_codePage;
HANDLE hService, hHook;
+ LIST<char> m_lResidentSettings;
+
////////////////////////////////////////////////////////////////////////////
// contacts
- MDB_dbi m_dbContacts;
+ MDB_dbi m_dbContacts;
MDB_cursor *m_curContacts;
- DWORD m_contactCount;
+ uint32_t m_contactCount;
MCONTACT m_maxContactId;
void GatherContactHistory(MCONTACT hContact, LIST<EventItem> &items);
@@ -242,9 +249,11 @@ protected:
////////////////////////////////////////////////////////////////////////////
// events
- MDB_dbi m_dbEvents, m_dbEventsSort;
+ MDB_dbi m_dbEvents, m_dbEventsSort;
MDB_cursor *m_curEvents, *m_curEventsSort;
- DWORD m_dwMaxEventId;
+ MEVENT m_dwMaxEventId;
+
+ HANDLE hEventAddedEvent, hEventDeletedEvent, hEventFilterAddedEvent;
void FindNextUnread(const txn_ptr &_txn, DBCachedContact *cc, DBEventSortingKey &key2);
@@ -254,16 +263,12 @@ protected:
MDB_dbi m_dbModules;
MDB_cursor *m_curModules;
- std::map<DWORD, std::string> m_Modules;
-
- LIST<char> m_lResidentSettings;
- HANDLE hEventAddedEvent, hEventDeletedEvent, hEventFilterAddedEvent;
- MCONTACT m_hLastCachedContact;
+ std::map<uint32_t, std::string> m_Modules;
int InitModules();
- DWORD GetModuleID(const char *szName);
- char* GetModuleName(DWORD dwId);
+ uint32_t GetModuleID(const char *szName);
+ char* GetModuleName(uint32_t dwId);
int GetContactSettingWorker(MCONTACT contactID, LPCSTR szModule, LPCSTR szSetting, DBVARIANT *dbv, int isStatic);
diff --git a/plugins/Dbx_mdb/src/dbmodulechain.cpp b/plugins/Dbx_mdb/src/dbmodulechain.cpp
index 142126c6dc..2b821023d6 100644
--- a/plugins/Dbx_mdb/src/dbmodulechain.cpp
+++ b/plugins/Dbx_mdb/src/dbmodulechain.cpp
@@ -31,7 +31,7 @@ int CDbxMdb::InitModules()
MDB_val key, data;
while (mdb_cursor_get(cursor, &key, &data, MDB_NEXT) == MDB_SUCCESS)
{
- DWORD iMod = *(DWORD*)key.mv_data;
+ uint32_t iMod = *(uint32_t*)key.mv_data;
const char *szMod = (const char*)data.mv_data;
m_Modules[iMod] = szMod;
}
@@ -39,10 +39,10 @@ int CDbxMdb::InitModules()
}
// will create the offset if it needs to
-DWORD CDbxMdb::GetModuleID(const char *szName)
+uint32_t CDbxMdb::GetModuleID(const char *szName)
{
- DWORD iHash = mir_hashstr(szName);
- if (auto it = m_Modules.find(iHash) == m_Modules.end())
+ uint32_t iHash = mir_hashstr(szName);
+ if (m_Modules.find(iHash) == m_Modules.end())
{
MDB_val key = { sizeof(iHash), &iHash }, data = { strlen(szName) + 1, (void*)szName };
@@ -58,7 +58,7 @@ DWORD CDbxMdb::GetModuleID(const char *szName)
return iHash;
}
-char* CDbxMdb::GetModuleName(DWORD dwId)
+char* CDbxMdb::GetModuleName(uint32_t dwId)
{
auto it = m_Modules.find(dwId);
return it != m_Modules.end() ? const_cast<char*>(it->second.c_str()) : nullptr;
diff --git a/plugins/Dbx_mdb/src/dbsettings.cpp b/plugins/Dbx_mdb/src/dbsettings.cpp
index 95faa27a68..9c48880618 100644
--- a/plugins/Dbx_mdb/src/dbsettings.cpp
+++ b/plugins/Dbx_mdb/src/dbsettings.cpp
@@ -345,7 +345,6 @@ STDMETHODIMP_(BOOL) CDbxMdb::SetSettingResident(BOOL bIsResident, const char *ps
char *szSetting = m_cache->GetCachedSetting(NULL, pszSettingName, 0, (int)strlen(pszSettingName));
szSetting[-1] = (char)bIsResident;
- mir_cslock lck(m_csDbAccess);
int idx = m_lResidentSettings.getIndex(szSetting);
if (idx == -1) {
if (bIsResident)
@@ -372,13 +371,11 @@ STDMETHODIMP_(BOOL) CDbxMdb::WriteContactSetting(MCONTACT contactID, DBCONTACTWR
{
if (dbcwNotif.value.pszVal != NULL)
{
- char* val = mir_utf8encodeW(dbcwNotif.value.pwszVal);
+ T2Utf val(dbcwNotif.value.pwszVal);
if (val == NULL)
return 1;
- dbcwNotif.value.pszVal = (char*)alloca(strlen(val) + 1);
- strcpy(dbcwNotif.value.pszVal, val);
- mir_free(val);
+ dbcwNotif.value.pszVal = NEWSTR_ALLOCA(val);
dbcwNotif.value.type = DBVT_UTF8;
}
else return 1;
diff --git a/plugins/Dbx_mdb/src/init.cpp b/plugins/Dbx_mdb/src/init.cpp
index 264ef433f4..18941a73d9 100644
--- a/plugins/Dbx_mdb/src/init.cpp
+++ b/plugins/Dbx_mdb/src/init.cpp
@@ -49,14 +49,14 @@ LIST<CDbxMdb> g_Dbs(1, HandleKeySortT);
// returns 0 if the profile is created, EMKPRF*
static int makeDatabase(const TCHAR *profile)
{
- std::auto_ptr<CDbxMdb> db(new CDbxMdb(profile, 0));
+ std::unique_ptr<CDbxMdb> db(new CDbxMdb(profile, 0));
return db->Create();
}
// returns 0 if the given profile has a valid header
static int grokHeader(const TCHAR *profile)
{
- std::auto_ptr<CDbxMdb> db(new CDbxMdb(profile, DBMODE_SHARED | DBMODE_READONLY));
+ std::unique_ptr<CDbxMdb> db(new CDbxMdb(profile, DBMODE_SHARED | DBMODE_READONLY));
return db->Check();
}
@@ -66,7 +66,7 @@ static MIDatabase* LoadDatabase(const TCHAR *profile, BOOL bReadOnly)
// set the memory, lists & UTF8 manager
mir_getLP(&pluginInfo);
- std::auto_ptr<CDbxMdb> db(new CDbxMdb(profile, (bReadOnly) ? DBMODE_READONLY : 0));
+ std::unique_ptr<CDbxMdb> db(new CDbxMdb(profile, (bReadOnly) ? DBMODE_READONLY : 0));
if (db->Load(false) != ERROR_SUCCESS)
return NULL;
@@ -83,7 +83,7 @@ static int UnloadDatabase(MIDatabase *db)
MIDatabaseChecker* CheckDb(const TCHAR *profile, int *error)
{
- std::auto_ptr<CDbxMdb> db(new CDbxMdb(profile, DBMODE_READONLY));
+ std::unique_ptr<CDbxMdb> db(new CDbxMdb(profile, DBMODE_READONLY));
if (db->Load(true) != ERROR_SUCCESS) {
*error = ERROR_ACCESS_DENIED;
return NULL;
diff --git a/plugins/Dbx_mdb/src/lmdb/mdb.c b/plugins/Dbx_mdb/src/lmdb/mdb.c
index 5fbe9beb9e..44499e4f1b 100644
--- a/plugins/Dbx_mdb/src/lmdb/mdb.c
+++ b/plugins/Dbx_mdb/src/lmdb/mdb.c
@@ -32,6 +32,7 @@
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
+
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif
diff --git a/plugins/Dbx_mdb/src/stdafx.h b/plugins/Dbx_mdb/src/stdafx.h
index 5360475a0c..128d323350 100644
--- a/plugins/Dbx_mdb/src/stdafx.h
+++ b/plugins/Dbx_mdb/src/stdafx.h
@@ -52,10 +52,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
class txn_ptr
{
- MDB_env *m_env;
MDB_txn *m_txn;
public:
- __forceinline txn_ptr(MDB_env *pEnv) : m_env(pEnv)
+ __forceinline txn_ptr(MDB_env *pEnv)
{
mdb_txn_begin(pEnv, NULL, 0, &m_txn);
}
@@ -82,13 +81,13 @@ public:
}
};
-struct TXN_RO
+struct CMDB_txn_ro
{
MDB_txn *m_txn;
bool bIsActive;
mir_cs cs;
- __forceinline TXN_RO() : m_txn(nullptr), bIsActive(false) {}
+ __forceinline CMDB_txn_ro() : m_txn(nullptr), bIsActive(false) {}
__forceinline operator MDB_txn* () { return m_txn; }
__forceinline MDB_txn** operator &() { return &m_txn; }
@@ -96,11 +95,11 @@ struct TXN_RO
class txn_ptr_ro
{
- TXN_RO &m_txn;
+ CMDB_txn_ro &m_txn;
bool bNeedReset;
mir_cslock lock;
public:
- __forceinline txn_ptr_ro(TXN_RO &txn) : m_txn(txn), bNeedReset(!txn.bIsActive), lock(m_txn.cs)
+ __forceinline txn_ptr_ro(CMDB_txn_ro &txn) : m_txn(txn), bNeedReset(!txn.bIsActive), lock(m_txn.cs)
{
if (bNeedReset)
{