ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/BDB/BDB.xs
Revision: 1.59
Committed: Mon Oct 20 02:31:51 2008 UTC (15 years, 7 months ago) by root
Branch: MAIN
Changes since 1.58: +8 -1 lines
Log Message:
*** empty log message ***

File Contents

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