MOBY

 view release on metacpan or  search on metacpan

lib/MOBY/RDF/Ontologies/Cache/CacheUtils.pm  view on Meta::CPAN

use strict;
use DirHandle;

# names of cache directories/files/locks
use constant LIST_FILE          => '__L__I__S__T__';
use constant RDF_FILE           => '__R__D__F__';
use constant UPDATE_FILE        => '__U__P__D__A__T__E__F__I__L__E__';
use constant DATATYPES_CACHE    => 'dataTypes';
use constant SERVICES_CACHE     => 'services';
use constant NAMESPACES_CACHE   => 'namespaces';
use constant SERVICETYPES_CACHE => 'serviceTypes';

use vars qw /$VERSION/;
$VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /: (\d+)\.(\d+)/;

=head1 NAME

MOBY::RDF::Ontologies::Cache::CacheUtils - Utility module that aids in caching 

=head1 SYNOPSIS

lib/MOBY/RDF/Ontologies/Cache/CacheUtils.pm  view on Meta::CPAN

									 SERVICES_CACHE
				 ),
				 File::Spec->catdir(
									 $self->cachedir,
									 $self->_clean( $self->_endpoint ),
									 NAMESPACES_CACHE
				 ),
				 File::Spec->catdir(
									 $self->cachedir,
									 $self->_clean( $self->_endpoint ),
									 SERVICETYPES_CACHE
				 ),
	);

	foreach my $file (@dirs) {
		my ( $v, $d, $f ) = File::Spec->splitpath($file);
		my $dir = File::Spec->catdir($v);
		foreach my $part ( File::Spec->splitdir( ( $d . $f ) ) ) {
			$dir = File::Spec->catdir( $dir, $part );
			next if -d $dir or -e $dir;
			mkdir($dir)

lib/MOBY/RDF/Ontologies/Cache/CacheUtils.pm  view on Meta::CPAN

			 SERVICES_CACHE => File::Spec->catdir(
											  $self->cachedir,
											  $self->_clean( $self->_endpoint ),
											  SERVICES_CACHE
			 ),
			 NAMESPACES_CACHE => File::Spec->catdir(
											  $self->cachedir,
											  $self->_clean( $self->_endpoint ),
											  NAMESPACES_CACHE
			 ),
			 SERVICETYPES_CACHE => File::Spec->catdir(
											  $self->cachedir,
											  $self->_clean( $self->_endpoint ),
											  SERVICETYPES_CACHE
			 ),
	};

}


sub plainfiles {
   my ($self, $dir )= @_;
   my $dh = DirHandle->new($dir)   or die "can't opendir $dir: $!";
   return sort                     # sort pathnames

lib/MOBY/RDF/Ontologies/Cache/ServiceTypeCache.pm  view on Meta::CPAN

		my $name = $nodes->get_node($_)->getAttribute('name');
		next if $authorities_completed{$name};
		$authorities_completed{$name} = 1;

		$xml = MOBY::RDF::Ontologies::ServiceTypes->new(
										   endpoint => $self->{utils}->_endpoint );
		$xml = $xml->createByName( { term => $name });
		my $file = File::Spec->catfile(
							$self->{utils}->cachedir,
							$self->{utils}->_clean( $self->{utils}->_endpoint ),
							$self->{utils}->SERVICETYPES_CACHE,
							$name
		);
		open( FILE, ">$file" )
		  or die("Can't open file '$file' for writing: $!");
		print FILE $xml;
		close FILE;
	}
}

#-----------------------------------------------------------------

lib/MOBY/RDF/Ontologies/Cache/ServiceTypeCache.pm  view on Meta::CPAN

	my $wasOld           = 0;
	my %old_services     = ();
	my %new_services     = ();
	my %changed_services = ();

	if (
		 !(
			-e File::Spec->catfile(
							$self->{utils}->cachedir,
							$self->{utils}->_clean( $self->{utils}->_endpoint ),
							$self->{utils}->SERVICETYPES_CACHE
			)
		 )
	  )
	{
		$self->create_service_type_cache;
		return -1;
	}

	if (
		 !(
			-e File::Spec->catfile(
							$self->{utils}->cachedir,
							$self->{utils}->_clean( $self->{utils}->_endpoint ),
							$self->{utils}->SERVICETYPES_CACHE,
							$self->{utils}->LIST_FILE
			)
		 )
	  )
	{
		warn(     "service type LIST_FILE doesn't exist, so I created the cache from scratch!"
		);
		$self->create_service_type_cache;
		return -1;
	}

	# steps:
	# read in the LIST file and extract lsids for all datatypes
	my $file = File::Spec->catfile(
							$self->{utils}->cachedir,
							$self->{utils}->_clean( $self->{utils}->_endpoint ),
							$self->{utils}->SERVICETYPES_CACHE,
							$self->{utils}->LIST_FILE
	);
	my $parser = XML::LibXML->new();
	my $doc;
	eval {
		$doc    = $parser->parse_file($file);
	};
	warn "There was something wrong with '$file' and we couldn't parse it.\nWill attempt to create from scratch.\n" if $@;
	$doc = $parser->parse_string($self->_create_list_file) if $@;
	

lib/MOBY/RDF/Ontologies/Cache/ServiceTypeCache.pm  view on Meta::CPAN

		$nodes  = undef;
		foreach my $authURI ( keys %changed_services ) {
			$wasOld++;
			$xml = MOBY::RDF::Ontologies::ServiceTypes->new(
										  endpoint => $self->{utils}->_endpoint, );

			$xml = $xml->createByName( { term => $authURI });
			$file = File::Spec->catfile(
							$self->{utils}->cachedir,
							$self->{utils}->_clean( $self->{utils}->_endpoint ),
							$self->{utils}->SERVICETYPES_CACHE,
							$authURI
			);
			open( FILE, ">$file" )
			  or die("Can't open file '$file' for writing: $!");
			print FILE $xml;
			close FILE;
		}
	}
	
	# remove any old files that should not be cached
	my $cachedir = File::Spec->catfile(
							$self->{utils}->cachedir,
							$self->{utils}->_clean( $self->{utils}->_endpoint ),
							$self->{utils}->SERVICETYPES_CACHE
	);

	eval {
		my @files = $self->{utils}->plainfiles($cachedir);
		foreach my $path (@files) {
			my $filename = substr $path, length($cachedir)+1;
			
			next if -d $filename;
			next
			  if $filename eq $self->{utils}->RDF_FILE

lib/MOBY/RDF/Ontologies/Cache/ServiceTypeCache.pm  view on Meta::CPAN

	* There was a problem parsing XML

=cut

sub get_rdf {
	my ($self) = @_;
	my $xml = "";
	my $lock = File::Spec->catfile(
							$self->{utils}->cachedir,
							$self->{utils}->_clean( $self->{utils}->_endpoint ),
							$self->{utils}->SERVICETYPES_CACHE,
							$self->{utils}->UPDATE_FILE
	);

	my $file = File::Spec->catfile(
							$self->{utils}->cachedir,
							$self->{utils}->_clean( $self->{utils}->_endpoint ),
							$self->{utils}->SERVICETYPES_CACHE,
							$self->{utils}->RDF_FILE
	);
	my $dir = File::Spec->catfile(
							$self->{utils}->cachedir,
							$self->{utils}->_clean( $self->{utils}->_endpoint ),
							$self->{utils}->SERVICETYPES_CACHE
	);

	open( LOCK, ">$lock" );
	flock( LOCK, LOCK_EX );
	eval {

		# check if we need to re-merge the RDF
		my $isStale = $self->update_service_type_cache;
		if ( $isStale or !( -e $file ) ) {
			

lib/MOBY/RDF/Ontologies/Cache/ServiceTypeCache.pm  view on Meta::CPAN

		}
	  );

	my $xml = $soap->retrieveServiceTypes()->result;

	# create cache dirs as needed
	$self->{utils}->create_cache_dirs;
	my $file = File::Spec->catfile(
							$self->{utils}->cachedir,
							$self->{utils}->_clean( $self->{utils}->_endpoint ),
							$self->{utils}->SERVICETYPES_CACHE,
							$self->{utils}->LIST_FILE
	);
	open( FILE, ">$file" )
	  or die("Can't open file '$file' for writing: $!");
	print FILE $xml;
	close FILE;
	
	return $xml;

}



( run in 0.541 second using v1.01-cache-2.11-cpan-49f99fa48dc )