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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
#include "db.hh"
#include "util.hh"
#include <memory>
/* Wrapper class to ensure proper destruction. */
class DestroyDb
{
Db * db;
public:
DestroyDb(Db * _db) : db(_db) { }
~DestroyDb() { db->close(0); delete db; }
};
class DestroyDbc
{
Dbc * dbc;
public:
DestroyDbc(Dbc * _dbc) : dbc(_dbc) { }
~DestroyDbc() { dbc->close(); /* close() frees dbc */ }
};
static void rethrow(DbException & e)
{
throw Error(e.what());
}
Transaction::Transaction()
: txn(0)
{
}
Transaction::Transaction(Database & db)
{
db.requireEnv();
db.env->txn_begin(0, &txn, 0);
}
Transaction::~Transaction()
{
if (txn) {
txn->abort();
txn = 0;
}
}
void Transaction::commit()
{
if (!txn) throw Error("commit called on null transaction");
txn->commit(0);
txn = 0;
}
void Database::requireEnv()
{
if (!env) throw Error("database environment not open");
}
Db * Database::openDB(const Transaction & txn,
const string & table, bool create)
{
requireEnv();
Db * db = new Db(env, 0);
try {
// !!! fixme when switching to BDB 4.1: use txn.
db->open(table.c_str(), 0,
DB_HASH, create ? DB_CREATE : 0, 0666);
} catch (...) {
delete db;
throw;
}
return db;
}
Database::Database()
: env(0)
{
}
Database::~Database()
{
if (env) {
env->close(0);
delete env;
}
}
void Database::open(const string & path)
{
try {
if (env) throw Error(format("environment already open"));
env = new DbEnv(0);
debug("foo" + path);
env->open(path.c_str(),
DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN |
DB_CREATE,
0666);
} catch (DbException e) { rethrow(e); }
}
void Database::createTable(const string & table)
{
try {
Db * db = openDB(noTxn, table, true);
DestroyDb destroyDb(db);
} catch (DbException e) { rethrow(e); }
}
bool Database::queryString(const Transaction & txn, const string & table,
const string & key, string & data)
{
try {
Db * db = openDB(txn, table, false);
DestroyDb destroyDb(db);
Dbt kt((void *) key.c_str(), key.length());
Dbt dt;
int err = db->get(txn.txn, &kt, &dt, 0);
if (err) return false;
if (!dt.get_data())
data = "";
else
data = string((char *) dt.get_data(), dt.get_size());
} catch (DbException e) { rethrow(e); }
return true;
}
bool Database::queryStrings(const Transaction & txn, const string & table,
const string & key, Strings & data)
{
string d;
if (!queryString(txn, table, key, d))
return false;
string::iterator it = d.begin();
while (it != d.end()) {
if (it + 4 > d.end())
throw Error(format("short db entry: `%1%'") % d);
unsigned int len;
len = (unsigned char) *it++;
len |= ((unsigned char) *it++) << 8;
len |= ((unsigned char) *it++) << 16;
len |= ((unsigned char) *it++) << 24;
if (it + len > d.end())
throw Error(format("short db entry: `%1%'") % d);
string s;
while (len--) s += *it++;
data.push_back(s);
}
return true;
}
void Database::setString(const Transaction & txn, const string & table,
const string & key, const string & data)
{
try {
Db * db = openDB(txn, table, false);
DestroyDb destroyDb(db);
Dbt kt((void *) key.c_str(), key.length());
Dbt dt((void *) data.c_str(), data.length());
db->put(txn.txn, &kt, &dt, 0);
} catch (DbException e) { rethrow(e); }
}
void Database::setStrings(const Transaction & txn, const string & table,
const string & key, const Strings & data)
{
string d;
for (Strings::const_iterator it = data.begin();
it != data.end(); it++)
{
string s = *it;
unsigned int len = s.size();
d += len & 0xff;
d += (len >> 8) & 0xff;
d += (len >> 16) & 0xff;
d += (len >> 24) & 0xff;
d += s;
}
setString(txn, table, key, d);
}
void Database::delPair(const Transaction & txn, const string & table,
const string & key)
{
try {
Db * db = openDB(txn, table, false);
DestroyDb destroyDb(db);
Dbt kt((void *) key.c_str(), key.length());
db->del(txn.txn, &kt, 0);
} catch (DbException e) { rethrow(e); }
}
void Database::enumTable(const Transaction & txn, const string & table,
Strings & keys)
{
try {
Db * db = openDB(txn, table, false);
DestroyDb destroyDb(db);
Dbc * dbc;
db->cursor(0, &dbc, 0);
DestroyDbc destroyDbc(dbc);
Dbt kt, dt;
while (dbc->get(&kt, &dt, DB_NEXT) != DB_NOTFOUND)
keys.push_back(
string((char *) kt.get_data(), kt.get_size()));
} catch (DbException e) { rethrow(e); }
}
|