ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/BDB/BDB.xs
Revision: 1.53
Committed: Thu Sep 25 22:09:02 2008 UTC (15 years, 7 months ago) by root
Branch: MAIN
Changes since 1.52: +80 -62 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.49 #include <sys/user.h>
2     #include <sys/ptrace.h>
3    
4 root 1.48 #define X_STACKSIZE 1024 * 128 + sizeof (long) * 64 * 1024 / 4
5    
6 root 1.11 #include "xthread.h"
7 root 1.1
8     #include <errno.h>
9    
10     #include "EXTERN.h"
11     #include "perl.h"
12     #include "XSUB.h"
13    
14 root 1.13 // perl stupidly defines these as macros, breaking
15     // lots and lots of code.
16     #undef open
17     #undef close
18     #undef abort
19     #undef malloc
20     #undef free
21     #undef send
22    
23 root 1.1 #include <stddef.h>
24     #include <stdlib.h>
25     #include <errno.h>
26     #include <sys/types.h>
27     #include <limits.h>
28     #include <fcntl.h>
29 root 1.2
30 root 1.13 #ifndef _WIN32
31     # include <sys/time.h>
32     # include <unistd.h>
33     #endif
34    
35 root 1.2 #include <db.h>
36 root 1.1
37 root 1.47 #if DB_VERSION_MAJOR != 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR < 3)
38     # error you need Berkeley DB 4.3 or a newer 4.x version installed
39 root 1.10 #endif
40    
41 root 1.1 /* number of seconds after which idle threads exit */
42     #define IDLE_TIMEOUT 10
43    
44 root 1.8 typedef DB_ENV DB_ENV_ornull;
45     typedef DB_TXN DB_TXN_ornull;
46     typedef DBC DBC_ornull;
47     typedef DB DB_ornull;
48 root 1.5
49 root 1.30 typedef DB_ENV DB_ENV_ornuked;
50     typedef DB_TXN DB_TXN_ornuked;
51     typedef DBC DBC_ornuked;
52     typedef DB DB_ornuked;
53 root 1.47
54     #if DB_VERSION_MINOR >= 3
55     typedef DB_SEQUENCE DB_SEQUENCE_ornull;
56 root 1.30 typedef DB_SEQUENCE DB_SEQUENCE_ornuked;
57 root 1.47 #endif
58 root 1.30
59 root 1.1 typedef SV SV8; /* byte-sv, used for argument-checking */
60 root 1.33 typedef char *bdb_filename;
61 root 1.1
62 root 1.3 static SV *prepare_cb;
63    
64 root 1.16 #if DB_VERSION_MINOR >= 6
65     # define c_close close
66     # define c_count count
67     # define c_del del
68     # define c_dup dup
69     # define c_get get
70     # define c_pget pget
71     # define c_put put
72     #endif
73    
74 root 1.34 static char *
75     get_bdb_filename (SV *sv)
76     {
77 root 1.36 if (!SvOK (sv))
78     return 0;
79    
80 root 1.35 #if _WIN32
81 root 1.36 /* win32 madness + win32 perl absolutely brokenness make for horrible hacks */
82     {
83     STRLEN len;
84     char *src = SvPVbyte (sv, len);
85     SV *t1 = sv_newmortal ();
86     SV *t2 = sv_newmortal ();
87    
88     sv_upgrade (t1, SVt_PV); SvPOK_only (t1); SvGROW (t1, len * 16 + 1);
89     sv_upgrade (t2, SVt_PV); SvPOK_only (t2); SvGROW (t2, len * 16 + 1);
90    
91     len = MultiByteToWideChar (CP_ACP, 0, src, len, (WCHAR *)SvPVX (t1), SvLEN (t1) / sizeof (WCHAR));
92     len = WideCharToMultiByte (CP_UTF8, 0, (WCHAR *)SvPVX (t1), len, SvPVX (t2), SvLEN (t2), 0, 0);
93     SvPOK_only (t2);
94     SvPVX (t2)[len] = 0;
95     SvCUR_set (t2, len);
96    
97     return SvPVX (t2);
98     }
99 root 1.34 #else
100 root 1.36 return SvPVbyte_nolen (sv);
101 root 1.34 #endif
102     }
103    
104 root 1.14 static void
105     debug_errcall (const DB_ENV *dbenv, const char *errpfx, const char *msg)
106     {
107     printf ("err[%s]\n", msg);
108     }
109    
110     static void
111     debug_msgcall (const DB_ENV *dbenv, const char *msg)
112     {
113     printf ("msg[%s]\n", msg);
114     }
115    
116 root 1.13 static char *
117 root 1.3 strdup_ornull (const char *s)
118 root 1.2 {
119 root 1.3 return s ? strdup (s) : 0;
120     }
121    
122 root 1.13 static void
123 root 1.5 sv_to_dbt (DBT *dbt, SV *sv)
124     {
125     STRLEN len;
126     char *data = SvPVbyte (sv, len);
127    
128     dbt->data = malloc (len);
129     memcpy (dbt->data, data, len);
130     dbt->size = len;
131 root 1.6 dbt->flags = DB_DBT_REALLOC;
132 root 1.5 }
133    
134 root 1.13 static void
135 root 1.5 dbt_to_sv (SV *sv, DBT *dbt)
136     {
137 root 1.6 if (sv)
138     {
139     SvREADONLY_off (sv);
140 root 1.49 sv_setsv_mg (sv, dbt->data ? newSVpvn (dbt->data, dbt->size) : &PL_sv_undef);
141 root 1.6 SvREFCNT_dec (sv);
142     }
143 root 1.5
144 root 1.49 //assert (dbt->flags & DBT_MALLOC || !dbt->data);
145 root 1.5 free (dbt->data);
146     }
147    
148 root 1.3 enum {
149 root 1.1 REQ_QUIT,
150 root 1.8 REQ_ENV_OPEN, REQ_ENV_CLOSE, REQ_ENV_TXN_CHECKPOINT, REQ_ENV_LOCK_DETECT,
151 root 1.39 REQ_ENV_MEMP_SYNC, REQ_ENV_MEMP_TRICKLE, REQ_ENV_DBREMOVE, REQ_ENV_DBRENAME,
152 root 1.26 REQ_DB_OPEN, REQ_DB_CLOSE, REQ_DB_COMPACT, REQ_DB_SYNC, REQ_DB_UPGRADE,
153 root 1.45 REQ_DB_PUT, REQ_DB_EXISTS, REQ_DB_GET, REQ_DB_PGET, REQ_DB_DEL, REQ_DB_KEY_RANGE,
154 root 1.17 REQ_TXN_COMMIT, REQ_TXN_ABORT, REQ_TXN_FINISH,
155 root 1.6 REQ_C_CLOSE, REQ_C_COUNT, REQ_C_PUT, REQ_C_GET, REQ_C_PGET, REQ_C_DEL,
156 root 1.8 REQ_SEQ_OPEN, REQ_SEQ_CLOSE, REQ_SEQ_GET, REQ_SEQ_REMOVE,
157 root 1.1 };
158    
159 root 1.28 typedef struct bdb_cb
160 root 1.1 {
161 root 1.28 struct bdb_cb *volatile next;
162 root 1.2 SV *callback;
163 root 1.3 int type, pri, result;
164 root 1.2
165     DB_ENV *env;
166     DB *db;
167     DB_TXN *txn;
168 root 1.6 DBC *dbc;
169    
170     UV uv1;
171 root 1.2 int int1, int2;
172     U32 uint1, uint2;
173 root 1.39 char *buf1, *buf2, *buf3;
174 root 1.6 SV *sv1, *sv2, *sv3;
175 root 1.3
176     DBT dbt1, dbt2, dbt3;
177 root 1.8 DB_KEY_RANGE key_range;
178 root 1.47 #if DB_VERSION_MINOR >= 3
179 root 1.8 DB_SEQUENCE *seq;
180     db_seq_t seq_t;
181 root 1.47 #endif
182 root 1.50
183     SV *rsv1, *rsv2; // keep some request objects alive
184 root 1.28 } bdb_cb;
185 root 1.1
186 root 1.28 typedef bdb_cb *bdb_req;
187 root 1.1
188     enum {
189     PRI_MIN = -4,
190     PRI_MAX = 4,
191    
192     DEFAULT_PRI = 0,
193     PRI_BIAS = -PRI_MIN,
194     NUM_PRI = PRI_MAX + PRI_BIAS + 1,
195     };
196    
197     #define AIO_TICKS ((1000000 + 1023) >> 10)
198    
199 root 1.37 static SV *on_next_submit;
200    
201 root 1.1 static unsigned int max_poll_time = 0;
202     static unsigned int max_poll_reqs = 0;
203    
204     /* calculcate time difference in ~1/AIO_TICKS of a second */
205     static int tvdiff (struct timeval *tv1, struct timeval *tv2)
206     {
207     return (tv2->tv_sec - tv1->tv_sec ) * AIO_TICKS
208     + ((tv2->tv_usec - tv1->tv_usec) >> 10);
209     }
210    
211     static int next_pri = DEFAULT_PRI + PRI_BIAS;
212    
213     static unsigned int started, idle, wanted;
214    
215     /* worker threads management */
216 root 1.12 static mutex_t wrklock = X_MUTEX_INIT;
217 root 1.1
218     typedef struct worker {
219     /* locked by wrklock */
220     struct worker *prev, *next;
221    
222 root 1.11 thread_t tid;
223 root 1.1
224     /* locked by reslock, reqlock or wrklock */
225 root 1.28 bdb_req req; /* currently processed request */
226 root 1.1 void *dbuf;
227     DIR *dirp;
228     } worker;
229    
230     static worker wrk_first = { &wrk_first, &wrk_first, 0 };
231    
232     static void worker_clear (worker *wrk)
233     {
234     }
235    
236     static void worker_free (worker *wrk)
237     {
238     wrk->next->prev = wrk->prev;
239     wrk->prev->next = wrk->next;
240    
241     free (wrk);
242     }
243    
244     static volatile unsigned int nreqs, nready, npending;
245     static volatile unsigned int max_idle = 4;
246     static volatile unsigned int max_outstanding = 0xffffffff;
247 root 1.19 static int respipe_osf [2], respipe [2] = { -1, -1 };
248 root 1.1
249 root 1.12 static mutex_t reslock = X_MUTEX_INIT;
250     static mutex_t reqlock = X_MUTEX_INIT;
251     static cond_t reqwait = X_COND_INIT;
252 root 1.1
253     #if WORDACCESS_UNSAFE
254    
255 root 1.34 static unsigned int get_nready (void)
256 root 1.1 {
257     unsigned int retval;
258    
259 root 1.12 X_LOCK (reqlock);
260 root 1.1 retval = nready;
261 root 1.12 X_UNLOCK (reqlock);
262 root 1.1
263     return retval;
264     }
265    
266 root 1.34 static unsigned int get_npending (void)
267 root 1.1 {
268     unsigned int retval;
269    
270 root 1.12 X_LOCK (reslock);
271 root 1.1 retval = npending;
272 root 1.12 X_UNLOCK (reslock);
273 root 1.1
274     return retval;
275     }
276    
277 root 1.34 static unsigned int get_nthreads (void)
278 root 1.1 {
279     unsigned int retval;
280    
281 root 1.12 X_LOCK (wrklock);
282 root 1.1 retval = started;
283 root 1.12 X_UNLOCK (wrklock);
284 root 1.1
285     return retval;
286     }
287    
288     #else
289    
290     # define get_nready() nready
291     # define get_npending() npending
292     # define get_nthreads() started
293    
294     #endif
295    
296     /*
297     * a somewhat faster data structure might be nice, but
298     * with 8 priorities this actually needs <20 insns
299     * per shift, the most expensive operation.
300     */
301     typedef struct {
302 root 1.28 bdb_req qs[NUM_PRI], qe[NUM_PRI]; /* qstart, qend */
303 root 1.1 int size;
304     } reqq;
305    
306     static reqq req_queue;
307     static reqq res_queue;
308    
309 root 1.28 int reqq_push (reqq *q, bdb_req req)
310 root 1.1 {
311     int pri = req->pri;
312     req->next = 0;
313    
314     if (q->qe[pri])
315     {
316     q->qe[pri]->next = req;
317     q->qe[pri] = req;
318     }
319     else
320     q->qe[pri] = q->qs[pri] = req;
321    
322     return q->size++;
323     }
324    
325 root 1.28 bdb_req reqq_shift (reqq *q)
326 root 1.1 {
327     int pri;
328    
329     if (!q->size)
330     return 0;
331    
332     --q->size;
333    
334     for (pri = NUM_PRI; pri--; )
335     {
336 root 1.28 bdb_req req = q->qs[pri];
337 root 1.1
338     if (req)
339     {
340     if (!(q->qs[pri] = req->next))
341     q->qe[pri] = 0;
342    
343     return req;
344     }
345     }
346    
347     abort ();
348     }
349    
350 root 1.34 static int poll_cb (void);
351 root 1.28 static void req_free (bdb_req req);
352     static void req_cancel (bdb_req req);
353 root 1.1
354 root 1.28 static int req_invoke (bdb_req req)
355 root 1.1 {
356 root 1.53 switch (req->type)
357 root 1.1 {
358 root 1.53 case REQ_DB_CLOSE:
359     SvREFCNT_dec (req->sv1);
360     break;
361 root 1.1
362 root 1.53 case REQ_DB_GET:
363     case REQ_DB_PGET:
364     dbt_to_sv (req->sv3, &req->dbt3);
365     break;
366 root 1.6
367 root 1.53 case REQ_C_GET:
368     case REQ_C_PGET:
369     dbt_to_sv (req->sv1, &req->dbt1);
370     dbt_to_sv (req->sv2, &req->dbt2);
371     dbt_to_sv (req->sv3, &req->dbt3);
372     break;
373 root 1.5
374 root 1.53 case REQ_DB_PUT:
375     case REQ_C_PUT:
376     dbt_to_sv (0, &req->dbt1);
377     dbt_to_sv (0, &req->dbt2);
378     break;
379 root 1.8
380 root 1.53 case REQ_DB_KEY_RANGE:
381     {
382     AV *av = newAV ();
383 root 1.23
384 root 1.53 av_push (av, newSVnv (req->key_range.less));
385     av_push (av, newSVnv (req->key_range.equal));
386     av_push (av, newSVnv (req->key_range.greater));
387    
388     SvREADONLY_off (req->sv1);
389     sv_setsv_mg (req->sv1, newRV_noinc ((SV *)av));
390     SvREFCNT_dec (req->sv1);
391     }
392     break;
393 root 1.8
394 root 1.47 #if DB_VERSION_MINOR >= 3
395 root 1.53 case REQ_SEQ_GET:
396     SvREADONLY_off (req->sv1);
397 root 1.8
398 root 1.53 if (sizeof (IV) > 4)
399     sv_setiv_mg (req->sv1, (IV)req->seq_t);
400     else
401     sv_setnv_mg (req->sv1, (NV)req->seq_t);
402 root 1.8
403 root 1.53 SvREFCNT_dec (req->sv1);
404     break;
405 root 1.47 #endif
406 root 1.53 }
407    
408     errno = req->result;
409    
410     if (req->callback)
411     {
412     dSP;
413 root 1.1
414 root 1.53 ENTER;
415     SAVETMPS;
416     PUSHMARK (SP);
417 root 1.3
418 root 1.1 PUTBACK;
419     call_sv (req->callback, G_VOID | G_EVAL);
420     SPAGAIN;
421    
422     FREETMPS;
423     LEAVE;
424 root 1.53
425     return !SvTRUE (ERRSV);
426 root 1.1 }
427    
428 root 1.53 return 1;
429 root 1.1 }
430    
431 root 1.28 static void req_free (bdb_req req)
432 root 1.1 {
433 root 1.43 SvREFCNT_dec (req->callback);
434    
435 root 1.50 SvREFCNT_dec (req->rsv1);
436     SvREFCNT_dec (req->rsv2);
437    
438 root 1.2 free (req->buf1);
439     free (req->buf2);
440 root 1.39 free (req->buf3);
441 root 1.43
442 root 1.1 Safefree (req);
443     }
444    
445 root 1.13 #ifdef USE_SOCKETS_AS_HANDLES
446     # define TO_SOCKET(x) (win32_get_osfhandle (x))
447     #else
448     # define TO_SOCKET(x) (x)
449     #endif
450    
451     static void
452 root 1.34 create_respipe (void)
453 root 1.13 {
454 root 1.21 #ifdef _WIN32
455     int arg; /* argg */
456     #endif
457 root 1.19 int old_readfd = respipe [0];
458    
459     if (respipe [1] >= 0)
460     respipe_close (TO_SOCKET (respipe [1]));
461    
462     #ifdef _WIN32
463     if (PerlSock_socketpair (AF_UNIX, SOCK_STREAM, 0, respipe))
464     #else
465     if (pipe (respipe))
466     #endif
467     croak ("unable to initialize result pipe");
468    
469     if (old_readfd >= 0)
470     {
471     if (dup2 (TO_SOCKET (respipe [0]), TO_SOCKET (old_readfd)) < 0)
472     croak ("unable to initialize result pipe(2)");
473    
474     respipe_close (respipe [0]);
475     respipe [0] = old_readfd;
476     }
477    
478 root 1.13 #ifdef _WIN32
479 root 1.21 arg = 1;
480 root 1.19 if (ioctlsocket (TO_SOCKET (respipe [0]), FIONBIO, &arg)
481     || ioctlsocket (TO_SOCKET (respipe [1]), FIONBIO, &arg))
482 root 1.13 #else
483 root 1.19 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)
484     || fcntl (respipe [1], F_SETFL, O_NONBLOCK))
485 root 1.13 #endif
486 root 1.19 croak ("unable to initialize result pipe(3)");
487 root 1.13
488     respipe_osf [0] = TO_SOCKET (respipe [0]);
489     respipe_osf [1] = TO_SOCKET (respipe [1]);
490     }
491    
492 root 1.53 static void bdb_request (bdb_req req);
493 root 1.13 X_THREAD_PROC (bdb_proc);
494 root 1.1
495     static void start_thread (void)
496     {
497     worker *wrk = calloc (1, sizeof (worker));
498    
499     if (!wrk)
500     croak ("unable to allocate worker thread data");
501    
502 root 1.12 X_LOCK (wrklock);
503 root 1.13 if (thread_create (&wrk->tid, bdb_proc, (void *)wrk))
504 root 1.1 {
505     wrk->prev = &wrk_first;
506     wrk->next = wrk_first.next;
507     wrk_first.next->prev = wrk;
508     wrk_first.next = wrk;
509     ++started;
510     }
511     else
512     free (wrk);
513    
514 root 1.12 X_UNLOCK (wrklock);
515 root 1.1 }
516    
517 root 1.34 static void maybe_start_thread (void)
518 root 1.1 {
519     if (get_nthreads () >= wanted)
520     return;
521    
522     /* todo: maybe use idle here, but might be less exact */
523     if (0 <= (int)get_nthreads () + (int)get_npending () - (int)nreqs)
524     return;
525    
526     start_thread ();
527     }
528    
529 root 1.28 static void req_send (bdb_req req)
530 root 1.1 {
531 root 1.3 SV *wait_callback = 0;
532    
533 root 1.37 if (on_next_submit)
534     {
535     dSP;
536     SV *cb = sv_2mortal (on_next_submit);
537    
538     on_next_submit = 0;
539    
540     PUSHMARK (SP);
541     PUTBACK;
542     call_sv (cb, G_DISCARD | G_EVAL);
543 root 1.49 SPAGAIN;
544 root 1.37 }
545    
546 root 1.3 // synthesize callback if none given
547 root 1.41 if (!req->callback)
548 root 1.3 {
549 root 1.53 if (SvOK (prepare_cb))
550     {
551     int count;
552 root 1.13
553 root 1.53 dSP;
554     PUSHMARK (SP);
555     PUTBACK;
556     count = call_sv (prepare_cb, G_ARRAY);
557     SPAGAIN;
558 root 1.3
559 root 1.53 if (count != 2)
560     croak ("prepare callback must return exactly two values\n");
561 root 1.3
562 root 1.53 wait_callback = POPs;
563     req->callback = SvREFCNT_inc (POPs);
564     }
565     else
566     {
567     // execute request synchronously
568     bdb_request (req);
569     req_invoke (req);
570     req_free (req);
571     return;
572     }
573 root 1.3 }
574    
575 root 1.1 ++nreqs;
576    
577 root 1.12 X_LOCK (reqlock);
578 root 1.1 ++nready;
579     reqq_push (&req_queue, req);
580 root 1.12 X_COND_SIGNAL (reqwait);
581     X_UNLOCK (reqlock);
582 root 1.1
583     maybe_start_thread ();
584 root 1.3
585     if (wait_callback)
586     {
587     dSP;
588     PUSHMARK (SP);
589     PUTBACK;
590     call_sv (wait_callback, G_DISCARD);
591     }
592 root 1.1 }
593    
594     static void end_thread (void)
595     {
596 root 1.52 bdb_req req = calloc (1, sizeof (bdb_cb));
597 root 1.1
598     req->type = REQ_QUIT;
599     req->pri = PRI_MAX + PRI_BIAS;
600    
601 root 1.12 X_LOCK (reqlock);
602 root 1.1 reqq_push (&req_queue, req);
603 root 1.12 X_COND_SIGNAL (reqwait);
604     X_UNLOCK (reqlock);
605 root 1.1
606 root 1.12 X_LOCK (wrklock);
607 root 1.1 --started;
608 root 1.12 X_UNLOCK (wrklock);
609 root 1.1 }
610    
611     static void set_max_idle (int nthreads)
612     {
613 root 1.12 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
614 root 1.1 max_idle = nthreads <= 0 ? 1 : nthreads;
615 root 1.12 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
616 root 1.1 }
617    
618     static void min_parallel (int nthreads)
619     {
620     if (wanted < nthreads)
621     wanted = nthreads;
622     }
623    
624     static void max_parallel (int nthreads)
625     {
626     if (wanted > nthreads)
627     wanted = nthreads;
628    
629     while (started > wanted)
630     end_thread ();
631     }
632    
633 root 1.34 static void poll_wait (void)
634 root 1.1 {
635     fd_set rfd;
636    
637     while (nreqs)
638     {
639     int size;
640 root 1.12 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
641 root 1.1 size = res_queue.size;
642 root 1.12 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
643 root 1.1
644     if (size)
645     return;
646    
647     maybe_start_thread ();
648    
649 root 1.13 FD_ZERO (&rfd);
650     FD_SET (respipe [0], &rfd);
651 root 1.1
652 root 1.13 PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0);
653 root 1.1 }
654     }
655    
656 root 1.34 static int poll_cb (void)
657 root 1.1 {
658     dSP;
659     int count = 0;
660     int maxreqs = max_poll_reqs;
661     int do_croak = 0;
662     struct timeval tv_start, tv_now;
663 root 1.28 bdb_req req;
664 root 1.1
665     if (max_poll_time)
666     gettimeofday (&tv_start, 0);
667    
668     for (;;)
669     {
670     for (;;)
671     {
672     maybe_start_thread ();
673    
674 root 1.12 X_LOCK (reslock);
675 root 1.1 req = reqq_shift (&res_queue);
676    
677     if (req)
678     {
679     --npending;
680    
681     if (!res_queue.size)
682     {
683     /* read any signals sent by the worker threads */
684     char buf [4];
685 root 1.13 while (respipe_read (respipe [0], buf, 4) == 4)
686 root 1.1 ;
687     }
688     }
689    
690 root 1.12 X_UNLOCK (reslock);
691 root 1.1
692     if (!req)
693     break;
694    
695     --nreqs;
696    
697     if (!req_invoke (req))
698     {
699     req_free (req);
700     croak (0);
701     }
702    
703     count++;
704    
705     req_free (req);
706    
707     if (maxreqs && !--maxreqs)
708     break;
709    
710     if (max_poll_time)
711     {
712     gettimeofday (&tv_now, 0);
713    
714     if (tvdiff (&tv_start, &tv_now) >= max_poll_time)
715     break;
716     }
717     }
718    
719     if (nreqs <= max_outstanding)
720     break;
721    
722     poll_wait ();
723    
724     ++maxreqs;
725     }
726    
727     return count;
728     }
729    
730     /*****************************************************************************/
731    
732 root 1.51 static void
733     bdb_request (bdb_req req)
734     {
735     switch (req->type)
736     {
737     case REQ_ENV_OPEN:
738     req->result = req->env->open (req->env, req->buf1, req->uint1, req->int1);
739     break;
740    
741     case REQ_ENV_CLOSE:
742     req->result = req->env->close (req->env, req->uint1);
743     break;
744    
745     case REQ_ENV_TXN_CHECKPOINT:
746     req->result = req->env->txn_checkpoint (req->env, req->uint1, req->int1, req->uint2);
747     break;
748    
749     case REQ_ENV_LOCK_DETECT:
750     req->result = req->env->lock_detect (req->env, req->uint1, req->uint2, &req->int1);
751     break;
752    
753     case REQ_ENV_MEMP_SYNC:
754     req->result = req->env->memp_sync (req->env, 0);
755     break;
756    
757     case REQ_ENV_MEMP_TRICKLE:
758     req->result = req->env->memp_trickle (req->env, req->int1, &req->int2);
759     break;
760    
761     case REQ_ENV_DBREMOVE:
762     req->result = req->env->dbremove (req->env, req->txn, req->buf1, req->buf2, req->uint1);
763     break;
764    
765     case REQ_ENV_DBRENAME:
766     req->result = req->env->dbrename (req->env, req->txn, req->buf1, req->buf2, req->buf3, req->uint1);
767     break;
768    
769     case REQ_DB_OPEN:
770     req->result = req->db->open (req->db, req->txn, req->buf1, req->buf2, req->int1, req->uint1, req->int2);
771     break;
772    
773     case REQ_DB_CLOSE:
774     req->result = req->db->close (req->db, req->uint1);
775     break;
776    
777     #if DB_VERSION_MINOR >= 4
778     case REQ_DB_COMPACT:
779     req->result = req->db->compact (req->db, req->txn, &req->dbt1, &req->dbt2, 0, req->uint1, 0);
780     break;
781     #endif
782    
783     case REQ_DB_SYNC:
784     req->result = req->db->sync (req->db, req->uint1);
785     break;
786    
787     case REQ_DB_UPGRADE:
788     req->result = req->db->upgrade (req->db, req->buf1, req->uint1);
789     break;
790    
791     case REQ_DB_PUT:
792     req->result = req->db->put (req->db, req->txn, &req->dbt1, &req->dbt2, req->uint1);
793     break;
794    
795     #if DB_VERSION_MINOR >= 6
796     case REQ_DB_EXISTS:
797     req->result = req->db->exists (req->db, req->txn, &req->dbt1, req->uint1);
798     break;
799     #endif
800     case REQ_DB_GET:
801     req->result = req->db->get (req->db, req->txn, &req->dbt1, &req->dbt3, req->uint1);
802     break;
803    
804     case REQ_DB_PGET:
805     req->result = req->db->pget (req->db, req->txn, &req->dbt1, &req->dbt2, &req->dbt3, req->uint1);
806     break;
807    
808     case REQ_DB_DEL:
809     req->result = req->db->del (req->db, req->txn, &req->dbt1, req->uint1);
810     break;
811    
812     case REQ_DB_KEY_RANGE:
813     req->result = req->db->key_range (req->db, req->txn, &req->dbt1, &req->key_range, req->uint1);
814     break;
815    
816     case REQ_TXN_COMMIT:
817     req->result = req->txn->commit (req->txn, req->uint1);
818     break;
819    
820     case REQ_TXN_ABORT:
821     req->result = req->txn->abort (req->txn);
822     break;
823    
824     case REQ_TXN_FINISH:
825     if (req->txn->flags & TXN_DEADLOCK)
826     {
827     req->result = req->txn->abort (req->txn);
828     if (!req->result)
829     req->result = DB_LOCK_DEADLOCK;
830     }
831     else
832     req->result = req->txn->commit (req->txn, req->uint1);
833     break;
834    
835     case REQ_C_CLOSE:
836     req->result = req->dbc->c_close (req->dbc);
837     break;
838    
839     case REQ_C_COUNT:
840     {
841     db_recno_t recno;
842     req->result = req->dbc->c_count (req->dbc, &recno, req->uint1);
843     req->uv1 = recno;
844     }
845     break;
846    
847     case REQ_C_PUT:
848     req->result = req->dbc->c_put (req->dbc, &req->dbt1, &req->dbt2, req->uint1);
849     break;
850    
851     case REQ_C_GET:
852     req->result = req->dbc->c_get (req->dbc, &req->dbt1, &req->dbt3, req->uint1);
853     break;
854    
855     case REQ_C_PGET:
856     req->result = req->dbc->c_pget (req->dbc, &req->dbt1, &req->dbt2, &req->dbt3, req->uint1);
857     break;
858    
859     case REQ_C_DEL:
860     req->result = req->dbc->c_del (req->dbc, req->uint1);
861     break;
862    
863     #if DB_VERSION_MINOR >= 3
864     case REQ_SEQ_OPEN:
865     req->result = req->seq->open (req->seq, req->txn, &req->dbt1, req->uint1);
866     break;
867    
868     case REQ_SEQ_CLOSE:
869     req->result = req->seq->close (req->seq, req->uint1);
870     break;
871    
872     case REQ_SEQ_GET:
873     req->result = req->seq->get (req->seq, req->txn, req->int1, &req->seq_t, req->uint1);
874     break;
875    
876     case REQ_SEQ_REMOVE:
877     req->result = req->seq->remove (req->seq, req->txn, req->uint1);
878     break;
879     #endif
880    
881     default:
882     req->result = ENOSYS;
883     break;
884     }
885    
886     if (req->txn && (req->result > 0 || req->result == DB_LOCK_NOTGRANTED))
887     req->txn->flags |= TXN_DEADLOCK;
888     }
889    
890 root 1.13 X_THREAD_PROC (bdb_proc)
891 root 1.1 {
892 root 1.28 bdb_req req;
893 root 1.1 struct timespec ts;
894     worker *self = (worker *)thr_arg;
895    
896     /* try to distribute timeouts somewhat evenly */
897 root 1.13 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
898 root 1.1
899     for (;;)
900     {
901     ts.tv_sec = time (0) + IDLE_TIMEOUT;
902    
903 root 1.12 X_LOCK (reqlock);
904 root 1.1
905     for (;;)
906     {
907     self->req = req = reqq_shift (&req_queue);
908    
909     if (req)
910     break;
911    
912     ++idle;
913    
914 root 1.12 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts)
915 root 1.1 == ETIMEDOUT)
916     {
917     if (idle > max_idle)
918     {
919     --idle;
920 root 1.12 X_UNLOCK (reqlock);
921     X_LOCK (wrklock);
922 root 1.1 --started;
923 root 1.12 X_UNLOCK (wrklock);
924 root 1.1 goto quit;
925     }
926    
927     /* we are allowed to idle, so do so without any timeout */
928 root 1.12 X_COND_WAIT (reqwait, reqlock);
929 root 1.1 ts.tv_sec = time (0) + IDLE_TIMEOUT;
930     }
931    
932     --idle;
933     }
934    
935     --nready;
936    
937 root 1.12 X_UNLOCK (reqlock);
938 root 1.49
939 root 1.51 if (req->type == REQ_QUIT)
940 root 1.52 {
941     X_LOCK (reslock);
942     free (req);
943     self->req = 0;
944     X_UNLOCK (reslock);
945    
946     goto quit;
947     }
948 root 1.1
949 root 1.51 bdb_request (req);
950 root 1.17
951 root 1.12 X_LOCK (reslock);
952 root 1.1
953     ++npending;
954    
955     if (!reqq_push (&res_queue, req))
956     /* write a dummy byte to the pipe so fh becomes ready */
957 root 1.13 respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1);
958 root 1.1
959     self->req = 0;
960     worker_clear (self);
961    
962 root 1.12 X_UNLOCK (reslock);
963 root 1.1 }
964    
965     quit:
966 root 1.12 X_LOCK (wrklock);
967 root 1.1 worker_free (self);
968 root 1.12 X_UNLOCK (wrklock);
969 root 1.1
970     return 0;
971     }
972    
973     /*****************************************************************************/
974    
975     static void atfork_prepare (void)
976     {
977 root 1.12 X_LOCK (wrklock);
978     X_LOCK (reqlock);
979     X_LOCK (reslock);
980 root 1.1 }
981    
982     static void atfork_parent (void)
983     {
984 root 1.12 X_UNLOCK (reslock);
985     X_UNLOCK (reqlock);
986     X_UNLOCK (wrklock);
987 root 1.1 }
988    
989     static void atfork_child (void)
990     {
991 root 1.28 bdb_req prv;
992 root 1.1
993     while (prv = reqq_shift (&req_queue))
994     req_free (prv);
995    
996     while (prv = reqq_shift (&res_queue))
997     req_free (prv);
998    
999     while (wrk_first.next != &wrk_first)
1000     {
1001     worker *wrk = wrk_first.next;
1002    
1003     if (wrk->req)
1004     req_free (wrk->req);
1005    
1006     worker_clear (wrk);
1007     worker_free (wrk);
1008     }
1009    
1010     started = 0;
1011     idle = 0;
1012     nreqs = 0;
1013     nready = 0;
1014     npending = 0;
1015    
1016 root 1.19 create_respipe ();
1017 root 1.1
1018     atfork_parent ();
1019     }
1020    
1021 root 1.50 #define dREQ(reqtype,rsvcnt) \
1022 root 1.28 bdb_req req; \
1023 root 1.1 int req_pri = next_pri; \
1024     next_pri = DEFAULT_PRI + PRI_BIAS; \
1025     \
1026 root 1.41 if (callback && SvOK (callback)) \
1027     croak ("callback has illegal type or extra arguments"); \
1028 root 1.1 \
1029 root 1.28 Newz (0, req, 1, bdb_cb); \
1030 root 1.1 if (!req) \
1031 root 1.28 croak ("out of memory during bdb_req allocation"); \
1032 root 1.1 \
1033 root 1.50 req->callback = SvREFCNT_inc (cb); \
1034 root 1.2 req->type = (reqtype); \
1035 root 1.50 req->pri = req_pri; \
1036     if (rsvcnt >= 1) req->rsv1 = SvREFCNT_inc (ST (0)); \
1037     if (rsvcnt >= 2) req->rsv2 = SvREFCNT_inc (ST (1)); \
1038     (void)0;
1039 root 1.1
1040     #define REQ_SEND \
1041 root 1.2 req_send (req)
1042    
1043 root 1.30 #define SvPTR(var, arg, type, class, nullok) \
1044 root 1.5 if (!SvOK (arg)) \
1045     { \
1046 root 1.30 if (nullok != 1) \
1047 root 1.7 croak (# var " must be a " # class " object, not undef"); \
1048 root 1.5 \
1049     (var) = 0; \
1050     } \
1051     else if (sv_derived_from ((arg), # class)) \
1052     { \
1053     IV tmp = SvIV ((SV*) SvRV (arg)); \
1054     (var) = INT2PTR (type, tmp); \
1055 root 1.30 if (!var && nullok != 2) \
1056 root 1.7 croak (# var " is not a valid " # class " object anymore"); \
1057 root 1.5 } \
1058     else \
1059 root 1.42 croak (# var " is not of type " # class);
1060 root 1.3
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.50 dREQ (REQ_ENV_CLOSE, 1);
1556 root 1.2 req->env = env;
1557     req->uint1 = flags;
1558     REQ_SEND;
1559 root 1.5 ptr_nuke (ST (0));
1560 root 1.2 }
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.50 dREQ (REQ_DB_CLOSE, 1);
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 root 1.5 ptr_nuke (ST (0));
1685 root 1.2 }
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.41 db_key_range (DB *db, DB_TXN_ornull *txn, SV *key, SV *key_range, U32 flags = 0, SV *callback = 0)
1733     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.41 db_get (DB *db, DB_TXN_ornull *txn, SV *key, SV *data, U32 flags = 0, SV *callback = 0)
1781     PREINIT:
1782     CALLBACK
1783 root 1.5 CODE:
1784 root 1.27 if (SvREADONLY (data))
1785     croak ("can't modify read-only data scalar in db_get");
1786 root 1.28 {
1787 root 1.50 dREQ (REQ_DB_GET, 2);
1788 root 1.5 req->db = db;
1789     req->txn = txn;
1790 root 1.6 req->uint1 = flags;
1791 root 1.5 sv_to_dbt (&req->dbt1, key);
1792     req->dbt3.flags = DB_DBT_MALLOC;
1793 root 1.6 req->sv3 = SvREFCNT_inc (data); SvREADONLY_on (data);
1794 root 1.5 REQ_SEND;
1795     }
1796    
1797     void
1798 root 1.41 db_pget (DB *db, DB_TXN_ornull *txn, SV *key, SV *pkey, SV *data, U32 flags = 0, SV *callback = 0)
1799     PREINIT:
1800     CALLBACK
1801 root 1.5 CODE:
1802 root 1.27 if (SvREADONLY (data))
1803     croak ("can't modify read-only data scalar in db_pget");
1804 root 1.28 {
1805 root 1.50 dREQ (REQ_DB_PGET, 2);
1806 root 1.5 req->db = db;
1807     req->txn = txn;
1808 root 1.6 req->uint1 = flags;
1809 root 1.5 sv_to_dbt (&req->dbt1, key);
1810     sv_to_dbt (&req->dbt2, pkey);
1811     req->dbt3.flags = DB_DBT_MALLOC;
1812 root 1.6 req->sv3 = SvREFCNT_inc (data); SvREADONLY_on (data);
1813     REQ_SEND;
1814     }
1815    
1816     void
1817 root 1.41 db_del (DB *db, DB_TXN_ornull *txn, SV *key, U32 flags = 0, SV *callback = 0)
1818     PREINIT:
1819     CALLBACK
1820 root 1.6 CODE:
1821     {
1822 root 1.50 dREQ (REQ_DB_DEL, 2);
1823 root 1.6 req->db = db;
1824     req->txn = txn;
1825 root 1.5 req->uint1 = flags;
1826 root 1.6 sv_to_dbt (&req->dbt1, key);
1827 root 1.5 REQ_SEND;
1828     }
1829    
1830     void
1831 root 1.41 db_txn_commit (DB_TXN *txn, U32 flags = 0, SV *callback = 0)
1832     PREINIT:
1833     CALLBACK
1834 root 1.5 CODE:
1835     {
1836 root 1.50 dREQ (REQ_TXN_COMMIT, 1);
1837 root 1.5 req->txn = txn;
1838     req->uint1 = flags;
1839     REQ_SEND;
1840     ptr_nuke (ST (0));
1841     }
1842    
1843     void
1844 root 1.41 db_txn_abort (DB_TXN *txn, SV *callback = 0)
1845     PREINIT:
1846     CALLBACK
1847 root 1.5 CODE:
1848     {
1849 root 1.50 dREQ (REQ_TXN_ABORT, 1);
1850 root 1.5 req->txn = txn;
1851     REQ_SEND;
1852     ptr_nuke (ST (0));
1853     }
1854    
1855 root 1.6 void
1856 root 1.41 db_txn_finish (DB_TXN *txn, U32 flags = 0, SV *callback = 0)
1857     PREINIT:
1858     CALLBACK
1859 root 1.17 CODE:
1860     {
1861 root 1.50 dREQ (REQ_TXN_FINISH, 1);
1862 root 1.17 req->txn = txn;
1863     req->uint1 = flags;
1864     REQ_SEND;
1865     ptr_nuke (ST (0));
1866     }
1867    
1868     void
1869 root 1.41 db_c_close (DBC *dbc, SV *callback = 0)
1870     PREINIT:
1871     CALLBACK
1872 root 1.6 CODE:
1873     {
1874 root 1.50 dREQ (REQ_C_CLOSE, 1);
1875 root 1.6 req->dbc = dbc;
1876     REQ_SEND;
1877     ptr_nuke (ST (0));
1878     }
1879    
1880     void
1881 root 1.41 db_c_count (DBC *dbc, SV *count, U32 flags = 0, SV *callback = 0)
1882     PREINIT:
1883     CALLBACK
1884 root 1.6 CODE:
1885     {
1886 root 1.50 dREQ (REQ_C_COUNT, 1);
1887 root 1.6 req->dbc = dbc;
1888     req->sv1 = SvREFCNT_inc (count);
1889     REQ_SEND;
1890     }
1891    
1892     void
1893 root 1.41 db_c_put (DBC *dbc, SV *key, SV *data, U32 flags = 0, SV *callback = 0)
1894     PREINIT:
1895     CALLBACK
1896 root 1.6 CODE:
1897     {
1898 root 1.50 dREQ (REQ_C_PUT, 1);
1899 root 1.6 req->dbc = dbc;
1900     sv_to_dbt (&req->dbt1, key);
1901     sv_to_dbt (&req->dbt2, data);
1902     req->uint1 = flags;
1903     REQ_SEND;
1904     }
1905    
1906     void
1907 root 1.41 db_c_get (DBC *dbc, SV *key, SV *data, U32 flags = 0, SV *callback = 0)
1908     PREINIT:
1909     CALLBACK
1910 root 1.6 CODE:
1911     {
1912 root 1.50 dREQ (REQ_C_GET, 1);
1913 root 1.6 req->dbc = dbc;
1914     req->uint1 = flags;
1915     if ((flags & DB_SET) == DB_SET
1916     || (flags & DB_SET_RANGE) == DB_SET_RANGE)
1917     sv_to_dbt (&req->dbt1, key);
1918     else
1919     req->dbt1.flags = DB_DBT_MALLOC;
1920    
1921     req->sv1 = SvREFCNT_inc (key); SvREADONLY_on (key);
1922    
1923     if ((flags & DB_GET_BOTH) == DB_GET_BOTH
1924     || (flags & DB_GET_BOTH_RANGE) == DB_GET_BOTH_RANGE)
1925     sv_to_dbt (&req->dbt3, data);
1926     else
1927     req->dbt3.flags = DB_DBT_MALLOC;
1928    
1929     req->sv3 = SvREFCNT_inc (data); SvREADONLY_on (data);
1930     REQ_SEND;
1931     }
1932    
1933     void
1934 root 1.41 db_c_pget (DBC *dbc, SV *key, SV *pkey, SV *data, U32 flags = 0, SV *callback = 0)
1935     PREINIT:
1936     CALLBACK
1937 root 1.6 CODE:
1938     {
1939 root 1.50 dREQ (REQ_C_PGET, 1);
1940 root 1.6 req->dbc = dbc;
1941     req->uint1 = flags;
1942     if ((flags & DB_SET) == DB_SET
1943     || (flags & DB_SET_RANGE) == DB_SET_RANGE)
1944     sv_to_dbt (&req->dbt1, key);
1945     else
1946     req->dbt1.flags = DB_DBT_MALLOC;
1947    
1948     req->sv1 = SvREFCNT_inc (key); SvREADONLY_on (key);
1949    
1950     req->dbt2.flags = DB_DBT_MALLOC;
1951     req->sv2 = SvREFCNT_inc (pkey); SvREADONLY_on (pkey);
1952    
1953     if ((flags & DB_GET_BOTH) == DB_GET_BOTH
1954     || (flags & DB_GET_BOTH_RANGE) == DB_GET_BOTH_RANGE)
1955     sv_to_dbt (&req->dbt3, data);
1956     else
1957     req->dbt3.flags = DB_DBT_MALLOC;
1958    
1959     req->sv3 = SvREFCNT_inc (data); SvREADONLY_on (data);
1960     REQ_SEND;
1961     }
1962    
1963     void
1964 root 1.41 db_c_del (DBC *dbc, U32 flags = 0, SV *callback = 0)
1965     PREINIT:
1966     CALLBACK
1967 root 1.6 CODE:
1968     {
1969 root 1.50 dREQ (REQ_C_DEL, 1);
1970 root 1.6 req->dbc = dbc;
1971     req->uint1 = flags;
1972     REQ_SEND;
1973     }
1974    
1975 root 1.2
1976 root 1.47 #if DB_VERSION_MINOR >= 3
1977    
1978 root 1.8 void
1979 root 1.41 db_sequence_open (DB_SEQUENCE *seq, DB_TXN_ornull *txnid, SV *key, U32 flags = 0, SV *callback = 0)
1980     PREINIT:
1981     CALLBACK
1982 root 1.8 CODE:
1983     {
1984 root 1.50 dREQ (REQ_SEQ_OPEN, 2);
1985 root 1.8 req->seq = seq;
1986     req->txn = txnid;
1987     req->uint1 = flags | DB_THREAD;
1988     sv_to_dbt (&req->dbt1, key);
1989     REQ_SEND;
1990     }
1991    
1992     void
1993 root 1.41 db_sequence_close (DB_SEQUENCE *seq, U32 flags = 0, SV *callback = 0)
1994     PREINIT:
1995     CALLBACK
1996 root 1.8 CODE:
1997     {
1998 root 1.50 dREQ (REQ_SEQ_CLOSE, 1);
1999 root 1.8 req->seq = seq;
2000     req->uint1 = flags;
2001     REQ_SEND;
2002     ptr_nuke (ST (0));
2003     }
2004    
2005     void
2006 root 1.41 db_sequence_get (DB_SEQUENCE *seq, DB_TXN_ornull *txnid, int delta, SV *seq_value, U32 flags = DB_TXN_NOSYNC, SV *callback = 0)
2007     PREINIT:
2008     CALLBACK
2009 root 1.8 CODE:
2010     {
2011 root 1.50 dREQ (REQ_SEQ_GET, 2);
2012 root 1.8 req->seq = seq;
2013     req->txn = txnid;
2014     req->int1 = delta;
2015     req->uint1 = flags;
2016     req->sv1 = SvREFCNT_inc (seq_value); SvREADONLY_on (seq_value);
2017     REQ_SEND;
2018     }
2019    
2020     void
2021 root 1.41 db_sequence_remove (DB_SEQUENCE *seq, DB_TXN_ornull *txnid = 0, U32 flags = 0, SV *callback = 0)
2022     PREINIT:
2023     CALLBACK
2024 root 1.8 CODE:
2025     {
2026 root 1.50 dREQ (REQ_SEQ_REMOVE, 2);
2027 root 1.8 req->seq = seq;
2028     req->txn = txnid;
2029     req->uint1 = flags;
2030     REQ_SEND;
2031     }
2032    
2033 root 1.47 #endif
2034    
2035 root 1.8
2036 root 1.2 MODULE = BDB PACKAGE = BDB::Env
2037    
2038 root 1.5 void
2039 root 1.30 DESTROY (DB_ENV_ornuked *env)
2040 root 1.5 CODE:
2041     if (env)
2042     env->close (env, 0);
2043    
2044 root 1.8 int set_data_dir (DB_ENV *env, const char *dir)
2045     CODE:
2046     RETVAL = env->set_data_dir (env, dir);
2047     OUTPUT:
2048     RETVAL
2049    
2050     int set_tmp_dir (DB_ENV *env, const char *dir)
2051     CODE:
2052     RETVAL = env->set_tmp_dir (env, dir);
2053     OUTPUT:
2054     RETVAL
2055    
2056     int set_lg_dir (DB_ENV *env, const char *dir)
2057     CODE:
2058     RETVAL = env->set_lg_dir (env, dir);
2059     OUTPUT:
2060     RETVAL
2061    
2062     int set_shm_key (DB_ENV *env, long shm_key)
2063     CODE:
2064     RETVAL = env->set_shm_key (env, shm_key);
2065     OUTPUT:
2066     RETVAL
2067    
2068 root 1.2 int set_cachesize (DB_ENV *env, U32 gbytes, U32 bytes, int ncache = 0)
2069 root 1.3 CODE:
2070     RETVAL = env->set_cachesize (env, gbytes, bytes, ncache);
2071     OUTPUT:
2072     RETVAL
2073 root 1.2
2074 root 1.25 int set_flags (DB_ENV *env, U32 flags, int onoff = 1)
2075 root 1.3 CODE:
2076     RETVAL = env->set_flags (env, flags, onoff);
2077     OUTPUT:
2078     RETVAL
2079    
2080 root 1.39 #if DB_VERSION_MINOR >= 7
2081    
2082     int set_intermediate_dir_mode (DB_ENV *env, const char *mode)
2083     CODE:
2084     RETVAL = env->set_intermediate_dir_mode (env, mode);
2085     OUTPUT:
2086     RETVAL
2087    
2088     int log_set_config (DB_ENV *env, U32 flags, int onoff = 1)
2089     CODE:
2090     RETVAL = env->log_set_config (env, flags, onoff);
2091     OUTPUT:
2092     RETVAL
2093    
2094     #endif
2095    
2096    
2097 root 1.16 void set_errfile (DB_ENV *env, FILE *errfile = 0)
2098 root 1.14 CODE:
2099     env->set_errfile (env, errfile);
2100    
2101 root 1.16 void set_msgfile (DB_ENV *env, FILE *msgfile = 0)
2102 root 1.14 CODE:
2103     env->set_msgfile (env, msgfile);
2104    
2105 root 1.25 int set_verbose (DB_ENV *env, U32 which = -1, int onoff = 1)
2106 root 1.14 CODE:
2107     RETVAL = env->set_verbose (env, which, onoff);
2108     OUTPUT:
2109     RETVAL
2110    
2111 root 1.3 int set_encrypt (DB_ENV *env, const char *password, U32 flags = 0)
2112     CODE:
2113     RETVAL = env->set_encrypt (env, password, flags);
2114     OUTPUT:
2115     RETVAL
2116    
2117 root 1.17 int set_timeout (DB_ENV *env, NV timeout, U32 flags = DB_SET_TXN_TIMEOUT)
2118 root 1.3 CODE:
2119     RETVAL = env->set_timeout (env, timeout * 1000000, flags);
2120     OUTPUT:
2121     RETVAL
2122 root 1.2
2123 root 1.8 int set_mp_max_openfd (DB_ENV *env, int maxopenfd);
2124     CODE:
2125     RETVAL = env->set_mp_max_openfd (env, maxopenfd);
2126     OUTPUT:
2127     RETVAL
2128    
2129     int set_mp_max_write (DB_ENV *env, int maxwrite, int maxwrite_sleep);
2130     CODE:
2131     RETVAL = env->set_mp_max_write (env, maxwrite, maxwrite_sleep);
2132     OUTPUT:
2133     RETVAL
2134    
2135     int set_mp_mmapsize (DB_ENV *env, int mmapsize_mb)
2136     CODE:
2137     RETVAL = env->set_mp_mmapsize (env, ((size_t)mmapsize_mb) << 20);
2138     OUTPUT:
2139     RETVAL
2140    
2141 root 1.9 int set_lk_detect (DB_ENV *env, U32 detect = DB_LOCK_DEFAULT)
2142 root 1.8 CODE:
2143     RETVAL = env->set_lk_detect (env, detect);
2144     OUTPUT:
2145     RETVAL
2146    
2147     int set_lk_max_lockers (DB_ENV *env, U32 max)
2148     CODE:
2149     RETVAL = env->set_lk_max_lockers (env, max);
2150     OUTPUT:
2151     RETVAL
2152    
2153     int set_lk_max_locks (DB_ENV *env, U32 max)
2154     CODE:
2155     RETVAL = env->set_lk_max_locks (env, max);
2156     OUTPUT:
2157     RETVAL
2158    
2159     int set_lk_max_objects (DB_ENV *env, U32 max)
2160     CODE:
2161     RETVAL = env->set_lk_max_objects (env, max);
2162     OUTPUT:
2163     RETVAL
2164    
2165     int set_lg_bsize (DB_ENV *env, U32 max)
2166     CODE:
2167     RETVAL = env->set_lg_bsize (env, max);
2168     OUTPUT:
2169     RETVAL
2170    
2171     int set_lg_max (DB_ENV *env, U32 max)
2172     CODE:
2173     RETVAL = env->set_lg_max (env, max);
2174     OUTPUT:
2175     RETVAL
2176    
2177 root 1.47 #if DB_VERSION_MINOR >= 4
2178    
2179 root 1.22 int mutex_set_max (DB_ENV *env, U32 max)
2180     CODE:
2181     RETVAL = env->mutex_set_max (env, max);
2182     OUTPUT:
2183     RETVAL
2184    
2185     int mutex_set_increment (DB_ENV *env, U32 increment)
2186     CODE:
2187     RETVAL = env->mutex_set_increment (env, increment);
2188     OUTPUT:
2189     RETVAL
2190    
2191     int mutex_set_tas_spins (DB_ENV *env, U32 tas_spins)
2192     CODE:
2193     RETVAL = env->mutex_set_tas_spins (env, tas_spins);
2194     OUTPUT:
2195     RETVAL
2196    
2197     int mutex_set_align (DB_ENV *env, U32 align)
2198     CODE:
2199     RETVAL = env->mutex_set_align (env, align);
2200     OUTPUT:
2201     RETVAL
2202    
2203 root 1.47 #endif
2204    
2205 root 1.5 DB_TXN *
2206     txn_begin (DB_ENV *env, DB_TXN_ornull *parent = 0, U32 flags = 0)
2207     CODE:
2208     errno = env->txn_begin (env, parent, &RETVAL, flags);
2209     if (errno)
2210 root 1.6 croak ("DB_ENV->txn_begin: %s", db_strerror (errno));
2211 root 1.5 OUTPUT:
2212     RETVAL
2213 root 1.2
2214 root 1.46 #if DB_VERSION_MINOR >= 5
2215    
2216 root 1.44 DB_TXN *
2217     cdsgroup_begin (DB_ENV *env)
2218     CODE:
2219     errno = env->cdsgroup_begin (env, &RETVAL);
2220     if (errno)
2221     croak ("DB_ENV->cdsgroup_begin: %s", db_strerror (errno));
2222     OUTPUT:
2223     RETVAL
2224    
2225 root 1.46 #endif
2226    
2227 root 1.2 MODULE = BDB PACKAGE = BDB::Db
2228    
2229 root 1.5 void
2230 root 1.30 DESTROY (DB_ornuked *db)
2231 root 1.5 CODE:
2232     if (db)
2233     {
2234     SV *env = (SV *)db->app_private;
2235     db->close (db, 0);
2236     SvREFCNT_dec (env);
2237     }
2238    
2239 root 1.2 int set_cachesize (DB *db, U32 gbytes, U32 bytes, int ncache = 0)
2240 root 1.3 CODE:
2241     RETVAL = db->set_cachesize (db, gbytes, bytes, ncache);
2242     OUTPUT:
2243     RETVAL
2244 root 1.2
2245 root 1.16 int set_flags (DB *db, U32 flags)
2246 root 1.3 CODE:
2247     RETVAL = db->set_flags (db, flags);
2248     OUTPUT:
2249     RETVAL
2250 root 1.2
2251     int set_encrypt (DB *db, const char *password, U32 flags)
2252 root 1.3 CODE:
2253     RETVAL = db->set_encrypt (db, password, flags);
2254     OUTPUT:
2255     RETVAL
2256 root 1.2
2257     int set_lorder (DB *db, int lorder)
2258 root 1.3 CODE:
2259     RETVAL = db->set_lorder (db, lorder);
2260     OUTPUT:
2261     RETVAL
2262 root 1.2
2263     int set_bt_minkey (DB *db, U32 minkey)
2264 root 1.3 CODE:
2265     RETVAL = db->set_bt_minkey (db, minkey);
2266     OUTPUT:
2267     RETVAL
2268 root 1.2
2269 root 1.16 int set_re_delim (DB *db, int delim)
2270 root 1.3 CODE:
2271     RETVAL = db->set_re_delim (db, delim);
2272     OUTPUT:
2273     RETVAL
2274 root 1.2
2275     int set_re_pad (DB *db, int re_pad)
2276 root 1.3 CODE:
2277     RETVAL = db->set_re_pad (db, re_pad);
2278     OUTPUT:
2279     RETVAL
2280 root 1.2
2281     int set_re_source (DB *db, char *source)
2282 root 1.3 CODE:
2283     RETVAL = db->set_re_source (db, source);
2284     OUTPUT:
2285     RETVAL
2286 root 1.2
2287     int set_re_len (DB *db, U32 re_len)
2288 root 1.3 CODE:
2289     RETVAL = db->set_re_len (db, re_len);
2290     OUTPUT:
2291     RETVAL
2292 root 1.2
2293     int set_h_ffactor (DB *db, U32 h_ffactor)
2294 root 1.3 CODE:
2295     RETVAL = db->set_h_ffactor (db, h_ffactor);
2296     OUTPUT:
2297     RETVAL
2298 root 1.2
2299     int set_h_nelem (DB *db, U32 h_nelem)
2300 root 1.3 CODE:
2301     RETVAL = db->set_h_nelem (db, h_nelem);
2302     OUTPUT:
2303     RETVAL
2304 root 1.2
2305     int set_q_extentsize (DB *db, U32 extentsize)
2306 root 1.3 CODE:
2307     RETVAL = db->set_q_extentsize (db, extentsize);
2308     OUTPUT:
2309     RETVAL
2310 root 1.2
2311 root 1.6 DBC *
2312     cursor (DB *db, DB_TXN_ornull *txn = 0, U32 flags = 0)
2313     CODE:
2314     errno = db->cursor (db, txn, &RETVAL, flags);
2315     if (errno)
2316     croak ("DB->cursor: %s", db_strerror (errno));
2317     OUTPUT:
2318     RETVAL
2319    
2320 root 1.47 #if DB_VERSION_MINOR >= 3
2321    
2322 root 1.8 DB_SEQUENCE *
2323     sequence (DB *db, U32 flags = 0)
2324     CODE:
2325     {
2326     errno = db_sequence_create (&RETVAL, db, flags);
2327     if (errno)
2328     croak ("db_sequence_create: %s", db_strerror (errno));
2329     }
2330     OUTPUT:
2331     RETVAL
2332    
2333 root 1.47 #endif
2334    
2335 root 1.6
2336 root 1.5 MODULE = BDB PACKAGE = BDB::Txn
2337    
2338     void
2339 root 1.30 DESTROY (DB_TXN_ornuked *txn)
2340 root 1.5 CODE:
2341     if (txn)
2342     txn->abort (txn);
2343    
2344 root 1.17 int set_timeout (DB_TXN *txn, NV timeout, U32 flags = DB_SET_TXN_TIMEOUT)
2345 root 1.5 CODE:
2346     RETVAL = txn->set_timeout (txn, timeout * 1000000, flags);
2347     OUTPUT:
2348     RETVAL
2349    
2350 root 1.17 int failed (DB_TXN *txn)
2351     CODE:
2352     RETVAL = !!(txn->flags & TXN_DEADLOCK);
2353     OUTPUT:
2354     RETVAL
2355    
2356 root 1.1
2357 root 1.6 MODULE = BDB PACKAGE = BDB::Cursor
2358    
2359     void
2360 root 1.30 DESTROY (DBC_ornuked *dbc)
2361 root 1.6 CODE:
2362     if (dbc)
2363     dbc->c_close (dbc);
2364    
2365 root 1.27 #if DB_VERSION_MINOR >= 6
2366    
2367     int set_priority (DBC *dbc, int priority)
2368     CODE:
2369     dbc->set_priority (dbc, priority);
2370    
2371     #endif
2372    
2373 root 1.47 #if DB_VERSION_MINOR >= 3
2374    
2375 root 1.8 MODULE = BDB PACKAGE = BDB::Sequence
2376    
2377     void
2378 root 1.30 DESTROY (DB_SEQUENCE_ornuked *seq)
2379 root 1.8 CODE:
2380     if (seq)
2381     seq->close (seq, 0);
2382    
2383     int initial_value (DB_SEQUENCE *seq, db_seq_t value)
2384     CODE:
2385     RETVAL = seq->initial_value (seq, value);
2386     OUTPUT:
2387     RETVAL
2388    
2389     int set_cachesize (DB_SEQUENCE *seq, U32 size)
2390     CODE:
2391     RETVAL = seq->set_cachesize (seq, size);
2392     OUTPUT:
2393     RETVAL
2394    
2395     int set_flags (DB_SEQUENCE *seq, U32 flags)
2396     CODE:
2397     RETVAL = seq->set_flags (seq, flags);
2398     OUTPUT:
2399     RETVAL
2400    
2401     int set_range (DB_SEQUENCE *seq, db_seq_t min, db_seq_t max)
2402     CODE:
2403     RETVAL = seq->set_range (seq, min, max);
2404     OUTPUT:
2405     RETVAL
2406    
2407 root 1.47 #endif
2408    
2409 root 1.31