--- trunk/lib/WAIT/Index.pm 2004/07/13 12:45:55 107 +++ trunk/lib/WAIT/Index.pm 2004/07/13 17:41:12 108 @@ -15,26 +15,34 @@ package WAIT::Index; use WAIT::IndexScan; use strict; -use DB_File; +use BerkeleyDB; use Fcntl; use vars qw($VERSION); -$VERSION = "1.801"; # Table.pm tests if we are loaded by checking $VERSION +$VERSION = "2.000"; # Table.pm tests if we are loaded by checking $VERSION sub new { my $type = shift; my %parm = @_; my $self = {}; - unless ($self->{file} = $parm{file}) { - require Carp; - Carp::croak("No file specified"); + for my $x (qw(file attr env subname maindbfile tablename)) { + unless ($self->{$x} = $parm{$x}) { + require Carp; + Carp::croak("No $x specified"); + } } - unless ($self->{attr} = $parm{attr}) { + bless $self, ref($type) || $type; +} + +for my $accessor (qw(maindbfile tablename subname)) { + no strict 'refs'; + *{$accessor} = sub { + my($self) = @_; + return $self->{$accessor} if $self->{$accessor}; require Carp; - Carp::croak("No attributes specified"); + Carp::confess("accessor $accessor not there"); } - bless $self, ref($type) || $type; } sub drop { @@ -55,8 +63,24 @@ if (exists $self->{dbh}) { $self->{dbh}; } else { - $self->{dbh} = tie(%{$self->{db}}, 'DB_File', $file, - $self->{mode}, 0664, $DB_BTREE); + my $flags; + if ($self->{mode} & O_RDWR) { + $flags = DB_CREATE; # | DB_INIT_MPOOL | DB_INIT_CDB; + # warn "Flags on index $file set to 'writing'"; + } else { + $flags = DB_RDONLY; + # warn "Flags on index $file set to 'readonly'"; + } + $self->{dbh} = tie(%{$self->{db}}, 'BerkeleyDB::Btree', + # Filename => $file, + Filename => $self->maindbfile, + $self->{env} ? (Env => $self->{env}) : (), + Subname => join("/",$self->tablename,$self->subname), + Mode => 0664, + Flags => $flags, + $WAIT::Database::Cachesize?(Cachesize => $WAIT::Database::Cachesize):(), + $WAIT::Database::Pagesize?(Pagesize => $WAIT::Database::Pagesize):(), + ) or die $BerkeleyDB::Error; } } @@ -120,10 +144,13 @@ delete $self->{scans} if defined $self->{scans}; + delete $self->{env}; if ($self->{dbh}) { delete $self->{dbh}; untie %{$self->{db}}; - delete $self->{db}; + for my $att (qw(db file maindbfile)) { + delete $self->{$att}; + } } }