ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/BDB/BDB.xs
(Generate patch)

Comparing BDB/BDB.xs (file contents):
Revision 1.11 by root, Wed May 9 06:42:24 2007 UTC vs.
Revision 1.49 by root, Thu Sep 25 12:28:49 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines