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

Comparing BDB/BDB.xs (file contents):
Revision 1.16 by root, Mon Aug 13 12:01:45 2007 UTC vs.
Revision 1.34 by root, Sun Mar 30 06:28:08 2008 UTC

40typedef DB_TXN DB_TXN_ornull; 40typedef DB_TXN DB_TXN_ornull;
41typedef DBC DBC_ornull; 41typedef DBC DBC_ornull;
42typedef DB DB_ornull; 42typedef DB DB_ornull;
43typedef DB_SEQUENCE DB_SEQUENCE_ornull; 43typedef DB_SEQUENCE DB_SEQUENCE_ornull;
44 44
45typedef DB_ENV DB_ENV_ornuked;
46typedef DB_TXN DB_TXN_ornuked;
47typedef DBC DBC_ornuked;
48typedef DB DB_ornuked;
49typedef DB_SEQUENCE DB_SEQUENCE_ornuked;
50
45typedef SV SV8; /* byte-sv, used for argument-checking */ 51typedef SV SV8; /* byte-sv, used for argument-checking */
46typedef char *octetstring; 52typedef char *bdb_filename;
47 53
48static SV *prepare_cb; 54static SV *prepare_cb;
49 55
50#if DB_VERSION_MINOR >= 6 56#if DB_VERSION_MINOR >= 6
51# define c_close close 57# define c_close close
55# define c_get get 61# define c_get get
56# define c_pget pget 62# define c_pget pget
57# define c_put put 63# define c_put put
58#endif 64#endif
59 65
66static char *
67get_bdb_filename (SV *sv)
68{
69 return !SvOK (sv)
70 ? 0
71 :
72#if WIN32
73 SvPVutf8_nolen (sv)
74#else
75 SvPVbyte_nolen (sv)
76#endif
77 ;
78}
79
60static void 80static void
61debug_errcall (const DB_ENV *dbenv, const char *errpfx, const char *msg) 81debug_errcall (const DB_ENV *dbenv, const char *errpfx, const char *msg)
62{ 82{
63 printf ("err[%s]\n", msg); 83 printf ("err[%s]\n", msg);
64} 84}
102 122
103enum { 123enum {
104 REQ_QUIT, 124 REQ_QUIT,
105 REQ_ENV_OPEN, REQ_ENV_CLOSE, REQ_ENV_TXN_CHECKPOINT, REQ_ENV_LOCK_DETECT, 125 REQ_ENV_OPEN, REQ_ENV_CLOSE, REQ_ENV_TXN_CHECKPOINT, REQ_ENV_LOCK_DETECT,
106 REQ_ENV_MEMP_SYNC, REQ_ENV_MEMP_TRICKLE, 126 REQ_ENV_MEMP_SYNC, REQ_ENV_MEMP_TRICKLE,
107 REQ_DB_OPEN, REQ_DB_CLOSE, REQ_DB_COMPACT, REQ_DB_SYNC, 127 REQ_DB_OPEN, REQ_DB_CLOSE, REQ_DB_COMPACT, REQ_DB_SYNC, REQ_DB_UPGRADE,
108 REQ_DB_PUT, REQ_DB_GET, REQ_DB_PGET, REQ_DB_DEL, REQ_DB_KEY_RANGE, 128 REQ_DB_PUT, REQ_DB_GET, REQ_DB_PGET, REQ_DB_DEL, REQ_DB_KEY_RANGE,
109 REQ_TXN_COMMIT, REQ_TXN_ABORT, 129 REQ_TXN_COMMIT, REQ_TXN_ABORT, REQ_TXN_FINISH,
110 REQ_C_CLOSE, REQ_C_COUNT, REQ_C_PUT, REQ_C_GET, REQ_C_PGET, REQ_C_DEL, 130 REQ_C_CLOSE, REQ_C_COUNT, REQ_C_PUT, REQ_C_GET, REQ_C_PGET, REQ_C_DEL,
111 REQ_SEQ_OPEN, REQ_SEQ_CLOSE, REQ_SEQ_GET, REQ_SEQ_REMOVE, 131 REQ_SEQ_OPEN, REQ_SEQ_CLOSE, REQ_SEQ_GET, REQ_SEQ_REMOVE,
112}; 132};
113 133
114typedef struct aio_cb 134typedef struct bdb_cb
115{ 135{
116 struct aio_cb *volatile next; 136 struct bdb_cb *volatile next;
117 SV *callback; 137 SV *callback;
118 int type, pri, result; 138 int type, pri, result;
119 139
120 DB_ENV *env; 140 DB_ENV *env;
121 DB *db; 141 DB *db;
130 150
131 DBT dbt1, dbt2, dbt3; 151 DBT dbt1, dbt2, dbt3;
132 DB_KEY_RANGE key_range; 152 DB_KEY_RANGE key_range;
133 DB_SEQUENCE *seq; 153 DB_SEQUENCE *seq;
134 db_seq_t seq_t; 154 db_seq_t seq_t;
135} aio_cb; 155} bdb_cb;
136 156
137typedef aio_cb *aio_req; 157typedef bdb_cb *bdb_req;
138 158
139enum { 159enum {
140 PRI_MIN = -4, 160 PRI_MIN = -4,
141 PRI_MAX = 4, 161 PRI_MAX = 4,
142 162
169 struct worker *prev, *next; 189 struct worker *prev, *next;
170 190
171 thread_t tid; 191 thread_t tid;
172 192
173 /* locked by reslock, reqlock or wrklock */ 193 /* locked by reslock, reqlock or wrklock */
174 aio_req req; /* currently processed request */ 194 bdb_req req; /* currently processed request */
175 void *dbuf; 195 void *dbuf;
176 DIR *dirp; 196 DIR *dirp;
177} worker; 197} worker;
178 198
179static worker wrk_first = { &wrk_first, &wrk_first, 0 }; 199static worker wrk_first = { &wrk_first, &wrk_first, 0 };
191} 211}
192 212
193static volatile unsigned int nreqs, nready, npending; 213static volatile unsigned int nreqs, nready, npending;
194static volatile unsigned int max_idle = 4; 214static volatile unsigned int max_idle = 4;
195static volatile unsigned int max_outstanding = 0xffffffff; 215static volatile unsigned int max_outstanding = 0xffffffff;
196static int respipe [2], respipe_osf [2]; 216static int respipe_osf [2], respipe [2] = { -1, -1 };
197 217
198static mutex_t reslock = X_MUTEX_INIT; 218static mutex_t reslock = X_MUTEX_INIT;
199static mutex_t reqlock = X_MUTEX_INIT; 219static mutex_t reqlock = X_MUTEX_INIT;
200static cond_t reqwait = X_COND_INIT; 220static cond_t reqwait = X_COND_INIT;
201 221
202#if WORDACCESS_UNSAFE 222#if WORDACCESS_UNSAFE
203 223
204static unsigned int get_nready () 224static unsigned int get_nready (void)
205{ 225{
206 unsigned int retval; 226 unsigned int retval;
207 227
208 X_LOCK (reqlock); 228 X_LOCK (reqlock);
209 retval = nready; 229 retval = nready;
210 X_UNLOCK (reqlock); 230 X_UNLOCK (reqlock);
211 231
212 return retval; 232 return retval;
213} 233}
214 234
215static unsigned int get_npending () 235static unsigned int get_npending (void)
216{ 236{
217 unsigned int retval; 237 unsigned int retval;
218 238
219 X_LOCK (reslock); 239 X_LOCK (reslock);
220 retval = npending; 240 retval = npending;
221 X_UNLOCK (reslock); 241 X_UNLOCK (reslock);
222 242
223 return retval; 243 return retval;
224} 244}
225 245
226static unsigned int get_nthreads () 246static unsigned int get_nthreads (void)
227{ 247{
228 unsigned int retval; 248 unsigned int retval;
229 249
230 X_LOCK (wrklock); 250 X_LOCK (wrklock);
231 retval = started; 251 retval = started;
246 * a somewhat faster data structure might be nice, but 266 * a somewhat faster data structure might be nice, but
247 * with 8 priorities this actually needs <20 insns 267 * with 8 priorities this actually needs <20 insns
248 * per shift, the most expensive operation. 268 * per shift, the most expensive operation.
249 */ 269 */
250typedef struct { 270typedef struct {
251 aio_req qs[NUM_PRI], qe[NUM_PRI]; /* qstart, qend */ 271 bdb_req qs[NUM_PRI], qe[NUM_PRI]; /* qstart, qend */
252 int size; 272 int size;
253} reqq; 273} reqq;
254 274
255static reqq req_queue; 275static reqq req_queue;
256static reqq res_queue; 276static reqq res_queue;
257 277
258int reqq_push (reqq *q, aio_req req) 278int reqq_push (reqq *q, bdb_req req)
259{ 279{
260 int pri = req->pri; 280 int pri = req->pri;
261 req->next = 0; 281 req->next = 0;
262 282
263 if (q->qe[pri]) 283 if (q->qe[pri])
269 q->qe[pri] = q->qs[pri] = req; 289 q->qe[pri] = q->qs[pri] = req;
270 290
271 return q->size++; 291 return q->size++;
272} 292}
273 293
274aio_req reqq_shift (reqq *q) 294bdb_req reqq_shift (reqq *q)
275{ 295{
276 int pri; 296 int pri;
277 297
278 if (!q->size) 298 if (!q->size)
279 return 0; 299 return 0;
280 300
281 --q->size; 301 --q->size;
282 302
283 for (pri = NUM_PRI; pri--; ) 303 for (pri = NUM_PRI; pri--; )
284 { 304 {
285 aio_req req = q->qs[pri]; 305 bdb_req req = q->qs[pri];
286 306
287 if (req) 307 if (req)
288 { 308 {
289 if (!(q->qs[pri] = req->next)) 309 if (!(q->qs[pri] = req->next))
290 q->qe[pri] = 0; 310 q->qe[pri] = 0;
294 } 314 }
295 315
296 abort (); 316 abort ();
297} 317}
298 318
299static int poll_cb (); 319static int poll_cb (void);
300static void req_free (aio_req req); 320static void req_free (bdb_req req);
301static void req_cancel (aio_req req); 321static void req_cancel (bdb_req req);
302 322
303static int req_invoke (aio_req req) 323static int req_invoke (bdb_req req)
304{ 324{
305 dSP; 325 dSP;
306 326
307 if (SvOK (req->callback)) 327 if (SvOK (req->callback))
308 { 328 {
326 dbt_to_sv (req->sv1, &req->dbt1); 346 dbt_to_sv (req->sv1, &req->dbt1);
327 dbt_to_sv (req->sv2, &req->dbt2); 347 dbt_to_sv (req->sv2, &req->dbt2);
328 dbt_to_sv (req->sv3, &req->dbt3); 348 dbt_to_sv (req->sv3, &req->dbt3);
329 break; 349 break;
330 350
351 case REQ_DB_PUT:
352 case REQ_C_PUT:
353 dbt_to_sv (0, &req->dbt1);
354 dbt_to_sv (0, &req->dbt2);
355 break;
356
331 case REQ_DB_KEY_RANGE: 357 case REQ_DB_KEY_RANGE:
332 { 358 {
333 AV *av = newAV (); 359 AV *av = newAV ();
334 360
335 av_push (av, newSVnv (req->key_range.less)); 361 av_push (av, newSVnv (req->key_range.less));
344 370
345 case REQ_SEQ_GET: 371 case REQ_SEQ_GET:
346 SvREADONLY_off (req->sv1); 372 SvREADONLY_off (req->sv1);
347 373
348 if (sizeof (IV) > 4) 374 if (sizeof (IV) > 4)
349 sv_setiv_mg (req->sv1, req->seq_t); 375 sv_setiv_mg (req->sv1, (IV)req->seq_t);
350 else 376 else
351 sv_setnv_mg (req->sv1, req->seq_t); 377 sv_setnv_mg (req->sv1, (NV)req->seq_t);
352 378
353 SvREFCNT_dec (req->sv1); 379 SvREFCNT_dec (req->sv1);
354 break; 380 break;
355 } 381 }
356 382
365 } 391 }
366 392
367 return !SvTRUE (ERRSV); 393 return !SvTRUE (ERRSV);
368} 394}
369 395
370static void req_free (aio_req req) 396static void req_free (bdb_req req)
371{ 397{
372 free (req->buf1); 398 free (req->buf1);
373 free (req->buf2); 399 free (req->buf2);
374 Safefree (req); 400 Safefree (req);
375} 401}
379#else 405#else
380# define TO_SOCKET(x) (x) 406# define TO_SOCKET(x) (x)
381#endif 407#endif
382 408
383static void 409static void
384create_pipe (int fd[2]) 410create_respipe (void)
385{ 411{
386#ifdef _WIN32 412#ifdef _WIN32
387 int arg = 1; 413 int arg; /* argg */
414#endif
415 int old_readfd = respipe [0];
416
417 if (respipe [1] >= 0)
418 respipe_close (TO_SOCKET (respipe [1]));
419
420#ifdef _WIN32
388 if (PerlSock_socketpair (AF_UNIX, SOCK_STREAM, 0, fd) 421 if (PerlSock_socketpair (AF_UNIX, SOCK_STREAM, 0, respipe))
389 || ioctlsocket (TO_SOCKET (fd [0]), FIONBIO, &arg)
390 || ioctlsocket (TO_SOCKET (fd [1]), FIONBIO, &arg))
391#else 422#else
392 if (pipe (fd) 423 if (pipe (respipe))
393 || fcntl (fd [0], F_SETFL, O_NONBLOCK)
394 || fcntl (fd [1], F_SETFL, O_NONBLOCK))
395#endif 424#endif
396 croak ("unable to initialize result pipe"); 425 croak ("unable to initialize result pipe");
426
427 if (old_readfd >= 0)
428 {
429 if (dup2 (TO_SOCKET (respipe [0]), TO_SOCKET (old_readfd)) < 0)
430 croak ("unable to initialize result pipe(2)");
431
432 respipe_close (respipe [0]);
433 respipe [0] = old_readfd;
434 }
435
436#ifdef _WIN32
437 arg = 1;
438 if (ioctlsocket (TO_SOCKET (respipe [0]), FIONBIO, &arg)
439 || ioctlsocket (TO_SOCKET (respipe [1]), FIONBIO, &arg))
440#else
441 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)
442 || fcntl (respipe [1], F_SETFL, O_NONBLOCK))
443#endif
444 croak ("unable to initialize result pipe(3)");
397 445
398 respipe_osf [0] = TO_SOCKET (respipe [0]); 446 respipe_osf [0] = TO_SOCKET (respipe [0]);
399 respipe_osf [1] = TO_SOCKET (respipe [1]); 447 respipe_osf [1] = TO_SOCKET (respipe [1]);
400} 448}
401 449
421 free (wrk); 469 free (wrk);
422 470
423 X_UNLOCK (wrklock); 471 X_UNLOCK (wrklock);
424} 472}
425 473
426static void maybe_start_thread () 474static void maybe_start_thread (void)
427{ 475{
428 if (get_nthreads () >= wanted) 476 if (get_nthreads () >= wanted)
429 return; 477 return;
430 478
431 /* todo: maybe use idle here, but might be less exact */ 479 /* todo: maybe use idle here, but might be less exact */
433 return; 481 return;
434 482
435 start_thread (); 483 start_thread ();
436} 484}
437 485
438static void req_send (aio_req req) 486static void req_send (bdb_req req)
439{ 487{
440 SV *wait_callback = 0; 488 SV *wait_callback = 0;
441 489
442 // synthesize callback if none given 490 // synthesize callback if none given
443 if (!SvOK (req->callback)) 491 if (!SvOK (req->callback))
451 SPAGAIN; 499 SPAGAIN;
452 500
453 if (count != 2) 501 if (count != 2)
454 croak ("prepare callback must return exactly two values\n"); 502 croak ("prepare callback must return exactly two values\n");
455 503
456 wait_callback = SvREFCNT_inc (POPs); 504 wait_callback = POPs;
457 SvREFCNT_dec (req->callback); 505 SvREFCNT_dec (req->callback);
458 req->callback = SvREFCNT_inc (POPs); 506 req->callback = SvREFCNT_inc (POPs);
459 } 507 }
460 508
461 ++nreqs; 509 ++nreqs;
472 { 520 {
473 dSP; 521 dSP;
474 PUSHMARK (SP); 522 PUSHMARK (SP);
475 PUTBACK; 523 PUTBACK;
476 call_sv (wait_callback, G_DISCARD); 524 call_sv (wait_callback, G_DISCARD);
477 SvREFCNT_dec (wait_callback);
478 } 525 }
479} 526}
480 527
481static void end_thread (void) 528static void end_thread (void)
482{ 529{
483 aio_req req; 530 bdb_req req;
484 531
485 Newz (0, req, 1, aio_cb); 532 Newz (0, req, 1, bdb_cb);
486 533
487 req->type = REQ_QUIT; 534 req->type = REQ_QUIT;
488 req->pri = PRI_MAX + PRI_BIAS; 535 req->pri = PRI_MAX + PRI_BIAS;
489 536
490 X_LOCK (reqlock); 537 X_LOCK (reqlock);
517 564
518 while (started > wanted) 565 while (started > wanted)
519 end_thread (); 566 end_thread ();
520} 567}
521 568
522static void poll_wait () 569static void poll_wait (void)
523{ 570{
524 fd_set rfd; 571 fd_set rfd;
525 572
526 while (nreqs) 573 while (nreqs)
527 { 574 {
540 587
541 PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0); 588 PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0);
542 } 589 }
543} 590}
544 591
545static int poll_cb () 592static int poll_cb (void)
546{ 593{
547 dSP; 594 dSP;
548 int count = 0; 595 int count = 0;
549 int maxreqs = max_poll_reqs; 596 int maxreqs = max_poll_reqs;
550 int do_croak = 0; 597 int do_croak = 0;
551 struct timeval tv_start, tv_now; 598 struct timeval tv_start, tv_now;
552 aio_req req; 599 bdb_req req;
553 600
554 if (max_poll_time) 601 if (max_poll_time)
555 gettimeofday (&tv_start, 0); 602 gettimeofday (&tv_start, 0);
556 603
557 for (;;) 604 for (;;)
618 665
619/*****************************************************************************/ 666/*****************************************************************************/
620 667
621X_THREAD_PROC (bdb_proc) 668X_THREAD_PROC (bdb_proc)
622{ 669{
623 aio_req req; 670 bdb_req req;
624 struct timespec ts; 671 struct timespec ts;
625 worker *self = (worker *)thr_arg; 672 worker *self = (worker *)thr_arg;
626 673
627 /* try to distribute timeouts somewhat evenly */ 674 /* try to distribute timeouts somewhat evenly */
628 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); 675 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
668 X_UNLOCK (reqlock); 715 X_UNLOCK (reqlock);
669 716
670 switch (req->type) 717 switch (req->type)
671 { 718 {
672 case REQ_QUIT: 719 case REQ_QUIT:
720 req->result = ENOSYS;
673 goto quit; 721 goto quit;
674 722
675 case REQ_ENV_OPEN: 723 case REQ_ENV_OPEN:
676 req->result = req->env->open (req->env, req->buf1, req->uint1, req->int1); 724 req->result = req->env->open (req->env, req->buf1, req->uint1, req->int1);
677 break; 725 break;
710 758
711 case REQ_DB_SYNC: 759 case REQ_DB_SYNC:
712 req->result = req->db->sync (req->db, req->uint1); 760 req->result = req->db->sync (req->db, req->uint1);
713 break; 761 break;
714 762
763 case REQ_DB_UPGRADE:
764 req->result = req->db->upgrade (req->db, req->buf1, req->uint1);
765 break;
766
715 case REQ_DB_PUT: 767 case REQ_DB_PUT:
716 req->result = req->db->put (req->db, req->txn, &req->dbt1, &req->dbt2, req->uint1); 768 req->result = req->db->put (req->db, req->txn, &req->dbt1, &req->dbt2, req->uint1);
717 break; 769 break;
718 770
719 case REQ_DB_GET: 771 case REQ_DB_GET:
736 req->result = req->txn->commit (req->txn, req->uint1); 788 req->result = req->txn->commit (req->txn, req->uint1);
737 break; 789 break;
738 790
739 case REQ_TXN_ABORT: 791 case REQ_TXN_ABORT:
740 req->result = req->txn->abort (req->txn); 792 req->result = req->txn->abort (req->txn);
793 break;
794
795 case REQ_TXN_FINISH:
796 if (req->txn->flags & TXN_DEADLOCK)
797 {
798 req->result = req->txn->abort (req->txn);
799 if (!req->result)
800 req->result = DB_LOCK_DEADLOCK;
801 }
802 else
803 req->result = req->txn->commit (req->txn, req->uint1);
741 break; 804 break;
742 805
743 case REQ_C_CLOSE: 806 case REQ_C_CLOSE:
744 req->result = req->dbc->c_close (req->dbc); 807 req->result = req->dbc->c_close (req->dbc);
745 break; 808 break;
787 default: 850 default:
788 req->result = ENOSYS; 851 req->result = ENOSYS;
789 break; 852 break;
790 } 853 }
791 854
855 if (req->txn && (req->result > 0 || req->result == DB_LOCK_NOTGRANTED))
856 req->txn->flags |= TXN_DEADLOCK;
857
792 X_LOCK (reslock); 858 X_LOCK (reslock);
793 859
794 ++npending; 860 ++npending;
795 861
796 if (!reqq_push (&res_queue, req)) 862 if (!reqq_push (&res_queue, req))
827 X_UNLOCK (wrklock); 893 X_UNLOCK (wrklock);
828} 894}
829 895
830static void atfork_child (void) 896static void atfork_child (void)
831{ 897{
832 aio_req prv; 898 bdb_req prv;
833 899
834 while (prv = reqq_shift (&req_queue)) 900 while (prv = reqq_shift (&req_queue))
835 req_free (prv); 901 req_free (prv);
836 902
837 while (prv = reqq_shift (&res_queue)) 903 while (prv = reqq_shift (&res_queue))
852 idle = 0; 918 idle = 0;
853 nreqs = 0; 919 nreqs = 0;
854 nready = 0; 920 nready = 0;
855 npending = 0; 921 npending = 0;
856 922
857 respipe_close (respipe [0]);
858 respipe_close (respipe [1]);
859
860 create_pipe (respipe); 923 create_respipe ();
861 924
862 atfork_parent (); 925 atfork_parent ();
863} 926}
864 927
865#define dREQ(reqtype) \ 928#define dREQ(reqtype) \
866 aio_req req; \ 929 bdb_req req; \
867 int req_pri = next_pri; \ 930 int req_pri = next_pri; \
868 next_pri = DEFAULT_PRI + PRI_BIAS; \ 931 next_pri = DEFAULT_PRI + PRI_BIAS; \
869 \ 932 \
870 if (SvOK (callback) && !SvROK (callback)) \ 933 if (SvOK (callback) && !SvROK (callback)) \
871 croak ("callback must be undef or of reference type"); \ 934 croak ("callback must be undef or of reference type"); \
872 \ 935 \
873 Newz (0, req, 1, aio_cb); \ 936 Newz (0, req, 1, bdb_cb); \
874 if (!req) \ 937 if (!req) \
875 croak ("out of memory during aio_req allocation"); \ 938 croak ("out of memory during bdb_req allocation"); \
876 \ 939 \
877 req->callback = newSVsv (callback); \ 940 req->callback = newSVsv (callback); \
878 req->type = (reqtype); \ 941 req->type = (reqtype); \
879 req->pri = req_pri 942 req->pri = req_pri
880 943
881#define REQ_SEND \ 944#define REQ_SEND \
882 req_send (req) 945 req_send (req)
883 946
884#define SvPTR(var, arg, type, class, nullok) \ 947#define SvPTR(var, arg, type, class, nullok) \
885 if (!SvOK (arg)) \ 948 if (!SvOK (arg)) \
886 { \ 949 { \
887 if (!nullok) \ 950 if (nullok != 1) \
888 croak (# var " must be a " # class " object, not undef"); \ 951 croak (# var " must be a " # class " object, not undef"); \
889 \ 952 \
890 (var) = 0; \ 953 (var) = 0; \
891 } \ 954 } \
892 else if (sv_derived_from ((arg), # class)) \ 955 else if (sv_derived_from ((arg), # class)) \
893 { \ 956 { \
894 IV tmp = SvIV ((SV*) SvRV (arg)); \ 957 IV tmp = SvIV ((SV*) SvRV (arg)); \
895 (var) = INT2PTR (type, tmp); \ 958 (var) = INT2PTR (type, tmp); \
896 if (!var) \ 959 if (!var && nullok != 2) \
897 croak (# var " is not a valid " # class " object anymore"); \ 960 croak (# var " is not a valid " # class " object anymore"); \
898 } \ 961 } \
899 else \ 962 else \
900 croak (# var " is not of type " # class); \ 963 croak (# var " is not of type " # class); \
901 \ 964 \
903static void 966static void
904ptr_nuke (SV *sv) 967ptr_nuke (SV *sv)
905{ 968{
906 assert (SvROK (sv)); 969 assert (SvROK (sv));
907 sv_setiv (SvRV (sv), 0); 970 sv_setiv (SvRV (sv), 0);
971}
972
973static int
974errno_get (pTHX_ SV *sv, MAGIC *mg)
975{
976 if (*mg->mg_ptr == '!') // should always be the case
977 if (-30999 <= errno && errno <= -30800)
978 {
979 sv_setnv (sv, (NV)errno);
980 sv_setpv (sv, db_strerror (errno));
981 SvNOK_on (sv); /* what a wonderful hack! */
982 // ^^^ copied from perl sources
983 return 0;
984 }
985
986 return PL_vtbl_sv.svt_get (aTHX_ sv, mg);
987}
988
989static MGVTBL vtbl_errno;
990
991// this wonderful hack :( patches perl's $! variable to support our errno values
992static void
993patch_errno (void)
994{
995 SV *sv;
996 MAGIC *mg;
997
998 if (!(sv = get_sv ("!", 1)))
999 return;
1000
1001 if (!(mg = mg_find (sv, PERL_MAGIC_sv)))
1002 return;
1003
1004 if (mg->mg_virtual != &PL_vtbl_sv)
1005 return;
1006
1007 vtbl_errno = PL_vtbl_sv;
1008 vtbl_errno.svt_get = errno_get;
1009 mg->mg_virtual = &vtbl_errno;
908} 1010}
909 1011
910MODULE = BDB PACKAGE = BDB 1012MODULE = BDB PACKAGE = BDB
911 1013
912PROTOTYPES: ENABLE 1014PROTOTYPES: ENABLE
929 const_iv (INIT_TXN) 1031 const_iv (INIT_TXN)
930 const_iv (RECOVER) 1032 const_iv (RECOVER)
931 const_iv (INIT_TXN) 1033 const_iv (INIT_TXN)
932 const_iv (RECOVER_FATAL) 1034 const_iv (RECOVER_FATAL)
933 const_iv (CREATE) 1035 const_iv (CREATE)
1036 const_iv (RDONLY)
934 const_iv (USE_ENVIRON) 1037 const_iv (USE_ENVIRON)
935 const_iv (USE_ENVIRON_ROOT) 1038 const_iv (USE_ENVIRON_ROOT)
936 const_iv (LOCKDOWN) 1039 const_iv (LOCKDOWN)
937 const_iv (PRIVATE) 1040 const_iv (PRIVATE)
938 const_iv (REGISTER) 1041 const_iv (REGISTER)
951 const_iv (OVERWRITE) 1054 const_iv (OVERWRITE)
952 const_iv (PANIC_ENVIRONMENT) 1055 const_iv (PANIC_ENVIRONMENT)
953 const_iv (REGION_INIT) 1056 const_iv (REGION_INIT)
954 const_iv (TIME_NOTGRANTED) 1057 const_iv (TIME_NOTGRANTED)
955 const_iv (TXN_NOSYNC) 1058 const_iv (TXN_NOSYNC)
1059 const_iv (TXN_NOT_DURABLE)
956 const_iv (TXN_WRITE_NOSYNC) 1060 const_iv (TXN_WRITE_NOSYNC)
957 const_iv (WRITECURSOR) 1061 const_iv (WRITECURSOR)
958 const_iv (YIELDCPU) 1062 const_iv (YIELDCPU)
959 const_iv (ENCRYPT_AES) 1063 const_iv (ENCRYPT_AES)
960 const_iv (XA_CREATE) 1064 const_iv (XA_CREATE)
968 const_iv (READ_UNCOMMITTED) 1072 const_iv (READ_UNCOMMITTED)
969 const_iv (TRUNCATE) 1073 const_iv (TRUNCATE)
970 const_iv (NOSYNC) 1074 const_iv (NOSYNC)
971 const_iv (CHKSUM) 1075 const_iv (CHKSUM)
972 const_iv (ENCRYPT) 1076 const_iv (ENCRYPT)
973 const_iv (TXN_NOT_DURABLE)
974 const_iv (DUP) 1077 const_iv (DUP)
975 const_iv (DUPSORT) 1078 const_iv (DUPSORT)
976 const_iv (RECNUM) 1079 const_iv (RECNUM)
977 const_iv (RENUMBER) 1080 const_iv (RENUMBER)
978 const_iv (REVSPLITOFF) 1081 const_iv (REVSPLITOFF)
983 const_iv (GET_BOTH_RANGE) 1086 const_iv (GET_BOTH_RANGE)
984 //const_iv (SET_RECNO) 1087 //const_iv (SET_RECNO)
985 //const_iv (MULTIPLE) 1088 //const_iv (MULTIPLE)
986 const_iv (SNAPSHOT) 1089 const_iv (SNAPSHOT)
987 const_iv (JOIN_ITEM) 1090 const_iv (JOIN_ITEM)
1091 const_iv (JOIN_NOSORT)
988 const_iv (RMW) 1092 const_iv (RMW)
989 1093
990 const_iv (NOTFOUND) 1094 const_iv (NOTFOUND)
991 const_iv (KEYEMPTY) 1095 const_iv (KEYEMPTY)
992 const_iv (LOCK_DEADLOCK) 1096 const_iv (LOCK_DEADLOCK)
1008 const_iv (TXN_SYNC) 1112 const_iv (TXN_SYNC)
1009 1113
1010 const_iv (SET_LOCK_TIMEOUT) 1114 const_iv (SET_LOCK_TIMEOUT)
1011 const_iv (SET_TXN_TIMEOUT) 1115 const_iv (SET_TXN_TIMEOUT)
1012 1116
1013 const_iv (JOIN_ITEM)
1014 const_iv (FIRST) 1117 const_iv (FIRST)
1015 const_iv (NEXT) 1118 const_iv (NEXT)
1016 const_iv (NEXT_DUP) 1119 const_iv (NEXT_DUP)
1017 const_iv (NEXT_NODUP) 1120 const_iv (NEXT_NODUP)
1018 const_iv (PREV) 1121 const_iv (PREV)
1052 const_iv (LOG_BUFFER_FULL) 1155 const_iv (LOG_BUFFER_FULL)
1053 const_iv (NOSERVER) 1156 const_iv (NOSERVER)
1054 const_iv (NOSERVER_HOME) 1157 const_iv (NOSERVER_HOME)
1055 const_iv (NOSERVER_ID) 1158 const_iv (NOSERVER_ID)
1056 const_iv (NOTFOUND) 1159 const_iv (NOTFOUND)
1057 const_iv (OLD_VERSION)
1058 const_iv (PAGE_NOTFOUND) 1160 const_iv (PAGE_NOTFOUND)
1059 const_iv (REP_DUPMASTER) 1161 const_iv (REP_DUPMASTER)
1060 const_iv (REP_HANDLE_DEAD) 1162 const_iv (REP_HANDLE_DEAD)
1061 const_iv (REP_HOLDELECTION) 1163 const_iv (REP_HOLDELECTION)
1062 const_iv (REP_IGNORE) 1164 const_iv (REP_IGNORE)
1085 const_iv (MULTIVERSION) 1187 const_iv (MULTIVERSION)
1086 const_iv (TXN_SNAPSHOT) 1188 const_iv (TXN_SNAPSHOT)
1087#endif 1189#endif
1088#if DB_VERSION_MINOR >= 6 1190#if DB_VERSION_MINOR >= 6
1089 const_iv (PREV_DUP) 1191 const_iv (PREV_DUP)
1090# if 0
1091 const_iv (PRIORITY_UNCHANGED) 1192 const_iv (PRIORITY_UNCHANGED)
1092 const_iv (PRIORITY_VERY_LOW) 1193 const_iv (PRIORITY_VERY_LOW)
1093 const_iv (PRIORITY_LOW) 1194 const_iv (PRIORITY_LOW)
1094 const_iv (PRIORITY_DEFAULT) 1195 const_iv (PRIORITY_DEFAULT)
1095 const_iv (PRIORITY_HIGH) 1196 const_iv (PRIORITY_HIGH)
1096 const_iv (PRIORITY_VERY_HIGH) 1197 const_iv (PRIORITY_VERY_HIGH)
1097# endif
1098#endif 1198#endif
1099 }; 1199 };
1100 1200
1101 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 1201 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
1102 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 1202 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
1103 1203
1104 newCONSTSUB (stash, "DB_VERSION", newSVnv (DB_VERSION_MAJOR + DB_VERSION_MINOR * .1)); 1204 newCONSTSUB (stash, "VERSION", newSVnv (DB_VERSION_MAJOR + DB_VERSION_MINOR * .1));
1105 newCONSTSUB (stash, "DB_VERSION_STRING", newSVpv (DB_VERSION_STRING, 0)); 1205 newCONSTSUB (stash, "VERSION_STRING", newSVpv (DB_VERSION_STRING, 0));
1106 1206
1107 create_pipe (respipe); 1207 create_respipe ();
1108 1208
1109 X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child); 1209 X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child);
1110#ifdef _WIN32 1210#ifdef _WIN32
1111 X_MUTEX_CHECK (wrklock); 1211 X_MUTEX_CHECK (wrklock);
1112 X_MUTEX_CHECK (reslock); 1212 X_MUTEX_CHECK (reslock);
1113 X_MUTEX_CHECK (reqlock); 1213 X_MUTEX_CHECK (reqlock);
1114 1214
1115 X_COND_CHECK (reqwait); 1215 X_COND_CHECK (reqwait);
1116#endif 1216#endif
1217 patch_errno ();
1117} 1218}
1118 1219
1119void 1220void
1120max_poll_reqs (int nreqs) 1221max_poll_reqs (int nreqs)
1121 PROTOTYPE: $ 1222 PROTOTYPE: $
1279} 1380}
1280 OUTPUT: 1381 OUTPUT:
1281 RETVAL 1382 RETVAL
1282 1383
1283void 1384void
1284db_env_open (DB_ENV *env, octetstring db_home, U32 open_flags, int mode, SV *callback = &PL_sv_undef) 1385db_env_open (DB_ENV *env, bdb_filename db_home, U32 open_flags, int mode, SV *callback = &PL_sv_undef)
1285 CODE: 1386 CODE:
1286{ 1387{
1287 dREQ (REQ_ENV_OPEN); 1388 dREQ (REQ_ENV_OPEN);
1288
1289 env->set_thread_count (env, wanted + 2);
1290 1389
1291 req->env = env; 1390 req->env = env;
1292 req->uint1 = open_flags | DB_THREAD; 1391 req->uint1 = open_flags | DB_THREAD;
1293 req->int1 = mode; 1392 req->int1 = mode;
1294 req->buf1 = strdup_ornull (db_home); 1393 req->buf1 = strdup_ornull (db_home);
1362} 1461}
1363 OUTPUT: 1462 OUTPUT:
1364 RETVAL 1463 RETVAL
1365 1464
1366void 1465void
1367db_open (DB *db, DB_TXN_ornull *txnid, octetstring file, octetstring database, int type, U32 flags, int mode, SV *callback = &PL_sv_undef) 1466db_open (DB *db, DB_TXN_ornull *txnid, bdb_filename file, bdb_filename database, int type, U32 flags, int mode, SV *callback = &PL_sv_undef)
1368 CODE: 1467 CODE:
1369{ 1468{
1370 dREQ (REQ_DB_OPEN); 1469 dREQ (REQ_DB_OPEN);
1371 req->db = db; 1470 req->db = db;
1372 req->txn = txnid; 1471 req->txn = txnid;
1412 req->uint1 = flags; 1511 req->uint1 = flags;
1413 REQ_SEND; 1512 REQ_SEND;
1414} 1513}
1415 1514
1416void 1515void
1516db_upgrade (DB *db, bdb_filename file, U32 flags = 0, SV *callback = &PL_sv_undef)
1517 CODE:
1518{
1519 dREQ (REQ_DB_SYNC);
1520 req->db = db;
1521 req->buf1 = strdup (file);
1522 req->uint1 = flags;
1523 REQ_SEND;
1524}
1525
1526void
1417db_key_range (DB *db, DB_TXN_ornull *txn, SV *key, SV *key_range, U32 flags = 0, SV *callback = &PL_sv_undef) 1527db_key_range (DB *db, DB_TXN_ornull *txn, SV *key, SV *key_range, U32 flags = 0, SV *callback = &PL_sv_undef)
1418 CODE: 1528 CODE:
1419{ 1529{
1420 dREQ (REQ_DB_KEY_RANGE); 1530 dREQ (REQ_DB_KEY_RANGE);
1421 req->db = db; 1531 req->db = db;
1440} 1550}
1441 1551
1442void 1552void
1443db_get (DB *db, DB_TXN_ornull *txn, SV *key, SV *data, U32 flags = 0, SV *callback = &PL_sv_undef) 1553db_get (DB *db, DB_TXN_ornull *txn, SV *key, SV *data, U32 flags = 0, SV *callback = &PL_sv_undef)
1444 CODE: 1554 CODE:
1555 if (SvREADONLY (data))
1556 croak ("can't modify read-only data scalar in db_get");
1445{ 1557{
1446 dREQ (REQ_DB_GET); 1558 dREQ (REQ_DB_GET);
1447 req->db = db; 1559 req->db = db;
1448 req->txn = txn; 1560 req->txn = txn;
1449 req->uint1 = flags; 1561 req->uint1 = flags;
1454} 1566}
1455 1567
1456void 1568void
1457db_pget (DB *db, DB_TXN_ornull *txn, SV *key, SV *pkey, SV *data, U32 flags = 0, SV *callback = &PL_sv_undef) 1569db_pget (DB *db, DB_TXN_ornull *txn, SV *key, SV *pkey, SV *data, U32 flags = 0, SV *callback = &PL_sv_undef)
1458 CODE: 1570 CODE:
1571 if (SvREADONLY (data))
1572 croak ("can't modify read-only data scalar in db_pget");
1459{ 1573{
1460 dREQ (REQ_DB_PGET); 1574 dREQ (REQ_DB_PGET);
1461 req->db = db; 1575 req->db = db;
1462 req->txn = txn; 1576 req->txn = txn;
1463 req->uint1 = flags; 1577 req->uint1 = flags;
1500 REQ_SEND; 1614 REQ_SEND;
1501 ptr_nuke (ST (0)); 1615 ptr_nuke (ST (0));
1502} 1616}
1503 1617
1504void 1618void
1619db_txn_finish (DB_TXN *txn, U32 flags = 0, SV *callback = &PL_sv_undef)
1620 CODE:
1621{
1622 dREQ (REQ_TXN_FINISH);
1623 req->txn = txn;
1624 req->uint1 = flags;
1625 REQ_SEND;
1626 ptr_nuke (ST (0));
1627}
1628
1629void
1505db_c_close (DBC *dbc, SV *callback = &PL_sv_undef) 1630db_c_close (DBC *dbc, SV *callback = &PL_sv_undef)
1506 CODE: 1631 CODE:
1507{ 1632{
1508 dREQ (REQ_C_CLOSE); 1633 dREQ (REQ_C_CLOSE);
1509 req->dbc = dbc; 1634 req->dbc = dbc;
1646 1771
1647 1772
1648MODULE = BDB PACKAGE = BDB::Env 1773MODULE = BDB PACKAGE = BDB::Env
1649 1774
1650void 1775void
1651DESTROY (DB_ENV_ornull *env) 1776DESTROY (DB_ENV_ornuked *env)
1652 CODE: 1777 CODE:
1653 if (env) 1778 if (env)
1654 env->close (env, 0); 1779 env->close (env, 0);
1655 1780
1656int set_data_dir (DB_ENV *env, const char *dir) 1781int set_data_dir (DB_ENV *env, const char *dir)
1681 CODE: 1806 CODE:
1682 RETVAL = env->set_cachesize (env, gbytes, bytes, ncache); 1807 RETVAL = env->set_cachesize (env, gbytes, bytes, ncache);
1683 OUTPUT: 1808 OUTPUT:
1684 RETVAL 1809 RETVAL
1685 1810
1686int set_flags (DB_ENV *env, U32 flags, int onoff) 1811int set_flags (DB_ENV *env, U32 flags, int onoff = 1)
1687 CODE: 1812 CODE:
1688 RETVAL = env->set_flags (env, flags, onoff); 1813 RETVAL = env->set_flags (env, flags, onoff);
1689 OUTPUT: 1814 OUTPUT:
1690 RETVAL 1815 RETVAL
1691 1816
1695 1820
1696void set_msgfile (DB_ENV *env, FILE *msgfile = 0) 1821void set_msgfile (DB_ENV *env, FILE *msgfile = 0)
1697 CODE: 1822 CODE:
1698 env->set_msgfile (env, msgfile); 1823 env->set_msgfile (env, msgfile);
1699 1824
1700int set_verbose (DB_ENV *env, U32 which, int onoff = 1) 1825int set_verbose (DB_ENV *env, U32 which = -1, int onoff = 1)
1701 CODE: 1826 CODE:
1702 RETVAL = env->set_verbose (env, which, onoff); 1827 RETVAL = env->set_verbose (env, which, onoff);
1703 OUTPUT: 1828 OUTPUT:
1704 RETVAL 1829 RETVAL
1705 1830
1707 CODE: 1832 CODE:
1708 RETVAL = env->set_encrypt (env, password, flags); 1833 RETVAL = env->set_encrypt (env, password, flags);
1709 OUTPUT: 1834 OUTPUT:
1710 RETVAL 1835 RETVAL
1711 1836
1712int set_timeout (DB_ENV *env, NV timeout, U32 flags) 1837int set_timeout (DB_ENV *env, NV timeout, U32 flags = DB_SET_TXN_TIMEOUT)
1713 CODE: 1838 CODE:
1714 RETVAL = env->set_timeout (env, timeout * 1000000, flags); 1839 RETVAL = env->set_timeout (env, timeout * 1000000, flags);
1715 OUTPUT: 1840 OUTPUT:
1716 RETVAL 1841 RETVAL
1717 1842
1765 1890
1766int set_lg_max (DB_ENV *env, U32 max) 1891int set_lg_max (DB_ENV *env, U32 max)
1767 CODE: 1892 CODE:
1768 RETVAL = env->set_lg_max (env, max); 1893 RETVAL = env->set_lg_max (env, max);
1769 OUTPUT: 1894 OUTPUT:
1895 RETVAL
1896
1897int mutex_set_max (DB_ENV *env, U32 max)
1898 CODE:
1899 RETVAL = env->mutex_set_max (env, max);
1900 OUTPUT:
1901 RETVAL
1902
1903int mutex_set_increment (DB_ENV *env, U32 increment)
1904 CODE:
1905 RETVAL = env->mutex_set_increment (env, increment);
1906 OUTPUT:
1907 RETVAL
1908
1909int mutex_set_tas_spins (DB_ENV *env, U32 tas_spins)
1910 CODE:
1911 RETVAL = env->mutex_set_tas_spins (env, tas_spins);
1912 OUTPUT:
1913 RETVAL
1914
1915int mutex_set_align (DB_ENV *env, U32 align)
1916 CODE:
1917 RETVAL = env->mutex_set_align (env, align);
1918 OUTPUT:
1770 RETVAL 1919 RETVAL
1771 1920
1772DB_TXN * 1921DB_TXN *
1773txn_begin (DB_ENV *env, DB_TXN_ornull *parent = 0, U32 flags = 0) 1922txn_begin (DB_ENV *env, DB_TXN_ornull *parent = 0, U32 flags = 0)
1774 CODE: 1923 CODE:
1779 RETVAL 1928 RETVAL
1780 1929
1781MODULE = BDB PACKAGE = BDB::Db 1930MODULE = BDB PACKAGE = BDB::Db
1782 1931
1783void 1932void
1784DESTROY (DB_ornull *db) 1933DESTROY (DB_ornuked *db)
1785 CODE: 1934 CODE:
1786 if (db) 1935 if (db)
1787 { 1936 {
1788 SV *env = (SV *)db->app_private; 1937 SV *env = (SV *)db->app_private;
1789 db->close (db, 0); 1938 db->close (db, 0);
1884 2033
1885 2034
1886MODULE = BDB PACKAGE = BDB::Txn 2035MODULE = BDB PACKAGE = BDB::Txn
1887 2036
1888void 2037void
1889DESTROY (DB_TXN_ornull *txn) 2038DESTROY (DB_TXN_ornuked *txn)
1890 CODE: 2039 CODE:
1891 if (txn) 2040 if (txn)
1892 txn->abort (txn); 2041 txn->abort (txn);
1893 2042
1894int set_timeout (DB_TXN *txn, NV timeout, U32 flags) 2043int set_timeout (DB_TXN *txn, NV timeout, U32 flags = DB_SET_TXN_TIMEOUT)
1895 CODE: 2044 CODE:
1896 RETVAL = txn->set_timeout (txn, timeout * 1000000, flags); 2045 RETVAL = txn->set_timeout (txn, timeout * 1000000, flags);
1897 OUTPUT: 2046 OUTPUT:
1898 RETVAL 2047 RETVAL
1899 2048
2049int failed (DB_TXN *txn)
2050 CODE:
2051 RETVAL = !!(txn->flags & TXN_DEADLOCK);
2052 OUTPUT:
2053 RETVAL
2054
1900 2055
1901MODULE = BDB PACKAGE = BDB::Cursor 2056MODULE = BDB PACKAGE = BDB::Cursor
1902 2057
1903void 2058void
1904DESTROY (DBC_ornull *dbc) 2059DESTROY (DBC_ornuked *dbc)
1905 CODE: 2060 CODE:
1906 if (dbc) 2061 if (dbc)
1907 dbc->c_close (dbc); 2062 dbc->c_close (dbc);
1908 2063
2064#if DB_VERSION_MINOR >= 6
2065
2066int set_priority (DBC *dbc, int priority)
2067 CODE:
2068 dbc->set_priority (dbc, priority);
2069
2070#endif
2071
1909MODULE = BDB PACKAGE = BDB::Sequence 2072MODULE = BDB PACKAGE = BDB::Sequence
1910 2073
1911void 2074void
1912DESTROY (DB_SEQUENCE_ornull *seq) 2075DESTROY (DB_SEQUENCE_ornuked *seq)
1913 CODE: 2076 CODE:
1914 if (seq) 2077 if (seq)
1915 seq->close (seq, 0); 2078 seq->close (seq, 0);
1916 2079
1917int initial_value (DB_SEQUENCE *seq, db_seq_t value) 2080int initial_value (DB_SEQUENCE *seq, db_seq_t value)
1936 CODE: 2099 CODE:
1937 RETVAL = seq->set_range (seq, min, max); 2100 RETVAL = seq->set_range (seq, min, max);
1938 OUTPUT: 2101 OUTPUT:
1939 RETVAL 2102 RETVAL
1940 2103
2104

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines