line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Tie::MAB2::Recno; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
21916
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
60
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
BEGIN { |
6
|
2
|
|
|
2
|
|
1616
|
use Tie::Array; |
|
2
|
|
|
|
|
2360
|
|
|
2
|
|
|
|
|
64
|
|
7
|
2
|
|
|
2
|
|
56
|
our @ISA = qw(Tie::StdArray); |
8
|
|
|
|
|
|
|
} |
9
|
|
|
|
|
|
|
|
10
|
2
|
|
|
2
|
|
1771
|
use BerkeleyDB qw( DB_RDONLY DB_CREATE DB_FAST_STAT ); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
warn sprintf "WARNING: Recommended Berkeley DB version is 4.0 or higher. Yours is %s. |
13
|
|
|
|
|
|
|
Be prepared for trouble!", $BerkeleyDB::db_version if $BerkeleyDB::db_version<4; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
use Fcntl qw( SEEK_SET ); |
16
|
|
|
|
|
|
|
use MAB2::Record::Base; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our $VERSION = sprintf "%d.%03d", q$Revision: 1.6 $ =~ /(\d+)\.(\d+)/; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub TIEARRAY { |
22
|
|
|
|
|
|
|
my($class,%args) = @_; |
23
|
|
|
|
|
|
|
my $self = {}; |
24
|
|
|
|
|
|
|
$self->{ARGS} = \%args; |
25
|
|
|
|
|
|
|
die "Could not tie: required argument file missing" unless exists $args{file}; |
26
|
|
|
|
|
|
|
my $fh; |
27
|
|
|
|
|
|
|
unless (open $fh, "<", $args{file}) { |
28
|
|
|
|
|
|
|
require Carp; |
29
|
|
|
|
|
|
|
Carp::confess("Could not open $args{file}: $!"); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
$self->{FH} = $fh; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
my $buf; |
34
|
|
|
|
|
|
|
read $fh, $buf, 3; |
35
|
|
|
|
|
|
|
seek $fh, 0, SEEK_SET; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
if ($buf eq "###") { |
38
|
|
|
|
|
|
|
$self->{RS} = ""; |
39
|
|
|
|
|
|
|
} else { |
40
|
|
|
|
|
|
|
$self->{RS} = "\n"; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# warn sprintf "Filesize: %d\n", -s $fh; |
44
|
|
|
|
|
|
|
my @offset; |
45
|
|
|
|
|
|
|
# ("BerkeleyDB::Recno", -Filename => "$args{file}.bdbrecno", -Flags => DB_RDONLY, -Mode => 0600); |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
my $db = tie(@offset, "BerkeleyDB::Recno", -Filename => "$args{file}.bdbrecno", -Flags => DB_RDONLY, -Mode => 0644); |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
#############################################^^^^^^^ did simply not work with RDONLY |
50
|
|
|
|
|
|
|
unless ($db) { |
51
|
|
|
|
|
|
|
$db = tie(@offset, "BerkeleyDB::Recno", -Filename => "$args{file}.bdbrecno", -Flags => DB_CREATE, -Mode => 0644) or die "Could not tie: $!"; |
52
|
|
|
|
|
|
|
local($/) = $self->{RS}; |
53
|
|
|
|
|
|
|
my $Loffset = 0; |
54
|
|
|
|
|
|
|
local($|) = 1; |
55
|
|
|
|
|
|
|
while (<$fh>) { |
56
|
|
|
|
|
|
|
$offset[$. - 1] = $Loffset; |
57
|
|
|
|
|
|
|
my $offset = tell $fh; |
58
|
|
|
|
|
|
|
printf "." unless int $offset/1000000 == int $Loffset/1000000; |
59
|
|
|
|
|
|
|
$Loffset = $offset; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
my $stat = $db->db_stat(DB_FAST_STAT); |
63
|
|
|
|
|
|
|
# use Data::Dumper; |
64
|
|
|
|
|
|
|
# print Data::Dumper::Dumper($stat); |
65
|
|
|
|
|
|
|
$self->{NKEYS} = $stat->{bt_nkeys}; # doesn't seem to improve much, but... |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
$self->{OFFSET} = \@offset; |
68
|
|
|
|
|
|
|
bless $self, ref $class || $class; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub UNTIE { |
72
|
|
|
|
|
|
|
my $self = shift; |
73
|
|
|
|
|
|
|
close $self->{FH}; |
74
|
|
|
|
|
|
|
untie @{$self->{OFFSET}}; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub FETCH { |
78
|
|
|
|
|
|
|
my($self, $key) = @_; |
79
|
|
|
|
|
|
|
my $fh = $self->{FH}; |
80
|
|
|
|
|
|
|
seek $fh, $self->{OFFSET}[$key], SEEK_SET; |
81
|
|
|
|
|
|
|
local($/) = $self->{RS}; |
82
|
|
|
|
|
|
|
my $rec = <$fh>; |
83
|
|
|
|
|
|
|
if ($self->{RS}){ # Band |
84
|
|
|
|
|
|
|
chomp $rec; |
85
|
|
|
|
|
|
|
} else { # convert Diskette to Band |
86
|
|
|
|
|
|
|
$rec =~ s/^### //; |
87
|
|
|
|
|
|
|
$rec =~ s/\015?\012//; # the first |
88
|
|
|
|
|
|
|
$rec =~ s/\s*\z/\c^\c]/; |
89
|
|
|
|
|
|
|
$rec =~ s/\015?\012/\c^/g ; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
my $obj = MAB2::Record::Base->new($rec,$key); |
92
|
|
|
|
|
|
|
$obj; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub FETCHSIZE { |
96
|
|
|
|
|
|
|
my($self) = @_; |
97
|
|
|
|
|
|
|
$self->{NKEYS}; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub EXISTS { |
101
|
|
|
|
|
|
|
my($self,$key) = @_; |
102
|
|
|
|
|
|
|
$key >= 0 && $key <= $self->{NKEYS}; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
for my $method (qw(STORE DELETE CLEAR)) { |
106
|
|
|
|
|
|
|
no strict "refs"; |
107
|
|
|
|
|
|
|
*$method = sub { |
108
|
|
|
|
|
|
|
warn "$method not supported on ".ref shift; |
109
|
|
|
|
|
|
|
return; |
110
|
|
|
|
|
|
|
}; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
#sub EXISTS { |
114
|
|
|
|
|
|
|
# my($self, $key) = @_; |
115
|
|
|
|
|
|
|
# exists $self->{OFFSET}[$key]; |
116
|
|
|
|
|
|
|
#} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
1; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
__END__ |