ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/BDB/BDB.xs
Revision: 1.47
Committed: Fri Jul 18 22:39:10 2008 UTC (15 years, 10 months ago) by root
Branch: MAIN
Changes since 1.46: +61 -25 lines
Log Message:
*** empty log message ***

File Contents

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