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

Comparing BDB/BDB.xs (file contents):
Revision 1.66 by root, Sun Jan 4 10:48:15 2009 UTC vs.
Revision 1.80 by root, Tue Feb 2 04:24:00 2016 UTC

6 6
7#include "EXTERN.h" 7#include "EXTERN.h"
8#include "perl.h" 8#include "perl.h"
9#include "XSUB.h" 9#include "XSUB.h"
10 10
11#include "schmorp.h"
12
11// perl stupidly defines these as macros, breaking 13// perl stupidly defines these as argument-less macros, breaking
12// lots and lots of code. 14// lots and lots of code.
13#undef open 15#undef open
14#undef close 16#undef close
15#undef abort 17#undef abort
16#undef malloc 18#undef malloc
29# include <unistd.h> 31# include <unistd.h>
30#endif 32#endif
31 33
32#include <db.h> 34#include <db.h>
33 35
34#if DB_VERSION_MAJOR != 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR < 3) 36#define DBVER DB_VERSION_MAJOR * 100 + DB_VERSION_MINOR
37
38#if DBVER < 403
35# error you need Berkeley DB 4.3 or a newer 4.x version installed 39# error you need Berkeley DB 4.3 or a newer version installed
36#endif 40#endif
37 41
38/* number of seconds after which idle threads exit */ 42/* number of seconds after which idle threads exit */
39#define IDLE_TIMEOUT 10 43#define IDLE_TIMEOUT 10
40 44
48typedef DB_ENV DB_ENV_ornuked; 52typedef DB_ENV DB_ENV_ornuked;
49typedef DB_TXN DB_TXN_ornuked; 53typedef DB_TXN DB_TXN_ornuked;
50typedef DBC DBC_ornuked; 54typedef DBC DBC_ornuked;
51typedef DB DB_ornuked; 55typedef DB DB_ornuked;
52 56
53#if DB_VERSION_MINOR >= 3 57#if DBVER >= 403
54typedef DB_SEQUENCE DB_SEQUENCE_ornull; 58typedef DB_SEQUENCE DB_SEQUENCE_ornull;
55typedef DB_SEQUENCE DB_SEQUENCE_ornuked; 59typedef DB_SEQUENCE DB_SEQUENCE_ornuked;
56#endif 60#endif
57 61
58typedef char *bdb_filename; 62typedef char *bdb_filename;
59 63
60static SV *prepare_cb; 64static SV *prepare_cb;
61 65
62#if DB_VERSION_MINOR >= 6 66static HV
67 *bdb_stash,
68 *bdb_env_stash,
69 *bdb_txn_stash,
70 *bdb_cursor_stash,
71 *bdb_db_stash,
72 *bdb_sequence_stash;
73
74#if DBVER >= 406
63# define c_close close 75# define c_close close
64# define c_count count 76# define c_count count
65# define c_del del 77# define c_del del
66# define c_dup dup 78# define c_dup dup
67# define c_get get 79# define c_get get
149 161
150enum { 162enum {
151 REQ_QUIT, 163 REQ_QUIT,
152 REQ_ENV_OPEN, REQ_ENV_CLOSE, REQ_ENV_TXN_CHECKPOINT, REQ_ENV_LOCK_DETECT, 164 REQ_ENV_OPEN, REQ_ENV_CLOSE, REQ_ENV_TXN_CHECKPOINT, REQ_ENV_LOCK_DETECT,
153 REQ_ENV_MEMP_SYNC, REQ_ENV_MEMP_TRICKLE, REQ_ENV_DBREMOVE, REQ_ENV_DBRENAME, 165 REQ_ENV_MEMP_SYNC, REQ_ENV_MEMP_TRICKLE, REQ_ENV_DBREMOVE, REQ_ENV_DBRENAME,
154 REQ_ENV_LOG_ARCHIVE, 166 REQ_ENV_LOG_ARCHIVE, REQ_ENV_LSN_RESET,
155 REQ_DB_OPEN, REQ_DB_CLOSE, REQ_DB_COMPACT, REQ_DB_SYNC, REQ_DB_UPGRADE, 167 REQ_DB_OPEN, REQ_DB_CLOSE, REQ_DB_COMPACT, REQ_DB_SYNC, REQ_DB_VERIFY, REQ_DB_UPGRADE,
156 REQ_DB_PUT, REQ_DB_EXISTS, REQ_DB_GET, REQ_DB_PGET, REQ_DB_DEL, REQ_DB_KEY_RANGE, 168 REQ_DB_PUT, REQ_DB_EXISTS, REQ_DB_GET, REQ_DB_PGET, REQ_DB_DEL, REQ_DB_KEY_RANGE,
157 REQ_TXN_COMMIT, REQ_TXN_ABORT, REQ_TXN_FINISH, 169 REQ_TXN_COMMIT, REQ_TXN_ABORT, REQ_TXN_FINISH,
158 REQ_C_CLOSE, REQ_C_COUNT, REQ_C_PUT, REQ_C_GET, REQ_C_PGET, REQ_C_DEL, 170 REQ_C_CLOSE, REQ_C_COUNT, REQ_C_PUT, REQ_C_GET, REQ_C_PGET, REQ_C_DEL,
159 REQ_SEQ_OPEN, REQ_SEQ_CLOSE, REQ_SEQ_GET, REQ_SEQ_REMOVE, 171 REQ_SEQ_OPEN, REQ_SEQ_CLOSE, REQ_SEQ_GET, REQ_SEQ_REMOVE,
160}; 172};
176 char *buf1, *buf2, *buf3; 188 char *buf1, *buf2, *buf3;
177 SV *sv1, *sv2, *sv3; 189 SV *sv1, *sv2, *sv3;
178 190
179 DBT dbt1, dbt2, dbt3; 191 DBT dbt1, dbt2, dbt3;
180 DB_KEY_RANGE key_range; 192 DB_KEY_RANGE key_range;
181#if DB_VERSION_MINOR >= 3 193#if DBVER >= 403
182 DB_SEQUENCE *seq; 194 DB_SEQUENCE *seq;
183 db_seq_t seq_t; 195 db_seq_t seq_t;
184#endif 196#endif
185 197
186 SV *rsv1, *rsv2; // keep some request objects alive 198 SV *rsv1, *rsv2; // keep some request objects alive
214static int next_pri = DEFAULT_PRI + PRI_BIAS; 226static int next_pri = DEFAULT_PRI + PRI_BIAS;
215 227
216static unsigned int started, idle, wanted; 228static unsigned int started, idle, wanted;
217 229
218/* worker threads management */ 230/* worker threads management */
219static mutex_t wrklock = X_MUTEX_INIT; 231static xmutex_t wrklock = X_MUTEX_INIT;
220 232
221typedef struct worker { 233typedef struct worker {
222 /* locked by wrklock */ 234 /* locked by wrklock */
223 struct worker *prev, *next; 235 struct worker *prev, *next;
224 236
225 thread_t tid; 237 xthread_t tid;
226 238
227 /* locked by reslock, reqlock or wrklock */ 239 /* locked by reslock, reqlock or wrklock */
228 bdb_req req; /* currently processed request */ 240 bdb_req req; /* currently processed request */
229 void *dbuf; 241 void *dbuf;
230 DIR *dirp; 242 DIR *dirp;
245} 257}
246 258
247static volatile unsigned int nreqs, nready, npending; 259static volatile unsigned int nreqs, nready, npending;
248static volatile unsigned int max_idle = 4; 260static volatile unsigned int max_idle = 4;
249static volatile unsigned int max_outstanding = 0xffffffff; 261static volatile unsigned int max_outstanding = 0xffffffff;
250static int respipe_osf [2], respipe [2] = { -1, -1 }; 262static s_epipe respipe;
251 263
252static mutex_t reslock = X_MUTEX_INIT; 264static xmutex_t reslock = X_MUTEX_INIT;
253static mutex_t reqlock = X_MUTEX_INIT; 265static xmutex_t reqlock = X_MUTEX_INIT;
254static cond_t reqwait = X_COND_INIT; 266static xcond_t reqwait = X_COND_INIT;
255 267
256#if WORDACCESS_UNSAFE 268#if WORDACCESS_UNSAFE
257 269
258static unsigned int get_nready (void) 270static unsigned int get_nready (void)
259{ 271{
387 SvREFCNT_dec (av); 399 SvREFCNT_dec (av);
388 SvREFCNT_dec (req->sv1); 400 SvREFCNT_dec (req->sv1);
389 } 401 }
390 break; 402 break;
391 403
392#if DB_VERSION_MINOR >= 3 404#if DBVER >= 403
393 case REQ_SEQ_GET: 405 case REQ_SEQ_GET:
394 SvREADONLY_off (req->sv1); 406 SvREADONLY_off (req->sv1);
395 407
396 if (sizeof (IV) > 4) 408 if (sizeof (IV) > 4)
397 sv_setiv_mg (req->sv1, (IV)req->seq_t); 409 sv_setiv_mg (req->sv1, (IV)req->seq_t);
456 free (req->buf3); 468 free (req->buf3);
457 469
458 Safefree (req); 470 Safefree (req);
459} 471}
460 472
461#ifdef USE_SOCKETS_AS_HANDLES
462# define TO_SOCKET(x) (win32_get_osfhandle (x))
463#else
464# define EV_SELECT_IS_WINSOCKET 1
465# define TO_SOCKET(x) (x)
466#endif
467
468#ifdef _WIN32
469/* taken verbatim from libev's ev_win32.c */
470/* oh, the humanity! */
471static int
472ev_pipe (int filedes [2])
473{
474 struct sockaddr_in addr = { 0 };
475 int addr_size = sizeof (addr);
476 struct sockaddr_in adr2;
477 int adr2_size;
478 SOCKET listener;
479 SOCKET sock [2] = { -1, -1 };
480
481 if ((listener = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
482 return -1;
483
484 addr.sin_family = AF_INET;
485 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
486 addr.sin_port = 0;
487
488 if (bind (listener, (struct sockaddr *)&addr, addr_size))
489 goto fail;
490
491 if (getsockname (listener, (struct sockaddr *)&addr, &addr_size))
492 goto fail;
493
494 if (listen (listener, 1))
495 goto fail;
496
497 if ((sock [0] = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
498 goto fail;
499
500 if (connect (sock [0], (struct sockaddr *)&addr, addr_size))
501 goto fail;
502
503 if ((sock [1] = accept (listener, 0, 0)) < 0)
504 goto fail;
505
506 /* windows vista returns fantasy port numbers for getpeername.
507 * example for two interconnected tcp sockets:
508 *
509 * (Socket::unpack_sockaddr_in getsockname $sock0)[0] == 53364
510 * (Socket::unpack_sockaddr_in getpeername $sock0)[0] == 53363
511 * (Socket::unpack_sockaddr_in getsockname $sock1)[0] == 53363
512 * (Socket::unpack_sockaddr_in getpeername $sock1)[0] == 53365
513 *
514 * wow! tridirectional sockets!
515 *
516 * this way of checking ports seems to work:
517 */
518 if (getpeername (sock [0], (struct sockaddr *)&addr, &addr_size))
519 goto fail;
520
521 if (getsockname (sock [1], (struct sockaddr *)&adr2, &adr2_size))
522 goto fail;
523
524 errno = WSAEINVAL;
525 if (addr_size != adr2_size
526 || addr.sin_addr.s_addr != adr2.sin_addr.s_addr /* just to be sure, I mean, it's windows */
527 || addr.sin_port != adr2.sin_port)
528 goto fail;
529
530 closesocket (listener);
531
532#if EV_SELECT_IS_WINSOCKET
533 filedes [0] = _open_osfhandle (sock [0], 0);
534 filedes [1] = _open_osfhandle (sock [1], 0);
535#else
536 /* when select isn't winsocket, we also expect socket, connect, accept etc.
537 * to work on fds */
538 filedes [0] = sock [0];
539 filedes [1] = sock [1];
540#endif
541
542 return 0;
543
544fail:
545 closesocket (listener);
546
547 if (sock [0] != INVALID_SOCKET) closesocket (sock [0]);
548 if (sock [1] != INVALID_SOCKET) closesocket (sock [1]);
549
550 return -1;
551}
552
553#define pipe(filedes) ev_pipe(filedes)
554#endif
555
556static void 473static void
557create_respipe (void) 474create_respipe (void)
558{ 475{
559#ifdef _WIN32
560 int arg; /* argg */
561#endif
562 int old_readfd = respipe [0];
563
564 if (respipe [1] >= 0)
565 respipe_close (TO_SOCKET (respipe [1]));
566
567 if (pipe (respipe)) 476 if (s_epipe_renew (&respipe))
568 croak ("unable to initialize result pipe"); 477 croak ("BDB: unable to create event pipe");
569
570 if (old_readfd >= 0)
571 {
572 if (dup2 (TO_SOCKET (respipe [0]), TO_SOCKET (old_readfd)) < 0)
573 croak ("unable to initialize result pipe(2)");
574
575 respipe_close (respipe [0]);
576 respipe [0] = old_readfd;
577 }
578
579#ifdef _WIN32
580 arg = 1;
581 if (ioctlsocket (TO_SOCKET (respipe [0]), FIONBIO, &arg)
582 || ioctlsocket (TO_SOCKET (respipe [1]), FIONBIO, &arg))
583#else
584 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)
585 || fcntl (respipe [1], F_SETFL, O_NONBLOCK))
586#endif
587 croak ("unable to initialize result pipe(3)");
588
589 respipe_osf [0] = TO_SOCKET (respipe [0]);
590 respipe_osf [1] = TO_SOCKET (respipe [1]);
591} 478}
592 479
593static void bdb_request (bdb_req req); 480static void bdb_request (bdb_req req);
594X_THREAD_PROC (bdb_proc); 481X_THREAD_PROC (bdb_proc);
595 482
599 486
600 if (!wrk) 487 if (!wrk)
601 croak ("unable to allocate worker thread data"); 488 croak ("unable to allocate worker thread data");
602 489
603 X_LOCK (wrklock); 490 X_LOCK (wrklock);
604 if (thread_create (&wrk->tid, bdb_proc, (void *)wrk)) 491 if (xthread_create (&wrk->tid, bdb_proc, (void *)wrk))
605 { 492 {
606 wrk->prev = &wrk_first; 493 wrk->prev = &wrk_first;
607 wrk->next = wrk_first.next; 494 wrk->next = wrk_first.next;
608 wrk_first.next->prev = wrk; 495 wrk_first.next->prev = wrk;
609 wrk_first.next = wrk; 496 wrk_first.next = wrk;
731 end_thread (); 618 end_thread ();
732} 619}
733 620
734static void poll_wait (void) 621static void poll_wait (void)
735{ 622{
736 fd_set rfd;
737
738 while (nreqs) 623 while (nreqs)
739 { 624 {
740 int size; 625 int size;
741 if (WORDACCESS_UNSAFE) X_LOCK (reslock); 626 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
742 size = res_queue.size; 627 size = res_queue.size;
745 if (size) 630 if (size)
746 return; 631 return;
747 632
748 maybe_start_thread (); 633 maybe_start_thread ();
749 634
750 FD_ZERO (&rfd); 635 s_epipe_wait (&respipe);
751 FD_SET (respipe [0], &rfd);
752
753 PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0);
754 } 636 }
755} 637}
756 638
757static int poll_cb (void) 639static int poll_cb (void)
758{ 640{
778 if (req) 660 if (req)
779 { 661 {
780 --npending; 662 --npending;
781 663
782 if (!res_queue.size) 664 if (!res_queue.size)
783 {
784 /* read any signals sent by the worker threads */ 665 /* read any signals sent by the worker threads */
785 char buf [4]; 666 s_epipe_drain (&respipe);
786 while (respipe_read (respipe [0], buf, 4) == 4)
787 ;
788 }
789 } 667 }
790 668
791 X_UNLOCK (reslock); 669 X_UNLOCK (reslock);
792 670
793 if (!req) 671 if (!req)
873 751
874 case REQ_DB_CLOSE: 752 case REQ_DB_CLOSE:
875 req->result = req->db->close (req->db, req->uint1); 753 req->result = req->db->close (req->db, req->uint1);
876 break; 754 break;
877 755
878#if DB_VERSION_MINOR >= 4 756#if DBVER >= 404
879 case REQ_DB_COMPACT: 757 case REQ_DB_COMPACT:
880 req->result = req->db->compact (req->db, req->txn, req->dbt1.data ? &req->dbt1 : 0, req->dbt2.data ? &req->dbt2 : 0, 0, req->uint1, 0); 758 req->result = req->db->compact (req->db, req->txn, req->dbt1.data ? &req->dbt1 : 0, req->dbt2.data ? &req->dbt2 : 0, 0, req->uint1, 0);
881 break; 759 break;
882#endif 760#endif
883 761
884 case REQ_DB_SYNC: 762 case REQ_DB_SYNC:
885 req->result = req->db->sync (req->db, req->uint1); 763 req->result = req->db->sync (req->db, req->uint1);
886 break; 764 break;
887 765
766 case REQ_DB_VERIFY:
767 req->result = req->db->verify (req->db, req->buf1, req->buf2, 0, req->uint1);
768 break;
769
888 case REQ_DB_UPGRADE: 770 case REQ_DB_UPGRADE:
889 req->result = req->db->upgrade (req->db, req->buf1, req->uint1); 771 req->result = req->db->upgrade (req->db, req->buf1, req->uint1);
890 break; 772 break;
891 773
892 case REQ_DB_PUT: 774 case REQ_DB_PUT:
893 req->result = req->db->put (req->db, req->txn, &req->dbt1, &req->dbt2, req->uint1); 775 req->result = req->db->put (req->db, req->txn, &req->dbt1, &req->dbt2, req->uint1);
894 break; 776 break;
895 777
896#if DB_VERSION_MINOR >= 6 778#if DBVER >= 406
897 case REQ_DB_EXISTS: 779 case REQ_DB_EXISTS:
898 req->result = req->db->exists (req->db, req->txn, &req->dbt1, req->uint1); 780 req->result = req->db->exists (req->db, req->txn, &req->dbt1, req->uint1);
899 break; 781 break;
900#endif 782#endif
901 case REQ_DB_GET: 783 case REQ_DB_GET:
959 841
960 case REQ_C_DEL: 842 case REQ_C_DEL:
961 req->result = req->dbc->c_del (req->dbc, req->uint1); 843 req->result = req->dbc->c_del (req->dbc, req->uint1);
962 break; 844 break;
963 845
964#if DB_VERSION_MINOR >= 3 846#if DBVER >= 403
965 case REQ_SEQ_OPEN: 847 case REQ_SEQ_OPEN:
966 req->result = req->seq->open (req->seq, req->txn, &req->dbt1, req->uint1); 848 req->result = req->seq->open (req->seq, req->txn, &req->dbt1, req->uint1);
967 break; 849 break;
968 850
969 case REQ_SEQ_CLOSE: 851 case REQ_SEQ_CLOSE:
974 req->result = req->seq->get (req->seq, req->txn, req->int1, &req->seq_t, req->uint1); 856 req->result = req->seq->get (req->seq, req->txn, req->int1, &req->seq_t, req->uint1);
975 break; 857 break;
976 858
977 case REQ_SEQ_REMOVE: 859 case REQ_SEQ_REMOVE:
978 req->result = req->seq->remove (req->seq, req->txn, req->uint1); 860 req->result = req->seq->remove (req->seq, req->txn, req->uint1);
861 break;
862#endif
863
864#if DBVER >= 407
865 case REQ_ENV_LSN_RESET:
866 req->result = req->env->lsn_reset (req->env, req->buf1, req->uint1);
979 break; 867 break;
980#endif 868#endif
981 869
982 case REQ_ENV_LOG_ARCHIVE: 870 case REQ_ENV_LOG_ARCHIVE:
983 { 871 {
1005 /* try to distribute timeouts somewhat evenly */ 893 /* try to distribute timeouts somewhat evenly */
1006 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); 894 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
1007 895
1008 for (;;) 896 for (;;)
1009 { 897 {
1010 ts.tv_sec = time (0) + IDLE_TIMEOUT; 898 ts.tv_sec = time (0) + IDLE_TIMEOUT;
1011 899
1012 X_LOCK (reqlock); 900 X_LOCK (reqlock);
1013 901
1014 for (;;) 902 for (;;)
1015 { 903 {
1060 X_LOCK (reslock); 948 X_LOCK (reslock);
1061 949
1062 ++npending; 950 ++npending;
1063 951
1064 if (!reqq_push (&res_queue, req)) 952 if (!reqq_push (&res_queue, req))
1065 /* write a dummy byte to the pipe so fh becomes ready */ 953 s_epipe_signal (&respipe);
1066 respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1);
1067 954
1068 self->req = 0; 955 self->req = 0;
1069 worker_clear (self); 956 worker_clear (self);
1070 957
1071 X_UNLOCK (reslock); 958 X_UNLOCK (reslock);
1133 next_pri = DEFAULT_PRI + PRI_BIAS; \ 1020 next_pri = DEFAULT_PRI + PRI_BIAS; \
1134 \ 1021 \
1135 if (callback && SvOK (callback)) \ 1022 if (callback && SvOK (callback)) \
1136 croak ("callback has illegal type or extra arguments"); \ 1023 croak ("callback has illegal type or extra arguments"); \
1137 \ 1024 \
1138 Newz (0, req, 1, bdb_cb); \ 1025 Newz (0, req, 1, bdb_cb); \
1139 if (!req) \ 1026 if (!req) \
1140 croak ("out of memory during bdb_req allocation"); \ 1027 croak ("out of memory during bdb_req allocation"); \
1141 \ 1028 \
1142 req->callback = SvREFCNT_inc (cb); \ 1029 req->callback = SvREFCNT_inc (cb); \
1143 req->type = (reqtype); \ 1030 req->type = (reqtype); \
1144 req->pri = req_pri; \ 1031 req->pri = req_pri; \
1145 if (rsvcnt >= 1) req->rsv1 = SvREFCNT_inc (ST (0)); \ 1032 if (rsvcnt >= 1) req->rsv1 = SvREFCNT_inc (ST (0)); \
1146 if (rsvcnt >= 2) req->rsv2 = SvREFCNT_inc (ST (1)); \ 1033 if (rsvcnt >= 2) req->rsv2 = SvREFCNT_inc (ST (1)); \
1147 (void)0; 1034 (void)0;
1148 1035
1149#define REQ_SEND \ 1036#define REQ_SEND \
1150 req_send (req) 1037 req_send (req)
1151 1038
1152#define SvPTR(var, arg, type, class, nullok) \ 1039#define SvPTR(var, arg, type, stash, class, nullok) \
1153 if (!SvOK (arg)) \ 1040 if (!SvOK (arg)) \
1154 { \ 1041 { \
1155 if (nullok != 1) \ 1042 if (nullok != 1) \
1156 croak (# var " must be a " # class " object, not undef"); \ 1043 croak (# var " must be a " # class " object, not undef"); \
1157 \ 1044 \
1158 (var) = 0; \ 1045 (var) = 0; \
1159 } \ 1046 } \
1160 else if (sv_derived_from ((arg), # class)) \ 1047 else if (SvSTASH (SvRV (arg)) == stash || sv_derived_from ((arg), # class)) \
1161 { \ 1048 { \
1162 IV tmp = SvIV ((SV*) SvRV (arg)); \ 1049 IV tmp = SvIV ((SV*) SvRV (arg)); \
1163 (var) = INT2PTR (type, tmp); \ 1050 (var) = INT2PTR (type, tmp); \
1164 if (!var && nullok != 2) \ 1051 if (!var && nullok != 2) \
1165 croak (# var " is not a valid " # class " object anymore"); \ 1052 croak (# var " is not a valid " # class " object anymore"); \
1168 croak (# var " is not of type " # class); 1055 croak (# var " is not of type " # class);
1169 1056
1170#define ARG_MUTABLE(name) \ 1057#define ARG_MUTABLE(name) \
1171 if (SvREADONLY (name)) \ 1058 if (SvREADONLY (name)) \
1172 croak ("argument " #name " is read-only/constant, but the request requires it to be mutable"); 1059 croak ("argument " #name " is read-only/constant, but the request requires it to be mutable");
1060
1061static SV *
1062newSVptr (void *ptr, HV *stash)
1063{
1064 SV *rv = NEWSV (0, 0);
1065 sv_upgrade (rv, SVt_PVMG);
1066 sv_setiv (rv, PTR2IV (ptr));
1067
1068 return sv_bless (newRV_noinc (rv), stash);
1069}
1173 1070
1174static void 1071static void
1175ptr_nuke (SV *sv) 1072ptr_nuke (SV *sv)
1176{ 1073{
1177 assert (SvROK (sv)); 1074 assert (SvROK (sv));
1299 1196
1300PROTOTYPES: ENABLE 1197PROTOTYPES: ENABLE
1301 1198
1302BOOT: 1199BOOT:
1303{ 1200{
1304 HV *stash = gv_stashpv ("BDB", 1);
1305
1306 static const struct { 1201 static const struct {
1307 const char *name; 1202 const char *name;
1308 IV iv; 1203 IV iv;
1309 } *civ, const_iv[] = { 1204 } *civ, const_iv[] = {
1310#define const_iv(name) { # name, (IV)DB_ ## name }, 1205#define const_iv(name) { # name, (IV)DB_ ## name },
1206#if DBVER <= 408
1311 const_iv (RPCCLIENT) 1207 const_iv (RPCCLIENT)
1208#endif
1312 const_iv (INIT_CDB) 1209 const_iv (INIT_CDB)
1313 const_iv (INIT_LOCK) 1210 const_iv (INIT_LOCK)
1314 const_iv (INIT_LOG) 1211 const_iv (INIT_LOG)
1315 const_iv (INIT_MPOOL) 1212 const_iv (INIT_MPOOL)
1316 const_iv (INIT_REP) 1213 const_iv (INIT_REP)
1339 const_iv (TXN_NOT_DURABLE) 1236 const_iv (TXN_NOT_DURABLE)
1340 const_iv (TXN_WRITE_NOSYNC) 1237 const_iv (TXN_WRITE_NOSYNC)
1341 const_iv (WRITECURSOR) 1238 const_iv (WRITECURSOR)
1342 const_iv (YIELDCPU) 1239 const_iv (YIELDCPU)
1343 const_iv (ENCRYPT_AES) 1240 const_iv (ENCRYPT_AES)
1241#if DBVER < 408
1344 const_iv (XA_CREATE) 1242 const_iv (XA_CREATE)
1243#endif
1345 const_iv (BTREE) 1244 const_iv (BTREE)
1346 const_iv (HASH) 1245 const_iv (HASH)
1347 const_iv (QUEUE) 1246 const_iv (QUEUE)
1348 const_iv (RECNO) 1247 const_iv (RECNO)
1349 const_iv (UNKNOWN) 1248 const_iv (UNKNOWN)
1413 const_iv (LOCK_OLDEST) 1312 const_iv (LOCK_OLDEST)
1414 const_iv (LOCK_RANDOM) 1313 const_iv (LOCK_RANDOM)
1415 const_iv (LOCK_YOUNGEST) 1314 const_iv (LOCK_YOUNGEST)
1416 1315
1417 const_iv (DONOTINDEX) 1316 const_iv (DONOTINDEX)
1418 const_iv (KEYEMPTY ) 1317 const_iv (KEYEMPTY)
1419 const_iv (KEYEXIST ) 1318 const_iv (KEYEXIST)
1420 const_iv (LOCK_DEADLOCK) 1319 const_iv (LOCK_DEADLOCK)
1421 const_iv (LOCK_NOTGRANTED) 1320 const_iv (LOCK_NOTGRANTED)
1422 const_iv (NOSERVER) 1321 const_iv (NOSERVER)
1322#if DBVER < 502
1423 const_iv (NOSERVER_HOME) 1323 const_iv (NOSERVER_HOME)
1424 const_iv (NOSERVER_ID) 1324 const_iv (NOSERVER_ID)
1325#endif
1425 const_iv (NOTFOUND) 1326 const_iv (NOTFOUND)
1426 const_iv (PAGE_NOTFOUND) 1327 const_iv (PAGE_NOTFOUND)
1427 const_iv (REP_DUPMASTER) 1328 const_iv (REP_DUPMASTER)
1428 const_iv (REP_HANDLE_DEAD) 1329 const_iv (REP_HANDLE_DEAD)
1429 const_iv (REP_HOLDELECTION) 1330 const_iv (REP_HOLDELECTION)
1434 const_iv (REP_UNAVAIL) 1335 const_iv (REP_UNAVAIL)
1435 const_iv (RUNRECOVERY) 1336 const_iv (RUNRECOVERY)
1436 const_iv (SECONDARY_BAD) 1337 const_iv (SECONDARY_BAD)
1437 const_iv (VERIFY_BAD) 1338 const_iv (VERIFY_BAD)
1438 1339
1340 const_iv (SALVAGE)
1341 const_iv (AGGRESSIVE)
1342 const_iv (PRINTABLE)
1343 const_iv (NOORDERCHK)
1344 const_iv (ORDERCHKONLY)
1345
1439 const_iv (ARCH_ABS) 1346 const_iv (ARCH_ABS)
1440 const_iv (ARCH_DATA) 1347 const_iv (ARCH_DATA)
1441 const_iv (ARCH_LOG) 1348 const_iv (ARCH_LOG)
1442 const_iv (ARCH_REMOVE) 1349 const_iv (ARCH_REMOVE)
1443 1350
1447 const_iv (VERB_WAITSFOR) 1354 const_iv (VERB_WAITSFOR)
1448 1355
1449 const_iv (VERSION_MAJOR) 1356 const_iv (VERSION_MAJOR)
1450 const_iv (VERSION_MINOR) 1357 const_iv (VERSION_MINOR)
1451 const_iv (VERSION_PATCH) 1358 const_iv (VERSION_PATCH)
1452#if DB_VERSION_MINOR >= 3 1359 const_iv (LOGVERSION)
1360 const_iv (LOGOLDVER)
1361#if DBVER >= 403
1453 const_iv (INORDER) 1362 const_iv (INORDER)
1454 const_iv (LOCK_MAXWRITE) 1363 const_iv (LOCK_MAXWRITE)
1455 const_iv (SEQ_DEC) 1364 const_iv (SEQ_DEC)
1456 const_iv (SEQ_INC) 1365 const_iv (SEQ_INC)
1457 const_iv (SEQ_WRAP) 1366 const_iv (SEQ_WRAP)
1458 const_iv (BUFFER_SMALL) 1367 const_iv (BUFFER_SMALL)
1459 const_iv (LOG_BUFFER_FULL) 1368 const_iv (LOG_BUFFER_FULL)
1460 const_iv (VERSION_MISMATCH) 1369 const_iv (VERSION_MISMATCH)
1461#endif 1370#endif
1462#if DB_VERSION_MINOR >= 4 1371#if DBVER >= 404
1463 const_iv (REGISTER) 1372 const_iv (REGISTER)
1464 const_iv (DSYNC_DB) 1373 const_iv (DSYNC_DB)
1465 const_iv (READ_COMMITTED) 1374 const_iv (READ_COMMITTED)
1466 const_iv (READ_UNCOMMITTED) 1375 const_iv (READ_UNCOMMITTED)
1467 const_iv (REP_IGNORE) 1376 const_iv (REP_IGNORE)
1469 const_iv (REP_JOIN_FAILURE) 1378 const_iv (REP_JOIN_FAILURE)
1470 const_iv (FREE_SPACE) 1379 const_iv (FREE_SPACE)
1471 const_iv (FREELIST_ONLY) 1380 const_iv (FREELIST_ONLY)
1472 const_iv (VERB_REGISTER) 1381 const_iv (VERB_REGISTER)
1473#endif 1382#endif
1474#if DB_VERSION_MINOR >= 5 1383#if DBVER >= 405
1475 const_iv (MULTIVERSION) 1384 const_iv (MULTIVERSION)
1476 const_iv (TXN_SNAPSHOT) 1385 const_iv (TXN_SNAPSHOT)
1477#endif 1386#endif
1478#if DB_VERSION_MINOR >= 6 1387#if DBVER >= 406
1479 const_iv (PREV_DUP) 1388 const_iv (PREV_DUP)
1480 const_iv (PRIORITY_UNCHANGED) 1389 const_iv (PRIORITY_UNCHANGED)
1481 const_iv (PRIORITY_VERY_LOW) 1390 const_iv (PRIORITY_VERY_LOW)
1482 const_iv (PRIORITY_LOW) 1391 const_iv (PRIORITY_LOW)
1483 const_iv (PRIORITY_DEFAULT) 1392 const_iv (PRIORITY_DEFAULT)
1484 const_iv (PRIORITY_HIGH) 1393 const_iv (PRIORITY_HIGH)
1485 const_iv (PRIORITY_VERY_HIGH) 1394 const_iv (PRIORITY_VERY_HIGH)
1486 const_iv (IGNORE_LEASE) 1395 const_iv (IGNORE_LEASE)
1487#endif 1396#endif
1488#if DB_VERSION_MINOR >= 7 1397#if DBVER >= 407
1489 //const_iv (MULTIPLE_KEY) 1398 //const_iv (MULTIPLE_KEY)
1490 const_iv (LOG_DIRECT) 1399 const_iv (LOG_DIRECT)
1491 const_iv (LOG_DSYNC) 1400 const_iv (LOG_DSYNC)
1492 const_iv (LOG_AUTO_REMOVE) 1401 const_iv (LOG_AUTO_REMOVE)
1493 const_iv (LOG_IN_MEMORY) 1402 const_iv (LOG_IN_MEMORY)
1494 const_iv (LOG_ZERO) 1403 const_iv (LOG_ZERO)
1495#else 1404#else
1496 const_iv (DIRECT_LOG) 1405 const_iv (DIRECT_LOG)
1497 const_iv (LOG_AUTOREMOVE) 1406 const_iv (LOG_AUTOREMOVE)
1498# if DB_VERSION_MINOR >= 3 1407# if DBVER >= 403
1499 const_iv (DSYNC_LOG) 1408 const_iv (DSYNC_LOG)
1500 const_iv (LOG_INMEMORY) 1409 const_iv (LOG_INMEMORY)
1501# endif 1410# endif
1411#if DBVER >= 408
1412 const_iv (LOGVERSION_LATCHING)
1413#endif
1502#endif 1414#endif
1503 }; 1415 };
1504 1416
1417 bdb_stash = gv_stashpv ("BDB" , 1);
1418 bdb_env_stash = gv_stashpv ("BDB::Env" , 1);
1419 bdb_txn_stash = gv_stashpv ("BDB::Txn" , 1);
1420 bdb_cursor_stash = gv_stashpv ("BDB::Cursor" , 1);
1421 bdb_db_stash = gv_stashpv ("BDB::Db" , 1);
1422 bdb_sequence_stash = gv_stashpv ("BDB::Sequence", 1);
1423
1505 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 1424 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--)
1506 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 1425 newCONSTSUB (bdb_stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
1507 1426
1508 prepare_cb = &PL_sv_undef; 1427 prepare_cb = &PL_sv_undef;
1509 1428
1510 { 1429 {
1511 /* we currently only allow version, minor-version and patchlevel to go up to 255 */ 1430 /* we currently only allow version, minor-version and patchlevel to go up to 255 */
1512 char vstring[3] = { DB_VERSION_MAJOR, DB_VERSION_MINOR, DB_VERSION_PATCH }; 1431 char vstring[3] = { DB_VERSION_MAJOR, DB_VERSION_MINOR, DB_VERSION_PATCH };
1513 1432
1514 newCONSTSUB (stash, "VERSION_v", newSVpvn (vstring, 3)); 1433 newCONSTSUB (bdb_stash, "VERSION_v", newSVpvn (vstring, 3));
1515 } 1434 }
1516 1435
1517 newCONSTSUB (stash, "VERSION_STRING", newSVpv (DB_VERSION_STRING, 0)); 1436 newCONSTSUB (bdb_stash, "VERSION_STRING", newSVpv (DB_VERSION_STRING, 0));
1518 1437
1519 create_respipe (); 1438 create_respipe ();
1520 1439
1521 X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child); 1440 X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child);
1522 patch_errno (); 1441 patch_errno ();
1600 1519
1601int 1520int
1602poll_fileno () 1521poll_fileno ()
1603 PROTOTYPE: 1522 PROTOTYPE:
1604 CODE: 1523 CODE:
1605 RETVAL = respipe [0]; 1524 RETVAL = s_epipe_fd (&respipe);
1606 OUTPUT: 1525 OUTPUT:
1607 RETVAL 1526 RETVAL
1608 1527
1609int 1528int
1610poll_cb (...) 1529poll_cb (...)
1795 req->env = env; 1714 req->env = env;
1796 req->buf1 = strdup_ornull (file); 1715 req->buf1 = strdup_ornull (file);
1797 req->buf2 = strdup_ornull (database); 1716 req->buf2 = strdup_ornull (database);
1798 req->buf3 = strdup_ornull (newname); 1717 req->buf3 = strdup_ornull (newname);
1799 req->uint1 = flags; 1718 req->uint1 = flags;
1719 REQ_SEND;
1720}
1721
1722void
1723db_env_lsn_reset (DB_ENV *env, bdb_filename db, U32 flags = 0, SV *callback = 0)
1724 PREINIT:
1725 CALLBACK
1726 CODE:
1727{
1728 dREQ (REQ_ENV_LSN_RESET, 1);
1729 req->env = env;
1730 req->uint1 = flags;
1731 req->buf1 = strdup_ornull (db);
1800 REQ_SEND; 1732 REQ_SEND;
1801} 1733}
1802 1734
1803void 1735void
1804db_env_log_archive (DB_ENV *env, SV_mutable *listp, U32 flags = 0, SV *callback = 0) 1736db_env_log_archive (DB_ENV *env, SV_mutable *listp, U32 flags = 0, SV *callback = 0)
1856 req->uint1 = flags; 1788 req->uint1 = flags;
1857 req->sv1 = (SV *)db->app_private; 1789 req->sv1 = (SV *)db->app_private;
1858 REQ_SEND; 1790 REQ_SEND;
1859} 1791}
1860 1792
1861#if DB_VERSION_MINOR >= 4 1793#if DBVER >= 404
1862 1794
1863void 1795void
1864db_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) 1796db_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)
1865 PREINIT: 1797 PREINIT:
1866 CALLBACK 1798 CALLBACK
1888 req->uint1 = flags; 1820 req->uint1 = flags;
1889 REQ_SEND; 1821 REQ_SEND;
1890} 1822}
1891 1823
1892void 1824void
1825db_verify (DB *db, bdb_filename file, bdb_filename database = 0, SV *dummy = 0, U32 flags = 0, SV *callback = 0)
1826 PREINIT:
1827 CALLBACK
1828 CODE:
1829{
1830 dREQ (REQ_DB_VERIFY, 1);
1831 ptr_nuke (ST (0)); /* verify destroys the database handle, hopefully it is freed as well */
1832 req->db = db;
1833 req->buf1 = strdup (file);
1834 req->buf2 = strdup_ornull (database);
1835 req->uint1 = flags;
1836 REQ_SEND;
1837}
1838
1839void
1893db_upgrade (DB *db, bdb_filename file, U32 flags = 0, SV *callback = 0) 1840db_upgrade (DB *db, bdb_filename file, U32 flags = 0, SV *callback = 0)
1894 PREINIT: 1841 PREINIT:
1895 CALLBACK 1842 CALLBACK
1896 CODE: 1843 CODE:
1897{ 1844{
1898 dREQ (REQ_DB_SYNC, 1); 1845 dREQ (REQ_DB_UPGRADE, 1);
1899 req->db = db; 1846 req->db = db;
1900 req->buf1 = strdup (file); 1847 req->buf1 = strdup (file);
1901 req->uint1 = flags; 1848 req->uint1 = flags;
1902 REQ_SEND; 1849 REQ_SEND;
1903} 1850}
1930 sv_to_dbt (&req->dbt2, data); 1877 sv_to_dbt (&req->dbt2, data);
1931 req->uint1 = flags; 1878 req->uint1 = flags;
1932 REQ_SEND; 1879 REQ_SEND;
1933} 1880}
1934 1881
1935#if DB_VERSION_MINOR >= 6 1882#if DBVER >= 406
1936 1883
1937void 1884void
1938db_exists (DB *db, DB_TXN_ornull *txn, SV *key, U32 flags = 0, SV *callback = 0) 1885db_exists (DB *db, DB_TXN_ornull *txn, SV *key, U32 flags = 0, SV *callback = 0)
1939 PREINIT: 1886 PREINIT:
1940 CALLBACK 1887 CALLBACK
2173 req->uint1 = flags; 2120 req->uint1 = flags;
2174 REQ_SEND; 2121 REQ_SEND;
2175} 2122}
2176 2123
2177 2124
2178#if DB_VERSION_MINOR >= 3 2125#if DBVER >= 403
2179 2126
2180void 2127void
2181db_sequence_open (DB_SEQUENCE *seq, DB_TXN_ornull *txnid, SV *key, U32 flags = 0, SV *callback = 0) 2128db_sequence_open (DB_SEQUENCE *seq, DB_TXN_ornull *txnid, SV *key, U32 flags = 0, SV *callback = 0)
2182 PREINIT: 2129 PREINIT:
2183 CALLBACK 2130 CALLBACK
2277 CODE: 2224 CODE:
2278 RETVAL = env->set_flags (env, flags, onoff); 2225 RETVAL = env->set_flags (env, flags, onoff);
2279 OUTPUT: 2226 OUTPUT:
2280 RETVAL 2227 RETVAL
2281 2228
2282#if DB_VERSION_MINOR >= 7 2229#if DBVER >= 407
2283 2230
2284int set_intermediate_dir_mode (DB_ENV *env, const char *mode) 2231int set_intermediate_dir_mode (DB_ENV *env, const char *mode)
2285 CODE: 2232 CODE:
2286 RETVAL = env->set_intermediate_dir_mode (env, mode); 2233 RETVAL = env->set_intermediate_dir_mode (env, mode);
2287 OUTPUT: 2234 OUTPUT:
2374 CODE: 2321 CODE:
2375 RETVAL = env->set_lg_max (env, max); 2322 RETVAL = env->set_lg_max (env, max);
2376 OUTPUT: 2323 OUTPUT:
2377 RETVAL 2324 RETVAL
2378 2325
2379#if DB_VERSION_MINOR >= 4 2326#if DBVER >= 404
2380 2327
2381int mutex_set_max (DB_ENV *env, U32 max) 2328int mutex_set_max (DB_ENV *env, U32 max)
2382 CODE: 2329 CODE:
2383 RETVAL = env->mutex_set_max (env, max); 2330 RETVAL = env->mutex_set_max (env, max);
2384 OUTPUT: 2331 OUTPUT:
2411 if (errno) 2358 if (errno)
2412 croak ("DB_ENV->txn_begin: %s", db_strerror (errno)); 2359 croak ("DB_ENV->txn_begin: %s", db_strerror (errno));
2413 OUTPUT: 2360 OUTPUT:
2414 RETVAL 2361 RETVAL
2415 2362
2416#if DB_VERSION_MINOR >= 5 2363#if DBVER >= 405
2417 2364
2418DB_TXN * 2365DB_TXN *
2419cdsgroup_begin (DB_ENV *env) 2366cdsgroup_begin (DB_ENV *env)
2420 CODE: 2367 CODE:
2421 errno = env->cdsgroup_begin (env, &RETVAL); 2368 errno = env->cdsgroup_begin (env, &RETVAL);
2523 if (errno) 2470 if (errno)
2524 croak ("DB->cursor: %s", db_strerror (errno)); 2471 croak ("DB->cursor: %s", db_strerror (errno));
2525 OUTPUT: 2472 OUTPUT:
2526 RETVAL 2473 RETVAL
2527 2474
2528#if DB_VERSION_MINOR >= 3 2475#if DBVER >= 403
2529 2476
2530DB_SEQUENCE * 2477DB_SEQUENCE *
2531sequence (DB *db, U32 flags = 0) 2478sequence (DB *db, U32 flags = 0)
2532 CODE: 2479 CODE:
2533{ 2480{
2568DESTROY (DBC_ornuked *dbc) 2515DESTROY (DBC_ornuked *dbc)
2569 CODE: 2516 CODE:
2570 if (dbc) 2517 if (dbc)
2571 dbc->c_close (dbc); 2518 dbc->c_close (dbc);
2572 2519
2573#if DB_VERSION_MINOR >= 6 2520#if DBVER >= 406
2574 2521
2575int set_priority (DBC *dbc, int priority) 2522int set_priority (DBC *dbc, int priority)
2576 CODE: 2523 CODE:
2577 dbc->set_priority (dbc, priority); 2524 dbc->set_priority (dbc, priority);
2578 2525
2579#endif 2526#endif
2580 2527
2581#if DB_VERSION_MINOR >= 3 2528#if DBVER >= 403
2582 2529
2583MODULE = BDB PACKAGE = BDB::Sequence 2530MODULE = BDB PACKAGE = BDB::Sequence
2584 2531
2585void 2532void
2586DESTROY (DB_SEQUENCE_ornuked *seq) 2533DESTROY (DB_SEQUENCE_ornuked *seq)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines