ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/BDB/BDB.xs
Revision: 1.54
Committed: Fri Sep 26 02:03:48 2008 UTC (15 years, 7 months ago) by root
Branch: MAIN
Changes since 1.53: +65 -50 lines
Log Message:
*** empty log message ***

File Contents

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