CAB/Analyzer/Dict/BDB.pm untested

filter: Statementstmt 0.0% 0 / 69 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 166 CC 9 SCAR 24.7
CC
9
Cov
0%
ensureLoaded
42.0
clear
6.0
dictHash
6.0
CRAP 11.8
1 ## -*- Mode: CPerl -*-
2 ##
3 ## File: DTA::CAB::Analyzer::Dict::BDB.pm
4 ## Author: Bryan Jurish <moocow@cpan.org>
5 ## Description: generic analysis dictionary API using Lingua::TT::DBFile
6  
7 package DTA::CAB::Analyzer::Dict::BDB;
8 use DTA::CAB::Analyzer ':child';
9 use DTA::CAB::Analyzer::Dict;
10 use DTA::CAB::Format;
11 use Lingua::TT::DBFile;
12 use IO::File;
13 use Carp;
14 use DB_File;
15 use Fcntl;
16 use Encode qw(encode decode);
17  
18 use strict;
19  
20 ##==============================================================================
21 ## Globals
22 ##==============================================================================
23  
24 our @ISA = qw(DTA::CAB::Analyzer::Dict);
25  
26 ##==============================================================================
27 ## Constructors etc.
28 ##==============================================================================
29  
30 ## $obj = CLASS_OR_OBJ->new(%args)
31 ## + object structure:
32 ## (
33 ## ##-- Filename Options
34 ## dictFile => $filename, ##-- DB filename (default=undef)
35 ##
36 ## ##-- Analysis Output
37 ## label => $lab, ##-- analyzer label
38 ## analyzeCode => $code, ##-- pseudo-accessor to perform actual analysis for token ($_); see DTA::CAB::Analyzer::Dict for details
39 ##
40 ## ##-- Analysis Options
41 ## encoding => $enc, ##-- encoding of db file (default='UTF-8'): clobbers $dba{encoding} ; uses DB filters
42 ## keyEncoding => $enc, ##-- encoding of db file (default=undef): alternative to 'encoding'
43 ## valEncoding => $enc, ##-- encoding of db file (default=undef): alternative to 'encoding'
44 ##
45 ## ##-- Analysis objects
46 ## dbf => $dbf, ##-- underlying Lingua::TT::DBFile object (default=undef)
47 ## dba => \%dba, ##-- args for Lingua::TT::DBFile->new()
48 ## #={
49 ## # mode => $mode, ##-- default: 0644
50 ## # flags => $flags, ##-- default: O_RDONLY
51 ## # type => $type, ##-- one of 'HASH', 'BTREE', 'RECNO', 'GUESS' (default: 'GUESS')
52 ## # dbinfo => \%dbinfo, ##-- default: "DB_File::${type}INFO"->new();
53 ## # dbopts => \%opts, ##-- db options (e.g. cachesize,bval,...) -- defaults to none (uses DB_File defaults)
54 ## # }
55 ## )
56 sub new {
57 my $that = shift;
58 my $dic = $that->SUPER::new(
59 ##-- filenames
60 dictFile => undef,
61  
62 ##-- analysis objects
63 dbf=>undef,
64 dba=>{
65 type => 'GUESS',
66 mode => 0644,
67 flags => O_RDONLY,
68 },
69  
70 ##-- options
71 encoding => 'UTF-8',
72 #keyEncoding => undef,
73 #valEncoding => undef,
74  
75 ##-- analysis output
76 label => 'dict',
77 analyzeCode => $DTA::CAB::Analyzer::Dict::CODE_DEFAULT,
78  
79 ##-- user args
80 @_
81 );
82 delete($dic->{ttd}); ##-- don't inherit 'ttd' (in-memory hash dict) key
83 return $dic;
84 }
85  
86 ## $dic = $dic->clear()
87 ## + just closes db
88 sub clear {
89 my $dic = shift;
90 $dic->{dbf}->close if ($dic->{dbf} && $dic->{dbf}->opened);
91 delete($dic->{dbf});
92 return $dic;
93 }
94  
95  
96 ##==============================================================================
97 ## Methods: Embedded API
98 ##==============================================================================
99  
100 ## $bool = $dic->dictOk()
101 ## + returns false iff dict is undefined or "empty"
102 sub dictOk {
103 return $_[0]{dbf} && $_[0]{dbf}->opened;
104 }
105  
106 ## \%key2val = $dict->dictHash()
107 ## + returns a (possibly tie()d hash) representing dict contents
108 ## + override returns $dict->{dbf}{data} or a new empty hash
109 sub dictHash {
110 return $_[0]{dbf} && $_[0]{dbf}->opened ? $_[0]{dbf}{data} : {};
111 }
112  
113 ## $val_or_undef = $dict->dictLookup($key)
114 ## + get stored value for key $key
115 ## + default returns $dict->{ttd}{dict}{$key} or undef
116 sub dictLookup {
117 return undef if (!$_[0]{dbf} || !$_[0]{dbf}->opened);
118 # return decode($_[0]{dbEncoding}, $_[0]{dbf}{data}{encode($_[0]{dbEncoding},$_[1])})
119 # if (defined($_[0]{dbEncoding}));
120 return $_[0]{dbf}{data}{$_[1]};
121 }
122  
123  
124 ##==============================================================================
125 ## Methods: I/O
126 ##==============================================================================
127  
128 ##--------------------------------------------------------------
129 ## Methods: I/O: Input: all
130  
131 ## $bool = $dic->ensureLoaded()
132 ## + ensures analyzer data is loaded from default files
133 sub ensureLoaded {
134 my $dic = shift;
135 my $rc = 1;
136 if ( defined($dic->{dictFile}) && !$dic->dictOk ) {
137 $dic->info("opening DB file '$dic->{dictFile}'");
138 $dic->{dbf} = Lingua::TT::DBFile->new(%{$dic->{dba}||{}});
139 $rc &&= $dic->{dbf}->open($dic->{dictFile}, encoding=>$dic->{encoding});
140  
141 ##-- setup filters
142 if ($rc && (!$dic->{encoding} || $dic->{encoding} eq 'raw')) {
143 my $dbf = $dic->{dbf};
144 my $tied = $dic->{dbf}{tied};
145 if ($dic->{keyEncoding} && $dic->{keyEncoding} ne 'raw') {
146 $tied->filter_fetch_key($dbf->encFilterFetch($dic->{keyEncoding}));
147 $tied->filter_store_key($dbf->encFilterStore($dic->{keyEncoding}));
148 }
149 if ($dic->{valEncoding} && $dic->{valEncoding} ne 'raw') {
150 $tied->filter_fetch_value($dbf->encFilterFetch($dic->{valEncoding}));
151 $tied->filter_store_value($dbf->encFilterStore($dic->{valEncoding}));
152 }
153 }
154 }
155 $dic->logwarn("error opening file '$dic->{dictFile}': $!") if (!$rc);
156 return $rc;
157 }
158  
159 ##==============================================================================
160 ## Methods: Persistence
161 ##==============================================================================
162  
163 ##======================================================================
164 ## Methods: Persistence: Perl
165  
166 ## @keys = $class_or_obj->noSaveKeys()
167 ## + returns list of keys not to be saved
168 sub noSaveKeys {
169 my $that = shift;
170 return ($that->SUPER::noSaveKeys, qw(dbf));
171 }
172  
173 ## @keys = $class_or_obj->noSaveBinKeys()
174 ## + returns list of keys not to be saved
175 sub noSaveBinKeys {
176 my $that = shift;
177 return ($that->SUPER::noSaveBinKeys, qw(dbf));
178 }
179  
180 ## $saveRef = $obj->savePerlRef()
181 ## + inherited from DTA::CAB::Persistent
182  
183 ## $loadedObj = $CLASS_OR_OBJ->loadPerlRef($ref)
184 ## + implicitly calls $obj->clear()
185 sub loadPerlRef {
186 my ($that,$ref) = @_;
187 my $obj = $that->SUPER::loadPerlRef($ref);
188 return $obj;
189 }
190  
191 ##==============================================================================
192 ## Methods: Analysis
193 ##==============================================================================
194  
195 ##------------------------------------------------------------------------
196 ## Methods: Analysis: Generic
197  
198 ## $bool = $anl->canAnalyze()
199 ## + returns true if analyzer can perform its function (e.g. data is loaded & non-empty)
200 ## + INHERITED from dict: calls dictOk()
201  
202 ##------------------------------------------------------------------------
203 ## Methods: Analysis: v1.x: API
204  
205 ## $doc = $anl->analyzeTypes($doc,\%types,\%opts)
206 ## + perform type-wise analysis of all (text) types in $doc->{types}
207 ## + INHERITED from Dict
208  
209  
210 1; ##-- be happy
211  
212 __END__