From 80207f75648fad4294a77ff9ce50f0edaedffeca Mon Sep 17 00:00:00 2001
From: George Hazan <ghazan@miranda.im>
Date: Tue, 23 Jan 2018 19:51:30 +0300
Subject: dbx_mdbx:

- fix for a name conflict;
- stricter transaction scope;
- version bump
---
 plugins/Dbx_mdbx/src/dbcontacts.cpp    |  4 ++--
 plugins/Dbx_mdbx/src/dbcrypt.cpp       |  4 ++--
 plugins/Dbx_mdbx/src/dbevents.cpp      | 29 ++++++++++++++++-------------
 plugins/Dbx_mdbx/src/dbintf.cpp        | 16 ++++++++--------
 plugins/Dbx_mdbx/src/dbintf.h          |  2 +-
 plugins/Dbx_mdbx/src/dbmodulechain.cpp |  2 +-
 plugins/Dbx_mdbx/src/dbsettings.cpp    | 25 ++++++++++++++-----------
 plugins/Dbx_mdbx/src/stdafx.h          | 32 ++++++++++++++++----------------
 plugins/Dbx_mdbx/src/version.h         |  2 +-
 9 files changed, 61 insertions(+), 55 deletions(-)

(limited to 'plugins')

diff --git a/plugins/Dbx_mdbx/src/dbcontacts.cpp b/plugins/Dbx_mdbx/src/dbcontacts.cpp
index 39d5c2ed27..b0eb5ca2ee 100644
--- a/plugins/Dbx_mdbx/src/dbcontacts.cpp
+++ b/plugins/Dbx_mdbx/src/dbcontacts.cpp
@@ -113,7 +113,7 @@ void CDbxMDBX::GatherContactHistory(MCONTACT hContact, LIST<EventItem> &list)
 	DBEventSortingKey keyVal = { hContact, 0, 0 };
 	MDBX_val key = { &keyVal, sizeof(keyVal) }, data;
 
-	txn_ptr_ro trnlck(m_txn);
+	txn_ptr_ro trnlck(m_txn_ro);
 	cursor_ptr_ro cursor(m_curEventsSort);
 
 	for (int res = mdbx_cursor_get(cursor, &key, &data, MDBX_SET_RANGE); res == MDBX_SUCCESS; res = mdbx_cursor_get(cursor, &key, &data, MDBX_NEXT)) {
@@ -218,7 +218,7 @@ void DBCachedContact::Revert()
 void CDbxMDBX::FillContacts()
 {
 	{
-		txn_ptr_ro trnlck(m_txn);
+		txn_ptr_ro trnlck(m_txn_ro);
 		cursor_ptr_ro cursor(m_curContacts);
 
 		MDBX_val key, data;
diff --git a/plugins/Dbx_mdbx/src/dbcrypt.cpp b/plugins/Dbx_mdbx/src/dbcrypt.cpp
index 77c1f1c59b..1e73ddddcc 100644
--- a/plugins/Dbx_mdbx/src/dbcrypt.cpp
+++ b/plugins/Dbx_mdbx/src/dbcrypt.cpp
@@ -69,7 +69,7 @@ int CDbxMDBX::InitCrypt()
 {
 	CRYPTO_PROVIDER *pProvider;
 
-	txn_ptr_ro txn(m_txn);
+	txn_ptr_ro txn(m_txn_ro);
 
 	MDBX_val key = { DBKey_Crypto_Provider, sizeof(DBKey_Crypto_Provider) }, value;
 	if (mdbx_get(txn, m_dbCrypto, &key, &value) == MDBX_SUCCESS) {
@@ -160,7 +160,7 @@ int CDbxMDBX::EnableEncryption(bool bEncrypted)
 		return 0;
 
 	{
-		txn_ptr_ro txnro(m_txn);
+		txn_ptr_ro txnro(m_txn_ro);
 
 		MDBX_stat st;
 		mdbx_dbi_stat(txnro, m_dbEvents, &st, sizeof(st));
diff --git a/plugins/Dbx_mdbx/src/dbevents.cpp b/plugins/Dbx_mdbx/src/dbevents.cpp
index 43d8e788f1..59a93cf494 100644
--- a/plugins/Dbx_mdbx/src/dbevents.cpp
+++ b/plugins/Dbx_mdbx/src/dbevents.cpp
@@ -133,7 +133,7 @@ STDMETHODIMP_(BOOL) CDbxMDBX::DeleteEvent(MCONTACT contactID, MEVENT hDbEvent)
 
 	DBEvent dbe;
 	{
-		txn_ptr_ro txn(m_txn);
+		txn_ptr_ro txn(m_txn_ro);
 		MDBX_val key = { &hDbEvent, sizeof(MEVENT) }, data;
 		if (mdbx_get(txn, m_dbEvents, &key, &data) != MDBX_SUCCESS)
 			return 1;
@@ -192,7 +192,7 @@ STDMETHODIMP_(BOOL) CDbxMDBX::DeleteEvent(MCONTACT contactID, MEVENT hDbEvent)
 
 STDMETHODIMP_(LONG) CDbxMDBX::GetBlobSize(MEVENT hDbEvent)
 {
-	txn_ptr_ro txn(m_txn);
+	txn_ptr_ro txn(m_txn_ro);
 
 	MDBX_val key = { &hDbEvent, sizeof(MEVENT) }, data;
 	if (mdbx_get(txn, m_dbEvents, &key, &data) != MDBX_SUCCESS)
@@ -208,13 +208,16 @@ STDMETHODIMP_(BOOL) CDbxMDBX::GetEvent(MEVENT hDbEvent, DBEVENTINFO *dbei)
 		return 1;
 	}
 
-	txn_ptr_ro txn(m_txn);
+	const DBEvent *dbe;
+	{
+		txn_ptr_ro txn(m_txn_ro);
 
-	MDBX_val key = { &hDbEvent, sizeof(MEVENT) }, data;
-	if (mdbx_get(txn, m_dbEvents, &key, &data) != MDBX_SUCCESS)
-		return 1;
+		MDBX_val key = { &hDbEvent, sizeof(MEVENT) }, data;
+		if (mdbx_get(txn, m_dbEvents, &key, &data) != MDBX_SUCCESS)
+			return 1;
 
-	const DBEvent *dbe = (const DBEvent*)data.iov_base;
+		dbe = (const DBEvent*)data.iov_base;
+	}
 
 	dbei->szModule = GetModuleName(dbe->iModuleId);
 	dbei->timestamp = dbe->timestamp;
@@ -223,7 +226,7 @@ STDMETHODIMP_(BOOL) CDbxMDBX::GetEvent(MEVENT hDbEvent, DBEVENTINFO *dbei)
 	size_t bytesToCopy = min(dbei->cbBlob, dbe->cbBlob);
 	dbei->cbBlob = dbe->cbBlob;
 	if (bytesToCopy && dbei->pBlob) {
-		BYTE *pSrc = (BYTE*)data.iov_base + sizeof(DBEvent);
+		BYTE *pSrc = (BYTE*)dbe + sizeof(DBEvent);
 		if (dbe->flags & DBEF_ENCRYPTED) {
 			dbei->flags &= ~DBEF_ENCRYPTED;
 			size_t len;
@@ -308,7 +311,7 @@ STDMETHODIMP_(MCONTACT) CDbxMDBX::GetEventContact(MEVENT hDbEvent)
 	if (hDbEvent == 0)
 		return INVALID_CONTACT_ID;
 
-	txn_ptr_ro txn(m_txn);
+	txn_ptr_ro txn(m_txn_ro);
 
 	MDBX_val key = { &hDbEvent, sizeof(MEVENT) }, data;
 	if (mdbx_get(txn, m_dbEvents, &key, &data) != MDBX_SUCCESS)
@@ -325,7 +328,7 @@ STDMETHODIMP_(MEVENT) CDbxMDBX::FindFirstEvent(MCONTACT contactID)
 	DBEventSortingKey keyVal = { contactID, 0, 0 };
 	MDBX_val key = { &keyVal, sizeof(keyVal) }, data;
 
-	txn_ptr_ro txn(m_txn);
+	txn_ptr_ro txn(m_txn_ro);
 
 	cursor_ptr_ro cursor(m_curEventsSort);
 	if (mdbx_cursor_get(cursor, &key, &data, MDBX_SET_RANGE) != MDBX_SUCCESS)
@@ -347,7 +350,7 @@ STDMETHODIMP_(MEVENT) CDbxMDBX::FindLastEvent(MCONTACT contactID)
 	DBEventSortingKey keyVal = { contactID, 0xFFFFFFFF, 0xFFFFFFFFFFFFFFFF };
 	MDBX_val key = { &keyVal, sizeof(keyVal) }, data;
 
-	txn_ptr_ro txn(m_txn);
+	txn_ptr_ro txn(m_txn_ro);
 	cursor_ptr_ro cursor(m_curEventsSort);
 
 	if (mdbx_cursor_get(cursor, &key, &data, MDBX_SET_RANGE) != MDBX_SUCCESS) {
@@ -369,7 +372,7 @@ STDMETHODIMP_(MEVENT) CDbxMDBX::FindNextEvent(MCONTACT contactID, MEVENT hDbEven
 	if (hDbEvent == 0)
 		return t_evLast = 0;
 
-	txn_ptr_ro txn(m_txn);
+	txn_ptr_ro txn(m_txn_ro);
 
 	if (t_evLast != hDbEvent) {
 		MDBX_val key = { &hDbEvent, sizeof(MEVENT) }, data;
@@ -400,7 +403,7 @@ STDMETHODIMP_(MEVENT) CDbxMDBX::FindPrevEvent(MCONTACT contactID, MEVENT hDbEven
 
 	MDBX_val data;
 
-	txn_ptr_ro txn(m_txn);
+	txn_ptr_ro txn(m_txn_ro);
 
 	if (t_evLast != hDbEvent) {
 		MDBX_val key = { &hDbEvent, sizeof(MEVENT) };
diff --git a/plugins/Dbx_mdbx/src/dbintf.cpp b/plugins/Dbx_mdbx/src/dbintf.cpp
index 7bdfa8b7d1..183ea4130d 100644
--- a/plugins/Dbx_mdbx/src/dbintf.cpp
+++ b/plugins/Dbx_mdbx/src/dbintf.cpp
@@ -101,25 +101,25 @@ int CDbxMDBX::Load(bool bSkipInit)
 		{
 			MDBX_val key, val;
 
-			mdbx_txn_begin(m_env, nullptr, MDBX_RDONLY, &m_txn);
+			mdbx_txn_begin(m_env, nullptr, MDBX_RDONLY, &m_txn_ro);
 
-			mdbx_cursor_open(m_txn, m_dbEvents, &m_curEvents);
+			mdbx_cursor_open(m_txn_ro, m_dbEvents, &m_curEvents);
 			if (mdbx_cursor_get(m_curEvents, &key, &val, MDBX_LAST) == MDBX_SUCCESS)
 				m_dwMaxEventId = *(MEVENT*)key.iov_base;
 
-			mdbx_cursor_open(m_txn, m_dbEventsSort, &m_curEventsSort);
-			mdbx_cursor_open(m_txn, m_dbSettings, &m_curSettings);
-			mdbx_cursor_open(m_txn, m_dbModules, &m_curModules);
+			mdbx_cursor_open(m_txn_ro, m_dbEventsSort, &m_curEventsSort);
+			mdbx_cursor_open(m_txn_ro, m_dbSettings, &m_curSettings);
+			mdbx_cursor_open(m_txn_ro, m_dbModules, &m_curModules);
 
-			mdbx_cursor_open(m_txn, m_dbContacts, &m_curContacts);
+			mdbx_cursor_open(m_txn_ro, m_dbContacts, &m_curContacts);
 			if (mdbx_cursor_get(m_curContacts, &key, &val, MDBX_LAST) == MDBX_SUCCESS)
 				m_maxContactId = *(MCONTACT*)key.iov_base;
 
 			MDBX_stat st;
-			mdbx_dbi_stat(m_txn, m_dbContacts, &st, sizeof(st));
+			mdbx_dbi_stat(m_txn_ro, m_dbContacts, &st, sizeof(st));
 			m_contactCount = st.ms_entries;
 
-			mdbx_txn_reset(m_txn);
+			mdbx_txn_reset(m_txn_ro);
 		}
 
 
diff --git a/plugins/Dbx_mdbx/src/dbintf.h b/plugins/Dbx_mdbx/src/dbintf.h
index 25ff6d07e3..a68e8ab4bc 100644
--- a/plugins/Dbx_mdbx/src/dbintf.h
+++ b/plugins/Dbx_mdbx/src/dbintf.h
@@ -201,7 +201,7 @@ public:
 
 protected:
 	MDBX_env *m_env;
-	CMDBX_txn_ro m_txn;
+	CMDBX_txn_ro m_txn_ro;
 
 	MDBX_dbi  m_dbGlobal;
 	DBHeader m_header;
diff --git a/plugins/Dbx_mdbx/src/dbmodulechain.cpp b/plugins/Dbx_mdbx/src/dbmodulechain.cpp
index b5f3546309..aa2427348a 100644
--- a/plugins/Dbx_mdbx/src/dbmodulechain.cpp
+++ b/plugins/Dbx_mdbx/src/dbmodulechain.cpp
@@ -25,7 +25,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 int CDbxMDBX::InitModules()
 {
-	txn_ptr_ro trnlck(m_txn);
+	txn_ptr_ro trnlck(m_txn_ro);
 	cursor_ptr_ro cursor(m_curModules);
 
 	MDBX_val key, data;
diff --git a/plugins/Dbx_mdbx/src/dbsettings.cpp b/plugins/Dbx_mdbx/src/dbsettings.cpp
index fe7b40626b..312d4aa68e 100644
--- a/plugins/Dbx_mdbx/src/dbsettings.cpp
+++ b/plugins/Dbx_mdbx/src/dbsettings.cpp
@@ -80,18 +80,22 @@ LBL_Seek:
 	if (szCachedSettingName[-1] != 0)
 		return 1;
 
+	int res;
+	const BYTE *pBlob;
 	DBCachedContact *cc = (contactID) ? m_cache->GetCachedContact(contactID) : nullptr;
+	{
+		txn_ptr_ro trnlck(m_txn_ro);
 
-	txn_ptr_ro trnlck(m_txn);
-
-	DBSettingKey *keyVal = (DBSettingKey *)_alloca(sizeof(DBSettingKey) + settingNameLen);
-	keyVal->hContact = contactID;
-	keyVal->dwModuleId = GetModuleID(szModule);
-	memcpy(&keyVal->szSettingName, szSetting, settingNameLen + 1);
-
+		DBSettingKey *keyVal = (DBSettingKey *)_alloca(sizeof(DBSettingKey) + settingNameLen);
+		keyVal->hContact = contactID;
+		keyVal->dwModuleId = GetModuleID(szModule);
+		memcpy(&keyVal->szSettingName, szSetting, settingNameLen + 1);
 
-	MDBX_val key = { keyVal,  sizeof(DBSettingKey) + settingNameLen }, data;
-	if (mdbx_get(trnlck, m_dbSettings, &key, &data) != MDBX_SUCCESS) {
+		MDBX_val key = { keyVal,  sizeof(DBSettingKey) + settingNameLen }, data;
+		res = mdbx_get(trnlck, m_dbSettings, &key, &data);
+		pBlob = (const BYTE*)data.iov_base;
+	}
+	if (res != MDBX_SUCCESS) {
 		// try to get the missing mc setting from the active sub
 		if (cc && cc->IsMeta() && ValidLookupName(szModule, szSetting)) {
 			if (contactID = db_mc_getDefault(contactID)) {
@@ -104,7 +108,6 @@ LBL_Seek:
 		return 1;
 	}
 
-	const BYTE *pBlob = (const BYTE*)data.iov_base;
 	if (isStatic && (pBlob[0] & DBVTF_VARIABLELENGTH) && VLT(dbv->type) != VLT(pBlob[0]))
 		return 1;
 
@@ -381,7 +384,7 @@ STDMETHODIMP_(BOOL) CDbxMDBX::EnumContactSettings(MCONTACT hContact, DBSETTINGEN
 	int result = -1;
 
 	DBSettingKey keyVal = { hContact, GetModuleID(szModule), 0 };
-	txn_ptr_ro txn(m_txn);
+	txn_ptr_ro txn(m_txn_ro);
 	cursor_ptr_ro cursor(m_curSettings);
 
 	LIST<char> arKeys(100);
diff --git a/plugins/Dbx_mdbx/src/stdafx.h b/plugins/Dbx_mdbx/src/stdafx.h
index f7f0ea0391..428860127c 100644
--- a/plugins/Dbx_mdbx/src/stdafx.h
+++ b/plugins/Dbx_mdbx/src/stdafx.h
@@ -53,11 +53,11 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 class txn_ptr
 {
-	MDBX_txn *m_txn;
+	MDBX_txn *txn;
 public:
 	__forceinline txn_ptr(MDBX_env *pEnv)
 	{
-		int rc = mdbx_txn_begin(pEnv, nullptr, 0, &m_txn);
+		int rc = mdbx_txn_begin(pEnv, nullptr, 0, &txn);
 		/* FIXME: throw an exception */
 		_ASSERT(rc == MDBX_SUCCESS);
 		UNREFERENCED_PARAMETER(rc);
@@ -65,66 +65,66 @@ public:
 
 	__forceinline ~txn_ptr()
 	{
-		if (m_txn) {
+		if (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 operator MDBX_txn*() const { return txn; }
 
 	__forceinline int commit()
 	{
-		int rc = mdbx_txn_commit(m_txn);
+		int rc = mdbx_txn_commit(txn);
 		if (rc != MDBX_SUCCESS) {
 			/* FIXME: throw an exception */
 			abort();
 			return rc;
 		}
-		m_txn = nullptr;
+		txn = nullptr;
 		return MDBX_SUCCESS;
 	}
 
 	__forceinline void abort()
 	{
-		int rc = mdbx_txn_abort(m_txn);
+		int rc = mdbx_txn_abort(txn);
 		/* FIXME: throw an exception */
 		_ASSERT(rc == MDBX_SUCCESS);
 		UNREFERENCED_PARAMETER(rc);
-		m_txn = nullptr;
+		txn = nullptr;
 	}
 };
 
 struct CMDBX_txn_ro
 {
-	MDBX_txn *m_txn = nullptr;
+	MDBX_txn *txn = nullptr;
 	mir_cs cs;
 
-	__forceinline operator MDBX_txn* () { return m_txn; }
-	__forceinline MDBX_txn** operator &() { return &m_txn; }
+	__forceinline operator MDBX_txn* () { return txn; }
+	__forceinline MDBX_txn** operator &() { return &txn; }
 };
 
 class txn_ptr_ro
 {
-	CMDBX_txn_ro &m_txn;
+	CMDBX_txn_ro &txn;
 	mir_cslock lock;
 
 public:
-	__forceinline txn_ptr_ro(CMDBX_txn_ro &txn) : m_txn(txn), lock(m_txn.cs)
+	__forceinline txn_ptr_ro(CMDBX_txn_ro &_txn) : txn(_txn), lock(txn.cs)
 	{
-		int rc = mdbx_txn_renew(m_txn);
+		int rc = mdbx_txn_renew(txn);
 		/* FIXME: throw an exception */
 		_ASSERT(rc == MDBX_SUCCESS);
 		UNREFERENCED_PARAMETER(rc);
 	}
 	__forceinline ~txn_ptr_ro()
 	{
-		int rc = mdbx_txn_reset(m_txn);
+		int rc = mdbx_txn_reset(txn);
 		/* FIXME: throw an exception */
 		_ASSERT(rc == MDBX_SUCCESS);
 		UNREFERENCED_PARAMETER(rc);
 	}
-	__forceinline operator MDBX_txn*() const { return m_txn; }
+	__forceinline operator MDBX_txn*() const { return txn; }
 };
 
 class cursor_ptr
diff --git a/plugins/Dbx_mdbx/src/version.h b/plugins/Dbx_mdbx/src/version.h
index cef7f37b48..f8079874d9 100644
--- a/plugins/Dbx_mdbx/src/version.h
+++ b/plugins/Dbx_mdbx/src/version.h
@@ -1,7 +1,7 @@
 #define __MAJOR_VERSION          0
 #define __MINOR_VERSION         95
 #define __RELEASE_NUM            8
-#define __BUILD_NUM              2
+#define __BUILD_NUM              3
 
 #include <stdver.h>
 
-- 
cgit v1.2.3