summaryrefslogtreecommitdiff
path: root/ext/DB_File/DB_File.xs
diff options
context:
space:
mode:
Diffstat (limited to 'ext/DB_File/DB_File.xs')
-rw-r--r--ext/DB_File/DB_File.xs628
1 files changed, 500 insertions, 128 deletions
diff --git a/ext/DB_File/DB_File.xs b/ext/DB_File/DB_File.xs
index b6e8a03792..f77757caa4 100644
--- a/ext/DB_File/DB_File.xs
+++ b/ext/DB_File/DB_File.xs
@@ -3,8 +3,8 @@
DB_File.xs -- Perl 5 interface to Berkeley DB
written by Paul Marquess (pmarquess@bfsec.bt.co.uk)
- last modified 8th Oct 1997
- version 1.16
+ last modified 20th Nov 1997
+ version 1.56
All comments/suggestions/problems are welcome
@@ -39,12 +39,19 @@
in-memory files when db 1.86 is used.
1.11 - No change to DB_File.xs
1.12 - No change to DB_File.xs
- 1.13 - Tidied up a few casts.
- 1.14 - Made it illegal to tie an associative array to a RECNO
- database and an ordinary array to a HASH or BTREE database.
- 1.15 - Patch from Gisle Aas <gisle@aas.no> to suppress "use of
+ 1.13 - Tidied up a few casts.
+ 1.14 - Made it illegal to tie an associative array to a RECNO
+ database and an ordinary array to a HASH or BTREE database.
+ 1.50 - Make work with both DB 1.x or DB 2.x
+ 1.51 - Fixed a bug in mapping 1.x O_RDONLY flag to 2.x DB_RDONLY equivalent
+ 1.52 - Patch from Gisle Aas <gisle@aas.no> to suppress "use of
undefined value" warning with db_get and db_seq.
- 1.16 - Minor additions to DB_File.xs to support multithreaded perl.
+ 1.53 - Added DB_RENUMBER to flags for recno.
+ 1.54 - Fixed bug in the fd method
+ 1.55 - Fix for AIX from Jarkko Hietaniemi
+ 1.56 - No change to DB_File.xs
+
+
*/
@@ -55,15 +62,94 @@
/* Being the Berkeley DB we prefer the <sys/cdefs.h> (which will be
* shortly #included by the <db.h>) __attribute__ to the possibly
* already defined __attribute__, for example by GNUC or by Perl. */
+
#undef __attribute__
#include <db.h>
-/* #ifdef DB_VERSION_MAJOR */
-/* #include <db_185.h> */
-/* #endif */
#include <fcntl.h>
+/* #define TRACE */
+
+
+
+#ifdef DB_VERSION_MAJOR
+
+/* map version 2 features & constants onto their version 1 equivalent */
+
+#ifdef DB_Prefix_t
+#undef DB_Prefix_t
+#endif
+#define DB_Prefix_t size_t
+
+#ifdef DB_Hash_t
+#undef DB_Hash_t
+#endif
+#define DB_Hash_t u_int32_t
+
+/* DBTYPE stays the same */
+/* HASHINFO, RECNOINFO and BTREEINFO map to DB_INFO */
+typedef DB_INFO INFO ;
+
+/* version 2 has db_recno_t in place of recno_t */
+typedef db_recno_t recno_t;
+
+
+#define R_CURSOR DB_SET_RANGE
+#define R_FIRST DB_FIRST
+#define R_IAFTER DB_AFTER
+#define R_IBEFORE DB_BEFORE
+#define R_LAST DB_LAST
+#define R_NEXT DB_NEXT
+#define R_NOOVERWRITE DB_NOOVERWRITE
+#define R_PREV DB_PREV
+#define R_SETCURSOR 0
+#define R_RECNOSYNC 0
+#define R_FIXEDLEN DB_FIXEDLEN
+#define R_DUP DB_DUP
+
+#define db_HA_hash h_hash
+#define db_HA_ffactor h_ffactor
+#define db_HA_nelem h_nelem
+#define db_HA_bsize db_pagesize
+#define db_HA_cachesize db_cachesize
+#define db_HA_lorder db_lorder
+
+#define db_BT_compare bt_compare
+#define db_BT_prefix bt_prefix
+#define db_BT_flags flags
+#define db_BT_psize db_pagesize
+#define db_BT_cachesize db_cachesize
+#define db_BT_lorder db_lorder
+#define db_BT_maxkeypage
+#define db_BT_minkeypage
+
+
+#define db_RE_reclen re_len
+#define db_RE_flags flags
+#define db_RE_bval re_pad
+#define db_RE_bfname re_source
+#define db_RE_psize db_pagesize
+#define db_RE_cachesize db_cachesize
+#define db_RE_lorder db_lorder
+
+#define TXN NULL,
+
+#define do_SEQ(db, key, value, flag) (db->cursor->c_get)(db->cursor, &key, &value, flag)
+
+
+#define DBT_flags(x) x.flags = 0
+#define DB_flags(x, v) x |= v
+
+#else /* db version 1.x */
+
+typedef union INFO {
+ HASHINFO hash ;
+ RECNOINFO recno ;
+ BTREEINFO btree ;
+ } INFO ;
+
+
#ifdef mDB_Prefix_t
#ifdef DB_Prefix_t
#undef DB_Prefix_t
@@ -78,11 +164,63 @@
#define DB_Hash_t mDB_Hash_t
#endif
-union INFO {
- HASHINFO hash ;
- RECNOINFO recno ;
- BTREEINFO btree ;
- } ;
+#define db_HA_hash hash.hash
+#define db_HA_ffactor hash.ffactor
+#define db_HA_nelem hash.nelem
+#define db_HA_bsize hash.bsize
+#define db_HA_cachesize hash.cachesize
+#define db_HA_lorder hash.lorder
+
+#define db_BT_compare btree.compare
+#define db_BT_prefix btree.prefix
+#define db_BT_flags btree.flags
+#define db_BT_psize btree.psize
+#define db_BT_cachesize btree.cachesize
+#define db_BT_lorder btree.lorder
+#define db_BT_maxkeypage btree.maxkeypage
+#define db_BT_minkeypage btree.minkeypage
+
+#define db_RE_reclen recno.reclen
+#define db_RE_flags recno.flags
+#define db_RE_bval recno.bval
+#define db_RE_bfname recno.bfname
+#define db_RE_psize recno.psize
+#define db_RE_cachesize recno.cachesize
+#define db_RE_lorder recno.lorder
+
+#define TXN
+
+#define do_SEQ(db, key, value, flag) (db->dbp->seq)(db->dbp, &key, &value, flag)
+#define DBT_flags(x)
+#define DB_flags(x, v)
+
+#endif /* db version 1 */
+
+
+
+#define db_DELETE(db, key, flags) ((db->dbp)->del)(db->dbp, TXN &key, flags)
+#define db_STORE(db, key, value, flags) ((db->dbp)->put)(db->dbp, TXN &key, &value, flags)
+#define db_FETCH(db, key, flags) ((db->dbp)->get)(db->dbp, TXN &key, &value, flags)
+
+#define db_sync(db, flags) ((db->dbp)->sync)(db->dbp, flags)
+#define db_get(db, key, value, flags) ((db->dbp)->get)(db->dbp, TXN &key, &value, flags)
+#ifdef DB_VERSION_MAJOR
+#define db_DESTROY(db) ((db->dbp)->close)(db->dbp, 0)
+#define db_close(db) ((db->dbp)->close)(db->dbp, 0)
+#define db_del(db, key, flags) ((flags & R_CURSOR) \
+ ? ((db->cursor)->c_del)(db->cursor, 0) \
+ : ((db->dbp)->del)(db->dbp, NULL, &key, flags) )
+
+#else
+
+#define db_DESTROY(db) ((db->dbp)->close)(db->dbp)
+#define db_close(db) ((db->dbp)->close)(db->dbp)
+#define db_del(db, key, flags) ((db->dbp)->del)(db->dbp, &key, flags)
+#define db_put(db, key, value, flags) ((db->dbp)->put)(db->dbp, &key, &value, flags)
+
+#endif
+
+#define db_seq(db, key, value, flags) do_SEQ(db, key, value, flags)
typedef struct {
DBTYPE type ;
@@ -91,31 +229,16 @@ typedef struct {
SV * prefix ;
SV * hash ;
int in_memory ;
- union INFO info ;
+ INFO info ;
+#ifdef DB_VERSION_MAJOR
+ DBC * cursor ;
+#endif
} DB_File_type;
typedef DB_File_type * DB_File ;
typedef DBT DBTKEY ;
-/* #define TRACE */
-
-#define db_DESTROY(db) ((db->dbp)->close)(db->dbp)
-#define db_DELETE(db, key, flags) ((db->dbp)->del)(db->dbp, &key, flags)
-#define db_STORE(db, key, value, flags) ((db->dbp)->put)(db->dbp, &key, &value, flags)
-#define db_FETCH(db, key, flags) ((db->dbp)->get)(db->dbp, &key, &value, flags)
-
-#define db_close(db) ((db->dbp)->close)(db->dbp)
-#define db_del(db, key, flags) ((db->dbp)->del)(db->dbp, &key, flags)
-#define db_fd(db) (db->in_memory \
- ? -1 \
- : ((db->dbp)->fd)(db->dbp) )
-#define db_put(db, key, value, flags) ((db->dbp)->put)(db->dbp, &key, &value, flags)
-#define db_get(db, key, value, flags) ((db->dbp)->get)(db->dbp, &key, &value, flags)
-#define db_seq(db, key, value, flags) ((db->dbp)->seq)(db->dbp, &key, &value, flags)
-#define db_sync(db, flags) ((db->dbp)->sync)(db->dbp, flags)
-
-
#define OutputValue(arg, name) \
{ if (RETVAL == 0) { \
sv_setpvn(arg, name.data, name.size) ; \
@@ -123,7 +246,7 @@ typedef DBT DBTKEY ;
}
#define OutputKey(arg, name) \
- { if (RETVAL == 0) \
+ { if (RETVAL == 0) \
{ \
if (db->type != DB_RECNO) { \
sv_setpvn(arg, name.data, name.size); \
@@ -135,9 +258,65 @@ typedef DBT DBTKEY ;
/* Internal Global Data */
static recno_t Value ;
-static DB_File CurrentDB ;
static recno_t zero = 0 ;
-static DBTKEY empty = { &zero, sizeof(recno_t) } ;
+static DB_File CurrentDB ;
+static DBTKEY empty ;
+
+#ifdef DB_VERSION_MAJOR
+
+static int
+db_put(db, key, value, flags)
+DB_File db ;
+DBTKEY key ;
+DBT value ;
+u_int flags ;
+
+{
+ int status ;
+
+ if (flags & R_CURSOR) {
+ status = ((db->cursor)->c_del)(db->cursor, 0);
+ if (status != 0)
+ return status ;
+
+ flags &= ~R_CURSOR ;
+ }
+
+ return ((db->dbp)->put)(db->dbp, NULL, &key, &value, flags) ;
+
+}
+
+#endif /* DB_VERSION_MAJOR */
+
+static void
+GetVersionInfo()
+{
+ SV * ver_sv = perl_get_sv("DB_File::db_version", TRUE) ;
+#ifdef DB_VERSION_MAJOR
+ int Major, Minor, Patch ;
+
+ (void)db_version(&Major, &Minor, &Patch) ;
+
+ /* check that libdb is recent enough */
+ if (Major == 2 && Minor == 0 && Patch < 5)
+ croak("DB_File needs Berkeley DB 2.0.5 or greater, you have %d.%d.%d\n",
+ Major, Minor, Patch) ;
+
+#if PATCHLEVEL > 3
+ sv_setpvf(ver_sv, "%d.%d", Major, Minor) ;
+#else
+ {
+ char buffer[40] ;
+ sprintf(buffer, "%d.%d", Major, Minor) ;
+ sv_setpv(ver_sv, buffer) ;
+ }
+#endif
+
+#else
+ sv_setiv(ver_sv, 1) ;
+#endif
+
+}
static int
@@ -276,45 +455,50 @@ size_t size ;
static void
PrintHash(hash)
-HASHINFO * hash ;
+INFO * hash ;
{
printf ("HASH Info\n") ;
- printf (" hash = %s\n", (hash->hash != NULL ? "redefined" : "default")) ;
- printf (" bsize = %d\n", hash->bsize) ;
- printf (" ffactor = %d\n", hash->ffactor) ;
- printf (" nelem = %d\n", hash->nelem) ;
- printf (" cachesize = %d\n", hash->cachesize) ;
- printf (" lorder = %d\n", hash->lorder) ;
+ printf (" hash = %s\n",
+ (hash->db_HA_hash != NULL ? "redefined" : "default")) ;
+ printf (" bsize = %d\n", hash->db_HA_bsize) ;
+ printf (" ffactor = %d\n", hash->db_HA_ffactor) ;
+ printf (" nelem = %d\n", hash->db_HA_nelem) ;
+ printf (" cachesize = %d\n", hash->db_HA_cachesize) ;
+ printf (" lorder = %d\n", hash->db_HA_lorder) ;
}
static void
PrintRecno(recno)
-RECNOINFO * recno ;
+INFO * recno ;
{
printf ("RECNO Info\n") ;
- printf (" flags = %d\n", recno->flags) ;
- printf (" cachesize = %d\n", recno->cachesize) ;
- printf (" psize = %d\n", recno->psize) ;
- printf (" lorder = %d\n", recno->lorder) ;
- printf (" reclen = %lu\n", (unsigned long)recno->reclen) ;
- printf (" bval = %d 0x%x\n", recno->bval, recno->bval) ;
- printf (" bfname = %d [%s]\n", recno->bfname, recno->bfname) ;
+ printf (" flags = %d\n", recno->db_RE_flags) ;
+ printf (" cachesize = %d\n", recno->db_RE_cachesize) ;
+ printf (" psize = %d\n", recno->db_RE_psize) ;
+ printf (" lorder = %d\n", recno->db_RE_lorder) ;
+ printf (" reclen = %ul\n", (unsigned long)recno->db_RE_reclen) ;
+ printf (" bval = %d 0x%x\n", recno->db_RE_bval, recno->db_RE_bval) ;
+ printf (" bfname = %d [%s]\n", recno->db_RE_bfname, recno->db_RE_bfname) ;
}
static void
PrintBtree(btree)
-BTREEINFO * btree ;
+INFO * btree ;
{
printf ("BTREE Info\n") ;
- printf (" compare = %s\n", (btree->compare ? "redefined" : "default")) ;
- printf (" prefix = %s\n", (btree->prefix ? "redefined" : "default")) ;
- printf (" flags = %d\n", btree->flags) ;
- printf (" cachesize = %d\n", btree->cachesize) ;
- printf (" psize = %d\n", btree->psize) ;
- printf (" maxkeypage = %d\n", btree->maxkeypage) ;
- printf (" minkeypage = %d\n", btree->minkeypage) ;
- printf (" lorder = %d\n", btree->lorder) ;
+ printf (" compare = %s\n",
+ (btree->db_BT_compare ? "redefined" : "default")) ;
+ printf (" prefix = %s\n",
+ (btree->db_BT_prefix ? "redefined" : "default")) ;
+ printf (" flags = %d\n", btree->db_BT_flags) ;
+ printf (" cachesize = %d\n", btree->db_BT_cachesize) ;
+ printf (" psize = %d\n", btree->db_BT_psize) ;
+#ifndef DB_VERSION_MAJOR
+ printf (" maxkeypage = %d\n", btree->db_BT_maxkeypage) ;
+ printf (" minkeypage = %d\n", btree->db_BT_minkeypage) ;
+#endif
+ printf (" lorder = %d\n", btree->db_BT_lorder) ;
}
#else
@@ -328,16 +512,18 @@ BTREEINFO * btree ;
static I32
GetArrayLength(db)
-DB * db ;
+DB_File db ;
{
DBT key ;
DBT value ;
int RETVAL ;
- RETVAL = (db->seq)(db, &key, &value, R_LAST) ;
+ DBT_flags(key) ;
+ DBT_flags(value) ;
+ RETVAL = do_SEQ(db, key, value, R_LAST) ;
if (RETVAL == 0)
RETVAL = *(I32 *)key.data ;
- else if (RETVAL == 1) /* No key means empty file */
+ else /* No key means empty file */
RETVAL = 0 ;
return ((I32)RETVAL) ;
@@ -350,7 +536,7 @@ I32 value ;
{
if (value < 0) {
/* Get the length of the array */
- I32 length = GetArrayLength(db->dbp) ;
+ I32 length = GetArrayLength(db) ;
/* check for attempt to write before start of array */
if (length + value + 1 <= 0)
@@ -376,7 +562,10 @@ SV * sv ;
HV * action ;
DB_File RETVAL = (DB_File)safemalloc(sizeof(DB_File_type)) ;
void * openinfo = NULL ;
- union INFO * info = &RETVAL->info ;
+ INFO * info = &RETVAL->info ;
+
+/* printf("In ParseOpenInfo name=[%s] flags=[%d] mode = [%d]\n", name, flags, mode) ; */
+ Zero(RETVAL, 1, DB_File_type) ;
/* Default to HASH */
RETVAL->hash = RETVAL->compare = RETVAL->prefix = NULL ;
@@ -412,26 +601,26 @@ SV * sv ;
if (svp && SvOK(*svp))
{
- info->hash.hash = hash_cb ;
+ info->db_HA_hash = hash_cb ;
RETVAL->hash = newSVsv(*svp) ;
}
else
- info->hash.hash = NULL ;
+ info->db_HA_hash = NULL ;
- svp = hv_fetch(action, "bsize", 5, FALSE);
- info->hash.bsize = svp ? SvIV(*svp) : 0;
-
svp = hv_fetch(action, "ffactor", 7, FALSE);
- info->hash.ffactor = svp ? SvIV(*svp) : 0;
+ info->db_HA_ffactor = svp ? SvIV(*svp) : 0;
svp = hv_fetch(action, "nelem", 5, FALSE);
- info->hash.nelem = svp ? SvIV(*svp) : 0;
+ info->db_HA_nelem = svp ? SvIV(*svp) : 0;
+ svp = hv_fetch(action, "bsize", 5, FALSE);
+ info->db_HA_bsize = svp ? SvIV(*svp) : 0;
+
svp = hv_fetch(action, "cachesize", 9, FALSE);
- info->hash.cachesize = svp ? SvIV(*svp) : 0;
+ info->db_HA_cachesize = svp ? SvIV(*svp) : 0;
svp = hv_fetch(action, "lorder", 6, FALSE);
- info->hash.lorder = svp ? SvIV(*svp) : 0;
+ info->db_HA_lorder = svp ? SvIV(*svp) : 0;
PrintHash(info) ;
}
@@ -446,38 +635,40 @@ SV * sv ;
svp = hv_fetch(action, "compare", 7, FALSE);
if (svp && SvOK(*svp))
{
- info->btree.compare = btree_compare ;
+ info->db_BT_compare = btree_compare ;
RETVAL->compare = newSVsv(*svp) ;
}
else
- info->btree.compare = NULL ;
+ info->db_BT_compare = NULL ;
svp = hv_fetch(action, "prefix", 6, FALSE);
if (svp && SvOK(*svp))
{
- info->btree.prefix = btree_prefix ;
+ info->db_BT_prefix = btree_prefix ;
RETVAL->prefix = newSVsv(*svp) ;
}
else
- info->btree.prefix = NULL ;
+ info->db_BT_prefix = NULL ;
svp = hv_fetch(action, "flags", 5, FALSE);
- info->btree.flags = svp ? SvIV(*svp) : 0;
+ info->db_BT_flags = svp ? SvIV(*svp) : 0;
svp = hv_fetch(action, "cachesize", 9, FALSE);
- info->btree.cachesize = svp ? SvIV(*svp) : 0;
+ info->db_BT_cachesize = svp ? SvIV(*svp) : 0;
+#ifndef DB_VERSION_MAJOR
svp = hv_fetch(action, "minkeypage", 10, FALSE);
info->btree.minkeypage = svp ? SvIV(*svp) : 0;
svp = hv_fetch(action, "maxkeypage", 10, FALSE);
info->btree.maxkeypage = svp ? SvIV(*svp) : 0;
+#endif
svp = hv_fetch(action, "psize", 5, FALSE);
- info->btree.psize = svp ? SvIV(*svp) : 0;
+ info->db_BT_psize = svp ? SvIV(*svp) : 0;
svp = hv_fetch(action, "lorder", 6, FALSE);
- info->btree.lorder = svp ? SvIV(*svp) : 0;
+ info->db_BT_lorder = svp ? SvIV(*svp) : 0;
PrintBtree(info) ;
@@ -490,45 +681,87 @@ SV * sv ;
RETVAL->type = DB_RECNO ;
openinfo = (void *)info ;
+ info->db_RE_flags = 0 ;
+
svp = hv_fetch(action, "flags", 5, FALSE);
- info->recno.flags = (u_long) (svp ? SvIV(*svp) : 0);
+ info->db_RE_flags = (u_long) (svp ? SvIV(*svp) : 0);
+
+ svp = hv_fetch(action, "reclen", 6, FALSE);
+ info->db_RE_reclen = (size_t) (svp ? SvIV(*svp) : 0);
svp = hv_fetch(action, "cachesize", 9, FALSE);
- info->recno.cachesize = (u_int) (svp ? SvIV(*svp) : 0);
+ info->db_RE_cachesize = (u_int) (svp ? SvIV(*svp) : 0);
svp = hv_fetch(action, "psize", 5, FALSE);
- info->recno.psize = (u_int) (svp ? SvIV(*svp) : 0);
+ info->db_RE_psize = (u_int) (svp ? SvIV(*svp) : 0);
svp = hv_fetch(action, "lorder", 6, FALSE);
- info->recno.lorder = (int) (svp ? SvIV(*svp) : 0);
-
- svp = hv_fetch(action, "reclen", 6, FALSE);
- info->recno.reclen = (size_t) (svp ? SvIV(*svp) : 0);
+ info->db_RE_lorder = (int) (svp ? SvIV(*svp) : 0);
+
+#ifdef DB_VERSION_MAJOR
+ info->re_source = name ;
+ name = NULL ;
+#endif
+ svp = hv_fetch(action, "bfname", 6, FALSE);
+ if (svp && SvOK(*svp)) {
+ char * ptr = SvPV(*svp,na) ;
+#ifdef DB_VERSION_MAJOR
+ name = (char*) na ? ptr : NULL ;
+#else
+ info->db_RE_bfname = (char*) (na ? ptr : NULL) ;
+#endif
+ }
+ else
+#ifdef DB_VERSION_MAJOR
+ name = NULL ;
+#else
+ info->db_RE_bfname = NULL ;
+#endif
svp = hv_fetch(action, "bval", 4, FALSE);
+#ifdef DB_VERSION_MAJOR
if (svp && SvOK(*svp))
{
+ int value ;
if (SvPOK(*svp))
- info->recno.bval = (u_char)*SvPV(*svp, na) ;
+ value = (int)*SvPV(*svp, na) ;
else
- info->recno.bval = (u_char)(unsigned long) SvIV(*svp) ;
+ value = SvIV(*svp) ;
+
+ if (info->flags & DB_FIXEDLEN) {
+ info->re_pad = value ;
+ info->flags |= DB_PAD ;
+ }
+ else {
+ info->re_delim = value ;
+ info->flags |= DB_DELIMITER ;
+ }
+
+ }
+#else
+ if (svp && SvOK(*svp))
+ {
+ if (SvPOK(*svp))
+ info->db_RE_bval = (u_char)*SvPV(*svp, na) ;
+ else
+ info->db_RE_bval = (u_char)(unsigned long) SvIV(*svp) ;
+ DB_flags(info->flags, DB_DELIMITER) ;
+
}
else
{
- if (info->recno.flags & R_FIXEDLEN)
- info->recno.bval = (u_char) ' ' ;
+ if (info->db_RE_flags & R_FIXEDLEN)
+ info->db_RE_bval = (u_char) ' ' ;
else
- info->recno.bval = (u_char) '\n' ;
+ info->db_RE_bval = (u_char) '\n' ;
+ DB_flags(info->flags, DB_DELIMITER) ;
}
-
- svp = hv_fetch(action, "bfname", 6, FALSE);
- if (svp && SvOK(*svp)) {
- char * ptr = SvPV(*svp,na) ;
- info->recno.bfname = (char*) (na ? ptr : NULL) ;
- }
- else
- info->recno.bfname = NULL ;
+#endif
+#ifdef DB_RENUMBER
+ info->flags |= DB_RENUMBER ;
+#endif
+
PrintRecno(info) ;
}
else
@@ -543,7 +776,44 @@ SV * sv ;
#endif /* __EMX__ */
#endif /* OS2 */
+#ifdef DB_VERSION_MAJOR
+
+ {
+ int Flags = 0 ;
+ int status ;
+
+ /* Map 1.x flags to 2.x flags */
+ if ((flags & O_CREAT) == O_CREAT)
+ Flags |= DB_CREATE ;
+
+#ifdef O_NONBLOCK
+ if ((flags & O_NONBLOCK) == O_NONBLOCK)
+ Flags |= DB_EXCL ;
+#endif
+
+#if O_RDONLY == 0
+ if (flags == O_RDONLY)
+#else
+ if (flags & O_RDONLY) == O_RDONLY)
+#endif
+ Flags |= DB_RDONLY ;
+
+#ifdef O_NONBLOCK
+ if ((flags & O_TRUNC) == O_TRUNC)
+ Flags |= DB_TRUNCATE ;
+#endif
+
+ status = db_open(name, RETVAL->type, Flags, mode, NULL, openinfo, &RETVAL->dbp) ;
+ if (status == 0)
+ status = (RETVAL->dbp->cursor)(RETVAL->dbp, NULL, &RETVAL->cursor) ;
+
+ if (status)
+ RETVAL->dbp = NULL ;
+
+ }
+#else
RETVAL->dbp = dbopen(name, flags, mode, RETVAL->type, openinfo) ;
+#endif
return (RETVAL) ;
}
@@ -779,12 +1049,6 @@ int arg;
case 'Z':
break;
case '_':
- if (strEQ(name, "__R_UNUSED"))
-#ifdef __R_UNUSED
- return __R_UNUSED;
-#else
- goto not_there;
-#endif
break;
}
errno = EINVAL;
@@ -797,6 +1061,15 @@ not_there:
MODULE = DB_File PACKAGE = DB_File PREFIX = db_
+BOOT:
+ {
+ GetVersionInfo() ;
+
+ empty.data = &zero ;
+ empty.size = sizeof(recno_t) ;
+ DBT_flags(empty) ;
+ }
+
double
constant(name,arg)
char * name
@@ -840,6 +1113,10 @@ db_DESTROY(db)
if (db->prefix)
SvREFCNT_dec(db->prefix) ;
Safefree(db) ;
+#ifdef DB_VERSION_MAJOR
+ if (RETVAL > 0)
+ RETVAL = -1 ;
+#endif
int
@@ -859,8 +1136,9 @@ db_EXISTS(db, key)
{
DBT value ;
+ DBT_flags(value) ;
CurrentDB = db ;
- RETVAL = (((db->dbp)->get)(db->dbp, &key, &value, 0) == 0) ;
+ RETVAL = (((db->dbp)->get)(db->dbp, TXN &key, &value, 0) == 0) ;
}
OUTPUT:
RETVAL
@@ -872,12 +1150,14 @@ db_FETCH(db, key, flags=0)
u_int flags
CODE:
{
- DBT value ;
+ DBT value ;
+ DBT_flags(value) ;
CurrentDB = db ;
- RETVAL = ((db->dbp)->get)(db->dbp, &key, &value, flags) ;
+ /* RETVAL = ((db->dbp)->get)(db->dbp, TXN &key, &value, flags) ; */
+ RETVAL = db_get(db, key, value, flags) ;
ST(0) = sv_newmortal();
- if (RETVAL == 0)
+ if (RETVAL == 0)
sv_setpvn(ST(0), value.data, value.size);
}
@@ -896,12 +1176,14 @@ db_FIRSTKEY(db)
DB_File db
CODE:
{
- DBTKEY key ;
+ DBTKEY key ;
DBT value ;
DB * Db = db->dbp ;
+ DBT_flags(key) ;
+ DBT_flags(value) ;
CurrentDB = db ;
- RETVAL = (Db->seq)(Db, &key, &value, R_FIRST) ;
+ RETVAL = do_SEQ(db, key, value, R_FIRST) ;
ST(0) = sv_newmortal();
if (RETVAL == 0)
{
@@ -921,8 +1203,9 @@ db_NEXTKEY(db, key)
DBT value ;
DB * Db = db->dbp ;
+ DBT_flags(value) ;
CurrentDB = db ;
- RETVAL = (Db->seq)(Db, &key, &value, R_NEXT) ;
+ RETVAL = do_SEQ(db, key, value, R_NEXT) ;
ST(0) = sv_newmortal();
if (RETVAL == 0)
{
@@ -948,8 +1231,16 @@ unshift(db, ...)
int One ;
DB * Db = db->dbp ;
+ DBT_flags(key) ;
+ DBT_flags(value) ;
CurrentDB = db ;
+#ifdef DB_VERSION_MAJOR
+ /* get the first value */
+ RETVAL = do_SEQ(db, key, value, DB_FIRST) ;
+ RETVAL = 0 ;
+#else
RETVAL = -1 ;
+#endif
for (i = items-1 ; i > 0 ; --i)
{
value.data = SvPV(ST(i), na) ;
@@ -957,7 +1248,11 @@ unshift(db, ...)
One = 1 ;
key.data = &One ;
key.size = sizeof(int) ;
+#ifdef DB_VERSION_MAJOR
+ RETVAL = (db->cursor->c_put)(db->cursor, &key, &value, DB_BEFORE) ;
+#else
RETVAL = (Db->put)(Db, &key, &value, R_IBEFORE) ;
+#endif
if (RETVAL != 0)
break;
}
@@ -974,16 +1269,19 @@ pop(db)
DBT value ;
DB * Db = db->dbp ;
+ DBT_flags(key) ;
+ DBT_flags(value) ;
CurrentDB = db ;
+
/* First get the final value */
- RETVAL = (Db->seq)(Db, &key, &value, R_LAST) ;
+ RETVAL = do_SEQ(db, key, value, R_LAST) ;
ST(0) = sv_newmortal();
/* Now delete it */
if (RETVAL == 0)
{
/* the call to del will trash value, so take a copy now */
sv_setpvn(ST(0), value.data, value.size);
- RETVAL = (Db->del)(Db, &key, R_CURSOR) ;
+ RETVAL = db_del(db, key, R_CURSOR) ;
if (RETVAL != 0)
sv_setsv(ST(0), &sv_undef);
}
@@ -998,16 +1296,18 @@ shift(db)
DBTKEY key ;
DB * Db = db->dbp ;
+ DBT_flags(key) ;
+ DBT_flags(value) ;
CurrentDB = db ;
/* get the first value */
- RETVAL = (Db->seq)(Db, &key, &value, R_FIRST) ;
+ RETVAL = do_SEQ(db, key, value, R_FIRST) ;
ST(0) = sv_newmortal();
/* Now delete it */
if (RETVAL == 0)
{
/* the call to del will trash value, so take a copy now */
sv_setpvn(ST(0), value.data, value.size);
- RETVAL = (Db->del)(Db, &key, R_CURSOR) ;
+ RETVAL = db_del(db, key, R_CURSOR) ;
if (RETVAL != 0)
sv_setsv (ST(0), &sv_undef) ;
}
@@ -1025,13 +1325,27 @@ push(db, ...)
DB * Db = db->dbp ;
int i ;
+ DBT_flags(key) ;
+ DBT_flags(value) ;
CurrentDB = db ;
/* Set the Cursor to the Last element */
- RETVAL = (Db->seq)(Db, &key, &value, R_LAST) ;
+ RETVAL = do_SEQ(db, key, value, R_LAST) ;
if (RETVAL >= 0)
{
if (RETVAL == 1)
keyptr = &empty ;
+#ifdef DB_VERSION_MAJOR
+ for (i = 1 ; i < items ; ++i)
+ {
+
+ ++ (* (int*)key.data) ;
+ value.data = SvPV(ST(i), na) ;
+ value.size = na ;
+ RETVAL = (Db->put)(Db, NULL, &key, &value, 0) ;
+ if (RETVAL != 0)
+ break;
+ }
+#else
for (i = items - 1 ; i > 0 ; --i)
{
value.data = SvPV(ST(i), na) ;
@@ -1040,6 +1354,7 @@ push(db, ...)
if (RETVAL != 0)
break;
}
+#endif
}
}
OUTPUT:
@@ -1051,7 +1366,7 @@ length(db)
DB_File db
CODE:
CurrentDB = db ;
- RETVAL = GetArrayLength(db->dbp) ;
+ RETVAL = GetArrayLength(db) ;
OUTPUT:
RETVAL
@@ -1065,8 +1380,17 @@ db_del(db, key, flags=0)
DB_File db
DBTKEY key
u_int flags
- INIT:
+ CODE:
CurrentDB = db ;
+ RETVAL = db_del(db, key, flags) ;
+#ifdef DB_VERSION_MAJOR
+ if (RETVAL > 0)
+ RETVAL = -1 ;
+ else if (RETVAL == DB_NOTFOUND)
+ RETVAL = 1 ;
+#endif
+ OUTPUT:
+ RETVAL
int
@@ -1075,9 +1399,18 @@ db_get(db, key, value, flags=0)
DBTKEY key
DBT value = NO_INIT
u_int flags
- INIT:
+ CODE:
CurrentDB = db ;
+ DBT_flags(value) ;
+ RETVAL = db_get(db, key, value, flags) ;
+#ifdef DB_VERSION_MAJOR
+ if (RETVAL > 0)
+ RETVAL = -1 ;
+ else if (RETVAL == DB_NOTFOUND)
+ RETVAL = 1 ;
+#endif
OUTPUT:
+ RETVAL
value
int
@@ -1086,23 +1419,53 @@ db_put(db, key, value, flags=0)
DBTKEY key
DBT value
u_int flags
- INIT:
+ CODE:
CurrentDB = db ;
+ RETVAL = db_put(db, key, value, flags) ;
+#ifdef DB_VERSION_MAJOR
+ if (RETVAL > 0)
+ RETVAL = -1 ;
+ else if (RETVAL == DB_KEYEXIST)
+ RETVAL = 1 ;
+#endif
OUTPUT:
+ RETVAL
key if (flags & (R_IAFTER|R_IBEFORE)) OutputKey(ST(1), key);
int
db_fd(db)
DB_File db
- INIT:
+ int status = 0 ;
+ CODE:
CurrentDB = db ;
+#ifdef DB_VERSION_MAJOR
+ RETVAL = -1 ;
+ status = (db->in_memory
+ ? -1
+ : ((db->dbp)->fd)(db->dbp, &RETVAL) ) ;
+ if (status != 0)
+ RETVAL = -1 ;
+#else
+ RETVAL = (db->in_memory
+ ? -1
+ : ((db->dbp)->fd)(db->dbp) ) ;
+#endif
+ OUTPUT:
+ RETVAL
int
db_sync(db, flags=0)
DB_File db
u_int flags
- INIT:
+ CODE:
CurrentDB = db ;
+ RETVAL = db_sync(db, flags) ;
+#ifdef DB_VERSION_MAJOR
+ if (RETVAL > 0)
+ RETVAL = -1 ;
+#endif
+ OUTPUT:
+ RETVAL
int
@@ -1111,9 +1474,18 @@ db_seq(db, key, value, flags)
DBTKEY key
DBT value = NO_INIT
u_int flags
- INIT:
+ CODE:
CurrentDB = db ;
+ DBT_flags(value) ;
+ RETVAL = db_seq(db, key, value, flags);
+#ifdef DB_VERSION_MAJOR
+ if (RETVAL > 0)
+ RETVAL = -1 ;
+ else if (RETVAL == DB_NOTFOUND)
+ RETVAL = 1 ;
+#endif
OUTPUT:
+ RETVAL
key
value