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