-
Notifications
You must be signed in to change notification settings - Fork 7
/
bdb.c
271 lines (239 loc) · 5.83 KB
/
bdb.c
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <db.h>
#include "rep_common.h"
#include "bdb.h"
void
log_error(const char *file, const char *function, int line, const char *msg, int err) {
char buf[1024];
int i, pos;
i = 0;
pos = 0;
while (1) {
if (file[i] == '\0') {
break;
}
#ifdef __WIN32
if (file[i] == '\\') {
#else
if (file[i] == '/') {
#endif
if (file[i + 1] != '\0') {
pos = i + 1;
}
}
++i;
}
snprintf(buf, sizeof buf, "%s|%d|%s|%s|%d|%s", file + pos, line, function, msg, err, db_strerror(err));
buf[sizeof buf - 1] = 0;
Error(buf);
}
int
db_close(DB *dbp) {
int ret;
if ((ret = dbp->close(dbp, 0)) != 0) {
LOG_ERROR("close", ret);
}
return 0;
}
int is_finished(SHARED_DATA *shared_data) {
return shared_data->app_finished;
}
int
db_get(DB *dbp, DB_TXN *txn, char *_key, unsigned int keylen, char **_data, unsigned int *datalen) {
DBT key, data;
int ret;
memset(&key, 0, sizeof key);
memset(&data, 0, sizeof data);
key.data = _key;
key.size = keylen;
data.flags = DB_DBT_REALLOC;
data.data = *_data;
ret = dbp->get(dbp, txn, &key, &data, 0);
if (ret == 0) {
*_data = data.data;
*datalen = data.size;
} else if (ret == DB_NOTFOUND) {
*_data = NULL;
*datalen = 0;
} else {
LOG_ERROR("get", ret);
}
return ret;
}
int
txn_begin(DB_ENV *dbenv, DB_TXN **txn, unsigned int flags) {
int ret;
ret = dbenv->txn_begin(dbenv, NULL, txn, flags);
if (ret) {
LOG_ERROR("txn_begin", ret);
}
}
int
txn_abort(DB_TXN *txn) {
int ret;
ret = txn->abort(txn);
if (ret) {
LOG_ERROR("abort", ret);
}
}
int
txn_commit(DB_TXN *txn) {
int ret;
ret = txn->commit(txn, 0);
if (ret) {
LOG_ERROR("commit", ret);
}
}
int
db_set_expire(
DB *expire_db,
DB *index_db,
DB_TXN *txn,
char *_key,
unsigned int keylen,
unsigned int sec,
unsigned int seq,
unsigned int tid) {
DBT key, data;
struct expire_key expire_value;
int ret;
memset(&key, 0, sizeof key);
memset(&data, 0, sizeof data);
time(&expire_value.t);
expire_value.t += sec;
expire_value.seq = seq;
expire_value.thread_id = tid;
key.data = &expire_value;
key.size = sizeof expire_value;
data.data = _key;
data.size = keylen;
ret = expire_db->put(expire_db, txn, &key, &data, 0);
if (ret != 0) {
LOG_ERROR("put|expire", ret);
return ret;
}
ret = index_db->put(index_db, txn, &data, &key, 0);
if (ret != 0) {
LOG_ERROR("put|index", ret);
return ret;
}
return 0;
}
int
db_put(DB *dbp, DB_TXN *txn, char *_key, unsigned int keylen, char *_data, unsigned int datalen) {
DBT key, data;
int ret;
memset(&key, 0, sizeof key);
memset(&data, 0, sizeof data);
key.data = _key;
key.size = keylen;
data.data = _data;
data.size = datalen;
ret = dbp->put(dbp, txn, &key, &data, 0);
return ret;
}
int
expire_key_compare(DB *db, const DBT *a, const DBT *b, size_t *locp) {
struct expire_key *ai, *bi;
ai = (struct expire_key*)a->data;
bi = (struct expire_key*)b->data;
uint64_t r = ai->t - bi->t;
if (r > 0) {
return 1;
} else if (r < 0) {
return -1;
}
r = ai->seq - bi->seq;
if (r > 0) {
return 1;
} else if (r < 0) {
return -1;
}
r = ai->thread_id - bi->thread_id;
if (r > 0) {
return 1;
} else if (r < 0) {
return -1;
} else {
return 0;
}
}
int
get_db(DB_ENV *dbenv, SHARED_DATA *shared_data, const char *name, int dbtype, DB **out) {
DB *dbp;
int ret;
u_int32_t flags;
permfail_t *pfinfo;
*out = NULL;
if ((ret = db_create(&dbp, dbenv, 0)) != 0)
return (ret);
flags = DB_AUTO_COMMIT | DB_READ_UNCOMMITTED | DB_THREAD;
/*
* Open database with DB_CREATE only if this is
* a master database. A client database uses
* polling to attempt to open the database without
* DB_CREATE until it is successful.
*
* This DB_CREATE polling logic can be simplified
* under some circumstances. For example, if the
* application can be sure a database is already
* there, it would never need to open it with
* DB_CREATE.
*/
if (dbtype != DB_UNKNOWN && shared_data->is_master) {
flags |= DB_CREATE;
}
if (strcmp("__expire.db", name) == 0) {
ret = dbp->set_dup_compare(dbp, expire_key_compare);
if (ret) {
return ret;
}
}
if ((ret = dbp->open(dbp, NULL, name, NULL, DB_HASH, flags, 0)) != 0) {
dbenv->err(dbenv, ret, "DB->open");
if ((ret = dbp->close(dbp, 0)) != 0) {
dbenv->err(dbenv, ret, "DB->close");
}
return ret;
}
/* Check this thread's PERM_FAILED indicator. */
*out = dbp;
return 0;
}
#define DEFAULT_TABLE "__default"
void
split_key(char *_key, int keylen, char **table, int *tablelen, char **name, int *namelen) {
int i;
*name = NULL;
*table = NULL;
for (i = 0; i < keylen; ++i) {
if (_key[i] == ':') {
if (i != 0) {
*table = _key;
_key[i] = '\0';
*tablelen = i;
}
*namelen = keylen - i - 1;
if (*namelen != 0) {
*name = _key + i + 1;
}
break;
}
}
if (*table == NULL && *name == NULL) {
*table = DEFAULT_TABLE;
*tablelen = (sizeof DEFAULT_TABLE) - 1;
*name = _key;
*namelen = keylen;
}
if (table == NULL) {
*table = DEFAULT_TABLE;
*tablelen = (sizeof DEFAULT_TABLE) - 1;
}
if (*name == NULL) {
*name = "\0";
*namelen = 1;
}
}