ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/BDB/BDB.xs
Revision: 1.49
Committed: Thu Sep 25 12:28:49 2008 UTC (15 years, 7 months ago) by root
Branch: MAIN
Changes since 1.48: +7 -3 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.49 #include <sys/user.h>
2     #include <sys/ptrace.h>
3    
4 root 1.48 #define X_STACKSIZE 1024 * 128 + sizeof (long) * 64 * 1024 / 4
5    
6 root 1.11 #include "xthread.h"
7 root 1.1
8     #include <errno.h>
9    
10     #include "EXTERN.h"
11     #include "perl.h"
12     #include "XSUB.h"
13    
14 root 1.13 // perl stupidly defines these as macros, breaking
15     // lots and lots of code.
16     #undef open
17     #undef close
18     #undef abort
19     #undef malloc
20     #undef free
21     #undef send
22    
23 root 1.1 #include <stddef.h>
24     #include <stdlib.h>
25     #include <errno.h>
26     #include <sys/types.h>
27     #include <limits.h>
28     #include <fcntl.h>
29 root 1.2
30 root 1.13 #ifndef _WIN32
31     # include <sys/time.h>
32     # include <unistd.h>
33     #endif
34    
35 root 1.2 #include <db.h>
36 root 1.1
37 root 1.47 #if DB_VERSION_MAJOR != 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR < 3)
38     # error you need Berkeley DB 4.3 or a newer 4.x version installed
39 root 1.10 #endif
40    
41 root 1.1 /* number of seconds after which idle threads exit */
42     #define IDLE_TIMEOUT 10
43    
44 root 1.8 typedef DB_ENV DB_ENV_ornull;
45     typedef DB_TXN DB_TXN_ornull;
46     typedef DBC DBC_ornull;
47     typedef DB DB_ornull;
48 root 1.5
49 root 1.30 typedef DB_ENV DB_ENV_ornuked;
50     typedef DB_TXN DB_TXN_ornuked;
51     typedef DBC DBC_ornuked;
52     typedef DB DB_ornuked;
53 root 1.47
54     #if DB_VERSION_MINOR >= 3
55     typedef DB_SEQUENCE DB_SEQUENCE_ornull;
56 root 1.30 typedef DB_SEQUENCE DB_SEQUENCE_ornuked;
57 root 1.47 #endif
58 root 1.30
59 root 1.1 typedef SV SV8; /* byte-sv, used for argument-checking */
60 root 1.33 typedef char *bdb_filename;
61 root 1.1
62 root 1.3 static SV *prepare_cb;
63    
64 root 1.16 #if DB_VERSION_MINOR >= 6
65     # define c_close close
66     # define c_count count
67     # define c_del del
68     # define c_dup dup
69     # define c_get get
70     # define c_pget pget
71     # define c_put put
72     #endif
73    
74 root 1.34 static char *
75     get_bdb_filename (SV *sv)
76     {
77 root 1.36 if (!SvOK (sv))
78     return 0;
79    
80 root 1.35 #if _WIN32
81 root 1.36 /* win32 madness + win32 perl absolutely brokenness make for horrible hacks */
82     {
83     STRLEN len;
84     char *src = SvPVbyte (sv, len);
85     SV *t1 = sv_newmortal ();
86     SV *t2 = sv_newmortal ();
87    
88     sv_upgrade (t1, SVt_PV); SvPOK_only (t1); SvGROW (t1, len * 16 + 1);
89     sv_upgrade (t2, SVt_PV); SvPOK_only (t2); SvGROW (t2, len * 16 + 1);
90    
91     len = MultiByteToWideChar (CP_ACP, 0, src, len, (WCHAR *)SvPVX (t1), SvLEN (t1) / sizeof (WCHAR));
92     len = WideCharToMultiByte (CP_UTF8, 0, (WCHAR *)SvPVX (t1), len, SvPVX (t2), SvLEN (t2), 0, 0);
93     SvPOK_only (t2);
94     SvPVX (t2)[len] = 0;
95     SvCUR_set (t2, len);
96    
97     return SvPVX (t2);
98     }
99 root 1.34 #else
100 root 1.36 return SvPVbyte_nolen (sv);
101 root 1.34 #endif
102     }
103    
104 root 1.14 static void
105     debug_errcall (const DB_ENV *dbenv, const char *errpfx, const char *msg)
106     {
107     printf ("err[%s]\n", msg);
108     }
109    
110     static void
111     debug_msgcall (const DB_ENV *dbenv, const char *msg)
112     {
113     printf ("msg[%s]\n", msg);
114     }
115    
116 root 1.13 static char *
117 root 1.3 strdup_ornull (const char *s)
118 root 1.2 {
119 root 1.3 return s ? strdup (s) : 0;
120     }
121    
122 root 1.13 static void
123 root 1.5 sv_to_dbt (DBT *dbt, SV *sv)
124     {
125     STRLEN len;
126     char *data = SvPVbyte (sv, len);
127    
128     dbt->data = malloc (len);
129     memcpy (dbt->data, data, len);
130     dbt->size = len;
131 root 1.6 dbt->flags = DB_DBT_REALLOC;
132 root 1.5 }
133    
134 root 1.13 static void
135 root 1.5 dbt_to_sv (SV *sv, DBT *dbt)
136     {
137 root 1.6 if (sv)
138     {
139     SvREADONLY_off (sv);
140 root 1.49 sv_setsv_mg (sv, dbt->data ? newSVpvn (dbt->data, dbt->size) : &PL_sv_undef);
141 root 1.6 SvREFCNT_dec (sv);
142     }
143 root 1.5
144 root 1.49 //assert (dbt->flags & DBT_MALLOC || !dbt->data);
145 root 1.5 free (dbt->data);
146     }
147    
148 root 1.3 enum {
149 root 1.1 REQ_QUIT,
150 root 1.8 REQ_ENV_OPEN, REQ_ENV_CLOSE, REQ_ENV_TXN_CHECKPOINT, REQ_ENV_LOCK_DETECT,
151 root 1.39 REQ_ENV_MEMP_SYNC, REQ_ENV_MEMP_TRICKLE, REQ_ENV_DBREMOVE, REQ_ENV_DBRENAME,
152 root 1.26 REQ_DB_OPEN, REQ_DB_CLOSE, REQ_DB_COMPACT, REQ_DB_SYNC, REQ_DB_UPGRADE,
153 root 1.45 REQ_DB_PUT, REQ_DB_EXISTS, REQ_DB_GET, REQ_DB_PGET, REQ_DB_DEL, REQ_DB_KEY_RANGE,
154 root 1.17 REQ_TXN_COMMIT, REQ_TXN_ABORT, REQ_TXN_FINISH,
155 root 1.6 REQ_C_CLOSE, REQ_C_COUNT, REQ_C_PUT, REQ_C_GET, REQ_C_PGET, REQ_C_DEL,
156 root 1.8 REQ_SEQ_OPEN, REQ_SEQ_CLOSE, REQ_SEQ_GET, REQ_SEQ_REMOVE,
157 root 1.1 };
158    
159 root 1.28 typedef struct bdb_cb
160 root 1.1 {
161 root 1.28 struct bdb_cb *volatile next;
162 root 1.2 SV *callback;
163 root 1.3 int type, pri, result;
164 root 1.2
165     DB_ENV *env;
166     DB *db;
167     DB_TXN *txn;
168 root 1.6 DBC *dbc;
169    
170     UV uv1;
171 root 1.2 int int1, int2;
172     U32 uint1, uint2;
173 root 1.39 char *buf1, *buf2, *buf3;
174 root 1.6 SV *sv1, *sv2, *sv3;
175 root 1.3
176     DBT dbt1, dbt2, dbt3;
177 root 1.8 DB_KEY_RANGE key_range;
178 root 1.47 #if DB_VERSION_MINOR >= 3
179 root 1.8 DB_SEQUENCE *seq;
180     db_seq_t seq_t;
181 root 1.47 #endif
182 root 1.28 } bdb_cb;
183 root 1.1
184 root 1.28 typedef bdb_cb *bdb_req;
185 root 1.1
186     enum {
187     PRI_MIN = -4,
188     PRI_MAX = 4,
189    
190     DEFAULT_PRI = 0,
191     PRI_BIAS = -PRI_MIN,
192     NUM_PRI = PRI_MAX + PRI_BIAS + 1,
193     };
194    
195     #define AIO_TICKS ((1000000 + 1023) >> 10)
196    
197 root 1.37 static SV *on_next_submit;
198    
199 root 1.1 static unsigned int max_poll_time = 0;
200     static unsigned int max_poll_reqs = 0;
201    
202     /* calculcate time difference in ~1/AIO_TICKS of a second */
203     static int tvdiff (struct timeval *tv1, struct timeval *tv2)
204     {
205     return (tv2->tv_sec - tv1->tv_sec ) * AIO_TICKS
206     + ((tv2->tv_usec - tv1->tv_usec) >> 10);
207     }
208    
209     static int next_pri = DEFAULT_PRI + PRI_BIAS;
210    
211     static unsigned int started, idle, wanted;
212    
213     /* worker threads management */
214 root 1.12 static mutex_t wrklock = X_MUTEX_INIT;
215 root 1.1
216     typedef struct worker {
217     /* locked by wrklock */
218     struct worker *prev, *next;
219    
220 root 1.11 thread_t tid;
221 root 1.1
222     /* locked by reslock, reqlock or wrklock */
223 root 1.28 bdb_req req; /* currently processed request */
224 root 1.1 void *dbuf;
225     DIR *dirp;
226     } worker;
227    
228     static worker wrk_first = { &wrk_first, &wrk_first, 0 };
229    
230     static void worker_clear (worker *wrk)
231     {
232     }
233    
234     static void worker_free (worker *wrk)
235     {
236     wrk->next->prev = wrk->prev;
237     wrk->prev->next = wrk->next;
238    
239     free (wrk);
240     }
241    
242     static volatile unsigned int nreqs, nready, npending;
243     static volatile unsigned int max_idle = 4;
244     static volatile unsigned int max_outstanding = 0xffffffff;
245 root 1.19 static int respipe_osf [2], respipe [2] = { -1, -1 };
246 root 1.1
247 root 1.12 static mutex_t reslock = X_MUTEX_INIT;
248     static mutex_t reqlock = X_MUTEX_INIT;
249     static cond_t reqwait = X_COND_INIT;
250 root 1.1
251     #if WORDACCESS_UNSAFE
252    
253 root 1.34 static unsigned int get_nready (void)
254 root 1.1 {
255     unsigned int retval;
256    
257 root 1.12 X_LOCK (reqlock);
258 root 1.1 retval = nready;
259 root 1.12 X_UNLOCK (reqlock);
260 root 1.1
261     return retval;
262     }
263    
264 root 1.34 static unsigned int get_npending (void)
265 root 1.1 {
266     unsigned int retval;
267    
268 root 1.12 X_LOCK (reslock);
269 root 1.1 retval = npending;
270 root 1.12 X_UNLOCK (reslock);
271 root 1.1
272     return retval;
273     }
274    
275 root 1.34 static unsigned int get_nthreads (void)
276 root 1.1 {
277     unsigned int retval;
278    
279 root 1.12 X_LOCK (wrklock);
280 root 1.1 retval = started;
281 root 1.12 X_UNLOCK (wrklock);
282 root 1.1
283     return retval;
284     }
285    
286     #else
287    
288     # define get_nready() nready
289     # define get_npending() npending
290     # define get_nthreads() started
291    
292     #endif
293    
294     /*
295     * a somewhat faster data structure might be nice, but
296     * with 8 priorities this actually needs <20 insns
297     * per shift, the most expensive operation.
298     */
299     typedef struct {
300 root 1.28 bdb_req qs[NUM_PRI], qe[NUM_PRI]; /* qstart, qend */
301 root 1.1 int size;
302     } reqq;
303    
304     static reqq req_queue;
305     static reqq res_queue;
306    
307 root 1.28 int reqq_push (reqq *q, bdb_req req)
308 root 1.1 {
309     int pri = req->pri;
310     req->next = 0;
311    
312     if (q->qe[pri])
313     {
314     q->qe[pri]->next = req;
315     q->qe[pri] = req;
316     }
317     else
318     q->qe[pri] = q->qs[pri] = req;
319    
320     return q->size++;
321     }
322    
323 root 1.28 bdb_req reqq_shift (reqq *q)
324 root 1.1 {
325     int pri;
326    
327     if (!q->size)
328     return 0;
329    
330     --q->size;
331    
332     for (pri = NUM_PRI; pri--; )
333     {
334 root 1.28 bdb_req req = q->qs[pri];
335 root 1.1
336     if (req)
337     {
338     if (!(q->qs[pri] = req->next))
339     q->qe[pri] = 0;
340    
341     return req;
342     }
343     }
344    
345     abort ();
346     }
347    
348 root 1.34 static int poll_cb (void);
349 root 1.28 static void req_free (bdb_req req);
350     static void req_cancel (bdb_req req);
351 root 1.1
352 root 1.28 static int req_invoke (bdb_req req)
353 root 1.1 {
354     dSP;
355    
356 root 1.41 if (req->callback)
357 root 1.1 {
358     ENTER;
359     SAVETMPS;
360     PUSHMARK (SP);
361    
362     switch (req->type)
363     {
364 root 1.6 case REQ_DB_CLOSE:
365     SvREFCNT_dec (req->sv1);
366     break;
367    
368 root 1.5 case REQ_DB_GET:
369     case REQ_DB_PGET:
370 root 1.6 dbt_to_sv (req->sv3, &req->dbt3);
371 root 1.5 break;
372    
373 root 1.6 case REQ_C_GET:
374     case REQ_C_PGET:
375     dbt_to_sv (req->sv1, &req->dbt1);
376     dbt_to_sv (req->sv2, &req->dbt2);
377     dbt_to_sv (req->sv3, &req->dbt3);
378 root 1.5 break;
379 root 1.8
380 root 1.23 case REQ_DB_PUT:
381     case REQ_C_PUT:
382     dbt_to_sv (0, &req->dbt1);
383     dbt_to_sv (0, &req->dbt2);
384     break;
385    
386 root 1.8 case REQ_DB_KEY_RANGE:
387     {
388     AV *av = newAV ();
389    
390     av_push (av, newSVnv (req->key_range.less));
391     av_push (av, newSVnv (req->key_range.equal));
392     av_push (av, newSVnv (req->key_range.greater));
393    
394     SvREADONLY_off (req->sv1);
395     sv_setsv_mg (req->sv1, newRV_noinc ((SV *)av));
396     SvREFCNT_dec (req->sv1);
397     }
398     break;
399    
400 root 1.47 #if DB_VERSION_MINOR >= 3
401 root 1.8 case REQ_SEQ_GET:
402     SvREADONLY_off (req->sv1);
403    
404     if (sizeof (IV) > 4)
405 root 1.21 sv_setiv_mg (req->sv1, (IV)req->seq_t);
406 root 1.8 else
407 root 1.21 sv_setnv_mg (req->sv1, (NV)req->seq_t);
408 root 1.8
409     SvREFCNT_dec (req->sv1);
410     break;
411 root 1.47 #endif
412 root 1.1 }
413    
414 root 1.3 errno = req->result;
415    
416 root 1.1 PUTBACK;
417     call_sv (req->callback, G_VOID | G_EVAL);
418     SPAGAIN;
419    
420     FREETMPS;
421     LEAVE;
422     }
423    
424     return !SvTRUE (ERRSV);
425     }
426    
427 root 1.28 static void req_free (bdb_req req)
428 root 1.1 {
429 root 1.43 SvREFCNT_dec (req->callback);
430    
431 root 1.2 free (req->buf1);
432     free (req->buf2);
433 root 1.39 free (req->buf3);
434 root 1.43
435 root 1.1 Safefree (req);
436     }
437    
438 root 1.13 #ifdef USE_SOCKETS_AS_HANDLES
439     # define TO_SOCKET(x) (win32_get_osfhandle (x))
440     #else
441     # define TO_SOCKET(x) (x)
442     #endif
443    
444     static void
445 root 1.34 create_respipe (void)
446 root 1.13 {
447 root 1.21 #ifdef _WIN32
448     int arg; /* argg */
449     #endif
450 root 1.19 int old_readfd = respipe [0];
451    
452     if (respipe [1] >= 0)
453     respipe_close (TO_SOCKET (respipe [1]));
454    
455     #ifdef _WIN32
456     if (PerlSock_socketpair (AF_UNIX, SOCK_STREAM, 0, respipe))
457     #else
458     if (pipe (respipe))
459     #endif
460     croak ("unable to initialize result pipe");
461    
462     if (old_readfd >= 0)
463     {
464     if (dup2 (TO_SOCKET (respipe [0]), TO_SOCKET (old_readfd)) < 0)
465     croak ("unable to initialize result pipe(2)");
466    
467     respipe_close (respipe [0]);
468     respipe [0] = old_readfd;
469     }
470    
471 root 1.13 #ifdef _WIN32
472 root 1.21 arg = 1;
473 root 1.19 if (ioctlsocket (TO_SOCKET (respipe [0]), FIONBIO, &arg)
474     || ioctlsocket (TO_SOCKET (respipe [1]), FIONBIO, &arg))
475 root 1.13 #else
476 root 1.19 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)
477     || fcntl (respipe [1], F_SETFL, O_NONBLOCK))
478 root 1.13 #endif
479 root 1.19 croak ("unable to initialize result pipe(3)");
480 root 1.13
481     respipe_osf [0] = TO_SOCKET (respipe [0]);
482     respipe_osf [1] = TO_SOCKET (respipe [1]);
483     }
484    
485     X_THREAD_PROC (bdb_proc);
486 root 1.1
487     static void start_thread (void)
488     {
489     worker *wrk = calloc (1, sizeof (worker));
490    
491     if (!wrk)
492     croak ("unable to allocate worker thread data");
493    
494 root 1.12 X_LOCK (wrklock);
495 root 1.13 if (thread_create (&wrk->tid, bdb_proc, (void *)wrk))
496 root 1.1 {
497     wrk->prev = &wrk_first;
498     wrk->next = wrk_first.next;
499     wrk_first.next->prev = wrk;
500     wrk_first.next = wrk;
501     ++started;
502     }
503     else
504     free (wrk);
505    
506 root 1.12 X_UNLOCK (wrklock);
507 root 1.1 }
508    
509 root 1.34 static void maybe_start_thread (void)
510 root 1.1 {
511     if (get_nthreads () >= wanted)
512     return;
513    
514     /* todo: maybe use idle here, but might be less exact */
515     if (0 <= (int)get_nthreads () + (int)get_npending () - (int)nreqs)
516     return;
517    
518     start_thread ();
519     }
520    
521 root 1.28 static void req_send (bdb_req req)
522 root 1.1 {
523 root 1.3 SV *wait_callback = 0;
524    
525 root 1.37 if (on_next_submit)
526     {
527     dSP;
528     SV *cb = sv_2mortal (on_next_submit);
529    
530     on_next_submit = 0;
531    
532     PUSHMARK (SP);
533     PUTBACK;
534     call_sv (cb, G_DISCARD | G_EVAL);
535 root 1.49 SPAGAIN;
536 root 1.37 }
537    
538 root 1.3 // synthesize callback if none given
539 root 1.41 if (!req->callback)
540 root 1.3 {
541 root 1.13 int count;
542    
543 root 1.3 dSP;
544     PUSHMARK (SP);
545     PUTBACK;
546 root 1.13 count = call_sv (prepare_cb, G_ARRAY);
547 root 1.3 SPAGAIN;
548    
549     if (count != 2)
550     croak ("prepare callback must return exactly two values\n");
551    
552 root 1.27 wait_callback = POPs;
553 root 1.3 req->callback = SvREFCNT_inc (POPs);
554     }
555    
556 root 1.1 ++nreqs;
557    
558 root 1.12 X_LOCK (reqlock);
559 root 1.1 ++nready;
560     reqq_push (&req_queue, req);
561 root 1.12 X_COND_SIGNAL (reqwait);
562     X_UNLOCK (reqlock);
563 root 1.1
564     maybe_start_thread ();
565 root 1.3
566     if (wait_callback)
567     {
568     dSP;
569     PUSHMARK (SP);
570     PUTBACK;
571     call_sv (wait_callback, G_DISCARD);
572     }
573 root 1.1 }
574    
575     static void end_thread (void)
576     {
577 root 1.28 bdb_req req;
578 root 1.1
579 root 1.28 Newz (0, req, 1, bdb_cb);
580 root 1.1
581     req->type = REQ_QUIT;
582     req->pri = PRI_MAX + PRI_BIAS;
583    
584 root 1.12 X_LOCK (reqlock);
585 root 1.1 reqq_push (&req_queue, req);
586 root 1.12 X_COND_SIGNAL (reqwait);
587     X_UNLOCK (reqlock);
588 root 1.1
589 root 1.12 X_LOCK (wrklock);
590 root 1.1 --started;
591 root 1.12 X_UNLOCK (wrklock);
592 root 1.1 }
593    
594     static void set_max_idle (int nthreads)
595     {
596 root 1.12 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
597 root 1.1 max_idle = nthreads <= 0 ? 1 : nthreads;
598 root 1.12 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
599 root 1.1 }
600    
601     static void min_parallel (int nthreads)
602     {
603     if (wanted < nthreads)
604     wanted = nthreads;
605     }
606    
607     static void max_parallel (int nthreads)
608     {
609     if (wanted > nthreads)
610     wanted = nthreads;
611    
612     while (started > wanted)
613     end_thread ();
614     }
615    
616 root 1.34 static void poll_wait (void)
617 root 1.1 {
618     fd_set rfd;
619    
620     while (nreqs)
621     {
622     int size;
623 root 1.12 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
624 root 1.1 size = res_queue.size;
625 root 1.12 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
626 root 1.1
627     if (size)
628     return;
629    
630     maybe_start_thread ();
631    
632 root 1.13 FD_ZERO (&rfd);
633     FD_SET (respipe [0], &rfd);
634 root 1.1
635 root 1.13 PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0);
636 root 1.1 }
637     }
638    
639 root 1.34 static int poll_cb (void)
640 root 1.1 {
641     dSP;
642     int count = 0;
643     int maxreqs = max_poll_reqs;
644     int do_croak = 0;
645     struct timeval tv_start, tv_now;
646 root 1.28 bdb_req req;
647 root 1.1
648     if (max_poll_time)
649     gettimeofday (&tv_start, 0);
650    
651     for (;;)
652     {
653     for (;;)
654     {
655     maybe_start_thread ();
656    
657 root 1.12 X_LOCK (reslock);
658 root 1.1 req = reqq_shift (&res_queue);
659    
660     if (req)
661     {
662     --npending;
663    
664     if (!res_queue.size)
665     {
666     /* read any signals sent by the worker threads */
667     char buf [4];
668 root 1.13 while (respipe_read (respipe [0], buf, 4) == 4)
669 root 1.1 ;
670     }
671     }
672    
673 root 1.12 X_UNLOCK (reslock);
674 root 1.1
675     if (!req)
676     break;
677    
678     --nreqs;
679    
680     if (!req_invoke (req))
681     {
682     req_free (req);
683     croak (0);
684     }
685    
686     count++;
687    
688     req_free (req);
689    
690     if (maxreqs && !--maxreqs)
691     break;
692    
693     if (max_poll_time)
694     {
695     gettimeofday (&tv_now, 0);
696    
697     if (tvdiff (&tv_start, &tv_now) >= max_poll_time)
698     break;
699     }
700     }
701    
702     if (nreqs <= max_outstanding)
703     break;
704    
705     poll_wait ();
706    
707     ++maxreqs;
708     }
709    
710     return count;
711     }
712    
713     /*****************************************************************************/
714    
715 root 1.13 X_THREAD_PROC (bdb_proc)
716 root 1.1 {
717 root 1.28 bdb_req req;
718 root 1.1 struct timespec ts;
719     worker *self = (worker *)thr_arg;
720    
721     /* try to distribute timeouts somewhat evenly */
722 root 1.13 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
723 root 1.1
724     for (;;)
725     {
726     ts.tv_sec = time (0) + IDLE_TIMEOUT;
727    
728 root 1.12 X_LOCK (reqlock);
729 root 1.1
730     for (;;)
731     {
732     self->req = req = reqq_shift (&req_queue);
733    
734     if (req)
735     break;
736    
737     ++idle;
738    
739 root 1.12 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts)
740 root 1.1 == ETIMEDOUT)
741     {
742     if (idle > max_idle)
743     {
744     --idle;
745 root 1.12 X_UNLOCK (reqlock);
746     X_LOCK (wrklock);
747 root 1.1 --started;
748 root 1.12 X_UNLOCK (wrklock);
749 root 1.1 goto quit;
750     }
751    
752     /* we are allowed to idle, so do so without any timeout */
753 root 1.12 X_COND_WAIT (reqwait, reqlock);
754 root 1.1 ts.tv_sec = time (0) + IDLE_TIMEOUT;
755     }
756    
757     --idle;
758     }
759    
760     --nready;
761    
762 root 1.12 X_UNLOCK (reqlock);
763 root 1.49
764 root 1.1 switch (req->type)
765     {
766     case REQ_QUIT:
767 root 1.17 req->result = ENOSYS;
768 root 1.1 goto quit;
769    
770 root 1.3 case REQ_ENV_OPEN:
771     req->result = req->env->open (req->env, req->buf1, req->uint1, req->int1);
772     break;
773    
774     case REQ_ENV_CLOSE:
775     req->result = req->env->close (req->env, req->uint1);
776     break;
777    
778 root 1.8 case REQ_ENV_TXN_CHECKPOINT:
779     req->result = req->env->txn_checkpoint (req->env, req->uint1, req->int1, req->uint2);
780     break;
781    
782     case REQ_ENV_LOCK_DETECT:
783     req->result = req->env->lock_detect (req->env, req->uint1, req->uint2, &req->int1);
784     break;
785    
786     case REQ_ENV_MEMP_SYNC:
787     req->result = req->env->memp_sync (req->env, 0);
788     break;
789    
790     case REQ_ENV_MEMP_TRICKLE:
791     req->result = req->env->memp_trickle (req->env, req->int1, &req->int2);
792     break;
793    
794 root 1.39 case REQ_ENV_DBREMOVE:
795     req->result = req->env->dbremove (req->env, req->txn, req->buf1, req->buf2, req->uint1);
796     break;
797    
798     case REQ_ENV_DBRENAME:
799     req->result = req->env->dbrename (req->env, req->txn, req->buf1, req->buf2, req->buf3, req->uint1);
800     break;
801    
802 root 1.3 case REQ_DB_OPEN:
803     req->result = req->db->open (req->db, req->txn, req->buf1, req->buf2, req->int1, req->uint1, req->int2);
804     break;
805    
806     case REQ_DB_CLOSE:
807     req->result = req->db->close (req->db, req->uint1);
808     break;
809    
810 root 1.47 #if DB_VERSION_MINOR >= 4
811 root 1.3 case REQ_DB_COMPACT:
812     req->result = req->db->compact (req->db, req->txn, &req->dbt1, &req->dbt2, 0, req->uint1, 0);
813     break;
814 root 1.47 #endif
815 root 1.3
816     case REQ_DB_SYNC:
817     req->result = req->db->sync (req->db, req->uint1);
818     break;
819    
820 root 1.26 case REQ_DB_UPGRADE:
821     req->result = req->db->upgrade (req->db, req->buf1, req->uint1);
822     break;
823    
824 root 1.3 case REQ_DB_PUT:
825     req->result = req->db->put (req->db, req->txn, &req->dbt1, &req->dbt2, req->uint1);
826     break;
827    
828 root 1.46 #if DB_VERSION_MINOR >= 6
829 root 1.45 case REQ_DB_EXISTS:
830     req->result = req->db->exists (req->db, req->txn, &req->dbt1, req->uint1);
831     break;
832 root 1.46 #endif
833 root 1.5 case REQ_DB_GET:
834     req->result = req->db->get (req->db, req->txn, &req->dbt1, &req->dbt3, req->uint1);
835     break;
836    
837     case REQ_DB_PGET:
838     req->result = req->db->pget (req->db, req->txn, &req->dbt1, &req->dbt2, &req->dbt3, req->uint1);
839     break;
840    
841 root 1.6 case REQ_DB_DEL:
842     req->result = req->db->del (req->db, req->txn, &req->dbt1, req->uint1);
843     break;
844    
845 root 1.8 case REQ_DB_KEY_RANGE:
846     req->result = req->db->key_range (req->db, req->txn, &req->dbt1, &req->key_range, req->uint1);
847     break;
848    
849 root 1.5 case REQ_TXN_COMMIT:
850     req->result = req->txn->commit (req->txn, req->uint1);
851     break;
852    
853     case REQ_TXN_ABORT:
854     req->result = req->txn->abort (req->txn);
855     break;
856    
857 root 1.17 case REQ_TXN_FINISH:
858     if (req->txn->flags & TXN_DEADLOCK)
859     {
860     req->result = req->txn->abort (req->txn);
861     if (!req->result)
862     req->result = DB_LOCK_DEADLOCK;
863     }
864 root 1.18 else
865     req->result = req->txn->commit (req->txn, req->uint1);
866 root 1.17 break;
867    
868 root 1.6 case REQ_C_CLOSE:
869     req->result = req->dbc->c_close (req->dbc);
870     break;
871    
872     case REQ_C_COUNT:
873     {
874     db_recno_t recno;
875     req->result = req->dbc->c_count (req->dbc, &recno, req->uint1);
876     req->uv1 = recno;
877     }
878     break;
879    
880     case REQ_C_PUT:
881     req->result = req->dbc->c_put (req->dbc, &req->dbt1, &req->dbt2, req->uint1);
882     break;
883    
884     case REQ_C_GET:
885     req->result = req->dbc->c_get (req->dbc, &req->dbt1, &req->dbt3, req->uint1);
886     break;
887    
888     case REQ_C_PGET:
889     req->result = req->dbc->c_pget (req->dbc, &req->dbt1, &req->dbt2, &req->dbt3, req->uint1);
890     break;
891    
892     case REQ_C_DEL:
893     req->result = req->dbc->c_del (req->dbc, req->uint1);
894     break;
895    
896 root 1.47 #if DB_VERSION_MINOR >= 3
897 root 1.8 case REQ_SEQ_OPEN:
898     req->result = req->seq->open (req->seq, req->txn, &req->dbt1, req->uint1);
899     break;
900    
901     case REQ_SEQ_CLOSE:
902     req->result = req->seq->close (req->seq, req->uint1);
903     break;
904    
905     case REQ_SEQ_GET:
906     req->result = req->seq->get (req->seq, req->txn, req->int1, &req->seq_t, req->uint1);
907     break;
908    
909     case REQ_SEQ_REMOVE:
910     req->result = req->seq->remove (req->seq, req->txn, req->uint1);
911     break;
912 root 1.47 #endif
913 root 1.8
914 root 1.1 default:
915 root 1.3 req->result = ENOSYS;
916 root 1.1 break;
917     }
918    
919 root 1.17 if (req->txn && (req->result > 0 || req->result == DB_LOCK_NOTGRANTED))
920     req->txn->flags |= TXN_DEADLOCK;
921    
922 root 1.12 X_LOCK (reslock);
923 root 1.1
924     ++npending;
925    
926     if (!reqq_push (&res_queue, req))
927     /* write a dummy byte to the pipe so fh becomes ready */
928 root 1.13 respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1);
929 root 1.1
930     self->req = 0;
931     worker_clear (self);
932    
933 root 1.12 X_UNLOCK (reslock);
934 root 1.1 }
935    
936     quit:
937 root 1.12 X_LOCK (wrklock);
938 root 1.1 worker_free (self);
939 root 1.12 X_UNLOCK (wrklock);
940 root 1.1
941     return 0;
942     }
943    
944     /*****************************************************************************/
945    
946     static void atfork_prepare (void)
947     {
948 root 1.12 X_LOCK (wrklock);
949     X_LOCK (reqlock);
950     X_LOCK (reslock);
951 root 1.1 }
952    
953     static void atfork_parent (void)
954     {
955 root 1.12 X_UNLOCK (reslock);
956     X_UNLOCK (reqlock);
957     X_UNLOCK (wrklock);
958 root 1.1 }
959    
960     static void atfork_child (void)
961     {
962 root 1.28 bdb_req prv;
963 root 1.1
964     while (prv = reqq_shift (&req_queue))
965     req_free (prv);
966    
967     while (prv = reqq_shift (&res_queue))
968     req_free (prv);
969    
970     while (wrk_first.next != &wrk_first)
971     {
972     worker *wrk = wrk_first.next;
973    
974     if (wrk->req)
975     req_free (wrk->req);
976    
977     worker_clear (wrk);
978     worker_free (wrk);
979     }
980    
981     started = 0;
982     idle = 0;
983     nreqs = 0;
984     nready = 0;
985     npending = 0;
986    
987 root 1.19 create_respipe ();
988 root 1.1
989     atfork_parent ();
990     }
991    
992 root 1.2 #define dREQ(reqtype) \
993 root 1.28 bdb_req req; \
994 root 1.1 int req_pri = next_pri; \
995     next_pri = DEFAULT_PRI + PRI_BIAS; \
996     \
997 root 1.41 if (callback && SvOK (callback)) \
998     croak ("callback has illegal type or extra arguments"); \
999 root 1.1 \
1000 root 1.28 Newz (0, req, 1, bdb_cb); \
1001 root 1.1 if (!req) \
1002 root 1.28 croak ("out of memory during bdb_req allocation"); \
1003 root 1.1 \
1004 root 1.41 req->callback = cb ? SvREFCNT_inc (cb) : 0; \
1005 root 1.2 req->type = (reqtype); \
1006 root 1.1 req->pri = req_pri
1007    
1008     #define REQ_SEND \
1009 root 1.2 req_send (req)
1010    
1011 root 1.30 #define SvPTR(var, arg, type, class, nullok) \
1012 root 1.5 if (!SvOK (arg)) \
1013     { \
1014 root 1.30 if (nullok != 1) \
1015 root 1.7 croak (# var " must be a " # class " object, not undef"); \
1016 root 1.5 \
1017     (var) = 0; \
1018     } \
1019     else if (sv_derived_from ((arg), # class)) \
1020     { \
1021     IV tmp = SvIV ((SV*) SvRV (arg)); \
1022     (var) = INT2PTR (type, tmp); \
1023 root 1.30 if (!var && nullok != 2) \
1024 root 1.7 croak (# var " is not a valid " # class " object anymore"); \
1025 root 1.5 } \
1026     else \
1027 root 1.42 croak (# var " is not of type " # class);
1028 root 1.3
1029 root 1.5 static void
1030     ptr_nuke (SV *sv)
1031 root 1.3 {
1032 root 1.5 assert (SvROK (sv));
1033     sv_setiv (SvRV (sv), 0);
1034     }
1035 root 1.3
1036 root 1.31 static int
1037     errno_get (pTHX_ SV *sv, MAGIC *mg)
1038     {
1039     if (*mg->mg_ptr == '!') // should always be the case
1040     if (-30999 <= errno && errno <= -30800)
1041     {
1042 root 1.32 sv_setnv (sv, (NV)errno);
1043 root 1.31 sv_setpv (sv, db_strerror (errno));
1044 root 1.32 SvNOK_on (sv); /* what a wonderful hack! */
1045     // ^^^ copied from perl sources
1046 root 1.31 return 0;
1047     }
1048    
1049     return PL_vtbl_sv.svt_get (aTHX_ sv, mg);
1050     }
1051    
1052     static MGVTBL vtbl_errno;
1053    
1054     // this wonderful hack :( patches perl's $! variable to support our errno values
1055     static void
1056     patch_errno (void)
1057     {
1058     SV *sv;
1059     MAGIC *mg;
1060    
1061     if (!(sv = get_sv ("!", 1)))
1062     return;
1063    
1064     if (!(mg = mg_find (sv, PERL_MAGIC_sv)))
1065     return;
1066    
1067     if (mg->mg_virtual != &PL_vtbl_sv)
1068     return;
1069    
1070     vtbl_errno = PL_vtbl_sv;
1071     vtbl_errno.svt_get = errno_get;
1072     mg->mg_virtual = &vtbl_errno;
1073     }
1074    
1075 root 1.42 #if __GNUC__ >= 4
1076     # define noinline __attribute__ ((noinline))
1077     #else
1078     # define noinline
1079     #endif
1080    
1081     static noinline SV *
1082 root 1.41 pop_callback (I32 *ritems, SV *sv)
1083     {
1084     if (SvROK (sv))
1085     {
1086     HV *st;
1087     GV *gvp;
1088 root 1.42 CV *cv;
1089     const char *name;
1090    
1091     /* forgive me */
1092     if (SvTYPE (SvRV (sv)) == SVt_PVMG
1093     && (st = SvSTASH (SvRV (sv)))
1094     && (name = HvNAME_get (st))
1095     && (name [0] == 'B' && name [1] == 'D' && name [2] == 'B' && name [3] == ':'))
1096     return 0;
1097 root 1.41
1098 root 1.42 if ((cv = sv_2cv (sv, &st, &gvp, 0)))
1099 root 1.41 {
1100     --*ritems;
1101     return (SV *)cv;
1102     }
1103     }
1104    
1105     return 0;
1106     }
1107    
1108 root 1.47 /* stupid windoes defined CALLBACK as well */
1109     #undef CALLBACK
1110 root 1.41 #define CALLBACK SV *cb = pop_callback (&items, ST (items - 1));
1111    
1112 root 1.2 MODULE = BDB PACKAGE = BDB
1113 root 1.1
1114     PROTOTYPES: ENABLE
1115    
1116     BOOT:
1117     {
1118 root 1.2 HV *stash = gv_stashpv ("BDB", 1);
1119    
1120     static const struct {
1121     const char *name;
1122     IV iv;
1123     } *civ, const_iv[] = {
1124     #define const_iv(name) { # name, (IV)DB_ ## name },
1125     const_iv (RPCCLIENT)
1126     const_iv (INIT_CDB)
1127     const_iv (INIT_LOCK)
1128     const_iv (INIT_LOG)
1129     const_iv (INIT_MPOOL)
1130     const_iv (INIT_REP)
1131     const_iv (INIT_TXN)
1132     const_iv (RECOVER)
1133     const_iv (INIT_TXN)
1134     const_iv (RECOVER_FATAL)
1135     const_iv (CREATE)
1136 root 1.20 const_iv (RDONLY)
1137 root 1.2 const_iv (USE_ENVIRON)
1138     const_iv (USE_ENVIRON_ROOT)
1139     const_iv (LOCKDOWN)
1140     const_iv (PRIVATE)
1141     const_iv (SYSTEM_MEM)
1142     const_iv (AUTO_COMMIT)
1143     const_iv (CDB_ALLDB)
1144     const_iv (DIRECT_DB)
1145     const_iv (NOLOCKING)
1146     const_iv (NOMMAP)
1147     const_iv (NOPANIC)
1148     const_iv (OVERWRITE)
1149     const_iv (PANIC_ENVIRONMENT)
1150     const_iv (REGION_INIT)
1151     const_iv (TIME_NOTGRANTED)
1152     const_iv (TXN_NOSYNC)
1153 root 1.20 const_iv (TXN_NOT_DURABLE)
1154 root 1.2 const_iv (TXN_WRITE_NOSYNC)
1155 root 1.5 const_iv (WRITECURSOR)
1156 root 1.2 const_iv (YIELDCPU)
1157     const_iv (ENCRYPT_AES)
1158     const_iv (XA_CREATE)
1159     const_iv (BTREE)
1160     const_iv (HASH)
1161     const_iv (QUEUE)
1162     const_iv (RECNO)
1163     const_iv (UNKNOWN)
1164     const_iv (EXCL)
1165     const_iv (TRUNCATE)
1166     const_iv (NOSYNC)
1167     const_iv (CHKSUM)
1168     const_iv (ENCRYPT)
1169     const_iv (DUP)
1170     const_iv (DUPSORT)
1171     const_iv (RECNUM)
1172     const_iv (RENUMBER)
1173     const_iv (REVSPLITOFF)
1174     const_iv (CONSUME)
1175     const_iv (CONSUME_WAIT)
1176 root 1.5 const_iv (GET_BOTH)
1177 root 1.6 const_iv (GET_BOTH_RANGE)
1178 root 1.5 //const_iv (SET_RECNO)
1179     //const_iv (MULTIPLE)
1180 root 1.2 const_iv (SNAPSHOT)
1181     const_iv (JOIN_ITEM)
1182 root 1.27 const_iv (JOIN_NOSORT)
1183 root 1.2 const_iv (RMW)
1184    
1185     const_iv (NOTFOUND)
1186     const_iv (KEYEMPTY)
1187     const_iv (LOCK_DEADLOCK)
1188     const_iv (LOCK_NOTGRANTED)
1189     const_iv (RUNRECOVERY)
1190 root 1.3 const_iv (OLD_VERSION)
1191     const_iv (REP_HANDLE_DEAD)
1192 root 1.6 const_iv (SECONDARY_BAD)
1193 root 1.3
1194     const_iv (APPEND)
1195     const_iv (NODUPDATA)
1196     const_iv (NOOVERWRITE)
1197    
1198 root 1.4 const_iv (TXN_NOWAIT)
1199     const_iv (TXN_SYNC)
1200    
1201 root 1.3 const_iv (SET_LOCK_TIMEOUT)
1202     const_iv (SET_TXN_TIMEOUT)
1203 root 1.6
1204     const_iv (FIRST)
1205     const_iv (NEXT)
1206     const_iv (NEXT_DUP)
1207     const_iv (NEXT_NODUP)
1208     const_iv (PREV)
1209     const_iv (PREV_NODUP)
1210     const_iv (SET)
1211     const_iv (SET_RANGE)
1212     const_iv (LAST)
1213     const_iv (BEFORE)
1214     const_iv (AFTER)
1215     const_iv (CURRENT)
1216     const_iv (KEYFIRST)
1217     const_iv (KEYLAST)
1218     const_iv (NODUPDATA)
1219 root 1.8
1220     const_iv (FORCE)
1221    
1222     const_iv (LOCK_DEFAULT)
1223     const_iv (LOCK_EXPIRE)
1224     const_iv (LOCK_MAXLOCKS)
1225     const_iv (LOCK_MINLOCKS)
1226     const_iv (LOCK_MINWRITE)
1227     const_iv (LOCK_OLDEST)
1228     const_iv (LOCK_RANDOM)
1229     const_iv (LOCK_YOUNGEST)
1230    
1231 root 1.14 const_iv (DONOTINDEX)
1232     const_iv (KEYEMPTY )
1233     const_iv (KEYEXIST )
1234     const_iv (LOCK_DEADLOCK)
1235     const_iv (LOCK_NOTGRANTED)
1236     const_iv (NOSERVER)
1237     const_iv (NOSERVER_HOME)
1238     const_iv (NOSERVER_ID)
1239     const_iv (NOTFOUND)
1240     const_iv (PAGE_NOTFOUND)
1241     const_iv (REP_DUPMASTER)
1242     const_iv (REP_HANDLE_DEAD)
1243     const_iv (REP_HOLDELECTION)
1244     const_iv (REP_ISPERM)
1245     const_iv (REP_NEWMASTER)
1246     const_iv (REP_NEWSITE)
1247     const_iv (REP_NOTPERM)
1248     const_iv (REP_UNAVAIL)
1249     const_iv (RUNRECOVERY)
1250     const_iv (SECONDARY_BAD)
1251     const_iv (VERIFY_BAD)
1252    
1253     const_iv (VERB_DEADLOCK)
1254     const_iv (VERB_RECOVERY)
1255     const_iv (VERB_REPLICATION)
1256     const_iv (VERB_WAITSFOR)
1257    
1258     const_iv (VERSION_MAJOR)
1259     const_iv (VERSION_MINOR)
1260     const_iv (VERSION_PATCH)
1261 root 1.47 #if DB_VERSION_MINOR >= 3
1262     const_iv (INORDER)
1263     const_iv (LOCK_MAXWRITE)
1264     const_iv (SEQ_DEC)
1265     const_iv (SEQ_INC)
1266     const_iv (SEQ_WRAP)
1267     const_iv (BUFFER_SMALL)
1268     const_iv (LOG_BUFFER_FULL)
1269     const_iv (VERSION_MISMATCH)
1270     #endif
1271     #if DB_VERSION_MINOR >= 4
1272     const_iv (REGISTER)
1273     const_iv (DSYNC_DB)
1274     const_iv (READ_COMMITTED)
1275     const_iv (READ_UNCOMMITTED)
1276     const_iv (REP_IGNORE)
1277     const_iv (REP_LOCKOUT)
1278     const_iv (REP_JOIN_FAILURE)
1279     const_iv (FREE_SPACE)
1280     const_iv (FREELIST_ONLY)
1281     const_iv (VERB_REGISTER)
1282     #endif
1283 root 1.13 #if DB_VERSION_MINOR >= 5
1284     const_iv (MULTIVERSION)
1285     const_iv (TXN_SNAPSHOT)
1286     #endif
1287 root 1.15 #if DB_VERSION_MINOR >= 6
1288 root 1.16 const_iv (PREV_DUP)
1289     const_iv (PRIORITY_UNCHANGED)
1290     const_iv (PRIORITY_VERY_LOW)
1291     const_iv (PRIORITY_LOW)
1292     const_iv (PRIORITY_DEFAULT)
1293     const_iv (PRIORITY_HIGH)
1294     const_iv (PRIORITY_VERY_HIGH)
1295 root 1.15 #endif
1296 root 1.39 #if DB_VERSION_MINOR >= 7
1297     const_iv (LOG_DIRECT)
1298     const_iv (LOG_DSYNC)
1299     const_iv (LOG_AUTO_REMOVE)
1300     const_iv (LOG_IN_MEMORY)
1301     const_iv (LOG_ZERO)
1302     #else
1303     const_iv (DIRECT_LOG)
1304 root 1.47 const_iv (LOG_AUTOREMOVE)
1305     # if DB_VERSION_MINOR >= 3
1306 root 1.39 const_iv (DSYNC_LOG)
1307     const_iv (LOG_INMEMORY)
1308 root 1.47 # endif
1309 root 1.39 #endif
1310 root 1.2 };
1311    
1312     for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
1313     newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
1314 root 1.1
1315 root 1.40 {
1316     /* we currently only allow version, minor-version and patchlevel to go up to 255 */
1317     char vstring[3] = { DB_VERSION_MAJOR, DB_VERSION_MINOR, DB_VERSION_PATCH };
1318    
1319     newCONSTSUB (stash, "VERSION_v", newSVpvn (vstring, 3));
1320     }
1321    
1322 root 1.24 newCONSTSUB (stash, "VERSION_STRING", newSVpv (DB_VERSION_STRING, 0));
1323 root 1.14
1324 root 1.19 create_respipe ();
1325 root 1.12
1326     X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child);
1327 root 1.31 patch_errno ();
1328 root 1.1 }
1329    
1330     void
1331     max_poll_reqs (int nreqs)
1332     PROTOTYPE: $
1333     CODE:
1334     max_poll_reqs = nreqs;
1335    
1336     void
1337     max_poll_time (double nseconds)
1338     PROTOTYPE: $
1339     CODE:
1340     max_poll_time = nseconds * AIO_TICKS;
1341    
1342     void
1343     min_parallel (int nthreads)
1344     PROTOTYPE: $
1345    
1346     void
1347     max_parallel (int nthreads)
1348     PROTOTYPE: $
1349    
1350     void
1351     max_idle (int nthreads)
1352     PROTOTYPE: $
1353     CODE:
1354     set_max_idle (nthreads);
1355    
1356     int
1357     max_outstanding (int maxreqs)
1358     PROTOTYPE: $
1359     CODE:
1360     RETVAL = max_outstanding;
1361     max_outstanding = maxreqs;
1362     OUTPUT:
1363     RETVAL
1364    
1365     int
1366 root 1.3 dbreq_pri (int pri = 0)
1367 root 1.1 PROTOTYPE: ;$
1368     CODE:
1369     RETVAL = next_pri - PRI_BIAS;
1370     if (items > 0)
1371     {
1372     if (pri < PRI_MIN) pri = PRI_MIN;
1373     if (pri > PRI_MAX) pri = PRI_MAX;
1374     next_pri = pri + PRI_BIAS;
1375     }
1376     OUTPUT:
1377     RETVAL
1378    
1379     void
1380 root 1.3 dbreq_nice (int nice = 0)
1381 root 1.1 CODE:
1382     nice = next_pri - nice;
1383     if (nice < PRI_MIN) nice = PRI_MIN;
1384     if (nice > PRI_MAX) nice = PRI_MAX;
1385     next_pri = nice + PRI_BIAS;
1386    
1387     void
1388     flush ()
1389     PROTOTYPE:
1390     CODE:
1391     while (nreqs)
1392     {
1393     poll_wait ();
1394     poll_cb ();
1395     }
1396    
1397     int
1398 root 1.3 poll ()
1399 root 1.1 PROTOTYPE:
1400     CODE:
1401     poll_wait ();
1402     RETVAL = poll_cb ();
1403     OUTPUT:
1404     RETVAL
1405    
1406     int
1407 root 1.3 poll_fileno ()
1408 root 1.1 PROTOTYPE:
1409     CODE:
1410     RETVAL = respipe [0];
1411     OUTPUT:
1412     RETVAL
1413    
1414     int
1415 root 1.3 poll_cb (...)
1416 root 1.1 PROTOTYPE:
1417     CODE:
1418     RETVAL = poll_cb ();
1419     OUTPUT:
1420     RETVAL
1421    
1422     void
1423 root 1.3 poll_wait ()
1424 root 1.1 PROTOTYPE:
1425     CODE:
1426     poll_wait ();
1427    
1428     int
1429 root 1.3 nreqs ()
1430 root 1.1 PROTOTYPE:
1431     CODE:
1432     RETVAL = nreqs;
1433     OUTPUT:
1434     RETVAL
1435    
1436     int
1437 root 1.3 nready ()
1438 root 1.1 PROTOTYPE:
1439     CODE:
1440     RETVAL = get_nready ();
1441     OUTPUT:
1442     RETVAL
1443    
1444     int
1445 root 1.3 npending ()
1446 root 1.1 PROTOTYPE:
1447     CODE:
1448     RETVAL = get_npending ();
1449     OUTPUT:
1450     RETVAL
1451    
1452     int
1453 root 1.3 nthreads ()
1454 root 1.1 PROTOTYPE:
1455     CODE:
1456 root 1.12 if (WORDACCESS_UNSAFE) X_LOCK (wrklock);
1457 root 1.1 RETVAL = started;
1458 root 1.12 if (WORDACCESS_UNSAFE) X_UNLOCK (wrklock);
1459 root 1.1 OUTPUT:
1460     RETVAL
1461    
1462 root 1.3 void
1463     set_sync_prepare (SV *cb)
1464     PROTOTYPE: &
1465     CODE:
1466     SvREFCNT_dec (prepare_cb);
1467     prepare_cb = newSVsv (cb);
1468    
1469 root 1.14 char *
1470     strerror (int errorno = errno)
1471     PROTOTYPE: ;$
1472     CODE:
1473     RETVAL = db_strerror (errorno);
1474     OUTPUT:
1475     RETVAL
1476 root 1.8
1477 root 1.37 void _on_next_submit (SV *cb)
1478     CODE:
1479     SvREFCNT_dec (on_next_submit);
1480     on_next_submit = SvOK (cb) ? newSVsv (cb) : 0;
1481    
1482 root 1.2 DB_ENV *
1483 root 1.3 db_env_create (U32 env_flags = 0)
1484 root 1.2 CODE:
1485     {
1486 root 1.3 errno = db_env_create (&RETVAL, env_flags);
1487     if (errno)
1488     croak ("db_env_create: %s", db_strerror (errno));
1489 root 1.14
1490     if (0)
1491     {
1492     RETVAL->set_errcall (RETVAL, debug_errcall);
1493     RETVAL->set_msgcall (RETVAL, debug_msgcall);
1494     }
1495 root 1.2 }
1496 root 1.3 OUTPUT:
1497     RETVAL
1498 root 1.2
1499     void
1500 root 1.41 db_env_open (DB_ENV *env, bdb_filename db_home, U32 open_flags, int mode, SV *callback = 0)
1501     PREINIT:
1502     CALLBACK
1503 root 1.2 CODE:
1504     {
1505 root 1.13 dREQ (REQ_ENV_OPEN);
1506 root 1.2 req->env = env;
1507 root 1.3 req->uint1 = open_flags | DB_THREAD;
1508 root 1.2 req->int1 = mode;
1509 root 1.3 req->buf1 = strdup_ornull (db_home);
1510 root 1.2 REQ_SEND;
1511     }
1512    
1513     void
1514 root 1.41 db_env_close (DB_ENV *env, U32 flags = 0, SV *callback = 0)
1515     PREINIT:
1516     CALLBACK
1517 root 1.2 CODE:
1518     {
1519     dREQ (REQ_ENV_CLOSE);
1520     req->env = env;
1521     req->uint1 = flags;
1522     REQ_SEND;
1523 root 1.5 ptr_nuke (ST (0));
1524 root 1.2 }
1525    
1526 root 1.8 void
1527 root 1.41 db_env_txn_checkpoint (DB_ENV *env, U32 kbyte = 0, U32 min = 0, U32 flags = 0, SV *callback = 0)
1528     PREINIT:
1529     CALLBACK
1530 root 1.8 CODE:
1531     {
1532     dREQ (REQ_ENV_TXN_CHECKPOINT);
1533     req->env = env;
1534     req->uint1 = kbyte;
1535     req->int1 = min;
1536     req->uint2 = flags;
1537     REQ_SEND;
1538     }
1539    
1540     void
1541 root 1.41 db_env_lock_detect (DB_ENV *env, U32 flags = 0, U32 atype = DB_LOCK_DEFAULT, SV *dummy = 0, SV *callback = 0)
1542     PREINIT:
1543     CALLBACK
1544 root 1.8 CODE:
1545     {
1546     dREQ (REQ_ENV_LOCK_DETECT);
1547     req->env = env;
1548     req->uint1 = flags;
1549     req->uint2 = atype;
1550 root 1.41 /* req->int2 = 0; dummy */
1551 root 1.8 REQ_SEND;
1552     }
1553    
1554     void
1555 root 1.41 db_env_memp_sync (DB_ENV *env, SV *dummy = 0, SV *callback = 0)
1556     PREINIT:
1557     CALLBACK
1558 root 1.8 CODE:
1559     {
1560     dREQ (REQ_ENV_MEMP_SYNC);
1561     req->env = env;
1562     REQ_SEND;
1563     }
1564    
1565     void
1566 root 1.41 db_env_memp_trickle (DB_ENV *env, int percent, SV *dummy = 0, SV *callback = 0)
1567     PREINIT:
1568     CALLBACK
1569 root 1.8 CODE:
1570     {
1571     dREQ (REQ_ENV_MEMP_TRICKLE);
1572     req->env = env;
1573     req->int1 = percent;
1574     REQ_SEND;
1575     }
1576    
1577 root 1.39 void
1578 root 1.41 db_env_dbremove (DB_ENV *env, DB_TXN_ornull *txnid, bdb_filename file, bdb_filename database, U32 flags = 0, SV *callback = 0)
1579     PREINIT:
1580     CALLBACK
1581 root 1.39 CODE:
1582     {
1583     dREQ (REQ_ENV_DBREMOVE);
1584     req->env = env;
1585     req->buf1 = strdup_ornull (file);
1586     req->buf2 = strdup_ornull (database);
1587     req->uint1 = flags;
1588     REQ_SEND;
1589     }
1590    
1591     void
1592 root 1.41 db_env_dbrename (DB_ENV *env, DB_TXN_ornull *txnid, bdb_filename file, bdb_filename database, bdb_filename newname, U32 flags = 0, SV *callback = 0)
1593     PREINIT:
1594     CALLBACK
1595 root 1.39 CODE:
1596     {
1597     dREQ (REQ_ENV_DBRENAME);
1598     req->env = env;
1599     req->buf1 = strdup_ornull (file);
1600     req->buf2 = strdup_ornull (database);
1601     req->buf3 = strdup_ornull (newname);
1602     req->uint1 = flags;
1603     REQ_SEND;
1604     }
1605 root 1.8
1606 root 1.2 DB *
1607 root 1.3 db_create (DB_ENV *env = 0, U32 flags = 0)
1608 root 1.2 CODE:
1609     {
1610 root 1.3 errno = db_create (&RETVAL, env, flags);
1611     if (errno)
1612 root 1.6 croak ("db_create: %s", db_strerror (errno));
1613 root 1.5
1614     if (RETVAL)
1615     RETVAL->app_private = (void *)newSVsv (ST (0));
1616 root 1.2 }
1617 root 1.3 OUTPUT:
1618     RETVAL
1619 root 1.2
1620     void
1621 root 1.41 db_open (DB *db, DB_TXN_ornull *txnid, bdb_filename file, bdb_filename database, int type, U32 flags, int mode, SV *callback = 0)
1622     PREINIT:
1623     CALLBACK
1624 root 1.2 CODE:
1625     {
1626     dREQ (REQ_DB_OPEN);
1627     req->db = db;
1628     req->txn = txnid;
1629 root 1.3 req->buf1 = strdup_ornull (file);
1630     req->buf2 = strdup_ornull (database);
1631 root 1.2 req->int1 = type;
1632 root 1.3 req->uint1 = flags | DB_THREAD;
1633 root 1.2 req->int2 = mode;
1634     REQ_SEND;
1635     }
1636    
1637     void
1638 root 1.41 db_close (DB *db, U32 flags = 0, SV *callback = 0)
1639     PREINIT:
1640     CALLBACK
1641 root 1.2 CODE:
1642     {
1643     dREQ (REQ_DB_CLOSE);
1644     req->db = db;
1645     req->uint1 = flags;
1646 root 1.5 req->sv1 = (SV *)db->app_private;
1647 root 1.2 REQ_SEND;
1648 root 1.5 ptr_nuke (ST (0));
1649 root 1.2 }
1650    
1651 root 1.47 #if DB_VERSION_MINOR >= 4
1652    
1653 root 1.3 void
1654 root 1.41 db_compact (DB *db, DB_TXN_ornull *txn = 0, SV *start = 0, SV *stop = 0, SV *unused1 = 0, U32 flags = DB_FREE_SPACE, SV *unused2 = 0, SV *callback = 0)
1655     PREINIT:
1656     CALLBACK
1657 root 1.3 CODE:
1658     {
1659     dREQ (REQ_DB_COMPACT);
1660     req->db = db;
1661     req->txn = txn;
1662 root 1.5 sv_to_dbt (&req->dbt1, start);
1663     sv_to_dbt (&req->dbt2, stop);
1664 root 1.3 req->uint1 = flags;
1665     REQ_SEND;
1666     }
1667    
1668 root 1.47 #endif
1669    
1670 root 1.3 void
1671 root 1.41 db_sync (DB *db, U32 flags = 0, SV *callback = 0)
1672     PREINIT:
1673     CALLBACK
1674 root 1.3 CODE:
1675     {
1676     dREQ (REQ_DB_SYNC);
1677     req->db = db;
1678     req->uint1 = flags;
1679     REQ_SEND;
1680     }
1681    
1682     void
1683 root 1.41 db_upgrade (DB *db, bdb_filename file, U32 flags = 0, SV *callback = 0)
1684     PREINIT:
1685     CALLBACK
1686 root 1.26 CODE:
1687     {
1688     dREQ (REQ_DB_SYNC);
1689     req->db = db;
1690     req->buf1 = strdup (file);
1691     req->uint1 = flags;
1692     REQ_SEND;
1693     }
1694    
1695     void
1696 root 1.41 db_key_range (DB *db, DB_TXN_ornull *txn, SV *key, SV *key_range, U32 flags = 0, SV *callback = 0)
1697     PREINIT:
1698     CALLBACK
1699 root 1.8 CODE:
1700     {
1701     dREQ (REQ_DB_KEY_RANGE);
1702     req->db = db;
1703     req->txn = txn;
1704     sv_to_dbt (&req->dbt1, key);
1705     req->uint1 = flags;
1706     req->sv1 = SvREFCNT_inc (key_range); SvREADONLY_on (key_range);
1707     REQ_SEND;
1708     }
1709    
1710     void
1711 root 1.41 db_put (DB *db, DB_TXN_ornull *txn, SV *key, SV *data, U32 flags = 0, SV *callback = 0)
1712     PREINIT:
1713     CALLBACK
1714 root 1.3 CODE:
1715     {
1716     dREQ (REQ_DB_PUT);
1717     req->db = db;
1718 root 1.5 req->txn = txn;
1719     sv_to_dbt (&req->dbt1, key);
1720     sv_to_dbt (&req->dbt2, data);
1721 root 1.3 req->uint1 = flags;
1722     REQ_SEND;
1723     }
1724    
1725 root 1.46 #if DB_VERSION_MINOR >= 6
1726    
1727 root 1.5 void
1728 root 1.45 db_exists (DB *db, DB_TXN_ornull *txn, SV *key, U32 flags = 0, SV *callback = 0)
1729     PREINIT:
1730     CALLBACK
1731     CODE:
1732     {
1733     dREQ (REQ_DB_EXISTS);
1734     req->db = db;
1735     req->txn = txn;
1736     req->uint1 = flags;
1737     sv_to_dbt (&req->dbt1, key);
1738     REQ_SEND;
1739     }
1740    
1741 root 1.46 #endif
1742    
1743 root 1.45 void
1744 root 1.41 db_get (DB *db, DB_TXN_ornull *txn, SV *key, SV *data, U32 flags = 0, SV *callback = 0)
1745     PREINIT:
1746     CALLBACK
1747 root 1.5 CODE:
1748 root 1.27 if (SvREADONLY (data))
1749     croak ("can't modify read-only data scalar in db_get");
1750 root 1.28 {
1751 root 1.5 dREQ (REQ_DB_GET);
1752     req->db = db;
1753     req->txn = txn;
1754 root 1.6 req->uint1 = flags;
1755 root 1.5 sv_to_dbt (&req->dbt1, key);
1756     req->dbt3.flags = DB_DBT_MALLOC;
1757 root 1.6 req->sv3 = SvREFCNT_inc (data); SvREADONLY_on (data);
1758 root 1.5 REQ_SEND;
1759     }
1760    
1761     void
1762 root 1.41 db_pget (DB *db, DB_TXN_ornull *txn, SV *key, SV *pkey, SV *data, U32 flags = 0, SV *callback = 0)
1763     PREINIT:
1764     CALLBACK
1765 root 1.5 CODE:
1766 root 1.27 if (SvREADONLY (data))
1767     croak ("can't modify read-only data scalar in db_pget");
1768 root 1.28 {
1769 root 1.5 dREQ (REQ_DB_PGET);
1770     req->db = db;
1771     req->txn = txn;
1772 root 1.6 req->uint1 = flags;
1773 root 1.5 sv_to_dbt (&req->dbt1, key);
1774     sv_to_dbt (&req->dbt2, pkey);
1775     req->dbt3.flags = DB_DBT_MALLOC;
1776 root 1.6 req->sv3 = SvREFCNT_inc (data); SvREADONLY_on (data);
1777     REQ_SEND;
1778     }
1779    
1780     void
1781 root 1.41 db_del (DB *db, DB_TXN_ornull *txn, SV *key, U32 flags = 0, SV *callback = 0)
1782     PREINIT:
1783     CALLBACK
1784 root 1.6 CODE:
1785     {
1786     dREQ (REQ_DB_DEL);
1787     req->db = db;
1788     req->txn = txn;
1789 root 1.5 req->uint1 = flags;
1790 root 1.6 sv_to_dbt (&req->dbt1, key);
1791 root 1.5 REQ_SEND;
1792     }
1793    
1794     void
1795 root 1.41 db_txn_commit (DB_TXN *txn, U32 flags = 0, SV *callback = 0)
1796     PREINIT:
1797     CALLBACK
1798 root 1.5 CODE:
1799     {
1800     dREQ (REQ_TXN_COMMIT);
1801     req->txn = txn;
1802     req->uint1 = flags;
1803     REQ_SEND;
1804     ptr_nuke (ST (0));
1805     }
1806    
1807     void
1808 root 1.41 db_txn_abort (DB_TXN *txn, SV *callback = 0)
1809     PREINIT:
1810     CALLBACK
1811 root 1.5 CODE:
1812     {
1813     dREQ (REQ_TXN_ABORT);
1814     req->txn = txn;
1815     REQ_SEND;
1816     ptr_nuke (ST (0));
1817     }
1818    
1819 root 1.6 void
1820 root 1.41 db_txn_finish (DB_TXN *txn, U32 flags = 0, SV *callback = 0)
1821     PREINIT:
1822     CALLBACK
1823 root 1.17 CODE:
1824     {
1825     dREQ (REQ_TXN_FINISH);
1826     req->txn = txn;
1827     req->uint1 = flags;
1828     REQ_SEND;
1829     ptr_nuke (ST (0));
1830     }
1831    
1832     void
1833 root 1.41 db_c_close (DBC *dbc, SV *callback = 0)
1834     PREINIT:
1835     CALLBACK
1836 root 1.6 CODE:
1837     {
1838     dREQ (REQ_C_CLOSE);
1839     req->dbc = dbc;
1840     REQ_SEND;
1841     ptr_nuke (ST (0));
1842     }
1843    
1844     void
1845 root 1.41 db_c_count (DBC *dbc, SV *count, U32 flags = 0, SV *callback = 0)
1846     PREINIT:
1847     CALLBACK
1848 root 1.6 CODE:
1849     {
1850     dREQ (REQ_C_COUNT);
1851     req->dbc = dbc;
1852     req->sv1 = SvREFCNT_inc (count);
1853     REQ_SEND;
1854     }
1855    
1856     void
1857 root 1.41 db_c_put (DBC *dbc, SV *key, SV *data, U32 flags = 0, SV *callback = 0)
1858     PREINIT:
1859     CALLBACK
1860 root 1.6 CODE:
1861     {
1862     dREQ (REQ_C_PUT);
1863     req->dbc = dbc;
1864     sv_to_dbt (&req->dbt1, key);
1865     sv_to_dbt (&req->dbt2, data);
1866     req->uint1 = flags;
1867     REQ_SEND;
1868     }
1869    
1870     void
1871 root 1.41 db_c_get (DBC *dbc, SV *key, SV *data, U32 flags = 0, SV *callback = 0)
1872     PREINIT:
1873     CALLBACK
1874 root 1.6 CODE:
1875     {
1876     dREQ (REQ_C_GET);
1877     req->dbc = dbc;
1878     req->uint1 = flags;
1879     if ((flags & DB_SET) == DB_SET
1880     || (flags & DB_SET_RANGE) == DB_SET_RANGE)
1881     sv_to_dbt (&req->dbt1, key);
1882     else
1883     req->dbt1.flags = DB_DBT_MALLOC;
1884    
1885     req->sv1 = SvREFCNT_inc (key); SvREADONLY_on (key);
1886    
1887     if ((flags & DB_GET_BOTH) == DB_GET_BOTH
1888     || (flags & DB_GET_BOTH_RANGE) == DB_GET_BOTH_RANGE)
1889     sv_to_dbt (&req->dbt3, data);
1890     else
1891     req->dbt3.flags = DB_DBT_MALLOC;
1892    
1893     req->sv3 = SvREFCNT_inc (data); SvREADONLY_on (data);
1894     REQ_SEND;
1895     }
1896    
1897     void
1898 root 1.41 db_c_pget (DBC *dbc, SV *key, SV *pkey, SV *data, U32 flags = 0, SV *callback = 0)
1899     PREINIT:
1900     CALLBACK
1901 root 1.6 CODE:
1902     {
1903     dREQ (REQ_C_PGET);
1904     req->dbc = dbc;
1905     req->uint1 = flags;
1906     if ((flags & DB_SET) == DB_SET
1907     || (flags & DB_SET_RANGE) == DB_SET_RANGE)
1908     sv_to_dbt (&req->dbt1, key);
1909     else
1910     req->dbt1.flags = DB_DBT_MALLOC;
1911    
1912     req->sv1 = SvREFCNT_inc (key); SvREADONLY_on (key);
1913    
1914     req->dbt2.flags = DB_DBT_MALLOC;
1915     req->sv2 = SvREFCNT_inc (pkey); SvREADONLY_on (pkey);
1916    
1917     if ((flags & DB_GET_BOTH) == DB_GET_BOTH
1918     || (flags & DB_GET_BOTH_RANGE) == DB_GET_BOTH_RANGE)
1919     sv_to_dbt (&req->dbt3, data);
1920     else
1921     req->dbt3.flags = DB_DBT_MALLOC;
1922    
1923     req->sv3 = SvREFCNT_inc (data); SvREADONLY_on (data);
1924     REQ_SEND;
1925     }
1926    
1927     void
1928 root 1.41 db_c_del (DBC *dbc, U32 flags = 0, SV *callback = 0)
1929     PREINIT:
1930     CALLBACK
1931 root 1.6 CODE:
1932     {
1933     dREQ (REQ_C_DEL);
1934     req->dbc = dbc;
1935     req->uint1 = flags;
1936     REQ_SEND;
1937     }
1938    
1939 root 1.2
1940 root 1.47 #if DB_VERSION_MINOR >= 3
1941    
1942 root 1.8 void
1943 root 1.41 db_sequence_open (DB_SEQUENCE *seq, DB_TXN_ornull *txnid, SV *key, U32 flags = 0, SV *callback = 0)
1944     PREINIT:
1945     CALLBACK
1946 root 1.8 CODE:
1947     {
1948     dREQ (REQ_SEQ_OPEN);
1949     req->seq = seq;
1950     req->txn = txnid;
1951     req->uint1 = flags | DB_THREAD;
1952     sv_to_dbt (&req->dbt1, key);
1953     REQ_SEND;
1954     }
1955    
1956     void
1957 root 1.41 db_sequence_close (DB_SEQUENCE *seq, U32 flags = 0, SV *callback = 0)
1958     PREINIT:
1959     CALLBACK
1960 root 1.8 CODE:
1961     {
1962     dREQ (REQ_SEQ_CLOSE);
1963     req->seq = seq;
1964     req->uint1 = flags;
1965     REQ_SEND;
1966     ptr_nuke (ST (0));
1967     }
1968    
1969     void
1970 root 1.41 db_sequence_get (DB_SEQUENCE *seq, DB_TXN_ornull *txnid, int delta, SV *seq_value, U32 flags = DB_TXN_NOSYNC, SV *callback = 0)
1971     PREINIT:
1972     CALLBACK
1973 root 1.8 CODE:
1974     {
1975     dREQ (REQ_SEQ_GET);
1976     req->seq = seq;
1977     req->txn = txnid;
1978     req->int1 = delta;
1979     req->uint1 = flags;
1980     req->sv1 = SvREFCNT_inc (seq_value); SvREADONLY_on (seq_value);
1981     REQ_SEND;
1982     }
1983    
1984     void
1985 root 1.41 db_sequence_remove (DB_SEQUENCE *seq, DB_TXN_ornull *txnid = 0, U32 flags = 0, SV *callback = 0)
1986     PREINIT:
1987     CALLBACK
1988 root 1.8 CODE:
1989     {
1990     dREQ (REQ_SEQ_REMOVE);
1991     req->seq = seq;
1992     req->txn = txnid;
1993     req->uint1 = flags;
1994     REQ_SEND;
1995     }
1996    
1997 root 1.47 #endif
1998    
1999 root 1.8
2000 root 1.2 MODULE = BDB PACKAGE = BDB::Env
2001    
2002 root 1.5 void
2003 root 1.30 DESTROY (DB_ENV_ornuked *env)
2004 root 1.5 CODE:
2005     if (env)
2006     env->close (env, 0);
2007    
2008 root 1.8 int set_data_dir (DB_ENV *env, const char *dir)
2009     CODE:
2010     RETVAL = env->set_data_dir (env, dir);
2011     OUTPUT:
2012     RETVAL
2013    
2014     int set_tmp_dir (DB_ENV *env, const char *dir)
2015     CODE:
2016     RETVAL = env->set_tmp_dir (env, dir);
2017     OUTPUT:
2018     RETVAL
2019    
2020     int set_lg_dir (DB_ENV *env, const char *dir)
2021     CODE:
2022     RETVAL = env->set_lg_dir (env, dir);
2023     OUTPUT:
2024     RETVAL
2025    
2026     int set_shm_key (DB_ENV *env, long shm_key)
2027     CODE:
2028     RETVAL = env->set_shm_key (env, shm_key);
2029     OUTPUT:
2030     RETVAL
2031    
2032 root 1.2 int set_cachesize (DB_ENV *env, U32 gbytes, U32 bytes, int ncache = 0)
2033 root 1.3 CODE:
2034     RETVAL = env->set_cachesize (env, gbytes, bytes, ncache);
2035     OUTPUT:
2036     RETVAL
2037 root 1.2
2038 root 1.25 int set_flags (DB_ENV *env, U32 flags, int onoff = 1)
2039 root 1.3 CODE:
2040     RETVAL = env->set_flags (env, flags, onoff);
2041     OUTPUT:
2042     RETVAL
2043    
2044 root 1.39 #if DB_VERSION_MINOR >= 7
2045    
2046     int set_intermediate_dir_mode (DB_ENV *env, const char *mode)
2047     CODE:
2048     RETVAL = env->set_intermediate_dir_mode (env, mode);
2049     OUTPUT:
2050     RETVAL
2051    
2052     int log_set_config (DB_ENV *env, U32 flags, int onoff = 1)
2053     CODE:
2054     RETVAL = env->log_set_config (env, flags, onoff);
2055     OUTPUT:
2056     RETVAL
2057    
2058     #endif
2059    
2060    
2061 root 1.16 void set_errfile (DB_ENV *env, FILE *errfile = 0)
2062 root 1.14 CODE:
2063     env->set_errfile (env, errfile);
2064    
2065 root 1.16 void set_msgfile (DB_ENV *env, FILE *msgfile = 0)
2066 root 1.14 CODE:
2067     env->set_msgfile (env, msgfile);
2068    
2069 root 1.25 int set_verbose (DB_ENV *env, U32 which = -1, int onoff = 1)
2070 root 1.14 CODE:
2071     RETVAL = env->set_verbose (env, which, onoff);
2072     OUTPUT:
2073     RETVAL
2074    
2075 root 1.3 int set_encrypt (DB_ENV *env, const char *password, U32 flags = 0)
2076     CODE:
2077     RETVAL = env->set_encrypt (env, password, flags);
2078     OUTPUT:
2079     RETVAL
2080    
2081 root 1.17 int set_timeout (DB_ENV *env, NV timeout, U32 flags = DB_SET_TXN_TIMEOUT)
2082 root 1.3 CODE:
2083     RETVAL = env->set_timeout (env, timeout * 1000000, flags);
2084     OUTPUT:
2085     RETVAL
2086 root 1.2
2087 root 1.8 int set_mp_max_openfd (DB_ENV *env, int maxopenfd);
2088     CODE:
2089     RETVAL = env->set_mp_max_openfd (env, maxopenfd);
2090     OUTPUT:
2091     RETVAL
2092    
2093     int set_mp_max_write (DB_ENV *env, int maxwrite, int maxwrite_sleep);
2094     CODE:
2095     RETVAL = env->set_mp_max_write (env, maxwrite, maxwrite_sleep);
2096     OUTPUT:
2097     RETVAL
2098    
2099     int set_mp_mmapsize (DB_ENV *env, int mmapsize_mb)
2100     CODE:
2101     RETVAL = env->set_mp_mmapsize (env, ((size_t)mmapsize_mb) << 20);
2102     OUTPUT:
2103     RETVAL
2104    
2105 root 1.9 int set_lk_detect (DB_ENV *env, U32 detect = DB_LOCK_DEFAULT)
2106 root 1.8 CODE:
2107     RETVAL = env->set_lk_detect (env, detect);
2108     OUTPUT:
2109     RETVAL
2110    
2111     int set_lk_max_lockers (DB_ENV *env, U32 max)
2112     CODE:
2113     RETVAL = env->set_lk_max_lockers (env, max);
2114     OUTPUT:
2115     RETVAL
2116    
2117     int set_lk_max_locks (DB_ENV *env, U32 max)
2118     CODE:
2119     RETVAL = env->set_lk_max_locks (env, max);
2120     OUTPUT:
2121     RETVAL
2122    
2123     int set_lk_max_objects (DB_ENV *env, U32 max)
2124     CODE:
2125     RETVAL = env->set_lk_max_objects (env, max);
2126     OUTPUT:
2127     RETVAL
2128    
2129     int set_lg_bsize (DB_ENV *env, U32 max)
2130     CODE:
2131     RETVAL = env->set_lg_bsize (env, max);
2132     OUTPUT:
2133     RETVAL
2134    
2135     int set_lg_max (DB_ENV *env, U32 max)
2136     CODE:
2137     RETVAL = env->set_lg_max (env, max);
2138     OUTPUT:
2139     RETVAL
2140    
2141 root 1.47 #if DB_VERSION_MINOR >= 4
2142    
2143 root 1.22 int mutex_set_max (DB_ENV *env, U32 max)
2144     CODE:
2145     RETVAL = env->mutex_set_max (env, max);
2146     OUTPUT:
2147     RETVAL
2148    
2149     int mutex_set_increment (DB_ENV *env, U32 increment)
2150     CODE:
2151     RETVAL = env->mutex_set_increment (env, increment);
2152     OUTPUT:
2153     RETVAL
2154    
2155     int mutex_set_tas_spins (DB_ENV *env, U32 tas_spins)
2156     CODE:
2157     RETVAL = env->mutex_set_tas_spins (env, tas_spins);
2158     OUTPUT:
2159     RETVAL
2160    
2161     int mutex_set_align (DB_ENV *env, U32 align)
2162     CODE:
2163     RETVAL = env->mutex_set_align (env, align);
2164     OUTPUT:
2165     RETVAL
2166    
2167 root 1.47 #endif
2168    
2169 root 1.5 DB_TXN *
2170     txn_begin (DB_ENV *env, DB_TXN_ornull *parent = 0, U32 flags = 0)
2171     CODE:
2172     errno = env->txn_begin (env, parent, &RETVAL, flags);
2173     if (errno)
2174 root 1.6 croak ("DB_ENV->txn_begin: %s", db_strerror (errno));
2175 root 1.5 OUTPUT:
2176     RETVAL
2177 root 1.2
2178 root 1.46 #if DB_VERSION_MINOR >= 5
2179    
2180 root 1.44 DB_TXN *
2181     cdsgroup_begin (DB_ENV *env)
2182     CODE:
2183     errno = env->cdsgroup_begin (env, &RETVAL);
2184     if (errno)
2185     croak ("DB_ENV->cdsgroup_begin: %s", db_strerror (errno));
2186     OUTPUT:
2187     RETVAL
2188    
2189 root 1.46 #endif
2190    
2191 root 1.2 MODULE = BDB PACKAGE = BDB::Db
2192    
2193 root 1.5 void
2194 root 1.30 DESTROY (DB_ornuked *db)
2195 root 1.5 CODE:
2196     if (db)
2197     {
2198     SV *env = (SV *)db->app_private;
2199     db->close (db, 0);
2200     SvREFCNT_dec (env);
2201     }
2202    
2203 root 1.2 int set_cachesize (DB *db, U32 gbytes, U32 bytes, int ncache = 0)
2204 root 1.3 CODE:
2205     RETVAL = db->set_cachesize (db, gbytes, bytes, ncache);
2206     OUTPUT:
2207     RETVAL
2208 root 1.2
2209 root 1.16 int set_flags (DB *db, U32 flags)
2210 root 1.3 CODE:
2211     RETVAL = db->set_flags (db, flags);
2212     OUTPUT:
2213     RETVAL
2214 root 1.2
2215     int set_encrypt (DB *db, const char *password, U32 flags)
2216 root 1.3 CODE:
2217     RETVAL = db->set_encrypt (db, password, flags);
2218     OUTPUT:
2219     RETVAL
2220 root 1.2
2221     int set_lorder (DB *db, int lorder)
2222 root 1.3 CODE:
2223     RETVAL = db->set_lorder (db, lorder);
2224     OUTPUT:
2225     RETVAL
2226 root 1.2
2227     int set_bt_minkey (DB *db, U32 minkey)
2228 root 1.3 CODE:
2229     RETVAL = db->set_bt_minkey (db, minkey);
2230     OUTPUT:
2231     RETVAL
2232 root 1.2
2233 root 1.16 int set_re_delim (DB *db, int delim)
2234 root 1.3 CODE:
2235     RETVAL = db->set_re_delim (db, delim);
2236     OUTPUT:
2237     RETVAL
2238 root 1.2
2239     int set_re_pad (DB *db, int re_pad)
2240 root 1.3 CODE:
2241     RETVAL = db->set_re_pad (db, re_pad);
2242     OUTPUT:
2243     RETVAL
2244 root 1.2
2245     int set_re_source (DB *db, char *source)
2246 root 1.3 CODE:
2247     RETVAL = db->set_re_source (db, source);
2248     OUTPUT:
2249     RETVAL
2250 root 1.2
2251     int set_re_len (DB *db, U32 re_len)
2252 root 1.3 CODE:
2253     RETVAL = db->set_re_len (db, re_len);
2254     OUTPUT:
2255     RETVAL
2256 root 1.2
2257     int set_h_ffactor (DB *db, U32 h_ffactor)
2258 root 1.3 CODE:
2259     RETVAL = db->set_h_ffactor (db, h_ffactor);
2260     OUTPUT:
2261     RETVAL
2262 root 1.2
2263     int set_h_nelem (DB *db, U32 h_nelem)
2264 root 1.3 CODE:
2265     RETVAL = db->set_h_nelem (db, h_nelem);
2266     OUTPUT:
2267     RETVAL
2268 root 1.2
2269     int set_q_extentsize (DB *db, U32 extentsize)
2270 root 1.3 CODE:
2271     RETVAL = db->set_q_extentsize (db, extentsize);
2272     OUTPUT:
2273     RETVAL
2274 root 1.2
2275 root 1.6 DBC *
2276     cursor (DB *db, DB_TXN_ornull *txn = 0, U32 flags = 0)
2277     CODE:
2278     errno = db->cursor (db, txn, &RETVAL, flags);
2279     if (errno)
2280     croak ("DB->cursor: %s", db_strerror (errno));
2281     OUTPUT:
2282     RETVAL
2283    
2284 root 1.47 #if DB_VERSION_MINOR >= 3
2285    
2286 root 1.8 DB_SEQUENCE *
2287     sequence (DB *db, U32 flags = 0)
2288     CODE:
2289     {
2290     errno = db_sequence_create (&RETVAL, db, flags);
2291     if (errno)
2292     croak ("db_sequence_create: %s", db_strerror (errno));
2293     }
2294     OUTPUT:
2295     RETVAL
2296    
2297 root 1.47 #endif
2298    
2299 root 1.6
2300 root 1.5 MODULE = BDB PACKAGE = BDB::Txn
2301    
2302     void
2303 root 1.30 DESTROY (DB_TXN_ornuked *txn)
2304 root 1.5 CODE:
2305     if (txn)
2306     txn->abort (txn);
2307    
2308 root 1.17 int set_timeout (DB_TXN *txn, NV timeout, U32 flags = DB_SET_TXN_TIMEOUT)
2309 root 1.5 CODE:
2310     RETVAL = txn->set_timeout (txn, timeout * 1000000, flags);
2311     OUTPUT:
2312     RETVAL
2313    
2314 root 1.17 int failed (DB_TXN *txn)
2315     CODE:
2316     RETVAL = !!(txn->flags & TXN_DEADLOCK);
2317     OUTPUT:
2318     RETVAL
2319    
2320 root 1.1
2321 root 1.6 MODULE = BDB PACKAGE = BDB::Cursor
2322    
2323     void
2324 root 1.30 DESTROY (DBC_ornuked *dbc)
2325 root 1.6 CODE:
2326     if (dbc)
2327     dbc->c_close (dbc);
2328    
2329 root 1.27 #if DB_VERSION_MINOR >= 6
2330    
2331     int set_priority (DBC *dbc, int priority)
2332     CODE:
2333     dbc->set_priority (dbc, priority);
2334    
2335     #endif
2336    
2337 root 1.47 #if DB_VERSION_MINOR >= 3
2338    
2339 root 1.8 MODULE = BDB PACKAGE = BDB::Sequence
2340    
2341     void
2342 root 1.30 DESTROY (DB_SEQUENCE_ornuked *seq)
2343 root 1.8 CODE:
2344     if (seq)
2345     seq->close (seq, 0);
2346    
2347     int initial_value (DB_SEQUENCE *seq, db_seq_t value)
2348     CODE:
2349     RETVAL = seq->initial_value (seq, value);
2350     OUTPUT:
2351     RETVAL
2352    
2353     int set_cachesize (DB_SEQUENCE *seq, U32 size)
2354     CODE:
2355     RETVAL = seq->set_cachesize (seq, size);
2356     OUTPUT:
2357     RETVAL
2358    
2359     int set_flags (DB_SEQUENCE *seq, U32 flags)
2360     CODE:
2361     RETVAL = seq->set_flags (seq, flags);
2362     OUTPUT:
2363     RETVAL
2364    
2365     int set_range (DB_SEQUENCE *seq, db_seq_t min, db_seq_t max)
2366     CODE:
2367     RETVAL = seq->set_range (seq, min, max);
2368     OUTPUT:
2369     RETVAL
2370    
2371 root 1.47 #endif
2372    
2373 root 1.31