From dd1fc18d107a2cac81acdd8055403bf8d8ca7413 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Thu, 11 Jan 2018 21:08:46 +0300 Subject: dbx_mdbx: - Remap() / MDBX_CHECK perversion removed; - mdbx_env_set_geometry() is used to grow file automatically --- plugins/Dbx_mdbx/src/dbcontacts.cpp | 75 ++++++++++++++++----------------- plugins/Dbx_mdbx/src/dbcrypt.cpp | 68 ++++++++++++++++-------------- plugins/Dbx_mdbx/src/dbevents.cpp | 77 ++++++++++++++++------------------ plugins/Dbx_mdbx/src/dbintf.cpp | 20 ++++----- plugins/Dbx_mdbx/src/dbintf.h | 1 - plugins/Dbx_mdbx/src/dbmodulechain.cpp | 11 ++--- plugins/Dbx_mdbx/src/dbsettings.cpp | 21 +++++----- plugins/Dbx_mdbx/src/stdafx.h | 57 +++++++++++++++---------- 8 files changed, 169 insertions(+), 161 deletions(-) (limited to 'plugins/Dbx_mdbx') diff --git a/plugins/Dbx_mdbx/src/dbcontacts.cpp b/plugins/Dbx_mdbx/src/dbcontacts.cpp index 1195c0af6e..1b8a269026 100644 --- a/plugins/Dbx_mdbx/src/dbcontacts.cpp +++ b/plugins/Dbx_mdbx/src/dbcontacts.cpp @@ -67,15 +67,15 @@ STDMETHODIMP_(LONG) CDbxMDBX::DeleteContact(MCONTACT contactID) } MDBX_val key = { &contactID, sizeof(MCONTACT) }; - for (;; Remap()) { + { txn_ptr trnlck(m_env); - MDBX_CHECK(mdbx_del(trnlck, m_dbContacts, &key, nullptr), 1); - if (trnlck.commit() == MDBX_SUCCESS) - break; + if (mdbx_del(trnlck, m_dbContacts, &key, nullptr) != MDBX_SUCCESS) + return 1; + if (trnlck.commit() != MDBX_SUCCESS) + return 1; } InterlockedDecrement(&m_contactCount); - return 0; } @@ -84,15 +84,15 @@ STDMETHODIMP_(MCONTACT) CDbxMDBX::AddContact() MCONTACT dwContactId = InterlockedIncrement(&m_maxContactId); DBCachedContact *cc = m_cache->AddContactToCache(dwContactId); + { + MDBX_val key = { &dwContactId, sizeof(MCONTACT) }; + MDBX_val data = { &cc->dbc, sizeof(cc->dbc) }; - MDBX_val key = { &dwContactId, sizeof(MCONTACT) }; - MDBX_val data = { &cc->dbc, sizeof(cc->dbc) }; - - for (;; Remap()) { txn_ptr trnlck(m_env); - MDBX_CHECK(mdbx_put(trnlck, m_dbContacts, &key, &data, 0), 0); - if (trnlck.commit() == MDBX_SUCCESS) - break; + if (mdbx_put(trnlck, m_dbContacts, &key, &data, 0) != MDBX_SUCCESS) + return 0; + if (trnlck.commit() != MDBX_SUCCESS) + return 0; } InterlockedIncrement(&m_contactCount); @@ -110,7 +110,7 @@ STDMETHODIMP_(BOOL) CDbxMDBX::IsDbContact(MCONTACT contactID) void CDbxMDBX::GatherContactHistory(MCONTACT hContact, LIST &list) { - DBEventSortingKey keyVal = { 0, 0, hContact }; + DBEventSortingKey keyVal = { hContact, 0, 0 }; MDBX_val key = { &keyVal, sizeof(keyVal) }, data; txn_ptr_ro trnlck(m_txn); @@ -132,27 +132,26 @@ BOOL CDbxMDBX::MetaMergeHistory(DBCachedContact *ccMeta, DBCachedContact *ccSub) for (int i = 0; i < list.getCount(); i++) { EventItem *EI = list[i]; - - for (;; Remap()) { + { txn_ptr trnlck(m_env); - DBEventSortingKey insVal = { EI->eventId, EI->ts, ccMeta->contactID }; + DBEventSortingKey insVal = { ccMeta->contactID, EI->eventId, EI->ts }; MDBX_val key = { &insVal, sizeof(insVal) }, data = { (void*)"", 1 }; - mdbx_put(trnlck, m_dbEventsSort, &key, &data, 0); - if (trnlck.commit() == MDBX_SUCCESS) - break; + + if (mdbx_put(trnlck, m_dbEventsSort, &key, &data, 0) != MDBX_SUCCESS) + return 1; + if (trnlck.commit() != MDBX_SUCCESS) + return 1; } ccMeta->dbc.dwEventCount++; delete EI; } MDBX_val keyc = { &ccMeta->contactID, sizeof(MCONTACT) }, datac = { &ccMeta->dbc, sizeof(ccMeta->dbc) }; - - for (;; Remap()) { - txn_ptr trnlck(m_env); - MDBX_CHECK(mdbx_put(trnlck, m_dbContacts, &keyc, &datac, 0), 1); - if (trnlck.commit() == MDBX_SUCCESS) - break; - } + txn_ptr trnlck(m_env); + if (mdbx_put(trnlck, m_dbContacts, &keyc, &datac, 0) != MDBX_SUCCESS) + return 1; + if (trnlck.commit() != MDBX_SUCCESS) + return 1; return 0; } @@ -165,27 +164,25 @@ BOOL CDbxMDBX::MetaSplitHistory(DBCachedContact *ccMeta, DBCachedContact *ccSub) for (int i = 0; i < list.getCount(); i++) { EventItem *EI = list[i]; - - for (;; Remap()) { + { txn_ptr trnlck(m_env); - DBEventSortingKey insVal = { EI->eventId, EI->ts, ccMeta->contactID }; + DBEventSortingKey insVal = { ccMeta->contactID, EI->eventId, EI->ts }; MDBX_val key = { &insVal, sizeof(insVal) }, data = { (void*)"", 1 }; - mdbx_del(trnlck, m_dbEventsSort, &key, &data); - if (trnlck.commit() == MDBX_SUCCESS) - break; + if (mdbx_del(trnlck, m_dbEventsSort, &key, &data) != MDBX_SUCCESS) + return 1; + if (trnlck.commit() != MDBX_SUCCESS) + return 1; } ccMeta->dbc.dwEventCount--; delete EI; } + txn_ptr trnlck(m_env); MDBX_val keyc = { &ccMeta->contactID, sizeof(MCONTACT) }, datac = { &ccMeta->dbc, sizeof(ccMeta->dbc) }; - - for (;; Remap()) { - txn_ptr trnlck(m_env); - MDBX_CHECK(mdbx_put(trnlck, m_dbContacts, &keyc, &datac, 0), 1); - if (trnlck.commit() == MDBX_SUCCESS) - break; - } + if(mdbx_put(trnlck, m_dbContacts, &keyc, &datac, 0) != MDBX_SUCCESS) + return 1; + if (trnlck.commit() != MDBX_SUCCESS) + return 1; return 0; } diff --git a/plugins/Dbx_mdbx/src/dbcrypt.cpp b/plugins/Dbx_mdbx/src/dbcrypt.cpp index 6142a7b577..590cf8494a 100644 --- a/plugins/Dbx_mdbx/src/dbcrypt.cpp +++ b/plugins/Dbx_mdbx/src/dbcrypt.cpp @@ -48,17 +48,18 @@ CRYPTO_PROVIDER* CDbxMDBX::SelectProvider() } else pProv = ppProvs[0]; - for (;; Remap()) { + { txn_ptr txn(m_env); - MDBX_val key = { DBKey_Crypto_Provider, sizeof(DBKey_Crypto_Provider) }, value = { pProv->pszName, mir_strlen(pProv->pszName) + 1 }; - MDBX_CHECK(mdbx_put(txn, m_dbCrypto, &key, &value, 0), nullptr); + if (mdbx_put(txn, m_dbCrypto, &key, &value, 0) != MDBX_SUCCESS) + return nullptr; key.iov_len = sizeof(DBKey_Crypto_IsEncrypted); key.iov_base = DBKey_Crypto_IsEncrypted; value.iov_len = sizeof(bool); value.iov_base = &bTotalCrypt; - MDBX_CHECK(mdbx_put(txn, m_dbCrypto, &key, &value, 0), nullptr); + if (mdbx_put(txn, m_dbCrypto, &key, &value, 0) != MDBX_SUCCESS) + return nullptr; - if (txn.commit() == MDBX_SUCCESS) - break; + if (txn.commit() != MDBX_SUCCESS) + return nullptr; } return pProv; @@ -124,14 +125,17 @@ void CDbxMDBX::StoreKey() size_t iKeyLength = m_crypto->getKeyLength(); BYTE *pKey = (BYTE*)_alloca(iKeyLength); m_crypto->getKey(pKey, iKeyLength); - - for (;; Remap()) { + { txn_ptr txn(m_env); MDBX_val key = { DBKey_Crypto_Key, sizeof(DBKey_Crypto_Key) }, value = { pKey, iKeyLength }; - mdbx_put(txn, m_dbCrypto, &key, &value, 0); - if (txn.commit() == MDBX_SUCCESS) - break; + int rc = mdbx_put(txn, m_dbCrypto, &key, &value, 0); + if (rc == MDBX_SUCCESS) + rc = txn.commit(); + /* FIXME: throw an exception */ + assert(rc == MDBX_SUCCESS); + UNREFERENCED_PARAMETER(rc); } + SecureZeroMemory(pKey, iKeyLength); } @@ -175,7 +179,10 @@ int CDbxMDBX::EnableEncryption(bool bEncrypted) for (auto it = lstEvents.begin(); it != lstEvents.end(); ++it) { MEVENT &hDbEvent = *it; MDBX_val key = { &hDbEvent, sizeof(MEVENT) }, data; - mdbx_get(txnro, m_dbEvents, &key, &data); + int rc = mdbx_get(txnro, m_dbEvents, &key, &data); + /* FIXME: throw an exception */ + assert(rc == MDBX_SUCCESS); + (void)rc; const DBEvent *dbEvent = (const DBEvent*)data.iov_base; const BYTE *pBlob = (BYTE*)(dbEvent + 1); @@ -194,32 +201,29 @@ int CDbxMDBX::EnableEncryption(bool bEncrypted) dwNewFlags = dbEvent->flags | DBEF_ENCRYPTED; } - for (;; Remap()) { - txn_ptr txn(m_env); - data.iov_len = sizeof(DBEvent) + nNewBlob; - MDBX_CHECK(mdbx_put(txn, m_dbEvents, &key, &data, MDBX_RESERVE), 1); - - DBEvent *pNewDBEvent = (DBEvent *)data.iov_base; - *pNewDBEvent = *dbEvent; - pNewDBEvent->cbBlob = (uint16_t)nNewBlob; - pNewDBEvent->flags = dwNewFlags; - memcpy(pNewDBEvent + 1, pNewBlob, nNewBlob); + txn_ptr txn(m_env); + data.iov_len = sizeof(DBEvent) + nNewBlob; + if (mdbx_put(txn, m_dbEvents, &key, &data, MDBX_RESERVE) != MDBX_SUCCESS) + return 1; + DBEvent *pNewDBEvent = (DBEvent *)data.iov_base; + *pNewDBEvent = *dbEvent; + pNewDBEvent->cbBlob = (uint16_t)nNewBlob; + pNewDBEvent->flags = dwNewFlags; + memcpy(pNewDBEvent + 1, pNewBlob, nNewBlob); - if (txn.commit() == MDBX_SUCCESS) - break; - } + if (txn.commit() != MDBX_SUCCESS) + return 1; } } } - for (;; Remap()) { - txn_ptr txn(m_env); - MDBX_val key = { DBKey_Crypto_IsEncrypted, sizeof(DBKey_Crypto_IsEncrypted) }, value = { &bEncrypted, sizeof(bool) }; - MDBX_CHECK(mdbx_put(txn, m_dbCrypto, &key, &value, 0), 1); - if (txn.commit() == MDBX_SUCCESS) - break; - } + txn_ptr txn(m_env); + MDBX_val key = { DBKey_Crypto_IsEncrypted, sizeof(DBKey_Crypto_IsEncrypted) }, value = { &bEncrypted, sizeof(bool) }; + if (mdbx_put(txn, m_dbCrypto, &key, &value, 0) != MDBX_SUCCESS) + return 1; + if (txn.commit() != MDBX_SUCCESS) + return 1; m_bEncrypted = bEncrypted; return 0; diff --git a/plugins/Dbx_mdbx/src/dbevents.cpp b/plugins/Dbx_mdbx/src/dbevents.cpp index 3ad663a5a9..4d4c76794d 100644 --- a/plugins/Dbx_mdbx/src/dbevents.cpp +++ b/plugins/Dbx_mdbx/src/dbevents.cpp @@ -77,17 +77,13 @@ STDMETHODIMP_(MEVENT) CDbxMDBX::AddEvent(MCONTACT contactID, DBEVENTINFO *dbei) } } - MEVENT dwEventId = InterlockedIncrement(&m_dwMaxEventId); - - const auto Snapshot = [&]() { cc->Snapshot(); if (ccSub) ccSub->Snapshot(); }; - const auto Revert = [&]() { cc->Revert(); if (ccSub) ccSub->Revert(); }; - - for (Snapshot();; Revert(), Remap()) { + { txn_ptr txn(m_env); MDBX_val key = { &dwEventId, sizeof(MEVENT) }, data = { NULL, sizeof(DBEvent) + dbe.cbBlob }; - MDBX_CHECK(mdbx_put(txn, m_dbEvents, &key, &data, MDBX_RESERVE), 0); + if (mdbx_put(txn, m_dbEvents, &key, &data, MDBX_RESERVE) != MDBX_SUCCESS) + return 0; DBEvent *pNewEvent = (DBEvent*)data.iov_base; *pNewEvent = dbe; @@ -97,25 +93,29 @@ STDMETHODIMP_(MEVENT) CDbxMDBX::AddEvent(MCONTACT contactID, DBEVENTINFO *dbei) DBEventSortingKey key2 = { contactID, dwEventId, dbe.timestamp }; key.iov_len = sizeof(key2); key.iov_base = &key2; data.iov_len = 1; data.iov_base = (char*)(""); - MDBX_CHECK(mdbx_put(txn, m_dbEventsSort, &key, &data, 0), 0); + if (mdbx_put(txn, m_dbEventsSort, &key, &data, 0) != MDBX_SUCCESS) + return 0; cc->Advance(dwEventId, dbe); MDBX_val keyc = { &contactID, sizeof(MCONTACT) }, datac = { &cc->dbc, sizeof(DBContact) }; - MDBX_CHECK(mdbx_put(txn, m_dbContacts, &keyc, &datac, 0), 0); + if (mdbx_put(txn, m_dbContacts, &keyc, &datac, 0) != MDBX_SUCCESS) + return 0; // insert an event into a sub's history too if (ccSub != NULL) { key2.hContact = ccSub->contactID; - MDBX_CHECK(mdbx_put(txn, m_dbEventsSort, &key, &data, 0), 0); + if (mdbx_put(txn, m_dbEventsSort, &key, &data, 0) != MDBX_SUCCESS) + return 0; ccSub->Advance(dwEventId, dbe); datac.iov_base = &ccSub->dbc; keyc.iov_base = &ccSub->contactID; - MDBX_CHECK(mdbx_put(txn, m_dbContacts, &keyc, &datac, 0), 0); + if (mdbx_put(txn, m_dbContacts, &keyc, &datac, 0) != MDBX_SUCCESS) + return 0; } - if (txn.commit() == MDBX_SUCCESS) - break; + if (txn.commit() != MDBX_SUCCESS) + return 0; } // Notify only in safe mode or on really new events @@ -140,20 +140,16 @@ STDMETHODIMP_(BOOL) CDbxMDBX::DeleteEvent(MCONTACT contactID, MEVENT hDbEvent) dbe = *(DBEvent*)data.iov_base; } - if (contactID != dbe.contactID) { + if (contactID != dbe.contactID) cc2 = m_cache->GetCachedContact(dbe.contactID); - } - - const auto Snapshot = [&]() { cc->Snapshot(); if (cc2) cc2->Snapshot(); }; - const auto Revert = [&]() { cc->Revert(); if (cc2) cc2->Revert(); }; - - for (Snapshot();; Revert(), Remap()) { - DBEventSortingKey key2 = { contactID, hDbEvent, dbe.timestamp }; + { txn_ptr txn(m_env); + DBEventSortingKey key2 = { contactID, hDbEvent, dbe.timestamp }; MDBX_val key = { &key2, sizeof(key2) }, data; - MDBX_CHECK(mdbx_del(txn, m_dbEventsSort, &key, &data), 1) + if (mdbx_del(txn, m_dbEventsSort, &key, &data) != MDBX_SUCCESS) + return 1; { key.iov_len = sizeof(MCONTACT); key.iov_base = &contactID; @@ -162,12 +158,14 @@ STDMETHODIMP_(BOOL) CDbxMDBX::DeleteEvent(MCONTACT contactID, MEVENT hDbEvent) FindNextUnread(txn, cc, key2); data.iov_len = sizeof(DBContact); data.iov_base = &cc->dbc; - MDBX_CHECK(mdbx_put(txn, m_dbContacts, &key, &data, 0), 1); + if (mdbx_put(txn, m_dbContacts, &key, &data, 0) != MDBX_SUCCESS) + return 1; } if (cc2) { key2.hContact = dbe.contactID; - MDBX_CHECK(mdbx_del(txn, m_dbEventsSort, &key, &data), 1); + if (mdbx_del(txn, m_dbEventsSort, &key, &data) != MDBX_SUCCESS) + return 1; key.iov_len = sizeof(MCONTACT); key.iov_base = &contactID; cc2->dbc.dwEventCount--; @@ -175,20 +173,20 @@ STDMETHODIMP_(BOOL) CDbxMDBX::DeleteEvent(MCONTACT contactID, MEVENT hDbEvent) FindNextUnread(txn, cc2, key2); data.iov_len = sizeof(DBContact); data.iov_base = &cc2->dbc; - MDBX_CHECK(mdbx_put(txn, m_dbContacts, &key, &data, 0), 1); - + if (mdbx_put(txn, m_dbContacts, &key, &data, 0) != MDBX_SUCCESS) + return 1; } // remove a event key.iov_len = sizeof(MEVENT); key.iov_base = &hDbEvent; - MDBX_CHECK(mdbx_del(txn, m_dbEvents, &key, &data), 1); + if (mdbx_del(txn, m_dbEvents, &key, &data) != MDBX_SUCCESS) + return 1; - if (txn.commit() == MDBX_SUCCESS) - break; + if (txn.commit() != MDBX_SUCCESS) + return 1; } NotifyEventHooks(hEventDeletedEvent, contactID, hDbEvent); - return 0; } @@ -272,21 +270,19 @@ STDMETHODIMP_(BOOL) CDbxMDBX::MarkEventRead(MCONTACT contactID, MEVENT hDbEvent) return -1; uint32_t wRetVal = -1; - - for (cc->Snapshot();; cc->Revert(), Remap()) { + { txn_ptr txn(m_env); - MDBX_val key = { &hDbEvent, sizeof(MEVENT) }, data; - MDBX_CHECK(mdbx_get(txn, m_dbEvents, &key, &data), -1); + if (mdbx_get(txn, m_dbEvents, &key, &data) != MDBX_SUCCESS) + return -1; const DBEvent *cdbe = (const DBEvent*)data.iov_base; - if (cdbe->markedRead()) return cdbe->flags; DBEventSortingKey keyVal = { contactID, hDbEvent, cdbe->timestamp }; - - MDBX_CHECK(mdbx_put(txn, m_dbEvents, &key, &data, MDBX_RESERVE), -1); + if (mdbx_put(txn, m_dbEvents, &key, &data, MDBX_RESERVE) != MDBX_SUCCESS) + return -1; DBEvent *pNewEvent = (DBEvent*)data.iov_base; *pNewEvent = *cdbe; @@ -296,10 +292,11 @@ STDMETHODIMP_(BOOL) CDbxMDBX::MarkEventRead(MCONTACT contactID, MEVENT hDbEvent) FindNextUnread(txn, cc, keyVal); key.iov_len = sizeof(MCONTACT); key.iov_base = &contactID; data.iov_base = &cc->dbc; data.iov_len = sizeof(cc->dbc); - MDBX_CHECK(mdbx_put(txn, m_dbContacts, &key, &data, 0), -1); + if (mdbx_put(txn, m_dbContacts, &key, &data, 0) != MDBX_SUCCESS) + return -1; - if (txn.commit() == MDBX_SUCCESS) - break; + if (txn.commit() != MDBX_SUCCESS) + return -1; } NotifyEventHooks(hEventMarkedRead, contactID, (LPARAM)hDbEvent); diff --git a/plugins/Dbx_mdbx/src/dbintf.cpp b/plugins/Dbx_mdbx/src/dbintf.cpp index 09d350b6c9..5fa6220a58 100644 --- a/plugins/Dbx_mdbx/src/dbintf.cpp +++ b/plugins/Dbx_mdbx/src/dbintf.cpp @@ -183,19 +183,19 @@ STDMETHODIMP_(void) CDbxMDBX::SetCacheSafetyMode(BOOL bIsSet) int CDbxMDBX::Map() { + int rc = mdbx_env_set_geometry(m_env, + -1 /* minimal lower limit */, + 64ul << 10 /* atleast 64K for now */, + 256ul << 20 /* 256M upper limit */, + 256ul << 10 /* 256K growth step */, + 512ul << 10 /* 512K shrink threshold */, + -1 /* default page size */); + if (rc != MDBX_SUCCESS) + return rc; unsigned int mode = MDBX_NOSUBDIR | MDBX_MAPASYNC | MDBX_WRITEMAP | MDBX_NOSYNC; if (m_bReadOnly) mode |= MDBX_RDONLY; - mdbx_env_open(m_env, _T2A(m_tszProfileName), mode, 0664); - mdbx_env_set_mapsize(m_env, 0x1000000); - return MDBX_SUCCESS; -} - -bool CDbxMDBX::Remap() -{ - MDBX_envinfo ei; - mdbx_env_info(m_env, &ei, sizeof(ei)); - return mdbx_env_set_geometry(m_env, -1, -1, ei.mi_mapsize + 0x100000, 0x100000, -1, -1) == MDBX_SUCCESS; + return mdbx_env_open(m_env, _T2A(m_tszProfileName), mode, 0664); } ///////////////////////////////////////////////////////////////////////////////////////// diff --git a/plugins/Dbx_mdbx/src/dbintf.h b/plugins/Dbx_mdbx/src/dbintf.h index fa5595dba3..6c1de8cd0b 100644 --- a/plugins/Dbx_mdbx/src/dbintf.h +++ b/plugins/Dbx_mdbx/src/dbintf.h @@ -191,7 +191,6 @@ protected: void FillContacts(void); int Map(); - bool Remap(); protected: TCHAR* m_tszProfileName; diff --git a/plugins/Dbx_mdbx/src/dbmodulechain.cpp b/plugins/Dbx_mdbx/src/dbmodulechain.cpp index b9b29b4c5a..b5f3546309 100644 --- a/plugins/Dbx_mdbx/src/dbmodulechain.cpp +++ b/plugins/Dbx_mdbx/src/dbmodulechain.cpp @@ -43,13 +43,14 @@ uint32_t CDbxMDBX::GetModuleID(const char *szName) uint32_t iHash = mir_hashstr(szName); if (m_Modules.find(iHash) == m_Modules.end()) { MDBX_val key = { &iHash, sizeof(iHash) }, data = { (void*)szName, strlen(szName) + 1 }; - - for (;; Remap()) { + { txn_ptr txn(m_env); - MDBX_CHECK(mdbx_put(txn, m_dbModules, &key, &data, 0), -1); - if (txn.commit() == MDBX_SUCCESS) - break; + if (mdbx_put(txn, m_dbModules, &key, &data, 0) != MDBX_SUCCESS) + return -1; + if (txn.commit() != MDBX_SUCCESS) + return -1; } + m_Modules[iHash] = szName; } diff --git a/plugins/Dbx_mdbx/src/dbsettings.cpp b/plugins/Dbx_mdbx/src/dbsettings.cpp index eacf9d82c4..1021c3d045 100644 --- a/plugins/Dbx_mdbx/src/dbsettings.cpp +++ b/plugins/Dbx_mdbx/src/dbsettings.cpp @@ -305,9 +305,10 @@ LBL_WriteString: data.iov_len = 3 + dbcwWork.value.cpbVal; break; } - for (;; Remap()) { + { txn_ptr trnlck(m_env); - MDBX_CHECK(mdbx_put(trnlck, m_dbSettings, &key, &data, MDBX_RESERVE), 1); + if (mdbx_put(trnlck, m_dbSettings, &key, &data, MDBX_RESERVE) != MDBX_SUCCESS) + return 1; BYTE *pBlob = (BYTE*)data.iov_base; *pBlob++ = dbcwWork.value.type; @@ -330,8 +331,8 @@ LBL_WriteString: memcpy(pBlob, dbcwWork.value.pbVal, dbcwWork.value.cpbVal); } - if (trnlck.commit() == MDBX_SUCCESS) - break; + if (trnlck.commit() != MDBX_SUCCESS) + return 1; } // notify @@ -356,14 +357,12 @@ STDMETHODIMP_(BOOL) CDbxMDBX::DeleteContactSetting(MCONTACT contactID, LPCSTR sz keyVal->dwModuleId = GetModuleID(szModule); memcpy(&keyVal->szSettingName, szSetting, settingNameLen + 1); + txn_ptr trnlck(m_env); MDBX_val key = { keyVal, sizeof(DBSettingKey) + settingNameLen }; - - for (;; Remap()) { - txn_ptr trnlck(m_env); - MDBX_CHECK(mdbx_del(trnlck, m_dbSettings, &key, nullptr), 1); - if (trnlck.commit() == MDBX_SUCCESS) - break; - } + if (mdbx_del(trnlck, m_dbSettings, &key, nullptr) != MDBX_SUCCESS) + return 1; + if (trnlck.commit() != MDBX_SUCCESS) + return 1; } m_cache->GetCachedValuePtr(contactID, szCachedSettingName, -1); diff --git a/plugins/Dbx_mdbx/src/stdafx.h b/plugins/Dbx_mdbx/src/stdafx.h index 25e91a4001..2e50a70e8b 100644 --- a/plugins/Dbx_mdbx/src/stdafx.h +++ b/plugins/Dbx_mdbx/src/stdafx.h @@ -29,6 +29,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include #include #include +#include #include #include @@ -50,34 +51,46 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # define thread_local __declspec(thread) #endif - class txn_ptr { MDBX_txn *m_txn; public: __forceinline txn_ptr(MDBX_env *pEnv) { - mdbx_txn_begin(pEnv, NULL, 0, &m_txn); + int rc = mdbx_txn_begin(pEnv, NULL, 0, &m_txn); + /* FIXME: throw an exception */ + assert(rc == MDBX_SUCCESS); + UNREFERENCED_PARAMETER(rc); } __forceinline ~txn_ptr() { - if (m_txn) - mdbx_txn_abort(m_txn); + if (m_txn) { + /* FIXME: see https://github.com/leo-yuriev/libfpta/blob/77a7251fde2030165a3916ee68fd86a1374b3dd8/src/common.cxx#L370 */ + abort(); + } } __forceinline operator MDBX_txn*() const { return m_txn; } __forceinline int commit() { - MDBX_txn *tmp = m_txn; + int rc = mdbx_txn_commit(m_txn); + if (rc != MDBX_SUCCESS) { + /* FIXME: throw an exception */ + abort(); + return rc; + } m_txn = nullptr; - return mdbx_txn_commit(tmp); + return MDBX_SUCCESS; } __forceinline void abort() { - mdbx_txn_abort(m_txn); + int rc = mdbx_txn_abort(m_txn); + /* FIXME: throw an exception */ + assert(rc == MDBX_SUCCESS); + UNREFERENCED_PARAMETER(rc); m_txn = NULL; } }; @@ -102,17 +115,21 @@ class txn_ptr_ro public: __forceinline txn_ptr_ro(CMDBX_txn_ro &txn) : m_txn(txn), bNeedReset(!txn.bIsActive), lock(m_txn.cs) { - if (bNeedReset) - { - mdbx_txn_renew(m_txn); + if (bNeedReset) { + int rc = mdbx_txn_renew(m_txn); + /* FIXME: throw an exception */ + assert(rc == MDBX_SUCCESS); + (void)rc; m_txn.bIsActive = true; } } __forceinline ~txn_ptr_ro() { - if (bNeedReset) - { - mdbx_txn_reset(m_txn); + if (bNeedReset) { + int rc = mdbx_txn_reset(m_txn); + /* FIXME: throw an exception */ + assert(rc == MDBX_SUCCESS); + (void)rc; m_txn.bIsActive = false; } } @@ -145,20 +162,14 @@ class cursor_ptr_ro public: __forceinline cursor_ptr_ro(MDBX_cursor *cursor) : m_cursor(cursor) { - mdbx_cursor_renew(mdbx_cursor_txn(m_cursor), m_cursor); + int rc = mdbx_cursor_renew(mdbx_cursor_txn(m_cursor), m_cursor); + /* FIXME: throw an exception */ + assert(rc == MDBX_SUCCESS); + UNREFERENCED_PARAMETER(rc); } __forceinline operator MDBX_cursor*() const { return m_cursor; } }; -#define MDBX_CHECK(A,B) \ - switch (A) { \ - case MDBX_SUCCESS: break; \ - case MDBX_MAP_FULL: continue; \ - default: return (B); } - - - - #include "dbintf.h" #include "resource.h" #include "version.h" -- cgit v1.2.3