--- BDB/BDB.xs 2007/12/22 07:33:48 1.28 +++ BDB/BDB.xs 2008/07/08 08:25:31 1.40 @@ -42,8 +42,14 @@ typedef DB DB_ornull; typedef DB_SEQUENCE DB_SEQUENCE_ornull; +typedef DB_ENV DB_ENV_ornuked; +typedef DB_TXN DB_TXN_ornuked; +typedef DBC DBC_ornuked; +typedef DB DB_ornuked; +typedef DB_SEQUENCE DB_SEQUENCE_ornuked; + typedef SV SV8; /* byte-sv, used for argument-checking */ -typedef char *octetstring; +typedef char *bdb_filename; static SV *prepare_cb; @@ -57,6 +63,36 @@ # define c_put put #endif +static char * +get_bdb_filename (SV *sv) +{ + if (!SvOK (sv)) + return 0; + +#if _WIN32 + /* win32 madness + win32 perl absolutely brokenness make for horrible hacks */ + { + STRLEN len; + char *src = SvPVbyte (sv, len); + SV *t1 = sv_newmortal (); + SV *t2 = sv_newmortal (); + + sv_upgrade (t1, SVt_PV); SvPOK_only (t1); SvGROW (t1, len * 16 + 1); + sv_upgrade (t2, SVt_PV); SvPOK_only (t2); SvGROW (t2, len * 16 + 1); + + len = MultiByteToWideChar (CP_ACP, 0, src, len, (WCHAR *)SvPVX (t1), SvLEN (t1) / sizeof (WCHAR)); + len = WideCharToMultiByte (CP_UTF8, 0, (WCHAR *)SvPVX (t1), len, SvPVX (t2), SvLEN (t2), 0, 0); + SvPOK_only (t2); + SvPVX (t2)[len] = 0; + SvCUR_set (t2, len); + + return SvPVX (t2); + } +#else + return SvPVbyte_nolen (sv); +#endif +} + static void debug_errcall (const DB_ENV *dbenv, const char *errpfx, const char *msg) { @@ -103,7 +139,7 @@ enum { REQ_QUIT, REQ_ENV_OPEN, REQ_ENV_CLOSE, REQ_ENV_TXN_CHECKPOINT, REQ_ENV_LOCK_DETECT, - REQ_ENV_MEMP_SYNC, REQ_ENV_MEMP_TRICKLE, + REQ_ENV_MEMP_SYNC, REQ_ENV_MEMP_TRICKLE, REQ_ENV_DBREMOVE, REQ_ENV_DBRENAME, REQ_DB_OPEN, REQ_DB_CLOSE, REQ_DB_COMPACT, REQ_DB_SYNC, REQ_DB_UPGRADE, REQ_DB_PUT, REQ_DB_GET, REQ_DB_PGET, REQ_DB_DEL, REQ_DB_KEY_RANGE, REQ_TXN_COMMIT, REQ_TXN_ABORT, REQ_TXN_FINISH, @@ -125,7 +161,7 @@ UV uv1; int int1, int2; U32 uint1, uint2; - char *buf1, *buf2; + char *buf1, *buf2, *buf3; SV *sv1, *sv2, *sv3; DBT dbt1, dbt2, dbt3; @@ -147,6 +183,8 @@ #define AIO_TICKS ((1000000 + 1023) >> 10) +static SV *on_next_submit; + static unsigned int max_poll_time = 0; static unsigned int max_poll_reqs = 0; @@ -201,7 +239,7 @@ #if WORDACCESS_UNSAFE -static unsigned int get_nready () +static unsigned int get_nready (void) { unsigned int retval; @@ -212,7 +250,7 @@ return retval; } -static unsigned int get_npending () +static unsigned int get_npending (void) { unsigned int retval; @@ -223,7 +261,7 @@ return retval; } -static unsigned int get_nthreads () +static unsigned int get_nthreads (void) { unsigned int retval; @@ -296,7 +334,7 @@ abort (); } -static int poll_cb (); +static int poll_cb (void); static void req_free (bdb_req req); static void req_cancel (bdb_req req); @@ -377,6 +415,7 @@ { free (req->buf1); free (req->buf2); + free (req->buf3); Safefree (req); } @@ -387,7 +426,7 @@ #endif static void -create_respipe () +create_respipe (void) { #ifdef _WIN32 int arg; /* argg */ @@ -451,7 +490,7 @@ X_UNLOCK (wrklock); } -static void maybe_start_thread () +static void maybe_start_thread (void) { if (get_nthreads () >= wanted) return; @@ -467,6 +506,18 @@ { SV *wait_callback = 0; + if (on_next_submit) + { + dSP; + SV *cb = sv_2mortal (on_next_submit); + + on_next_submit = 0; + + PUSHMARK (SP); + PUTBACK; + call_sv (cb, G_DISCARD | G_EVAL); + } + // synthesize callback if none given if (!SvOK (req->callback)) { @@ -546,7 +597,7 @@ end_thread (); } -static void poll_wait () +static void poll_wait (void) { fd_set rfd; @@ -569,7 +620,7 @@ } } -static int poll_cb () +static int poll_cb (void) { dSP; int count = 0; @@ -724,6 +775,14 @@ req->result = req->env->memp_trickle (req->env, req->int1, &req->int2); break; + case REQ_ENV_DBREMOVE: + req->result = req->env->dbremove (req->env, req->txn, req->buf1, req->buf2, req->uint1); + break; + + case REQ_ENV_DBRENAME: + req->result = req->env->dbrename (req->env, req->txn, req->buf1, req->buf2, req->buf3, req->uint1); + break; + case REQ_DB_OPEN: req->result = req->db->open (req->db, req->txn, req->buf1, req->buf2, req->int1, req->uint1, req->int2); break; @@ -924,10 +983,10 @@ #define REQ_SEND \ req_send (req) -#define SvPTR(var, arg, type, class, nullok) \ +#define SvPTR(var, arg, type, class, nullok) \ if (!SvOK (arg)) \ { \ - if (!nullok) \ + if (nullok != 1) \ croak (# var " must be a " # class " object, not undef"); \ \ (var) = 0; \ @@ -936,7 +995,7 @@ { \ IV tmp = SvIV ((SV*) SvRV (arg)); \ (var) = INT2PTR (type, tmp); \ - if (!var) \ + if (!var && nullok != 2) \ croak (# var " is not a valid " # class " object anymore"); \ } \ else \ @@ -950,6 +1009,45 @@ sv_setiv (SvRV (sv), 0); } +static int +errno_get (pTHX_ SV *sv, MAGIC *mg) +{ + if (*mg->mg_ptr == '!') // should always be the case + if (-30999 <= errno && errno <= -30800) + { + sv_setnv (sv, (NV)errno); + sv_setpv (sv, db_strerror (errno)); + SvNOK_on (sv); /* what a wonderful hack! */ + // ^^^ copied from perl sources + return 0; + } + + return PL_vtbl_sv.svt_get (aTHX_ sv, mg); +} + +static MGVTBL vtbl_errno; + +// this wonderful hack :( patches perl's $! variable to support our errno values +static void +patch_errno (void) +{ + SV *sv; + MAGIC *mg; + + if (!(sv = get_sv ("!", 1))) + return; + + if (!(mg = mg_find (sv, PERL_MAGIC_sv))) + return; + + if (mg->mg_virtual != &PL_vtbl_sv) + return; + + vtbl_errno = PL_vtbl_sv; + vtbl_errno.svt_get = errno_get; + mg->mg_virtual = &vtbl_errno; +} + MODULE = BDB PACKAGE = BDB PROTOTYPES: ENABLE @@ -984,11 +1082,7 @@ const_iv (AUTO_COMMIT) const_iv (CDB_ALLDB) const_iv (DIRECT_DB) - const_iv (DIRECT_LOG) const_iv (DSYNC_DB) - const_iv (DSYNC_LOG) - const_iv (LOG_AUTOREMOVE) - const_iv (LOG_INMEMORY) const_iv (NOLOCKING) const_iv (NOMMAP) const_iv (NOPANIC) @@ -1137,24 +1231,36 @@ const_iv (PRIORITY_HIGH) const_iv (PRIORITY_VERY_HIGH) #endif +#if DB_VERSION_MINOR >= 7 + const_iv (LOG_DIRECT) + const_iv (LOG_DSYNC) + const_iv (LOG_AUTO_REMOVE) + const_iv (LOG_IN_MEMORY) + const_iv (LOG_ZERO) +#else + const_iv (DIRECT_LOG) + const_iv (DSYNC_LOG) + const_iv (LOG_AUTOREMOVE) + const_iv (LOG_INMEMORY) +#endif }; for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); - newCONSTSUB (stash, "VERSION", newSVnv (DB_VERSION_MAJOR + DB_VERSION_MINOR * .1)); + { + /* we currently only allow version, minor-version and patchlevel to go up to 255 */ + char vstring[3] = { DB_VERSION_MAJOR, DB_VERSION_MINOR, DB_VERSION_PATCH }; + + newCONSTSUB (stash, "VERSION_v", newSVpvn (vstring, 3)); + } + newCONSTSUB (stash, "VERSION_STRING", newSVpv (DB_VERSION_STRING, 0)); create_respipe (); X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child); -#ifdef _WIN32 - X_MUTEX_CHECK (wrklock); - X_MUTEX_CHECK (reslock); - X_MUTEX_CHECK (reqlock); - - X_COND_CHECK (reqwait); -#endif + patch_errno (); } void @@ -1304,6 +1410,11 @@ OUTPUT: RETVAL +void _on_next_submit (SV *cb) + CODE: + SvREFCNT_dec (on_next_submit); + on_next_submit = SvOK (cb) ? newSVsv (cb) : 0; + DB_ENV * db_env_create (U32 env_flags = 0) CODE: @@ -1322,13 +1433,11 @@ RETVAL void -db_env_open (DB_ENV *env, octetstring db_home, U32 open_flags, int mode, SV *callback = &PL_sv_undef) +db_env_open (DB_ENV *env, bdb_filename db_home, U32 open_flags, int mode, SV *callback = &PL_sv_undef) CODE: { dREQ (REQ_ENV_OPEN); - env->set_thread_count (env, wanted + 2); - req->env = env; req->uint1 = open_flags | DB_THREAD; req->int1 = mode; @@ -1389,6 +1498,30 @@ REQ_SEND; } +void +db_env_dbremove (DB_ENV *env, DB_TXN_ornull *txnid, bdb_filename file, bdb_filename database, U32 flags = 0, SV *callback = &PL_sv_undef) + CODE: +{ + dREQ (REQ_ENV_DBREMOVE); + req->env = env; + req->buf1 = strdup_ornull (file); + req->buf2 = strdup_ornull (database); + req->uint1 = flags; + REQ_SEND; +} + +void +db_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) + CODE: +{ + dREQ (REQ_ENV_DBRENAME); + req->env = env; + req->buf1 = strdup_ornull (file); + req->buf2 = strdup_ornull (database); + req->buf3 = strdup_ornull (newname); + req->uint1 = flags; + REQ_SEND; +} DB * db_create (DB_ENV *env = 0, U32 flags = 0) @@ -1405,7 +1538,7 @@ RETVAL void -db_open (DB *db, DB_TXN_ornull *txnid, octetstring file, octetstring database, int type, U32 flags, int mode, SV *callback = &PL_sv_undef) +db_open (DB *db, DB_TXN_ornull *txnid, bdb_filename file, bdb_filename database, int type, U32 flags, int mode, SV *callback = &PL_sv_undef) CODE: { dREQ (REQ_DB_OPEN); @@ -1455,7 +1588,7 @@ } void -db_upgrade (DB *db, octetstring file, U32 flags = 0, SV *callback = &PL_sv_undef) +db_upgrade (DB *db, bdb_filename file, U32 flags = 0, SV *callback = &PL_sv_undef) CODE: { dREQ (REQ_DB_SYNC); @@ -1715,7 +1848,7 @@ MODULE = BDB PACKAGE = BDB::Env void -DESTROY (DB_ENV_ornull *env) +DESTROY (DB_ENV_ornuked *env) CODE: if (env) env->close (env, 0); @@ -1756,6 +1889,23 @@ OUTPUT: RETVAL +#if DB_VERSION_MINOR >= 7 + +int set_intermediate_dir_mode (DB_ENV *env, const char *mode) + CODE: + RETVAL = env->set_intermediate_dir_mode (env, mode); + OUTPUT: + RETVAL + +int log_set_config (DB_ENV *env, U32 flags, int onoff = 1) + CODE: + RETVAL = env->log_set_config (env, flags, onoff); + OUTPUT: + RETVAL + +#endif + + void set_errfile (DB_ENV *env, FILE *errfile = 0) CODE: env->set_errfile (env, errfile); @@ -1872,7 +2022,7 @@ MODULE = BDB PACKAGE = BDB::Db void -DESTROY (DB_ornull *db) +DESTROY (DB_ornuked *db) CODE: if (db) { @@ -1977,7 +2127,7 @@ MODULE = BDB PACKAGE = BDB::Txn void -DESTROY (DB_TXN_ornull *txn) +DESTROY (DB_TXN_ornuked *txn) CODE: if (txn) txn->abort (txn); @@ -1998,7 +2148,7 @@ MODULE = BDB PACKAGE = BDB::Cursor void -DESTROY (DBC_ornull *dbc) +DESTROY (DBC_ornuked *dbc) CODE: if (dbc) dbc->c_close (dbc); @@ -2014,7 +2164,7 @@ MODULE = BDB PACKAGE = BDB::Sequence void -DESTROY (DB_SEQUENCE_ornull *seq) +DESTROY (DB_SEQUENCE_ornuked *seq) CODE: if (seq) seq->close (seq, 0); @@ -2043,3 +2193,4 @@ OUTPUT: RETVAL +