File Coverage

blib/lib/Tie/MAB2/Dualdb.pm
Criterion Covered Total %
statement 8 10 80.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 12 14 85.7


line stmt bran cond sub pod time code
1             package Tie::MAB2::Dualdb;
2              
3 1     1   20932 use strict;
  1         2  
  1         29  
4              
5             BEGIN {
6 1     1   809 use Tie::Array;
  1         1669  
  1         36  
7 1     1   33 our @ISA = qw(Tie::Array);
8             }
9              
10 1     1   1472 use BerkeleyDB qw( DB_RDONLY DB_CREATE DB_FAST_STAT DB_INIT_MPOOL DB_INIT_CDB);
  0            
  0            
11              
12             use Tie::MAB2::Dualdb::Recno;
13             use Tie::MAB2::Dualdb::Id;
14             use MAB2::Record::Base;
15             use File::Basename;
16              
17             sub TIEARRAY {
18             my($class,%args) = @_;
19             my $self = {};
20             $self->{ARGS} = \%args;
21              
22             # directory?
23             die unless $args{filename};
24             my $dir = File::Basename::dirname($args{filename});
25             my $basename = File::Basename::basename($args{filename});
26              
27             # environment?
28             my $flags = $args{flags};
29             my $env = BerkeleyDB::Env
30             ->new(
31             Home => $dir,
32             Flags => $flags|DB_INIT_MPOOL,
33             )
34             or die "Could not create environment: $BerkeleyDB::Error; home[$dir]flags[$args{flags}]";
35             $self->{ENV} = $env;
36              
37             my @recs;
38             tie(@recs,
39             "Tie::MAB2::Dualdb::Recno",
40             filename => "$basename",
41             flags => $flags,
42             env => $env,
43             ) or die "Could not tie \@recs: $BerkeleyDB::Error";
44             $self->{RECS} = \@recs;
45             my %recnos;
46             tie(%recnos,
47             "Tie::MAB2::Dualdb::Id",
48             filename => "$basename",
49             flags => $flags,
50             env => $env,
51             ) or die "Could not tie %recnos: $BerkeleyDB::Error";
52             $self->{IDS} = \%recnos;
53              
54             bless $self, ref $class || $class;
55             }
56              
57             sub env {
58             shift->{ENV};
59             }
60              
61             sub UNTIE {
62             my $self = shift;
63             untie %{$self->{IDS}};
64             untie @{$self->{RECS}};
65             delete $self->{ENV};
66             }
67              
68             sub FETCH {
69             my($self, $key) = @_;
70             my $obj = $self->{RECS}[$key];
71             $obj;
72             }
73              
74             sub FETCHSIZE {
75             my($self) = @_;
76             scalar @{$self->{RECS}}
77             }
78              
79             sub EXISTS {
80             my($self,$key) = @_;
81             exists $self->{RECS}[$key];
82             }
83              
84             sub STORE {
85             my($self,$key,$val) = @_;
86             if (my $oldrec = $self->{RECS}[$key]) {
87             my $oldid = $oldrec->id;
88             delete $self->{IDS}{$oldid};
89             }
90             if (! defined $val or ! length $val) {
91             $self->{RECS}[$key] = "";
92             return;
93             }
94             my $blrec = MAB2::Record::Base->new($val, $key);
95             my $id = $blrec->id;
96             if (defined(my $oldidx = $self->{IDS}{$id})) {
97             if ($oldidx != $key) {
98             require Carp;
99             Carp::confess("Duplicate record identified: trying to store id[$id] under key[$key] but found it as oldidx[$oldidx]");
100             }
101             }
102             $self->{RECS}[$key] = $val;
103             $self->{IDS}{$id} = $key;
104             }
105              
106             # sub CLEAR {
107             # my($self) = @_;
108             # warn "clearing the inner array";
109             # @{$self->{RECS}} = ();
110             # warn "cleared the inner array";
111             # warn "clearing the inner hash";
112             # %{$self->{IDS}} = ();
113             # warn "cleared the inner hash";
114             # }
115              
116             for my $method (qw(STORESIZE CLEAR POP SHIFT UNSHIFT SPLICE)) {
117             no strict "refs";
118             *$method = sub {
119             require Carp;
120             Carp::confess("$method not supported on ".ref shift);
121             return;
122             };
123             }
124              
125             1;
126              
127             __END__