forked from bcosorg/bcos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LvlDbInterface.h
174 lines (144 loc) · 5.36 KB
/
LvlDbInterface.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#pragma once
#include <myAPI.h>
#include <string>
#include <iostream>
#include <memory>
#include <vector>
#include <leveldb/db.h>
#include <leveldb/write_batch.h>
#include <leveldb/cache.h>
namespace leveldb{
enum DBUseType{
stateType = 1, //statedb
blockType = 2, //blockdb
extrasType = 3 //extrasdb
};
enum DBEngineType{
mysql = 1, //mysql
oracle = 2 //oracle
};
//static string DecodeValue(void* v) { return reinterpret_cast<char *>(v); }
class OdbcWriteStruct{
public:
OdbcWriteStruct(leveldb::Slice const &sKey, leveldb::Slice const & sValue)
:_sKey(sKey),
_sValue(sValue)
{}
public:
leveldb::Slice _sKey;
leveldb::Slice _sValue;
};
class LvlDbInterface : public leveldb::DB
{
public:
friend class leveldb::DB;
LvlDbInterface(const std::string &sDbConnInfo, const std::string &sDbName, const std::string &sTableName, const std::string &sUserName, const std::string &sPwd, int iDBEngineType, int iCacheSize = 100 * 1048576)
:_sDbConnInfo(sDbConnInfo),
_sDbName(sDbName),
_sTableName(sTableName),
_sUserName(sUserName),
_sPwd(sPwd)
{
std::cout << "LvlDbInterface init:" << _sDbConnInfo << "|" << _sDbName << "|" << _sTableName << "|" << _sUserName << "|" << _sPwd << "|" << iDBEngineType << "|" << iCacheSize << std::endl;;
//cnote << _sDbConnInfo << "|" << _sDbName << "|" << _sTableName << "|" << _sUserName << "|" << _sPwd;
switch (iDBEngineType)
{
case DBEngineType::mysql:
{
_saClient = SA_MySQL_Client;
}
break;
case DBEngineType::oracle:
{
_saClient = SA_Oracle_Client;
}
break;
}
if (!Connect())
{
//cwarn << "db connect failed.";
exit(-1);
}
_dataCc = leveldb::NewLRUCache(iCacheSize);//100m = 100 * 1048576
}
virtual bool Connect()
{
bool bRet = true;
try
{
SAString saDbInfo(_sDbConnInfo.c_str());
SAString saDbUser(_sUserName.c_str());
SAString saDbPwd(_sPwd.c_str());
con.Connect(saDbInfo, saDbUser, saDbPwd, _saClient);
con.setOption(SAString("MYSQL_OPT_READ_TIMEOUT")) = SAString("1");
con.setOption(SAString("MYSQL_OPT_CONNECT_TIMEOUT")) = SAString("86400");
con.setAutoCommit(SA_AutoCommitOn);
// std::cout << "conn GetServerVersionString() :" << con.ServerVersionString().GetMultiByteChars() << std::endl;
}
catch (SAException &x){
std::cerr << "SAException: " << x.ErrText().GetMultiByteChars() << std::endl;
bRet = false;
}
catch (std::exception& e){
std::cerr << "exception: " << e.what() << std::endl;
bRet = false;
}
catch (...){
std::cerr << "unknown exception occured" << std::endl;
bRet = false;
}
return bRet;
}
virtual Status Delete(const WriteOptions&, const Slice& key) override = 0;
virtual Status Get(const ReadOptions& options, const Slice& key, std::string* value) override = 0;
virtual Status Write(const WriteOptions& options, WriteBatch* updates)override = 0;
virtual Status Put(const WriteOptions&, const Slice& key, const Slice& value) = 0;
//����Ľӿ���ʱ�Ȳ���Ҫʵ�� �ȼ���־ ����Щ��Ҫ
virtual Iterator* NewIterator(const ReadOptions&) override{
std::cout << "weodbc interface neet NewIterator ";
//cnote << "weodbc interface neet NewIterator ";
return nullptr;
//return leveldb::DB::NewIterator(ro);
}
virtual const Snapshot* GetSnapshot() override{
std::cout << "weodbc interface neet GetSnapshot ";
//cnote << "weodbc interface neet GetSnapshot ";
return nullptr;
}
virtual void ReleaseSnapshot(const Snapshot*) override{
std::cout << "weodbc interface neet ReleaseSnapshot.";
//cnote << "weodbc interface neet ReleaseSnapshot.";
}
virtual bool GetProperty(const Slice&, std::string* value) override{
std::cout << "weodbc interface neet GetProperty:" << *value;
//cnote << "weodbc interface neet GetProperty:" << *value;
return true;
}
virtual void GetApproximateSizes(const Range*, int n, uint64_t* sizes)override{
std::cout << "weodbc interface neet GetApproximateSizes." << n << "|" << sizes;
//cnote << "weodbc interface neet GetApproximateSizes." << n << "|" << sizes;
}
virtual void CompactRange(const Slice* begin, const Slice* end)override{
std::cout << "weodbc interface neet put " << begin->ToString() << "|" << end->ToString();
//cnote << "weodbc interface neet put " << begin->ToString() << "|" << end->ToString();
}
LvlDbInterface(){}
~LvlDbInterface(){ delete(_dataCc); _dataCc = nullptr; }
public:
//sa����
SAConnection con;
SAClient_t _saClient;
std::string _sDbConnInfo;
std::string _sDbName;
std::string _sTableName;
std::string _sUserName;
std::string _sPwd;
leveldb::Cache *_dataCc = nullptr;
};
class LvlDbInterfaceFactory{
public:
//static std::shared_ptr<LvlDbInterface> create(const std::string &sDbInfo, const std::string &sDbName, const std::string &sTableName, const std::string &username, const std::string &pwd);
static LvlDbInterface* create(const std::string &sDbInfo, const std::string &sDbName, const std::string &sTableName, const std::string &username, const std::string &pwd, int dbEngineType, int iCacheSize = 100 * 1048576);
static LvlDbInterface* create(int dbType);
};
}