ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/BDB/BDB.xs
Revision: 1.64
Committed: Wed Nov 12 16:52:51 2008 UTC (15 years, 6 months ago) by root
Branch: MAIN
Changes since 1.63: +41 -0 lines
Log Message:
*** empty log message ***

File Contents

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