--- nfo/perl/libs/Data/Rap/Metadata.pm 2003/02/20 20:04:35 1.2 +++ nfo/perl/libs/Data/Rap/Metadata.pm 2004/06/20 23:00:23 1.5 @@ -1,7 +1,17 @@ ## ---------------------------------------------------------------------- -## $Id: Metadata.pm,v 1.2 2003/02/20 20:04:35 joko Exp $ +## $Id: Metadata.pm,v 1.5 2004/06/20 23:00:23 joko Exp $ ## ---------------------------------------------------------------------- ## $Log: Metadata.pm,v $ +## Revision 1.5 2004/06/20 23:00:23 joko +## minor fix: now creates cache-file in same directory where rap.xml is +## +## Revision 1.4 2004/06/20 16:12:24 joko +## + sub indexTargets +## modified getTargetDetails to use cached target details +## +## Revision 1.3 2003/03/29 07:13:19 joko +## better exception handling if target was not found in xml +## ## Revision 1.2 2003/02/20 20:04:35 joko ## renamed methods ## - refactored xml-specific code to Data::Storage::Handler::XML @@ -20,6 +30,8 @@ use Data::Dumper; use DesignPattern::Object; +use Storable; +use File::Basename qw( dirname ); sub accessMetadata { @@ -38,7 +50,7 @@ sub getTargetList { my $self = shift; - $self->log( "Reading Job Database from XML.", 'notice' ); + $self->log( "Reading database of targets (dot) from XML.", 'notice' ); # prepare access to metadata (assure instantiated storage handle) my $mdbe = $self->accessMetadata(); @@ -80,12 +92,70 @@ sub getTargetDetails { my $self = shift; my $target = shift; + my $options = shift; + + # check cache and return result from there + if (my $result = $self->{cache}->{targets}->{$target} and not $options->{force}) { + return $result; + } + + # get metadata for single task from storage my $mdbe = $self->accessMetadata(); $mdbe->sendQuery("*/target[\@name=\"$target\"]"); + + # FIXME: this is wrong behaviour! upper statement should return a proper + # result, which itself (already) has the method 'isEmpty' or similar... + if ($mdbe->isEmpty()) { + $self->log( "Target '$target' not found.", 'notice' ); + return; + } + my $tree = $mdbe->toEasyTree(); + + # trace + #print Dumper($tree); + #exit; + return $tree; } +sub indexTargets { + my $self = shift; + my $options = shift; + + # determine filename for cache-file + my $filename = 'rap.xml.cache'; + if (my $rapfile = $self->{__rap}->{filename}) { + if (my $path = dirname($rapfile)) { + $filename = $path . '/' . $filename; + } + } + + if ($options->{build}) { + $self->log("Building index of Targets", 'notice'); + # clear cache + delete $self->{cache}->{targets}; + my $list = $self->getTargetList(); + foreach my $target (@$list) { + $self->log("Reading target: $target->{name}", 'info'); + my $details = $self->getTargetDetails($target->{name}, { force => 1 }); + $self->{cache}->{targets}->{$target->{name}} = $details; + } + store $self->{cache}->{targets}, $filename; + + } elsif ($options->{load}) { + if (not -e $filename) { + return; + } + if ($self->{cache}->{targets} = retrieve($filename)) { + return 1; + } + + } else { + $self->log("Please call with options build or load", 'warning'); + + } +} 1; __END__