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