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