ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/BDB/BDB.xs
Revision: 1.50
Committed: Thu Sep 25 13:22:32 2008 UTC (15 years, 7 months ago) by root
Branch: MAIN
Changes since 1.49: +43 -35 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.13 X_THREAD_PROC (bdb_proc)
721 root 1.1 {
722 root 1.28 bdb_req req;
723 root 1.1 struct timespec ts;
724     worker *self = (worker *)thr_arg;
725    
726     /* try to distribute timeouts somewhat evenly */
727 root 1.13 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
728 root 1.1
729     for (;;)
730     {
731     ts.tv_sec = time (0) + IDLE_TIMEOUT;
732    
733 root 1.12 X_LOCK (reqlock);
734 root 1.1
735     for (;;)
736     {
737     self->req = req = reqq_shift (&req_queue);
738    
739     if (req)
740     break;
741    
742     ++idle;
743    
744 root 1.12 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts)
745 root 1.1 == ETIMEDOUT)
746     {
747     if (idle > max_idle)
748     {
749     --idle;
750 root 1.12 X_UNLOCK (reqlock);
751     X_LOCK (wrklock);
752 root 1.1 --started;
753 root 1.12 X_UNLOCK (wrklock);
754 root 1.1 goto quit;
755     }
756    
757     /* we are allowed to idle, so do so without any timeout */
758 root 1.12 X_COND_WAIT (reqwait, reqlock);
759 root 1.1 ts.tv_sec = time (0) + IDLE_TIMEOUT;
760     }
761    
762     --idle;
763     }
764    
765     --nready;
766    
767 root 1.12 X_UNLOCK (reqlock);
768 root 1.49
769 root 1.1 switch (req->type)
770     {
771     case REQ_QUIT:
772 root 1.17 req->result = ENOSYS;
773 root 1.1 goto quit;
774    
775 root 1.3 case REQ_ENV_OPEN:
776     req->result = req->env->open (req->env, req->buf1, req->uint1, req->int1);
777     break;
778    
779     case REQ_ENV_CLOSE:
780     req->result = req->env->close (req->env, req->uint1);
781     break;
782    
783 root 1.8 case REQ_ENV_TXN_CHECKPOINT:
784     req->result = req->env->txn_checkpoint (req->env, req->uint1, req->int1, req->uint2);
785     break;
786    
787     case REQ_ENV_LOCK_DETECT:
788     req->result = req->env->lock_detect (req->env, req->uint1, req->uint2, &req->int1);
789     break;
790    
791     case REQ_ENV_MEMP_SYNC:
792     req->result = req->env->memp_sync (req->env, 0);
793     break;
794    
795     case REQ_ENV_MEMP_TRICKLE:
796     req->result = req->env->memp_trickle (req->env, req->int1, &req->int2);
797     break;
798    
799 root 1.39 case REQ_ENV_DBREMOVE:
800     req->result = req->env->dbremove (req->env, req->txn, req->buf1, req->buf2, req->uint1);
801     break;
802    
803     case REQ_ENV_DBRENAME:
804     req->result = req->env->dbrename (req->env, req->txn, req->buf1, req->buf2, req->buf3, req->uint1);
805     break;
806    
807 root 1.3 case REQ_DB_OPEN:
808     req->result = req->db->open (req->db, req->txn, req->buf1, req->buf2, req->int1, req->uint1, req->int2);
809     break;
810    
811     case REQ_DB_CLOSE:
812     req->result = req->db->close (req->db, req->uint1);
813     break;
814    
815 root 1.47 #if DB_VERSION_MINOR >= 4
816 root 1.3 case REQ_DB_COMPACT:
817     req->result = req->db->compact (req->db, req->txn, &req->dbt1, &req->dbt2, 0, req->uint1, 0);
818     break;
819 root 1.47 #endif
820 root 1.3
821     case REQ_DB_SYNC:
822     req->result = req->db->sync (req->db, req->uint1);
823     break;
824    
825 root 1.26 case REQ_DB_UPGRADE:
826     req->result = req->db->upgrade (req->db, req->buf1, req->uint1);
827     break;
828    
829 root 1.3 case REQ_DB_PUT:
830     req->result = req->db->put (req->db, req->txn, &req->dbt1, &req->dbt2, req->uint1);
831     break;
832    
833 root 1.46 #if DB_VERSION_MINOR >= 6
834 root 1.45 case REQ_DB_EXISTS:
835     req->result = req->db->exists (req->db, req->txn, &req->dbt1, req->uint1);
836     break;
837 root 1.46 #endif
838 root 1.5 case REQ_DB_GET:
839     req->result = req->db->get (req->db, req->txn, &req->dbt1, &req->dbt3, req->uint1);
840     break;
841    
842     case REQ_DB_PGET:
843     req->result = req->db->pget (req->db, req->txn, &req->dbt1, &req->dbt2, &req->dbt3, req->uint1);
844     break;
845    
846 root 1.6 case REQ_DB_DEL:
847     req->result = req->db->del (req->db, req->txn, &req->dbt1, req->uint1);
848     break;
849    
850 root 1.8 case REQ_DB_KEY_RANGE:
851     req->result = req->db->key_range (req->db, req->txn, &req->dbt1, &req->key_range, req->uint1);
852     break;
853    
854 root 1.5 case REQ_TXN_COMMIT:
855     req->result = req->txn->commit (req->txn, req->uint1);
856     break;
857    
858     case REQ_TXN_ABORT:
859     req->result = req->txn->abort (req->txn);
860     break;
861    
862 root 1.17 case REQ_TXN_FINISH:
863     if (req->txn->flags & TXN_DEADLOCK)
864     {
865     req->result = req->txn->abort (req->txn);
866     if (!req->result)
867     req->result = DB_LOCK_DEADLOCK;
868     }
869 root 1.18 else
870     req->result = req->txn->commit (req->txn, req->uint1);
871 root 1.17 break;
872    
873 root 1.6 case REQ_C_CLOSE:
874     req->result = req->dbc->c_close (req->dbc);
875     break;
876    
877     case REQ_C_COUNT:
878     {
879     db_recno_t recno;
880     req->result = req->dbc->c_count (req->dbc, &recno, req->uint1);
881     req->uv1 = recno;
882     }
883     break;
884    
885     case REQ_C_PUT:
886     req->result = req->dbc->c_put (req->dbc, &req->dbt1, &req->dbt2, req->uint1);
887     break;
888    
889     case REQ_C_GET:
890     req->result = req->dbc->c_get (req->dbc, &req->dbt1, &req->dbt3, req->uint1);
891     break;
892    
893     case REQ_C_PGET:
894     req->result = req->dbc->c_pget (req->dbc, &req->dbt1, &req->dbt2, &req->dbt3, req->uint1);
895     break;
896    
897     case REQ_C_DEL:
898     req->result = req->dbc->c_del (req->dbc, req->uint1);
899     break;
900    
901 root 1.47 #if DB_VERSION_MINOR >= 3
902 root 1.8 case REQ_SEQ_OPEN:
903     req->result = req->seq->open (req->seq, req->txn, &req->dbt1, req->uint1);
904     break;
905    
906     case REQ_SEQ_CLOSE:
907     req->result = req->seq->close (req->seq, req->uint1);
908     break;
909    
910     case REQ_SEQ_GET:
911     req->result = req->seq->get (req->seq, req->txn, req->int1, &req->seq_t, req->uint1);
912     break;
913    
914     case REQ_SEQ_REMOVE:
915     req->result = req->seq->remove (req->seq, req->txn, req->uint1);
916     break;
917 root 1.47 #endif
918 root 1.8
919 root 1.1 default:
920 root 1.3 req->result = ENOSYS;
921 root 1.1 break;
922     }
923    
924 root 1.17 if (req->txn && (req->result > 0 || req->result == DB_LOCK_NOTGRANTED))
925     req->txn->flags |= TXN_DEADLOCK;
926    
927 root 1.12 X_LOCK (reslock);
928 root 1.1
929     ++npending;
930    
931     if (!reqq_push (&res_queue, req))
932     /* write a dummy byte to the pipe so fh becomes ready */
933 root 1.13 respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1);
934 root 1.1
935     self->req = 0;
936     worker_clear (self);
937    
938 root 1.12 X_UNLOCK (reslock);
939 root 1.1 }
940    
941     quit:
942 root 1.12 X_LOCK (wrklock);
943 root 1.1 worker_free (self);
944 root 1.12 X_UNLOCK (wrklock);
945 root 1.1
946     return 0;
947     }
948    
949     /*****************************************************************************/
950    
951     static void atfork_prepare (void)
952     {
953 root 1.12 X_LOCK (wrklock);
954     X_LOCK (reqlock);
955     X_LOCK (reslock);
956 root 1.1 }
957    
958     static void atfork_parent (void)
959     {
960 root 1.12 X_UNLOCK (reslock);
961     X_UNLOCK (reqlock);
962     X_UNLOCK (wrklock);
963 root 1.1 }
964    
965     static void atfork_child (void)
966     {
967 root 1.28 bdb_req prv;
968 root 1.1
969     while (prv = reqq_shift (&req_queue))
970     req_free (prv);
971    
972     while (prv = reqq_shift (&res_queue))
973     req_free (prv);
974    
975     while (wrk_first.next != &wrk_first)
976     {
977     worker *wrk = wrk_first.next;
978    
979     if (wrk->req)
980     req_free (wrk->req);
981    
982     worker_clear (wrk);
983     worker_free (wrk);
984     }
985    
986     started = 0;
987     idle = 0;
988     nreqs = 0;
989     nready = 0;
990     npending = 0;
991    
992 root 1.19 create_respipe ();
993 root 1.1
994     atfork_parent ();
995     }
996    
997 root 1.50 #define dREQ(reqtype,rsvcnt) \
998 root 1.28 bdb_req req; \
999 root 1.1 int req_pri = next_pri; \
1000     next_pri = DEFAULT_PRI + PRI_BIAS; \
1001     \
1002 root 1.41 if (callback && SvOK (callback)) \
1003     croak ("callback has illegal type or extra arguments"); \
1004 root 1.1 \
1005 root 1.28 Newz (0, req, 1, bdb_cb); \
1006 root 1.1 if (!req) \
1007 root 1.28 croak ("out of memory during bdb_req allocation"); \
1008 root 1.1 \
1009 root 1.50 req->callback = SvREFCNT_inc (cb); \
1010 root 1.2 req->type = (reqtype); \
1011 root 1.50 req->pri = req_pri; \
1012     if (rsvcnt >= 1) req->rsv1 = SvREFCNT_inc (ST (0)); \
1013     if (rsvcnt >= 2) req->rsv2 = SvREFCNT_inc (ST (1)); \
1014     (void)0;
1015 root 1.1
1016     #define REQ_SEND \
1017 root 1.2 req_send (req)
1018    
1019 root 1.30 #define SvPTR(var, arg, type, class, nullok) \
1020 root 1.5 if (!SvOK (arg)) \
1021     { \
1022 root 1.30 if (nullok != 1) \
1023 root 1.7 croak (# var " must be a " # class " object, not undef"); \
1024 root 1.5 \
1025     (var) = 0; \
1026     } \
1027     else if (sv_derived_from ((arg), # class)) \
1028     { \
1029     IV tmp = SvIV ((SV*) SvRV (arg)); \
1030     (var) = INT2PTR (type, tmp); \
1031 root 1.30 if (!var && nullok != 2) \
1032 root 1.7 croak (# var " is not a valid " # class " object anymore"); \
1033 root 1.5 } \
1034     else \
1035 root 1.42 croak (# var " is not of type " # class);
1036 root 1.3
1037 root 1.5 static void
1038     ptr_nuke (SV *sv)
1039 root 1.3 {
1040 root 1.5 assert (SvROK (sv));
1041     sv_setiv (SvRV (sv), 0);
1042     }
1043 root 1.3
1044 root 1.31 static int
1045     errno_get (pTHX_ SV *sv, MAGIC *mg)
1046     {
1047     if (*mg->mg_ptr == '!') // should always be the case
1048     if (-30999 <= errno && errno <= -30800)
1049     {
1050 root 1.32 sv_setnv (sv, (NV)errno);
1051 root 1.31 sv_setpv (sv, db_strerror (errno));
1052 root 1.32 SvNOK_on (sv); /* what a wonderful hack! */
1053     // ^^^ copied from perl sources
1054 root 1.31 return 0;
1055     }
1056    
1057     return PL_vtbl_sv.svt_get (aTHX_ sv, mg);
1058     }
1059    
1060     static MGVTBL vtbl_errno;
1061    
1062     // this wonderful hack :( patches perl's $! variable to support our errno values
1063     static void
1064     patch_errno (void)
1065     {
1066     SV *sv;
1067     MAGIC *mg;
1068    
1069     if (!(sv = get_sv ("!", 1)))
1070     return;
1071    
1072     if (!(mg = mg_find (sv, PERL_MAGIC_sv)))
1073     return;
1074    
1075     if (mg->mg_virtual != &PL_vtbl_sv)
1076     return;
1077    
1078     vtbl_errno = PL_vtbl_sv;
1079     vtbl_errno.svt_get = errno_get;
1080     mg->mg_virtual = &vtbl_errno;
1081     }
1082    
1083 root 1.42 #if __GNUC__ >= 4
1084     # define noinline __attribute__ ((noinline))
1085     #else
1086     # define noinline
1087     #endif
1088    
1089     static noinline SV *
1090 root 1.41 pop_callback (I32 *ritems, SV *sv)
1091     {
1092     if (SvROK (sv))
1093     {
1094     HV *st;
1095     GV *gvp;
1096 root 1.42 CV *cv;
1097     const char *name;
1098    
1099     /* forgive me */
1100     if (SvTYPE (SvRV (sv)) == SVt_PVMG
1101     && (st = SvSTASH (SvRV (sv)))
1102     && (name = HvNAME_get (st))
1103     && (name [0] == 'B' && name [1] == 'D' && name [2] == 'B' && name [3] == ':'))
1104     return 0;
1105 root 1.41
1106 root 1.42 if ((cv = sv_2cv (sv, &st, &gvp, 0)))
1107 root 1.41 {
1108     --*ritems;
1109     return (SV *)cv;
1110     }
1111     }
1112    
1113     return 0;
1114     }
1115    
1116 root 1.47 /* stupid windoes defined CALLBACK as well */
1117     #undef CALLBACK
1118 root 1.41 #define CALLBACK SV *cb = pop_callback (&items, ST (items - 1));
1119    
1120 root 1.2 MODULE = BDB PACKAGE = BDB
1121 root 1.1
1122     PROTOTYPES: ENABLE
1123    
1124     BOOT:
1125     {
1126 root 1.2 HV *stash = gv_stashpv ("BDB", 1);
1127    
1128     static const struct {
1129     const char *name;
1130     IV iv;
1131     } *civ, const_iv[] = {
1132     #define const_iv(name) { # name, (IV)DB_ ## name },
1133     const_iv (RPCCLIENT)
1134     const_iv (INIT_CDB)
1135     const_iv (INIT_LOCK)
1136     const_iv (INIT_LOG)
1137     const_iv (INIT_MPOOL)
1138     const_iv (INIT_REP)
1139     const_iv (INIT_TXN)
1140     const_iv (RECOVER)
1141     const_iv (INIT_TXN)
1142     const_iv (RECOVER_FATAL)
1143     const_iv (CREATE)
1144 root 1.20 const_iv (RDONLY)
1145 root 1.2 const_iv (USE_ENVIRON)
1146     const_iv (USE_ENVIRON_ROOT)
1147     const_iv (LOCKDOWN)
1148     const_iv (PRIVATE)
1149     const_iv (SYSTEM_MEM)
1150     const_iv (AUTO_COMMIT)
1151     const_iv (CDB_ALLDB)
1152     const_iv (DIRECT_DB)
1153     const_iv (NOLOCKING)
1154     const_iv (NOMMAP)
1155     const_iv (NOPANIC)
1156     const_iv (OVERWRITE)
1157     const_iv (PANIC_ENVIRONMENT)
1158     const_iv (REGION_INIT)
1159     const_iv (TIME_NOTGRANTED)
1160     const_iv (TXN_NOSYNC)
1161 root 1.20 const_iv (TXN_NOT_DURABLE)
1162 root 1.2 const_iv (TXN_WRITE_NOSYNC)
1163 root 1.5 const_iv (WRITECURSOR)
1164 root 1.2 const_iv (YIELDCPU)
1165     const_iv (ENCRYPT_AES)
1166     const_iv (XA_CREATE)
1167     const_iv (BTREE)
1168     const_iv (HASH)
1169     const_iv (QUEUE)
1170     const_iv (RECNO)
1171     const_iv (UNKNOWN)
1172     const_iv (EXCL)
1173     const_iv (TRUNCATE)
1174     const_iv (NOSYNC)
1175     const_iv (CHKSUM)
1176     const_iv (ENCRYPT)
1177     const_iv (DUP)
1178     const_iv (DUPSORT)
1179     const_iv (RECNUM)
1180     const_iv (RENUMBER)
1181     const_iv (REVSPLITOFF)
1182     const_iv (CONSUME)
1183     const_iv (CONSUME_WAIT)
1184 root 1.5 const_iv (GET_BOTH)
1185 root 1.6 const_iv (GET_BOTH_RANGE)
1186 root 1.5 //const_iv (SET_RECNO)
1187     //const_iv (MULTIPLE)
1188 root 1.2 const_iv (SNAPSHOT)
1189     const_iv (JOIN_ITEM)
1190 root 1.27 const_iv (JOIN_NOSORT)
1191 root 1.2 const_iv (RMW)
1192    
1193     const_iv (NOTFOUND)
1194     const_iv (KEYEMPTY)
1195     const_iv (LOCK_DEADLOCK)
1196     const_iv (LOCK_NOTGRANTED)
1197     const_iv (RUNRECOVERY)
1198 root 1.3 const_iv (OLD_VERSION)
1199     const_iv (REP_HANDLE_DEAD)
1200 root 1.6 const_iv (SECONDARY_BAD)
1201 root 1.3
1202     const_iv (APPEND)
1203     const_iv (NODUPDATA)
1204     const_iv (NOOVERWRITE)
1205    
1206 root 1.4 const_iv (TXN_NOWAIT)
1207     const_iv (TXN_SYNC)
1208    
1209 root 1.3 const_iv (SET_LOCK_TIMEOUT)
1210     const_iv (SET_TXN_TIMEOUT)
1211 root 1.6
1212     const_iv (FIRST)
1213     const_iv (NEXT)
1214     const_iv (NEXT_DUP)
1215     const_iv (NEXT_NODUP)
1216     const_iv (PREV)
1217     const_iv (PREV_NODUP)
1218     const_iv (SET)
1219     const_iv (SET_RANGE)
1220     const_iv (LAST)
1221     const_iv (BEFORE)
1222     const_iv (AFTER)
1223     const_iv (CURRENT)
1224     const_iv (KEYFIRST)
1225     const_iv (KEYLAST)
1226     const_iv (NODUPDATA)
1227 root 1.8
1228     const_iv (FORCE)
1229    
1230     const_iv (LOCK_DEFAULT)
1231     const_iv (LOCK_EXPIRE)
1232     const_iv (LOCK_MAXLOCKS)
1233     const_iv (LOCK_MINLOCKS)
1234     const_iv (LOCK_MINWRITE)
1235     const_iv (LOCK_OLDEST)
1236     const_iv (LOCK_RANDOM)
1237     const_iv (LOCK_YOUNGEST)
1238    
1239 root 1.14 const_iv (DONOTINDEX)
1240     const_iv (KEYEMPTY )
1241     const_iv (KEYEXIST )
1242     const_iv (LOCK_DEADLOCK)
1243     const_iv (LOCK_NOTGRANTED)
1244     const_iv (NOSERVER)
1245     const_iv (NOSERVER_HOME)
1246     const_iv (NOSERVER_ID)
1247     const_iv (NOTFOUND)
1248     const_iv (PAGE_NOTFOUND)
1249     const_iv (REP_DUPMASTER)
1250     const_iv (REP_HANDLE_DEAD)
1251     const_iv (REP_HOLDELECTION)
1252     const_iv (REP_ISPERM)
1253     const_iv (REP_NEWMASTER)
1254     const_iv (REP_NEWSITE)
1255     const_iv (REP_NOTPERM)
1256     const_iv (REP_UNAVAIL)
1257     const_iv (RUNRECOVERY)
1258     const_iv (SECONDARY_BAD)
1259     const_iv (VERIFY_BAD)
1260    
1261     const_iv (VERB_DEADLOCK)
1262     const_iv (VERB_RECOVERY)
1263     const_iv (VERB_REPLICATION)
1264     const_iv (VERB_WAITSFOR)
1265    
1266     const_iv (VERSION_MAJOR)
1267     const_iv (VERSION_MINOR)
1268     const_iv (VERSION_PATCH)
1269 root 1.47 #if DB_VERSION_MINOR >= 3
1270     const_iv (INORDER)
1271     const_iv (LOCK_MAXWRITE)
1272     const_iv (SEQ_DEC)
1273     const_iv (SEQ_INC)
1274     const_iv (SEQ_WRAP)
1275     const_iv (BUFFER_SMALL)
1276     const_iv (LOG_BUFFER_FULL)
1277     const_iv (VERSION_MISMATCH)
1278     #endif
1279     #if DB_VERSION_MINOR >= 4
1280     const_iv (REGISTER)
1281     const_iv (DSYNC_DB)
1282     const_iv (READ_COMMITTED)
1283     const_iv (READ_UNCOMMITTED)
1284     const_iv (REP_IGNORE)
1285     const_iv (REP_LOCKOUT)
1286     const_iv (REP_JOIN_FAILURE)
1287     const_iv (FREE_SPACE)
1288     const_iv (FREELIST_ONLY)
1289     const_iv (VERB_REGISTER)
1290     #endif
1291 root 1.13 #if DB_VERSION_MINOR >= 5
1292     const_iv (MULTIVERSION)
1293     const_iv (TXN_SNAPSHOT)
1294     #endif
1295 root 1.15 #if DB_VERSION_MINOR >= 6
1296 root 1.16 const_iv (PREV_DUP)
1297     const_iv (PRIORITY_UNCHANGED)
1298     const_iv (PRIORITY_VERY_LOW)
1299     const_iv (PRIORITY_LOW)
1300     const_iv (PRIORITY_DEFAULT)
1301     const_iv (PRIORITY_HIGH)
1302     const_iv (PRIORITY_VERY_HIGH)
1303 root 1.15 #endif
1304 root 1.39 #if DB_VERSION_MINOR >= 7
1305     const_iv (LOG_DIRECT)
1306     const_iv (LOG_DSYNC)
1307     const_iv (LOG_AUTO_REMOVE)
1308     const_iv (LOG_IN_MEMORY)
1309     const_iv (LOG_ZERO)
1310     #else
1311     const_iv (DIRECT_LOG)
1312 root 1.47 const_iv (LOG_AUTOREMOVE)
1313     # if DB_VERSION_MINOR >= 3
1314 root 1.39 const_iv (DSYNC_LOG)
1315     const_iv (LOG_INMEMORY)
1316 root 1.47 # endif
1317 root 1.39 #endif
1318 root 1.2 };
1319    
1320     for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
1321     newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
1322 root 1.1
1323 root 1.40 {
1324     /* we currently only allow version, minor-version and patchlevel to go up to 255 */
1325     char vstring[3] = { DB_VERSION_MAJOR, DB_VERSION_MINOR, DB_VERSION_PATCH };
1326    
1327     newCONSTSUB (stash, "VERSION_v", newSVpvn (vstring, 3));
1328     }
1329    
1330 root 1.24 newCONSTSUB (stash, "VERSION_STRING", newSVpv (DB_VERSION_STRING, 0));
1331 root 1.14
1332 root 1.19 create_respipe ();
1333 root 1.12
1334     X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child);
1335 root 1.31 patch_errno ();
1336 root 1.1 }
1337    
1338     void
1339     max_poll_reqs (int nreqs)
1340     PROTOTYPE: $
1341     CODE:
1342     max_poll_reqs = nreqs;
1343    
1344     void
1345     max_poll_time (double nseconds)
1346     PROTOTYPE: $
1347     CODE:
1348     max_poll_time = nseconds * AIO_TICKS;
1349    
1350     void
1351     min_parallel (int nthreads)
1352     PROTOTYPE: $
1353    
1354     void
1355     max_parallel (int nthreads)
1356     PROTOTYPE: $
1357    
1358     void
1359     max_idle (int nthreads)
1360     PROTOTYPE: $
1361     CODE:
1362     set_max_idle (nthreads);
1363    
1364     int
1365     max_outstanding (int maxreqs)
1366     PROTOTYPE: $
1367     CODE:
1368     RETVAL = max_outstanding;
1369     max_outstanding = maxreqs;
1370     OUTPUT:
1371     RETVAL
1372    
1373     int
1374 root 1.3 dbreq_pri (int pri = 0)
1375 root 1.1 PROTOTYPE: ;$
1376     CODE:
1377     RETVAL = next_pri - PRI_BIAS;
1378     if (items > 0)
1379     {
1380     if (pri < PRI_MIN) pri = PRI_MIN;
1381     if (pri > PRI_MAX) pri = PRI_MAX;
1382     next_pri = pri + PRI_BIAS;
1383     }
1384     OUTPUT:
1385     RETVAL
1386    
1387     void
1388 root 1.3 dbreq_nice (int nice = 0)
1389 root 1.1 CODE:
1390     nice = next_pri - nice;
1391     if (nice < PRI_MIN) nice = PRI_MIN;
1392     if (nice > PRI_MAX) nice = PRI_MAX;
1393     next_pri = nice + PRI_BIAS;
1394    
1395     void
1396     flush ()
1397     PROTOTYPE:
1398     CODE:
1399     while (nreqs)
1400     {
1401     poll_wait ();
1402     poll_cb ();
1403     }
1404    
1405     int
1406 root 1.3 poll ()
1407 root 1.1 PROTOTYPE:
1408     CODE:
1409     poll_wait ();
1410     RETVAL = poll_cb ();
1411     OUTPUT:
1412     RETVAL
1413    
1414     int
1415 root 1.3 poll_fileno ()
1416 root 1.1 PROTOTYPE:
1417     CODE:
1418     RETVAL = respipe [0];
1419     OUTPUT:
1420     RETVAL
1421    
1422     int
1423 root 1.3 poll_cb (...)
1424 root 1.1 PROTOTYPE:
1425     CODE:
1426     RETVAL = poll_cb ();
1427     OUTPUT:
1428     RETVAL
1429    
1430     void
1431 root 1.3 poll_wait ()
1432 root 1.1 PROTOTYPE:
1433     CODE:
1434     poll_wait ();
1435    
1436     int
1437 root 1.3 nreqs ()
1438 root 1.1 PROTOTYPE:
1439     CODE:
1440     RETVAL = nreqs;
1441     OUTPUT:
1442     RETVAL
1443    
1444     int
1445 root 1.3 nready ()
1446 root 1.1 PROTOTYPE:
1447     CODE:
1448     RETVAL = get_nready ();
1449     OUTPUT:
1450     RETVAL
1451    
1452     int
1453 root 1.3 npending ()
1454 root 1.1 PROTOTYPE:
1455     CODE:
1456     RETVAL = get_npending ();
1457     OUTPUT:
1458     RETVAL
1459    
1460     int
1461 root 1.3 nthreads ()
1462 root 1.1 PROTOTYPE:
1463     CODE:
1464 root 1.12 if (WORDACCESS_UNSAFE) X_LOCK (wrklock);
1465 root 1.1 RETVAL = started;
1466 root 1.12 if (WORDACCESS_UNSAFE) X_UNLOCK (wrklock);
1467 root 1.1 OUTPUT:
1468     RETVAL
1469    
1470 root 1.3 void
1471     set_sync_prepare (SV *cb)
1472     PROTOTYPE: &
1473     CODE:
1474     SvREFCNT_dec (prepare_cb);
1475     prepare_cb = newSVsv (cb);
1476    
1477 root 1.14 char *
1478     strerror (int errorno = errno)
1479     PROTOTYPE: ;$
1480     CODE:
1481     RETVAL = db_strerror (errorno);
1482     OUTPUT:
1483     RETVAL
1484 root 1.8
1485 root 1.37 void _on_next_submit (SV *cb)
1486     CODE:
1487     SvREFCNT_dec (on_next_submit);
1488     on_next_submit = SvOK (cb) ? newSVsv (cb) : 0;
1489    
1490 root 1.2 DB_ENV *
1491 root 1.3 db_env_create (U32 env_flags = 0)
1492 root 1.2 CODE:
1493     {
1494 root 1.3 errno = db_env_create (&RETVAL, env_flags);
1495     if (errno)
1496     croak ("db_env_create: %s", db_strerror (errno));
1497 root 1.14
1498     if (0)
1499     {
1500     RETVAL->set_errcall (RETVAL, debug_errcall);
1501     RETVAL->set_msgcall (RETVAL, debug_msgcall);
1502     }
1503 root 1.2 }
1504 root 1.3 OUTPUT:
1505     RETVAL
1506 root 1.2
1507     void
1508 root 1.41 db_env_open (DB_ENV *env, bdb_filename db_home, U32 open_flags, int mode, SV *callback = 0)
1509     PREINIT:
1510     CALLBACK
1511 root 1.2 CODE:
1512     {
1513 root 1.50 dREQ (REQ_ENV_OPEN, 1);
1514 root 1.2 req->env = env;
1515 root 1.3 req->uint1 = open_flags | DB_THREAD;
1516 root 1.2 req->int1 = mode;
1517 root 1.3 req->buf1 = strdup_ornull (db_home);
1518 root 1.2 REQ_SEND;
1519     }
1520    
1521     void
1522 root 1.41 db_env_close (DB_ENV *env, U32 flags = 0, SV *callback = 0)
1523     PREINIT:
1524     CALLBACK
1525 root 1.2 CODE:
1526     {
1527 root 1.50 dREQ (REQ_ENV_CLOSE, 1);
1528 root 1.2 req->env = env;
1529     req->uint1 = flags;
1530     REQ_SEND;
1531 root 1.5 ptr_nuke (ST (0));
1532 root 1.2 }
1533    
1534 root 1.8 void
1535 root 1.41 db_env_txn_checkpoint (DB_ENV *env, U32 kbyte = 0, U32 min = 0, U32 flags = 0, SV *callback = 0)
1536     PREINIT:
1537     CALLBACK
1538 root 1.8 CODE:
1539     {
1540 root 1.50 dREQ (REQ_ENV_TXN_CHECKPOINT, 1);
1541 root 1.8 req->env = env;
1542     req->uint1 = kbyte;
1543     req->int1 = min;
1544     req->uint2 = flags;
1545     REQ_SEND;
1546     }
1547    
1548     void
1549 root 1.41 db_env_lock_detect (DB_ENV *env, U32 flags = 0, U32 atype = DB_LOCK_DEFAULT, SV *dummy = 0, SV *callback = 0)
1550     PREINIT:
1551     CALLBACK
1552 root 1.8 CODE:
1553     {
1554 root 1.50 dREQ (REQ_ENV_LOCK_DETECT, 1);
1555 root 1.8 req->env = env;
1556     req->uint1 = flags;
1557     req->uint2 = atype;
1558 root 1.41 /* req->int2 = 0; dummy */
1559 root 1.8 REQ_SEND;
1560     }
1561    
1562     void
1563 root 1.41 db_env_memp_sync (DB_ENV *env, SV *dummy = 0, SV *callback = 0)
1564     PREINIT:
1565     CALLBACK
1566 root 1.8 CODE:
1567     {
1568 root 1.50 dREQ (REQ_ENV_MEMP_SYNC, 1);
1569 root 1.8 req->env = env;
1570     REQ_SEND;
1571     }
1572    
1573     void
1574 root 1.41 db_env_memp_trickle (DB_ENV *env, int percent, SV *dummy = 0, SV *callback = 0)
1575     PREINIT:
1576     CALLBACK
1577 root 1.8 CODE:
1578     {
1579 root 1.50 dREQ (REQ_ENV_MEMP_TRICKLE, 1);
1580 root 1.8 req->env = env;
1581     req->int1 = percent;
1582     REQ_SEND;
1583     }
1584    
1585 root 1.39 void
1586 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)
1587     PREINIT:
1588     CALLBACK
1589 root 1.39 CODE:
1590     {
1591 root 1.50 dREQ (REQ_ENV_DBREMOVE, 2);
1592 root 1.39 req->env = env;
1593     req->buf1 = strdup_ornull (file);
1594     req->buf2 = strdup_ornull (database);
1595     req->uint1 = flags;
1596     REQ_SEND;
1597     }
1598    
1599     void
1600 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)
1601     PREINIT:
1602     CALLBACK
1603 root 1.39 CODE:
1604     {
1605 root 1.50 dREQ (REQ_ENV_DBRENAME, 2);
1606 root 1.39 req->env = env;
1607     req->buf1 = strdup_ornull (file);
1608     req->buf2 = strdup_ornull (database);
1609     req->buf3 = strdup_ornull (newname);
1610     req->uint1 = flags;
1611     REQ_SEND;
1612     }
1613 root 1.8
1614 root 1.2 DB *
1615 root 1.3 db_create (DB_ENV *env = 0, U32 flags = 0)
1616 root 1.2 CODE:
1617     {
1618 root 1.3 errno = db_create (&RETVAL, env, flags);
1619     if (errno)
1620 root 1.6 croak ("db_create: %s", db_strerror (errno));
1621 root 1.5
1622     if (RETVAL)
1623     RETVAL->app_private = (void *)newSVsv (ST (0));
1624 root 1.2 }
1625 root 1.3 OUTPUT:
1626     RETVAL
1627 root 1.2
1628     void
1629 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)
1630     PREINIT:
1631     CALLBACK
1632 root 1.2 CODE:
1633     {
1634 root 1.50 dREQ (REQ_DB_OPEN, 2);
1635 root 1.2 req->db = db;
1636     req->txn = txnid;
1637 root 1.3 req->buf1 = strdup_ornull (file);
1638     req->buf2 = strdup_ornull (database);
1639 root 1.2 req->int1 = type;
1640 root 1.3 req->uint1 = flags | DB_THREAD;
1641 root 1.2 req->int2 = mode;
1642     REQ_SEND;
1643     }
1644    
1645     void
1646 root 1.41 db_close (DB *db, U32 flags = 0, SV *callback = 0)
1647     PREINIT:
1648     CALLBACK
1649 root 1.2 CODE:
1650     {
1651 root 1.50 dREQ (REQ_DB_CLOSE, 1);
1652 root 1.2 req->db = db;
1653     req->uint1 = flags;
1654 root 1.5 req->sv1 = (SV *)db->app_private;
1655 root 1.2 REQ_SEND;
1656 root 1.5 ptr_nuke (ST (0));
1657 root 1.2 }
1658    
1659 root 1.47 #if DB_VERSION_MINOR >= 4
1660    
1661 root 1.3 void
1662 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)
1663     PREINIT:
1664     CALLBACK
1665 root 1.3 CODE:
1666     {
1667 root 1.50 dREQ (REQ_DB_COMPACT, 2);
1668 root 1.3 req->db = db;
1669     req->txn = txn;
1670 root 1.5 sv_to_dbt (&req->dbt1, start);
1671     sv_to_dbt (&req->dbt2, stop);
1672 root 1.3 req->uint1 = flags;
1673     REQ_SEND;
1674     }
1675    
1676 root 1.47 #endif
1677    
1678 root 1.3 void
1679 root 1.41 db_sync (DB *db, U32 flags = 0, SV *callback = 0)
1680     PREINIT:
1681     CALLBACK
1682 root 1.3 CODE:
1683     {
1684 root 1.50 dREQ (REQ_DB_SYNC, 1);
1685 root 1.3 req->db = db;
1686     req->uint1 = flags;
1687     REQ_SEND;
1688     }
1689    
1690     void
1691 root 1.41 db_upgrade (DB *db, bdb_filename file, U32 flags = 0, SV *callback = 0)
1692     PREINIT:
1693     CALLBACK
1694 root 1.26 CODE:
1695     {
1696 root 1.50 dREQ (REQ_DB_SYNC, 1);
1697 root 1.26 req->db = db;
1698     req->buf1 = strdup (file);
1699     req->uint1 = flags;
1700     REQ_SEND;
1701     }
1702    
1703     void
1704 root 1.41 db_key_range (DB *db, DB_TXN_ornull *txn, SV *key, SV *key_range, U32 flags = 0, SV *callback = 0)
1705     PREINIT:
1706     CALLBACK
1707 root 1.8 CODE:
1708     {
1709 root 1.50 dREQ (REQ_DB_KEY_RANGE, 2);
1710 root 1.8 req->db = db;
1711     req->txn = txn;
1712     sv_to_dbt (&req->dbt1, key);
1713     req->uint1 = flags;
1714     req->sv1 = SvREFCNT_inc (key_range); SvREADONLY_on (key_range);
1715     REQ_SEND;
1716     }
1717    
1718     void
1719 root 1.41 db_put (DB *db, DB_TXN_ornull *txn, SV *key, SV *data, U32 flags = 0, SV *callback = 0)
1720     PREINIT:
1721     CALLBACK
1722 root 1.3 CODE:
1723     {
1724 root 1.50 dREQ (REQ_DB_PUT, 2);
1725 root 1.3 req->db = db;
1726 root 1.5 req->txn = txn;
1727     sv_to_dbt (&req->dbt1, key);
1728     sv_to_dbt (&req->dbt2, data);
1729 root 1.3 req->uint1 = flags;
1730     REQ_SEND;
1731     }
1732    
1733 root 1.46 #if DB_VERSION_MINOR >= 6
1734    
1735 root 1.5 void
1736 root 1.45 db_exists (DB *db, DB_TXN_ornull *txn, SV *key, U32 flags = 0, SV *callback = 0)
1737     PREINIT:
1738     CALLBACK
1739     CODE:
1740     {
1741 root 1.50 dREQ (REQ_DB_EXISTS, 2);
1742 root 1.45 req->db = db;
1743     req->txn = txn;
1744     req->uint1 = flags;
1745     sv_to_dbt (&req->dbt1, key);
1746     REQ_SEND;
1747     }
1748    
1749 root 1.46 #endif
1750    
1751 root 1.45 void
1752 root 1.41 db_get (DB *db, DB_TXN_ornull *txn, SV *key, SV *data, U32 flags = 0, SV *callback = 0)
1753     PREINIT:
1754     CALLBACK
1755 root 1.5 CODE:
1756 root 1.27 if (SvREADONLY (data))
1757     croak ("can't modify read-only data scalar in db_get");
1758 root 1.28 {
1759 root 1.50 dREQ (REQ_DB_GET, 2);
1760 root 1.5 req->db = db;
1761     req->txn = txn;
1762 root 1.6 req->uint1 = flags;
1763 root 1.5 sv_to_dbt (&req->dbt1, key);
1764     req->dbt3.flags = DB_DBT_MALLOC;
1765 root 1.6 req->sv3 = SvREFCNT_inc (data); SvREADONLY_on (data);
1766 root 1.5 REQ_SEND;
1767     }
1768    
1769     void
1770 root 1.41 db_pget (DB *db, DB_TXN_ornull *txn, SV *key, SV *pkey, SV *data, U32 flags = 0, SV *callback = 0)
1771     PREINIT:
1772     CALLBACK
1773 root 1.5 CODE:
1774 root 1.27 if (SvREADONLY (data))
1775     croak ("can't modify read-only data scalar in db_pget");
1776 root 1.28 {
1777 root 1.50 dREQ (REQ_DB_PGET, 2);
1778 root 1.5 req->db = db;
1779     req->txn = txn;
1780 root 1.6 req->uint1 = flags;
1781 root 1.5 sv_to_dbt (&req->dbt1, key);
1782     sv_to_dbt (&req->dbt2, pkey);
1783     req->dbt3.flags = DB_DBT_MALLOC;
1784 root 1.6 req->sv3 = SvREFCNT_inc (data); SvREADONLY_on (data);
1785     REQ_SEND;
1786     }
1787    
1788     void
1789 root 1.41 db_del (DB *db, DB_TXN_ornull *txn, SV *key, U32 flags = 0, SV *callback = 0)
1790     PREINIT:
1791     CALLBACK
1792 root 1.6 CODE:
1793     {
1794 root 1.50 dREQ (REQ_DB_DEL, 2);
1795 root 1.6 req->db = db;
1796     req->txn = txn;
1797 root 1.5 req->uint1 = flags;
1798 root 1.6 sv_to_dbt (&req->dbt1, key);
1799 root 1.5 REQ_SEND;
1800     }
1801    
1802     void
1803 root 1.41 db_txn_commit (DB_TXN *txn, U32 flags = 0, SV *callback = 0)
1804     PREINIT:
1805     CALLBACK
1806 root 1.5 CODE:
1807     {
1808 root 1.50 dREQ (REQ_TXN_COMMIT, 1);
1809 root 1.5 req->txn = txn;
1810     req->uint1 = flags;
1811     REQ_SEND;
1812     ptr_nuke (ST (0));
1813     }
1814    
1815     void
1816 root 1.41 db_txn_abort (DB_TXN *txn, SV *callback = 0)
1817     PREINIT:
1818     CALLBACK
1819 root 1.5 CODE:
1820     {
1821 root 1.50 dREQ (REQ_TXN_ABORT, 1);
1822 root 1.5 req->txn = txn;
1823     REQ_SEND;
1824     ptr_nuke (ST (0));
1825     }
1826    
1827 root 1.6 void
1828 root 1.41 db_txn_finish (DB_TXN *txn, U32 flags = 0, SV *callback = 0)
1829     PREINIT:
1830     CALLBACK
1831 root 1.17 CODE:
1832     {
1833 root 1.50 dREQ (REQ_TXN_FINISH, 1);
1834 root 1.17 req->txn = txn;
1835     req->uint1 = flags;
1836     REQ_SEND;
1837     ptr_nuke (ST (0));
1838     }
1839    
1840     void
1841 root 1.41 db_c_close (DBC *dbc, SV *callback = 0)
1842     PREINIT:
1843     CALLBACK
1844 root 1.6 CODE:
1845     {
1846 root 1.50 dREQ (REQ_C_CLOSE, 1);
1847 root 1.6 req->dbc = dbc;
1848     REQ_SEND;
1849     ptr_nuke (ST (0));
1850     }
1851    
1852     void
1853 root 1.41 db_c_count (DBC *dbc, SV *count, U32 flags = 0, SV *callback = 0)
1854     PREINIT:
1855     CALLBACK
1856 root 1.6 CODE:
1857     {
1858 root 1.50 dREQ (REQ_C_COUNT, 1);
1859 root 1.6 req->dbc = dbc;
1860     req->sv1 = SvREFCNT_inc (count);
1861     REQ_SEND;
1862     }
1863    
1864     void
1865 root 1.41 db_c_put (DBC *dbc, SV *key, SV *data, U32 flags = 0, SV *callback = 0)
1866     PREINIT:
1867     CALLBACK
1868 root 1.6 CODE:
1869     {
1870 root 1.50 dREQ (REQ_C_PUT, 1);
1871 root 1.6 req->dbc = dbc;
1872     sv_to_dbt (&req->dbt1, key);
1873     sv_to_dbt (&req->dbt2, data);
1874     req->uint1 = flags;
1875     REQ_SEND;
1876     }
1877    
1878     void
1879 root 1.41 db_c_get (DBC *dbc, SV *key, SV *data, U32 flags = 0, SV *callback = 0)
1880     PREINIT:
1881     CALLBACK
1882 root 1.6 CODE:
1883     {
1884 root 1.50 dREQ (REQ_C_GET, 1);
1885 root 1.6 req->dbc = dbc;
1886     req->uint1 = flags;
1887     if ((flags & DB_SET) == DB_SET
1888     || (flags & DB_SET_RANGE) == DB_SET_RANGE)
1889     sv_to_dbt (&req->dbt1, key);
1890     else
1891     req->dbt1.flags = DB_DBT_MALLOC;
1892    
1893     req->sv1 = SvREFCNT_inc (key); SvREADONLY_on (key);
1894    
1895     if ((flags & DB_GET_BOTH) == DB_GET_BOTH
1896     || (flags & DB_GET_BOTH_RANGE) == DB_GET_BOTH_RANGE)
1897     sv_to_dbt (&req->dbt3, data);
1898     else
1899     req->dbt3.flags = DB_DBT_MALLOC;
1900    
1901     req->sv3 = SvREFCNT_inc (data); SvREADONLY_on (data);
1902     REQ_SEND;
1903     }
1904    
1905     void
1906 root 1.41 db_c_pget (DBC *dbc, SV *key, SV *pkey, SV *data, U32 flags = 0, SV *callback = 0)
1907     PREINIT:
1908     CALLBACK
1909 root 1.6 CODE:
1910     {
1911 root 1.50 dREQ (REQ_C_PGET, 1);
1912 root 1.6 req->dbc = dbc;
1913     req->uint1 = flags;
1914     if ((flags & DB_SET) == DB_SET
1915     || (flags & DB_SET_RANGE) == DB_SET_RANGE)
1916     sv_to_dbt (&req->dbt1, key);
1917     else
1918     req->dbt1.flags = DB_DBT_MALLOC;
1919    
1920     req->sv1 = SvREFCNT_inc (key); SvREADONLY_on (key);
1921    
1922     req->dbt2.flags = DB_DBT_MALLOC;
1923     req->sv2 = SvREFCNT_inc (pkey); SvREADONLY_on (pkey);
1924    
1925     if ((flags & DB_GET_BOTH) == DB_GET_BOTH
1926     || (flags & DB_GET_BOTH_RANGE) == DB_GET_BOTH_RANGE)
1927     sv_to_dbt (&req->dbt3, data);
1928     else
1929     req->dbt3.flags = DB_DBT_MALLOC;
1930    
1931     req->sv3 = SvREFCNT_inc (data); SvREADONLY_on (data);
1932     REQ_SEND;
1933     }
1934    
1935     void
1936 root 1.41 db_c_del (DBC *dbc, U32 flags = 0, SV *callback = 0)
1937     PREINIT:
1938     CALLBACK
1939 root 1.6 CODE:
1940     {
1941 root 1.50 dREQ (REQ_C_DEL, 1);
1942 root 1.6 req->dbc = dbc;
1943     req->uint1 = flags;
1944     REQ_SEND;
1945     }
1946    
1947 root 1.2
1948 root 1.47 #if DB_VERSION_MINOR >= 3
1949    
1950 root 1.8 void
1951 root 1.41 db_sequence_open (DB_SEQUENCE *seq, DB_TXN_ornull *txnid, SV *key, U32 flags = 0, SV *callback = 0)
1952     PREINIT:
1953     CALLBACK
1954 root 1.8 CODE:
1955     {
1956 root 1.50 dREQ (REQ_SEQ_OPEN, 2);
1957 root 1.8 req->seq = seq;
1958     req->txn = txnid;
1959     req->uint1 = flags | DB_THREAD;
1960     sv_to_dbt (&req->dbt1, key);
1961     REQ_SEND;
1962     }
1963    
1964     void
1965 root 1.41 db_sequence_close (DB_SEQUENCE *seq, U32 flags = 0, SV *callback = 0)
1966     PREINIT:
1967     CALLBACK
1968 root 1.8 CODE:
1969     {
1970 root 1.50 dREQ (REQ_SEQ_CLOSE, 1);
1971 root 1.8 req->seq = seq;
1972     req->uint1 = flags;
1973     REQ_SEND;
1974     ptr_nuke (ST (0));
1975     }
1976    
1977     void
1978 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)
1979     PREINIT:
1980     CALLBACK
1981 root 1.8 CODE:
1982     {
1983 root 1.50 dREQ (REQ_SEQ_GET, 2);
1984 root 1.8 req->seq = seq;
1985     req->txn = txnid;
1986     req->int1 = delta;
1987     req->uint1 = flags;
1988     req->sv1 = SvREFCNT_inc (seq_value); SvREADONLY_on (seq_value);
1989     REQ_SEND;
1990     }
1991    
1992     void
1993 root 1.41 db_sequence_remove (DB_SEQUENCE *seq, DB_TXN_ornull *txnid = 0, U32 flags = 0, SV *callback = 0)
1994     PREINIT:
1995     CALLBACK
1996 root 1.8 CODE:
1997     {
1998 root 1.50 dREQ (REQ_SEQ_REMOVE, 2);
1999 root 1.8 req->seq = seq;
2000     req->txn = txnid;
2001     req->uint1 = flags;
2002     REQ_SEND;
2003     }
2004    
2005 root 1.47 #endif
2006    
2007 root 1.8
2008 root 1.2 MODULE = BDB PACKAGE = BDB::Env
2009    
2010 root 1.5 void
2011 root 1.30 DESTROY (DB_ENV_ornuked *env)
2012 root 1.5 CODE:
2013     if (env)
2014     env->close (env, 0);
2015    
2016 root 1.8 int set_data_dir (DB_ENV *env, const char *dir)
2017     CODE:
2018     RETVAL = env->set_data_dir (env, dir);
2019     OUTPUT:
2020     RETVAL
2021    
2022     int set_tmp_dir (DB_ENV *env, const char *dir)
2023     CODE:
2024     RETVAL = env->set_tmp_dir (env, dir);
2025     OUTPUT:
2026     RETVAL
2027    
2028     int set_lg_dir (DB_ENV *env, const char *dir)
2029     CODE:
2030     RETVAL = env->set_lg_dir (env, dir);
2031     OUTPUT:
2032     RETVAL
2033    
2034     int set_shm_key (DB_ENV *env, long shm_key)
2035     CODE:
2036     RETVAL = env->set_shm_key (env, shm_key);
2037     OUTPUT:
2038     RETVAL
2039    
2040 root 1.2 int set_cachesize (DB_ENV *env, U32 gbytes, U32 bytes, int ncache = 0)
2041 root 1.3 CODE:
2042     RETVAL = env->set_cachesize (env, gbytes, bytes, ncache);
2043     OUTPUT:
2044     RETVAL
2045 root 1.2
2046 root 1.25 int set_flags (DB_ENV *env, U32 flags, int onoff = 1)
2047 root 1.3 CODE:
2048     RETVAL = env->set_flags (env, flags, onoff);
2049     OUTPUT:
2050     RETVAL
2051    
2052 root 1.39 #if DB_VERSION_MINOR >= 7
2053    
2054     int set_intermediate_dir_mode (DB_ENV *env, const char *mode)
2055     CODE:
2056     RETVAL = env->set_intermediate_dir_mode (env, mode);
2057     OUTPUT:
2058     RETVAL
2059    
2060     int log_set_config (DB_ENV *env, U32 flags, int onoff = 1)
2061     CODE:
2062     RETVAL = env->log_set_config (env, flags, onoff);
2063     OUTPUT:
2064     RETVAL
2065    
2066     #endif
2067    
2068    
2069 root 1.16 void set_errfile (DB_ENV *env, FILE *errfile = 0)
2070 root 1.14 CODE:
2071     env->set_errfile (env, errfile);
2072    
2073 root 1.16 void set_msgfile (DB_ENV *env, FILE *msgfile = 0)
2074 root 1.14 CODE:
2075     env->set_msgfile (env, msgfile);
2076    
2077 root 1.25 int set_verbose (DB_ENV *env, U32 which = -1, int onoff = 1)
2078 root 1.14 CODE:
2079     RETVAL = env->set_verbose (env, which, onoff);
2080     OUTPUT:
2081     RETVAL
2082    
2083 root 1.3 int set_encrypt (DB_ENV *env, const char *password, U32 flags = 0)
2084     CODE:
2085     RETVAL = env->set_encrypt (env, password, flags);
2086     OUTPUT:
2087     RETVAL
2088    
2089 root 1.17 int set_timeout (DB_ENV *env, NV timeout, U32 flags = DB_SET_TXN_TIMEOUT)
2090 root 1.3 CODE:
2091     RETVAL = env->set_timeout (env, timeout * 1000000, flags);
2092     OUTPUT:
2093     RETVAL
2094 root 1.2
2095 root 1.8 int set_mp_max_openfd (DB_ENV *env, int maxopenfd);
2096     CODE:
2097     RETVAL = env->set_mp_max_openfd (env, maxopenfd);
2098     OUTPUT:
2099     RETVAL
2100    
2101     int set_mp_max_write (DB_ENV *env, int maxwrite, int maxwrite_sleep);
2102     CODE:
2103     RETVAL = env->set_mp_max_write (env, maxwrite, maxwrite_sleep);
2104     OUTPUT:
2105     RETVAL
2106    
2107     int set_mp_mmapsize (DB_ENV *env, int mmapsize_mb)
2108     CODE:
2109     RETVAL = env->set_mp_mmapsize (env, ((size_t)mmapsize_mb) << 20);
2110     OUTPUT:
2111     RETVAL
2112    
2113 root 1.9 int set_lk_detect (DB_ENV *env, U32 detect = DB_LOCK_DEFAULT)
2114 root 1.8 CODE:
2115     RETVAL = env->set_lk_detect (env, detect);
2116     OUTPUT:
2117     RETVAL
2118    
2119     int set_lk_max_lockers (DB_ENV *env, U32 max)
2120     CODE:
2121     RETVAL = env->set_lk_max_lockers (env, max);
2122     OUTPUT:
2123     RETVAL
2124    
2125     int set_lk_max_locks (DB_ENV *env, U32 max)
2126     CODE:
2127     RETVAL = env->set_lk_max_locks (env, max);
2128     OUTPUT:
2129     RETVAL
2130    
2131     int set_lk_max_objects (DB_ENV *env, U32 max)
2132     CODE:
2133     RETVAL = env->set_lk_max_objects (env, max);
2134     OUTPUT:
2135     RETVAL
2136    
2137     int set_lg_bsize (DB_ENV *env, U32 max)
2138     CODE:
2139     RETVAL = env->set_lg_bsize (env, max);
2140     OUTPUT:
2141     RETVAL
2142    
2143     int set_lg_max (DB_ENV *env, U32 max)
2144     CODE:
2145     RETVAL = env->set_lg_max (env, max);
2146     OUTPUT:
2147     RETVAL
2148    
2149 root 1.47 #if DB_VERSION_MINOR >= 4
2150    
2151 root 1.22 int mutex_set_max (DB_ENV *env, U32 max)
2152     CODE:
2153     RETVAL = env->mutex_set_max (env, max);
2154     OUTPUT:
2155     RETVAL
2156    
2157     int mutex_set_increment (DB_ENV *env, U32 increment)
2158     CODE:
2159     RETVAL = env->mutex_set_increment (env, increment);
2160     OUTPUT:
2161     RETVAL
2162    
2163     int mutex_set_tas_spins (DB_ENV *env, U32 tas_spins)
2164     CODE:
2165     RETVAL = env->mutex_set_tas_spins (env, tas_spins);
2166     OUTPUT:
2167     RETVAL
2168    
2169     int mutex_set_align (DB_ENV *env, U32 align)
2170     CODE:
2171     RETVAL = env->mutex_set_align (env, align);
2172     OUTPUT:
2173     RETVAL
2174    
2175 root 1.47 #endif
2176    
2177 root 1.5 DB_TXN *
2178     txn_begin (DB_ENV *env, DB_TXN_ornull *parent = 0, U32 flags = 0)
2179     CODE:
2180     errno = env->txn_begin (env, parent, &RETVAL, flags);
2181     if (errno)
2182 root 1.6 croak ("DB_ENV->txn_begin: %s", db_strerror (errno));
2183 root 1.5 OUTPUT:
2184     RETVAL
2185 root 1.2
2186 root 1.46 #if DB_VERSION_MINOR >= 5
2187    
2188 root 1.44 DB_TXN *
2189     cdsgroup_begin (DB_ENV *env)
2190     CODE:
2191     errno = env->cdsgroup_begin (env, &RETVAL);
2192     if (errno)
2193     croak ("DB_ENV->cdsgroup_begin: %s", db_strerror (errno));
2194     OUTPUT:
2195     RETVAL
2196    
2197 root 1.46 #endif
2198    
2199 root 1.2 MODULE = BDB PACKAGE = BDB::Db
2200    
2201 root 1.5 void
2202 root 1.30 DESTROY (DB_ornuked *db)
2203 root 1.5 CODE:
2204     if (db)
2205     {
2206     SV *env = (SV *)db->app_private;
2207     db->close (db, 0);
2208     SvREFCNT_dec (env);
2209     }
2210    
2211 root 1.2 int set_cachesize (DB *db, U32 gbytes, U32 bytes, int ncache = 0)
2212 root 1.3 CODE:
2213     RETVAL = db->set_cachesize (db, gbytes, bytes, ncache);
2214     OUTPUT:
2215     RETVAL
2216 root 1.2
2217 root 1.16 int set_flags (DB *db, U32 flags)
2218 root 1.3 CODE:
2219     RETVAL = db->set_flags (db, flags);
2220     OUTPUT:
2221     RETVAL
2222 root 1.2
2223     int set_encrypt (DB *db, const char *password, U32 flags)
2224 root 1.3 CODE:
2225     RETVAL = db->set_encrypt (db, password, flags);
2226     OUTPUT:
2227     RETVAL
2228 root 1.2
2229     int set_lorder (DB *db, int lorder)
2230 root 1.3 CODE:
2231     RETVAL = db->set_lorder (db, lorder);
2232     OUTPUT:
2233     RETVAL
2234 root 1.2
2235     int set_bt_minkey (DB *db, U32 minkey)
2236 root 1.3 CODE:
2237     RETVAL = db->set_bt_minkey (db, minkey);
2238     OUTPUT:
2239     RETVAL
2240 root 1.2
2241 root 1.16 int set_re_delim (DB *db, int delim)
2242 root 1.3 CODE:
2243     RETVAL = db->set_re_delim (db, delim);
2244     OUTPUT:
2245     RETVAL
2246 root 1.2
2247     int set_re_pad (DB *db, int re_pad)
2248 root 1.3 CODE:
2249     RETVAL = db->set_re_pad (db, re_pad);
2250     OUTPUT:
2251     RETVAL
2252 root 1.2
2253     int set_re_source (DB *db, char *source)
2254 root 1.3 CODE:
2255     RETVAL = db->set_re_source (db, source);
2256     OUTPUT:
2257     RETVAL
2258 root 1.2
2259     int set_re_len (DB *db, U32 re_len)
2260 root 1.3 CODE:
2261     RETVAL = db->set_re_len (db, re_len);
2262     OUTPUT:
2263     RETVAL
2264 root 1.2
2265     int set_h_ffactor (DB *db, U32 h_ffactor)
2266 root 1.3 CODE:
2267     RETVAL = db->set_h_ffactor (db, h_ffactor);
2268     OUTPUT:
2269     RETVAL
2270 root 1.2
2271     int set_h_nelem (DB *db, U32 h_nelem)
2272 root 1.3 CODE:
2273     RETVAL = db->set_h_nelem (db, h_nelem);
2274     OUTPUT:
2275     RETVAL
2276 root 1.2
2277     int set_q_extentsize (DB *db, U32 extentsize)
2278 root 1.3 CODE:
2279     RETVAL = db->set_q_extentsize (db, extentsize);
2280     OUTPUT:
2281     RETVAL
2282 root 1.2
2283 root 1.6 DBC *
2284     cursor (DB *db, DB_TXN_ornull *txn = 0, U32 flags = 0)
2285     CODE:
2286     errno = db->cursor (db, txn, &RETVAL, flags);
2287     if (errno)
2288     croak ("DB->cursor: %s", db_strerror (errno));
2289     OUTPUT:
2290     RETVAL
2291    
2292 root 1.47 #if DB_VERSION_MINOR >= 3
2293    
2294 root 1.8 DB_SEQUENCE *
2295     sequence (DB *db, U32 flags = 0)
2296     CODE:
2297     {
2298     errno = db_sequence_create (&RETVAL, db, flags);
2299     if (errno)
2300     croak ("db_sequence_create: %s", db_strerror (errno));
2301     }
2302     OUTPUT:
2303     RETVAL
2304    
2305 root 1.47 #endif
2306    
2307 root 1.6
2308 root 1.5 MODULE = BDB PACKAGE = BDB::Txn
2309    
2310     void
2311 root 1.30 DESTROY (DB_TXN_ornuked *txn)
2312 root 1.5 CODE:
2313     if (txn)
2314     txn->abort (txn);
2315    
2316 root 1.17 int set_timeout (DB_TXN *txn, NV timeout, U32 flags = DB_SET_TXN_TIMEOUT)
2317 root 1.5 CODE:
2318     RETVAL = txn->set_timeout (txn, timeout * 1000000, flags);
2319     OUTPUT:
2320     RETVAL
2321    
2322 root 1.17 int failed (DB_TXN *txn)
2323     CODE:
2324     RETVAL = !!(txn->flags & TXN_DEADLOCK);
2325     OUTPUT:
2326     RETVAL
2327    
2328 root 1.1
2329 root 1.6 MODULE = BDB PACKAGE = BDB::Cursor
2330    
2331     void
2332 root 1.30 DESTROY (DBC_ornuked *dbc)
2333 root 1.6 CODE:
2334     if (dbc)
2335     dbc->c_close (dbc);
2336    
2337 root 1.27 #if DB_VERSION_MINOR >= 6
2338    
2339     int set_priority (DBC *dbc, int priority)
2340     CODE:
2341     dbc->set_priority (dbc, priority);
2342    
2343     #endif
2344    
2345 root 1.47 #if DB_VERSION_MINOR >= 3
2346    
2347 root 1.8 MODULE = BDB PACKAGE = BDB::Sequence
2348    
2349     void
2350 root 1.30 DESTROY (DB_SEQUENCE_ornuked *seq)
2351 root 1.8 CODE:
2352     if (seq)
2353     seq->close (seq, 0);
2354    
2355     int initial_value (DB_SEQUENCE *seq, db_seq_t value)
2356     CODE:
2357     RETVAL = seq->initial_value (seq, value);
2358     OUTPUT:
2359     RETVAL
2360    
2361     int set_cachesize (DB_SEQUENCE *seq, U32 size)
2362     CODE:
2363     RETVAL = seq->set_cachesize (seq, size);
2364     OUTPUT:
2365     RETVAL
2366    
2367     int set_flags (DB_SEQUENCE *seq, U32 flags)
2368     CODE:
2369     RETVAL = seq->set_flags (seq, flags);
2370     OUTPUT:
2371     RETVAL
2372    
2373     int set_range (DB_SEQUENCE *seq, db_seq_t min, db_seq_t max)
2374     CODE:
2375     RETVAL = seq->set_range (seq, min, max);
2376     OUTPUT:
2377     RETVAL
2378    
2379 root 1.47 #endif
2380    
2381 root 1.31