r1045@llin: dpavlin | 2006-09-29 21:38:42 +0200
[webpac2] / lib / WebPAC / Input / MARC.pm
index fdcce36..987bab1 100644 (file)
@@ -3,10 +3,8 @@ package WebPAC::Input::MARC;
 use warnings;
 use strict;
 
-use blib;
-
-use WebPAC::Common;
-use MARC::Fast;
+use MARC::Fast 0.03;
+use base qw/WebPAC::Common/;
 
 =head1 NAME
 
@@ -14,11 +12,11 @@ WebPAC::Input::MARC - support for MARC database files
 
 =head1 VERSION
 
-Version 0.01
+Version 0.06
 
 =cut
 
-our $VERSION = '0.01';
+our $VERSION = '0.06';
 
 
 =head1 SYNOPSIS
@@ -26,23 +24,27 @@ our $VERSION = '0.01';
 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' );
+ my $marc = new WebPAC::Input::MARC(
+       path => '/path/to/marc.iso'
+ );
 
 =head1 FUNCTIONS
 
-=head2 open_db
+=head2 new
 
-Returns handle to database
+Returns new low-level input API object
 
-  my $db = $open_db(
-       path => '/path/to/marc.iso'
+  my $marc = new WebPAC::Input::MARC(
+       path => '/path/to/marc.iso',
+       filter => \&code_ref,
   }
 
 =cut
 
-sub open_db {
-       my $self = shift;
+sub new {
+       my $class = shift;
+       my $self = {@_};
+       bless($self, $class);
 
        my $arg = {@_};
 
@@ -50,17 +52,23 @@ sub open_db {
 
        $log->info("opening MARC database '$arg->{path}'");
 
-       my $db = new MARC::Fast( marcdb => $arg->{path});
-       my $db_size = $db->count;
+       my $db = new MARC::Fast(
+               marcdb => $arg->{path},
+               hash_filter => $arg->{filter},
+       );
+       my $db_size = $db->count - 1;   # FIXME
+
+       $self->{_marc_size} = $db_size;
+       $self->{_marc_db} = $db;
 
-       return ($db, $db_size);
+       $self ? return $self : return undef;
 }
 
 =head2 fetch_rec
 
 Return record with ID C<$mfn> from database
 
-  my $rec = $self->fetch_rec( $db, $mfn );
+  my $rec = $self->fetch_rec( $mfn );
 
 }
 
@@ -69,11 +77,31 @@ Return record with ID C<$mfn> from database
 sub fetch_rec {
        my $self = shift;
 
-       my ($db, $mfn) = @_;
+       my $mfn = shift;
+
+       if ($mfn > $self->{_marc_size}) {
+               $self->_get_logger()->warn("seek beyond database size $self->{_marc_size} to $mfn");
+       } else {
+               my $row = $self->{_marc_db}->to_hash($mfn);
+               push @{$row->{'000'}}, $mfn;
+               return $row;
+       }
+}
+
+=head2 size
 
-       return $db->fetch($mfn);
+Return number of records in database
+
+  my $size = $isis->size;
+
+=cut
+
+sub size {
+       my $self = shift;
+       return $self->{_marc_size};
 }
 
+
 =head1 AUTHOR
 
 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>