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