CCDICT/Storage/BerkeleyDB.pm untested

filter: Statementstmt 0.0% 0 / 90 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 151 CC 12 SCAR 27.8
CC
12
Cov
0%
_get_all_values
42.0
_real_add_entry
30.0
new
6.0
CRAP 16.1
1 package Lingua::ZH::CCDICT::Storage::BerkeleyDB;
2  
3 use strict;
4 use warnings;
5  
6 use base qw( Lingua::ZH::CCDICT );
7  
8 use BerkeleyDB qw( DB_CREATE DB_DUP DB_INIT_MPOOL DB_NOTFOUND DB_SET DB_NEXT DB_NEXT_DUP );
9  
10 use File::Spec;
11  
12 use Params::Validate qw( validate SCALAR );
13  
14 use Storable qw( nfreeze );
15  
16 use Lingua::ZH::CCDICT::ResultItem;
17 use Lingua::ZH::CCDICT::ResultSet::BerkeleyDB;
18  
19  
20 sub new
21 {
22 my $class = shift;
23 my %p = validate( @_,
24 { work_dir => { type => SCALAR },
25 },
26 );
27  
28 my $env = BerkeleyDB::Env->new( -Home => $p{work_dir},
29 -Flags => DB_CREATE | DB_INIT_MPOOL,
30 )
31 or die "Cannot create BerkeleyDB::Env object: $BerkeleyDB::Error\n";
32  
33 my @files;
34  
35 my %main_db;
36 tie %main_db, 'BerkeleyDB::Hash', ( -Filename => 'ccdict_data',
37 -Env => $env,
38 -Flags => DB_CREATE,
39 )
40 or die "Cannot create BerkeleyDB::Hash object: $! -- $BerkeleyDB::Error\n";
41  
42  
43 push @files, File::Spec->catfile( $p{work_dir}, 'ccdict_data' );
44  
45 my %indexes;
46  
47 foreach my $type ( $class->_InternalTypes )
48 {
49 next if $type eq 'english';
50  
51 $indexes{$type} = BerkeleyDB::Hash->new( -Filename => "ccdict_index_$type",
52 -Env => $env,
53 -Flags => DB_CREATE,
54 -Property => DB_DUP,
55 )
56 or die "Cannot create BerkeleyDB::Hash object: $! -- $BerkeleyDB::Error\n";
57  
58 push @files, File::Spec->catfile( $p{work_dir}, "ccdict_index_$type" );
59 }
60  
61 return bless { data => \%main_db,
62 db => tied %main_db,
63 indexes => \%indexes,
64 files => \@files,
65 }, $class;
66 }
67  
68 sub _real_add_entry
69 {
70 my $self = shift;
71 my $unicode = shift;
72 my $entry = shift;
73  
74 foreach my $key ( keys %{ $entry } )
75 {
76 next if $key eq 'english' || $key eq 'unicode';
77  
78 foreach my $val ( ref $entry->{$key} eq 'ARRAY' ?
79 @{ $entry->{$key} } :
80 $entry->{$key} )
81 {
82  
83 my $status = $self->{indexes}{$key}->db_put( "$val" => $unicode );
84  
85 die "Failed to store key ($val): $status" if $status;
86 }
87 }
88  
89 my $status = $self->{db}->db_put( $unicode => nfreeze($entry) );
90  
91 die "Failed to store key ($unicode): $status" if $status;
92 }
93  
94 sub match_unicode
95 {
96 my $self = shift;
97  
98 my %seen;
99 my @chars = map { $seen{$_}++ ? () : $_ } sort @_;
100  
101 return
102 Lingua::ZH::CCDICT::ResultSet::BerkeleyDB->new
103 ( keys => \@chars,
104 db => $self->{db},
105 );
106 }
107  
108 sub _match
109 {
110 my $self = shift;
111 my $type = shift;
112  
113 return
114 $self->match_unicode
115 ( map { $self->_get_all_values( $self->{indexes}{$type}, $_ ) } @_ );
116 }
117  
118 sub _get_all_values
119 {
120 my $self = shift;
121 my $db = shift;
122 my $key = shift;
123  
124 my $cursor = $db->db_cursor;
125  
126 die "Failed to create a cursor: $BerkeleyDB::Error" unless $cursor;
127  
128 my @values;
129  
130 my $value;
131 my $status = $cursor->c_get( $key, $value, DB_SET );
132  
133 if ( $status )
134 {
135 return if $status == DB_NOTFOUND;
136  
137 die "Calling c_get on a cursor failed: $status";
138 }
139  
140 push @values, $value;
141  
142 while (1)
143 {
144 $status = $cursor->c_get( $key, $value, DB_NEXT_DUP );
145  
146 if ( $status )
147 {
148 last if $status == DB_NOTFOUND;
149  
150 die "Calling c_get on a cursor failed: $status";
151 }
152  
153 push @values, $value;
154 }
155  
156 return @values;
157 }
158  
159 sub all_characters
160 {
161 my $self = shift;
162  
163 return
164 Lingua::ZH::CCDICT::ResultSet::BerkeleyDB->new
165 ( keys => [ keys %{ $self->{data} } ],
166 db => $self->{db},
167 );
168 }
169  
170 sub entry_count
171 {
172 my $self = shift;
173  
174 return scalar keys %{ $self->{data} };
175 }
176  
177 sub files { @{ $_[0]->{files} } }
178  
179  
180 1;
181  
182 __END__