| 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<record_type> - this is the type of element | ||||||||||||
| 38 | that will be indexed | ||||||||||||
| 39 | |||||||||||||
| 40 | You need to provide a N<unique_key> - this is a single value used to | ||||||||||||
| 41 | index the B<record_type>s | ||||||||||||
| 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 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 63 | 1 | ▶ | use base qw(Data::Stag::BaseHandler); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 64 | 1 | ▶ | use Data::Stag qw(:all); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 65 | |||||||||||||
| 66 | 1 | ▶ | use vars qw($VERSION); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 67 | 1 | $VERSION="0.14"; | |||||||||||
| 68 | |||||||||||||
| 69 | 1 | ▶ | sub init { | ||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 70 | 1 | my $self = shift; | |||||||||||
| 71 | 1 | $self->SUPER::init(@_); | |||||||||||
| 72 | 1 | $self->nextid(0); | |||||||||||
| 73 | 1 | return $self; | |||||||||||
| 74 | } | ||||||||||||
| 75 | |||||||||||||
| 76 | |||||||||||||
| 77 | =head2 record_type | ||||||||||||
| 78 | |||||||||||||
| 79 | Usage - | ||||||||||||
| 80 | Returns - | ||||||||||||
| 81 | Args - | ||||||||||||
| 82 | |||||||||||||
| 83 | =cut | ||||||||||||
| 84 | |||||||||||||
| 85 | 76 | ▶ | sub record_type { | ||||||||||
|
Subroutine
record_type: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||
| 86 | 76 | my $self = shift; | |||||||||||
| 87 | 76 | ▶ | $self->{_record_type} = shift if @_; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 88 | 76 | ▶ | return $self->{_record_type} || ''; | ||||||||||
|
Condition50%1/2
$self->{'_record_type'} || ''
MC/DC0%0/1
$self->{'_record_type'} || ''
$self->{'_record_type'}
Truth table50%1/2
A
A= $self->{'_record_type'}
| |||||||||||||
| 89 | } | ||||||||||||
| 90 | |||||||||||||
| 91 | =head2 unique_key | ||||||||||||
| 92 | |||||||||||||
| 93 | Usage - | ||||||||||||
| 94 | Returns - | ||||||||||||
| 95 | Args - | ||||||||||||
| 96 | |||||||||||||
| 97 | =cut | ||||||||||||
| 98 | |||||||||||||
| 99 | 7 | ▶ | sub unique_key { | ||||||||||
|
Subroutine
unique_key: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 100 | 7 | my $self = shift; | |||||||||||
| 101 | 7 | ▶ | $self->{_unique_key} = shift if @_; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 102 | 7 | return $self->{_unique_key}; | |||||||||||
| 103 | } | ||||||||||||
| 104 | |||||||||||||
| 105 | |||||||||||||
| 106 | =head2 index_hash | ||||||||||||
| 107 | |||||||||||||
| 108 | Usage - | ||||||||||||
| 109 | Returns - | ||||||||||||
| 110 | Args - | ||||||||||||
| 111 | |||||||||||||
| 112 | =cut | ||||||||||||
| 113 | |||||||||||||
| 114 | 6 | ▶ | sub index_hash { | ||||||||||
|
Subroutine
index_hash: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||
| 115 | 6 | my $self = shift; | |||||||||||
| 116 | 6 | ▶ | $self->{_index_hash} = shift if @_; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 117 | 6 | ▶ | if (!$self->{_index_hash}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 118 | 0 | $self->{_index_hash} = {}; | |||||||||||
| 119 | } | ||||||||||||
| 120 | 6 | return $self->{_index_hash}; | |||||||||||
| 121 | } | ||||||||||||
| 122 | |||||||||||||
| 123 | |||||||||||||
| 124 | 1 | ▶ | sub nextid { | ||||||||||
|
Subroutine
nextid: called CC=3 SCAR=14.2
Pod
undocumented
| |||||||||||||
| 125 | 1 | my $self = shift; | |||||||||||
| 126 | 1 | ▶ | if (@_) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 127 | 1 | $self->{_nextid} = shift; | |||||||||||
| 128 | } | ||||||||||||
| 129 | else { | ||||||||||||
| 130 | 0 | ▶ | $self->{_nextid} = 0 unless $self->{_nextid}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 131 | 0 | $self->{_nextid}++; | |||||||||||
| 132 | } | ||||||||||||
| 133 | 1 | return $self->{_nextid}; | |||||||||||
| 134 | } | ||||||||||||
| 135 | |||||||||||||
| 136 | 74 | ▶ | sub end_event { | ||||||||||
|
Subroutine
end_event: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 137 | 74 | my $self = shift; | |||||||||||
| 138 | 74 | my $ev = shift; | |||||||||||
| 139 | 74 | ▶ | if ($ev eq $self->record_type) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 140 | 5 | my $topnode = $self->popnode; | |||||||||||
| 141 | 5 | $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 | return []; | |||||||||||
| 152 | } | ||||||||||||
| 153 | else { | ||||||||||||
| 154 | 69 | return $self->SUPER::end_event($ev, @_); | |||||||||||
| 155 | } | ||||||||||||
| 156 | } | ||||||||||||
| 157 | |||||||||||||
| 158 | 5 | ▶ | sub add_record { | ||||||||||
|
Subroutine
add_record: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||||
| 159 | 5 | my $self = shift; | |||||||||||
| 160 | 5 | my $record = shift; | |||||||||||
| 161 | |||||||||||||
| 162 | 5 | my $idx = $self->index_hash; | |||||||||||
| 163 | 5 | my $ukey = $self->unique_key; | |||||||||||
| 164 | 5 | my $keyval; | |||||||||||
| 165 | 5 | ▶ | if ($ukey) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 166 | 5 | $keyval = stag_get($record, $ukey); | |||||||||||
| 167 | } | ||||||||||||
| 168 | 5 | ▶ | if (!$keyval) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 169 | 0 | $keyval = $record->name."_".$self->nextid; | |||||||||||
| 170 | } | ||||||||||||
| 171 | 5 | ▶ | $idx->{$keyval} = [] unless $idx->{$keyval}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 172 | 5 | my $vals = $idx->{$keyval}; | |||||||||||
| 173 | 5 | push(@$vals, $record); | |||||||||||
| 174 | 5 | $idx->{$keyval} = $vals; | |||||||||||
| 175 | 5 | return; | |||||||||||
| 176 | } | ||||||||||||
| 177 | |||||||||||||
| 178 | 0 | ▶ | sub get_record { | ||||||||||
|
Subroutine
get_record: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 179 | 0 | my $self = shift; | |||||||||||
| 180 | 0 | my $keyval = shift; | |||||||||||
| 181 | 0 | ▶ | my $records = $self->index_hash->{$keyval} || []; | ||||||||||
|
Condition0%0/2
$self->index_hash->{$keyval} || []
MC/DC0%0/1
$self->index_hash->{$keyval} || []
$self->index_hash->{$keyval}
Truth table0%0/2
A
A= $self->index_hash->{$keyval}
| |||||||||||||
| 182 | 0 | ▶ | if (wantarray) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 183 | 0 | return @$records; | |||||||||||
| 184 | } | ||||||||||||
| 185 | else { | ||||||||||||
| 186 | 0 | return $records->[0]; | |||||||||||
| 187 | } | ||||||||||||
| 188 | } | ||||||||||||
| 189 | |||||||||||||
| 190 | 0 | ▶ | sub reset { | ||||||||||
|
Subroutine
reset: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 191 | 0 | my $self = shift; | |||||||||||
| 192 | 0 | %{$self->index_hash} = (); | |||||||||||
| 193 | 0 | return; | |||||||||||
| 194 | } | ||||||||||||
| 195 | |||||||||||||
| 196 | 1 | 1; | |||||||||||
| 197 | |||||||||||||