lib/XBase/SDBM.pm

filter: Statementstmt 49.2% 99 / 201 Branchbran 37.2% 32 / 86 Conditioncond 50.0% 6 / 12 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 53.3% 8 / 15 Podpod 0.0% 0 / 13 totaltotal 44.1% 148 / 335 CC 49 SCAR 32.6
CC
49
Cov
46%
delete
90.0
new
34.9
prepare_select_eq
29.0
CRAP 26.0
« lib/XBase.pm ↑ Summary lib/XBase/Memo.pm »
1  
2 =head1 NAME
3  
4 XBase::SDBM - SDBM index support for dbf
5  
6 =head1 DESCRIPTION
7  
8 When developing the XBase.pm/DBD::XBase module, I was trying to
9 support as many existing variants of file formats as possible. The
10 module thus accepts wide range of dbf files and their versions from
11 various producers. But with index files, the task is much, much
12 harder. First, there is little or no documentation of index files
13 formats, so the development is based on reverse engineering.
14  
15 None if the index formats support is finalized. That made it hard to
16 integrate them into one consistent API. That is why I decided to write
17 my own index support, and as I wanted to avoid inventing yet another
18 way of storing records in pages and similar things, I used SDBM. It
19 comes with Perl, so you already have it, and it's proven and it
20 works.
21  
22 Now, SDBM is a module that aims at other task than to do supporting
23 indexes for a dbf. But equality tests are fast with it and I have
24 creted a structure in each index file to enable "walk" though the
25 index file.
26  
27 =head1 VERSION
28  
29 1.02
30  
31 =head1 AVAILABLE FROM
32  
33 http://www.adelton.com/perl/DBD-XBase/
34  
35 =head1 AUTHOR
36  
37 (c) 2001--2011 Jan Pazdziora.
38  
39 All rights reserved. This package is free software; you can
40 redistribute it and/or modify it under the same terms as Perl itself.
41  
42 =cut
43  
44 package XBase::SDBM;
45 1 ▶use SDBM_File;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 sub fetch {
49 85 ▶ my $self = shift;
Subroutine fetch: called CC=5 SCAR=16.5
Pod undocumented
50 85 my $current = $self->{'current'}; # current pointer
51 85 ▶ return unless defined $current;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $current 3 82 85
52 82 my $hash = $self->{'sdbmhash'};
53 82 my $value = $hash->{$current};
54  
55 82 ▶ if (not defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 82 82
56 0 delete $self->{'current'};
57 0 return;
58 }
59 82 my ($key, $num) = ($current =~ /^(.*):(\d+)$/s);
60 82 $num++;
61 82 ▶ if (defined $hash->{"$key:$num"}) { # next record for the same key
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$hash{"${key}:$num"}) { } 0 82 82
62 0 $self->{'current'} = "$key:$num";
63 } else {
64 82 my $newkey = $hash->{"$key:next"}; # next key
65 82 ▶ if (defined $newkey) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $newkey) { } 79 3 82
66 79 $self->{'current'} = "$newkey:1";
67 } else {
68 3 delete $self->{'current'};
69 }
70 }
71 82 return ($key, $value);
72 }
73 sub fetch_current {
74 0 ▶ my $self = shift;
Subroutine fetch_current: not called CC=3 SCAR=24.8
Pod undocumented
75 0 my $current = $self->{'current'};
76 0 ▶ return unless defined $current;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $current 0 0 0
77 0 my $value = $self->{'sdbmhash'}{$current};
78 0 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
79 0 my ($key) = ($current =~ /^(.*):\d+$/s);
80 0 return ($key, $value);
81 }
82 sub tags {
83 0 ▶ my $self = shift;
Subroutine tags: not called CC=2 SCAR=17.9
Pod undocumented
84 0 ▶ return map { if (s/:file$//) { ( $_ ) } else { () } }
Branch: 1 branch
BranchTrueFalseTotal
if (s/:file$//) { } 0 0 0
85 0 keys %{$self->{'definition'}};
86 }
87  
88 sub prepare_select {
89 1 ▶ my $self = shift;
Subroutine prepare_select: called CC=2 SCAR=7.0
Pod undocumented
90 1 $self->{'current'} = $self->{'sdbmhash'}{':first'};
91 1 ▶ $self->{'current'} .= ':1' if defined $self->{'current'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"current"} 1 0 1
92 1 1;
93 }
94 sub prepare_select_eq {
95 2 ▶ my ($self, $eq, $recno) = @_;
Subroutine prepare_select_eq: called CC=12 SCAR=33.7
Pod undocumented
96 2 delete $self->{'current'};
97 2 my $hash = $self->{'sdbmhash'};
98 2 my $start = $eq;
99 2 my $value = $hash->{"$start:1"};
100  
101 2 ▶ if (not defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 1 1 2
102 # not exact match
103 1 $start = $hash->{':first'};
104 1 ▶ if (not defined $start) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $start) 0 1 1
105 # no records, jsut return
106 0 return 1;
107 }
108 # move throught the chain
109 1 ▶ while (defined $start and $start lt $eq) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $start and $start lt $eq) 22 1 23
Condition66%2/3
defined $start and $start lt $eq
!ll&&!rl&&r
0122
MC/DC50%1/2
defined $start and $start lt $eq
defined $start $start lt $eq
Truth table66%2/3
A and B
A= defined $startB= $start lt $eq
ABresult
0X0
100
111
110 22 $start = $hash->{"$start:next"};
111 }
112 1 ▶ if (not defined $start) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $start) 0 1 1
113 0 return 1;
114 }
115 1 ▶ if ($start gt $eq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start gt $eq) 1 0 1
116 1 $self->{'current'} = "$start:1";
117 1 return 1;
118 }
119 # we shouldn't have never got here, but nevermind
120 0 $value = $hash->{"$start:1"};
121 }
122  
123 # here we've found exact match of the key
124 1 ▶ if (not defined $recno) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $recno) 1 0 1
125 # if not requested exact match of the recno, return
126 1 $self->{'current'} = "$start:1";
127 1 return 1;
128 }
129  
130 0 my $num = 1;
131 0 ▶ while (defined $value and $value != $recno) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and $value != $recno) 0 0 0
Condition0%0/3
defined $value and $value != $recno
!ll&&!rl&&r
000
MC/DC0%0/2
defined $value and $value != $recno
defined $value $value != $recno
Truth table0%0/3
A and B
A= defined $valueB= $value != $recno
ABresult
0X0
100
111
132 0 $num++;
133 0 $value = $hash->{"$start:$num"};
134 }
135  
136 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 0 0
137 0 $self->{'current'} = "$start:$num";
138 } else {
139 0 $start = $hash->{"$start:next"};
140 0 ▶ $self->{'current'} = "$start:1" if defined $start;
Branch: 1 branch
BranchTrueFalseTotal
if defined $start 0 0 0
141 }
142 0 1;
143 }
144  
145  
146 # method new (open) will open the named SDBM index for given dbf
147 sub new {
148 1 ▶ my ($class, $filename, %opts) = @_;
Subroutine new: called CC=7 SCAR=35.5
Pod undocumented
149 1 my $dbf = $opts{'dbf'};
150 1 my $tag = $opts{'tag'};
151
152 # return immediatelly if the index file was already opened
153 return $dbf->{'sdbm_definition'}{'tags'}{$tag}
154 if defined $dbf->{'sdbm_definition'}
155 1 ▶ and defined $dbf->{'sdbm_definition'}{'tags'}{$tag};
Branch: 1 branch
BranchTrueFalseTotal
if defined $dbf->{"sdbm_definition"} and defined $dbf->{"sdbm_definition"}{"tags"}{$tag} 1 0 1
Condition33%1/3
defined $dbf->{"sdbm_definition"} and defined $dbf->{"sdbm_definition"}{"tags"}{$tag}
!ll&&!rl&&r
001
MC/DC0%0/2
defined $dbf->{"sdbm_definition"} and defined $dbf->{"sdbm_definition"}{"tags"}{$tag}
defined $dbf->{"sdbm_definition"} defined $dbf->{"sdbm_definition"}{"tags"}{$tag}
Truth table33%1/3
A and B
A= defined $dbf->{"sdbm_definition"}B= defined $dbf->{"sdbm_definition"}{"tags"}{$tag}
ABresult
0X0
100
111
156  
157 0 my $dbffile = $dbf->{'filename'};
158 0 my $file = $dbffile;
159 0 $file =~ s/\.dbf$/.sdbmd/i;
160  
161 # some of the SDBM indexes were already touched
162 # the definitionhash is a SDBM that lists the content of the
163 # actual SDBM index files
164 0 my $definitionhash = {};
165 0 ▶ if (defined $dbf->{'sdbm_definition'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dbf->{'sdbm_definition'}) { } 0 0 0
166 0 $definitionhash = $dbf->{'sdbm_definition'}{'definitionhash'};
167 }
168 else {
169 # if it wasn't opened yet, open the definition file
170 0 ▶ if (not tie(%$definitionhash, 'SDBM_File',
Branch: 1 branch
BranchTrueFalseTotal
unless (tie %$definitionhash, "SDBM_File", $file, 2, 438) 0 0 0
171 $file, O_RDWR, 0666)) {
172 0 die "SDBM index definition file `$file' not found for `$dbffile': $!.";
173 }
174 0 $dbf->{'sdbm_definition'} = { 'filename' => $file,
175 'definitionhash' => $definitionhash };
176 }
177  
178 # check the definition file for tag requested
179 0 my $sdbmfile = $definitionhash->{"$tag:file"};
180 0 ▶ if (not defined $sdbmfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sdbmfile) 0 0 0
181 # no such SDBM index exists, the definition SDBM says
182 0 die "SDBM index `$tag' not known for `$dbffile'.";
183 }
184  
185 # open the SDBM index file
186 0 my $sdbmhash = {};
187 0 ▶ unless (tie(%$sdbmhash, 'SDBM_File', $sdbmfile, O_RDWR, 0666)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (tie %$sdbmhash, "SDBM_File", $sdbmfile, 2, 438) 0 0 0
188 0 die "SDBM index file `$sdbmfile' not found for `$dbffile': $!.";
189 }
190  
191 0 my $self = bless { 'dbf' => $dbf,
192 'tag' => $tag, 'sdbmhash' => $sdbmhash,
193 'definition' => $definitionhash }, $class;
194 0 $dbf->{'sdbm_definition'}{'tags'}{$tag} = $self;
195 0 return $self;
196 }
197 1 *open = \&new;
198  
199 # method create will create SDBM index with given name and expression
200 # for the dbf table
201 sub create {
202 1 ▶ my ($class, $dbf, $tag, $expression) = @_;
Subroutine create: called CC=9 SCAR=23.4
Pod undocumented
203 1 my $dbffile = $dbf->{'filename'};
204 1 my $file;
205  
206 my $definitionhash;
207 1 ▶ if (defined $dbf->{'sdbm_definition'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dbf->{'sdbm_definition'}) { } 0 1 1
208 # the definition SDBM was already opened
209 0 $definitionhash = $dbf->{'sdbm_definition'}{'definitionhash'};
210 } else {
211 1 $file = $dbffile;
212 1 $file =~ s/\.dbf$/.sdbmd/i;
213  
214 1 $definitionhash = {};
215 # open or create the definition SDBM file
216 1 ▶ if (not tie(%$definitionhash, 'SDBM_File',
Branch: 1 branch
BranchTrueFalseTotal
unless (tie %$definitionhash, "SDBM_File", $file, 66, 438) 0 1 1
217 $file, O_RDWR|O_CREAT, 0666)) {
218 0 die "SDBM index definition file `$file' not found/created for `$dbffile': $!.";
219 }
220 1 $dbf->{'sdbm_definition'} = { 'filename' => $file,
221 'definitionhash' => $definitionhash };
222 }
223  
224 1 ▶ if (defined $definitionhash->{"$tag:file"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$definitionhash{"${tag}:file"}) 0 1 1
225 0 die "SDBM index `$tag' already exists for `$dbfffile'.";
226 }
227  
228 1 my $maxindexnumber = ++$definitionhash->{'tagnumber'};
229  
230 1 my $sdbmfile = $dbffile;
231 1 $sdbmfile =~ s/\.dbf$/.sdbm$maxindexnumber/i;
232  
233 1 my $sdbmhash = {};
234 1 ▶ if (not tie(%$sdbmhash, 'SDBM_File', $sdbmfile, O_CREAT|O_EXCL|O_RDWR, 0666)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (tie %$sdbmhash, "SDBM_File", $sdbmfile, 194, 438) 0 1 1
235 0 die "SDBM index file `$sdbmfile' couldn't be created for `$dbffile': $!."
236 }
237  
238 1 my $self = bless { 'dbf' => $dbf, 'tag' => $tag,
239 'sdbmhash' => $sdbmhash,
240 'definition' => $definitionhash}, $class;
241 1 $dbf->{'sdbm_definition'}{'tags'}{$tag} = $self;
242 1 $definitionhash->{"$tag:file"} = $sdbmfile;
243  
244 1 ▶ if (defined $dbf->field_type(uc $expression)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dbf->field_type(uc $expression)) 1 0 1
245 1 $expression = uc $expression;
246 }
247 1 ▶ if (not defined $dbf->field_type($expression)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dbf->field_type($expression)) 0 1 1
248 0 $self->drop;
249 0 die "SDBM index `$expression' couldn't be created for `$dbffile': no such column name.";
250 }
251 1 $definitionhash->{"$tag:expression"} = $expression;
252  
253 1 my $i = 0;
254 1 ▶ while ($i <= $dbf->last_record) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $dbf->last_record) 42 1 43
255 42 my ($deleted, $value) = $dbf->get_record($i);
256 42 ▶ if (not $deleted) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($deleted) 42 0 42
257 42 $self->insert($value, $i + 1);
258 }
259 42 $i++;
260 }
261  
262 1 return $self;
263 }
264  
265 # method drop will drop the SDBM index
266 sub drop {
267 0 ▶ my ($self) = @_;
Subroutine drop: not called CC=1 SCAR=6.9
Pod undocumented
268 0 my $tag = $self->{'tag'};
269 0 my $definitionhash = $self->{'definition'};
270 0 my $sdbmfile = $definitionhash->{"$tag:file"};
271 0 delete $definitionhash->{"$tag:file"};
272 0 delete $definitionhash->{"$tag:definition"};
273 0 delete $self->{'dbf'}{'sdbm_definition'}{'tags'}{$tag};
274 0 unlink "$sdbmfile.pag", "$sdbmfile.dir";
275 }
276  
277 sub insert {
278 42 ▶ my ($self, $key, $value) = @_;
Subroutine insert: called CC=6 SCAR=17.9
Pod undocumented
279 ### print "Adding $key $value\n";
280 42 my $hash = $self->{'sdbmhash'};
281 42 my $key_maxid = $hash->{"$key:0"};
282 42 $key_maxid++;
283  
284 42 $hash->{"$key:$key_maxid"} = $value;
285 42 $hash->{"$key:0"} = $key_maxid;
286 42 ▶ return 1 if $key_maxid > 1; # no need to change the chain
Branch: 1 branch
BranchTrueFalseTotal
if $key_maxid > 1 0 42 42
287
288 42 my $prev = undef;
289 42 my $prev_next = ':first';
290 42 my $next;
291 42 ▶ while (defined($next = $hash->{$prev_next}) and $key gt $next) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($next = $hash->{$prev_next}) and $key gt $next) 591 42 633
Condition100%3/3
defined($next = $hash->{$prev_next}) and $key gt $next
!ll&&!rl&&r
1032591
MC/DC100%2/2
defined($next = $hash->{$prev_next}) and $key gt $next
defined($next = $hash->{$prev_next}) $key gt $next
Truth table100%3/3
A and B
A= defined($next = $hash->{$prev_next})B= $key gt $next
ABresult
0X0
100
111
292 591 $prev = $next;
293 591 $prev_next = "$prev:next";
294 591 $next = undef;
295 }
296  
297 42 ▶ if (not defined $next) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $next) { } 10 32 42
298 10 $hash->{':last'} = $key; # we reached the last record
299 } else {
300 32 $hash->{"$key:next"} = $next;
301 32 $hash->{"$next:prev"} = $key;
302 }
303 42 ▶ if (not defined $prev) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $prev) { } 2 40 42
304 2 $hash->{':first'} = $key;
305 } else {
306 40 $hash->{"$prev:next"} = $key;
307 40 $hash->{"$key:prev"} = $prev;
308 }
309 42 return 1;
310 }
311  
312 sub delete {
313 0 ▶ my ($self, $key, $value) = @_;
Subroutine delete: not called CC=9 SCAR=45.0
Pod undocumented
314 ### print "Deleting $key $value\n";
315 0 my $hash = $self->{'sdbmhash'};
316 0 my $key_maxid = $hash->{"$key:0"};
317  
318 0 my $number = 1;
319 0 ▶ while ($number <= $key_maxid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number <= $key_maxid) 0 0 0
320 0 ▶ if ($hash->{"$key:$number"} == $value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$hash{"${key}:$number"} == $value) 0 0 0
321 0 last;
322 }
323 0 $number++;
324 }
325 0 ▶ if ($number > $key_maxid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number > $key_maxid) 0 0 0
326 # such a record was not found
327 0 return 0;
328 }
329  
330 0 ▶ if ($key_maxid > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key_maxid > 1) { } 0 0 0
331 0 ▶ $hash->{"$key:$number"} = $hash->{"$key:$key_maxid"}
Branch: 1 branch
BranchTrueFalseTotal
if $number != $key_maxid 0 0 0
332 if $number != $key_maxid;
333 0 delete $hash->{"$key:$key_maxid"};
334 0 $hash->{"$key:0"} = $key_maxid - 1;
335 } else {
336 0 my $next = $hash->{"$key:next"};
337 0 my $prev = $hash->{"$key:prev"};
338 0 ▶ if (defined $next) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $next) { } 0 0 0
339 0 ▶ if (not defined $prev) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $prev) { } 0 0 0
340 0 $hash->{':first'} = $next;
341 0 delete $hash->{"$next:prev"};
342 } else {
343 0 $hash->{"$prev:next"} = $next;
344 0 $hash->{"$next:prev"} = $prev;
345 0 delete $hash->{"$key:prev"};
346 }
347 0 delete $hash->{"$key:next"};
348 } else {
349 0 ▶ if (not defined $prev) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $prev) { } 0 0 0
350 0 delete $hash->{':first'};
351 0 delete $hash->{':last'};
352 } else {
353 0 $hash->{':last'} = $prev;
354 0 delete $hash->{"$prev:next"};
355 0 delete $hash->{"$key:prev"};
356 }
357 }
358 0 delete $hash->{"$key:0"};
359 0 delete $hash->{"$key:1"};
360 }
361 0 return 1;
362 }
363 sub delete_current {
364 0 ▶ my $self = shift;
Subroutine delete_current: not called CC=2 SCAR=17.9
Pod undocumented
365 0 my ($key, $value) = $self->fetch_current;
366 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
367 0 $self->delete($key, $value);
368 }
369 }
370 sub insert_before_current {
371 0 ▶ die "SDBM index doesn't support backward rolling yet.\n";
Subroutine insert_before_current: not called CC=1 SCAR=6.9
Pod undocumented
372 }
373  
374 sub dump {
375 0 ▶ my $self = shift;
Subroutine dump: not called CC=2 SCAR=17.9
Pod undocumented
376 0 my $hash = $self->{'sdbmhash'};
377  
378 0 for (sort keys %$hash) {
379 0 print "$_ $hash->{$_}\n";
380 }
381 }
382  
383 1 1;
384  
« lib/XBase.pm ↑ Summary lib/XBase/Memo.pm »