复制编译即可运行
简介:主要运用mdb_put/mdb_cursor_put |mdb_get/mdb_cursor_get存取数据
makefile
all: g++ -o mdb_demo mdb_demo.cpp liblmdb.a -pthread //需要把liblmdb.a包包含进去,网上可以找到clean: rm -rf mdb_demo core
code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include "lmdb.h" //包含头文件
using namespace std;
const char *pMdbFilePath = "./MdbFile";
//Mdb Class
class Mdb
{
public:
Mdb()
{
cout << "Im Mdb Constuctor" << endl;
}
~Mdb()
{
cout << "Im Desructor" << endl;
}
//Create Mdb Env, Set Env Mapsize and Open Env
bool InitMdbEnv(const char *pMdbFile, int nSize);
//Open Mdb File
bool MdbOpen();
//Open a Mdb Cursor
bool MdbCursorOpen();
//Put Data Into Mdb File
bool MdbPut(char* pkey, char* pValue);
//Get Data From Mdb File
bool MdbGet(char* pkey, char* pData);
//Using Cursor Get Data From Mdb File
bool MdbCursorGet(char* pKey, char *pData);
//Using Cursor Restore Data Into Mdb File
bool MdbCursorPut(char* pKey, char* pData);
//Transition Begin
bool MdbTxnBegin();
//Transition Commit, Current Transiton end
bool MdbTxnCommit();
//Transition Abort
void MdbTxnAbort();
//Mdb Cursor Handle Close
void MdbCursorClose();
//Mdb File Close
void MdbClose();
//Mdb Env Close
void MdbEnvClose();
private:
MDB_env *env;
MDB_dbi dbi;
MDB_txn *txn;
MDB_cursor *cursor;
};
void Mdb::MdbTxnAbort()
{
mdb_txn_abort(txn);
return ;
}
void Mdb::MdbCursorClose()
{
mdb_cursor_close(cursor);
return ;
}
bool Mdb::MdbCursorPut(char* pKey, char* pData)
{
MDB_val key, data;
key.mv_size = strlen(pKey)+1;
key.mv_data = pKey;
data.mv_size = strlen(pData)+1;
data.mv_data = pData;
if(mdb_cursor_put(cursor, &key, &data, MDB_NODUPDATA)) { cout << "MdbCursorPut Failed!!!" <<endl; return false; }
return true;
}
bool Mdb::MdbCursorGet(char* pKey, char *pData)
{
MDB_val key, data;
key.mv_size = strlen(pKey)+1;
key.mv_data = pKey;
if(mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) { cout << "mdb_cursor_get Failed Or No Data!!!" <<endl; return
false; }
strcpy(pData, (char*)data.mv_data);
strcpy(pKey, (char*)key.mv_data);
return true;
}
bool Mdb::MdbCursorOpen()
{
if(mdb_cursor_open(txn, dbi, &cursor)) { cout << "mdb_cursor_open Failed!!!" << endl; return false; }
return true;
}
bool Mdb::InitMdbEnv(const char *pMdbFile, int nSize)
{
int rc=(mdb_env_create(&env) ||
mdb_env_set_mapsize(env, nSize) ||
mdb_env_open(env, pMdbFile, 0, 0664));
if(rc) { cout << "Initialize " << pMdbFile << "Mdb Fialed!!!" << endl; return false; }
return true;
}
bool Mdb::MdbTxnBegin()
{
if(mdb_txn_begin(env, NULL, 0, &txn)) {cout << "mdb_open failed!!!" << endl; return false;}
return true;
}
bool Mdb::MdbOpen()
{
if(mdb_open(txn, NULL, 0, &dbi)) {cout << "mdb_txn_beginfailed!!!" << endl; return false;}
return true;
}
void Mdb::MdbClose()
{
mdb_close(env, dbi);
return ;
}
void Mdb::MdbEnvClose()
{
mdb_env_close(env);
return ;
}
bool Mdb::MdbPut(char* pKey, char* pData)
{
MDB_val key, data;
key.mv_size = strlen(pKey)+1;
key.mv_data = pKey;
data.mv_size = strlen(pData)+1;
data.mv_data = pData;
int rc = mdb_put(txn, dbi, &key, &data, MDB_NOOVERWRITE);
if(rc == MDB_MAP_FULL) { cout << "Mdb File is full, Alert!!!" << endl; return false; }
return true;
}
bool Mdb::MdbTxnCommit()
{
if(mdb_txn_commit(txn)) { cout << "mdb_txn_commit failed" << endl; return false; }
return true;
}
bool Mdb::MdbGet(char* pKey, char* pData)
{
MDB_val key, data;
key.mv_size = strlen(pKey)+1;
key.mv_data = pKey;
if(mdb_get(txn, dbi, &key, &data)) { cout << "mdb_get failed!!!" << endl; return false; }
strcpy(pData, (char*)data.mv_data);
return true;
}
int main(int argc,char *argv[])
{
Mdb mdb; //Create a Mdb Objiect
mdb.InitMdbEnv(pMdbFilePath, 200*1024*1024);//Initialize Mdb Env and Specifc Mdb File Max Size
mdb.MdbOpen(); //Open a Mdb File
//************mdb_put Restore Data*******************
char cKey[10]="";
char cValue[100]="Im value";
mdb.MdbTxnBegin(); //Begin a Transition
for(int i=0; i<100; i++) {
sprintf(cKey, "%06d", i);
mdb.MdbPut(cKey, cValue); //mdb_put Restore Data
}
mdb.MdbTxnCommit(); //Commit Transition
//*****************************************************
//************mdb_get Get items Form Database**********
char cKey1[]="000002";
char cValue1[100]="";
mdb.MdbTxnBegin();
mdb.MdbGet(cKey1, cValue1);
mdb.MdbTxnCommit();
cout << "Key:000002:" << cValue1 << endl;
//*****************************************************
//************mdb_cursor_put Using Cursor Restore Data*******
char cKey2[10]="";
char cValue2[100]="I m Cursor Values";
mdb.MdbTxnBegin();
mdb.MdbCursorOpen();
for(int i=101; i<201; i++) {
sprintf(cKey2, "%06d", i);
mdb.MdbCursorPut(cKey2, cValue2);
}
mdb.MdbCursorClose();
mdb.MdbTxnCommit();
//***********************************************************
//***********mdb_cursor_get Using Cursor Get Data************
mdb.MdbTxnBegin();
mdb.MdbCursorOpen();
char cKey3[10]="";
char cValue3[100]="";
while (mdb.MdbCursorGet(cKey3, cValue3)) {
cout << "Key: " << cKey3 << " data: " << cValue3 << endl;
}
mdb.MdbCursorClose();
mdb.MdbTxnAbort();
//***********************************************************
mdb.MdbClose(); //Close Mdb File
mdb.MdbEnvClose(); //Close Mdb Env
return 0;
}