4 |
# Author : Ulrich Pfeifer |
# Author : Ulrich Pfeifer |
5 |
# Created On : Thu Aug 8 13:05:10 1996 |
# Created On : Thu Aug 8 13:05:10 1996 |
6 |
# Last Modified By: Ulrich Pfeifer |
# Last Modified By: Ulrich Pfeifer |
7 |
# Last Modified On: Sat Apr 27 18:06:47 2002 |
# Last Modified On: Sun Nov 22 18:44:43 1998 |
8 |
# Language : CPerl |
# Language : CPerl |
9 |
# Update Count : 128 |
# Update Count : 107 |
10 |
# Status : Unknown, Use with caution! |
# Status : Unknown, Use with caution! |
11 |
# |
# |
12 |
# Copyright (c) 1996-1997, Ulrich Pfeifer |
# Copyright (c) 1996-1997, Ulrich Pfeifer |
19 |
use Fcntl; |
use Fcntl; |
20 |
use vars qw($VERSION); |
use vars qw($VERSION); |
21 |
|
|
22 |
$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 |
23 |
|
|
24 |
sub new { |
sub new { |
25 |
my $type = shift; |
my $type = shift; |
26 |
my %parm = @_; |
my %parm = @_; |
27 |
my $self = {}; |
my $self = {}; |
28 |
|
|
29 |
unless ($self->{file} = $parm{file}) { |
for my $x (qw(file attr env subname maindbfile tablename)) { |
30 |
require Carp; |
unless ($self->{$x} = $parm{$x}) { |
31 |
Carp::croak("No file specified"); |
require Carp; |
32 |
} |
Carp::croak("No $x specified"); |
33 |
unless ($self->{name} = $parm{name}) { |
} |
|
require Carp; |
|
|
Carp::croak("No name specified"); |
|
34 |
} |
} |
35 |
unless ($self->{attr} = $parm{attr}) { |
bless $self, ref($type) || $type; |
36 |
|
} |
37 |
|
|
38 |
|
for my $accessor (qw(maindbfile tablename subname)) { |
39 |
|
no strict 'refs'; |
40 |
|
*{$accessor} = sub { |
41 |
|
my($self) = @_; |
42 |
|
return $self->{$accessor} if $self->{$accessor}; |
43 |
require Carp; |
require Carp; |
44 |
Carp::croak("No attributes specified"); |
Carp::confess("accessor $accessor not there"); |
45 |
} |
} |
|
bless $self, ref($type) || $type; |
|
46 |
} |
} |
47 |
|
|
48 |
sub drop { |
sub drop { |
60 |
my $self = shift; |
my $self = shift; |
61 |
my $file = $self->{file}; |
my $file = $self->{file}; |
62 |
|
|
63 |
if ($self->{dbh}) { |
if (exists $self->{dbh}) { |
64 |
$self->{dbh}; |
$self->{dbh}; |
65 |
} else { |
} else { |
66 |
my $dbmode = ($self->{mode} & O_CREAT) ? DB_CREATE : 0; |
my $flags; |
67 |
|
if ($self->{mode} & O_RDWR) { |
68 |
|
$flags = DB_CREATE; # | DB_INIT_MPOOL | DB_INIT_CDB; |
69 |
|
# warn "Flags on index $file set to 'writing'"; |
70 |
|
} else { |
71 |
|
$flags = DB_RDONLY; |
72 |
|
# warn "Flags on index $file set to 'readonly'"; |
73 |
|
} |
74 |
$self->{dbh} = tie(%{$self->{db}}, 'BerkeleyDB::Btree', |
$self->{dbh} = tie(%{$self->{db}}, 'BerkeleyDB::Btree', |
75 |
-Filename => $self->{file}, |
# Filename => $file, |
76 |
-Subname => 'records', |
Filename => $self->maindbfile, |
77 |
-Flags => $dbmode, |
$self->{env} ? (Env => $self->{env}) : (), |
78 |
-Mode => 0664); |
Subname => join("/",$self->tablename,$self->subname), |
79 |
|
Mode => 0664, |
80 |
|
Flags => $flags, |
81 |
|
$WAIT::Database::Cachesize?(Cachesize => $WAIT::Database::Cachesize):(), |
82 |
|
$WAIT::Database::Pagesize?(Pagesize => $WAIT::Database::Pagesize):(), |
83 |
|
) or die $BerkeleyDB::Error; |
84 |
} |
} |
85 |
} |
} |
86 |
|
|
97 |
# duplicate entry |
# duplicate entry |
98 |
return undef; |
return undef; |
99 |
} |
} |
|
print STDERR "$tuple => $key\n"; |
|
100 |
$self->{db}->{$tuple} = $key; |
$self->{db}->{$tuple} = $key; |
101 |
} |
} |
102 |
|
|
108 |
|
|
109 |
my $tuple = join($;, map($parm{$_}, @{$self->{attr}})); |
my $tuple = join($;, map($parm{$_}, @{$self->{attr}})); |
110 |
|
|
|
print STDERR "$tuple <= ", $self->{db}->{$tuple}, "\n"; |
|
111 |
$self->{db}->{$tuple}; |
$self->{db}->{$tuple}; |
112 |
} |
} |
113 |
|
|
136 |
|
|
137 |
sub sync { |
sub sync { |
138 |
my $self = shift; |
my $self = shift; |
139 |
$self->{dbh}->db_sync if $self->{dbh}; |
$self->{dbh}->sync if $self->{dbh}; |
140 |
} |
} |
141 |
|
|
142 |
sub close { |
sub close { |
144 |
|
|
145 |
delete $self->{scans} if defined $self->{scans}; |
delete $self->{scans} if defined $self->{scans}; |
146 |
|
|
147 |
|
delete $self->{env}; |
148 |
if ($self->{dbh}) { |
if ($self->{dbh}) { |
149 |
delete $self->{dbh}; |
delete $self->{dbh}; |
150 |
delete $self->{db}; |
untie %{$self->{db}}; |
151 |
#untie %{$self->{db}}; |
for my $att (qw(db file maindbfile)) { |
152 |
|
delete $self->{$att}; |
153 |
|
} |
154 |
} |
} |
155 |
} |
} |
156 |
|
|