lib/XBase/Index.pm

filter: Statementstmt 62.1% 585 / 942 Branchbran 47.3% 200 / 422 Conditioncond 52.5% 42 / 80 MC/DCmcdc 33.3% 16 / 48 Subroutinesub 80.9% 51 / 63 Podpod 7.6% 1 / 13 totaltotal 57.0% 895 / 1568 CC 243 SCAR 46.1
CC
243
Cov
57%
write_with_context
718.9
new
147.6
read_header
110.0
CRAP 100.6
« lib/XBase/SQL.pm ↑ Summary lib/XBase.pm »
1  
2 =head1 NAME
3  
4 XBase::Index - base class for the index files for dbf
5  
6 =cut
7  
8 package XBase::Index;
9 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use vars qw( @ISA $DEBUG $VERSION $VERBOSE $BIGEND );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use XBase::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 @ISA = qw( XBase::Base );
13  
14 6 $VERSION = '1.05';
15  
16 6 $DEBUG = 0;
17  
18 6 ▶$VERBOSE = 0 unless defined $VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $XBase::Index::VERBOSE 5 1 6
19  
20 # We will setup global variable to denote the byte order (endian)
21 6 my $packed = pack('d', 1);
22 6 ▶if ($packed eq "\077\360\000\000\000\000\000\000") {
Branch: 2 branches
BranchTrueFalseTotal
if ($packed eq "?\360\000\000\000\000\000\000") { } 0 6 6
elsif ($packed eq "\000\000\000\000\000\000\360?") { } 6 0 6
23 0 $BIGEND = 1;
24 } elsif ($packed eq "\000\000\000\000\000\000\360\077") {
25 6 $BIGEND = 0;
26 } else {
27 0 die "XBase::Index: your architecture is not supported.\n";
28 }
29  
30 # Open appropriate index file and create object according to suffix
31 sub new {
32 16 ▶ my ($class, $file) = (shift, shift);
Subroutine new: called CC=8 SCAR=22.2
Pod documented
33 16 my @opts = @_;
34 16 ▶print "XBase::Index::new($class, $file, @_)\n" if $XBase::Index::VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $XBase::Index::VERBOSE 0 16 16
35 16 ▶ if (ref $class) { @opts = ('dbf', $class, @opts); }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) 6 10 16
36 16 my ($ext) = ($file =~ /\.(...)$/);
37 16 $ext = lc $ext;
38  
39 16 ▶ if ($ext eq 'sdbm' or $ext eq 'pag' or $ext eq 'dir') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext eq "sdbm" or $ext eq "pag" or $ext eq "dir") 1 15 16
Condition66%4/6
$ext eq "sdbm" or $ext eq "pag" or $ext eq "dir"
l!l&&r!l&&!r
1015
$ext eq "sdbm" or $ext eq "pag"
l!l&&r!l&&!r
0115
MC/DC33%1/3
$ext eq "sdbm" or $ext eq "pag" or $ext eq "dir"
$ext eq "sdbm" $ext eq "pag" $ext eq "dir"
Truth table50%2/4
A or B or C
A= $ext eq "sdbm"B= $ext eq "pag"C= $ext eq "dir"
ABCresult
1XX1
01X1
0011
0000
40 1 require XBase::SDBM;
41 1 $ext = 'SDBM';
42 }
43  
44 16 my $object = eval "new XBase::$ext \$file, \@opts";
45 16 ▶ return $object if defined $object;
Branch: 1 branch
BranchTrueFalseTotal
if defined $object 16 0 16
46  
47 0 ▶ __PACKAGE__->Error("Error loading index: unknown extension\n") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
48 0 return;
49 }
50  
51 # For XBase::*x object, a record is one page, object XBase::*x::Page here
52 sub get_record {
53 227 ▶ my ($self, $num) = @_;
Subroutine get_record: called CC=6 SCAR=20.6
Pod undocumented
54 return $self->{'pages_cache'}{$num}
55 227 ▶ if defined $self->{'pages_cache'}{$num};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"pages_cache"}{$num} 182 45 227
56  
57 45 my $newpage = (ref $self) . '::Page::new';
58 45 my $page = $self->$newpage($num);
59  
60 45 ▶ if (defined $page) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $page) 45 0 45
61 45 $self->{'pages_cache'}{$num} = $page;
62  
63 45 local $^W = 0;
64 45 ▶ print "Page $page->{'num'}:\tkeys: @{[ map { s/\s+$//; $_; } @{$page->{'keys'}}]}\n\tvalues: @{$page->{'values'}}\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XBase::Index::DEBUG 0 45 45
65 45 ▶ print "\tlefts: @{$page->{'lefts'}}\n" if defined $page->{'lefts'} and $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if defined $page->{"lefts"} and $XBase::Index::DEBUG 0 45 45
Condition66%2/3
defined $page->{"lefts"} and $XBase::Index::DEBUG
!ll&&!rl&&r
17280
MC/DC0%0/2
defined $page->{"lefts"} and $XBase::Index::DEBUG
defined $page->{"lefts"} $XBase::Index::DEBUG
Truth table66%2/3
A and B
A= defined $page->{"lefts"}B= $XBase::Index::DEBUG
ABresult
0X0
100
111
66 }
67 45 $page;
68 }
69  
70 # Get next (value, record number in dbf) pair
71 # The important values of the index object are 'level' holding the
72 # current level of the "cursor", 'pages' holding an array of pages
73 # currently open for each level and 'rows' with an array of current row
74 # in each level
75 sub fetch {
76 1366 ▶ my $self = shift;
Subroutine fetch: called CC=14 SCAR=27.3
Pod undocumented
77 1366 my ($level, $page, $row, $key, $val, $left);
78
79 # cycle while we get to the leaf record or otherwise get
80 # a real value, not a pointer to lower page
81 1366 ▶ while (not defined $val)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 1534 0 1534
82 {
83 1534 $level = $self->{'level'};
84  
85 # if we do not have level, let's start from zero
86 1534 ▶ if (not defined $level) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $level) 14 1520 1534
87 14 $level = $self->{'level'} = 0;
88 14 $page = $self->get_record($self->{'start_page'});
89 14 ▶ if (not defined $page) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $page) 0 14 14
90 0 $self->Error("Index corrupt: $self: no root page $self->{'start_page'}\n");
91 0 return;
92 }
93 # and initialize 'pages' and 'rows'
94 14 $self->{'pages'} = [ $page ];
95 14 $self->{'rows'} = [];
96 }
97  
98 # get current page for this level
99 1534 $page = $self->{'pages'}[$level];
100 1534 ▶ if (not defined $page) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $page) 0 1534 1534
101 0 $self->Error("Index corrupt: $self: page for level $level lost in normal course\n");
102 0 return;
103 }
104  
105 # get current row for current level and increase it
106 # (or setup to zero)
107 1534 my $row = $self->{'rows'}[$level];
108 1534 ▶ if (not defined $row) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $row) { } 142 1392 1534
109 142 $row = $self->{'rows'}[$level] = 0;
110 } else {
111 1392 $self->{'rows'}[$level] = ++$row;
112 }
113  
114 # get the (key, value, pointer) from the page
115 1534 ($key, $val, $left) = $page->get_key_val_left($row);
116  
117 # there is another page to walk
118 1534 ▶ if (defined $left) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $left) 118 1416 1534
119 # go deeper
120 118 $level++;
121 118 my $oldpage = $page;
122 # load the next page
123 118 $page = $self->get_record($left);
124 118 ▶ if (not defined $page) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $page) 0 118 118
125 0 $self->Error("Index corrupt: $self: no page $left, ref'd from $oldpage, row $row, level $level\n");
126 0 return;
127 }
128 # and put it into the structure
129 118 $self->{'pages'}[$level] = $page;
130 118 $self->{'rows'}[$level] = undef;
131 118 $self->{'level'} = $level;
132 # and even if some index structures allow the
133 # value in the same row as record, we want to
134 # skip it when going down
135 118 $val = undef;
136 118 next;
137 }
138 # if we're lucky and got the value, return it
139 1416 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 1212 204 1416
140 1212 return ($key, $val);
141 }
142 # we neither got link to lower page, nor the value
143 # so it means we are backtracking the structure one
144 # (or more) levels back
145 else {
146 204 $self->{'level'} = --$level; # go up the levels
147 204 ▶ return if $level < 0; # do not fall over
Branch: 1 branch
BranchTrueFalseTotal
if $level < 0 51 153 204
148 153 $page = $self->{'pages'}[$level];
149 153 ▶ if (not defined $page)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $page) 0 153 153
150 {
151 0 $self->Error("Index corrupt: $self: page for level $level lost when backtracking\n");
152 0 return;
153 }
154 ### next unless defined $page;
155 153 $row = $self->{'rows'}[$level];
156 153 my ($backkey, $backval, $backleft) = $page->get_key_val_left($row);
157 # this is a hook for ntx files where we do not
158 # want to miss a values that are stored inside
159 # the structure, not only in leaves.
160 153 ▶ if (not defined $page->{'last_key_is_just_overflow'} and defined $backleft and defined $backval) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $page->{"last_key_is_just_overflow"} and defined $backleft and defined $backval) 103 50 153
Condition100%6/6
not defined $page->{"last_key_is_just_overflow"} and defined $backleft and defined $backval
!ll&&!rl&&r
1238103
not defined $page->{"last_key_is_just_overflow"} and defined $backleft
!ll&&!rl&&r
66141
MC/DC100%3/3
not defined $page->{"last_key_is_just_overflow"} and defined $backleft and defined $backval
not defined $page->{"last_key_is_just_overflow"} defined $backleft defined $backval
Truth table100%4/4
not(A) and B and C
A= not defined $page->{"last_key_is_just_overflow"}B= defined $backleftC= defined $backval
ABCresult
0XX0
10X0
1100
1111
161 103 return ($backkey, $backval);
162 }
163 }
164 }
165 0 return;
166 }
167  
168 # Get list of tags in the indexfile (an indexfile may not have any)
169 sub tags {
170 1 ▶ my $self = shift;
Subroutine tags: called CC=2 SCAR=7.1
Pod undocumented
171 1 ▶ @{$self->{'tags'}} if defined $self->{'tags'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"tags"} 1 0 1
172 }
173  
174 # Method allowing to refetch the active values (key, val) without
175 # rolling forward
176 sub fetch_current {
177 20 ▶ my $self = shift;
Subroutine fetch_current: called CC=1 SCAR=0.0
Pod undocumented
178 20 my $level = $self->{'level'};
179 20 my $page = $self->{'pages'}[$level];
180 20 my $row = $self->{'rows'}[$level];
181 20 my ($key, $val, $left) = $page->get_key_val_left($row);
182 20 return ($key, $val);
183 }
184  
185 # Rewind the index to start
186 # the easiest way to do this is to cancel the 'level' -- this way we
187 # do not know where we are and we have to start anew
188 sub prepare_select {
189 77 ▶ my $self = shift;
Subroutine prepare_select: called CC=1 SCAR=0.0
Pod undocumented
190 77 delete $self->{'level'};
191 77 delete $self->{'pages'};
192 77 delete $self->{'rows'};
193 77 1;
194 }
195  
196 # Position index to a value (or behind it, if nothing found), so that
197 # next fetch fetches the correct value
198 sub prepare_select_eq {
199 59 ▶ my ($self, $eq, $recno) = @_;
Subroutine prepare_select_eq: called CC=16 SCAR=28.1
Pod undocumented
200 59 $self->prepare_select(); # start from scratch
201  
202 ### { local $^W = 0; print STDERR "Will look for $eq $recno\n"; }
203  
204 59 my $left = $self->{'start_page'};
205 59 my $level = 0;
206 59 my $parent = undef;
207
208 # we'll need to know if we want numeric or string compares
209 59 ▶ my $numdate = ($self->{'key_type'} ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'key_type'} ? : 2 57 59
210  
211 59 while (1) {
212 95 my $page = $self->get_record($left); # get page
213 95 ▶ if (not defined $page) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $page) 0 95 95
214 0 $self->Error("Index corrupt: $self: no page $left for level $level\n");
215 0 return;
216 }
217 95 my $row = 0;
218 95 my ($key, $val);
219 95 my $empty = 1;
220 95 ▶ while (($key, $val, my $newleft) = $page->get_key_val_left($row)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $page->get_key_val_left($row)) 584 10 594
221 ### { local $^W = 0; print "Got: $key, $val, $newleft ($numdate)\n"; }
222  
223 584 $empty = 0; # There is at least 1 key
224 584 $left = $newleft;
225 # Joe Campbell says:
226 # Compound char keys have two parts preceded by white space
227 # get rid of the white space so that I can do a matching....
228 # and suggests
229 # $key =~ s/^\s*//g;
230  
231  
232 # finish if we are at the end of the page or
233 # behind the correct value
234 584 ▶ if (not defined $key)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $key) 0 584 584
235 0 { last; }
236 584 ▶ if ($numdate == 1 ? $key >= $eq : $key ge $eq)
Branch: 2 branches
BranchTrueFalseTotal
if ($numdate == 1 ? $key >= $eq : $key ge $eq) 85 499 584
$numdate == 1 ? : 52 532 584
237 85 { last; }
238 499 $row++;
239 }
240
241 # we know where we are positioned on the page now
242 95 $self->{'pages'}[$level] = $page;
243 95 $self->{'rows'}[$level] = $row;
244  
245 # if there is no lower level
246 95 ▶ if ($empty or not defined $left) {
Branch: 1 branch
BranchTrueFalseTotal
if ($empty or not defined $left) 59 36 95
Condition66%2/3
$empty or not defined $left
l!l&&r!l&&!r
05936
MC/DC50%1/2
$empty or not defined $left
$empty not defined $left
Truth table66%2/3
A or not(B)
A= $emptyB= not defined $left
ABresult
1X1
011
000
247 59 ▶ $self->{'rows'}[$level] = ( $row ? $row - 1: undef);
Branch: 1 branch
BranchTrueFalseTotal
$row ? : 46 13 59
248 59 $self->{'level'} = $level;
249 59 last;
250 }
251 36 ▶ $page->{'parent'} = $parent->{'num'} if defined $parent;
Branch: 1 branch
BranchTrueFalseTotal
if defined $parent 2 34 36
252 36 $parent = $page;
253 36 $level++;
254 }
255 59 ▶ if (defined $recno) { # exact match requested
Branch: 1 branch
BranchTrueFalseTotal
if (defined $recno) 10 49 59
256 # get current values
257 10 my ($key, $val) = $self->fetch_current;
258 10 ▶ while (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 20 0 20
259 20 ▶ last if ($numdate ? $key > $eq : $key gt $eq);
Branch: 2 branches
BranchTrueFalseTotal
if $numdate ? $key > $eq : $key gt $eq 10 10 20
$numdate ? : 0 20 20
260  
261 # if we're here, we still have exact match
262 10 ▶ last if $val == $recno;
Branch: 1 branch
BranchTrueFalseTotal
if $val == $recno 0 10 10
263  
264 # move forward
265 10 ($key, $val) = $self->fetch;
266 }
267 }
268 59 1;
269 }
270  
271 # Get (key, dbf record number, lower page index) from the index page
272 sub get_key_val_left {
273 2301 ▶ my ($self, $num) = @_;
Subroutine get_key_val_left: called CC=3 SCAR=11.0
Pod undocumented
274 {
275 2301 local $^W = 0;
276 2301 my $printkey = $self->{'keys'}[$num];
277 2301 $printkey =~ s/\s+$//;
278 2301 $printkey =~ s/\000/\\0/g;
279 2301 ▶ print "Getkeyval: Page $self->{'num'}, row $num: $printkey, $self->{'values'}[$num], $self->{'lefts'}[$num]\n"
Branch: 1 branch
BranchTrueFalseTotal
if $XBase::Index::DEBUG > 5 0 2301 2301
280 if $DEBUG > 5;
281 return ($self->{'keys'}[$num], $self->{'values'}[$num], $self->{'lefts'}[$num])
282 2301 ▶ if $num <= $#{$self->{'keys'}};
Branch: 1 branch
BranchTrueFalseTotal
if $num <= $#{$$self{"keys"};} 2079 222 2301
283 }
284 222 return;
285 }
286  
287 sub num_keys {
288 0 ▶ $#{shift->{'keys'}};
Subroutine num_keys: not called CC=1 SCAR=6.9
Pod undocumented
289 }
290  
291 sub delete {
292 5 ▶ my ($self, $key, $value) = @_;
Subroutine delete: called CC=7 SCAR=24.0
Pod undocumented
293 5 ▶ print "XBase::Index::delete($key, $value) called ($self->{'tag'} -> $self->{'key_string'}/$self->{'for_string'})\n" if $XBase::Index::VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $XBase::Index::VERBOSE 0 5 5
294 5 ▶ $self->prepare_select_eq($key, $value) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->prepare_select_eq($key, $value) or return 0 5 5
295 5 my ($foundkey, $foundvalue) = $self->fetch_current;
296  
297 5 ▶ if (defined $foundvalue
Branch: 1 branch
BranchTrueFalseTotal
if (defined $foundvalue and $foundkey eq $key and $foundvalue == $value) 0 5 5
Condition33%2/6
defined $foundvalue and $foundkey eq $key and $foundvalue == $value
!ll&&!rl&&r
500
defined $foundvalue and $foundkey eq $key
!ll&&!rl&&r
050
MC/DC0%0/3
defined $foundvalue and $foundkey eq $key and $foundvalue == $value
defined $foundvalue $foundkey eq $key $foundvalue == $value
Truth table25%1/4
A and B and C
A= defined $foundvalueB= $foundkey eq $keyC= $foundvalue == $value
ABCresult
0XX0
10X0
1100
1111
298 and $foundkey eq $key and $foundvalue == $value) {
299 0 $self->delete_current;
300 0 return 1;
301 }
302 5 ▶ print "$key/$value is not in the index (wanted to delete)\n" if $XBase::Index::VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $XBase::Index::VERBOSE 0 5 5
303 5 undef;
304 }
305 sub insert {
306 5 ▶ my ($self, $key, $value) = @_;
Subroutine insert: called CC=6 SCAR=22.3
Pod undocumented
307 5 ▶ print "XBase::Index::insert($key, $value) called\n" if $XBase::Index::VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $XBase::Index::VERBOSE 0 5 5
308  
309 5 ▶ $self->prepare_select_eq($key, $value) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->prepare_select_eq($key, $value) or return 0 5 5
310 5 my ($foundkey, $foundvalue) = $self->fetch_current;
311  
312 5 ▶ if (defined $foundvalue
Branch: 1 branch
BranchTrueFalseTotal
if (defined $foundvalue and $foundkey eq $key and $foundvalue == $value) 0 5 5
Condition33%2/6
defined $foundvalue and $foundkey eq $key and $foundvalue == $value
!ll&&!rl&&r
500
defined $foundvalue and $foundkey eq $key
!ll&&!rl&&r
050
MC/DC0%0/3
defined $foundvalue and $foundkey eq $key and $foundvalue == $value
defined $foundvalue $foundkey eq $key $foundvalue == $value
Truth table25%1/4
A and B and C
A= defined $foundvalueB= $foundkey eq $keyC= $foundvalue == $value
ABCresult
0XX0
10X0
1100
1111
313 and $foundkey eq $key and $foundvalue == $value) {
314 0 print STDERR "Already found, strange.\n";
315 0 return;
316 }
317  
318 5 $self->insert_before_current($key, $value);
319 }
320  
321 sub delete_current {
322 0 ▶ my $self = shift;
Subroutine delete_current: not called CC=4 SCAR=30.0
Pod undocumented
323 0 ▶ print "Delete_current called\n" if $XBase::Index::VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $XBase::Index::VERBOSE 0 0 0
324 0 my $level = $self->{'level'};
325 0 my $page = $self->{'pages'}[$level];
326 0 my $row = $self->{'rows'}[$level];
327  
328 0 splice @{$page->{'values'}}, $row, 1;
329 0 splice @{$page->{'keys'}}, $row, 1;
330 0 splice @{$page->{'lefts'}}, $row, 1;
331  
332 0 $self->{'rows'}[$level]--;
333 0 ▶ if ($self->{'rows'}[$level] < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"rows"}[$level] < 0) 0 0 0
334 0 $self->{'rows'}[$level] = undef;
335 }
336  
337 0 $page->write_with_context;
338  
339 0 delete $self->{'pages_cache'};
340  
341 0 ▶ print STDERR "Delete_current returning\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XBase::Index::DEBUG 0 0 0
342 }
343  
344 sub insert_before_current {
345 5 ▶ my ($self, $key, $value) = @_;
Subroutine insert_before_current: called CC=5 SCAR=16.4
Pod undocumented
346 5 ▶ print "Insert_current called ($key $value)\n" if $XBase::Index::VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $XBase::Index::VERBOSE 0 5 5
347 5 my $level = $self->{'level'};
348 5 my $page = $self->{'pages'}[$level];
349 5 my $row = $self->{'rows'}[$level];
350 5 ▶ $row = 0 unless defined $row;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row 0 5 5
351  
352 # update keys and values and then call save
353 5 splice @{$page->{'keys'}}, $row, 0, $key;
354 5 splice @{$page->{'values'}}, $row, 0, $value;
355 5 ▶ splice @{$page->{'lefts'}}, $row, 0, undef if defined $page->{'lefts'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $page->{"lefts"} 5 0 5
356  
357 5 $page->write_with_context;
358  
359 5 delete $self->{'pages_cache'};
360  
361 5 ▶ print STDERR "Insert_current returning\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XBase::Index::DEBUG 0 5 5
362 }
363  
364 # #############
365 # dBase III NDX
366  
367 package XBase::ndx;
368 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
369 6 ▶use vars qw( @ISA $DEBUG );
Subroutine BEGIN: called CC=1 SCAR=0.0
370 6 @ISA = qw( XBase::Base XBase::Index );
371  
372 6 *DEBUG = \$XBase::Index::DEBUG;
373  
374 sub read_header {
375 3 ▶ my $self = shift;
Subroutine read_header: called CC=2 SCAR=7.1
376 3 my %opts = @_;
377 3 my $header;
378 3 $self->{'dbf'} = $opts{'dbf'};
379 $self->{'fh'}->read($header, 512) == 512 or do
380 3 ▶ { __PACKAGE__->Error("Error reading header of $self->{'filename'}: $!\n"); return; };
Branch: 1 branch
BranchTrueFalseTotal
$self->{"fh"}->read($header, 512) == 512 or do { __PACKAGE__->Error(qq[Error reading header of $self->{"filename"}: $!\n]); return } 0 3 3
381 3 @{$self}{ qw( start_page total_pages key_length keys_per_page
382 key_type key_record_length unique key_string ) }
383 = unpack 'VV @12vvvv @23c a*', $header;
384
385 3 $self->{'key_string'} =~ s/[\000 ].*$//s;
386 3 $self->{'record_len'} = 512;
387 3 $self->{'header_len'} = 0;
388  
389 3 $self;
390 }
391  
392 sub last_record {
393 30 ▶ shift->{'total_pages'};
Subroutine last_record: called CC=1 SCAR=0.0
394 }
395  
396 package XBase::ndx::Page;
397 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
398 6 ▶use vars qw( @ISA $DEBUG );
Subroutine BEGIN: called CC=1 SCAR=0.0
399 6 @ISA = qw( XBase::ndx );
400  
401 6 *DEBUG = \$XBase::Index::DEBUG;
402  
403 # Constructor for the ndx page
404 sub new {
405 15 ▶ my ($indexfile, $num) = @_;
Subroutine new: called CC=15 SCAR=27.8
406 15 my $parent;
407 # we can be called from parent page
408 15 ▶ if ((ref $indexfile) =~ /::Page$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($indexfile) =~ /::Page$/) 0 15 15
409 0 $parent = $indexfile;
410 0 $indexfile = $parent->{'indexfile'};
411 }
412
413 15 ▶ my $data = $indexfile->read_record($num) or return; # get 512 bytes
Branch: 1 branch
BranchTrueFalseTotal
my $data = $indexfile->read_record($num) or return 0 15 15
414 15 my $noentries = unpack 'V', $data; # num of entries
415
416 15 my $keylength = $indexfile->{'key_length'};
417 15 my $keyreclength = $indexfile->{'key_record_length'}; # length
418  
419 15 ▶ print "page $num, noentries $noentries, keylength $keylength\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XBase::ndx::Page::DEBUG 0 15 15
420 15 my $numdate = $indexfile->{'key_type'}; # numeric or string?
421
422 15 my $offset = 4;
423 15 my $i = 0;
424 15 my ($keys, $values, $lefts) = ([], [], []); # three arrays
425  
426 # walk the page
427 15 ▶ while ($i < $noentries) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $noentries) 147 15 162
428 # get the values for entry
429 147 my ($left, $recno, $key)
430 = unpack 'VVa*', substr($data, $offset, $keylength + 8);
431 147 ▶ if ($numdate) { # some decoding for numbers
Branch: 1 branch
BranchTrueFalseTotal
if ($numdate) 110 37 147
432 110 ▶ $key = reverse $key if $XBase::Index::BIGEND;
Branch: 1 branch
BranchTrueFalseTotal
if $XBase::Index::BIGEND 0 110 110
433 110 $key = unpack 'd', $key;
434 }
435 147 ▶ print "$i: \@$offset VVa$keylength -> ($left, $recno, $key)\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $XBase::ndx::Page::DEBUG > 1 0 147 147
436 147 push @$keys, $key;
437 147 ▶ push @$values, ($recno ? $recno : undef);
Branch: 1 branch
BranchTrueFalseTotal
$recno ? : 109 38 147
438 147 ▶ $left = ($left ? $left : undef);
Branch: 1 branch
BranchTrueFalseTotal
$left ? : 39 108 147
439 147 push @$lefts, $left;
440
441 147 ▶ if ($i == 0 and defined $left)
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0 and defined $left) 5 142 147
Condition100%3/3
$i == 0 and defined $left
!ll&&!rl&&r
132105
MC/DC100%2/2
$i == 0 and defined $left
$i == 0 defined $left
Truth table100%3/3
A and B
A= $i == 0B= defined $left
ABresult
0X0
100
111
442 5 { $noentries++; } # fixup for nonleaf page
443 ### shouldn't this be for last page only?
444 }
445 continue {
446 147 $i++;
447 147 $offset += $keyreclength;
448 }
449  
450 15 my $self = bless { 'keys' => $keys, 'values' => $values,
451 'num' => $num, 'keylength' => $keylength,
452 'lefts' => $lefts, 'indexfile' => $indexfile }, __PACKAGE__;
453
454 15 ▶ if ($num == $indexfile->{'start_page'}
Branch: 1 branch
BranchTrueFalseTotal
if ($num == $indexfile->{"start_page"} or defined $parent->{"last_key_is_just_overflow"} and $parent->{"lefts"}[$#{$$parent{"lefts"};}] == $num) 3 12 15
Condition50%3/6
$num == $indexfile->{"start_page"} or defined $parent->{"last_key_is_just_overflow"} and $parent->{"lefts"}[$#{$$parent{"lefts"};}] == $num
l!l&&r!l&&!r
3012
defined $parent->{"last_key_is_just_overflow"} and $parent->{"lefts"}[$#{$$parent{"lefts"};}] == $num
!ll&&!rl&&r
1200
MC/DC0%0/3
$num == $indexfile->{"start_page"} or defined $parent->{"last_key_is_just_overflow"} and $parent->{"lefts"}[$#{$$parent{"lefts"};}] == $num
$num == $indexfile->{"start_page"} defined $parent->{"last_key_is_just_overflow"} $parent->{"lefts"}[$#{$$parent{"lefts"};}] == $num
Truth table25%1/4
A or B and C
A= $num == $indexfile->{"start_page"}B= defined $parent->{"last_key_is_just_overflow"}C= $parent->{"lefts"}[$#{$$parent{"lefts"};}] == $num
ABCresult
1XX1
0111
00X0
0100
455 or (defined
456 $parent->{'last_key_is_just_overflow'} and
457 0 $parent->{'lefts'}[$#{$parent->{'lefts'}}] == $num)) {
458 3 $self->{'last_key_is_just_overflow'} = 1;
459 }
460  
461 15 $self;
462 }
463  
464 # ###########
465 # Clipper NTX
466  
467 package XBase::ntx;
468 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
469 6 ▶use vars qw( @ISA $DEBUG );
Subroutine BEGIN: called CC=1 SCAR=0.0
470 6 @ISA = qw( XBase::Base XBase::Index );
471  
472 sub read_header {
473 1 ▶ my $self = shift;
Subroutine read_header: called CC=8 SCAR=24.4
474 1 my %opts = @_;
475 1 my $header;
476 1 $self->{'dbf'} = $opts{'dbf'};
477 $self->{'fh'}->read($header, 1024) == 1024 or do
478 1 ▶ { __PACKAGE__->Error("Error reading header of $self->{'filename'}: $!\n"); return; };
Branch: 1 branch
BranchTrueFalseTotal
$self->{"fh"}->read($header, 1024) == 1024 or do { __PACKAGE__->Error(qq[Error reading header of $self->{"filename"}: $!\n]); return } 0 1 1
479
480 1 @{$self}{ qw( signature compiler_version start_offset first_unused
481 key_record_length key_length decimals max_item
482 half_page key_string unique ) }
483 = unpack 'vvVVvvvvvA256c', $header;
484  
485 1 my $key_string = uc $self->{'key_string'};
486 1 $key_string =~ s/^.*?->//;
487 1 $self->{'key_string'} = $key_string;
488  
489 1 ▶ if ($self->{'signature'} != 3 and $self->{'signature'} != 6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"signature"} != 3 and $self->{"signature"} != 6) 0 1 1
Condition33%1/3
$self->{"signature"} != 3 and $self->{"signature"} != 6
!ll&&!rl&&r
010
MC/DC0%0/2
$self->{"signature"} != 3 and $self->{"signature"} != 6
$self->{"signature"} != 3 $self->{"signature"} != 6
Truth table33%1/3
A and B
A= $self->{"signature"} != 3B= $self->{"signature"} != 6
ABresult
0X0
100
111
490 0 __PACKAGE__->Error("$self: bad signature value `$self->{'signature'}' found\n");
491 0 return;
492 }
493 1 $self->{'key_string'} =~ s/[\000 ].*$//s;
494 1 $self->{'record_len'} = 1024;
495 1 $self->{'header_len'} = 0;
496
497 1 $self->{'start_page'} = int($self->{'start_offset'} / $self->{'record_len'});
498 1 my $field_type;
499 1 ▶ if (defined $opts{'type'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'type'}) { } 0 1 1
elsif (defined $self->{'dbf'}) { } 1 0 1
500 0 $field_type = $opts{'type'};
501 } elsif (defined $self->{'dbf'}) {
502 1 $field_type = $self->{'dbf'}->field_type($key_string);
503 1 ▶ if (not defined $field_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $field_type) 0 1 1
504 0 __PACKAGE__->Error("Couldn't find key string `$key_string' in dbf file, can't determine field type\n");
505 0 return;
506 }
507 } else {
508 0 __PACKAGE__->Error("Index type (char/numeric) unknown for $self\n");
509 0 return;
510 }
511 1 ▶ $self->{'key_type'} = ($field_type =~ /^[NDIF]$/ ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$field_type =~ /^[NDIF]$/ ? : 0 1 1
512  
513 1 $self;
514 }
515 sub last_record {
516 9 ▶ -1;
Subroutine last_record: called CC=1 SCAR=0.0
517 }
518  
519  
520 package XBase::ntx::Page;
521 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
522 6 ▶use vars qw( @ISA $DEBUG );
Subroutine BEGIN: called CC=1 SCAR=0.0
523 6 @ISA = qw( XBase::ntx );
524  
525 6 *DEBUG = \$XBase::Index::DEBUG;
526  
527 # Constructor for the ntx page
528 sub new {
529 9 ▶ my ($indexfile, $num) = @_;
Subroutine new: called CC=12 SCAR=26.2
530 9 my $parent;
531 # we could be called from parent page
532 9 ▶ if ((ref $indexfile) =~ /::Page$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($indexfile) =~ /::Page$/) 0 9 9
533 0 $parent = $indexfile;
534 0 $indexfile = $parent->{'indexfile'};
535 }
536 9 ▶ my $data = $indexfile->read_record($num) or return; # get data
Branch: 1 branch
BranchTrueFalseTotal
my $data = $indexfile->read_record($num) or return 0 9 9
537 9 my $maxnumitem = $indexfile->{'max_item'} + 1; # limit from header
538 9 my $keylength = $indexfile->{'key_length'};
539 9 my $record_len = $indexfile->{'record_len'}; # length
540  
541 9 my $numdate = $indexfile->{'key_type'}; # numeric or string?
542  
543 9 my ($noentries, @pointers) = unpack "vv$maxnumitem", $data;
544 # get pointers where the entries are
545
546 9 ▶ print "page $num, noentries $noentries, keylength $keylength; pointers @pointers\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XBase::ntx::Page::DEBUG 0 9 9
547
548 9 my ($keys, $values, $lefts) = ([], [], []);
549 # walk the pointers
550 9 ▶ for (my $i = 0; $i < $noentries; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $noentries) 69 9 78
551 69 my $offset = $pointers[$i];
552 69 my ($left, $recno, $key)
553 = unpack 'VVa*', substr($data, $offset, $keylength + 8);
554  
555 69 ▶ if ($numdate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($numdate) 0 69 69
556 ### if looks like with ntx the numbers are
557 ### stored as ASCII strings or something
558 ### To Be Done
559 0 ▶ if ($key =~ tr!,+*)('&%$#"!0123456789!) { $key = '-' . $key; }
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ tr/"-,/99876543210/) 0 0 0
560 0 $key += 0;
561 }
562  
563 69 ▶ print "$i: \@$offset VVa$keylength -> ($left, $recno, $key)\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $XBase::ntx::Page::DEBUG > 1 0 69 69
564 69 push @$keys, $key;
565 69 ▶ push @$values, ($recno ? $recno : undef);
Branch: 1 branch
BranchTrueFalseTotal
$recno ? : 68 1 69
566 69 ▶ $left = ($left ? ($left / $record_len) : undef);
Branch: 1 branch
BranchTrueFalseTotal
$left ? : 8 61 69
567 69 push @$lefts, $left;
568  
569 ### if ($i == 0 and defined $left and (not defined $parent or $num == $parent->{'lefts'}[-1]))
570 69 ▶ if ($i == 0 and defined $left)
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0 and defined $left) 1 68 69
Condition100%3/3
$i == 0 and defined $left
!ll&&!rl&&r
6081
MC/DC100%2/2
$i == 0 and defined $left
$i == 0 defined $left
Truth table100%3/3
A and B
A= $i == 0B= defined $left
ABresult
0X0
100
111
571 1 { $noentries++; }
572 ### shouldn't this be for last page only?
573 }
574  
575 9 my $self = bless { 'num' => $num, 'indexfile' => $indexfile,
576 'keys' => $keys, 'values' => $values, 'lefts' => $lefts, },
577 __PACKAGE__;
578 9 $self;
579 }
580  
581 # ###########
582 # FoxBase IDX
583  
584 package XBase::idx;
585 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
586 6 ▶use vars qw( @ISA $DEBUG );
Subroutine BEGIN: called CC=1 SCAR=0.0
587 6 @ISA = qw( XBase::Base XBase::Index );
588  
589 6 *DEBUG = \$XBase::Index::DEBUG;
590  
591 sub read_header {
592 2 ▶ my $self = shift;
Subroutine read_header: called CC=3 SCAR=11.1
593 2 my %opts = @_;
594 2 my $header;
595 2 $self->{'dbf'} = $opts{'dbf'};
596 $self->{'fh'}->read($header, 512) == 512 or do
597 2 ▶ { __PACKAGE__->Error("Error reading header of $self->{'filename'}: $!\n"); return; };
Branch: 1 branch
BranchTrueFalseTotal
$self->{"fh"}->read($header, 512) == 512 or do { __PACKAGE__->Error(qq[Error reading header of $self->{"filename"}: $!\n]); return } 0 2 2
598 2 @{$self}{ qw( start_page start_free_list total_pages
599 key_length index_options index_signature
600 key_string for_expression
601 ) }
602 = unpack 'VVVv CC a220 a276', $header;
603
604 2 $self->{'key_record_length'} = $self->{'key_length'} + 4;
605 2 $self->{'key_string'} =~ s/[\000 ].*$//s;
606 2 $self->{'record_len'} = 512;
607 2 $self->{'start_page'} /= $self->{'record_len'};
608 2 $self->{'start_free_list'} /= $self->{'record_len'};
609 2 $self->{'header_len'} = 0;
610  
611 2 ▶ if ($opts{'type'} eq 'N') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"type"} eq "N") 1 1 2
612 1 $self->{'key_type'} = 1;
613 }
614  
615 2 $self;
616 }
617  
618 sub last_record {
619 8 ▶ shift->{'total_pages'};
Subroutine last_record: called CC=1 SCAR=0.0
620 }
621  
622 sub create {
623 2 ▶ my ($class, $table, $filename, $column) = @_;
Subroutine create: called CC=17 SCAR=28.4
624 2 my $type = $table->field_type($column);
625 2 ▶ if (not defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 0 2 2
626 0 die "XBase::idx: could determine index type for `$column'\n";
627 }
628 2 my $numdate = 0;
629 2 ▶ $numdate = 1 if $type eq 'N' or $type eq 'D';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "N" or $type eq "D" 1 1 2
Condition66%2/3
$type eq "N" or $type eq "D"
l!l&&r!l&&!r
101
MC/DC50%1/2
$type eq "N" or $type eq "D"
$type eq "N" $type eq "D"
Truth table66%2/3
A or B
A= $type eq "N"B= $type eq "D"
ABresult
1X1
011
000
630  
631 2 my $self = bless {}, $class;
632 2 ▶ $self->create_file($filename) or die "Error creating `$filename'\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->create_file($filename) or die "Error creating `${filename}'\n" 0 2 2
633 2 $self->write_to(0, "\000" x 512);
634 2 my $key_length = $table->field_length($column);
635 2 ▶ $key_length = 8 if $numdate;
Branch: 1 branch
BranchTrueFalseTotal
if $numdate 1 1 2
636  
637 2 my $count = int((512 - 12) / ($key_length + 4));
638 ### warn "Key length $key_length, per page $count.\n";
639  
640 2 my $encode_function;
641 2 ▶ if ($numdate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($numdate) { } 1 1 2
642 $encode_function = sub {
643 8 ▶ my $key = pack 'd', shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
644 8 ▶ $key = reverse $key unless $XBase::Index::BIGEND;
Branch: 1 branch
BranchTrueFalseTotal
unless $XBase::Index::BIGEND 8 0 8
645 8 ▶ if ((substr($key, 0, 1) & "\200") eq "\200") {
Branch: 1 branch
BranchTrueFalseTotal
if ((substr($key, 0, 1) & "\200") eq "\200") { } 2 6 8
646 2 $key ^= "\377\377\377\377\377\377\377\377";
647 } else {
648 6 $key ^= "\200";
649 }
650 8 return $key;
651 1 };
652 } else {
653 $encode_function = sub {
654 8 ▶ return sprintf "%-${key_length}s", shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
655 1 };
656 }
657  
658 2 my @data;
659 2 my $last_record = $table->last_record;
660 2 ▶ for (my $i = 0; $i <= $last_record; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $last_record) 16 2 18
661 16 my ($deleted, $data) = $table->get_record($i, $column);
662 16 push @data, [ $encode_function->($data), $i + 1 ];
663 }
664 2 @data = sort { $a->[0] cmp $b->[0] } @data;
665  
666 2 $self->{'header_len'} = 0; # it is 512 really, but we
667 # count from 1, not from 0
668 2 $self->{'record_len'} = 512;
669  
670 2 my $pageno = 1;
671 2 my $level = 1;
672 2 my @newdata;
673 2 ▶ while ($level == 1 or @data > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level == 1 or @data > 1) 3 2 5
Condition100%3/3
$level == 1 or @data > 1
l!l&&r!l&&!r
212
MC/DC100%2/2
$level == 1 or @data > 1
$level == 1 @data > 1
Truth table100%3/3
A or B
A= $level == 1B= @data > 1
ABresult
1X1
011
000
674 3 ▶ last if $pageno > 5;
Branch: 1 branch
BranchTrueFalseTotal
if $pageno > 5 0 3 3
675 3 my $attributes = 0;
676 3 ▶ $attributes = 2 if $level == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $level == 1 2 1 3
677 3 ▶ if (scalar(@data) < $count) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @data < $count) 2 1 3
678 # we have less than one page, so it's root.
679 2 $attributes++;
680 }
681  
682 3 my $left_page = 0xFFFFFFFF;
683 3 my $current_count = 0;
684 3 my $out = '';
685 3 @newdata = ();
686 3 ▶ for (my $i = 0; $i < @data; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @data) 18 3 21
687 18 my $key = $data[$i][0];
688 ### print STDERR "Page $pageno: $i: @{$data[$i]}\n";
689 18 $out .= pack "a$key_length N", $key, $data[$i][1];
690 18 $current_count++;
691  
692 18 ▶ if ($current_count == $count or $i == $#data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_count == $count or $i == $#data) 4 14 18
Condition100%3/3
$current_count == $count or $i == $#data
l!l&&r!l&&!r
2214
MC/DC100%2/2
$current_count == $count or $i == $#data
$current_count == $count $i == $#data
Truth table100%3/3
A or B
A= $current_count == $countB= $i == $#data
ABresult
1X1
011
000
693 ### print STDERR "Dumping $pageno.\n";
694 # time to close this page and move on
695 4 my $right_page = 0xFFFFFFFF;
696 4 ▶ if ($i < $#data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#data) 1 3 4
697 1 $right_page = $pageno + 1;
698 }
699 4 $self->write_record($pageno,
700 pack 'a512',
701 pack('vvVV', $attributes, $current_count,
702 $left_page, $right_page)
703 . $out);
704 4 push @newdata, [$data[$i][0], $pageno * 512];
705 4 $left_page = $pageno;
706 4 $current_count = 0;
707 4 $pageno++;
708 4 $out = '';
709 }
710 }
711  
712 3 @data = @newdata;
713 3 $level++;
714 }
715  
716 2 my $header = pack 'VVVv CC a220 a276',
717 ($pageno - 1) * 512, 0xFFFFFFFF, $pageno * 512,
718 $key_length, 0, 0, $column, '';
719 2 $self->write_to(0, $header);
720 2 $self->close;
721  
722 2 return new XBase::Index($filename, 'type' => $type);
723 }
724  
725 package XBase::idx::Page;
726 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
727 6 ▶use vars qw( @ISA $DEBUG );
Subroutine BEGIN: called CC=1 SCAR=0.0
728 6 @ISA = qw( XBase::idx );
729  
730 6 *DEBUG = \$XBase::Index::DEBUG;
731  
732 ### $DEBUG = 1;
733 # Constructor for the idx page
734 sub new {
735 4 ▶ local $^W = 0;
Subroutine new: called CC=15 SCAR=27.3
736 4 my ($indexfile, $num) = @_;
737 4 my $parent;
738 # we can be called from parent page
739 4 ▶ if ((ref $indexfile) =~ /::Page$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($indexfile) =~ /::Page$/) 0 4 4
740 0 $parent = $indexfile;
741 0 $indexfile = $parent->{'indexfile'};
742 }
743 4 ▶ my $data = $indexfile->read_record($num) or return; # get 512 bytes
Branch: 1 branch
BranchTrueFalseTotal
my $data = $indexfile->read_record($num) or return 0 4 4
744 4 my ($attributes, $noentries, $left_brother, $right_brother)
745 = unpack 'vvVV', $data; # parse header of the page
746 4 my $keylength = $indexfile->{'key_length'};
747 4 my $keyreclength = $indexfile->{'key_record_length'}; # length
748  
749 4 ▶ print "page $num, noentries $noentries, keylength $keylength\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XBase::idx::Page::DEBUG 0 4 4
750 4 my $numdate = $indexfile->{'key_type'}; # numeric or string?
751
752 4 my $offset = 12;
753 4 my $i = 0;
754 4 my ($keys, $values, $lefts) = ([], [], []); # three arrays
755  
756 # walk the page
757 4 ▶ while ($i < $noentries) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $noentries) 19 4 23
758 # get the values for entry
759 19 my ($key, $recno) = unpack "\@$offset a$keylength N", $data;
760 19 my $left;
761 19 ▶ unless ($attributes & 2) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($attributes & 2) 3 16 19
762 3 $left = $recno / 512;
763 3 $recno = undef;
764 }
765 19 ▶ print "$i: \@$offset a$keylength N -> ($left, $recno, $key)\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $XBase::idx::Page::DEBUG > 1 0 19 19
766 ### use Data::Dumper; print Dumper $indexfile;
767 # some decoding for numbers
768 19 ▶ if ($numdate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($numdate) 8 11 19
769 8 ▶ if ((substr($key, 0, 1) & "\200") ne "\200") {
Branch: 1 branch
BranchTrueFalseTotal
if ((substr($key, 0, 1) & "\200") ne "\200") { } 2 6 8
770 2 $key ^= "\377\377\377\377\377\377\377\377";
771 } else {
772 6 $key ^= "\200";
773 }
774 8 ▶ if (not $XBase::Index::BIGEND) { $key = reverse $key; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($XBase::Index::BIGEND) 8 0 8
775 8 $key = unpack 'd', $key;
776 }
777 19 ▶ print "$i: \@$offset a$keylength N -> ($left, $recno, $key)\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $XBase::idx::Page::DEBUG > 1 0 19 19
778 19 push @$keys, $key;
779 19 ▶ push @$values, ($recno ? $recno : undef);
Branch: 1 branch
BranchTrueFalseTotal
$recno ? : 16 3 19
780 19 ▶ $left = ($left ? $left : undef);
Branch: 1 branch
BranchTrueFalseTotal
$left ? : 2 17 19
781 19 push @$lefts, $left;
782
783 19 ▶ if ($i == 0 and defined $left)
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0 and defined $left) 1 18 19
Condition100%3/3
$i == 0 and defined $left
!ll&&!rl&&r
1531
MC/DC100%2/2
$i == 0 and defined $left
$i == 0 defined $left
Truth table100%3/3
A and B
A= $i == 0B= defined $left
ABresult
0X0
100
111
784 1 { $noentries++; } # fixup for nonleaf page
785 ### shouldn't this be for last page only?
786 }
787 continue {
788 19 $i++;
789 19 $offset += $keyreclength;
790 }
791  
792 4 my $self = bless { 'keys' => $keys, 'values' => $values,
793 'num' => $num, 'keylength' => $keylength,
794 'lefts' => $lefts, 'indexfile' => $indexfile,
795 'attributes' => $attributes,
796 'left_brother' => $left_brother,
797 'right_brother' => $right_brother }, __PACKAGE__;
798 4 $self;
799 }
800  
801 # ############
802 # dBase IV MDX
803  
804 package XBase::mdx;
805 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
806 6 ▶use vars qw( @ISA $DEBUG );
Subroutine BEGIN: called CC=1 SCAR=0.0
807 6 @ISA = qw( XBase::Base XBase::Index );
808  
809 sub read_header {
810 0 ▶ my $self = shift;
Subroutine read_header: not called CC=10 SCAR=47.0
811 0 my %opts = @_;
812 0 my $expr_name = $opts{'tag'};
813  
814 0 my $header;
815 0 $self->{'dbf'} = $opts{'dbf'};
816 $self->{'fh'}->read($header, 544) == 544 or do
817 0 ▶ { __PACKAGE__->Error("Error reading header of $self->{'filename'}: $!\n"); return; };
Branch: 1 branch
BranchTrueFalseTotal
$self->{"fh"}->read($header, 544) == 544 or do { __PACKAGE__->Error(qq[Error reading header of $self->{"filename"}: $!\n]); return } 0 0 0
818  
819 0 @{$self}{ qw( version created dbf_filename block_size
820 block_size_adder production noentries tag_length res
821 tags_used res nopages first_free noavail last_update ) }
822 = unpack 'Ca3A16vvccccvvVVVa3', $header;
823
824 0 $self->{'record_len'} = 512;
825 0 $self->{'header_len'} = 0;
826  
827 0 for my $i (1 .. $self->{'tags_used'}) {
828 0 my $len = $self->{'tag_length'};
829
830 $self->seek_to(544 + ($i - 1) * $len) or do
831 0 ▶ { __PACKAGE__->Error($self->errstr); return; };
Branch: 1 branch
BranchTrueFalseTotal
$self->seek_to(544 + ($i - 1) * $len) or do { __PACKAGE__->Error($self->errstr); return } 0 0 0
832  
833 $self->{'fh'}->read($header, $len) == $len or do
834 0 ▶ { __PACKAGE__->Error("Error reading tag header $i in $self->{'filename'}: $!\n"); return; };
Branch: 1 branch
BranchTrueFalseTotal
$self->{"fh"}->read($header, $len) == $len or do { __PACKAGE__->Error(qq[Error reading tag header $i in $self->{"filename"}: $!\n]); return } 0 0 0
835
836 0 my $tag;
837 0 @{$tag}{ qw( header_page tag_name key_format fwd_low
838 fwd_high backward res key_type ) }
839 = unpack 'VA11ccccca1', $header;
840  
841 0 $self->{'tags'}{$tag->{'tag_name'}} = $tag;
842 0 ▶ $expr_name ||= $tag->{'tag_name'}; # Default to first tag
Condition0%0/3
$expr_name ||= $tag->{"tag_name"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$expr_name ||= $tag->{"tag_name"}
$expr_name $tag->{"tag_name"}
Truth table0%0/3
A ||= B
A= $expr_nameB= $tag->{"tag_name"}
ABresult
1X1
011
000
843  
844 $self->seek_to($tag->{'header_page'} * 512) or do
845 0 ▶ { __PACKAGE__->Error($self->errstr); return; };
Branch: 1 branch
BranchTrueFalseTotal
$self->seek_to($tag->{"header_page"} * 512) or do { __PACKAGE__->Error($self->errstr); return } 0 0 0
846  
847 $self->{'fh'}->read($header, 24) == 24 or do
848 0 ▶ { __PACKAGE__->Error("Error reading tag definition in $self->{'filename'}: $!\n"); return; };
Branch: 1 branch
BranchTrueFalseTotal
$self->{"fh"}->read($header, 24) == 24 or do { __PACKAGE__->Error(qq[Error reading tag definition in $self->{"filename"}: $!\n]); return } 0 0 0
849
850 0 @{$tag}{ qw( start_page file_size key_format_1
851 key_type_1 res key_length max_no_keys_per_page
852 second_key_type key_record_length res unique) }
853 = unpack 'VVca1vvvvva3c', $header;
854 }
855  
856 ### use Data::Dumper; print Dumper $self;
857  
858 0 ▶ if (defined $expr_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expr_name) 0 0 0
859 0 ▶ if (defined $self->{'tags'}{$expr_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'tags'}{$expr_name}) { } 0 0 0
860 0 $self->{'active'} = $self->{'tags'}{$expr_name};
861 0 $self->{'start_page'} = $self->{'active'}{'start_page'};
862 } else {
863 0 __PACKAGE__->Error("No tag $expr_name found in index file $self->{'filename'}.\n"); return;
864 }
865 }
866  
867 0 $self;
868 }
869  
870 sub last_record {
871 0 ▶ -1;
Subroutine last_record: not called CC=1 SCAR=6.9
872 }
873  
874 sub tags {
875 0 ▶ my $self = shift;
Subroutine tags: not called CC=2 SCAR=17.9
876 0 ▶ return sort keys %{$self->{'tags'}} if defined $self->{'tags'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"tags"} 0 0 0
877 }
878  
879 package XBase::mdx::Page;
880 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
881 6 ▶use vars qw( @ISA $DEBUG );
Subroutine BEGIN: called CC=1 SCAR=0.0
882 6 @ISA = qw( XBase::mdx );
883  
884 6 *DEBUG = \$XBase::Index::DEBUG;
885  
886 # Constructor for the mdx page
887 sub new {
888 0 ▶ my ($indexfile, $num) = @_;
Subroutine new: not called CC=8 SCAR=42.8
889  
890 0 my $parent;
891 ### parent page
892 0 ▶ if ((ref $indexfile) =~ /::Page$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($indexfile) =~ /::Page$/) 0 0 0
893 0 $parent = $indexfile;
894 0 $indexfile = $parent->{'indexfile'};
895 }
896 0 ▶ $indexfile->seek_to_record($num) or return;
Branch: 1 branch
BranchTrueFalseTotal
$indexfile->seek_to_record($num) or return 0 0 0
897 0 my $data;
898 0 ▶ $indexfile->{'fh'}->read($data, 1024) == 1024 or return;
Branch: 1 branch
BranchTrueFalseTotal
$indexfile->{"fh"}->read($data, 1024) == 1024 or return 0 0 0
899  
900 0 my $keylength = $indexfile->{'active'}{'key_length'};
901 0 my $keyreclength = $indexfile->{'active'}{'key_record_length'};
902 0 my $offset = 8;
903  
904 0 my ($noentries, $noleaf) = unpack 'VV', $data;
905  
906 0 ▶ print "page $num, noentries $noentries, keylength $keylength; noleaf: $noleaf\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XBase::mdx::Page::DEBUG 0 0 0
907  
908 0 my ($keys, $values, $lefts, $refs) = ([], [], [], []);
909  
910 0 ▶ for (my $i = 0; $i < $noentries; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $noentries) 0 0 0
911 0 my ($left, $key)
912 = unpack "\@${offset}Va${keylength}", $data;
913  
914 0 push @$keys, $key;
915  
916 0 push @$refs, $left;
917  
918 0 $offset += $keyreclength;
919 }
920  
921 0 my $right;
922  
923 0 ▶ $right = unpack "\@${offset}V", $data if $offset <= (1024-4);
Branch: 1 branch
BranchTrueFalseTotal
if $offset <= 1020 0 0 0
924  
925 0 ▶ if ($right) {
Branch: 1 branch
BranchTrueFalseTotal
if ($right) { } 0 0 0
926 # It's a branch page and the next ref is for values > last key
927 0 push @$keys, "";
928 0 push @$refs, $right;
929 0 $lefts = $refs;
930 } else {
931 # It's a leaf page
932 0 $values = $refs;
933 }
934  
935 0 my $self = bless { 'num' => $num, 'indexfile' => $indexfile,
936 'keys' => $keys, 'values' => $values, 'lefts' => $lefts, },
937 __PACKAGE__;
938 0 $self;
939 }
940  
941 # ###########
942 # FoxBase CDX
943  
944 package XBase::cdx;
945 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
946 6 ▶use vars qw( @ISA $DEBUG );
Subroutine BEGIN: called CC=1 SCAR=0.0
947 6 @ISA = qw( XBase::Base XBase::Index );
948  
949 6 *DEBUG = \$XBase::Index::DEBUG;
950  
951 sub prepare_write_header {
952 0 ▶ my $self = shift;
Subroutine prepare_write_header: not called CC=1 SCAR=6.9
953 my $data = pack 'VVNv CC @502 vvv @510 v @512 a512',
954 $self->{'start_page'} * 512,
955 $self->{'start_free_list'} * 512,
956 0 @{$self}{ qw( total_pages
957 key_length index_options index_signature
958 sort_order total_expr_length for_expression_length
959 key_expression_length
960 key_string
961 ) };
962 0 $data;
963 }
964 sub write_header {
965 0 ▶ my $self = shift;
Subroutine write_header: not called CC=2 SCAR=17.9
966 0 my $data = $self->prepare_write_header;
967 0 ▶ $self->{'fh'}->seek($self->{'adjusted_offset'} || 0, 0);
Condition0%0/2
$self->{'adjusted_offset'} || 0
l!l
00
MC/DC0%0/1
$self->{'adjusted_offset'} || 0
$self->{'adjusted_offset'}
Truth table0%0/2
A
A= $self->{'adjusted_offset'}
Aresult
11
00
968 0 $self->{'fh'}->print($data);
969 }
970 sub read_header {
971 18 ▶ my ($self, %opts) = @_;
Subroutine read_header: called CC=17 SCAR=31.0
972 18 ▶ $self->{'dbf'} = $opts{'dbf'} if not exists $self->{'dbf'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"dbf"} 9 9 18
973  
974 18 my $header;
975 $self->{'fh'}->read($header, 1024) == 1024 or do
976 18 ▶ { __PACKAGE__->Error("Error reading header of $self->{'filename'}: $!\n"); return; };
Branch: 1 branch
BranchTrueFalseTotal
$self->{"fh"}->read($header, 1024) == 1024 or do { __PACKAGE__->Error(qq[Error reading header of $self->{"filename"}: $!\n]); return } 0 18 18
977  
978 18 @{$self}{ qw( start_page start_free_list total_pages
979 key_length index_options index_signature
980 sort_order total_expr_length for_expression_length
981 key_expression_length
982 key_string
983 ) }
984 = unpack 'VVNv CC @502 vvv @510 v @512 a512', $header;
985  
986 18 $self->{'total_pages'} = -1; ### the total_pages value 11
987 ### that found in rooms.cdx is not correct, so we invalidate it
988  
989 ($self->{'key_string'}, $self->{'for_string'}) =
990 18 ($self->{'key_string'} =~ /^([^\000]*)\000([^\000]*)/);
991  
992 18 $self->{'key_record_length'} = $self->{'key_length'} + 4;
993 18 $self->{'record_len'} = 512;
994 18 $self->{'start_page'} /= $self->{'record_len'};
995 18 $self->{'start_free_list'} /= $self->{'record_len'};
996 18 $self->{'header_len'} = 0;
997 18 $self->{'key_type'} = 0;
998  
999 ## my $out = $self->prepare_write_header;
1000 ## if ($out ne $header) {
1001 ## print STDERR "I won't be able to write the header back\n",
1002 ## unpack("H*", $out), "\n ++\n",
1003 ## unpack("H*", $header), "\n";
1004 ## }
1005  
1006 18 ▶ if (not defined $self->{'tag'}) { # top level
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"tag"}) 9 9 18
1007 9 $self->prepare_select;
1008 9 ▶ while (my ($tag) = $self->fetch) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($tag) = $self->fetch) 45 9 54
1009 45 push @{$self->{'tags'}}, $tag;
1010 45 ▶ $opts{'tag'} ||= $tag; # Default to first tag
Condition66%2/3
$opts{"tag"} ||= $tag
l!l&&r!l&&!r
4410
MC/DC0%0/2
$opts{"tag"} ||= $tag
$opts{"tag"} $tag
Truth table66%2/3
A ||= B
A= $opts{"tag"}B= $tag
ABresult
1X1
011
000
1011 }
1012 }
1013 ### use Data::Dumper; print Dumper \%opts;
1014  
1015 18 ▶ if (defined $opts{'tag'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"tag"}) 9 9 18
1016 9 $self->prepare_select_eq($opts{'tag'});
1017 9 my ($foundkey, $value) = $self->fetch;
1018  
1019 9 ▶ if (not defined $foundkey or $opts{'tag'} ne $foundkey) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $foundkey or $opts{"tag"} ne $foundkey) 0 9 9
Condition33%1/3
not defined $foundkey or $opts{"tag"} ne $foundkey
l!l&&r!l&&!r
009
MC/DC0%0/2
not defined $foundkey or $opts{"tag"} ne $foundkey
not defined $foundkey $opts{"tag"} ne $foundkey
Truth table33%1/3
not(A) or B
A= not defined $foundkeyB= $opts{"tag"} ne $foundkey
ABresult
1X1
011
000
1020 0 __PACKAGE__->Error("No tag $opts{'tag'} found in index file $self->{'filename'}.\n"); return; };
1021  
1022 9 my $subidx = bless { %$self }, ref $self;
1023 9 ▶ print "Adjusting start_page value by $value for $opts{'tag'}\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XBase::cdx::DEBUG 0 9 9
1024 9 $subidx->{'fh'}->seek($value, 0);
1025 9 $subidx->{'adjusted_offset'} = $value;
1026 9 $subidx->{'tag'} = $opts{'tag'};
1027 9 $subidx->read_header;
1028  
1029 9 my $key_string = $subidx->{'key_string'};
1030 9 my $field_type;
1031 9 ▶ if (defined $opts{'type'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'type'}) { } 0 9 9
elsif (defined $subidx->{'dbf'}) { } 9 0 9
1032 0 $field_type = $opts{'type'};
1033 }
1034 elsif (defined $subidx->{'dbf'}) {
1035 9 $field_type = $subidx->{'dbf'}->field_type($key_string);
1036 9 ▶ if (not defined $field_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $field_type) 0 9 9
1037 0 __PACKAGE__->Error("Couldn't find key string `$key_string' in dbf file, can't determine field type\n");
1038 0 return;
1039 }
1040 }
1041 else {
1042 0 __PACKAGE__->Error("Index type (char/numeric) unknown for $subidx\n");
1043 0 return;
1044 }
1045 9 ▶ $subidx->{'key_type'} = ($field_type =~ /^[NDIF]$/ ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$field_type =~ /^[NDIF]$/ ? : 0 9 9
1046 9 ▶ if ($field_type eq 'D') {
Branch: 1 branch
BranchTrueFalseTotal
if ($field_type eq "D") 0 9 9
1047 0 $subidx->{'key_type'} = 2;
1048 0 require Time::JulianDay;
1049 }
1050  
1051 9 for (keys %$self) { delete $self->{$_} }
1052 9 for (keys %$subidx) { $self->{$_} = $subidx->{$_} }
1053 9 $self = $subidx;
1054 ### use Data::Dumper; print Dumper $self;
1055 }
1056 18 $self;
1057 }
1058  
1059 sub last_record {
1060 17 ▶ shift->{'total_pages'};
Subroutine last_record: called CC=1 SCAR=0.0
1061 }
1062  
1063 package XBase::cdx::Page;
1064 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1065 6 ▶use vars qw( @ISA $DEBUG );
Subroutine BEGIN: called CC=1 SCAR=0.0
1066 6 @ISA = qw( XBase::cdx );
1067  
1068 6 *DEBUG = \$XBase::Index::DEBUG;
1069  
1070 # Constructor for the cdx page
1071 sub new {
1072 17 ▶ my ($indexfile, $num) = @_;
Subroutine new: called CC=28 SCAR=49.9
1073 my $data = $indexfile->read_record($num)
1074 17 ▶ or do { print $indexfile->errstr; return; }; # get 512 bytes
Branch: 1 branch
BranchTrueFalseTotal
my $data = $indexfile->read_record($num) or do { print $indexfile->errstr; return } 0 17 17
1075  
1076 17 my $origdata = $data;
1077  
1078 17 my ($attributes, $noentries, $left_brother, $right_brother)
1079 = unpack 'vvVV', $data; # parse header of the page
1080 17 my $keylength = $indexfile->{'key_length'};
1081 17 my $keyreclength = $indexfile->{'key_record_length'}; # length
1082  
1083 17 ▶ print "page $num, attr $attributes, noentries $noentries, keylength $keylength (bro $left_brother, $right_brother)\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XBase::cdx::Page::DEBUG 0 17 17
1084 17 my $numdate = $indexfile->{'key_type'}; # numeric or string?
1085  
1086 17 my ($keys, $values, $lefts) = ([], [], undef);
1087  
1088 17 my %opts = ();
1089  
1090 17 ▶ if ($attributes & 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attributes & 2) { } 17 0 17
1091 17 ▶ print "leaf page, compressed\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XBase::cdx::Page::DEBUG 0 17 17
1092 17 my ($free_space, $recno_mask, $duplicate_count_mask,
1093 $trailing_count_mask, $recno_count, $duplicate_count,
1094 $trailing_count, $holding_recno) = unpack '@12 vVCCCCCC', $data;
1095 17 ▶ print '$free_space, $recno_mask, $duplicate_count_mask, $trailing_count_mask, $recno_count, $duplicate_count, $trailing_count, $holding_recno) = ',
Branch: 1 branch
BranchTrueFalseTotal
if $XBase::cdx::Page::DEBUG > 2 0 17 17
1096 "$free_space, $recno_mask, $duplicate_count_mask, $trailing_count_mask, $recno_count, $duplicate_count, $trailing_count, $holding_recno)\n" if $DEBUG > 2;
1097  
1098 17 @opts{ qw! recno_count duplicate_count trailing_count
1099 holding_recno ! } =
1100 ( $recno_count, $duplicate_count, $trailing_count,
1101 $holding_recno);
1102  
1103 17 my $prevkeyval = '';
1104 17 ▶ for (my $i = 0; $i < $noentries; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $noentries) 295 17 312
1105 295 my $one_item = substr($data, 24 + $i * $holding_recno, $holding_recno) . "\0" x 4;
1106 295 my $numeric_one_item = unpack 'V', $one_item;
1107
1108 295 ▶ print "one_item: 0x", unpack('H*', $one_item), " ($numeric_one_item)\n" if $DEBUG > 3;
Branch: 1 branch
BranchTrueFalseTotal
if $XBase::cdx::Page::DEBUG > 3 0 295 295
1109  
1110 295 my $recno = $numeric_one_item & $recno_mask;
1111 295 my $bytes_of_recno = int($recno_count / 8);
1112 295 $one_item = substr($one_item, $bytes_of_recno);
1113  
1114 295 $numeric_one_item = unpack 'V', $one_item;
1115 295 $numeric_one_item >>= $recno_count - (8 * $bytes_of_recno);
1116
1117 295 my $dupl = $numeric_one_item & $duplicate_count_mask;
1118 295 $numeric_one_item >>= $duplicate_count;
1119 295 my $trail = $numeric_one_item & $trailing_count_mask;
1120 ### $numeric_one_item >>= $trailing_count;
1121  
1122 295 ▶ print "Item $i: trail $trail, dupl $dupl, recno $recno\n" if $DEBUG > 6;
Branch: 1 branch
BranchTrueFalseTotal
if $XBase::cdx::Page::DEBUG > 6 0 295 295
1123  
1124 295 my $getlength = $keylength - $trail - $dupl;
1125 295 my $key = substr($prevkeyval, 0, $dupl);
1126 295 ▶ $key .= substr($data, -$getlength) if $getlength;
Branch: 1 branch
BranchTrueFalseTotal
if $getlength 176 119 295
1127 295 $key .= "\000" x $trail;
1128 295 ▶ substr($data, -$getlength) = '' if $getlength;
Branch: 1 branch
BranchTrueFalseTotal
if $getlength 176 119 295
1129 295 $prevkeyval = $key;
1130  
1131 ### print "Numdate $numdate\n";
1132 295 ▶ if ($numdate) { # some decoding for numbers
Branch: 1 branch
BranchTrueFalseTotal
if ($numdate) { } 0 295 295
1133 ### print " *** In: ", unpack("H*", $key), "\n";
1134 0 ▶ if (0x80 & unpack('C', $key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (128 & unpack('C', $key)) { } 0 0 0
1135 0 substr($key, 0, 1) &= "\177";
1136 }
1137 0 else { $key = ~$key; }
1138 0 ▶ if ($keylength == 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keylength == 8) { } 0 0 0
1139 0 ▶ $key = reverse $key unless $XBase::Index::BIGEND;
Branch: 1 branch
BranchTrueFalseTotal
unless $XBase::Index::BIGEND 0 0 0
1140 0 $key = unpack 'd', $key;
1141 } else {
1142 0 $key = unpack 'N', $key;
1143 }
1144 0 ▶ if ($numdate == 2 and $key) { # date
Branch: 1 branch
BranchTrueFalseTotal
if ($numdate == 2 and $key) 0 0 0
Condition0%0/3
$numdate == 2 and $key
!ll&&!rl&&r
000
MC/DC0%0/2
$numdate == 2 and $key
$numdate == 2 $key
Truth table0%0/3
A and B
A= $numdate == 2B= $key
ABresult
0X0
100
111
1145 0 $key = sprintf "%04d%02d%02d",
1146 Time::JulianDay::inverse_julian_day($key);
1147 }
1148 } else {
1149 295 ▶ substr($key, -$trail) = '' if $trail;
Branch: 1 branch
BranchTrueFalseTotal
if $trail 255 40 295
1150 }
1151  
1152 295 ▶ print "$key -> $recno\n" if $DEBUG > 4;
Branch: 1 branch
BranchTrueFalseTotal
if $XBase::cdx::Page::DEBUG > 4 0 295 295
1153 295 push @$keys, $key;
1154 295 push @$values, $recno;
1155 }
1156 } else {
1157 0 ▶ for (my $i = 0; $i < $noentries; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $noentries) 0 0 0
1158 0 my $offset = 12 + $i * ($keylength + 8);
1159 0 my ($key, $recno, $page)
1160 = unpack "\@$offset a$keylength NN", $data;
1161 # some decoding for numbers
1162 0 ▶ if ($numdate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($numdate) { } 0 0 0
1163 0 ▶ if (0x80 & unpack('C', $key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (128 & unpack('C', $key)) { } 0 0 0
1164 ### if ("\200" & substr($key, 0, 1)) {
1165 ### print STDERR "Declean\n";
1166 ### print STDERR unpack("H*", $key), ' -> ';
1167 0 substr($key, 0, 1) &= "\177";
1168 ### print STDERR unpack("H*", $key), "\n";
1169 }
1170 0 else { $key = ~$key; }
1171 0 ▶ if ($keylength == 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keylength == 8) { } 0 0 0
1172 0 ▶ $key = reverse $key unless $XBase::Index::BIGEND;
Branch: 1 branch
BranchTrueFalseTotal
unless $XBase::Index::BIGEND 0 0 0
1173 0 $key = unpack 'd', $key;
1174 } else {
1175 0 $key = unpack 'N', $key;
1176 }
1177 0 ▶ if ($numdate == 2 and $key) { # date
Branch: 1 branch
BranchTrueFalseTotal
if ($numdate == 2 and $key) 0 0 0
Condition0%0/3
$numdate == 2 and $key
!ll&&!rl&&r
000
MC/DC0%0/2
$numdate == 2 and $key
$numdate == 2 $key
Truth table0%0/3
A and B
A= $numdate == 2B= $key
ABresult
0X0
100
111
1178 0 $key = sprintf "%04d%02d%02d",
1179 Time::JulianDay::inverse_julian_day($key);
1180 }
1181 } else {
1182 0 $key =~ s/\000+$//;
1183 }
1184 0 ▶ print "item: $key -> $recno via $page\n" if $DEBUG > 4;
Branch: 1 branch
BranchTrueFalseTotal
if $XBase::cdx::Page::DEBUG > 4 0 0 0
1185 0 push @$keys, $key;
1186 0 push @$values, $recno;
1187 0 ▶ $lefts = [] unless defined $lefts;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lefts 0 0 0
1188 0 push @$lefts, $page / 512;
1189 }
1190 0 $opts{'last_key_is_just_overflow'} = 1;
1191 }
1192  
1193 17 my $self = bless { 'keys' => $keys, 'values' => $values,
1194 'num' => $num, 'keylength' => $keylength,
1195 'lefts' => $lefts, 'indexfile' => $indexfile,
1196 'attributes' => $attributes,
1197 'left_brother' => $left_brother,
1198 'right_brother' => $right_brother, %opts,
1199 }, __PACKAGE__;
1200  
1201 17 my $outdata = $self->prepare_scalar_for_write;
1202 17 if (0 and $outdata ne $origdata) {
1203 print "I won't be able to write this page back.\n",
1204 unpack("H*", $outdata), "\n ++\n",
1205 unpack("H*", $origdata), "\n";
1206 } else {
1207 ### print STDERR " ** Bingo: I will be able to write this page back ($num).\n";
1208 }
1209  
1210 17 $self;
1211 }
1212  
1213 # Create "new" page -- allocates memory in the file and returns
1214 # structure that can reasonably used as XBase::cdx::Page
1215 sub create {
1216 0 ▶ my ($class, $indexfile) = @_;
Subroutine create: not called CC=4 SCAR=30.0
1217 0 ▶ if (not defined $indexfile and ref $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $indexfile and ref $class) 0 0 0
Condition0%0/3
not defined $indexfile and ref $class
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $indexfile and ref $class
not defined $indexfile ref $class
Truth table0%0/3
not(A) and B
A= not defined $indexfileB= ref $class
ABresult
0X0
100
111
1218 0 $indexfile = $class->{'indexfile'};
1219 }
1220 0 my $fh = $indexfile->{'fh'};
1221 0 $fh->seek(0, 2); # seek to the end;
1222 0 my $position = $fh->tell; # get the length of the file
1223 0 ▶ if ($position % 512) {
Branch: 1 branch
BranchTrueFalseTotal
if ($position % 512) 0 0 0
1224 0 $fh->print("\000" x (512 - ($position % 512)));
1225 # pad the file to multiply of 512
1226 0 $position = $fh->tell; # get the length of the file
1227 }
1228 0 $fh->print("\000" x 512);
1229 return bless { 'num' => $position / 512,
1230 0 'keylength' => $indexfile->{'key_length'},
1231 'indexfile' => $indexfile }, $class;
1232 }
1233  
1234 sub prepare_scalar_for_write {
1235 22 ▶ my $self = shift;
Subroutine prepare_scalar_for_write: called CC=16 SCAR=38.0
1236  
1237 my ($attributes, $noentries, $left_brother, $right_brother)
1238 22 = ($self->{'attributes'}, scalar(@{$self->{'keys'}}),
1239 22 $self->{'left_brother'}, $self->{'right_brother'});
1240
1241 22 my $data = pack 'vvVV', $attributes, $noentries, $left_brother,
1242 $right_brother;
1243
1244 22 my $indexfile = $self->{'indexfile'};
1245 22 my $numdate = $indexfile->{'key_type'}; # numeric or string?
1246 22 my $record_len = $indexfile->{'record_len'};
1247 22 my $keylength = $self->{'keylength'};
1248  
1249 22 ▶ if ($attributes & 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attributes & 2) { } 22 0 22
1250  
1251 22 my ($recno_count, $duplicate_count, $trailing_count,
1252 $holding_recno) = (16, 4, 4, 3);
1253 22 ▶ if (defined $self->{'recno_count'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"recno_count"}) 22 0 22
1254 ($recno_count, $duplicate_count, $trailing_count,
1255 $holding_recno) =
1256 22 @{$self}{ qw! recno_count duplicate_count trailing_count
1257 holding_recno ! };
1258 }
1259  
1260 ### print STDERR "Hmmm. We are setting hardcoded values for bitmasks, not good. Write to adelton.\n";
1261 22 my ($recno_mask, $duplicate_mask, $trailing_mask)
1262 = ( 2**$recno_count - 1, 2**$duplicate_count - 1,
1263 2**$trailing_count - 1);
1264  
1265  
1266 22 my $recno_data = '';
1267  
1268 22 my $keys_string = '';
1269 22 my $prevkey = '';
1270  
1271 22 my $row = 0;
1272 22 for my $key (@{$self->{'keys'}}) {
1273 424 my $dupl = 0;
1274  
1275 424 my $out = $key;
1276 # some encoding for numbers
1277 424 ▶ if ($numdate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($numdate) 0 424 424
1278 0 ▶ if ($keylength == 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keylength == 8) { } 0 0 0
1279 0 $out = pack 'd', $out;
1280 0 ▶ $out = reverse $out unless $XBase::Index::BIGEND;
Branch: 1 branch
BranchTrueFalseTotal
unless $XBase::Index::BIGEND 0 0 0
1281 } else {
1282 0 $out = pack 'N', $out;
1283 }
1284  
1285  
1286 0 ▶ unless (0x80 & unpack('C', $out)) {
Branch: 1 branch
BranchTrueFalseTotal
if (128 & unpack('C', $out)) { } 0 0 0
1287 0 substr($out, 0, 1) |= "\200";
1288 }
1289 0 else { $out = ~$out; }
1290 }
1291  
1292 424 for my $i (0 .. length($out) - 1) {
1293 1522 ▶ unless (substr($out, $i, 1) eq substr($prevkey, $i, 1)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (substr($out, $i, 1) eq substr($prevkey, $i, 1)) 265 1257 1522
1294 265 last;
1295 }
1296 1257 $dupl++;
1297 }
1298  
1299 424 my $trail = $keylength - length $out;
1300 424 ▶ while (substr($out, -1) eq "\000") {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($out, -1) eq "\0") 0 424 424
1301 0 $out = substr($out, 0, length($out) - 1);
1302 0 $trail++;
1303 }
1304 424 $keys_string = substr($out, $dupl) . $keys_string;
1305  
1306  
1307 my $numdata =
1308 (((($trail & $trailing_mask) << $duplicate_count)
1309 | ($dupl & $duplicate_mask)) << $recno_count)
1310 424 | ($self->{'values'}[$row] & $recno_mask);
1311  
1312 424 $recno_data .= substr(pack('V', $numdata), 0, $holding_recno);
1313  
1314 ### print unpack("H*", substr($out, $dupl)), ": trail $trail, dupl $dupl\n";
1315  
1316 424 $prevkey = $out;
1317 424 $row++;
1318 }
1319 ### print $keys_string, "\n";
1320  
1321 ### print STDERR "Hmmm. The \$numdata is really just a hack -- the shifts have to be made 64 bit clean.\n";
1322 22 $data .= pack 'vVCCCCCC',
1323 ($record_len - length($recno_data) - length($keys_string)
1324 - 24), $recno_mask, $duplicate_mask,
1325 $trailing_mask, $recno_count, $duplicate_count,
1326 $trailing_count, $holding_recno;
1327  
1328 22 $data .= $recno_data;
1329 22 $data .= "\000" x ($record_len - length($data) - length($keys_string));
1330 22 $data .= $keys_string;
1331 } else {
1332 0 my $row = 0;
1333 0 for my $key (@{$self->{'keys'}}) {
1334 0 my $out = $key;
1335 # some encoding for numbers
1336 0 ▶ if ($numdate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($numdate) 0 0 0
1337 0 ▶ if ($keylength == 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keylength == 8) { } 0 0 0
1338 0 $out = pack 'd', $out;
1339 0 ▶ $out = reverse $out unless $XBase::Index::BIGEND;
Branch: 1 branch
BranchTrueFalseTotal
unless $XBase::Index::BIGEND 0 0 0
1340 } else {
1341 0 $out = pack 'N', $out;
1342 }
1343  
1344  
1345 0 ▶ unless (0x80 & unpack('C', $out)) {
Branch: 1 branch
BranchTrueFalseTotal
if (128 & unpack('C', $out)) { } 0 0 0
1346 0 substr($out, 0, 1) |= "\200";
1347 }
1348 0 else { $out = ~$out; }
1349 ### print " *** Out2: ", unpack("H*", $out), "\n";
1350 }
1351 $data .= pack "a$keylength NN", $out,
1352 $self->{'values'}[$row],
1353 0 $self->{'lefts'}[$row] * 512;
1354 0 $row++;
1355 }
1356 0 $data .= "\000" x ($record_len - length($data));
1357 }
1358 22 $data;
1359 }
1360  
1361 sub write_page {
1362 0 ▶ my $self = shift;
Subroutine write_page: not called CC=2 SCAR=17.9
1363 0 my $indexfile = $self->{'indexfile'};
1364  
1365 0 my $data = $self->prepare_scalar_for_write;
1366 0 ▶ die "Data is too long in cdx::write_page for $self->{'num'}\n"
Branch: 1 branch
BranchTrueFalseTotal
if length $data > 512 0 0 0
1367 if length $data > 512;
1368 0 $indexfile->write_record($self->{'num'}, $data);
1369 }
1370  
1371 # Saves current page, taking into account all neighbour and parent
1372 # pages. We can safely assume that this method is called for pages
1373 # that have been loaded using prepare_select_eq and fetch, so they
1374 # have the parent pointers set correctly.
1375 sub write_with_context {
1376 5 ▶ my $self = shift; # page to save
Subroutine write_with_context: called CC=30 SCAR=65.8
1377 5 ▶ print STDERR "XBase::cdx::Page::write_with_context called ($self->{'num'})\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XBase::cdx::Page::DEBUG 0 5 5
1378  
1379 5 my $indexfile = $self->{'indexfile'};
1380  
1381 5 my $self_num = $self->{'num'};
1382  
1383 # get the current page as data to be written
1384 5 my $data = $self->prepare_scalar_for_write;
1385  
1386 5 ▶ if (not @{$self->{'keys'}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"keys"};}) 0 5 5
1387 0 $indexfile->write_record($self_num, $data);
1388  
1389 # empty root page means no more work, just save
1390 0 ▶ return if $self_num == $indexfile->{'start_page'};
Branch: 1 branch
BranchTrueFalseTotal
if $self_num == $indexfile->{"start_page"} 0 0 0
1391  
1392 0 print STDERR "The page $self_num is empty, releasing from the chain\n";
1393
1394 # first we update the brothers
1395 0 my $right_brother_num = $self->{'right_brother'};
1396 0 my $left_brother_num = $self->{'left_brother'};
1397 0 ▶ if ($right_brother_num != 0xFFFFFFFF) {
Branch: 1 branch
BranchTrueFalseTotal
if ($right_brother_num != 4294967295) 0 0 0
1398 0 my $fix_brother = $indexfile->get_record($right_brother_num / 512);
1399 0 $fix_brother->{'left_brother'} = $left_brother_num;
1400 0 $fix_brother->write_page;
1401 }
1402 0 ▶ if ($left_brother_num != 0xFFFFFFFF) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left_brother_num != 4294967295) 0 0 0
1403 0 my $fix_brother = $indexfile->get_record($left_brother_num / 512);
1404 0 $fix_brother->{'right_brother'} = $right_brother_num;
1405 0 $fix_brother->write_page;
1406 }
1407  
1408 # now we need to release ourselves from parent as well
1409 0 ▶ my $parent = $self->get_parent_page or die "Index corrupt: no parent for page $self ($self_num)\n";
Branch: 1 branch
BranchTrueFalseTotal
my $parent = $self->get_parent_page or die "Index corrupt: no parent for page $self ($self_num)\n" 0 0 0
1410  
1411 0 my $maxindex = $#{$parent->{'lefts'}};
1412 0 my $i;
1413 0 ▶ for ($i = 0; $i <= $maxindex; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $maxindex) 0 0 0
1414 0 ▶ if ($parent->{'lefts'}[$i] == $self_num) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->{"lefts"}[$i] == $self_num) 0 0 0
1415 0 splice @{$parent->{'keys'}}, $i, 1;
1416 0 splice @{$parent->{'values'}}, $i, 1;
1417 0 splice @{$parent->{'lefts'}}, $i, 1;
1418 0 last;
1419 }
1420 }
1421 0 ▶ if ($i > $maxindex) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > $maxindex) 0 0 0
1422 0 die "Index corrupt: parent doesn't point to us in write_with_context $self ($self_num)\n";
1423 }
1424 0 $parent->write_with_context;
1425 0 return;
1426 }
1427  
1428  
1429 5 ▶ if (length $data > 512) { # we need to split the page
Branch: 2 branches
BranchTrueFalseTotal
if (length $data > 512) { } 0 5 5
elsif ($self_num != $indexfile->{'start_page'}) { } 0 5 5
1430 0 print STDERR "Splitting full page $self ($self_num)\n";
1431  
1432 # create will give us brand new empty page
1433
1434 0 my $new_page = __PACKAGE__->create($indexfile);
1435 0 $self->{'attributes'} &= 0xfffe;
1436 0 $new_page->{'attributes'} = $self->{'attributes'};
1437  
1438 0 my $total_rows = scalar(@{$self->{'keys'}});
1439 0 my $half_rows = int($total_rows / 2);
1440  
1441 # primary split
1442 0 ▶ if ($half_rows == 0) { $half_rows++; }
Branch: 1 branch
BranchTrueFalseTotal
if ($half_rows == 0) 0 0 0
1443 0 ▶ if ($half_rows == $total_rows) {
Branch: 1 branch
BranchTrueFalseTotal
if ($half_rows == $total_rows) 0 0 0
1444 0 die "Fatal trouble: page $self ($self_num) is full but I'm not able to split it\n";
1445 }
1446  
1447 # new page is right brother (will get bigger values)
1448 0 $new_page->{'right_brother'} = $self->{'right_brother'};
1449 0 $new_page->{'left_brother'} = $self_num * 512;
1450 0 $self->{'right_brother'} = $new_page->{'num'} * 512;
1451  
1452 0 ▶ if ($new_page->{'right_brother'} != 0xFFFFFFFF) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_page->{"right_brother"} != 4294967295) 0 0 0
1453 0 my $fix_brother = $indexfile->get_record($new_page->{'right_brother'} / 512);
1454 0 $fix_brother->{'left_brother'} = $new_page->{'num'} * 512;
1455 0 $fix_brother->write_page;
1456 }
1457  
1458 # we'll split keys and values
1459 0 $new_page->{'keys'} = [ @{$self->{'keys'}}[$half_rows .. $total_rows - 1] ];
1460 0 splice @{$self->{'keys'}}, $half_rows, $total_rows - $half_rows;
1461 0 $new_page->{'values'} = [ @{$self->{'values'}}[$half_rows .. $total_rows - 1] ];
1462 0 splice @{$self->{'values'}}, $half_rows, $total_rows - $half_rows;
1463  
1464 # and we'll split pointers to lower levels, if there are any
1465 0 ▶ if (defined $self->{'lefts'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"lefts"}) 0 0 0
1466 0 $new_page->{'lefts'} = [ @{$self->{'lefts'}}[$half_rows .. $total_rows - 1] ];
1467 0 my $new_page_num = $new_page->{'num'};
1468 0 for my $q (@{$new_page->{'lefts'}}) {
1469 0 ▶ if (defined $q and defined $indexfile->{'pages_cache'}{$q}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $q and defined $indexfile->{"pages_cache"}{$q}) 0 0 0
Condition0%0/3
defined $q and defined $indexfile->{"pages_cache"}{$q}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $q and defined $indexfile->{"pages_cache"}{$q}
defined $q defined $indexfile->{"pages_cache"}{$q}
Truth table0%0/3
A and B
A= defined $qB= defined $indexfile->{"pages_cache"}{$q}
ABresult
0X0
100
111
1470 0 $indexfile->{'pages_cache'}{$q}{'parent'} = $new_page_num;
1471 }
1472 }
1473 0 splice @{$self->{'lefts'}}, $half_rows, $total_rows - $half_rows - 1;
1474 }
1475  
1476 0 my $parent;
1477 0 ▶ if ($self_num == $indexfile->{'start_page'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self_num == $indexfile->{'start_page'}) { } 0 0 0
1478 # we're splitting the root page, so we will
1479 # create new one
1480 0 $parent = __PACKAGE__->create($indexfile);
1481  
1482 0 $indexfile->{'start_page'} = $parent->{'num'};
1483 0 $indexfile->write_header;
1484  
1485 ### xxxxxxxxxxxxxxxxxxx
1486 ### And here we should write the header so that
1487 ### the new root page is saved to disk. Not
1488 ### tested yet.
1489 ### xxxxxxxxxxxxxxxxxxx
1490  
1491 0 $parent->{'attributes'} = 1; # root page
1492  
1493 $parent->{'keys'} = [ $self->{'keys'}[-1],
1494 0 $new_page->{'keys'}[-1] ];
1495 $parent->{'values'} = [ $self->{'values'}[-1],
1496 0 $new_page->{'values'}[-1] ];
1497 0 $parent->{'lefts'} = [ $self_num, $new_page->{'num'} ];
1498 } else { # update pointers in parent page
1499 0 ▶ $parent = $self->get_parent_page or die "Index corrupt: no parent for page $self ($self_num)\n";
Branch: 1 branch
BranchTrueFalseTotal
$parent = $self->get_parent_page or die "Index corrupt: no parent for page $self ($self_num)\n" 0 0 0
1500 0 my $maxindex = $#{$parent->{'lefts'}};
1501 0 my $i = 0;
1502  
1503 # find pointer to ourselves in the parent
1504 0 ▶ while ($i <= $maxindex) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $maxindex) 0 0 0
1505 0 ▶ last if $parent->{'lefts'}[$i] == $self_num;
Branch: 1 branch
BranchTrueFalseTotal
if $parent->{"lefts"}[$i] == $self_num 0 0 0
1506 0 $i++;
1507 }
1508
1509 0 ▶ if ($i > $maxindex) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > $maxindex) 0 0 0
1510 0 die "Index corrupt: parent doesn't point to us in write_with_context $self ($self_num)\n";
1511 }
1512  
1513 # now $i is index in parent of the record pointing to us
1514  
1515 0 splice @{$parent->{'keys'}}, $i, 1,
1516 0 $self->{'keys'}[-1], $new_page->{'keys'}[-1];
1517 0 splice @{$parent->{'values'}}, $i, 1,
1518 0 $self->{'values'}[-1], $new_page->{'values'}[-1];
1519 0 splice @{$parent->{'lefts'}}, $i, 1,
1520 0 $self_num, $new_page->{'num'};
1521 }
1522  
1523 0 $self->write_page;
1524  
1525 0 $new_page->{'parent'} = $self->{'parent'};
1526 0 $new_page->write_page;
1527  
1528 0 $parent->write_with_context;
1529 }
1530 elsif ($self_num != $indexfile->{'start_page'}) {
1531 # the output data is OK, write is out
1532 # but this is not root page, so we need to make sure the
1533 # parent is updated as well
1534 0 $indexfile->write_record($self_num, $data);
1535  
1536 # now we need to check if the parent page still points
1537 # correctly to us (the last value might have changed)
1538 0 ▶ my $parent = $self->get_parent_page or die "Index corrupt: no parent for page $self ($self_num)\n";
Branch: 1 branch
BranchTrueFalseTotal
my $parent = $self->get_parent_page or die "Index corrupt: no parent for page $self ($self_num)\n" 0 0 0
1539  
1540 0 my $maxindex = $#{$parent->{'lefts'}};
1541 0 my $i = 0;
1542  
1543 # find pointer to ourselves in the parent
1544 0 ▶ while ($i <= $maxindex) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $maxindex) 0 0 0
1545 0 ▶ last if $parent->{'lefts'}[$i] == $self_num;
Branch: 1 branch
BranchTrueFalseTotal
if $parent->{"lefts"}[$i] == $self_num 0 0 0
1546 0 $i++;
1547 }
1548
1549 0 ▶ if ($i > $maxindex) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > $maxindex) 0 0 0
1550 0 die "Index corrupt: parent doesn't point to us in write_with_context $self ($self_num)\n";
1551 }
1552  
1553 # now $i is index in parent of the record pointing to us
1554  
1555 0 ▶ if ($parent->{'values'}[$i] != $self->{'values'}[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->{"values"}[$i] != $self->{"values"}[-1]) 0 0 0
1556 0 print STDERR "Will need to update the parent -- last value in myself changed ($self_num)\n";
1557 0 $parent->{'values'}[$i] = $self->{'values'}[-1];
1558 0 $parent->{'keys'}[$i] = $self->{'keys'}[-1];
1559 0 $parent->write_with_context;
1560 }
1561
1562 } else { # write out root page
1563 5 $indexfile->write_record($self_num, $data);
1564 }
1565  
1566 5 ▶ print STDERR "XBase::cdx::Page::write_with_context finished ($self->{'num'})\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XBase::cdx::Page::DEBUG 0 5 5
1567 }
1568  
1569 # finds parent page for the object
1570 sub get_parent_page_num {
1571 0 ▶ my $self = shift;
Subroutine get_parent_page_num: not called CC=5 SCAR=34.0
1572 0 ▶ return $self->{'parent'} if defined $self->{'parent'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"parent"} 0 0 0
1573  
1574 0 my $indexfile = $self->{'indexfile'};
1575  
1576 0 ▶ return if $self->{'num'} == $indexfile->{'start_page'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"num"} == $indexfile->{"start_page"} 0 0 0
1577  
1578 # this should search to this page, effectivelly setting the
1579 # level array in such a way that the parent page is there
1580 0 $indexfile->prepare_select_eq($self->{'keys'}[0], $self->{'values'}[0]);
1581  
1582 ### print STDERR "self($self->{'num'}): $self, pages: @{$indexfile->{'pages'}}\n";
1583 ### use Data::Dumper; print Dumper $indexfile;
1584 0 my $pageindex = $#{$indexfile->{'pages'}};
1585 0 ▶ while ($pageindex >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pageindex >= 0) 0 0 0
1586 0 ▶ if ("$self" eq "$indexfile->{'pages'}[$pageindex]") {
Branch: 1 branch
BranchTrueFalseTotal
if ("$self" eq qq[$indexfile->{"pages"}[$pageindex]]) 0 0 0
1587 0 print STDERR "Parent page for $self->{'num'} is $indexfile->{'pages'}[$pageindex - 1]{'num'}.\n";
1588 0 return $indexfile->{'pages'}[$pageindex - 1]->{'num'};
1589 }
1590 0 $pageindex--;
1591 }
1592 0 return undef;
1593 }
1594 sub get_parent_page {
1595 0 ▶ my $self = shift;
Subroutine get_parent_page: not called CC=2 SCAR=17.9
1596 0 ▶ my $parent_num = $self->get_parent_page_num or return;
Branch: 1 branch
BranchTrueFalseTotal
my $parent_num = $self->get_parent_page_num or return 0 0 0
1597 0 my $indexfile = $self->{'indexfile'};
1598 0 return $indexfile->get_record($parent_num);
1599 }
1600  
1601 6 1;
1602  
1603 __END__
« lib/XBase/SQL.pm ↑ Summary lib/XBase.pm »