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

Comparing BDB/BDB.xs (file contents):
Revision 1.65 by root, Fri Jan 2 22:37:51 2009 UTC vs.
Revision 1.79 by root, Tue Feb 2 04:07:06 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#endif
553
554static void 473static void
555create_respipe (void) 474create_respipe (void)
556{ 475{
557#ifdef _WIN32
558 int arg; /* argg */
559#endif
560 int old_readfd = respipe [0];
561
562 if (respipe [1] >= 0)
563 respipe_close (TO_SOCKET (respipe [1]));
564
565#ifdef _WIN32
566 if (ev_pipe (respipe))
567#else
568 if (pipe (respipe)) 476 if (s_epipe_renew (&respipe))
569#endif 477 croak ("BDB: unable to create event pipe");
570 croak ("unable to initialize result pipe");
571
572 if (old_readfd >= 0)
573 {
574 if (dup2 (TO_SOCKET (respipe [0]), TO_SOCKET (old_readfd)) < 0)
575 croak ("unable to initialize result pipe(2)");
576
577 respipe_close (respipe [0]);
578 respipe [0] = old_readfd;
579 }
580
581#ifdef _WIN32
582 arg = 1;
583 if (ioctlsocket (TO_SOCKET (respipe [0]), FIONBIO, &arg)
584 || ioctlsocket (TO_SOCKET (respipe [1]), FIONBIO, &arg))
585#else
586 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)
587 || fcntl (respipe [1], F_SETFL, O_NONBLOCK))
588#endif
589 croak ("unable to initialize result pipe(3)");
590
591 respipe_osf [0] = TO_SOCKET (respipe [0]);
592 respipe_osf [1] = TO_SOCKET (respipe [1]);
593} 478}
594 479
595static void bdb_request (bdb_req req); 480static void bdb_request (bdb_req req);
596X_THREAD_PROC (bdb_proc); 481X_THREAD_PROC (bdb_proc);
597 482
601 486
602 if (!wrk) 487 if (!wrk)
603 croak ("unable to allocate worker thread data"); 488 croak ("unable to allocate worker thread data");
604 489
605 X_LOCK (wrklock); 490 X_LOCK (wrklock);
606 if (thread_create (&wrk->tid, bdb_proc, (void *)wrk)) 491 if (xthread_create (&wrk->tid, bdb_proc, (void *)wrk))
607 { 492 {
608 wrk->prev = &wrk_first; 493 wrk->prev = &wrk_first;
609 wrk->next = wrk_first.next; 494 wrk->next = wrk_first.next;
610 wrk_first.next->prev = wrk; 495 wrk_first.next->prev = wrk;
611 wrk_first.next = wrk; 496 wrk_first.next = wrk;
733 end_thread (); 618 end_thread ();
734} 619}
735 620
736static void poll_wait (void) 621static void poll_wait (void)
737{ 622{
738 fd_set rfd;
739
740 while (nreqs) 623 while (nreqs)
741 { 624 {
742 int size; 625 int size;
743 if (WORDACCESS_UNSAFE) X_LOCK (reslock); 626 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
744 size = res_queue.size; 627 size = res_queue.size;
747 if (size) 630 if (size)
748 return; 631 return;
749 632
750 maybe_start_thread (); 633 maybe_start_thread ();
751 634
752 FD_ZERO (&rfd); 635 s_epipe_wait (&respipe);
753 FD_SET (respipe [0], &rfd);
754
755 PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0);
756 } 636 }
757} 637}
758 638
759static int poll_cb (void) 639static int poll_cb (void)
760{ 640{
780 if (req) 660 if (req)
781 { 661 {
782 --npending; 662 --npending;
783 663
784 if (!res_queue.size) 664 if (!res_queue.size)
785 {
786 /* read any signals sent by the worker threads */ 665 /* read any signals sent by the worker threads */
787 char buf [4]; 666 s_epipe_drain (&respipe);
788 while (respipe_read (respipe [0], buf, 4) == 4)
789 ;
790 }
791 } 667 }
792 668
793 X_UNLOCK (reslock); 669 X_UNLOCK (reslock);
794 670
795 if (!req) 671 if (!req)
875 751
876 case REQ_DB_CLOSE: 752 case REQ_DB_CLOSE:
877 req->result = req->db->close (req->db, req->uint1); 753 req->result = req->db->close (req->db, req->uint1);
878 break; 754 break;
879 755
880#if DB_VERSION_MINOR >= 4 756#if DBVER >= 404
881 case REQ_DB_COMPACT: 757 case REQ_DB_COMPACT:
882 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);
883 break; 759 break;
884#endif 760#endif
885 761
886 case REQ_DB_SYNC: 762 case REQ_DB_SYNC:
887 req->result = req->db->sync (req->db, req->uint1); 763 req->result = req->db->sync (req->db, req->uint1);
888 break; 764 break;
889 765
766 case REQ_DB_VERIFY:
767 req->result = req->db->verify (req->db, req->buf1, req->buf2, 0, req->uint1);
768 break;
769
890 case REQ_DB_UPGRADE: 770 case REQ_DB_UPGRADE:
891 req->result = req->db->upgrade (req->db, req->buf1, req->uint1); 771 req->result = req->db->upgrade (req->db, req->buf1, req->uint1);
892 break; 772 break;
893 773
894 case REQ_DB_PUT: 774 case REQ_DB_PUT:
895 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);
896 break; 776 break;
897 777
898#if DB_VERSION_MINOR >= 6 778#if DBVER >= 406
899 case REQ_DB_EXISTS: 779 case REQ_DB_EXISTS:
900 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);
901 break; 781 break;
902#endif 782#endif
903 case REQ_DB_GET: 783 case REQ_DB_GET:
961 841
962 case REQ_C_DEL: 842 case REQ_C_DEL:
963 req->result = req->dbc->c_del (req->dbc, req->uint1); 843 req->result = req->dbc->c_del (req->dbc, req->uint1);
964 break; 844 break;
965 845
966#if DB_VERSION_MINOR >= 3 846#if DBVER >= 403
967 case REQ_SEQ_OPEN: 847 case REQ_SEQ_OPEN:
968 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);
969 break; 849 break;
970 850
971 case REQ_SEQ_CLOSE: 851 case REQ_SEQ_CLOSE:
978 858
979 case REQ_SEQ_REMOVE: 859 case REQ_SEQ_REMOVE:
980 req->result = req->seq->remove (req->seq, req->txn, req->uint1); 860 req->result = req->seq->remove (req->seq, req->txn, req->uint1);
981 break; 861 break;
982#endif 862#endif
863
864 case REQ_ENV_LSN_RESET:
865 req->result = req->env->lsn_reset (req->env, req->buf1, req->uint1);
866 break;
983 867
984 case REQ_ENV_LOG_ARCHIVE: 868 case REQ_ENV_LOG_ARCHIVE:
985 { 869 {
986 char **listp = 0; /* DB_ARCH_REMOVE does not touch listp, contrary to docs */ 870 char **listp = 0; /* DB_ARCH_REMOVE does not touch listp, contrary to docs */
987 req->result = req->env->log_archive (req->env, &listp, req->uint1); 871 req->result = req->env->log_archive (req->env, &listp, req->uint1);
1007 /* try to distribute timeouts somewhat evenly */ 891 /* try to distribute timeouts somewhat evenly */
1008 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); 892 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
1009 893
1010 for (;;) 894 for (;;)
1011 { 895 {
1012 ts.tv_sec = time (0) + IDLE_TIMEOUT; 896 ts.tv_sec = time (0) + IDLE_TIMEOUT;
1013 897
1014 X_LOCK (reqlock); 898 X_LOCK (reqlock);
1015 899
1016 for (;;) 900 for (;;)
1017 { 901 {
1062 X_LOCK (reslock); 946 X_LOCK (reslock);
1063 947
1064 ++npending; 948 ++npending;
1065 949
1066 if (!reqq_push (&res_queue, req)) 950 if (!reqq_push (&res_queue, req))
1067 /* write a dummy byte to the pipe so fh becomes ready */ 951 s_epipe_signal (&respipe);
1068 respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1);
1069 952
1070 self->req = 0; 953 self->req = 0;
1071 worker_clear (self); 954 worker_clear (self);
1072 955
1073 X_UNLOCK (reslock); 956 X_UNLOCK (reslock);
1135 next_pri = DEFAULT_PRI + PRI_BIAS; \ 1018 next_pri = DEFAULT_PRI + PRI_BIAS; \
1136 \ 1019 \
1137 if (callback && SvOK (callback)) \ 1020 if (callback && SvOK (callback)) \
1138 croak ("callback has illegal type or extra arguments"); \ 1021 croak ("callback has illegal type or extra arguments"); \
1139 \ 1022 \
1140 Newz (0, req, 1, bdb_cb); \ 1023 Newz (0, req, 1, bdb_cb); \
1141 if (!req) \ 1024 if (!req) \
1142 croak ("out of memory during bdb_req allocation"); \ 1025 croak ("out of memory during bdb_req allocation"); \
1143 \ 1026 \
1144 req->callback = SvREFCNT_inc (cb); \ 1027 req->callback = SvREFCNT_inc (cb); \
1145 req->type = (reqtype); \ 1028 req->type = (reqtype); \
1146 req->pri = req_pri; \ 1029 req->pri = req_pri; \
1147 if (rsvcnt >= 1) req->rsv1 = SvREFCNT_inc (ST (0)); \ 1030 if (rsvcnt >= 1) req->rsv1 = SvREFCNT_inc (ST (0)); \
1148 if (rsvcnt >= 2) req->rsv2 = SvREFCNT_inc (ST (1)); \ 1031 if (rsvcnt >= 2) req->rsv2 = SvREFCNT_inc (ST (1)); \
1149 (void)0; 1032 (void)0;
1150 1033
1151#define REQ_SEND \ 1034#define REQ_SEND \
1152 req_send (req) 1035 req_send (req)
1153 1036
1154#define SvPTR(var, arg, type, class, nullok) \ 1037#define SvPTR(var, arg, type, stash, class, nullok) \
1155 if (!SvOK (arg)) \ 1038 if (!SvOK (arg)) \
1156 { \ 1039 { \
1157 if (nullok != 1) \ 1040 if (nullok != 1) \
1158 croak (# var " must be a " # class " object, not undef"); \ 1041 croak (# var " must be a " # class " object, not undef"); \
1159 \ 1042 \
1160 (var) = 0; \ 1043 (var) = 0; \
1161 } \ 1044 } \
1162 else if (sv_derived_from ((arg), # class)) \ 1045 else if (SvSTASH (SvRV (arg)) == stash || sv_derived_from ((arg), # class)) \
1163 { \ 1046 { \
1164 IV tmp = SvIV ((SV*) SvRV (arg)); \ 1047 IV tmp = SvIV ((SV*) SvRV (arg)); \
1165 (var) = INT2PTR (type, tmp); \ 1048 (var) = INT2PTR (type, tmp); \
1166 if (!var && nullok != 2) \ 1049 if (!var && nullok != 2) \
1167 croak (# var " is not a valid " # class " object anymore"); \ 1050 croak (# var " is not a valid " # class " object anymore"); \
1170 croak (# var " is not of type " # class); 1053 croak (# var " is not of type " # class);
1171 1054
1172#define ARG_MUTABLE(name) \ 1055#define ARG_MUTABLE(name) \
1173 if (SvREADONLY (name)) \ 1056 if (SvREADONLY (name)) \
1174 croak ("argument " #name " is read-only/constant, but the request requires it to be mutable"); 1057 croak ("argument " #name " is read-only/constant, but the request requires it to be mutable");
1058
1059static SV *
1060newSVptr (void *ptr, HV *stash)
1061{
1062 SV *rv = NEWSV (0, 0);
1063 sv_upgrade (rv, SVt_PVMG);
1064 sv_setiv (rv, PTR2IV (ptr));
1065
1066 return sv_bless (newRV_noinc (rv), stash);
1067}
1175 1068
1176static void 1069static void
1177ptr_nuke (SV *sv) 1070ptr_nuke (SV *sv)
1178{ 1071{
1179 assert (SvROK (sv)); 1072 assert (SvROK (sv));
1301 1194
1302PROTOTYPES: ENABLE 1195PROTOTYPES: ENABLE
1303 1196
1304BOOT: 1197BOOT:
1305{ 1198{
1306 HV *stash = gv_stashpv ("BDB", 1);
1307
1308 static const struct { 1199 static const struct {
1309 const char *name; 1200 const char *name;
1310 IV iv; 1201 IV iv;
1311 } *civ, const_iv[] = { 1202 } *civ, const_iv[] = {
1312#define const_iv(name) { # name, (IV)DB_ ## name }, 1203#define const_iv(name) { # name, (IV)DB_ ## name },
1204#if DBVER <= 408
1313 const_iv (RPCCLIENT) 1205 const_iv (RPCCLIENT)
1206#endif
1314 const_iv (INIT_CDB) 1207 const_iv (INIT_CDB)
1315 const_iv (INIT_LOCK) 1208 const_iv (INIT_LOCK)
1316 const_iv (INIT_LOG) 1209 const_iv (INIT_LOG)
1317 const_iv (INIT_MPOOL) 1210 const_iv (INIT_MPOOL)
1318 const_iv (INIT_REP) 1211 const_iv (INIT_REP)
1341 const_iv (TXN_NOT_DURABLE) 1234 const_iv (TXN_NOT_DURABLE)
1342 const_iv (TXN_WRITE_NOSYNC) 1235 const_iv (TXN_WRITE_NOSYNC)
1343 const_iv (WRITECURSOR) 1236 const_iv (WRITECURSOR)
1344 const_iv (YIELDCPU) 1237 const_iv (YIELDCPU)
1345 const_iv (ENCRYPT_AES) 1238 const_iv (ENCRYPT_AES)
1239#if DBVER < 408
1346 const_iv (XA_CREATE) 1240 const_iv (XA_CREATE)
1241#endif
1347 const_iv (BTREE) 1242 const_iv (BTREE)
1348 const_iv (HASH) 1243 const_iv (HASH)
1349 const_iv (QUEUE) 1244 const_iv (QUEUE)
1350 const_iv (RECNO) 1245 const_iv (RECNO)
1351 const_iv (UNKNOWN) 1246 const_iv (UNKNOWN)
1415 const_iv (LOCK_OLDEST) 1310 const_iv (LOCK_OLDEST)
1416 const_iv (LOCK_RANDOM) 1311 const_iv (LOCK_RANDOM)
1417 const_iv (LOCK_YOUNGEST) 1312 const_iv (LOCK_YOUNGEST)
1418 1313
1419 const_iv (DONOTINDEX) 1314 const_iv (DONOTINDEX)
1420 const_iv (KEYEMPTY ) 1315 const_iv (KEYEMPTY)
1421 const_iv (KEYEXIST ) 1316 const_iv (KEYEXIST)
1422 const_iv (LOCK_DEADLOCK) 1317 const_iv (LOCK_DEADLOCK)
1423 const_iv (LOCK_NOTGRANTED) 1318 const_iv (LOCK_NOTGRANTED)
1424 const_iv (NOSERVER) 1319 const_iv (NOSERVER)
1320#if DBVER < 502
1425 const_iv (NOSERVER_HOME) 1321 const_iv (NOSERVER_HOME)
1426 const_iv (NOSERVER_ID) 1322 const_iv (NOSERVER_ID)
1323#endif
1427 const_iv (NOTFOUND) 1324 const_iv (NOTFOUND)
1428 const_iv (PAGE_NOTFOUND) 1325 const_iv (PAGE_NOTFOUND)
1429 const_iv (REP_DUPMASTER) 1326 const_iv (REP_DUPMASTER)
1430 const_iv (REP_HANDLE_DEAD) 1327 const_iv (REP_HANDLE_DEAD)
1431 const_iv (REP_HOLDELECTION) 1328 const_iv (REP_HOLDELECTION)
1436 const_iv (REP_UNAVAIL) 1333 const_iv (REP_UNAVAIL)
1437 const_iv (RUNRECOVERY) 1334 const_iv (RUNRECOVERY)
1438 const_iv (SECONDARY_BAD) 1335 const_iv (SECONDARY_BAD)
1439 const_iv (VERIFY_BAD) 1336 const_iv (VERIFY_BAD)
1440 1337
1338 const_iv (SALVAGE)
1339 const_iv (AGGRESSIVE)
1340 const_iv (PRINTABLE)
1341 const_iv (NOORDERCHK)
1342 const_iv (ORDERCHKONLY)
1343
1441 const_iv (ARCH_ABS) 1344 const_iv (ARCH_ABS)
1442 const_iv (ARCH_DATA) 1345 const_iv (ARCH_DATA)
1443 const_iv (ARCH_LOG) 1346 const_iv (ARCH_LOG)
1444 const_iv (ARCH_REMOVE) 1347 const_iv (ARCH_REMOVE)
1445 1348
1449 const_iv (VERB_WAITSFOR) 1352 const_iv (VERB_WAITSFOR)
1450 1353
1451 const_iv (VERSION_MAJOR) 1354 const_iv (VERSION_MAJOR)
1452 const_iv (VERSION_MINOR) 1355 const_iv (VERSION_MINOR)
1453 const_iv (VERSION_PATCH) 1356 const_iv (VERSION_PATCH)
1454#if DB_VERSION_MINOR >= 3 1357 const_iv (LOGVERSION)
1358 const_iv (LOGOLDVER)
1359#if DBVER >= 403
1455 const_iv (INORDER) 1360 const_iv (INORDER)
1456 const_iv (LOCK_MAXWRITE) 1361 const_iv (LOCK_MAXWRITE)
1457 const_iv (SEQ_DEC) 1362 const_iv (SEQ_DEC)
1458 const_iv (SEQ_INC) 1363 const_iv (SEQ_INC)
1459 const_iv (SEQ_WRAP) 1364 const_iv (SEQ_WRAP)
1460 const_iv (BUFFER_SMALL) 1365 const_iv (BUFFER_SMALL)
1461 const_iv (LOG_BUFFER_FULL) 1366 const_iv (LOG_BUFFER_FULL)
1462 const_iv (VERSION_MISMATCH) 1367 const_iv (VERSION_MISMATCH)
1463#endif 1368#endif
1464#if DB_VERSION_MINOR >= 4 1369#if DBVER >= 404
1465 const_iv (REGISTER) 1370 const_iv (REGISTER)
1466 const_iv (DSYNC_DB) 1371 const_iv (DSYNC_DB)
1467 const_iv (READ_COMMITTED) 1372 const_iv (READ_COMMITTED)
1468 const_iv (READ_UNCOMMITTED) 1373 const_iv (READ_UNCOMMITTED)
1469 const_iv (REP_IGNORE) 1374 const_iv (REP_IGNORE)
1471 const_iv (REP_JOIN_FAILURE) 1376 const_iv (REP_JOIN_FAILURE)
1472 const_iv (FREE_SPACE) 1377 const_iv (FREE_SPACE)
1473 const_iv (FREELIST_ONLY) 1378 const_iv (FREELIST_ONLY)
1474 const_iv (VERB_REGISTER) 1379 const_iv (VERB_REGISTER)
1475#endif 1380#endif
1476#if DB_VERSION_MINOR >= 5 1381#if DBVER >= 405
1477 const_iv (MULTIVERSION) 1382 const_iv (MULTIVERSION)
1478 const_iv (TXN_SNAPSHOT) 1383 const_iv (TXN_SNAPSHOT)
1479#endif 1384#endif
1480#if DB_VERSION_MINOR >= 6 1385#if DBVER >= 406
1481 const_iv (PREV_DUP) 1386 const_iv (PREV_DUP)
1482 const_iv (PRIORITY_UNCHANGED) 1387 const_iv (PRIORITY_UNCHANGED)
1483 const_iv (PRIORITY_VERY_LOW) 1388 const_iv (PRIORITY_VERY_LOW)
1484 const_iv (PRIORITY_LOW) 1389 const_iv (PRIORITY_LOW)
1485 const_iv (PRIORITY_DEFAULT) 1390 const_iv (PRIORITY_DEFAULT)
1486 const_iv (PRIORITY_HIGH) 1391 const_iv (PRIORITY_HIGH)
1487 const_iv (PRIORITY_VERY_HIGH) 1392 const_iv (PRIORITY_VERY_HIGH)
1488 const_iv (IGNORE_LEASE) 1393 const_iv (IGNORE_LEASE)
1489#endif 1394#endif
1490#if DB_VERSION_MINOR >= 7 1395#if DBVER >= 407
1491 //const_iv (MULTIPLE_KEY) 1396 //const_iv (MULTIPLE_KEY)
1492 const_iv (LOG_DIRECT) 1397 const_iv (LOG_DIRECT)
1493 const_iv (LOG_DSYNC) 1398 const_iv (LOG_DSYNC)
1494 const_iv (LOG_AUTO_REMOVE) 1399 const_iv (LOG_AUTO_REMOVE)
1495 const_iv (LOG_IN_MEMORY) 1400 const_iv (LOG_IN_MEMORY)
1496 const_iv (LOG_ZERO) 1401 const_iv (LOG_ZERO)
1497#else 1402#else
1498 const_iv (DIRECT_LOG) 1403 const_iv (DIRECT_LOG)
1499 const_iv (LOG_AUTOREMOVE) 1404 const_iv (LOG_AUTOREMOVE)
1500# if DB_VERSION_MINOR >= 3 1405# if DBVER >= 403
1501 const_iv (DSYNC_LOG) 1406 const_iv (DSYNC_LOG)
1502 const_iv (LOG_INMEMORY) 1407 const_iv (LOG_INMEMORY)
1503# endif 1408# endif
1409#if DBVER >= 408
1410 const_iv (LOGVERSION_LATCHING)
1411#endif
1504#endif 1412#endif
1505 }; 1413 };
1506 1414
1415 bdb_stash = gv_stashpv ("BDB" , 1);
1416 bdb_env_stash = gv_stashpv ("BDB::Env" , 1);
1417 bdb_txn_stash = gv_stashpv ("BDB::Txn" , 1);
1418 bdb_cursor_stash = gv_stashpv ("BDB::Cursor" , 1);
1419 bdb_db_stash = gv_stashpv ("BDB::Db" , 1);
1420 bdb_sequence_stash = gv_stashpv ("BDB::Sequence", 1);
1421
1507 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 1422 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--)
1508 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 1423 newCONSTSUB (bdb_stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
1509 1424
1510 prepare_cb = &PL_sv_undef; 1425 prepare_cb = &PL_sv_undef;
1511 1426
1512 { 1427 {
1513 /* we currently only allow version, minor-version and patchlevel to go up to 255 */ 1428 /* we currently only allow version, minor-version and patchlevel to go up to 255 */
1514 char vstring[3] = { DB_VERSION_MAJOR, DB_VERSION_MINOR, DB_VERSION_PATCH }; 1429 char vstring[3] = { DB_VERSION_MAJOR, DB_VERSION_MINOR, DB_VERSION_PATCH };
1515 1430
1516 newCONSTSUB (stash, "VERSION_v", newSVpvn (vstring, 3)); 1431 newCONSTSUB (bdb_stash, "VERSION_v", newSVpvn (vstring, 3));
1517 } 1432 }
1518 1433
1519 newCONSTSUB (stash, "VERSION_STRING", newSVpv (DB_VERSION_STRING, 0)); 1434 newCONSTSUB (bdb_stash, "VERSION_STRING", newSVpv (DB_VERSION_STRING, 0));
1520 1435
1521 create_respipe (); 1436 create_respipe ();
1522 1437
1523 X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child); 1438 X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child);
1524 patch_errno (); 1439 patch_errno ();
1602 1517
1603int 1518int
1604poll_fileno () 1519poll_fileno ()
1605 PROTOTYPE: 1520 PROTOTYPE:
1606 CODE: 1521 CODE:
1607 RETVAL = respipe [0]; 1522 RETVAL = s_epipe_fd (&respipe);
1608 OUTPUT: 1523 OUTPUT:
1609 RETVAL 1524 RETVAL
1610 1525
1611int 1526int
1612poll_cb (...) 1527poll_cb (...)
1797 req->env = env; 1712 req->env = env;
1798 req->buf1 = strdup_ornull (file); 1713 req->buf1 = strdup_ornull (file);
1799 req->buf2 = strdup_ornull (database); 1714 req->buf2 = strdup_ornull (database);
1800 req->buf3 = strdup_ornull (newname); 1715 req->buf3 = strdup_ornull (newname);
1801 req->uint1 = flags; 1716 req->uint1 = flags;
1717 REQ_SEND;
1718}
1719
1720void
1721db_env_lsn_reset (DB_ENV *env, bdb_filename db, U32 flags = 0, SV *callback = 0)
1722 PREINIT:
1723 CALLBACK
1724 CODE:
1725{
1726 dREQ (REQ_ENV_LSN_RESET, 1);
1727 req->env = env;
1728 req->uint1 = flags;
1729 req->buf1 = strdup_ornull (db);
1802 REQ_SEND; 1730 REQ_SEND;
1803} 1731}
1804 1732
1805void 1733void
1806db_env_log_archive (DB_ENV *env, SV_mutable *listp, U32 flags = 0, SV *callback = 0) 1734db_env_log_archive (DB_ENV *env, SV_mutable *listp, U32 flags = 0, SV *callback = 0)
1858 req->uint1 = flags; 1786 req->uint1 = flags;
1859 req->sv1 = (SV *)db->app_private; 1787 req->sv1 = (SV *)db->app_private;
1860 REQ_SEND; 1788 REQ_SEND;
1861} 1789}
1862 1790
1863#if DB_VERSION_MINOR >= 4 1791#if DBVER >= 404
1864 1792
1865void 1793void
1866db_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) 1794db_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)
1867 PREINIT: 1795 PREINIT:
1868 CALLBACK 1796 CALLBACK
1890 req->uint1 = flags; 1818 req->uint1 = flags;
1891 REQ_SEND; 1819 REQ_SEND;
1892} 1820}
1893 1821
1894void 1822void
1823db_verify (DB *db, bdb_filename file, bdb_filename database = 0, SV *dummy = 0, U32 flags = 0, SV *callback = 0)
1824 PREINIT:
1825 CALLBACK
1826 CODE:
1827{
1828 dREQ (REQ_DB_VERIFY, 1);
1829 ptr_nuke (ST (0)); /* verify destroys the database handle, hopefully it is freed as well */
1830 req->db = db;
1831 req->buf1 = strdup (file);
1832 req->buf2 = strdup_ornull (database);
1833 req->uint1 = flags;
1834 REQ_SEND;
1835}
1836
1837void
1895db_upgrade (DB *db, bdb_filename file, U32 flags = 0, SV *callback = 0) 1838db_upgrade (DB *db, bdb_filename file, U32 flags = 0, SV *callback = 0)
1896 PREINIT: 1839 PREINIT:
1897 CALLBACK 1840 CALLBACK
1898 CODE: 1841 CODE:
1899{ 1842{
1900 dREQ (REQ_DB_SYNC, 1); 1843 dREQ (REQ_DB_UPGRADE, 1);
1901 req->db = db; 1844 req->db = db;
1902 req->buf1 = strdup (file); 1845 req->buf1 = strdup (file);
1903 req->uint1 = flags; 1846 req->uint1 = flags;
1904 REQ_SEND; 1847 REQ_SEND;
1905} 1848}
1932 sv_to_dbt (&req->dbt2, data); 1875 sv_to_dbt (&req->dbt2, data);
1933 req->uint1 = flags; 1876 req->uint1 = flags;
1934 REQ_SEND; 1877 REQ_SEND;
1935} 1878}
1936 1879
1937#if DB_VERSION_MINOR >= 6 1880#if DBVER >= 406
1938 1881
1939void 1882void
1940db_exists (DB *db, DB_TXN_ornull *txn, SV *key, U32 flags = 0, SV *callback = 0) 1883db_exists (DB *db, DB_TXN_ornull *txn, SV *key, U32 flags = 0, SV *callback = 0)
1941 PREINIT: 1884 PREINIT:
1942 CALLBACK 1885 CALLBACK
2175 req->uint1 = flags; 2118 req->uint1 = flags;
2176 REQ_SEND; 2119 REQ_SEND;
2177} 2120}
2178 2121
2179 2122
2180#if DB_VERSION_MINOR >= 3 2123#if DBVER >= 403
2181 2124
2182void 2125void
2183db_sequence_open (DB_SEQUENCE *seq, DB_TXN_ornull *txnid, SV *key, U32 flags = 0, SV *callback = 0) 2126db_sequence_open (DB_SEQUENCE *seq, DB_TXN_ornull *txnid, SV *key, U32 flags = 0, SV *callback = 0)
2184 PREINIT: 2127 PREINIT:
2185 CALLBACK 2128 CALLBACK
2279 CODE: 2222 CODE:
2280 RETVAL = env->set_flags (env, flags, onoff); 2223 RETVAL = env->set_flags (env, flags, onoff);
2281 OUTPUT: 2224 OUTPUT:
2282 RETVAL 2225 RETVAL
2283 2226
2284#if DB_VERSION_MINOR >= 7 2227#if DBVER >= 407
2285 2228
2286int set_intermediate_dir_mode (DB_ENV *env, const char *mode) 2229int set_intermediate_dir_mode (DB_ENV *env, const char *mode)
2287 CODE: 2230 CODE:
2288 RETVAL = env->set_intermediate_dir_mode (env, mode); 2231 RETVAL = env->set_intermediate_dir_mode (env, mode);
2289 OUTPUT: 2232 OUTPUT:
2376 CODE: 2319 CODE:
2377 RETVAL = env->set_lg_max (env, max); 2320 RETVAL = env->set_lg_max (env, max);
2378 OUTPUT: 2321 OUTPUT:
2379 RETVAL 2322 RETVAL
2380 2323
2381#if DB_VERSION_MINOR >= 4 2324#if DBVER >= 404
2382 2325
2383int mutex_set_max (DB_ENV *env, U32 max) 2326int mutex_set_max (DB_ENV *env, U32 max)
2384 CODE: 2327 CODE:
2385 RETVAL = env->mutex_set_max (env, max); 2328 RETVAL = env->mutex_set_max (env, max);
2386 OUTPUT: 2329 OUTPUT:
2413 if (errno) 2356 if (errno)
2414 croak ("DB_ENV->txn_begin: %s", db_strerror (errno)); 2357 croak ("DB_ENV->txn_begin: %s", db_strerror (errno));
2415 OUTPUT: 2358 OUTPUT:
2416 RETVAL 2359 RETVAL
2417 2360
2418#if DB_VERSION_MINOR >= 5 2361#if DBVER >= 405
2419 2362
2420DB_TXN * 2363DB_TXN *
2421cdsgroup_begin (DB_ENV *env) 2364cdsgroup_begin (DB_ENV *env)
2422 CODE: 2365 CODE:
2423 errno = env->cdsgroup_begin (env, &RETVAL); 2366 errno = env->cdsgroup_begin (env, &RETVAL);
2525 if (errno) 2468 if (errno)
2526 croak ("DB->cursor: %s", db_strerror (errno)); 2469 croak ("DB->cursor: %s", db_strerror (errno));
2527 OUTPUT: 2470 OUTPUT:
2528 RETVAL 2471 RETVAL
2529 2472
2530#if DB_VERSION_MINOR >= 3 2473#if DBVER >= 403
2531 2474
2532DB_SEQUENCE * 2475DB_SEQUENCE *
2533sequence (DB *db, U32 flags = 0) 2476sequence (DB *db, U32 flags = 0)
2534 CODE: 2477 CODE:
2535{ 2478{
2570DESTROY (DBC_ornuked *dbc) 2513DESTROY (DBC_ornuked *dbc)
2571 CODE: 2514 CODE:
2572 if (dbc) 2515 if (dbc)
2573 dbc->c_close (dbc); 2516 dbc->c_close (dbc);
2574 2517
2575#if DB_VERSION_MINOR >= 6 2518#if DBVER >= 406
2576 2519
2577int set_priority (DBC *dbc, int priority) 2520int set_priority (DBC *dbc, int priority)
2578 CODE: 2521 CODE:
2579 dbc->set_priority (dbc, priority); 2522 dbc->set_priority (dbc, priority);
2580 2523
2581#endif 2524#endif
2582 2525
2583#if DB_VERSION_MINOR >= 3 2526#if DBVER >= 403
2584 2527
2585MODULE = BDB PACKAGE = BDB::Sequence 2528MODULE = BDB PACKAGE = BDB::Sequence
2586 2529
2587void 2530void
2588DESTROY (DB_SEQUENCE_ornuked *seq) 2531DESTROY (DB_SEQUENCE_ornuked *seq)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines