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