-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
bdb.h
614 lines (525 loc) · 14.6 KB
/
bdb.h
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
#include <db.h>
#include <errno.h>
#include <ruby.h>
#ifdef HAVE_RUBY_IO_H
#include <ruby/io.h>
#else
#include <rubysig.h>
#include <rubyio.h>
#endif
#include "bdb_features.h"
#ifndef StringValue
#define StringValue(x) do { \
if (TYPE(x) != T_STRING) x = rb_str_to_str(x); \
} while (0)
#endif
#ifndef StringValuePtr
#define StringValuePtr(x) STR2CSTR(x)
#endif
#ifndef SafeStringValue
#define SafeStringValue(x) Check_SafeStr(x)
#endif
#ifndef RSTRING_PTR
# define RSTRING_PTR(x_) RSTRING(x_)->ptr
# define RSTRING_LEN(x_) RSTRING(x_)->len
#endif
#ifndef RARRAY_PTR
# define RARRAY_PTR(x_) RARRAY(x_)->ptr
# define RARRAY_LEN(x_) RARRAY(x_)->len
#endif
#ifndef RCLASS_SUPER
#define RCLASS_SUPER(c) (RCLASS(c)->super)
#endif
#ifdef close
#undef close
#endif
#ifdef stat
#undef stat
#endif
#ifdef rename
#undef rename
#endif
#if defined(__cplusplus)
extern "C" {
#endif
#if (DB_VERSION_MAJOR == 2) || (DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR < 2)
#define BDB_OLD_FUNCTION_PROTO 1
#else
#define BDB_OLD_FUNCTION_PROTO 0
#endif
#if ! HAVE_CONST_DB_AUTO_COMMIT
#define DB_AUTO_COMMIT 0
#endif
#if ! HAVE_CONST_DB_NEXT_DUP
#define DB_NEXT_DUP 0
#endif
#define BDB_VERSION (10000*DB_VERSION_MAJOR+100*DB_VERSION_MINOR+DB_VERSION_PATCH)
#define BDB_MARSHAL (1<<0)
#define BDB_NOT_OPEN (1<<1)
#define BDB_RE_SOURCE (1<<2)
#define BDB_BT_COMPARE (1<<3)
#define BDB_BT_PREFIX (1<<4)
#define BDB_DUP_COMPARE (1<<5)
#define BDB_H_HASH (1<<6)
#define BDB_APPEND_RECNO (1<<7)
#define BDB_H_COMPARE (1<<13)
#define BDB_FEEDBACK (1<<8)
#define BDB_AUTO_COMMIT (1<<9)
#define BDB_NO_THREAD (1<<10)
#define BDB_INIT_LOCK (1<<11)
#define BDB_NIL (1<<12)
#define BDB_TXN_COMMIT (1<<0)
#define BDB_APP_DISPATCH (1<<0)
#define BDB_REP_TRANSPORT (1<<1)
#define BDB_ENV_ENCRYPT (1<<2)
#define BDB_ENV_NOT_OPEN (1<<3)
#if HAVE_ST_DB_ASSOCIATE
#define BDB_ERROR_PRIVATE 44444
#endif
#define HAVE_DB_STAT_4_TXN 0
#if HAVE_DB_STAT_4
#if HAVE_ST_DB_OPEN
#if HAVE_DB_OPEN_7
#undef HAVE_DB_STAT_4_TXN
#define HAVE_DB_STAT_4_TXN 1
#endif
#endif
#endif
#define BDB_INIT_TRANSACTION (DB_INIT_LOCK | DB_INIT_MPOOL | DB_INIT_TXN | DB_INIT_LOG)
#define BDB_INIT_LOMP (DB_INIT_LOCK | DB_INIT_MPOOL | DB_INIT_LOG)
#define BDB_NEED_CURRENT (BDB_MARSHAL | BDB_BT_COMPARE | BDB_BT_PREFIX | BDB_DUP_COMPARE | BDB_H_HASH | BDB_H_COMPARE | BDB_APPEND_RECNO | BDB_FEEDBACK)
#if HAVE_ST_DB_ENV_SET_FEEDBACK || HAVE_ST_DB_ENV_REP_SET_TRANSPORT || \
HAVE_ST_DB_ENV_SET_REP_TRANSPORT || HAVE_ST_DB_ENV_SET_APP_DISPATCH
#define BDB_NEED_ENV_CURRENT (BDB_FEEDBACK | BDB_APP_DISPATCH | BDB_REP_TRANSPORT)
#else
#define BDB_NEED_ENV_CURRENT 0
#endif
#if ! HAVE_CONST_DB_RMW
#define DB_RMW 0
#endif
extern VALUE bdb_cEnv;
extern VALUE bdb_eFatal;
extern VALUE bdb_eLock, bdb_eLockDead, bdb_eLockHeld, bdb_eLockGranted;
extern VALUE bdb_mDb;
extern VALUE bdb_cCommon, bdb_cBtree, bdb_cRecnum, bdb_cHash, bdb_cRecno, bdb_cUnknown;
extern VALUE bdb_cDelegate;
#if HAVE_TYPE_DB_KEY_RANGE
extern VALUE bdb_sKeyrange;
#endif
#if HAVE_CONST_DB_QUEUE
extern VALUE bdb_cQueue;
#else
#define DB_QUEUE DB_RECNO
#endif
extern VALUE bdb_cTxn, bdb_cTxnCatch;
extern VALUE bdb_cCursor;
extern VALUE bdb_cLock, bdb_cLockid;
extern VALUE bdb_cLsn;
extern VALUE bdb_mMarshal;
extern ID bdb_id_dump, bdb_id_load;
extern ID bdb_id_current_db, bdb_id_current_env;
extern VALUE bdb_deleg_to_orig _((VALUE));
struct ary_st {
int len, total, mark;
VALUE *ptr;
};
typedef struct {
int options;
VALUE marshal;
struct ary_st db_ary;
VALUE home;
DB_ENV *envp;
#if HAVE_DB_LOG_REGISTER_4 || HAVE_ST_DB_ENV_LG_INFO
u_int32_t fidp;
#endif
#if HAVE_ST_DB_ENV_REP_SET_TRANSPORT || HAVE_ST_DB_ENV_SET_REP_TRANSPORT
VALUE rep_transport;
#endif
#if HAVE_ST_DB_ENV_SET_FEEDBACK
VALUE feedback;
#endif
#if HAVE_ST_DB_ENV_SET_APP_DISPATCH
VALUE app_dispatch;
#endif
#if HAVE_ST_DB_ENV_SET_MSGCALL
VALUE msgcall;
#endif
#if HAVE_ST_DB_ENV_SET_THREAD_ID
VALUE thread_id;
#endif
#if HAVE_ST_DB_ENV_SET_THREAD_ID_STRING
VALUE thread_id_string;
#endif
#if HAVE_ST_DB_ENV_SET_ISALIVE
VALUE isalive;
#endif
#if HAVE_ST_DB_ENV_SET_EVENT_NOTIFY
VALUE event_notify;
#endif
} bdb_ENV;
#if HAVE_DBXML_INTERFACE
struct txn_rslbl {
DB_TXN *txn;
void *txn_cxx;
VALUE man;
void (*txn_cxx_free) (void **);
int (*txn_cxx_abort) (void *);
int (*txn_cxx_commit) (void *, u_int32_t );
int (*txn_cxx_discard) (void *, u_int32_t );
};
extern VALUE bdb_env_s_rslbl _((int, VALUE *,VALUE, DB_ENV *));
#endif
typedef struct {
int status, options;
VALUE marshal, mutex;
struct ary_st db_ary;
struct ary_st db_assoc;
VALUE env;
DB_TXN *txnid;
DB_TXN *parent;
#if HAVE_DBXML_INTERFACE
void *txn_cxx;
VALUE man;
void (*txn_cxx_free) (void **);
int (*txn_cxx_abort) (void *);
int (*txn_cxx_commit) (void *, u_int32_t );
int (*txn_cxx_discard) (void *, u_int32_t );
#endif
} bdb_TXN;
#define FILTER_KEY 0
#define FILTER_VALUE 1
#define FILTER_FREE 2
typedef struct {
int options;
VALUE marshal;
DBTYPE type;
VALUE env, orig, secondary, txn;
VALUE filename, database;
VALUE bt_compare, bt_prefix, h_hash;
#if HAVE_CONST_DB_DUPSORT
VALUE dup_compare;
#endif
#if HAVE_ST_DB_SET_H_COMPARE
VALUE h_compare;
#endif
VALUE filter[4];
VALUE ori_val;
DB *dbp;
long len;
u_int32_t flags;
u_int32_t partial;
u_int32_t dlen;
u_int32_t doff;
int array_base;
#if HAVE_TYPE_DB_INFO
DB_INFO *dbinfo;
#else
int re_len;
char re_pad;
VALUE feedback;
#endif
#if HAVE_ST_DB_SET_APPEND_RECNO
VALUE append_recno;
#endif
#if HAVE_ST_DB_SET_CACHE_PRIORITY
int priority;
#endif
} bdb_DB;
#if HAVE_TYPE_DB_SEQUENCE
typedef struct {
DB_SEQUENCE *seqp;
VALUE db, txn, orig;
DB_TXN *txnid;
} bdb_SEQ;
#define GetSEQ(obj_,seq_) do { \
Data_Get_Struct(obj_, bdb_SEQ, seq_); \
if (seq_->seqp == 0) { \
rb_raise(bdb_eFatal, "closed sequence"); \
} \
} while (0)
#endif
typedef struct {
DBC *dbc;
VALUE db;
} bdb_DBC;
typedef struct {
unsigned int lock;
VALUE env, self;
} bdb_LOCKID;
typedef struct {
#if HAVE_TYPE_DB_INFO
DB_LOCK lock;
#else
DB_LOCK *lock;
#endif
VALUE env;
} bdb_LOCK;
typedef struct {
bdb_DB *dbst;
DBT *key;
VALUE value;
} bdb_VALUE;
struct deleg_class {
int type;
VALUE db;
VALUE obj;
VALUE key;
};
struct dblsnst {
VALUE env, self;
DB_LSN *lsn;
#if HAVE_TYPE_DB_LOGC
DB_LOGC *cursor;
int flags;
#endif
};
#define RECNUM_TYPE(dbst) ((dbst->type == DB_RECNO || dbst->type == DB_QUEUE) || (dbst->type == DB_BTREE && (dbst->flags & DB_RECNUM)))
#define GetCursorDB(obj, dbcst, dbst) \
{ \
Data_Get_Struct(obj, bdb_DBC, dbcst); \
if (dbcst->db == 0) \
rb_raise(bdb_eFatal, "closed cursor"); \
GetDB(dbcst->db, dbst); \
}
#define GetEnvDBErr(obj, envst, id_c, eErr) \
{ \
Data_Get_Struct(obj, bdb_ENV, envst); \
if (envst->envp == 0) \
rb_raise(eErr, "closed environment"); \
if (envst->options & BDB_NEED_ENV_CURRENT) { \
VALUE th = rb_thread_current(); \
\
if (!RTEST(th) || !RBASIC(th)->flags) { \
rb_raise(eErr, "invalid thread object"); \
} \
rb_thread_local_aset(th, id_c, obj); \
} \
}
#define GetEnvDB(obj, envst) GetEnvDBErr(obj, envst, bdb_id_current_env, bdb_eFatal)
#define GetDB(obj, dbst) \
{ \
Data_Get_Struct(obj, bdb_DB, dbst); \
if (dbst->dbp == 0) { \
rb_raise(bdb_eFatal, "closed DB"); \
} \
if (dbst->options & BDB_NEED_CURRENT) { \
VALUE th = rb_thread_current(); \
\
if (!RTEST(th) || !RBASIC(th)->flags) { \
rb_raise(bdb_eFatal, "invalid thread object"); \
} \
rb_thread_local_aset(th, bdb_id_current_db, obj); \
} \
}
#if BDB_VERSION < 20600
#define HAVE_CURSOR_C_GET_KEY_MALLOC 1
#define INIT_RECNO(dbst, key, recno) \
{ \
recno = 1; \
if (RECNUM_TYPE(dbst)) { \
key.data = &recno; \
key.size = sizeof(db_recno_t); \
key.flags |= DB_DBT_MALLOC; \
} \
else { \
key.flags |= DB_DBT_MALLOC; \
} \
}
#define FREE_KEY(dbst, key) \
{ \
if (((key).flags & DB_DBT_MALLOC) && !RECNUM_TYPE(dbst)) { \
free((key).data); \
} \
}
#else
#define HAVE_CURSOR_C_GET_KEY_MALLOC 0
#define INIT_RECNO(dbst, key, recno) \
{ \
recno = 1; \
if (RECNUM_TYPE(dbst)) { \
key.data = &recno; \
key.size = sizeof(db_recno_t); \
} \
else { \
key.flags |= DB_DBT_MALLOC; \
} \
}
#define FREE_KEY(dbst, key) \
{ \
if ((key).flags & DB_DBT_MALLOC) { \
free((key).data); \
} \
}
#endif
#define INIT_TXN(txnid, obj, dbst) { \
txnid = NULL; \
GetDB(obj, dbst); \
if (RTEST(dbst->txn)) { \
bdb_TXN *txnst; \
Data_Get_Struct(dbst->txn, bdb_TXN, txnst); \
if (txnst->txnid == 0) \
rb_warning("using a db handle associated with a closed transaction"); \
txnid = txnst->txnid; \
} \
}
#define SET_PARTIAL(db, data) \
(data).flags |= db->partial; \
(data).dlen = db->dlen; \
(data).doff = db->doff;
#define GetTxnDBErr(obj, txnst, error) \
{ \
Data_Get_Struct(obj, bdb_TXN, txnst); \
if (txnst->txnid == 0) \
rb_raise(error, "closed transaction"); \
}
#define GetTxnDB(obj, txnst) GetTxnDBErr(obj, txnst, bdb_eFatal)
#define BDB_VALID(obj, type) (RTEST(obj) && BUILTIN_TYPE(obj) == (type))
#if HAVE_TYPE_DB_INFO
#define TEST_INIT_LOCK(dbst) (((dbst)->options & BDB_INIT_LOCK)?DB_RMW:0)
#else
#define TEST_INIT_LOCK(dbst) (0)
#endif
#define BDB_ST_KEY 1
#define BDB_ST_VALUE 2
#define BDB_ST_KV 3
#define BDB_ST_DELETE 4
#define BDB_ST_DUPU (5 | BDB_ST_DUP)
#define BDB_ST_DUPKV (6 | BDB_ST_DUP)
#define BDB_ST_DUPVAL (7 | BDB_ST_DUP)
#define BDB_ST_REJECT 8
#define BDB_ST_DUP 32
#define BDB_ST_ONE 64
#define BDB_ST_SELECT 128
#define BDB_ST_PREFIX 256
extern VALUE bdb_errstr;
extern int bdb_errcall;
extern int bdb_test_error _((int));
#if HAVE_CONST_DB_INCOMPLETE
#define bdb_cache_error(commande_, correction_, result_) do { \
result_ = commande_; \
\
switch (result_) { \
case 0: \
case DB_NOTFOUND: \
case DB_KEYEMPTY: \
case DB_KEYEXIST: \
break; \
case DB_INCOMPLETE: \
result_ = 0; \
break; \
default: \
correction_; \
bdb_test_error(result_); \
} \
} while (0);
#else
#define bdb_cache_error(commande_, correction_, result_) do { \
result_ = commande_; \
\
switch (result_) { \
case 0: \
case DB_NOTFOUND: \
case DB_KEYEMPTY: \
case DB_KEYEXIST: \
break; \
default: \
correction_; \
bdb_test_error(result_); \
} \
} while (0);
#endif
extern VALUE bdb_obj_init _((int, VALUE *, VALUE));
extern ID bdb_id_call;
#if ! HAVE_DB_STRERROR
extern char *db_strerror _((int));
#endif
extern VALUE bdb_local_aref _(());
extern VALUE bdb_test_recno _((VALUE, DBT *, db_recno_t *, VALUE));
extern VALUE bdb_test_dump _((VALUE, DBT *, VALUE, int));
extern VALUE bdb_test_ret _((VALUE, VALUE, VALUE, int));
extern VALUE bdb_test_load_key _((VALUE, DBT *));
extern VALUE bdb_assoc _((VALUE, DBT *, DBT *));
extern VALUE bdb_assoc3 _((VALUE, DBT *, DBT *, DBT *));
extern VALUE bdb_assoc_dyna _((VALUE, DBT *, DBT *));
extern VALUE bdb_clear _((int, VALUE *, VALUE));
extern VALUE bdb_del _((VALUE, VALUE));
extern void bdb_deleg_mark _((struct deleg_class *));
extern VALUE bdb_each_eulav _((int, VALUE *, VALUE));
extern VALUE bdb_each_key _((int, VALUE *, VALUE));
extern VALUE bdb_each_value _((int, VALUE *, VALUE));
extern VALUE bdb_each_valuec _((int, VALUE *, VALUE, int, VALUE));
extern VALUE bdb_each_kvc _((int, VALUE *, VALUE, int, VALUE, int));
#if HAVE_DB_ENV_ERRCALL_3
extern void bdb_env_errcall _((const DB_ENV *, const char *, const char *));
#else
extern void bdb_env_errcall _((const char *, char *));
#endif
extern VALUE bdb_env_init _((int, VALUE *, VALUE));
extern VALUE bdb_protect_close _((VALUE));
extern VALUE bdb_env_open_db _((int, VALUE *, VALUE));
extern VALUE bdb_get _((int, VALUE *, VALUE));
extern VALUE bdb_env_p _((VALUE));
extern VALUE bdb_has_value _((VALUE, VALUE));
extern VALUE bdb_index _((VALUE, VALUE));
extern VALUE bdb_internal_value _((VALUE, VALUE, VALUE, int));
extern VALUE bdb_put _((int, VALUE *, VALUE));
extern VALUE bdb_test_load _((VALUE, DBT *, int));
extern VALUE bdb_to_type _((VALUE, VALUE, VALUE));
extern VALUE bdb_tree_stat _((int, VALUE *, VALUE));
extern VALUE bdb_init _((int, VALUE *, VALUE));
extern void bdb_init_env _((void));
extern void bdb_init_common _((void));
extern void bdb_init_recnum _((void));
extern void bdb_init_transaction _((void));
extern void bdb_init_cursor _((void));
extern void bdb_init_lock _((void));
extern void bdb_init_log _((void));
extern void bdb_init_delegator _((void));
extern void bdb_init_sequence _((void));
extern void bdb_clean_env _((VALUE, VALUE));
extern VALUE bdb_makelsn _((VALUE));
extern VALUE bdb_env_rslbl_begin _((VALUE, int, VALUE *, VALUE));
extern VALUE bdb_return_err _((void));
extern void bdb_ary_push _((struct ary_st *, VALUE));
extern void bdb_ary_unshift _((struct ary_st *, VALUE));
extern VALUE bdb_ary_delete _((struct ary_st *, VALUE));
extern void bdb_ary_mark _((struct ary_st *));
extern VALUE bdb_respond_to _((VALUE, ID));
#if ! HAVE_ST_DBC_C_CLOSE && HAVE_ST_DBC_CLOSE
#define c_close close
#undef HAVE_ST_DBC_C_CLOSE
#define HAVE_ST_DBC_C_CLOSE 1
#endif
#if ! HAVE_ST_DBC_C_COUNT && HAVE_ST_DBC_COUNT
#define c_count count
#undef HAVE_ST_DBC_C_COUNT
#define HAVE_ST_DBC_C_COUNT 1
#endif
#if ! HAVE_ST_DBC_C_DEL && HAVE_ST_DBC_DEL
#define c_del del
#undef HAVE_ST_DBC_C_DEL
#define HAVE_ST_DBC_C_DEL 1
#endif
#if ! HAVE_ST_DBC_C_DUP && HAVE_ST_DBC_DUP
#define c_dup dup
#undef HAVE_ST_DBC_C_DUP
#define HAVE_ST_DBC_C_DUP 1
#endif
#if ! HAVE_ST_DBC_C_GET && HAVE_ST_DBC_GET
#define c_get get
#undef HAVE_ST_DBC_C_GET
#define HAVE_ST_DBC_C_GET 1
#endif
#if ! HAVE_ST_DBC_C_PGET && HAVE_ST_DBC_PGET
#define c_pget pget
#undef HAVE_ST_DBC_C_PGET
#define HAVE_ST_DBC_C_PGET 1
#endif
#if ! HAVE_ST_DBC_C_PUT && HAVE_ST_DBC_PUT
#define c_put put
#undef HAVE_ST_DBC_C_PUT
#define HAVE_ST_DBC_C_PUT 1
#endif
#if defined(__cplusplus)
}
#endif