| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$top and not $found
MC/DC0%0/2
$top and not $found
$top
not $found
Truth table0%0/3
A and not(B)
A= $topB= not $found
| |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | $found++ if ($key,$value) = $top->next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | if (!$found) { # this iterator is done | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
ref $value and not exists $value->{".name"}
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"}
| |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | if (%{$value}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 127 | 0 | $self->reset; | |||||||||||||||||||||||||||
| 128 | 0 | return (); | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | 0 | ▶ | return ($key,$value) if $short_keys; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 163 | 0 | $self->{hashindex}=++$hashi; | |||||||||||||||||||||||||||
| 164 | 0 | $self->{arrayindex}=$arrayi=0; | |||||||||||||||||||||||||||
| 165 | 0 | ▶ | unless (defined($keys->[$hashi]) && | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $keys->[$hashi] and defined $stone->get($keys->[$hashi], $arrayi)
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)
| |||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||