ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/BDB/BDB.xs
Revision: 1.57
Committed: Mon Sep 29 03:01:54 2008 UTC (15 years, 7 months ago) by root
Branch: MAIN
CVS Tags: rel-1_801
Changes since 1.56: +53 -49 lines
Log Message:
1.801

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