From 7f9dacb21b81a04549df028b1493802b7da069fd Mon Sep 17 00:00:00 2001 From: MikalaiR Date: Sun, 14 Feb 2016 15:48:21 +0000 Subject: dbx_lmdb: common project, small speed optimization git-svn-id: http://svn.miranda-ng.org/main/trunk@16278 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/Dbx_mdb/src/dbcontacts.cpp | 4 +- plugins/Dbx_mdb/src/dbcrypt.cpp | 6 +- plugins/Dbx_mdb/src/dbevents.cpp | 14 ++-- plugins/Dbx_mdb/src/dbintf.cpp | 10 +-- plugins/Dbx_mdb/src/dbmodulechain.cpp | 2 +- plugins/Dbx_mdb/src/dbsettings.cpp | 58 ++++++++-------- plugins/Dbx_mdb/src/init.cpp | 2 +- plugins/Dbx_mdb/src/stdafx.cpp | 18 ----- plugins/Dbx_mdb/src/stdafx.cxx | 18 +++++ plugins/Dbx_mdb/src/stdafx.h | 123 ++++++++++++++++++++++++++++++++++ plugins/Dbx_mdb/src/ui.cpp | 12 ++-- 11 files changed, 196 insertions(+), 71 deletions(-) delete mode 100644 plugins/Dbx_mdb/src/stdafx.cpp create mode 100644 plugins/Dbx_mdb/src/stdafx.cxx create mode 100644 plugins/Dbx_mdb/src/stdafx.h (limited to 'plugins/Dbx_mdb/src') diff --git a/plugins/Dbx_mdb/src/dbcontacts.cpp b/plugins/Dbx_mdb/src/dbcontacts.cpp index 31d9f7bf1b..4bfec0261c 100644 --- a/plugins/Dbx_mdb/src/dbcontacts.cpp +++ b/plugins/Dbx_mdb/src/dbcontacts.cpp @@ -21,7 +21,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include "commonheaders.h" +#include "stdafx.h" int CDbxMdb::CheckProto(DBCachedContact *cc, const char *proto) { @@ -283,7 +283,7 @@ void CDbxMdb::FillContacts() cc->pSubs = (MCONTACT*)mir_alloc(cc->nSubs*sizeof(MCONTACT)); for (int i = 0; i < cc->nSubs; i++) { char setting[100]; - mir_snprintf(setting, SIZEOF(setting), "Handle%d", i); + mir_snprintf(setting, _countof(setting), "Handle%d", i); cc->pSubs[i] = (0 != GetContactSetting(cc->contactID, META_PROTO, setting, &dbv)) ? NULL : dbv.dVal; } } diff --git a/plugins/Dbx_mdb/src/dbcrypt.cpp b/plugins/Dbx_mdb/src/dbcrypt.cpp index 32b8966408..2cf2613c45 100644 --- a/plugins/Dbx_mdb/src/dbcrypt.cpp +++ b/plugins/Dbx_mdb/src/dbcrypt.cpp @@ -21,7 +21,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include "commonheaders.h" +#include "stdafx.h" ///////////////////////////////////////////////////////////////////////////////////////// @@ -254,10 +254,10 @@ void CDbxMdb::ToggleEncryption() hEventFilterAddedEvent = hSave4; } -void CDbxMdb::ToggleSettingsEncryption(MCONTACT contactID) +void CDbxMdb::ToggleSettingsEncryption(MCONTACT /*contactID*/) { } -void CDbxMdb::ToggleEventsEncryption(MCONTACT contactID) +void CDbxMdb::ToggleEventsEncryption(MCONTACT /*contactID*/) { } diff --git a/plugins/Dbx_mdb/src/dbevents.cpp b/plugins/Dbx_mdb/src/dbevents.cpp index 16a518ff33..dd9a75e12f 100644 --- a/plugins/Dbx_mdb/src/dbevents.cpp +++ b/plugins/Dbx_mdb/src/dbevents.cpp @@ -21,7 +21,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include "commonheaders.h" +#include "stdafx.h" STDMETHODIMP_(LONG) CDbxMdb::GetEventCount(MCONTACT contactID) { @@ -38,11 +38,7 @@ STDMETHODIMP_(MEVENT) CDbxMdb::AddEvent(MCONTACT contactID, DBEVENTINFO *dbei) dbe.dwSignature = DBEVENT_SIGNATURE; dbe.contactID = contactID; // store native or subcontact's id dbe.ofsModuleName = GetModuleNameOfs(dbei->szModule); - dbe.timestamp = dbei->timestamp; - dbe.flags = dbei->flags; - dbe.wEventType = dbei->eventType; - dbe.cbBlob = dbei->cbBlob; - BYTE *pBlob = dbei->pBlob; + MCONTACT contactNotifyID = contactID; DBCachedContact *cc, *ccSub = NULL; @@ -66,6 +62,12 @@ STDMETHODIMP_(MEVENT) CDbxMdb::AddEvent(MCONTACT contactID, DBEVENTINFO *dbei) if (NotifyEventHooks(hEventFilterAddedEvent, contactNotifyID, (LPARAM)dbei)) return NULL; + dbe.timestamp = dbei->timestamp; + dbe.flags = dbei->flags; + dbe.wEventType = dbei->eventType; + dbe.cbBlob = dbei->cbBlob; + BYTE *pBlob = dbei->pBlob; + mir_ptr pCryptBlob; if (m_bEncrypted) { size_t len; diff --git a/plugins/Dbx_mdb/src/dbintf.cpp b/plugins/Dbx_mdb/src/dbintf.cpp index b0ab3a2b01..503746369e 100644 --- a/plugins/Dbx_mdb/src/dbintf.cpp +++ b/plugins/Dbx_mdb/src/dbintf.cpp @@ -21,7 +21,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include "commonheaders.h" +#include "stdafx.h" static int ModCompare(const ModuleName *mn1, const ModuleName *mn2) { @@ -187,11 +187,11 @@ bool CDbxMdb::Remap() m_dwFileSize += 0x100000; mdb_env_set_mapsize(m_pMdbEnv, m_dwFileSize); - int mode = MDB_NOSYNC | MDB_NOSUBDIR; + int mode = MDB_NOSYNC | MDB_NOSUBDIR | MDB_NOLOCK; // nolock - miranda using m_csDbAccess lock if (m_bReadOnly) - mode += MDB_RDONLY; + mode |= MDB_RDONLY; else - mode += MDB_WRITEMAP; + mode |= MDB_WRITEMAP; return mdb_env_open(m_pMdbEnv, _T2A(m_tszProfileName), mode, 0664) == MDB_SUCCESS; } @@ -209,7 +209,7 @@ void __cdecl dbpanic(void *) msg = TranslateT("Disk is full. Miranda will now shut down."); TCHAR err[256]; - mir_sntprintf(err, SIZEOF(err), msg, TranslateT("Database failure. Miranda will now shut down."), dwErr); + mir_sntprintf(err, _countof(err), msg, TranslateT("Database failure. Miranda will now shut down."), dwErr); MessageBox(0, err, TranslateT("Database Error"), MB_SETFOREGROUND | MB_TOPMOST | MB_APPLMODAL | MB_ICONWARNING | MB_OK); } diff --git a/plugins/Dbx_mdb/src/dbmodulechain.cpp b/plugins/Dbx_mdb/src/dbmodulechain.cpp index 81e3f66910..1ab0971b20 100644 --- a/plugins/Dbx_mdb/src/dbmodulechain.cpp +++ b/plugins/Dbx_mdb/src/dbmodulechain.cpp @@ -21,7 +21,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include "commonheaders.h" +#include "stdafx.h" void CDbxMdb::AddToList(char *name, DWORD ofs) { diff --git a/plugins/Dbx_mdb/src/dbsettings.cpp b/plugins/Dbx_mdb/src/dbsettings.cpp index dac3892d79..89750b8fc7 100644 --- a/plugins/Dbx_mdb/src/dbsettings.cpp +++ b/plugins/Dbx_mdb/src/dbsettings.cpp @@ -21,7 +21,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include "commonheaders.h" +#include "stdafx.h" struct DBSettingKey { @@ -121,8 +121,8 @@ LBL_Seek: DBCachedContact *cc = (contactID) ? m_cache->GetCachedContact(contactID) : NULL; - txn_ptr trnlck(m_pMdbEnv); - //mdb_open(trnlck, "settings", 0, &m_dbSettings); + txn_ptr trnlck(m_pMdbEnv); + //mdb_open(trnlck, "settings", 0, &m_dbSettings); DBSettingKey keySearch; keySearch.dwContactID = contactID; @@ -140,10 +140,10 @@ LBL_Seek: } } } - return 1; + return 1; } - BYTE *pBlob = (BYTE*)data.mv_data; + BYTE *pBlob = (BYTE*)data.mv_data; if (isStatic && (pBlob[0] & DBVTF_VARIABLELENGTH) && VLT(dbv->type) != VLT(pBlob[0])) return 1; @@ -493,7 +493,7 @@ STDMETHODIMP_(BOOL) CDbxMdb::WriteContactSetting(MCONTACT contactID, DBCONTACTWR } for (;; Remap()) { - txn_ptr trnlck(m_pMdbEnv); + txn_ptr trnlck(m_pMdbEnv); MDB_CHECK(mdb_put(trnlck, m_dbSettings, &key, &data, MDB_RESERVE), 1); BYTE *pBlob = (BYTE*)data.mv_data; @@ -561,9 +561,9 @@ STDMETHODIMP_(BOOL) CDbxMdb::DeleteContactSetting(MCONTACT contactID, LPCSTR szM MDB_val key = { 2 * sizeof(DWORD) + settingNameLen, &keySearch }, data; for (;; Remap()) { - txn_ptr trnlck(m_pMdbEnv); + txn_ptr trnlck(m_pMdbEnv); if (mdb_del(trnlck, m_dbSettings, &key, &data)) - return 1; + return 1; if (trnlck.commit()) break; @@ -593,36 +593,36 @@ STDMETHODIMP_(BOOL) CDbxMdb::EnumContactSettings(MCONTACT contactID, DBCONTACTEN DBSettingKey keySearch; keySearch.dwContactID = contactID; keySearch.dwOfsModule = GetModuleNameOfs(dbces->szModule); - memset(keySearch.szSettingName, 0, SIZEOF(keySearch.szSettingName)); + memset(keySearch.szSettingName, 0, _countof(keySearch.szSettingName)); LIST arSettings(50); { - 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; - + 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)); - } - while (mdb_cursor_get(cursor, &key, &data, MDB_NEXT) == MDB_SUCCESS); - } - } - - if (arSettings.getCount() == 0) - return -1; - - for (int i = 0; i < arSettings.getCount(); i++) { + } + while (mdb_cursor_get(cursor, &key, &data, MDB_NEXT) == MDB_SUCCESS); + } + } + + if (arSettings.getCount() == 0) + return -1; + + for (int i = 0; i < arSettings.getCount(); i++) { result = (dbces->pfnEnumProc)(arSettings[i], dbces->lParam); mir_free(arSettings[i]); } - + return result; } diff --git a/plugins/Dbx_mdb/src/init.cpp b/plugins/Dbx_mdb/src/init.cpp index 5d97945cf1..101608e8da 100644 --- a/plugins/Dbx_mdb/src/init.cpp +++ b/plugins/Dbx_mdb/src/init.cpp @@ -21,7 +21,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include "commonheaders.h" +#include "stdafx.h" int hLangpack; diff --git a/plugins/Dbx_mdb/src/stdafx.cpp b/plugins/Dbx_mdb/src/stdafx.cpp deleted file mode 100644 index 60c5301ed3..0000000000 --- a/plugins/Dbx_mdb/src/stdafx.cpp +++ /dev/null @@ -1,18 +0,0 @@ -/* -Copyright (C) 2012-16 Miranda NG project (http://miranda-ng.org) - -This program is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation version 2 -of the License. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . -*/ - -#include "commonheaders.h" \ No newline at end of file diff --git a/plugins/Dbx_mdb/src/stdafx.cxx b/plugins/Dbx_mdb/src/stdafx.cxx new file mode 100644 index 0000000000..0765fbaa6f --- /dev/null +++ b/plugins/Dbx_mdb/src/stdafx.cxx @@ -0,0 +1,18 @@ +/* +Copyright (C) 2012-16 Miranda NG project (http://miranda-ng.org) + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation version 2 +of the License. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#include "stdafx.h" \ No newline at end of file diff --git a/plugins/Dbx_mdb/src/stdafx.h b/plugins/Dbx_mdb/src/stdafx.h new file mode 100644 index 0000000000..dd06f5806a --- /dev/null +++ b/plugins/Dbx_mdb/src/stdafx.h @@ -0,0 +1,123 @@ +/* + +Miranda NG: the free IM client for Microsoft* Windows* + +Copyright (ñ) 2012-16 Miranda NG project (http://miranda-ng.org) +all portions of this codebase are copyrighted to the people +listed in contributors.txt. + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#define _CRT_SECURE_NO_WARNINGS +#define _WIN32_WINNT 0x0501 + +#pragma warning(disable:4509) + +//#define SIZEOF(x) _countof(x) + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +extern "C" +{ + #include "lmdb\lmdb.h" +}; + +class txn_ptr +{ + MDB_txn *m_txn; + +public: + __forceinline txn_ptr(MDB_env *pEnv, bool bReadOnly = false) + { + mdb_txn_begin(pEnv, NULL, (bReadOnly) ? MDB_RDONLY : 0, &m_txn); + } + + __forceinline ~txn_ptr() + { + if (m_txn) + mdb_txn_abort(m_txn); + } + + __forceinline operator MDB_txn*() const { return m_txn; } + + __forceinline bool commit() + { + bool bRes = (mdb_txn_commit(m_txn) != MDB_MAP_FULL); + m_txn = NULL; + return bRes; + } + + __forceinline void abort() + { + 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; } +}; + +#define MDB_CHECK(A,B) \ + switch(A) { \ + case MDB_SUCCESS: break; \ + case MDB_MAP_FULL: continue; \ + default: return (B); } + +#include "dbintf.h" +#include "resource.h" +#include "version.h" + +extern HINSTANCE g_hInst; +extern LIST g_Dbs; + +#ifdef __GNUC__ +#define mir_i64(x) (x##LL) +#else +#define mir_i64(x) (x##i64) +#endif diff --git a/plugins/Dbx_mdb/src/ui.cpp b/plugins/Dbx_mdb/src/ui.cpp index fe1059a7de..aa7a2ecd11 100644 --- a/plugins/Dbx_mdb/src/ui.cpp +++ b/plugins/Dbx_mdb/src/ui.cpp @@ -21,7 +21,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include "commonheaders.h" +#include "stdafx.h" struct DlgChangePassParam { @@ -100,7 +100,7 @@ static INT_PTR CALLBACK sttEnterPassword(HWND hwndDlg, UINT uMsg, WPARAM wParam, break; case IDOK: - GetDlgItemText(hwndDlg, IDC_USERPASS, param->newPass, SIZEOF(param->newPass)); + GetDlgItemText(hwndDlg, IDC_USERPASS, param->newPass, _countof(param->newPass)); EndDialog(hwndDlg, IDOK); } break; @@ -142,7 +142,7 @@ static bool CheckOldPassword(HWND hwndDlg, CDbxMdb *db) { if (db->usesPassword()) { TCHAR buf[100]; - GetDlgItemText(hwndDlg, IDC_OLDPASS, buf, SIZEOF(buf)); + GetDlgItemText(hwndDlg, IDC_OLDPASS, buf, _countof(buf)); ptrA oldPass(mir_utf8encodeT(buf)); if (!db->m_crypto->checkPassword(oldPass)) { SetDlgItemText(hwndDlg, IDC_HEADERBAR, TranslateT("Wrong old password entered!")); @@ -201,13 +201,13 @@ static INT_PTR CALLBACK sttChangePassword(HWND hwndDlg, UINT uMsg, WPARAM wParam case IDOK: TCHAR buf2[100]; - GetDlgItemText(hwndDlg, IDC_USERPASS1, buf2, SIZEOF(buf2)); + GetDlgItemText(hwndDlg, IDC_USERPASS1, buf2, _countof(buf2)); if (_tcslen(buf2) < 3) { SetDlgItemText(hwndDlg, IDC_HEADERBAR, TranslateT("Password is too short!")); goto LBL_Error; } - GetDlgItemText(hwndDlg, IDC_USERPASS2, buf, SIZEOF(buf)); + GetDlgItemText(hwndDlg, IDC_USERPASS2, buf, _countof(buf)); if (_tcscmp(buf2, buf)) { SetDlgItemText(hwndDlg, IDC_HEADERBAR, TranslateT("Passwords do not match!")); goto LBL_Error; @@ -307,7 +307,7 @@ static int OnModulesLoaded(PVOID obj, WPARAM, LPARAM) { CDbxMdb *db = (CDbxMdb*)obj; - Icon_Register(g_hInst, LPGEN("Database"), iconList, SIZEOF(iconList), "mmap"); + Icon_Register(g_hInst, LPGEN("Database"), iconList, _countof(iconList), "mmap"); HookEventObj(ME_OPT_INITIALISE, OnOptionsInit, db); -- cgit v1.2.3