home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
BURKS 2
/
BURKS_AUG97.ISO
/
SLAKWARE
/
D12
/
PERL1.TGZ
/
perl1.tar
/
usr
/
lib
/
perl5
/
DB_File.pm
< prev
next >
Wrap
Text File
|
1996-06-28
|
16KB
|
674 lines
# DB_File.pm -- Perl 5 interface to Berkeley DB
#
# written by Paul Marquess (pmarquess@bfsec.bt.co.uk)
# last modified 14th November 1995
# version 1.01
package DB_File::HASHINFO ;
use strict;
use vars qw(%elements);
use Carp;
sub TIEHASH
{
bless {} ;
}
%elements = ( 'bsize' => 0,
'ffactor' => 0,
'nelem' => 0,
'cachesize' => 0,
'hash' => 0,
'lorder' => 0
) ;
sub FETCH
{
return $_[0]{$_[1]} if defined $elements{$_[1]} ;
croak "DB_File::HASHINFO::FETCH - Unknown element '$_[1]'" ;
}
sub STORE
{
if ( defined $elements{$_[1]} )
{
$_[0]{$_[1]} = $_[2] ;
return ;
}
croak "DB_File::HASHINFO::STORE - Unknown element '$_[1]'" ;
}
sub DELETE
{
if ( defined $elements{$_[1]} )
{
delete ${$_[0]}{$_[1]} ;
return ;
}
croak "DB_File::HASHINFO::DELETE - Unknown element '$_[1]'" ;
}
sub DESTROY {undef %{$_[0]} }
sub FIRSTKEY { croak "DB_File::HASHINFO::FIRSTKEY is not implemented" }
sub NEXTKEY { croak "DB_File::HASHINFO::NEXTKEY is not implemented" }
sub EXISTS { croak "DB_File::HASHINFO::EXISTS is not implemented" }
sub CLEAR { croak "DB_File::HASHINFO::CLEAR is not implemented" }
package DB_File::BTREEINFO ;
use strict;
use vars qw(%elements);
use Carp;
sub TIEHASH
{
bless {} ;
}
%elements = ( 'flags' => 0,
'cachesize' => 0,
'maxkeypage' => 0,
'minkeypage' => 0,
'psize' => 0,
'compare' => 0,
'prefix' => 0,
'lorder' => 0
) ;
sub FETCH
{
return $_[0]{$_[1]} if defined $elements{$_[1]} ;
croak "DB_File::BTREEINFO::FETCH - Unknown element '$_[1]'" ;
}
sub STORE
{
if ( defined $elements{$_[1]} )
{
$_[0]{$_[1]} = $_[2] ;
return ;
}
croak "DB_File::BTREEINFO::STORE - Unknown element '$_[1]'" ;
}
sub DELETE
{
if ( defined $elements{$_[1]} )
{
delete ${$_[0]}{$_[1]} ;
return ;
}
croak "DB_File::BTREEINFO::DELETE - Unknown element '$_[1]'" ;
}
sub DESTROY {undef %{$_[0]} }
sub FIRSTKEY { croak "DB_File::BTREEINFO::FIRSTKEY is not implemented" }
sub NEXTKEY { croak "DB_File::BTREEINFO::NEXTKEY is not implemented" }
sub EXISTS { croak "DB_File::BTREEINFO::EXISTS is not implemented" }
sub CLEAR { croak "DB_File::BTREEINFO::CLEAR is not implemented" }
package DB_File::RECNOINFO ;
use strict;
use vars qw(%elements);
use Carp;
sub TIEHASH
{
bless {} ;
}
%elements = ( 'bval' => 0,
'cachesize' => 0,
'psize' => 0,
'flags' => 0,
'lorder' => 0,
'reclen' => 0,
'bfname' => 0
) ;
sub FETCH
{
return $_[0]{$_[1]} if defined $elements{$_[1]} ;
croak "DB_File::RECNOINFO::FETCH - Unknown element '$_[1]'" ;
}
sub STORE
{
if ( defined $elements{$_[1]} )
{
$_[0]{$_[1]} = $_[2] ;
return ;
}
croak "DB_File::RECNOINFO::STORE - Unknown element '$_[1]'" ;
}
sub DELETE
{
if ( defined $elements{$_[1]} )
{
delete ${$_[0]}{$_[1]} ;
return ;
}
croak "DB_File::RECNOINFO::DELETE - Unknown element '$_[1]'" ;
}
sub DESTROY {undef %{$_[0]} }
sub FIRSTKEY { croak "DB_File::RECNOINFO::FIRSTKEY is not implemented" }
sub NEXTKEY { croak "DB_File::RECNOINFO::NEXTKEY is not implemented" }
sub EXISTS { croak "DB_File::BTREEINFO::EXISTS is not implemented" }
sub CLEAR { croak "DB_File::BTREEINFO::CLEAR is not implemented" }
package DB_File ;
use strict;
use vars qw($VERSION @ISA @EXPORT $AUTOLOAD $DB_BTREE $DB_HASH $DB_RECNO) ;
use Carp;
$VERSION = "1.01" ;
#typedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE;
$DB_BTREE = TIEHASH DB_File::BTREEINFO ;
$DB_HASH = TIEHASH DB_File::HASHINFO ;
$DB_RECNO = TIEHASH DB_File::RECNOINFO ;
require Tie::Hash;
require Exporter;
use AutoLoader;
require DynaLoader;
@ISA = qw(Tie::Hash Exporter DynaLoader);
@EXPORT = qw(
$DB_BTREE $DB_HASH $DB_RECNO
BTREEMAGIC
BTREEVERSION
DB_LOCK
DB_SHMEM
DB_TXN
HASHMAGIC
HASHVERSION
MAX_PAGE_NUMBER
MAX_PAGE_OFFSET
MAX_REC_NUMBER
RET_ERROR
RET_SPECIAL
RET_SUCCESS
R_CURSOR
R_DUP
R_FIRST
R_FIXEDLEN
R_IAFTER
R_IBEFORE
R_LAST
R_NEXT
R_NOKEY
R_NOOVERWRITE
R_PREV
R_RECNOSYNC
R_SETCURSOR
R_SNAPSHOT
__R_UNUSED
);
sub AUTOLOAD {
my($constname);
($constname = $AUTOLOAD) =~ s/.*:://;
my $val = constant($constname, @_ ? $_[0] : 0);
if ($! != 0) {
if ($! =~ /Invalid/) {
$AutoLoader::AUTOLOAD = $AUTOLOAD;
goto &AutoLoader::AUTOLOAD;
}
else {
my($pack,$file,$line) = caller;
croak "Your vendor has not defined DB macro $constname, used at $file line $line.
";
}
}
eval "sub $AUTOLOAD { $val }";
goto &$AUTOLOAD;
}
bootstrap DB_File $VERSION;
# Preloaded methods go here. Autoload methods go after __END__, and are
# processed by the autosplit program.
1;
__END__
=cut
=head1 NAME
DB_File - Perl5 access to Berkeley DB
=head1 SYNOPSIS
use DB_File ;
[$X =] tie %hash, DB_File, $filename [, $flags, $mode, $DB_HASH] ;
[$X =] tie %hash, DB_File, $filename, $flags, $mode, $DB_BTREE ;
[$X =] tie @array, DB_File, $filename, $flags, $mode, $DB_RECNO ;
$status = $X->del($key [, $flags]) ;
$status = $X->put($key, $value [, $flags]) ;
$status = $X->get($key, $value [, $flags]) ;
$status = $X->seq($key, $value [, $flags]) ;
$status = $X->sync([$flags]) ;
$status = $X->fd ;
untie %hash ;
untie @array ;
=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 manualpage at
hand. 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
three of the database types currently supported by Berkeley DB.
The file types are:
=over 5
=item DB_HASH
This database type allows arbitrary key/data pairs to be stored in data
files. This is equivalent to the functionality provided by other
hashing packages like DBM, NDBM, ODBM, GDBM, and SDBM. Remember though,
the files created using DB_HASH are not compatible with any of the
other packages mentioned.
A default hashing algorithm, which will be adequate for most
applications, is built into Berkeley DB. If you do need to use your own
hashing algorithm it is possible to write your own in Perl and have
B<DB_File> use it instead.
=item DB_BTREE
The btree format allows arbitrary key/data pairs to be stored in a
sorted, balanced binary tree.
As with the DB_HASH format, it is possible to provide a user defined
Perl routine to perform the comparison of keys. By default, though, the
keys are stored in lexical order.
=item DB_RECNO
DB_RECNO allows both fixed-length and variable-length flat text files
to be manipulated using the same key/value pair interface as in DB_HASH
and DB_BTREE. In this case the key will consist of a record (line)
number.
=back
=head2 How does DB_File interface to Berkeley DB?
B<DB_File> allows access to Berkeley DB files using the tie() mechanism
in Perl 5 (for full details, see L<perlfunc/tie()>). This facility
allows B<DB_File> to access Berkeley DB files using either an
associative array (for DB_HASH & DB_BTREE file types) or an ordinary
array (for the DB_RECNO file type).
In addition to the tie() interface, it is also possible to use most of
the functions provided in the Berkeley DB API.
=head2 Differences with Berkeley DB
Berkeley DB uses the function dbopen() to open or create a database.
Below is the C prototype for dbopen().
DB*
dbopen (const char * file, int flags, int mode,
DBTYPE type, const void * openinfo)
The parameter C<type> is an enumeration which specifies which of the 3
interface methods (DB_HASH, DB_BTREE or DB_RECNO) is to be used.
Depending on which of these is actually chosen, the final p