C’est très simple… Il suffit d’encapsuler les types C de base de la lib et les routines Get/Set. Voici le header:
#pragma once #include "LMDBWindowsDll.h" #include "lmdb-dll.h" #include "midl-dll.h" class LMDBWRAPPER_API LMDBData { public: LMDBData(); virtual ~LMDBData(); public: bool m_Init = false; MDB_env * m_env; MDB_dbi m_dbi; MDB_txn * m_txn; }; // This class is exported from the LMDBWrapper.dll class LMDBWRAPPER_API CLMDBWrapper { public: CLMDBWrapper(void) { } ~CLMDBWrapper() { } bool Init(LPSTR lpszDbName); bool Uninit(LPSTR lpszDbName); bool SetData(LPSTR lpszKey, LPSTR lpszValue); bool SetData(LPSTR lpszKey, LPSTR lpszValue, DWORD dwLen); bool GetData(LPSTR lpszKey, LPSTR lpszValue); private: LMDBData m_lmdb; }; extern LMDBWRAPPER_API int nLMDBWrapper; LMDBWRAPPER_API int fnLMDBWrapper(void);
On y trouve une structure avec différents éléments comme l’environnement, la connexion et la transaction… Regardons le corps de la classe CLMDBWrapper:
#include "header.h" #include "..\Include\LMDBWrapper.h" #include "..\Include\CWrapper.h" #include "..\Include\LMDBWrapper.h" #include "..\Include\MyServer\Constants.h" #ifdef _DEBUG #define new DEBUG_NEW #endif LMDBData::LMDBData() { m_dbi = 0; m_env = nullptr; m_txn = nullptr; } LMDBData::~LMDBData() { } bool CLMDBWrapper::Init(LPSTR lpszDbName) { char sz[255]; sprintf_s(sz, "%s\\%s", Constants::LMDBRootPath.c_str(), lpszDbName); ::CreateDirectoryA(sz, NULL); std::wcout << _T("Init LMDB") << std::endl; mdb_env_create(&m_lmdb.m_env); mdb_env_set_maxreaders(m_lmdb.m_env, 1); mdb_env_set_mapsize(m_lmdb.m_env, 10485760); mdb_env_open(m_lmdb.m_env, sz, MDB_CREATE/*|MDB_NOSYNC*/, 0664); m_lmdb.m_Init = true; return true; } bool CLMDBWrapper::Uninit(LPSTR lpszDbName) { std::wcout << _T("Uninit LMDB") << std::endl; mdb_dbi_close(m_lmdb.m_env, m_lmdb.m_dbi); mdb_env_close(m_lmdb.m_env); m_lmdb.m_dbi = 0; m_lmdb.m_env = nullptr; m_lmdb.m_txn = nullptr; m_lmdb.m_Init = false; return true; } bool CLMDBWrapper::SetData(LPSTR lpszKey, LPSTR lpszValue) { if (m_lmdb.m_Init == false) { std::wcout << _T("Init not done !") << std::endl; return false; } char szKey[255]; char szValue[255]; strcpy(szKey, lpszKey); strcpy(szValue, lpszValue); MDB_val VKey; MDB_val VData; VKey.mv_size = sizeof(szKey); VKey.mv_data = szKey; VData.mv_size = sizeof(szValue); VData.mv_data = szValue; //_tprintf(_T("Add Key:%s Data:%s\n"), szKey, szValue); mdb_txn_begin(m_lmdb.m_env, NULL, 0, &m_lmdb.m_txn); mdb_dbi_open(m_lmdb.m_txn, NULL, 0, &m_lmdb.m_dbi); mdb_put(m_lmdb.m_txn, m_lmdb.m_dbi, &VKey, &VData, MDB_NOOVERWRITE); mdb_txn_commit(m_lmdb.m_txn); return true; } bool CLMDBWrapper::SetData(LPSTR lpszKey, LPSTR lpszValueb64, DWORD dwLen) { return true; } bool CLMDBWrapper::GetData(LPSTR lpszKey, LPSTR lpszValue) { if (m_lmdb.m_Init == false) { std::wcout << _T("Init not done !") << std::endl; return false; } char szKey[255]; char szValue[255]; strcpy(szKey, lpszKey); strcpy(szValue, ""); MDB_val VKey; MDB_val VData; VKey.mv_size = sizeof(szKey); VKey.mv_data = szKey; mdb_txn_begin(m_lmdb.m_env, NULL, 0, &m_lmdb.m_txn); mdb_dbi_open(m_lmdb.m_txn, NULL, 0, &m_lmdb.m_dbi); int err = mdb_get(m_lmdb.m_txn, m_lmdb.m_dbi, &VKey, &VData); //printf("Get Key:%s Data:%s\n", VKey.mv_data, VData.mv_data); mdb_txn_commit(m_lmdb.m_txn); if (err == MDB_NOTFOUND) { strcpy(lpszValue, ""); } else { strcpy(lpszValue, (char *)VData.mv_data); } return true; }