From e37f12dec252a78cdf94d31ccc87ce37c1436a1f Mon Sep 17 00:00:00 2001 From: George Hazan Date: Wed, 14 Jan 2015 14:29:52 +0000 Subject: txn_ptr & cursor_ptr classes git-svn-id: http://svn.miranda-ng.org/main/trunk@11853 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/Dbx_mdb/src/commonheaders.h | 48 ++++++++++++++++++++++++----------- plugins/Dbx_mdb/src/dbcontacts.cpp | 47 ++++++++++++++++------------------ plugins/Dbx_mdb/src/dbintf.cpp | 16 ++++++------ plugins/Dbx_mdb/src/dbmodulechain.cpp | 11 +++----- plugins/Dbx_mdb/src/dbsettings.cpp | 38 +++++++++++++-------------- 5 files changed, 86 insertions(+), 74 deletions(-) (limited to 'plugins/Dbx_mdb/src') diff --git a/plugins/Dbx_mdb/src/commonheaders.h b/plugins/Dbx_mdb/src/commonheaders.h index 7b0c0bc0ad..3daf109bef 100644 --- a/plugins/Dbx_mdb/src/commonheaders.h +++ b/plugins/Dbx_mdb/src/commonheaders.h @@ -56,38 +56,56 @@ extern "C" extern HINSTANCE g_hInst; extern LIST g_Dbs; -class txn_lock +class txn_ptr { - MDB_txn *txn; - MDB_env *env; + MDB_txn *m_txn; public: - __forceinline txn_lock(MDB_env *pEnv) : - env(pEnv) - { - mdb_txn_begin(pEnv, NULL, 0, &txn); + __forceinline txn_ptr(MDB_env *pEnv, bool bReadOnly = false) + { + mdb_txn_begin(pEnv, NULL, (bReadOnly) ? MDB_RDONLY : 0, &m_txn); } - __forceinline ~txn_lock() + __forceinline ~txn_ptr() { - if (txn) - mdb_txn_abort(txn); + if (m_txn) + mdb_txn_abort(m_txn); } - __forceinline operator MDB_txn*() const { return txn; } + __forceinline operator MDB_txn*() const { return m_txn; } __forceinline bool commit() { - bool bRes = (mdb_txn_commit(txn) != MDB_MAP_FULL); - txn = NULL; + bool bRes = (mdb_txn_commit(m_txn) != MDB_MAP_FULL); + m_txn = NULL; return bRes; } __forceinline void abort() { - mdb_txn_abort(txn); - txn = NULL; + mdb_txn_abort(m_txn); + m_txn = NULL; + } +}; + +class cursor_ptr +{ + MDB_cursor *m_cursor; + +public: + __forceinline cursor_ptr(const txn_ptr &_txn, MDB_dbi _dbi) + { + if (mdb_cursor_open(_txn, _dbi, &m_cursor) != MDB_SUCCESS) + m_cursor = NULL; + } + + __forceinline ~cursor_ptr() + { + if (m_cursor) + mdb_cursor_close(m_cursor); } + + __forceinline operator MDB_cursor*() const { return m_cursor; } }; #ifdef __GNUC__ diff --git a/plugins/Dbx_mdb/src/dbcontacts.cpp b/plugins/Dbx_mdb/src/dbcontacts.cpp index 0a6e6859f1..48c8b7e7cb 100644 --- a/plugins/Dbx_mdb/src/dbcontacts.cpp +++ b/plugins/Dbx_mdb/src/dbcontacts.cpp @@ -87,14 +87,14 @@ STDMETHODIMP_(LONG) CDbxMdb::DeleteContact(MCONTACT contactID) // delete mir_cslock lck(m_csDbAccess); - MDB_val key = { sizeof(DWORD), &contactID }; + MDB_val key = { sizeof(DWORD), &contactID }; for (;; Remap()) { - txn_lock trnlck(m_pMdbEnv); - mdb_del(trnlck, m_dbContacts, &key, NULL); - if (trnlck.commit()) - break; - } + txn_ptr trnlck(m_pMdbEnv); + mdb_del(trnlck, m_dbContacts, &key, NULL); + if (trnlck.commit()) + break; + } return 0; } @@ -107,17 +107,17 @@ STDMETHODIMP_(MCONTACT) CDbxMdb::AddContact() dbc.eventCount = 0; { mir_cslock lck(m_csDbAccess); - dwContactId = m_dwMaxContactId++; - - MDB_val key = { sizeof(DWORD), &dwContactId }; - MDB_val data = { sizeof(DBContact), &dbc }; - + dwContactId = m_dwMaxContactId++; + + MDB_val key = { sizeof(DWORD), &dwContactId }; + MDB_val data = { sizeof(DBContact), &dbc }; + for (;; Remap()) { - txn_lock trnlck(m_pMdbEnv); - mdb_put(trnlck, m_dbContacts, &key, &data, 0); - if (trnlck.commit()) - break; - } + txn_ptr trnlck(m_pMdbEnv); + mdb_put(trnlck, m_dbContacts, &key, &data, 0); + if (trnlck.commit()) + break; + } DBCachedContact *cc = m_cache->AddContactToCache(dwContactId); cc->dwDriverData = 0; @@ -173,19 +173,18 @@ void CDbxMdb::FillContacts() { m_contactCount = 0; - txn_lock trnlck(m_pMdbEnv); - mdb_open(trnlck, "contacts", MDB_INTEGERKEY, &m_dbContacts); + txn_ptr trnlck(m_pMdbEnv); + mdb_open(trnlck, "contacts", MDB_INTEGERKEY, &m_dbContacts); + + cursor_ptr cursor(trnlck, m_dbContacts); - MDB_cursor *cursor; - mdb_cursor_open(trnlck, m_dbContacts, &cursor); - - MDB_val key, data; + MDB_val key, data; while (mdb_cursor_get(cursor, &key, &data, MDB_NEXT) == 0) { DBContact *dbc = (DBContact*)data.mv_data; if (dbc->signature != DBCONTACT_SIGNATURE) DatabaseCorruption(NULL); - DWORD dwContactId = *(DWORD*)key.mv_data; + DWORD dwContactId = *(DWORD*)key.mv_data; DBCachedContact *cc = m_cache->AddContactToCache(dwContactId); cc->dwDriverData = 0; CheckProto(cc, ""); @@ -206,6 +205,4 @@ void CDbxMdb::FillContacts() cc->nDefault = (0 != GetContactSetting(dwContactId, META_PROTO, "Default", &dbv)) ? -1 : dbv.dVal; cc->parentID = (0 != GetContactSetting(dwContactId, META_PROTO, "ParentMeta", &dbv)) ? NULL : dbv.dVal; } - - mdb_cursor_close(cursor); } diff --git a/plugins/Dbx_mdb/src/dbintf.cpp b/plugins/Dbx_mdb/src/dbintf.cpp index b889ea8f5b..48f7deb1db 100644 --- a/plugins/Dbx_mdb/src/dbintf.cpp +++ b/plugins/Dbx_mdb/src/dbintf.cpp @@ -52,7 +52,7 @@ CDbxMdb::CDbxMdb(const TCHAR *tszFileName, int iMode) : mdb_env_create(&m_pMdbEnv); mdb_env_set_maxdbs(m_pMdbEnv, 10); - + m_codePage = CallService(MS_LANGPACK_GETCODEPAGE, 0, 0); m_hModHeap = HeapCreate(0, 0, 0); } @@ -62,7 +62,7 @@ CDbxMdb::~CDbxMdb() // destroy modules HeapDestroy(m_hModHeap); - mdb_env_close(m_pMdbEnv); + mdb_env_close(m_pMdbEnv); DestroyServiceFunction(hService); UnhookEvent(hHook); @@ -89,12 +89,12 @@ int CDbxMdb::Load(bool bSkipInit) return EGROKPRF_CANTREAD; if (!bSkipInit) { - txn_lock trnlck(m_pMdbEnv); - mdb_open(trnlck, "contacts", MDB_CREATE | MDB_INTEGERKEY, &m_dbContacts); - mdb_open(trnlck, "modules", MDB_CREATE | MDB_INTEGERKEY, &m_dbModules); - mdb_open(trnlck, "events", MDB_CREATE | MDB_INTEGERKEY, &m_dbEvents); - mdb_open(trnlck, "settings", MDB_CREATE, &m_dbSettings); - trnlck.commit(); + txn_ptr trnlck(m_pMdbEnv); + mdb_open(trnlck, "contacts", MDB_CREATE | MDB_INTEGERKEY, &m_dbContacts); + mdb_open(trnlck, "modules", MDB_CREATE | MDB_INTEGERKEY, &m_dbModules); + mdb_open(trnlck, "events", MDB_CREATE | MDB_INTEGERKEY, &m_dbEvents); + mdb_open(trnlck, "settings", MDB_CREATE, &m_dbSettings); + trnlck.commit(); if (InitModuleNames()) return EGROKPRF_CANTREAD; if (InitCrypt()) return EGROKPRF_CANTREAD; diff --git a/plugins/Dbx_mdb/src/dbmodulechain.cpp b/plugins/Dbx_mdb/src/dbmodulechain.cpp index a54d7ab2f9..1a78284d78 100644 --- a/plugins/Dbx_mdb/src/dbmodulechain.cpp +++ b/plugins/Dbx_mdb/src/dbmodulechain.cpp @@ -42,15 +42,14 @@ int CDbxMdb::InitModuleNames(void) { m_maxModuleID = 0; - txn_lock trnlck(m_pMdbEnv); + txn_ptr trnlck(m_pMdbEnv); mdb_open(trnlck, "modules", MDB_INTEGERKEY, &m_dbModules); - MDB_cursor *cursor; - if (mdb_cursor_open(trnlck, m_dbModules, &cursor) != MDB_SUCCESS) + cursor_ptr cursor(trnlck, m_dbModules); + if (!cursor) return 1; MDB_val key, data; - while (mdb_cursor_get(cursor, &key, &data, MDB_NEXT) == 0) { DBModuleName *pmod = (DBModuleName*)data.mv_data; if (pmod->signature != DBMODULENAME_SIGNATURE) @@ -66,8 +65,6 @@ int CDbxMdb::InitModuleNames(void) if (moduleId > m_maxModuleID) m_maxModuleID = moduleId; } - - mdb_cursor_close(cursor); return 0; } @@ -104,7 +101,7 @@ DWORD CDbxMdb::GetModuleNameOfs(const char *szName) MDB_val key = { sizeof(int), &newIdx }, data = { sizeof(DBModuleName) + nameLen, pmod }; for (;; Remap()) { - txn_lock trnlck(m_pMdbEnv); + txn_ptr trnlck(m_pMdbEnv); mdb_open(trnlck, "modules", MDB_INTEGERKEY, &m_dbModules); mdb_put(trnlck, m_dbModules, &key, &data, 0); if (trnlck.commit()) diff --git a/plugins/Dbx_mdb/src/dbsettings.cpp b/plugins/Dbx_mdb/src/dbsettings.cpp index c345c42174..375b973c7e 100644 --- a/plugins/Dbx_mdb/src/dbsettings.cpp +++ b/plugins/Dbx_mdb/src/dbsettings.cpp @@ -119,7 +119,7 @@ LBL_Seek: DBCachedContact *cc = (contactID) ? m_cache->GetCachedContact(contactID) : NULL; - txn_lock trnlck(m_pMdbEnv); + txn_ptr trnlck(m_pMdbEnv); //mdb_open(trnlck, "settings", 0, &m_dbSettings); DBSettingKey keySearch; @@ -495,7 +495,7 @@ STDMETHODIMP_(BOOL) CDbxMdb::WriteContactSetting(MCONTACT contactID, DBCONTACTWR } for (;; Remap()) { - txn_lock trnlck(m_pMdbEnv); + txn_ptr trnlck(m_pMdbEnv); if (mdb_put(trnlck, m_dbSettings, &key, &data, MDB_RESERVE) != 0) return 1; @@ -564,7 +564,7 @@ STDMETHODIMP_(BOOL) CDbxMdb::DeleteContactSetting(MCONTACT contactID, LPCSTR szM MDB_val key = { 2 * sizeof(DWORD) + settingNameLen, &keySearch }, data; for (;; Remap()) { - txn_lock trnlck(m_pMdbEnv); + txn_ptr trnlck(m_pMdbEnv); if (mdb_del(trnlck, m_dbSettings, &key, &data)) return 1; @@ -599,24 +599,24 @@ STDMETHODIMP_(BOOL) CDbxMdb::EnumContactSettings(MCONTACT contactID, DBCONTACTEN memset(keySearch.szSettingName, 0, SIZEOF(keySearch.szSettingName)); LIST arSettings(50); - - txn_lock trnlck(m_pMdbEnv); - MDB_cursor *cursor; - mdb_cursor_open(trnlck, m_dbSettings, &cursor); - - MDB_val key = { sizeof(keySearch), &keySearch }, data; - mdb_cursor_get(cursor, &key, &data, MDB_SET_RANGE); - do { - DBSettingKey *pKey = (DBSettingKey*)key.mv_data; - if (pKey->dwContactID != contactID || pKey->dwOfsModule != keySearch.dwOfsModule) - break; + { + txn_ptr trnlck(m_pMdbEnv, true); + cursor_ptr cursor(trnlck, m_dbSettings); + + MDB_val key = { sizeof(keySearch), &keySearch }, data; + if (mdb_cursor_get(cursor, &key, &data, MDB_SET_RANGE) == MDB_SUCCESS) { + do { + DBSettingKey *pKey = (DBSettingKey*)key.mv_data; + if (pKey->dwContactID != contactID || pKey->dwOfsModule != keySearch.dwOfsModule) + break; - char szSetting[256]; - strncpy_s(szSetting, pKey->szSettingName, key.mv_size - sizeof(DWORD)*2); - arSettings.insert(mir_strdup(szSetting)); + char szSetting[256]; + strncpy_s(szSetting, pKey->szSettingName, key.mv_size - sizeof(DWORD) * 2); + arSettings.insert(mir_strdup(szSetting)); + } + while (mdb_cursor_get(cursor, &key, &data, MDB_NEXT) == MDB_SUCCESS); + } } - while (mdb_cursor_get(cursor, &key, &data, MDB_NEXT) == 0); - mdb_cursor_close(cursor); for (int i = 0; i < arSettings.getCount(); i++) { result = (dbces->pfnEnumProc)(arSettings[i], dbces->lParam); -- cgit v1.2.3