File Coverage

blib/lib/TM/ResourceAble/BDB.pm
Criterion Covered Total %
statement 28 30 93.3
branch n/a
condition n/a
subroutine 10 10 100.0
pod n/a
total 38 40 95.0


line stmt bran cond sub pod time code
1             package TM::ResourceAble::BDB::main;
2              
3 1     1   1243 use strict;
  1         3  
  1         41  
4 1     1   6 use warnings;
  1         3  
  1         34  
5              
6 1     1   5 use Tie::Hash;
  1         169  
  1         36  
7 1     1   6 use base qw(Tie::StdHash);
  1         2  
  1         810  
8              
9             sub FETCH {
10             my ($self, $key) = @_;
11             #warn "FETCH main $key";
12             if ($key eq 'assertions' || $key eq 'mid2iid') {
13             return $self->{$key}; # where the BDB is tied to
14             } elsif ($key eq 'index') { # not part of this game
15             return $self->{'index'};
16             } elsif ($key eq 'variants') { # not part of this game
17             return $self->{'variants'};
18             } elsif ($key eq '__main') { # dont like being asked about that
19             return $self->{'__main'};
20             } else {
21             return $self->{'__main'}->{$key}; # get it from the secret store
22             }
23             }
24              
25             sub STORE {
26             my ($self, $key, $val) = @_;
27             #warn "STORE main $key $val";
28             if ($key eq 'assertions' || $key eq 'mid2iid') {
29             $self->{$key} = $val; # those go directly into the hash
30             } elsif ($key eq 'index') { # not part of this game
31             $self->{'index'} = $val;
32             } elsif ($key eq 'variants') { # not part of this game
33             $self->{'variants'} = $val; # memory-only, will not be persisted, THIS IS A BUG
34             } elsif ($key eq '__main') { # this is *directly* store (the value being a tied hash)
35             $self->{'__main'} = $val;
36             } else {
37             $self->{'__main'}->{$key} = $val; # those will be redirected into the tied store
38             }
39             }
40              
41             1;
42              
43             package TM::ResourceAble::BDB;
44              
45 1     1   7 use strict;
  1         3  
  1         38  
46 1     1   5 use warnings;
  1         3  
  1         33  
47              
48 1     1   5 use Data::Dumper;
  1         2  
  1         88  
49              
50 1     1   7 use TM;
  1         2  
  1         42  
51 1     1   5 use base qw(TM);
  1         2  
  1         78  
52 1     1   506 use Class::Trait ('TM::ResourceAble');
  0            
  0            
53              
54              
55             sub new {
56             my $class = shift;
57             my %options = @_;
58              
59             my $file = delete $options{file} or die "file?";
60             $options{url} = "file:$file";
61              
62             my %self;
63             tie %self, 'TM::ResourceAble::BDB::main', "$file.main";
64              
65             use BerkeleyDB;
66             my %flags = (-Flags => DB_CREATE ) unless -e "$file.main" && -s "$file.main";
67              
68             # warn Dumper \%flags;
69              
70             my $dbm = tie %{ $self{'__main'} }, 'BerkeleyDB::Hash',
71             -Filename => "$file.main", %flags;
72              
73             # $dbm->filter_store_value ( sub {
74             # warn "really storing $_ into __main";
75             # $_;
76             # } ) ;
77             # $dbm->filter_fetch_value ( sub {
78             # warn "really getting $_ from __main";
79             # $_;
80             # } ) ;
81              
82              
83             my $dba = tie %{ $self{assertions} }, 'BerkeleyDB::Hash',
84             -Filename => "$file.assertions", %flags;
85              
86             $dba->filter_store_value ( sub {
87             use Storable qw(freeze);
88             $_ = freeze ($_);
89             } ) ;
90             $dba->filter_fetch_value ( sub {
91             use Storable qw(thaw);
92             $_ = thaw ($_);
93             } ) ;
94             my $dbt = tie %{ $self{mid2iid} }, 'BerkeleyDB::Hash',
95             -Filename => "$file.toplets", %flags;
96              
97             $dbt->filter_store_value ( sub {
98             use Storable qw(freeze);
99             $_ = freeze ($_);
100             } ) ;
101             $dbt->filter_fetch_value ( sub {
102             use Storable qw(thaw);
103             $_ = thaw ($_);
104             } ) ;
105              
106             unless (defined $self{last_mod}) { # empty? => careful cloning from prototypical TM
107             #warn "initializing BDB";
108             my $tmp = bless $class->SUPER::new (%options), $class;
109             foreach my $k (keys %$tmp) {
110             if ($k eq 'mid2iid') {
111             my $mid2iid = $self{mid2iid}; # fetch once
112             $mid2iid->{$_} = $tmp->{mid2iid}->{$_} for keys %{ $tmp->{mid2iid} };
113             $self{mid2iid} = $mid2iid;
114              
115             } elsif ($k eq 'assertions') {
116             my $asserts = $self{assertions};
117             $asserts->{$_} = $tmp->{assertions}->{$_} for keys %{ $tmp->{assertions} };
118             $self{assertions} = $asserts;
119              
120             } else {
121             $self{$k} = $tmp->{$k};
122             }
123             }
124             }
125             return bless \%self, $class;
126             }
127              
128             1;
129              
130             __END__