-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmdbm-binding.c
347 lines (267 loc) · 8.06 KB
/
mdbm-binding.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include <mdbm-binding.h>
extern void get_mdbm_iter(MDBM_ITER *iter) {
MDBM_ITER_INIT(iter);
}
extern mdbm_ubig_t get_pageno_of_mdbm_iter(MDBM_ITER *iter) {
return (*iter).m_pageno;
}
extern int get_next_of_mdbm_iter(MDBM_ITER *iter) {
return (*iter).m_next;
}
//debug
static inline void echo_str(const char *str) {
fprintf(stderr,"----- echo_str -----\n");
fprintf(stderr,"[%s][%d]\n", str, (int)strlen(str));
fprintf(stderr,"--------------------\n");
}
static inline const char *get_locktype_name(int locktype) {
switch(locktype) {
case LT_SMART:
return (const char *)"SMART";
case LT_SHARED:
return (const char *)"SHARED";
case LT_PLOCK:
return (const char *)"PLOCK";
case LT_TRY_LOCK:
return (const char *)"TRY_LOCK";
case LT_TRY_SMART:
return (const char *)"TRY_LOCK_SMART";
case LT_TRY_SHARED:
return (const char *)"TRY_LOCK_SHARED";
case LT_TRY_PLOCK:
return (const char *)"TRY_PLOCK";
case LT_NONE:
return (const char *)"NONE";
default: //with case LT_LOCK:
return (const char *)"LOCK";
break;
}
}
//LOCK
static inline int common_lock_func(MDBM *db, datum *key, int locktype, int lockflags) {
int rv = -1;
if(locktype == LT_NONE) {
return 1;
}
if(key == NULL && (locktype == LT_SMART || locktype == LT_PLOCK || locktype == LT_TRY_SMART || locktype == LT_TRY_PLOCK)) {
fprintf(stderr, "Not support Lock(=%s) without key\n", get_locktype_name(locktype));
return rv;
}
switch(locktype) {
case LT_SMART:
rv = mdbm_lock_smart(db, key, lockflags);
break;
case LT_SHARED:
rv = mdbm_lock_shared(db);
break;
case LT_PLOCK:
rv = mdbm_plock(db, key, lockflags);
break;
case LT_TRY_LOCK:
rv = mdbm_trylock(db);
break;
case LT_TRY_SMART:
rv = mdbm_trylock_smart(db, key, lockflags);
break;
case LT_TRY_SHARED:
rv = mdbm_trylock_shared(db);
break;
case LT_TRY_PLOCK:
rv = mdbm_plock(db, key, lockflags);
break;
case LT_LOCK:
rv = mdbm_lock(db);
break;
default:
fprintf(stderr, "Not support Lock(=%s,%d) \n", get_locktype_name(locktype), locktype);
break;
}
return rv;
}
//UNLOCK
static inline int common_unlock_func(MDBM *db, datum *key, int locktype, int lockflags) {
int rv = -1;
if(locktype == LT_NONE) {
return 1;
}
if(key == NULL && (locktype == LT_SMART || locktype == LT_PLOCK || locktype == LT_TRY_SMART || locktype == LT_TRY_PLOCK)) {
return rv;
}
switch(locktype) {
case LT_SMART:
rv = mdbm_unlock_smart(db, key, lockflags);
break;
case LT_PLOCK:
rv = mdbm_punlock(db, key, lockflags);
break;
case LT_TRY_SMART:
rv = mdbm_unlock_smart(db, key, lockflags);
break;
case LT_TRY_PLOCK:
rv = mdbm_punlock(db, key, lockflags);
break;
case LT_LOCK:
rv = mdbm_unlock(db);
break;
default:
rv = mdbm_unlock(db);
break;
}
return rv;
}
// STORE
extern int set_mdbm_store_with_lock(MDBM *db, datum key, datum val, int flags, int locktype, int lockflags) {
int rv = -1;
rv = common_lock_func(db, &key, locktype, lockflags);
if(rv != 1) {
return rv;
}
rv = mdbm_store(db,key,val,flags);
common_unlock_func(db, &key, locktype, lockflags);
return rv;
}
// STORE_R
extern int set_mdbm_store_r_with_lock(MDBM *db, datum *key, datum *val, int flags, MDBM_ITER *iter, int locktype, int lockflags) {
int rv = -1;
rv = common_lock_func(db, key, locktype, lockflags);
if(rv != 1) {
return rv;
}
rv = mdbm_store_r(db,key,val,flags, iter);
common_unlock_func(db, key, locktype, lockflags);
return rv;
}
///STORE_STR
extern int set_mdbm_store_str_with_lock(MDBM *db, const char *key, const char *val, int flags, int locktype, int lockflags) {
int rv = -1;
datum lockkey = {(char *)key, strlen(key)+1};
rv = common_lock_func(db, &lockkey, locktype, lockflags);
if(rv != 1) {
return rv;
}
rv = mdbm_store_str(db, key, val, flags);
common_unlock_func(db, &lockkey, locktype, lockflags);
return rv;
}
// FETCH
extern char *get_mdbm_fetch_with_lock(MDBM *db, datum key, int locktype, int lockflags) {
int rv = -1;
datum val;
char *buf = NULL;
rv = common_lock_func(db, &key, locktype, lockflags);
if(rv != 1) {
return buf;
}
val = mdbm_fetch(db, key);
if (val.dptr != NULL) {
buf = (char *)calloc(val.dsize+1, sizeof(char));
if (buf == NULL) {
perror("failed allocation:");
} else {
strncpy(buf, val.dptr, val.dsize);
}
}
common_unlock_func(db, &key, locktype, lockflags);
return buf;
}
// FETCH_R
extern int get_mdbm_fetch_r_with_lock(MDBM *db, datum *key, datum *val, MDBM_ITER *iter, int locktype, int lockflags) {
int rv = -1;
rv = common_lock_func(db, key, locktype, lockflags);
if(rv != 1) {
return rv;
}
rv = mdbm_fetch_r(db, key, val, iter);
common_unlock_func(db, key, locktype, lockflags);
return rv;
}
// FETCH_DUP_R
extern int get_mdbm_fetch_dup_r_with_lock(MDBM *db, datum *key, datum *val, MDBM_ITER *iter, int locktype, int lockflags) {
int rv = -1;
rv = common_lock_func(db, key, locktype, lockflags);
if(rv != 1) {
return rv;
}
rv = mdbm_fetch_dup_r(db, key, val, iter);
common_unlock_func(db, key, locktype, lockflags);
return rv;
}
// FETCH_STR
extern char *get_mdbm_fetch_str_with_lock(MDBM *db, const char *key, int locktype, int lockflags) {
int rv = -1;
char *retval = NULL;
datum lockkey = {(char *)key, strlen(key)+1};
rv = common_lock_func(db, &lockkey, locktype, lockflags);
if(rv != 1) {
return NULL;
}
retval = mdbm_fetch_str(db, key);
common_unlock_func(db, &lockkey, locktype, lockflags);
return retval;
}
// DELETE
extern int set_mdbm_delete_with_lock(MDBM *db, datum key, int locktype, int lockflags) {
int rv = -1;
rv = common_lock_func(db, &key, locktype, lockflags);
if(rv != 1) {
return rv;
}
rv = mdbm_delete(db, key);
common_unlock_func(db, &key, locktype, lockflags);
return rv;
}
// DELETE_R
extern int set_mdbm_delete_r_with_lock(MDBM *db, datum key, MDBM_ITER *iter, int locktype, int lockflags) {
int rv = -1;
datum *lockkey = NULL;
if(key.dsize >= 0) {
lockkey = &key;
}
rv = common_lock_func(db, lockkey, locktype, lockflags);
if(rv != 1) {
return rv;
}
rv = mdbm_delete_r(db, iter);
common_unlock_func(db, lockkey, locktype, lockflags);
return rv;
}
extern int set_mdbm_delete_str_with_lock(MDBM *db, const char *key, int locktype, int lockflags) {
int rv = -1;
datum lockkey = {(char *)key, strlen(key)};
rv = common_lock_func(db, &lockkey, locktype, lockflags);
if(rv != 1) {
return rv;
}
rv = mdbm_delete_str(db, key);
common_unlock_func(db, &lockkey, locktype, lockflags);
return rv;
}
extern int dummy_clean_func(MDBM *db, const datum *key, const datum *val, struct mdbm_clean_data *mcd, int *quit) {
*quit = 0;
return 1;
}
extern int clean_anything_func(MDBM *db, const datum *key, const datum *val, struct mdbm_clean_data *mcd, int *quit) {
if (quit) {
*quit = 0;
}
return 1;
}
extern int set_mdbm_clean(MDBM *db, int pagenum, int flags) {
int rv = -1;
rv = mdbm_set_cleanfunc(db, clean_anything_func, 0);
if(rv != 1) {
return rv;
}
return mdbm_clean(db, pagenum, flags); //flags ignored
}
extern int set_mdbm_fcopy(MDBM *db, const char *filepath, int mode) {
int rv = -1;
int fd = open(filepath, O_RDWR | O_CREAT | O_TRUNC, mode);
if(fd == -1) {
fprintf(stderr, "Error : can't create a tempfeil(=%s), err=%s", filepath, strerror(errno));
return fd;
}
rv = mdbm_fcopy(db, fd, MDBM_COPY_LOCK_ALL);
close(fd);
return rv;
}