# DB_File.pm -- 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
#
# Copyright (c) 1995, 1996, 1997 Paul Marquess. All rights reserved.
# This program is free software; you can redistribute it and/or
package DB_File ;
use strict;
-use vars qw($VERSION @ISA @EXPORT $AUTOLOAD $DB_BTREE $DB_HASH $DB_RECNO) ;
+use vars qw($VERSION @ISA @EXPORT $AUTOLOAD $DB_BTREE $DB_HASH $DB_RECNO $db_version) ;
use Carp;
-$VERSION = "1.15" ;
+$VERSION = "1.56" ;
#typedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE;
$DB_BTREE = new DB_File::BTREEINFO ;
eval {
- # Make all Fcntl O_XXX constants available for importing
- require Fcntl;
- my @O = grep /^O_/, @Fcntl::EXPORT;
- Fcntl->import(@O); # first we import what we want to export
- push(@EXPORT, @O);
+ # Make all Fcntl O_XXX constants available for importing
+ require Fcntl;
+ my @O = grep /^O_/, @Fcntl::EXPORT;
+ Fcntl->import(@O); # first we import what we want to export
+ push(@EXPORT, @O);
};
+## import borrowed from IO::File
+## exports Fcntl constants if available.
+#sub import {
+# my $pkg = shift;
+# my $callpkg = caller;
+# Exporter::export $pkg, $callpkg, @_;
+# eval {
+# require Fcntl;
+# Exporter::export 'Fcntl', $callpkg, '/^O_/';
+# };
+#}
+
bootstrap DB_File $VERSION;
# Preloaded methods go here. Autoload methods go after __END__, and are
$arg[4] = tied %{ $arg[4] }
if @arg >= 5 && ref $arg[4] && $arg[4] =~ /=HASH/ && tied %{ $arg[4] } ;
+ # make recno in Berkeley DB version 2 work like recno in version 1.
+ if ($db_version > 1 and defined $arg[4] and $arg[4] =~ /RECNO/ and
+ $arg[1] and ! -e $arg[1]) {
+ open(FH, ">$arg[1]") or return undef ;
+ close FH ;
+ chmod $arg[3] ? $arg[3] : 0666 , $arg[1] ;
+ }
+
DoTie_($tieHASH, @arg) ;
}
tie_hash_or_array(@_) ;
}
+sub CLEAR {
+ my $self = shift;
+ my $key = "" ;
+ my $value = "" ;
+ my $status = $self->seq($key, $value, R_FIRST());
+ my @keys;
+
+ while ($status == 0) {
+ push @keys, $key;
+ $status = $self->seq($key, $value, R_NEXT());
+ }
+ foreach $key (reverse @keys) {
+ my $s = $self->del($key);
+ }
+}
+
sub get_dup
{
croak "Usage: \$db->get_dup(key [,flag])\n"
=head1 NAME
-DB_File - Perl5 access to Berkeley DB
+DB_File - Perl5 access to Berkeley DB version 1.x
=head1 SYNOPSIS
=head1 DESCRIPTION
B<DB_File> is a module which allows Perl programs to make use of the
-facilities provided by Berkeley DB. If you intend to use this
-module you should really have a copy of the Berkeley DB manual pages at
-hand. The interface defined here mirrors the Berkeley DB interface
-closely.
-
-Please note that this module will only work with version 1.x of
-Berkeley DB. Once Berkeley DB version 2 is released, B<DB_File> will be
-upgraded to work with it.
+facilities provided by Berkeley DB version 1.x (if you have a newer
+version of DB, see L<Using DB_File with Berkeley DB version 2>). It is
+assumed that you have a copy of the Berkeley DB manual pages at hand
+when reading this documentation. The interface defined here mirrors the
+Berkeley DB interface closely.
Berkeley DB is a C library which provides a consistent interface to a
number of database formats. B<DB_File> provides an interface to all
=back
+=head2 Using DB_File with Berkeley DB version 2
+
+Although B<DB_File> is intended to be used with Berkeley DB version 1,
+it can also be used with version 2. In this case the interface is
+limited to the functionality provided by Berkeley DB 1.x. Anywhere the
+version 2 interface differs, B<DB_File> arranges for it to work like
+version 1. This feature allows B<DB_File> scripts that were built with
+version 1 to be migrated to version 2 without any changes.
+
+If you want to make use of the new features available in Berkeley DB
+2.x, use the Perl module B<BerkeleyDB> instead.
+
+At the time of writing this document the B<BerkeleyDB> module is still
+alpha quality (the version number is < 1.0), and so unsuitable for use
+in any serious development work. Once its version number is >= 1.0, it
+is considered stable enough for real work.
+
+B<Note:> The database file format has changed in Berkeley DB version 2.
+If you cannot recreate your databases, you must dump any existing
+databases with the C<db_dump185> utility that comes with Berkeley DB.
+Once you have upgraded DB_File to use Berkeley DB version 2, your
+databases can be recreated using C<db_load>. Refer to the Berkeley DB
+documentation for further details.
+
+Please read L<COPYRIGHT> before using version 2.x of Berkeley DB with
+DB_File.
+
=head2 Interface to Berkeley DB
B<DB_File> allows access to Berkeley DB files using the tie() mechanism
=head1 HISTORY
-=over
-
-=item 0.1
-
-First Release.
-
-=item 0.2
-
-When B<DB_File> is opening a database file it no longer terminates the
-process if I<dbopen> returned an error. This allows file protection
-errors to be caught at run time. Thanks to Judith Grass
-E<lt>grass@cybercash.comE<gt> for spotting the bug.
-
-=item 0.3
-
-Added prototype support for multiple btree compare callbacks.
-
-=item 1.0
-
-B<DB_File> has been in use for over a year. To reflect that, the
-version number has been incremented to 1.0.
-
-Added complete support for multiple concurrent callbacks.
-
-Using the I<push> method on an empty list didn't work properly. This
-has been fixed.
-
-=item 1.01
-
-Fixed a core dump problem with SunOS.
-
-The return value from TIEHASH wasn't set to NULL when dbopen returned
-an error.
-
-=item 1.02
-
-Merged OS/2 specific code into DB_File.xs
-
-Removed some redundant code in DB_File.xs.
-
-Documentation update.
-
-Allow negative subscripts with RECNO interface.
-
-Changed the default flags from O_RDWR to O_CREAT|O_RDWR.
-
-The example code which showed how to lock a database needed a call to
-C<sync> added. Without it the resultant database file was empty.
-
-Added get_dup method.
-
-=item 1.03
-
-Documentation update.
-
-B<DB_File> now imports the constants (O_RDWR, O_CREAT etc.) from Fcntl
-automatically.
-
-The standard hash function C<exists> is now supported.
-
-Modified the behavior of get_dup. When it returns an associative
-array, the value is the count of the number of matching BTREE values.
-
-=item 1.04
+Moved to the Changes file.
-Minor documentation changes.
-
-Fixed a bug in hash_cb. Patches supplied by Dave Hammen,
-E<lt>hammen@gothamcity.jsc.nasa.govE<gt>.
-
-Fixed a bug with the constructors for DB_File::HASHINFO,
-DB_File::BTREEINFO and DB_File::RECNOINFO. Also tidied up the
-constructors to make them C<-w> clean.
-
-Reworked part of the test harness to be more locale friendly.
-
-=item 1.05
-
-Made all scripts in the documentation C<strict> and C<-w> clean.
-
-Added logic to F<DB_File.xs> to allow the module to be built after Perl
-is installed.
-
-=item 1.06
-
-Minor namespace cleanup: Localized C<PrintBtree>.
-
-=item 1.07
-
-Fixed bug with RECNO, where bval wasn't defaulting to "\n".
-
-=item 1.08
-
-Documented operation of bval.
-
-=item 1.09
-
-Minor bug fix in DB_File::HASHINFO, DB_File::RECNOINFO and
-DB_File::BTREEINFO.
-
-Changed default mode to 0666.
-
-=item 1.10
-
-Fixed fd method so that it still returns -1 for in-memory files when db
-1.86 is used.
-
-=item 1.11
-
-Documented the untie gotcha.
-
-=item 1.12
-
-Documented the incompatibility with version 2 of Berkeley DB.
-
-=item 1.13
-
-Minor changes to DB_FIle.xs and DB_File.pm
-
-=item 1.14
-
-Made it illegal to tie an associative array to a RECNO database and an
-ordinary array to a HASH or BTREE database.
+=head1 BUGS
-=item 1.15
+Some older versions of Berkeley DB had problems with fixed length
+records using the RECNO file format. This problem has been fixed since
+version 1.85 of Berkeley DB.
-Patch from Gisle Aas <gisle@aas.no> to suppress "use of undefined
-value" warning with db_get and db_seq.
+I am sure there are bugs in the code. If you do find any, or can
+suggest any enhancements, I would welcome your comments.
-Patch from Gisle Aas <gisle@aas.no> to make DB_File export only the O_*
-constants from Fcntl.
+=head1 AVAILABILITY
-Removed the DESTROY method from the DB_File::HASHINFO module.
+B<DB_File> comes with the standard Perl source distribution. Look in
+the directory F<ext/DB_File>. Given the amount of time between releases
+of Perl the version that ships with Perl is quite likely to be out of
+date, so the most recent version can always be found on CPAN (see
+L<perlmod/CPAN> for details), in the directory
+F<modules/by-module/DB_File>.
-Previously DB_File hard-wired the class name of any object that it
-created to "DB_File". This makes sub-classing difficult. Now DB_File
-creats objects in the namespace of the package it has been inherited
-into.
+This version of B<DB_File> will work with either version 1.x or 2.x of
+Berkeley DB, but is limited to the functionality provided by version 1.
-=item 1.16
+The official web site for Berkeley DB is
+F<http://www.sleepycat.com/db>. The ftp equivalent is
+F<ftp.sleepycat.com:/pub>. Both versions 1 and 2 of Berkeley DB are
+available there.
-Minor changes to DB_File.xs to support multithreaded perl.
+Alternatively, Berkeley DB version 1 is available at your nearest CPAN
+archive in F<src/misc/db.1.85.tar.gz>.
-=back
+If you are running IRIX, then get Berkeley DB version 1 from
+F<http://reality.sgi.com/ariel>. It has the patches necessary to
+compile properly on IRIX 5.3.
-=head1 BUGS
+=head1 COPYRIGHT
-Some older versions of Berkeley DB had problems with fixed length
-records using the RECNO file format. The newest version at the time of
-writing was 1.85 - this seems to have fixed the problems with RECNO.
+Copyright (c) 1997 Paul Marquess. All rights reserved. This program is
+free software; you can redistribute it and/or modify it under the same
+terms as Perl itself.
-I am sure there are bugs in the code. If you do find any, or can
-suggest any enhancements, I would welcome your comments.
+Although B<DB_File> is covered by the Perl license, the library it
+makes use of, namely Berkeley DB, is not. Berkeley DB has its own
+copyright and its own license. Please take the time to read it.
-=head1 AVAILABILITY
+The license for Berkeley DB version 2, and how it relates to DB_File
+does need some extra clarification. Here are are few words taken from
+the Berkeley DB FAQ regarding the version 2 license:
-B<DB_File> comes with the standard Perl source distribution. Look in
-the directory F<ext/DB_File>.
+ The major difference is that the license for DB 2.0, when
+ downloaded from the net, requires that the software that
+ uses DB 2.0 be freely redistributable.
-This version of B<DB_File> will only work with version 1.x of Berkeley
-DB. It is I<not> yet compatible with version 2.
+That means that if you want to use DB_File, and you have changed either
+the source for Berkeley DB or Perl, then the changes must be freely
+available.
-Version 1 of Berkeley DB is available at your nearest CPAN archive (see
-L<perlmod/"CPAN"> for a list) in F<src/misc/db.1.85.tar.gz>, or via the
-host F<ftp.cs.berkeley.edu> in F</ucb/4bsd/db.tar.gz>. Alternatively,
-check out the Berkeley DB home page at F<http://www.bostic.com/db>. It
-is I<not> under the GPL.
+In the case of Perl, the term source refers to the complete source
+code for Perl (e.g. sv.c, toke.c, perl.h) and any external modules that
+you are using (e.g. DB_File, Tk).
-If you are running IRIX, then get Berkeley DB from
-F<http://reality.sgi.com/ariel>. It has the patches necessary to
-compile properly on IRIX 5.3.
+Note that any Perl scripts that you write are your property - this
+includes scripts that make use of DB_File. Neither the Perl license or
+the Berkeley DB license place any restriction on what you have to do
+with them.
-As of January 1997, version 1.86 of Berkeley DB is available from the
-Berkeley DB home page. Although this release does fix a number of bugs
-that were present in 1.85 you should be aware of the following
-information (taken from the Berkeley DB home page) before you consider
-using it:
-
- DB version 1.86 includes a new implementation of the hash access
- method that fixes a variety of hashing problems found in DB version
- 1.85. We are making it available as an interim solution until DB
- 2.0 is available.
-
- PLEASE NOTE: the underlying file format for the hash access method
- changed between version 1.85 and version 1.86, so you will have to
- dump and reload all of your databases to convert from version 1.85
- to version 1.86. If you do not absolutely require the fixes from
- version 1.86, we strongly urge you to wait until DB 2.0 is released
- before upgrading from 1.85.
+If you are in any doubt about the license situation, contact either the
+Berkeley DB authors or the author of DB_File. See L<"AUTHOR"> for details.
=head1 SEE ALSO
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
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
+
+
*/
/* 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
#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 ;
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) ; \
}
#define OutputKey(arg, name) \
- { if (RETVAL == 0) \
+ { if (RETVAL == 0) \
{ \
if (db->type != DB_RECNO) { \
sv_setpvn(arg, name.data, name.size); \
/* 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
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
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) ;
{
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)
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 ;
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) ;
}
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) ;
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
#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) ;
}
case 'Z':
break;
case '_':
- if (strEQ(name, "__R_UNUSED"))
-#ifdef __R_UNUSED
- return __R_UNUSED;
-#else
- goto not_there;
-#endif
break;
}
errno = EINVAL;
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
if (db->prefix)
SvREFCNT_dec(db->prefix) ;
Safefree(db) ;
+#ifdef DB_VERSION_MAJOR
+ if (RETVAL > 0)
+ RETVAL = -1 ;
+#endif
int
{
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
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);
}
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)
{
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)
{
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) ;
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;
}
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);
}
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) ;
}
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) ;
if (RETVAL != 0)
break;
}
+#endif
}
}
OUTPUT:
DB_File db
CODE:
CurrentDB = db ;
- RETVAL = GetArrayLength(db->dbp) ;
+ RETVAL = GetArrayLength(db) ;
OUTPUT:
RETVAL
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
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
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
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