package WebPAC::Input::MARC;

use warnings;
use strict;

use MARC::Fast;

=head1 NAME

WebPAC::Input::MARC - support for MARC database files

=head1 VERSION

Version 0.04

=cut

our $VERSION = '0.04';


=head1 SYNOPSIS

Open USMARC, Unimarc or any other file format that has same internal
structure using C<MARC::Fast>.

 my $marc = new WebPAC::Input::MARC();
 $marc->open( path => '/path/to/marc.iso' );

=head1 FUNCTIONS

=head2 open_db

Returns handle to database

  my $db = $open_db(
  	path => '/path/to/marc.iso'
  }

=cut

sub open_db {
	my $self = shift;

	my $arg = {@_};

	my $log = $self->_get_logger();

	$log->info("opening MARC database '$arg->{path}'");

	my $db = new MARC::Fast( marcdb => $arg->{path});
	my $db_size = $db->count - 1;	# FIXME

	$self->{_marc_size} = $db_size;

	return ($db, $db_size);
}

=head2 fetch_rec

Return record with ID C<$mfn> from database

  my $rec = $self->fetch_rec( $db, $mfn );

}

=cut

sub fetch_rec {
	my $self = shift;

	my ($db, $mfn) = @_;

	if ($mfn > $self->{_marc_size}) {
		$self->_get_logger()->warn("seek beyond database size $self->{_marc_size} to $mfn");
	} else {
		my $row = $db->to_hash($mfn);
		push @{$row->{'000'}}, $mfn;
		return $row;
	}
}

=head1 PROPERTIES

=head2 _marc_size

Store size of MARC database

  print $self->{_marc_size};



=head1 AUTHOR

Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>

=head1 COPYRIGHT & LICENSE

Copyright 2005 Dobrica Pavlinusic, All Rights Reserved.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut

1; # End of WebPAC::Input::MARC
