| 1 | package Data::Stag::StagDB; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Data::Stag::StagDB - persistent storage and retrieval of stag nodes | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | # parsing a file into a file based index | ||
| 10 | my $sdb = Data::Stag::StagDB->new; | ||
| 11 | $sdb->unique_key("ss_details/social_security_no"); | ||
| 12 | $sdb->record_type("person"); | ||
| 13 | $sdb->indexfile("./person_by_ss-idx"); | ||
| 14 | Data::Stag->parse(-file=>$fn, -handler=>$sdb); | ||
| 15 | my $obj = $sdb->index_hash; | ||
| 16 | my $person = $obj->{'999-9999-9999'}; | ||
| 17 | print $person->xml; | ||
| 18 | |||
| 19 | # indexing an existing stag tree into a file based index | ||
| 20 | my $personset = Data::Stag->parse($fn); | ||
| 21 | my $sdb = Data::Stag::StagDB->new; | ||
| 22 | $sdb->unique_key("ss_details/social_security_no"); | ||
| 23 | $sdb->record_type("person"); | ||
| 24 | $sdb->indexfile("./person_by_ss-idx"); | ||
| 25 | $personset->sax($sdb); | ||
| 26 | my $obj = $sdb->index_hash; | ||
| 27 | my $person = $obj->{'999-9999-9999'}; | ||
| 28 | print $person->xml; | ||
| 29 | |||
| 30 | |||
| 31 | =cut | ||
| 32 | |||
| 33 | =head1 DESCRIPTION | ||
| 34 | |||
| 35 | This module is an extension of L<Data::Stag::HashDB> - you can use it | ||
| 36 | in the same way. | ||
| 37 | |||
| 38 | It creates a simple file based database of stag nodes | ||
| 39 | |||
| 40 | This is useful if you want your data to persist; or if you want to use L<Data::Stag::HashDB> but your data will not fit into memory | ||
| 41 | |||
| 42 | =head1 PUBLIC METHODS - | ||
| 43 | |||
| 44 | =cut | ||
| 45 | |||
| 46 | use strict; | ||
| 47 | use base qw(Data::Stag::HashDB); | ||
| 48 | use Data::Stag qw(:all); | ||
| 49 | use MLDBM qw(DB_File Storable); | ||
| 50 | use Fcntl; | ||
| 51 | |||
| 52 | use vars qw($VERSION); | ||
| 53 | $VERSION="0.14"; | ||
| 54 | |||
| 55 | sub init { | ||
| 56 | my $self = shift; | ||
| 57 | $self->SUPER::init(@_); | ||
| 58 | return $self; | ||
| 59 | } | ||
| 60 | |||
| 61 | |||
| 62 | =head2 indexfile | ||
| 63 | |||
| 64 | Usage - | ||
| 65 | Returns - | ||
| 66 | Args - | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | sub indexfile { | ||
| 71 | my $self = shift; | ||
| 72 | $self->{_indexfile} = shift if @_; | ||
| 73 | return $self->{_indexfile}; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub index_hash { | ||
| 77 | my $self = shift; | ||
| 78 | $self->{_index_hash} = shift if @_; | ||
| 79 | if (!$self->{_index_hash}) { | ||
| 80 | my %obj = (); | ||
| 81 | my $f = $self->indexfile || 'tmp-stagdb'; | ||
| 82 | my $dbm = tie %obj, 'MLDBM', $f, O_CREAT|O_RDWR, 0640 or die $!; | ||
| 83 | $self->dbm($dbm); | ||
| 84 | $self->{_index_hash} = \%obj; | ||
| 85 | } | ||
| 86 | return $self->{_index_hash}; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub dbm { | ||
| 90 | my $self = shift; | ||
| 91 | $self->{_dbm} = shift if @_; | ||
| 92 | return $self->{_dbm}; | ||
| 93 | } | ||
| 94 | |||
| 95 | |||
| 96 | 1; | ||
| 97 |