From cdf4c110510a39c162b469ecbd6f69571019cf69 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Thu, 12 Apr 2018 20:55:18 +0300 Subject: initial version of profile compactor for MDBX --- plugins/Db3x_mmap/src/init.cpp | 2 +- plugins/Dbx_mdbx/src/dbintf.cpp | 33 +++++++++++++++++++++++++++++++++ plugins/Dbx_mdbx/src/dbintf.h | 2 ++ plugins/Dbx_mdbx/src/init.cpp | 2 +- plugins/Dbx_mdbx/src/stdafx.h | 1 + plugins/Import/src/dbrw/dbrw.cpp | 2 +- 6 files changed, 39 insertions(+), 3 deletions(-) (limited to 'plugins') diff --git a/plugins/Db3x_mmap/src/init.cpp b/plugins/Db3x_mmap/src/init.cpp index aea65a0ddf..587b79253b 100644 --- a/plugins/Db3x_mmap/src/init.cpp +++ b/plugins/Db3x_mmap/src/init.cpp @@ -121,7 +121,7 @@ LBL_Error: static DATABASELINK dblink = { - sizeof(DATABASELINK), + 0, "dbx_mmap", L"dbx mmap driver", makeDatabase, diff --git a/plugins/Dbx_mdbx/src/dbintf.cpp b/plugins/Dbx_mdbx/src/dbintf.cpp index 230a01920e..2c3f8907b8 100644 --- a/plugins/Dbx_mdbx/src/dbintf.cpp +++ b/plugins/Dbx_mdbx/src/dbintf.cpp @@ -167,6 +167,39 @@ int CDbxMDBX::Check(void) return (memcmp(buf, bDefHeader, _countof(bDefHeader))) ? EGROKPRF_UNKHEADER : 0; } +BOOL CDbxMDBX::Compact() +{ + CMStringW wszTmpFile(FORMAT, L"%s.tmp", m_tszProfileName); + + HANDLE pFile = ::CreateFile(wszTmpFile, GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); + if (pFile == nullptr) { + Netlib_Logf(0, "Temporary file <%S> cannot be created", wszTmpFile.c_str()); + return 1; + } + + mir_cslock lck(m_csDbAccess); + int res = mdbx_env_copy2fd(m_env, pFile, MDBX_CP_COMPACT); + CloseHandle(pFile); + + if (res == MDBX_SUCCESS) { + mdbx_env_close(m_env); + + DeleteFileW(m_tszProfileName); + MoveFileW(wszTmpFile, m_tszProfileName); + + mdbx_env_create(&m_env); + mdbx_env_set_maxdbs(m_env, 10); + mdbx_env_set_maxreaders(m_env, 244); + mdbx_env_set_userctx(m_env, this); + + Map(); + Load(); + } + else DeleteFileW(wszTmpFile); + + return 0; +} + int CDbxMDBX::PrepareCheck() { InitModules(); diff --git a/plugins/Dbx_mdbx/src/dbintf.h b/plugins/Dbx_mdbx/src/dbintf.h index 0b616916ed..6d998f7d77 100644 --- a/plugins/Dbx_mdbx/src/dbintf.h +++ b/plugins/Dbx_mdbx/src/dbintf.h @@ -267,6 +267,8 @@ public: STDMETHODIMP_(BOOL) MetaMergeHistory(DBCachedContact *ccMeta, DBCachedContact *ccSub); STDMETHODIMP_(BOOL) MetaSplitHistory(DBCachedContact *ccMeta, DBCachedContact *ccSub); + STDMETHODIMP_(BOOL) Compact(); + public: MICryptoEngine *m_crypto; }; diff --git a/plugins/Dbx_mdbx/src/init.cpp b/plugins/Dbx_mdbx/src/init.cpp index 6efe170cb5..2500cd30ad 100644 --- a/plugins/Dbx_mdbx/src/init.cpp +++ b/plugins/Dbx_mdbx/src/init.cpp @@ -93,7 +93,7 @@ static MDatabaseCommon* loadDatabase(const TCHAR *profile, BOOL bReadOnly) static DATABASELINK dblink = { - sizeof(DATABASELINK), + MDB_CAPS_COMPACT, "dbx_mdbx", L"MDBX database driver", makeDatabase, diff --git a/plugins/Dbx_mdbx/src/stdafx.h b/plugins/Dbx_mdbx/src/stdafx.h index 243756e39a..5e00445dba 100644 --- a/plugins/Dbx_mdbx/src/stdafx.h +++ b/plugins/Dbx_mdbx/src/stdafx.h @@ -24,6 +24,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #pragma once #include +#include #include #include diff --git a/plugins/Import/src/dbrw/dbrw.cpp b/plugins/Import/src/dbrw/dbrw.cpp index b5ed672bdb..00bf079c05 100644 --- a/plugins/Import/src/dbrw/dbrw.cpp +++ b/plugins/Import/src/dbrw/dbrw.cpp @@ -86,7 +86,7 @@ static MDatabaseCommon* dbrw_Load(const wchar_t *profile, BOOL) static DATABASELINK dblink = { - sizeof(DATABASELINK), + 0, "dbrw", L"dbx SQLite driver", dbrw_makeDatabase, -- cgit v1.2.3