Stone/Cursor.pm

filter: Statementstmt 3.5% 2 / 57 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 5 Podpod 75.0% 3 / 4 totaltotal 4.8% 5 / 103 CC 15 SCAR 41.5
CC
15
Cov
2%
each
110.0
next
20.0
new
6.0
CRAP 63.5
« Boulder/Genbank.pm ↑ Summary Boulder/Swissprot.pm »
1 # A simple iterator on a Stone.
2 package Stone::Cursor;
3  
4 =head1 NAME
5  
6 Stone::Cursor - Traverse tags and values of a Stone
7  
8 =head1 SYNOPSIS
9  
10 use Boulder::Store;
11 $store = Boulder::Store->new('./soccer_teams');
12  
13 my $stone = $store->get(28);
14 $cursor = $stone->cursor;
15 while (my ($key,$value) = $cursor->each) {
16 print "$value: Go Bluejays!\n" if $key eq 'State' and $value eq 'Katonah';
17 }
18  
19 =head1 DESCRIPTION
20  
21 Boulder::Cursor is a utility class that allows you to create one or
22 more iterators across a L<Stone> object. This is used for traversing
23 large Stone objects in order to identify or modify portions of the
24 record.
25  
26 =head2 CLASS METHODS
27  
28 =item Boulder::Cursor->new($stone)
29  
30 Return a new Boulder::Cursor over the specified L<Stone> object. This
31 will return an error if the object is not a L<Stone> or a
32 descendent. This method is usually not called directly, but rather
33 indirectly via the L<Stone> cursor() method:
34  
35 my $cursor = $stone->cursor;
36  
37 =head2 OBJECT METHODS
38  
39 =item $cursor->each()
40  
41 Iterate over the attached B<Stone>. Each iteration will return a
42 two-valued list consisting of a tag path and a value. The tag path is
43 of a form that can be used with B<Stone::index()> (in fact, a cursor
44 is used internally to implement the B<Stone::dump()> method. When the
45 end of the B<Stone> is reached, C<each()> will return an empty list,
46 after which it will start over again from the beginning. If you
47 attempt to insert or delete from the stone while iterating over it,
48 all attached cursors will reset to the beginnning.
49  
50 For example:
51  
52 $cursor = $s->cursor;
53 while (($key,$value) = $cursor->each) {
54 print "$value: BOW WOW!\n" if $key=~/pet/;
55 }
56  
57 =item $cursor->reset()
58  
59 This resets the cursor back to the beginning of the associated
60 B<Stone>.
61  
62 =head1 AUTHOR
63  
64 Lincoln D. Stein <lstein@cshl.org>.
65  
66 =head1 COPYRIGHT
67  
68 Copyright 1997-1999, Cold Spring Harbor Laboratory, Cold Spring Harbor
69 NY. This module can be used and distributed on the same terms as Perl
70 itself.
71  
72 =head1 SEE ALSO
73  
74 L<Boulder>, L<Stone>
75  
76 =cut
77  
78  
79 #------------------- Boulder::Cursor---------------
80  
81  
82 2 *next_pair = \&each;
83  
84 # New expects a Stone object as its single
85 # parameter.
86 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
87 0 my($package,$stone) = @_;
88 0 ▶ die "Boulder::Cursor: expect a Stone object parameter"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $stone 0 0 0
89 unless ref($stone);
90  
91 0 my $self = bless {'stone'=>$stone},$package;
92 0 $self->reset;
93 0 $stone->_register_cursor($self,'true');
94 0 return $self;
95 }
96  
97 # This procedure does a breadth-first search
98 # over the entire structure. It returns an array that looks like this
99 # (key1[index1].key2[index2].key3[index3],value)
100 0 ▶sub each {
Subroutine each: not called CC=10 SCAR=47.0
Pod documented
101 0 my $self = shift;
102 0 my $short_keys = shift;
103  
104 0 my $stack = $self->{'stack'};
105  
106 0 my($found,$key,$value);
107 0 my $top = $stack->[$#{$stack}];
108 0 ▶ while ($top && !$found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($top and not $found) 0 0 0
Condition0%0/3
$top and not $found
!ll&&!rl&&r
000
MC/DC0%0/2
$top and not $found
$top not $found
Truth table0%0/3
A and not(B)
A= $topB= not $found
ABresult
0X0
100
111
109 0 ▶ $found++ if ($key,$value) = $top->next;
Branch: 1 branch
BranchTrueFalseTotal
if () = $top->next 0 0 0
110 0 ▶ if (!$found) { # this iterator is done
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 0 0 0
111 0 pop @{$stack};
112 0 $top = $stack->[$#{$stack}];
113 0 next;
114 }
115 0 ▶ if ( ref $value && !exists $value->{'.name'} ) { # found another record to begin iterating on
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value and not exists $value->{".name"}) 0 0 0
Condition0%0/3
ref $value and not exists $value->{".name"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $value and not exists $value->{".name"}
ref $value not exists $value->{".name"}
Truth table0%0/3
A and not(B)
A= ref $valueB= not exists $value->{".name"}
ABresult
0X0
100
111
116 0 ▶ if (%{$value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (%{$value;}) { } 0 0 0
117 0 undef $found;
118 0 $top = $value->cursor;
119 0 push @{$stack},$top;
120 0 next;
121 } else {
122 0 undef $value;
123 }
124 }
125 }
126 0 ▶ unless ($found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 0 0 0
127 0 $self->reset;
128 0 return ();
129 }
130 0 ▶ return ($key,$value) if $short_keys;
Branch: 1 branch
BranchTrueFalseTotal
if $short_keys 0 0 0
131
132 my @keylist = map {($_->{'keys'}->[$_->{'hashindex'}])
133 0 . "[" . ($_->{'arrayindex'}-1) ."]"; } @{$stack};
134 0 return (join(".",@keylist),$value);
135 }
136  
137 0 ▶sub reset {
Subroutine reset: not called CC=1 SCAR=6.9
Pod documented
138 0 my $self = shift;
139 0 $self->{'arrayindex'} = 0;
140 0 $self->{'hashindex'} = 0;
141 0 $self->{'keys'}=[$self->{'stone'}->tags];
142 0 $self->{'stack'}=[$self];
143 }
144  
145 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
146 0 my $self = shift;
147 0 ▶ if (ref $self->{'stone'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"stone"}) 0 0 0
148 0 $self->{'stone'}->_register_cursor($self,undef);
149 }
150 }
151  
152 # Next will return the next index in its Stone object,
153 # indexing first through the members of the array, and then through
154 # the individual keys. When iteration is finished, it resets itself
155 # and returns an empty array.
156 0 ▶sub next {
Subroutine next: not called CC=4 SCAR=30.0
Pod undocumented
157 0 my $self = shift;
158 my($arrayi,$hashi,$stone,$keys) = ($self->{'arrayindex'},
159 $self->{'hashindex'},
160 $self->{'stone'},
161 0 $self->{'keys'});
162 0 ▶ unless ($stone->exists($keys->[$hashi],$arrayi)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($stone->exists($keys->[$hashi], $arrayi)) 0 0 0
163 0 $self->{hashindex}=++$hashi;
164 0 $self->{arrayindex}=$arrayi=0;
165 0 ▶ unless (defined($keys->[$hashi]) &&
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $keys->[$hashi] and defined $stone->get($keys->[$hashi], $arrayi)) 0 0 0
Condition0%0/3
defined $keys->[$hashi] and defined $stone->get($keys->[$hashi], $arrayi)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $keys->[$hashi] and defined $stone->get($keys->[$hashi], $arrayi)
defined $keys->[$hashi] defined $stone->get($keys->[$hashi], $arrayi)
Truth table0%0/3
A and B
A= defined $keys->[$hashi]B= defined $stone->get($keys->[$hashi], $arrayi)
ABresult
0X0
100
111
166 defined($stone->get($keys->[$hashi],$arrayi))) {
167 0 $self->reset;
168 0 return ();
169 }
170 }
171 0 $self->{arrayindex}++;
172 0 return ($keys->[$hashi],$stone->get($keys->[$hashi],$arrayi));
173 }
174  
175  
176 2 1;
« Boulder/Genbank.pm ↑ Summary Boulder/Swissprot.pm »