line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::Stag::HashDB; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Data::Stag::HashDB - build indexes over Stag files or objects |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 SYNOPSIS |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
# parsing a file into a hash |
10
|
|
|
|
|
|
|
my $hdb = Data::Stag::HashDB->new; |
11
|
|
|
|
|
|
|
$hdb->unique_key("ss_details/social_security_no"); |
12
|
|
|
|
|
|
|
$hdb->record_type("person"); |
13
|
|
|
|
|
|
|
my $obj = {}; |
14
|
|
|
|
|
|
|
$hdb->index_hash($obj); |
15
|
|
|
|
|
|
|
Data::Stag->parse(-file=>$fn, -handler=>$hdb); |
16
|
|
|
|
|
|
|
my $person = $obj->{'999-9999-9999'}; |
17
|
|
|
|
|
|
|
print $person->xml; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# indexing an existing stag tree into a hash |
20
|
|
|
|
|
|
|
my $personset = Data::Stag->parse($fn); |
21
|
|
|
|
|
|
|
my $hdb = Data::Stag::HashDB->new; |
22
|
|
|
|
|
|
|
$hdb->unique_key("ss_details/social_security_no"); |
23
|
|
|
|
|
|
|
$hdb->record_type("person"); |
24
|
|
|
|
|
|
|
my $obj = {}; |
25
|
|
|
|
|
|
|
$hdb->index_hash($obj); |
26
|
|
|
|
|
|
|
$personset->sax($hdb); |
27
|
|
|
|
|
|
|
my $person = $obj->{'999-9999-9999'}; |
28
|
|
|
|
|
|
|
print $person->xml; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=cut |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=head1 DESCRIPTION |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
Used for building indexes over Stag files or objects |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
You need to provide a B - this is the type of element |
38
|
|
|
|
|
|
|
that will be indexed |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
You need to provide a N - this is a single value used to |
41
|
|
|
|
|
|
|
index the Bs |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
For example, if we have data in the stag structure below, and if ss_no |
44
|
|
|
|
|
|
|
is unique (we assume it is) then we can index all the people in the |
45
|
|
|
|
|
|
|
database using the code above |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
publicinfo: |
48
|
|
|
|
|
|
|
persondata: |
49
|
|
|
|
|
|
|
person: |
50
|
|
|
|
|
|
|
ss_details: |
51
|
|
|
|
|
|
|
social_security_no: |
52
|
|
|
|
|
|
|
name: |
53
|
|
|
|
|
|
|
address: |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
There is a subclass of this method callsed Data::Stag::StagDB, which |
56
|
|
|
|
|
|
|
makes the hash persistent |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head1 PUBLIC METHODS - |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=cut |
61
|
|
|
|
|
|
|
|
62
|
1
|
|
|
1
|
|
1792
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
40
|
|
63
|
1
|
|
|
1
|
|
6
|
use base qw(Data::Stag::BaseHandler); |
|
1
|
|
|
|
|
1403
|
|
|
1
|
|
|
|
|
123
|
|
64
|
1
|
|
|
1
|
|
8
|
use Data::Stag qw(:all); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
2645
|
|
65
|
|
|
|
|
|
|
|
66
|
1
|
|
|
1
|
|
8
|
use vars qw($VERSION); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
626
|
|
67
|
|
|
|
|
|
|
$VERSION="0.14"; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub init { |
70
|
1
|
|
|
1
|
0
|
4
|
my $self = shift; |
71
|
1
|
|
|
|
|
10
|
$self->SUPER::init(@_); |
72
|
1
|
|
|
|
|
6
|
$self->nextid(0); |
73
|
1
|
|
|
|
|
3
|
return $self; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head2 record_type |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
Usage - |
80
|
|
|
|
|
|
|
Returns - |
81
|
|
|
|
|
|
|
Args - |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=cut |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub record_type { |
86
|
76
|
|
|
76
|
1
|
98
|
my $self = shift; |
87
|
76
|
100
|
|
|
|
146
|
$self->{_record_type} = shift if @_; |
88
|
76
|
|
50
|
|
|
257
|
return $self->{_record_type} || ''; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head2 unique_key |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
Usage - |
94
|
|
|
|
|
|
|
Returns - |
95
|
|
|
|
|
|
|
Args - |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=cut |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub unique_key { |
100
|
7
|
|
|
7
|
1
|
33
|
my $self = shift; |
101
|
7
|
100
|
|
|
|
25
|
$self->{_unique_key} = shift if @_; |
102
|
7
|
|
|
|
|
16
|
return $self->{_unique_key}; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head2 index_hash |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Usage - |
109
|
|
|
|
|
|
|
Returns - |
110
|
|
|
|
|
|
|
Args - |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=cut |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub index_hash { |
115
|
6
|
|
|
6
|
1
|
15
|
my $self = shift; |
116
|
6
|
100
|
|
|
|
18
|
$self->{_index_hash} = shift if @_; |
117
|
6
|
50
|
|
|
|
15
|
if (!$self->{_index_hash}) { |
118
|
0
|
|
|
|
|
0
|
$self->{_index_hash} = {}; |
119
|
|
|
|
|
|
|
} |
120
|
6
|
|
|
|
|
13
|
return $self->{_index_hash}; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub nextid { |
125
|
1
|
|
|
1
|
0
|
2
|
my $self = shift; |
126
|
1
|
50
|
|
|
|
5
|
if (@_) { |
127
|
1
|
|
|
|
|
9
|
$self->{_nextid} = shift; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
else { |
130
|
0
|
0
|
|
|
|
0
|
$self->{_nextid} = 0 unless $self->{_nextid}; |
131
|
0
|
|
|
|
|
0
|
$self->{_nextid}++; |
132
|
|
|
|
|
|
|
} |
133
|
1
|
|
|
|
|
4
|
return $self->{_nextid}; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub end_event { |
137
|
74
|
|
|
74
|
1
|
93
|
my $self = shift; |
138
|
74
|
|
|
|
|
85
|
my $ev = shift; |
139
|
74
|
100
|
|
|
|
133
|
if ($ev eq $self->record_type) { |
140
|
5
|
|
|
|
|
27
|
my $topnode = $self->popnode; |
141
|
5
|
|
|
|
|
35
|
$self->add_record(stag_stagify($topnode)); |
142
|
|
|
|
|
|
|
# my $name_elt = $self->unique_key; |
143
|
|
|
|
|
|
|
# my $name; |
144
|
|
|
|
|
|
|
# if ($name_elt) { |
145
|
|
|
|
|
|
|
# $name = stag_get($topnode, $name_elt); |
146
|
|
|
|
|
|
|
# } |
147
|
|
|
|
|
|
|
# if (!$name) { |
148
|
|
|
|
|
|
|
# $name = $ev."_".$self->nextid; |
149
|
|
|
|
|
|
|
# } |
150
|
|
|
|
|
|
|
# $self->index_hash->{$name} = stag_stagify($topnode); |
151
|
5
|
|
|
|
|
13
|
return []; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
else { |
154
|
69
|
|
|
|
|
225
|
return $self->SUPER::end_event($ev, @_); |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub add_record { |
159
|
5
|
|
|
5
|
0
|
8
|
my $self = shift; |
160
|
5
|
|
|
|
|
7
|
my $record = shift; |
161
|
|
|
|
|
|
|
|
162
|
5
|
|
|
|
|
11
|
my $idx = $self->index_hash; |
163
|
5
|
|
|
|
|
14
|
my $ukey = $self->unique_key; |
164
|
5
|
|
|
|
|
5
|
my $keyval; |
165
|
5
|
50
|
|
|
|
13
|
if ($ukey) { |
166
|
5
|
|
|
|
|
22
|
$keyval = stag_get($record, $ukey); |
167
|
|
|
|
|
|
|
} |
168
|
5
|
50
|
|
|
|
13
|
if (!$keyval) { |
169
|
0
|
|
|
|
|
0
|
$keyval = $record->name."_".$self->nextid; |
170
|
|
|
|
|
|
|
} |
171
|
5
|
50
|
|
|
|
20
|
$idx->{$keyval} = [] unless $idx->{$keyval}; |
172
|
5
|
|
|
|
|
9
|
my $vals = $idx->{$keyval}; |
173
|
5
|
|
|
|
|
7
|
push(@$vals, $record); |
174
|
5
|
|
|
|
|
8
|
$idx->{$keyval} = $vals; |
175
|
5
|
|
|
|
|
9
|
return; |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
sub get_record { |
179
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
180
|
0
|
|
|
|
|
|
my $keyval = shift; |
181
|
0
|
|
0
|
|
|
|
my $records = $self->index_hash->{$keyval} || []; |
182
|
0
|
0
|
|
|
|
|
if (wantarray) { |
183
|
0
|
|
|
|
|
|
return @$records; |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
else { |
186
|
0
|
|
|
|
|
|
return $records->[0]; |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
sub reset { |
191
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
192
|
0
|
|
|
|
|
|
%{$self->index_hash} = (); |
|
0
|
|
|
|
|
|
|
193
|
0
|
|
|
|
|
|
return; |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
1; |
197
|
|
|
|
|
|
|
|