line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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 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 object. This |
31
|
|
|
|
|
|
|
will return an error if the object is not a L or a |
32
|
|
|
|
|
|
|
descendent. This method is usually not called directly, but rather |
33
|
|
|
|
|
|
|
indirectly via the L 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. 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 (in fact, a cursor |
44
|
|
|
|
|
|
|
is used internally to implement the B method. When the |
45
|
|
|
|
|
|
|
end of the B is reached, C 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. |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head1 AUTHOR |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
Lincoln D. Stein . |
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, L |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=cut |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
#------------------- Boulder::Cursor--------------- |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
*next_pair = \&each; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# New expects a Stone object as its single |
85
|
|
|
|
|
|
|
# parameter. |
86
|
|
|
|
|
|
|
sub new { |
87
|
0
|
|
|
0
|
1
|
|
my($package,$stone) = @_; |
88
|
0
|
0
|
|
|
|
|
die "Boulder::Cursor: expect a Stone object parameter" |
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
|
|
|
|
|
|
|
sub each { |
101
|
0
|
|
|
0
|
1
|
|
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}]; |
|
0
|
|
|
|
|
|
|
108
|
0
|
|
0
|
|
|
|
while ($top && !$found) { |
109
|
0
|
0
|
|
|
|
|
$found++ if ($key,$value) = $top->next; |
110
|
0
|
0
|
|
|
|
|
if (!$found) { # this iterator is done |
111
|
0
|
|
|
|
|
|
pop @{$stack}; |
|
0
|
|
|
|
|
|
|
112
|
0
|
|
|
|
|
|
$top = $stack->[$#{$stack}]; |
|
0
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
|
next; |
114
|
|
|
|
|
|
|
} |
115
|
0
|
0
|
0
|
|
|
|
if ( ref $value && !exists $value->{'.name'} ) { # found another record to begin iterating on |
116
|
0
|
0
|
|
|
|
|
if (%{$value}) { |
|
0
|
|
|
|
|
|
|
117
|
0
|
|
|
|
|
|
undef $found; |
118
|
0
|
|
|
|
|
|
$top = $value->cursor; |
119
|
0
|
|
|
|
|
|
push @{$stack},$top; |
|
0
|
|
|
|
|
|
|
120
|
0
|
|
|
|
|
|
next; |
121
|
|
|
|
|
|
|
} else { |
122
|
0
|
|
|
|
|
|
undef $value; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
} |
126
|
0
|
0
|
|
|
|
|
unless ($found) { |
127
|
0
|
|
|
|
|
|
$self->reset; |
128
|
0
|
|
|
|
|
|
return (); |
129
|
|
|
|
|
|
|
} |
130
|
0
|
0
|
|
|
|
|
return ($key,$value) if $short_keys; |
131
|
|
|
|
|
|
|
|
132
|
0
|
|
|
|
|
|
my @keylist = map {($_->{'keys'}->[$_->{'hashindex'}]) |
|
0
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
|
. "[" . ($_->{'arrayindex'}-1) ."]"; } @{$stack}; |
134
|
0
|
|
|
|
|
|
return (join(".",@keylist),$value); |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub reset { |
138
|
0
|
|
|
0
|
1
|
|
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
|
|
|
|
|
|
|
sub DESTROY { |
146
|
0
|
|
|
0
|
|
|
my $self = shift; |
147
|
0
|
0
|
|
|
|
|
if (ref $self->{'stone'}) { |
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
|
|
|
|
|
|
|
sub next { |
157
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
158
|
0
|
|
|
|
|
|
my($arrayi,$hashi,$stone,$keys) = ($self->{'arrayindex'}, |
159
|
|
|
|
|
|
|
$self->{'hashindex'}, |
160
|
|
|
|
|
|
|
$self->{'stone'}, |
161
|
|
|
|
|
|
|
$self->{'keys'}); |
162
|
0
|
0
|
|
|
|
|
unless ($stone->exists($keys->[$hashi],$arrayi)) { |
163
|
0
|
|
|
|
|
|
$self->{hashindex}=++$hashi; |
164
|
0
|
|
|
|
|
|
$self->{arrayindex}=$arrayi=0; |
165
|
0
|
0
|
0
|
|
|
|
unless (defined($keys->[$hashi]) && |
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
|
|
|
|
|
|
|
1; |