CCDICT/ResultSet/BerkeleyDB.pm untested

filter: Statementstmt 0.0% 0 / 34 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 50 CC 3 SCAR 12.8
CC
3
Cov
0%
next
6.0
_get_value
6.0
new
2.0
CRAP 3.6
1 package Lingua::ZH::CCDICT::ResultSet::BerkeleyDB;
2  
3 use strict;
4 use warnings;
5  
6 use base 'Lingua::ZH::CCDICT::ResultSet';
7  
8 use Storable qw( thaw );
9  
10  
11 sub new
12 {
13 my $class = shift;
14 my %p = @_;
15  
16 return bless { index => 0,
17 keys => $p{keys},
18 db => $p{db},
19 }, $class;
20 }
21  
22 sub next
23 {
24 my $self = shift;
25  
26 my $index = $self->{index};
27  
28 unless ( exists $self->{keys}[$index] )
29 {
30 $self->{index} = 0;
31  
32 return;
33 }
34  
35 my $value = $self->_get_value( $self->{keys}[$index] );
36  
37 $self->{index}++;
38  
39 return Lingua::ZH::CCDICT::ResultItem->new( thaw($value) );
40 }
41  
42 sub all
43 {
44 my $self = shift;
45  
46 return
47 ( map { Lingua::ZH::CCDICT::ResultItem->new( thaw( $self->_get_value($_) ) ) }
48 @{ $self->{keys} }
49 );
50 }
51  
52 sub _get_value
53 {
54 my $self = shift;
55 my $key = shift;
56  
57 my $value;
58 my $status = $self->{db}->db_get( $key, $value );
59  
60 die "Failed to retrieve key ($key): $status" if $status;
61  
62 return $value;
63 }
64  
65  
66 1;
67  
68 __END__