-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxmdb.h
112 lines (93 loc) · 2.99 KB
/
xmdb.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* database_center.h
*
* Created on: Jul 8, 2017
* Author: lyzh
*/
#ifndef XMDB_H_
#define XMDB_H_
#include <iostream>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <memory>
#include "lmdb.h"
class xmdb
{
public:
xmdb(const char *_path);
virtual ~xmdb();
// cursor for a trans
class cursor
{
private:
std::shared_ptr<MDB_cursor> c;
cursor();
cursor(MDB_cursor *_c):c(std::shared_ptr<MDB_cursor>(_c, [_c](void*f){
mdb_cursor_close(_c);
} )){}
friend class xmdb;
public:
cursor(const cursor &o):c(o.c){}
cursor &operator = (const cursor &o){this->c = o.c;return *this;}
~cursor(){}
public:
bool get_next(std::string &key, std::string &value);
bool get_value(const std::string &key, std::string &value);
bool get_value(const std::string &key, void *data, int len, int *p_outlen = 0);
bool get_value(const std::string &key, int32_t &value);
bool get_value(const std::string &key, int64_t &value);
bool put(const std::string &key, const std::string &value);
bool put(const std::string &key, const char *data, int len);
bool put(const std::string &key, int32_t value );
};
// trans for a database
class trans
{
private:
trans() = delete;
std::shared_ptr<MDB_txn> txn;
MDB_dbi dbi;
trans(MDB_dbi d, MDB_txn *t);
friend class xmdb;
public:
trans(const trans &o):txn(o.txn), dbi(o.dbi){}
trans &operator = (const trans &o);
~trans(){
}
public:
cursor cursor_open();
MDB_dbi db()const;
public:
bool put(const std::string &key, const std::string &value);
bool put(const std::string &key, const char *data, int len);
bool put(const std::string &key, int32_t value );
bool put_if_not_exist(const std::string &key, const std::string &value);
bool put_if_not_exist(const std::string &key, const char *data, int len);
bool put_if_not_exist(const std::string &key, int32_t value );
bool get_value(const std::string &key, std::string &value);
bool get_value(const std::string &key, void *data, int len, int *p_outlen = 0);
bool get_value(const std::string &key, int32_t &value);
bool get_value(const std::string &key, int64_t &value);
bool get_value(const std::string &key, uint16_t &value);
bool commit();
void abort();
void rollback(){ abort();}
};
/* xmdb factory interface */
static xmdb *get_instance( int index );
static xmdb *get_instance( const std::string &dbname );
trans transaction_begin_rw();
trans transaction_begin_rd();
MDB_dbi db(){ return this->dbi;}
private:
const char *path;
MDB_env *dbenv;
MDB_dbi dbi;
MDB_txn *txn;
MDB_stat mst;
void init_lmdb();
xmdb() = delete;
xmdb &operator = (const xmdb &o) = delete;
};
#endif /* FIAR_DATABASE_CENTER_H_ */