ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/BDB/BDB.xs
Revision: 1.58
Committed: Tue Sep 30 17:03:07 2008 UTC (15 years, 7 months ago) by root
Branch: MAIN
Changes since 1.57: +1 -2 lines
Log Message:
*** empty log message ***

File Contents

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