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.48 by root, Tue Jul 29 03:33:16 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines