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