forked from meganz/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbdb.cpp
More file actions
240 lines (194 loc) · 4.23 KB
/
bdb.cpp
File metadata and controls
240 lines (194 loc) · 4.23 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/**
* @file bdb.cpp
* @brief Berkeley DB access layer
*
* (c) 2013-2014 by Mega Limited, Auckland, New Zealand
*
* This file is part of the MEGA SDK - Client Access Engine.
*
* Applications using the MEGA API must present a valid application key
* and comply with the the rules set forth in the Terms of Service.
*
* The MEGA SDK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* @copyright Simplified (2-clause) BSD License.
*
* You should have received a copy of the license along with this
* program.
*/
// FIXME: close in a way that does not require logfile merging upon the next open()
#ifdef USE_BDB
#include "mega.h"
namespace mega {
// basepath is prepended to the name
BdbAccess::BdbAccess(string* cdbpathprefix)
{
if (cdbpathprefix)
{
dbpathprefix = *cdbpathprefix;
}
else
{
// FIXME: Unicode support for default prefix?
dbpathprefix = "megaclient_statecache2_";
}
env = NULL;
}
BdbAccess::~BdbAccess()
{
if (env)
{
env->close(0);
}
}
DbTable* BdbAccess::open(FileSystemAccess* fsaccess, string* name, bool)
{
if (env)
{
env->close(0);
}
string dbname = *name;
fsaccess->name2local(&dbname);
dbname.insert(0, dbpathprefix);
fsaccess->mkdirlocal(&dbname);
env = new DbEnv(DB_CXX_NO_EXCEPTIONS);
env->set_lk_max_lockers(10000000);
env->set_lk_max_locks(10000000);
env->set_lk_max_objects(10000000);
if (env->open(dbname.c_str(), DB_CREATE | DB_REGISTER | DB_INIT_TXN | DB_INIT_MPOOL | DB_INIT_LOCK | DB_RECOVER, 0))
{
return NULL;
}
return new BdbTable(env);
}
// tablename can be modified - do not reuse
BdbTable::BdbTable(DbEnv* env)
{
db = new Db(env, 0);
db->open(NULL, "statecache", NULL, DB_BTREE, DB_CREATE | DB_DIRTY_READ | DB_AUTO_COMMIT, 0);
dbenv = env;
dbtxn = NULL;
dbcursor = NULL;
nextid = 0;
}
BdbTable::~BdbTable()
{
resetCommitter();
if (dbcursor)
{
dbcursor->close();
}
abort();
if (db)
{
db->close(0);
delete db;
}
}
// set cursor to first record
void BdbTable::rewind()
{
if (dbcursor)
{
dbcursor->close();
}
db->cursor(dbtxn, &dbcursor, DB_DIRTY_READ);
}
// retrieve next record through cursor
bool BdbTable::next(uint32_t* index, string* data)
{
if (!dbcursor)
{
return false;
}
Dbt key, value;
if (dbcursor->get(&key, &value, DB_NEXT | DB_DIRTY_READ))
{
return false;
}
if (sizeof(*index) != key.get_size())
{
return false;
}
*index = *(uint32_t*)key.get_data();
data->assign((char*)value.get_data(), value.get_size());
return true;
}
// retrieve record by index
bool BdbTable::get(uint32_t index, string* data)
{
Dbt key((char*)&index, sizeof index), value;
if (db->get(dbtxn, &key, &value, DB_READ_UNCOMMITTED))
{
return false;
}
data->assign((char*)value.get_data(), value.get_size());
return true;
}
// add/update record by index
bool BdbTable::put(uint32_t index, char* data, unsigned len)
{
if (dbcursor)
{
dbcursor->close();
dbcursor = NULL;
}
Dbt key((char*)&index, sizeof index), value(data, len);
if (db->put(dbtxn, &key, &value, 0))
{
return false;
}
return true;
}
// delete record by index
bool BdbTable::del(uint32_t index)
{
Dbt key((char*)&index, sizeof index);
db->del(dbtxn, &key, 0);
return true;
}
// truncate table
void BdbTable::truncate()
{
u_int32_t count;
if (dbcursor)
{
dbcursor->close();
dbcursor = NULL;
}
db->truncate(dbtxn, &count, 0);
}
// begin transaction
void BdbTable::begin()
{
dbenv->txn_begin(NULL, &dbtxn, DB_TXN_SYNC | DB_READ_UNCOMMITTED);
}
// commit transaction
void BdbTable::commit()
{
if (dbtxn)
{
dbtxn->commit(DB_TXN_SYNC);
dbtxn = NULL;
}
}
// abort transaction
void BdbTable::abort()
{
if (dbtxn)
{
dbtxn->abort();
dbtxn = NULL;
}
}
void BdbTable::remove()
{
abort();
begin()
truncate();
commit();
}
} // namespace
#endif