ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/BDB/BDB.xs
Revision: 1.48
Committed: Tue Jul 29 03:33:16 2008 UTC (15 years, 9 months ago) by root
Branch: MAIN
CVS Tags: rel-1_71
Changes since 1.47: +2 -0 lines
Log Message:
1.71

File Contents

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