ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/BDB/BDB.xs
Revision: 1.74
Committed: Wed Mar 31 00:44:51 2010 UTC (14 years, 1 month ago) by root
Branch: MAIN
CVS Tags: rel-1_88
Changes since 1.73: +5 -5 lines
Log Message:
1.88

File Contents

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