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

Comparing BDB/BDB.xs (file contents):
Revision 1.30 by root, Sun Jan 13 09:43:21 2008 UTC vs.
Revision 1.40 by root, Tue Jul 8 08:25:31 2008 UTC

47typedef DBC DBC_ornuked; 47typedef DBC DBC_ornuked;
48typedef DB DB_ornuked; 48typedef DB DB_ornuked;
49typedef DB_SEQUENCE DB_SEQUENCE_ornuked; 49typedef DB_SEQUENCE DB_SEQUENCE_ornuked;
50 50
51typedef SV SV8; /* byte-sv, used for argument-checking */ 51typedef SV SV8; /* byte-sv, used for argument-checking */
52typedef char *octetstring; 52typedef char *bdb_filename;
53 53
54static SV *prepare_cb; 54static SV *prepare_cb;
55 55
56#if DB_VERSION_MINOR >= 6 56#if DB_VERSION_MINOR >= 6
57# define c_close close 57# define c_close close
61# define c_get get 61# define c_get get
62# define c_pget pget 62# define c_pget pget
63# define c_put put 63# define c_put put
64#endif 64#endif
65 65
66static char *
67get_bdb_filename (SV *sv)
68{
69 if (!SvOK (sv))
70 return 0;
71
72#if _WIN32
73 /* win32 madness + win32 perl absolutely brokenness make for horrible hacks */
74 {
75 STRLEN len;
76 char *src = SvPVbyte (sv, len);
77 SV *t1 = sv_newmortal ();
78 SV *t2 = sv_newmortal ();
79
80 sv_upgrade (t1, SVt_PV); SvPOK_only (t1); SvGROW (t1, len * 16 + 1);
81 sv_upgrade (t2, SVt_PV); SvPOK_only (t2); SvGROW (t2, len * 16 + 1);
82
83 len = MultiByteToWideChar (CP_ACP, 0, src, len, (WCHAR *)SvPVX (t1), SvLEN (t1) / sizeof (WCHAR));
84 len = WideCharToMultiByte (CP_UTF8, 0, (WCHAR *)SvPVX (t1), len, SvPVX (t2), SvLEN (t2), 0, 0);
85 SvPOK_only (t2);
86 SvPVX (t2)[len] = 0;
87 SvCUR_set (t2, len);
88
89 return SvPVX (t2);
90 }
91#else
92 return SvPVbyte_nolen (sv);
93#endif
94}
95
66static void 96static void
67debug_errcall (const DB_ENV *dbenv, const char *errpfx, const char *msg) 97debug_errcall (const DB_ENV *dbenv, const char *errpfx, const char *msg)
68{ 98{
69 printf ("err[%s]\n", msg); 99 printf ("err[%s]\n", msg);
70} 100}
107} 137}
108 138
109enum { 139enum {
110 REQ_QUIT, 140 REQ_QUIT,
111 REQ_ENV_OPEN, REQ_ENV_CLOSE, REQ_ENV_TXN_CHECKPOINT, REQ_ENV_LOCK_DETECT, 141 REQ_ENV_OPEN, REQ_ENV_CLOSE, REQ_ENV_TXN_CHECKPOINT, REQ_ENV_LOCK_DETECT,
112 REQ_ENV_MEMP_SYNC, REQ_ENV_MEMP_TRICKLE, 142 REQ_ENV_MEMP_SYNC, REQ_ENV_MEMP_TRICKLE, REQ_ENV_DBREMOVE, REQ_ENV_DBRENAME,
113 REQ_DB_OPEN, REQ_DB_CLOSE, REQ_DB_COMPACT, REQ_DB_SYNC, REQ_DB_UPGRADE, 143 REQ_DB_OPEN, REQ_DB_CLOSE, REQ_DB_COMPACT, REQ_DB_SYNC, REQ_DB_UPGRADE,
114 REQ_DB_PUT, REQ_DB_GET, REQ_DB_PGET, REQ_DB_DEL, REQ_DB_KEY_RANGE, 144 REQ_DB_PUT, REQ_DB_GET, REQ_DB_PGET, REQ_DB_DEL, REQ_DB_KEY_RANGE,
115 REQ_TXN_COMMIT, REQ_TXN_ABORT, REQ_TXN_FINISH, 145 REQ_TXN_COMMIT, REQ_TXN_ABORT, REQ_TXN_FINISH,
116 REQ_C_CLOSE, REQ_C_COUNT, REQ_C_PUT, REQ_C_GET, REQ_C_PGET, REQ_C_DEL, 146 REQ_C_CLOSE, REQ_C_COUNT, REQ_C_PUT, REQ_C_GET, REQ_C_PGET, REQ_C_DEL,
117 REQ_SEQ_OPEN, REQ_SEQ_CLOSE, REQ_SEQ_GET, REQ_SEQ_REMOVE, 147 REQ_SEQ_OPEN, REQ_SEQ_CLOSE, REQ_SEQ_GET, REQ_SEQ_REMOVE,
129 DBC *dbc; 159 DBC *dbc;
130 160
131 UV uv1; 161 UV uv1;
132 int int1, int2; 162 int int1, int2;
133 U32 uint1, uint2; 163 U32 uint1, uint2;
134 char *buf1, *buf2; 164 char *buf1, *buf2, *buf3;
135 SV *sv1, *sv2, *sv3; 165 SV *sv1, *sv2, *sv3;
136 166
137 DBT dbt1, dbt2, dbt3; 167 DBT dbt1, dbt2, dbt3;
138 DB_KEY_RANGE key_range; 168 DB_KEY_RANGE key_range;
139 DB_SEQUENCE *seq; 169 DB_SEQUENCE *seq;
151 NUM_PRI = PRI_MAX + PRI_BIAS + 1, 181 NUM_PRI = PRI_MAX + PRI_BIAS + 1,
152}; 182};
153 183
154#define AIO_TICKS ((1000000 + 1023) >> 10) 184#define AIO_TICKS ((1000000 + 1023) >> 10)
155 185
186static SV *on_next_submit;
187
156static unsigned int max_poll_time = 0; 188static unsigned int max_poll_time = 0;
157static unsigned int max_poll_reqs = 0; 189static unsigned int max_poll_reqs = 0;
158 190
159/* calculcate time difference in ~1/AIO_TICKS of a second */ 191/* calculcate time difference in ~1/AIO_TICKS of a second */
160static int tvdiff (struct timeval *tv1, struct timeval *tv2) 192static int tvdiff (struct timeval *tv1, struct timeval *tv2)
205static mutex_t reqlock = X_MUTEX_INIT; 237static mutex_t reqlock = X_MUTEX_INIT;
206static cond_t reqwait = X_COND_INIT; 238static cond_t reqwait = X_COND_INIT;
207 239
208#if WORDACCESS_UNSAFE 240#if WORDACCESS_UNSAFE
209 241
210static unsigned int get_nready () 242static unsigned int get_nready (void)
211{ 243{
212 unsigned int retval; 244 unsigned int retval;
213 245
214 X_LOCK (reqlock); 246 X_LOCK (reqlock);
215 retval = nready; 247 retval = nready;
216 X_UNLOCK (reqlock); 248 X_UNLOCK (reqlock);
217 249
218 return retval; 250 return retval;
219} 251}
220 252
221static unsigned int get_npending () 253static unsigned int get_npending (void)
222{ 254{
223 unsigned int retval; 255 unsigned int retval;
224 256
225 X_LOCK (reslock); 257 X_LOCK (reslock);
226 retval = npending; 258 retval = npending;
227 X_UNLOCK (reslock); 259 X_UNLOCK (reslock);
228 260
229 return retval; 261 return retval;
230} 262}
231 263
232static unsigned int get_nthreads () 264static unsigned int get_nthreads (void)
233{ 265{
234 unsigned int retval; 266 unsigned int retval;
235 267
236 X_LOCK (wrklock); 268 X_LOCK (wrklock);
237 retval = started; 269 retval = started;
300 } 332 }
301 333
302 abort (); 334 abort ();
303} 335}
304 336
305static int poll_cb (); 337static int poll_cb (void);
306static void req_free (bdb_req req); 338static void req_free (bdb_req req);
307static void req_cancel (bdb_req req); 339static void req_cancel (bdb_req req);
308 340
309static int req_invoke (bdb_req req) 341static int req_invoke (bdb_req req)
310{ 342{
381 413
382static void req_free (bdb_req req) 414static void req_free (bdb_req req)
383{ 415{
384 free (req->buf1); 416 free (req->buf1);
385 free (req->buf2); 417 free (req->buf2);
418 free (req->buf3);
386 Safefree (req); 419 Safefree (req);
387} 420}
388 421
389#ifdef USE_SOCKETS_AS_HANDLES 422#ifdef USE_SOCKETS_AS_HANDLES
390# define TO_SOCKET(x) (win32_get_osfhandle (x)) 423# define TO_SOCKET(x) (win32_get_osfhandle (x))
391#else 424#else
392# define TO_SOCKET(x) (x) 425# define TO_SOCKET(x) (x)
393#endif 426#endif
394 427
395static void 428static void
396create_respipe () 429create_respipe (void)
397{ 430{
398#ifdef _WIN32 431#ifdef _WIN32
399 int arg; /* argg */ 432 int arg; /* argg */
400#endif 433#endif
401 int old_readfd = respipe [0]; 434 int old_readfd = respipe [0];
455 free (wrk); 488 free (wrk);
456 489
457 X_UNLOCK (wrklock); 490 X_UNLOCK (wrklock);
458} 491}
459 492
460static void maybe_start_thread () 493static void maybe_start_thread (void)
461{ 494{
462 if (get_nthreads () >= wanted) 495 if (get_nthreads () >= wanted)
463 return; 496 return;
464 497
465 /* todo: maybe use idle here, but might be less exact */ 498 /* todo: maybe use idle here, but might be less exact */
470} 503}
471 504
472static void req_send (bdb_req req) 505static void req_send (bdb_req req)
473{ 506{
474 SV *wait_callback = 0; 507 SV *wait_callback = 0;
508
509 if (on_next_submit)
510 {
511 dSP;
512 SV *cb = sv_2mortal (on_next_submit);
513
514 on_next_submit = 0;
515
516 PUSHMARK (SP);
517 PUTBACK;
518 call_sv (cb, G_DISCARD | G_EVAL);
519 }
475 520
476 // synthesize callback if none given 521 // synthesize callback if none given
477 if (!SvOK (req->callback)) 522 if (!SvOK (req->callback))
478 { 523 {
479 int count; 524 int count;
550 595
551 while (started > wanted) 596 while (started > wanted)
552 end_thread (); 597 end_thread ();
553} 598}
554 599
555static void poll_wait () 600static void poll_wait (void)
556{ 601{
557 fd_set rfd; 602 fd_set rfd;
558 603
559 while (nreqs) 604 while (nreqs)
560 { 605 {
573 618
574 PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0); 619 PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0);
575 } 620 }
576} 621}
577 622
578static int poll_cb () 623static int poll_cb (void)
579{ 624{
580 dSP; 625 dSP;
581 int count = 0; 626 int count = 0;
582 int maxreqs = max_poll_reqs; 627 int maxreqs = max_poll_reqs;
583 int do_croak = 0; 628 int do_croak = 0;
726 req->result = req->env->memp_sync (req->env, 0); 771 req->result = req->env->memp_sync (req->env, 0);
727 break; 772 break;
728 773
729 case REQ_ENV_MEMP_TRICKLE: 774 case REQ_ENV_MEMP_TRICKLE:
730 req->result = req->env->memp_trickle (req->env, req->int1, &req->int2); 775 req->result = req->env->memp_trickle (req->env, req->int1, &req->int2);
776 break;
777
778 case REQ_ENV_DBREMOVE:
779 req->result = req->env->dbremove (req->env, req->txn, req->buf1, req->buf2, req->uint1);
780 break;
781
782 case REQ_ENV_DBRENAME:
783 req->result = req->env->dbrename (req->env, req->txn, req->buf1, req->buf2, req->buf3, req->uint1);
731 break; 784 break;
732 785
733 case REQ_DB_OPEN: 786 case REQ_DB_OPEN:
734 req->result = req->db->open (req->db, req->txn, req->buf1, req->buf2, req->int1, req->uint1, req->int2); 787 req->result = req->db->open (req->db, req->txn, req->buf1, req->buf2, req->int1, req->uint1, req->int2);
735 break; 788 break;
952static void 1005static void
953ptr_nuke (SV *sv) 1006ptr_nuke (SV *sv)
954{ 1007{
955 assert (SvROK (sv)); 1008 assert (SvROK (sv));
956 sv_setiv (SvRV (sv), 0); 1009 sv_setiv (SvRV (sv), 0);
1010}
1011
1012static int
1013errno_get (pTHX_ SV *sv, MAGIC *mg)
1014{
1015 if (*mg->mg_ptr == '!') // should always be the case
1016 if (-30999 <= errno && errno <= -30800)
1017 {
1018 sv_setnv (sv, (NV)errno);
1019 sv_setpv (sv, db_strerror (errno));
1020 SvNOK_on (sv); /* what a wonderful hack! */
1021 // ^^^ copied from perl sources
1022 return 0;
1023 }
1024
1025 return PL_vtbl_sv.svt_get (aTHX_ sv, mg);
1026}
1027
1028static MGVTBL vtbl_errno;
1029
1030// this wonderful hack :( patches perl's $! variable to support our errno values
1031static void
1032patch_errno (void)
1033{
1034 SV *sv;
1035 MAGIC *mg;
1036
1037 if (!(sv = get_sv ("!", 1)))
1038 return;
1039
1040 if (!(mg = mg_find (sv, PERL_MAGIC_sv)))
1041 return;
1042
1043 if (mg->mg_virtual != &PL_vtbl_sv)
1044 return;
1045
1046 vtbl_errno = PL_vtbl_sv;
1047 vtbl_errno.svt_get = errno_get;
1048 mg->mg_virtual = &vtbl_errno;
957} 1049}
958 1050
959MODULE = BDB PACKAGE = BDB 1051MODULE = BDB PACKAGE = BDB
960 1052
961PROTOTYPES: ENABLE 1053PROTOTYPES: ENABLE
988 const_iv (REGISTER) 1080 const_iv (REGISTER)
989 const_iv (SYSTEM_MEM) 1081 const_iv (SYSTEM_MEM)
990 const_iv (AUTO_COMMIT) 1082 const_iv (AUTO_COMMIT)
991 const_iv (CDB_ALLDB) 1083 const_iv (CDB_ALLDB)
992 const_iv (DIRECT_DB) 1084 const_iv (DIRECT_DB)
993 const_iv (DIRECT_LOG)
994 const_iv (DSYNC_DB) 1085 const_iv (DSYNC_DB)
995 const_iv (DSYNC_LOG)
996 const_iv (LOG_AUTOREMOVE)
997 const_iv (LOG_INMEMORY)
998 const_iv (NOLOCKING) 1086 const_iv (NOLOCKING)
999 const_iv (NOMMAP) 1087 const_iv (NOMMAP)
1000 const_iv (NOPANIC) 1088 const_iv (NOPANIC)
1001 const_iv (OVERWRITE) 1089 const_iv (OVERWRITE)
1002 const_iv (PANIC_ENVIRONMENT) 1090 const_iv (PANIC_ENVIRONMENT)
1141 const_iv (PRIORITY_LOW) 1229 const_iv (PRIORITY_LOW)
1142 const_iv (PRIORITY_DEFAULT) 1230 const_iv (PRIORITY_DEFAULT)
1143 const_iv (PRIORITY_HIGH) 1231 const_iv (PRIORITY_HIGH)
1144 const_iv (PRIORITY_VERY_HIGH) 1232 const_iv (PRIORITY_VERY_HIGH)
1145#endif 1233#endif
1234#if DB_VERSION_MINOR >= 7
1235 const_iv (LOG_DIRECT)
1236 const_iv (LOG_DSYNC)
1237 const_iv (LOG_AUTO_REMOVE)
1238 const_iv (LOG_IN_MEMORY)
1239 const_iv (LOG_ZERO)
1240#else
1241 const_iv (DIRECT_LOG)
1242 const_iv (DSYNC_LOG)
1243 const_iv (LOG_AUTOREMOVE)
1244 const_iv (LOG_INMEMORY)
1245#endif
1146 }; 1246 };
1147 1247
1148 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 1248 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
1149 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 1249 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
1150 1250
1151 newCONSTSUB (stash, "VERSION", newSVnv (DB_VERSION_MAJOR + DB_VERSION_MINOR * .1)); 1251 {
1252 /* we currently only allow version, minor-version and patchlevel to go up to 255 */
1253 char vstring[3] = { DB_VERSION_MAJOR, DB_VERSION_MINOR, DB_VERSION_PATCH };
1254
1255 newCONSTSUB (stash, "VERSION_v", newSVpvn (vstring, 3));
1256 }
1257
1152 newCONSTSUB (stash, "VERSION_STRING", newSVpv (DB_VERSION_STRING, 0)); 1258 newCONSTSUB (stash, "VERSION_STRING", newSVpv (DB_VERSION_STRING, 0));
1153 1259
1154 create_respipe (); 1260 create_respipe ();
1155 1261
1156 X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child); 1262 X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child);
1157#ifdef _WIN32 1263 patch_errno ();
1158 X_MUTEX_CHECK (wrklock);
1159 X_MUTEX_CHECK (reslock);
1160 X_MUTEX_CHECK (reqlock);
1161
1162 X_COND_CHECK (reqwait);
1163#endif
1164} 1264}
1165 1265
1166void 1266void
1167max_poll_reqs (int nreqs) 1267max_poll_reqs (int nreqs)
1168 PROTOTYPE: $ 1268 PROTOTYPE: $
1308 CODE: 1408 CODE:
1309 RETVAL = db_strerror (errorno); 1409 RETVAL = db_strerror (errorno);
1310 OUTPUT: 1410 OUTPUT:
1311 RETVAL 1411 RETVAL
1312 1412
1413void _on_next_submit (SV *cb)
1414 CODE:
1415 SvREFCNT_dec (on_next_submit);
1416 on_next_submit = SvOK (cb) ? newSVsv (cb) : 0;
1417
1313DB_ENV * 1418DB_ENV *
1314db_env_create (U32 env_flags = 0) 1419db_env_create (U32 env_flags = 0)
1315 CODE: 1420 CODE:
1316{ 1421{
1317 errno = db_env_create (&RETVAL, env_flags); 1422 errno = db_env_create (&RETVAL, env_flags);
1326} 1431}
1327 OUTPUT: 1432 OUTPUT:
1328 RETVAL 1433 RETVAL
1329 1434
1330void 1435void
1331db_env_open (DB_ENV *env, octetstring db_home, U32 open_flags, int mode, SV *callback = &PL_sv_undef) 1436db_env_open (DB_ENV *env, bdb_filename db_home, U32 open_flags, int mode, SV *callback = &PL_sv_undef)
1332 CODE: 1437 CODE:
1333{ 1438{
1334 dREQ (REQ_ENV_OPEN); 1439 dREQ (REQ_ENV_OPEN);
1335 1440
1336 req->env = env; 1441 req->env = env;
1391 req->env = env; 1496 req->env = env;
1392 req->int1 = percent; 1497 req->int1 = percent;
1393 REQ_SEND; 1498 REQ_SEND;
1394} 1499}
1395 1500
1501void
1502db_env_dbremove (DB_ENV *env, DB_TXN_ornull *txnid, bdb_filename file, bdb_filename database, U32 flags = 0, SV *callback = &PL_sv_undef)
1503 CODE:
1504{
1505 dREQ (REQ_ENV_DBREMOVE);
1506 req->env = env;
1507 req->buf1 = strdup_ornull (file);
1508 req->buf2 = strdup_ornull (database);
1509 req->uint1 = flags;
1510 REQ_SEND;
1511}
1512
1513void
1514db_env_dbrename (DB_ENV *env, DB_TXN_ornull *txnid, bdb_filename file, bdb_filename database, bdb_filename newname, U32 flags = 0, SV *callback = &PL_sv_undef)
1515 CODE:
1516{
1517 dREQ (REQ_ENV_DBRENAME);
1518 req->env = env;
1519 req->buf1 = strdup_ornull (file);
1520 req->buf2 = strdup_ornull (database);
1521 req->buf3 = strdup_ornull (newname);
1522 req->uint1 = flags;
1523 REQ_SEND;
1524}
1396 1525
1397DB * 1526DB *
1398db_create (DB_ENV *env = 0, U32 flags = 0) 1527db_create (DB_ENV *env = 0, U32 flags = 0)
1399 CODE: 1528 CODE:
1400{ 1529{
1407} 1536}
1408 OUTPUT: 1537 OUTPUT:
1409 RETVAL 1538 RETVAL
1410 1539
1411void 1540void
1412db_open (DB *db, DB_TXN_ornull *txnid, octetstring file, octetstring database, int type, U32 flags, int mode, SV *callback = &PL_sv_undef) 1541db_open (DB *db, DB_TXN_ornull *txnid, bdb_filename file, bdb_filename database, int type, U32 flags, int mode, SV *callback = &PL_sv_undef)
1413 CODE: 1542 CODE:
1414{ 1543{
1415 dREQ (REQ_DB_OPEN); 1544 dREQ (REQ_DB_OPEN);
1416 req->db = db; 1545 req->db = db;
1417 req->txn = txnid; 1546 req->txn = txnid;
1457 req->uint1 = flags; 1586 req->uint1 = flags;
1458 REQ_SEND; 1587 REQ_SEND;
1459} 1588}
1460 1589
1461void 1590void
1462db_upgrade (DB *db, octetstring file, U32 flags = 0, SV *callback = &PL_sv_undef) 1591db_upgrade (DB *db, bdb_filename file, U32 flags = 0, SV *callback = &PL_sv_undef)
1463 CODE: 1592 CODE:
1464{ 1593{
1465 dREQ (REQ_DB_SYNC); 1594 dREQ (REQ_DB_SYNC);
1466 req->db = db; 1595 req->db = db;
1467 req->buf1 = strdup (file); 1596 req->buf1 = strdup (file);
1758 CODE: 1887 CODE:
1759 RETVAL = env->set_flags (env, flags, onoff); 1888 RETVAL = env->set_flags (env, flags, onoff);
1760 OUTPUT: 1889 OUTPUT:
1761 RETVAL 1890 RETVAL
1762 1891
1892#if DB_VERSION_MINOR >= 7
1893
1894int set_intermediate_dir_mode (DB_ENV *env, const char *mode)
1895 CODE:
1896 RETVAL = env->set_intermediate_dir_mode (env, mode);
1897 OUTPUT:
1898 RETVAL
1899
1900int log_set_config (DB_ENV *env, U32 flags, int onoff = 1)
1901 CODE:
1902 RETVAL = env->log_set_config (env, flags, onoff);
1903 OUTPUT:
1904 RETVAL
1905
1906#endif
1907
1908
1763void set_errfile (DB_ENV *env, FILE *errfile = 0) 1909void set_errfile (DB_ENV *env, FILE *errfile = 0)
1764 CODE: 1910 CODE:
1765 env->set_errfile (env, errfile); 1911 env->set_errfile (env, errfile);
1766 1912
1767void set_msgfile (DB_ENV *env, FILE *msgfile = 0) 1913void set_msgfile (DB_ENV *env, FILE *msgfile = 0)
2045 CODE: 2191 CODE:
2046 RETVAL = seq->set_range (seq, min, max); 2192 RETVAL = seq->set_range (seq, min, max);
2047 OUTPUT: 2193 OUTPUT:
2048 RETVAL 2194 RETVAL
2049 2195
2196

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines