ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/BDB/BDB.xs
Revision: 1.67
Committed: Fri Jan 9 22:32:12 2009 UTC (15 years, 4 months ago) by root
Branch: MAIN
CVS Tags: rel-1_83
Changes since 1.66: +27 -2 lines
Log Message:
*** empty log message ***

File Contents

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