JunkBox_Lib++ (for Windows) 1.10.1
Loading...
Searching...
No Matches
bdb_tool.cpp
Go to the documentation of this file.
1
16#include "bdb_tool.h"
17#include "jbxl_state.h"
18
19
20#ifdef ENABLE_BERKELEYDB
21
34DB* open_fdb(char* fname, unsigned int mode)
35{
36 DB* dp;
37
38 if (fname==NULL) return NULL;
39
40 int ret = db_create(&dp, NULL, 0);
41 if (ret==0) {
42 ret = dp->open(dp, NULL, fname, NULL, DB_HASH, (u_int32_t)mode, 0600);
43 }
44
45 if (ret!=0) {
47 dp->err(dp, ret, "DB->open");
48 PRINT_MESG("dbname is %s\n", fname);
49 }
50 return NULL;
51 }
52 return dp;
53}
54
55
61void close_fdb(DB** dp)
62{
63 if (dp==NULL || *dp==NULL) return;
64
65 (*dp)->close((*dp), 0);
66 *dp = NULL;
67 return;
68}
69
70
83int check_fdb_file(char* fname)
84{
85 DB* dp = open_fdb(fname, DB_CREATE);
86
87 if (dp==NULL) return FALSE;
88 close_fdb(&dp);
89 return TRUE;
90}
91
92
109int set_fdb_data(DB* dp, Buffer id, Buffer dbdata)
110{
111 DBT key, data;
112 int ret;
113
114 memset(&key, 0, sizeof(DBT));
115 memset(&data, 0, sizeof(DBT));
116
117 key.data = id.buf;
118 key.size = id.vldsz;
119 if (key.size<=0) {
120 key.size = strlen((char*)id.buf);
121 if (key.size<=0) return JBXL_BDB_DATASZ_ERROR;
122 }
123
124 data.data = dbdata.buf;
125 data.size = dbdata.vldsz;
126 if (data.size<=0) return JBXL_BDB_DATASZ_ERROR;
127
128 ret = dp->put(dp, NULL, &key, &data, 0);
129 if (ret!=0) {
130 DEBUG_MODE dp->err(dp, ret, "DB->put");
131 return JBXL_BDB_PUT_ERROR;
132 }
133 return data.size;
134}
135
136
154int set_fdb_data_file(char* fname, Buffer id, Buffer dbdata)
155{
156 DBT key, data;
157 DB* dp;
158 int ret;
159
160 dp = open_fdb(fname, DB_CREATE);
161 if (dp==NULL) return JBXL_BDB_OPEN_ERROR;
162
163 memset(&key, 0, sizeof(DBT));
164 memset(&data, 0, sizeof(DBT));
165
166 key.data = (char*)(id.buf);
167 key.size = id.vldsz;
168 if (key.size<=0) {
169 key.size = strlen((char*)id.buf);
170 if (key.size<=0) return JBXL_BDB_KEY_ERROR;
171 }
172
173 data.data = (char*)dbdata.buf;
174 data.size = dbdata.vldsz;
175 if (data.size<=0) return JBXL_BDB_DATASZ_ERROR;
176
177 ret = dp->put(dp, NULL, &key, &data, 0);
178 close_fdb(&dp);
179
180 if (ret!=0) {
181 DEBUG_MODE dp->err(dp, ret, "DB->put");
182 return JBXL_BDB_PUT_ERROR;
183 }
184 return data.size;
185}
186
187
210Buffer get_fdb_data(DB* dp, Buffer id)
211{
212 int ret;
213 DBT key, data;
214 Buffer dbdata;
215
216 memset(&key, 0, sizeof(DBT));
217 memset(&data, 0, sizeof(DBT));
218
219 dbdata = init_Buffer();
220 key.data = (void*)id.buf;
221 key.size = (u_int32_t)id.vldsz;
222 if (key.size<=0) {
223 key.size = strlen((char*)id.buf);
224 if (key.size<=0) {
225 dbdata.state = JBXL_BDB_MEMOEY_ERROR;
226 return dbdata;
227 }
228 }
229
230 ret = dp->get(dp, NULL, &key, &data, 0);
231 if (ret!=0) {
232 dbdata.state = JBXL_DBD_MEMORY_ERROR;
233 return dbdata;
234 }
235
236 dbdata = make_Buffer(data.size+1); // +1 is for string
237 if (dbdata.buf==NULL) {
238 dbdata.state = JBXL_BDB_GET_ERROR;
239 return dbdata;
240 }
241 memcpy(dbdata.buf, data.data, data.size);
242 dbdata.vldsz = data.size;
243 dbdata.buf[data.size] = '\0'; // for string
244
245 return dbdata;
246}
247
248
273Buffer get_fdb_data_file(char* fname, Buffer id)
274{
275 int ret;
276 DBT key, data;
277 DB* dp;
278 Buffer dbdata;
279
280 dbdata = init_Buffer();
281 dp = open_fdb(fname, DB_RDONLY);
282 if (dp==NULL) {
283 dbdata.state = JBXL_BDB_OPEN_ERROR;
284 return dbdata;
285 }
286
287 memset(&key, 0, sizeof(DBT));
288 memset(&data, 0, sizeof(DBT));
289
290 key.data = (char*)(id.buf);
291 key.size = id.vldsz;
292 if (key.size<=0) {
293 key.size = strlen((char*)id.buf);
294 if (key.size<=0) {
295 dbdata.state = JBXL_BDB_KEY_ERROR;
296 return dbdata;
297 }
298 }
299
300 ret = dp->get(dp, NULL, &key, &data, 0);
301 close_fdb(&dp);
302 if (ret!=0) {
303 dbdata.state = JBXL_BDB_GET_ERROR;
304 return dbdata;
305 }
306
307 dbdata = make_Buffer(data.size+1); // +1 is for string
308 if (dbdata.buf==NULL) {
310 return dbdata;
311 }
312
313 dbdata.vldsz = data.size;
314 memcpy(dbdata.buf, data.data, data.size);
315 dbdata.buf[data.size] = '\0'; // for string
316
317 return dbdata;
318}
319
320
335int del_fdb_data(DB* dp, Buffer id)
336{
337 DBT key;
338 int ret;
339
340 memset(&key, 0, sizeof(DBT));
341 key.data = (char*)(id.buf);
342 key.size = id.vldsz;
343 if (key.size<=0) {
344 key.size = strlen((char*)id.buf);
345 if (key.size<=0) return FALSE;
346 }
347 ret = dp->del(dp, NULL, &key, 0);
348
349 if (ret!=0) {
350 DEBUG_MODE dp->err(dp, ret, "DB->del");
351 return FALSE;
352 }
353 return TRUE;
354}
355
356
371int del_fdb_data_file(char* fname, Buffer id)
372{
373 DBT key;
374 DB* dp;
375 int ret;
376
377 dp = open_fdb(fname, 0);
378 if (dp==NULL) return FALSE;
379
380 memset(&key, 0, sizeof(DBT));
381 key.data = (char*)(id.buf);
382 key.size = id.vldsz;
383 if (key.size<=0) {
384 key.size = strlen((char*)id.buf);
385 if (key.size<=0) return FALSE;
386 }
387
388 ret = dp->del(dp, NULL, &key, 0);
389 close_fdb(&dp);
390
391 if (ret!=0) {
392 DEBUG_MODE dp->err(dp, ret, "DB->del");
393 return FALSE;
394 }
395 return TRUE;
396}
397
398
406tList* get_all_fdb_data(DB* dp)
407{
408 int ret;
409 DBT key, data;
410 DBC* crsp;
411
412 tList* lt = NULL;
413 tList* lp = NULL;
414 Buffer id, dt;
415
416 if (dp==NULL) return NULL;
417
418 dp->cursor(dp, NULL, &crsp, 0);
419 if (crsp==NULL) return NULL;
420
421 memset(&key, 0, sizeof(DBT));
422 memset(&data, 0, sizeof(DBT));
423
424 ret = crsp->c_get(crsp, &key, &data, DB_NEXT);
425 while (ret==0) {
426 id = make_Buffer_bin(key.data, key.size);
427 dt = make_Buffer_bin(data.data, data.size);
428 lt = add_tList_node_Buffer(lt, id, dt);
429 if (lp==NULL) lp = lt;
430 free_Buffer(&id);
431 free_Buffer(&dt);
432
433 ret = crsp->c_get(crsp, &key, &data, DB_NEXT);
434 }
435
436 crsp->c_close(crsp);
437
438 return lp;
439}
440
441
449tList* get_all_fdb_data_file(char* fname)
450{
451 DB* dp;
452 tList* lp = NULL;
453
454 if (fname==NULL) return NULL;
455
456 dp = open_fdb(fname, DB_RDONLY);
457 if (dp==NULL) return NULL;
458
459 lp = get_all_fdb_data(dp);
460 close_fdb(&dp);
461
462 return lp;
463}
464
465
466#endif
簡易データベースライブラリヘッダ for Berkeley BD4
Buffer make_Buffer(int sz)
Buffer型変数のバッファ部をつくり出す.
Definition buffer.cpp:71
void free_Buffer(Buffer *buf)
Buffer型変数のバッファ部を解放する
Definition buffer.cpp:128
Buffer init_Buffer()
初期化したBuffer型変数を返す.
Definition buffer.cpp:47
#define make_Buffer_bin(str, len)
set_Buffer()
Definition buffer.h:62
#define TRUE
Definition common.h:226
#define FALSE
Definition common.h:223
JunkBox_Lib 状態ヘッダ
#define JBXL_BDB_DATASZ_ERROR
BDB でデータのサイズが不明
Definition jbxl_state.h:133
#define JBXL_BDB_PUT_ERROR
BDB でデータの登録に失敗
Definition jbxl_state.h:132
#define JBXL_BDB_KEY_ERROR
BDB でキーの取得に失敗
Definition jbxl_state.h:130
#define JBXL_BDB_GET_ERROR
BDB でデータの取得に失敗
Definition jbxl_state.h:131
#define JBXL_BDB_MEMORY_ERROR
BDB でメモリの確保に失敗
Definition jbxl_state.h:134
#define JBXL_BDB_OPEN_ERROR
BDB でデータファイルのオープンに失敗
Definition jbxl_state.h:129
int vldsz
データの長さ.バイナリデータの場合も使用可能.文字列の場合は 0x00 を含まない.
Definition buffer.h:37
int state
変数の状態を表す.正常は JBXL_NORMAL
Definition buffer.h:38
unsigned char * buf
バッファの先頭へのポインタ.str[bufsz]は必ず 0x00となる.
Definition buffer.h:39
#define add_tList_node_Buffer(p, k, v)
add_tList_node_byBuffer()
Definition tlist.h:144
#define PRINT_MESG(...)
環境依存用の出力関数.MS Windows用は未実装
Definition tools.h:469
#define DEBUG_MODE
Definition tools.h:502