r11778@llin: dpavlin | 2005-12-19 03:59:54 +0100
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 18 Dec 2005 21:06:46 +0000 (21:06 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 18 Dec 2005 21:06:46 +0000 (21:06 +0000)
 move work on input

git-svn-id: svn+ssh://mjesec/home/dpavlin/svn/webpac2/trunk@286 07558da8-63fa-0310-ba24-9fe276d99e06

conf/log.conf
lib/WebPAC/Input.pm
lib/WebPAC/Input/ISIS.pm
run.pl
t/2-input-isis.t [deleted file]
t/2-input.t [new file with mode: 0755]

index e4285ff..4842d4a 100644 (file)
@@ -6,7 +6,8 @@ log4perl.rootLogger=INFO, LOG, SCREEN
 #, SCREEN
 
 # you can specify methods from WebPAC here also!
-log4perl.logger.main=INFO
+#log4perl.logger.main=INFO
+log4perl.logger.main=DEBUG
 
 #log4perl.logger.WebPAC=DEBUG
 
index 46d67ed..a962e1a 100644 (file)
@@ -11,39 +11,49 @@ use Text::Iconv;
 
 =head1 NAME
 
-WebPAC::Input - core module for input file format
+WebPAC::Input - read different file formats into WebPAC
 
 =head1 VERSION
 
-Version 0.02
+Version 0.03
 
 =cut
 
-our $VERSION = '0.02';
+our $VERSION = '0.03';
 
 =head1 SYNOPSIS
 
-This module is used as base class for all database specific modules
-(basically, files which have one handle, fixed size while indexing and some
-kind of numeric idefinirier which goes from 1 to filesize).
+This module implements input as database which have fixed and known
+I<size> while indexing and single unique numeric identifier for database
+position ranging from 1 to I<size>.
+
+Simply, something that is indexed by unmber from 1 .. I<size>.
+
+Examples of such databases are CDS/ISIS files, MARC files, lines in
+text file, and so on.
+
+Specific file formats are implemented using low-level interface modules,
+located in C<WebPAC::Input::*> namespace which export C<open_db>,
+C<fetch_rec> and optional C<init> functions.
 
 Perhaps a little code snippet.
 
     use WebPAC::Input;
 
     my $db = WebPAC::Input->new(
-       format => 'NULL',
-       config => $config,
-       lookup => $lookup_obj,
-       low_mem => 1,
+       module => 'WebPAC::Input::ISIS',
+               config => $config,
+               lookup => $lookup_obj,
+               low_mem => 1,
     );
 
     $db->open('/path/to/database');
     print "database size: ",$db->size,"\n";
-    while (my $row = $db->fetch) {
-       ...
+    while (my $rec = $db->fetch) {
     }
 
+
+
 =head1 FUNCTIONS
 
 =head2 new
@@ -51,11 +61,14 @@ Perhaps a little code snippet.
 Create new input database object.
 
   my $db = new WebPAC::Input(
-       format => 'NULL'
+       module => 'WebPAC::Input::MARC',
        code_page => 'ISO-8859-2',
        low_mem => 1,
   );
 
+C<module> is low-level file format module. See L<WebPAC::Input::Isis> and
+L<WebPAC::Input::MARC>.
+
 Optional parametar C<code_page> specify application code page (which will be
 used internally). This should probably be your terminal encoding, and by
 default, it C<ISO-8859-2>.
@@ -74,9 +87,21 @@ sub new {
 
        my $log = $self->_get_logger;
 
+       $log->logconfess("specify low-level file format module") unless ($self->{module});
+       my $module = $self->{module};
+       $module =~ s#::#/#g;
+       $module .= '.pm';
+       $log->debug("require low-level module $self->{module} from $module");
+       require $module;
+       eval $self->{module} .'->import';
+
        # check if required subclasses are implemented
        foreach my $subclass (qw/open_db fetch_rec/) {
-               $log->logdie("missing implementation of $subclass") unless ($self->SUPER::can($subclass));
+               if ( $self->can($subclass) ) {
+                       $log->debug("imported $subclass");
+               } else {
+                       $log->warn("missing $subclass in $self->{module}");
+               }
        }
 
        if ($self->can('init')) {
@@ -119,24 +144,22 @@ sub new {
 
 This function will read whole database in memory and produce lookups.
 
- $isis->open(
+ $input->open(
        path => '/path/to/database/file',
        code_page => '852',
-       limit_mfn => 500,
-       start_mfn => 6000,
+       limit => 500,
+       offset => 6000,
        lookup => $lookup_obj,
  );
 
 By default, C<code_page> is assumed to be C<852>.
 
-If optional parametar C<start_mfn> is set, this will be first MFN to read
-from database (so you can skip beginning of your database if you need to).
+C<offset> is optional parametar to position at some offset before reading from database.
 
-If optional parametar C<limit_mfn> is set, it will read just 500 records
-from database in example above.
+C<limit> is optional parametar to read just C<limit> records from database
 
-Returns size of database, regardless of C<start_mfn> and C<limit_mfn>
-parametars, see also C<$isis->size>.
+Returns size of database, regardless of C<offset> and C<limit>
+parametars, see also C<size>.
 
 =cut
 
@@ -151,7 +174,7 @@ sub open {
 
        # store data in object
        $self->{'code_page'} = $code_page;
-       foreach my $v (qw/path start_mfn limit_mfn/) {
+       foreach my $v (qw/path offset limit/) {
                $self->{$v} = $arg->{$v} if ($arg->{$v});
        }
 
@@ -172,60 +195,59 @@ sub open {
                return;
        }
 
-       my $startmfn = 1;
-       my $maxmfn = $size;
+       my $offset = 1;
+       my $limit = $size;
 
-       if (my $s = $self->{start_mfn}) {
+       if (my $s = $self->{offset}) {
                $log->info("skipping to MFN $s");
-               $startmfn = $s;
+               $offset = $s;
        } else {
-               $self->{start_mfn} = $startmfn;
+               $self->{offset} = $offset;
        }
 
-       if ($self->{limit_mfn}) {
-               $log->info("limiting to ",$self->{limit_mfn}," records");
-               $maxmfn = $startmfn + $self->{limit_mfn} - 1;
-               $maxmfn = $size if ($maxmfn > $size);
+       if ($self->{limit}) {
+               $log->info("limiting to ",$self->{limit}," records");
+               $limit = $offset + $self->{limit} - 1;
+               $limit = $size if ($limit > $size);
        }
 
        # store size for later
-       $self->{size} = ($maxmfn - $startmfn) ? ($maxmfn - $startmfn + 1) : 0;
+       $self->{size} = ($limit - $offset) ? ($limit - $offset + 1) : 0;
 
        $log->info("processing $self->{size} records in $code_page, convert to $self->{code_page}");
 
        # read database
-       for (my $mfn = $startmfn; $mfn <= $maxmfn; $mfn++) {
+       for (my $pos = $offset; $pos <= $limit; $mfn++) {
 
-               $log->debug("mfn: $mfn\n");
+               $log->debug("position: $pos\n");
 
-               my $rec = $self->fetch_rec( $db, $mfn );
+               my $rec = $self->fetch_rec( $db, $pos );
 
                if (! $rec) {
-                       $log->warn("record $mfn empty? skipping...");
+                       $log->warn("record $pos empty? skipping...");
                        next;
                }
 
                # store
-               if ($self->{'low_mem'}) {
-                       $self->{'db'}->put($mfn, $rec);
+               if ($self->{low_mem}) {
+                       $self->{db}->put($pos, $rec);
                } else {
-                       $self->{'data'}->{$mfn} = $rec;
+                       $self->{data}->{$pos} = $rec;
                }
 
                # create lookup
                $self->{'lookup'}->add( $rec ) if ($rec && $self->{'lookup'});
 
-               $self->progress_bar($mfn,$maxmfn);
+               $self->progress_bar($pos,$limit);
 
        }
 
-       $self->{'current_mfn'} = -1;
-       $self->{'last_pcnt'} = 0;
-
-       $log->debug("max mfn: $maxmfn");
+       $self->{pos} = -1;
+       $self->{last_pcnt} = 0;
 
        # store max mfn and return it.
-       $self->{'max_mfn'} = $maxmfn;
+       $self->{max_pos} = $limit;
+       $log->debug("max_pos: $limit");
 
        return $size;
 }
@@ -246,30 +268,30 @@ sub fetch {
 
        my $log = $self->_get_logger();
 
-       $log->logconfess("it seems that you didn't load database!") unless ($self->{'current_mfn'});
+       $log->logconfess("it seems that you didn't load database!") unless ($self->{pos});
 
-       if ($self->{'current_mfn'} == -1) {
-               $self->{'current_mfn'} = $self->{'start_mfn'};
+       if ($self->{pos} == -1) {
+               $self->{pos} = $self->{offset};
        } else {
-               $self->{'current_mfn'}++;
+               $self->{pos}++;
        }
 
-       my $mfn = $self->{'current_mfn'};
+       my $mfn = $self->{pos};
 
-       if ($mfn > $self->{'max_mfn'}) {
-               $self->{'current_mfn'} = $self->{'max_mfn'};
+       if ($mfn > $self->{max_pos}) {
+               $self->{pos} = $self->{max_pos};
                $log->debug("at EOF");
                return;
        }
 
-       $self->progress_bar($mfn,$self->{'max_mfn'});
+       $self->progress_bar($mfn,$self->{max_pos});
 
        my $rec;
 
-       if ($self->{'low_mem'}) {
-               $rec = $self->{'db'}->get($mfn);
+       if ($self->{low_mem}) {
+               $rec = $self->{db}->get($mfn);
        } else {
-               $rec = $self->{'data'}->{$mfn};
+               $rec = $self->{data}->{$mfn};
        }
 
        $rec ||= 0E0;
@@ -287,7 +309,7 @@ First record in database has position 1.
 
 sub pos {
        my $self = shift;
-       return $self->{'current_mfn'};
+       return $self->{pos};
 }
 
 
@@ -301,13 +323,13 @@ Result from this function can be used to loop through all records
 
  foreach my $mfn ( 1 ... $isis->size ) { ... }
 
-because it takes into account C<start_mfn> and C<limit_mfn>.
+because it takes into account C<offset> and C<limit>.
 
 =cut
 
 sub size {
        my $self = shift;
-       return $self->{'size'};
+       return $self->{size};
 }
 
 =head2 seek
@@ -329,12 +351,12 @@ sub seek {
        if ($pos < 1) {
                $log->warn("seek before first record");
                $pos = 1;
-       } elsif ($pos > $self->{'max_mfn'}) {
+       } elsif ($pos > $self->{max_pos}) {
                $log->warn("seek beyond last record");
-               $pos = $self->{'max_mfn'};
+               $pos = $self->{max_pos};
        }
 
-       return $self->{'current_mfn'} = (($pos - 1) || -1);
+       return $self->{pos} = (($pos - 1) || -1);
 }
 
 
index 786fb36..5cb7776 100644 (file)
@@ -3,10 +3,14 @@ package WebPAC::Input::ISIS;
 use warnings;
 use strict;
 
-use blib;
+require Exporter;
+use vars qw/@ISA @EXPORT/;
+@ISA = qw/Exporter/;
+@EXPORT = qw/init open_db fetch_rec/;
 
+use blib;
 use WebPAC::Input;
-use base qw/WebPAC::Input/;
+#use base qw/WebPAC::Input/;
 
 =head1 NAME
 
@@ -14,11 +18,11 @@ WebPAC::Input::ISIS - support for CDS/ISIS database files
 
 =head1 VERSION
 
-Version 0.02
+Version 0.03
 
 =cut
 
-our $VERSION = '0.02';
+our $VERSION = '0.03';
 
 
 =head1 SYNOPSIS
diff --git a/run.pl b/run.pl
index e80ebe5..8be3dac 100755 (executable)
--- a/run.pl
+++ b/run.pl
@@ -74,25 +74,36 @@ while (my ($database, $db_config) = each %{ $config->{databases} }) {
                $log->info("database $database doesn't have inputs defined");
        }
 
+       my @supported_inputs = keys %{ $config->{webpac}->{inputs} };
+
        foreach my $input (@inputs) {
 
                my $type = lc($input->{type});
 
-               die "I know only how to handle input type isis, not '$type'!\n" unless ($type eq 'isis');
+               die "I know only how to handle input types ", join(",", @supported_inputs), " not '$type'!\n" unless (grep(/$type/, @supported_inputs));
 
                my $lookup = new WebPAC::Lookup(
                        lookup_file => $input->{lookup},
                );
 
-               $log->info("working on input $input->{path} [$input->{type}]");
+               my $input_module = $config->{webpac}->{inputs}->{$type};
+
+               $log->info("working on input $input->{path} [$input->{type}] using $input_module");
 
-               my $isis = new WebPAC::Input::ISIS(
+               sub new_input {
+                       my $name = shift;
+                       my $args = shift;
+                       new $name->($args);
+               }
+
+               my $input = new_input($input_module,{
                        code_page => $config->{webpac}->{webpac_encoding},
                        limit_mfn => $input->{limit},
                        lookup => $lookup,
-               );
+               });
+               $log->logdie("can't create input using $input_module") unless ($input);
 
-               my $maxmfn = $isis->open(
+               my $maxmfn = $input->open(
                        path => $input->{path},
                        code_page => $input->{encoding},        # database encoding
                );
@@ -119,9 +130,9 @@ while (my ($database, $db_config) = each %{ $config->{databases} }) {
                        );
                }
 
-               for ( 0 ... $isis->size ) {
+               for ( 0 ... $input->size ) {
 
-                       my $row = $isis->fetch || next;
+                       my $row = $input->fetch || next;
 
                        my $mfn = $row->{'000'}->[0] || die "can't find MFN";
 
diff --git a/t/2-input-isis.t b/t/2-input-isis.t
deleted file mode 100755 (executable)
index d12d39c..0000000
+++ /dev/null
@@ -1,63 +0,0 @@
-#!/usr/bin/perl -w
-
-use Test::More tests => 48;
-use Test::Exception;
-use Cwd qw/abs_path/;
-use blib;
-use strict;
-
-BEGIN {
-use_ok( 'WebPAC::Input::ISIS' );
-}
-
-ok(my $abs_path = abs_path($0), "abs_path");
-$abs_path =~ s#/[^/]*$#/#;
-
-ok(my $isis = new WebPAC::Input::ISIS( no_log => 1 ), "new");
-ok(my $isis_lm = new WebPAC::Input::ISIS( low_mem => 1, no_log => 1 ), "new");
-
-throws_ok { $isis->open( ) } qr/path/, "need path";
-
-throws_ok { $isis->open( path => '/dev/null', ) } qr/can't find database/ , "open";
-
-ok($isis->open( path => "$abs_path/winisis/BIBL" ), "open");
-ok($isis_lm->open( path => "$abs_path/winisis/BIBL", low_mem => 1 ), "open");
-
-cmp_ok($isis->pos, '==', -1, "mfn");
-
-ok(my $size = $isis->size, "size");
-
-my @db1;
-
-foreach my $mfn ( 1 ... $size ) {
-       ok(my $rec = $isis->fetch, "fetch");
-       cmp_ok($isis->pos, '==', $mfn, "rec $mfn");
-       push @db1, $rec;
-}
-
-my @db2;
-
-foreach my $mfn ( 1 ... $size ) {
-       ok($isis_lm->seek($mfn), "seek");
-       ok(my $rec = $isis_lm->fetch, "fetch");
-       cmp_ok($isis_lm->pos, '==', $mfn, "rec $mfn");
-       push @db2, $rec;
-}
-
-is_deeply(\@db1, \@db2, "seek working");
-
-sub test_start_limit($$$) {
-       my ($s,$l,$e) = @_;
-
-       diag "start_mfn: $s, limit_mfn: $l, expected: $e";
-
-       ok($s = $isis->open( path => "$abs_path/winisis/BIBL", start_mfn => $s, limit_mfn => $l, debug => 1 ), "open");
-       cmp_ok($s, '==', $size, "db size from open");
-       cmp_ok($isis->size, '==', $e, "size");
-}
-
-test_start_limit(1, 3, 3);
-test_start_limit($size, 3, 0);
-test_start_limit(3, $size, $size - 2);
-test_start_limit(1, $size + 2, $size);
-
diff --git a/t/2-input.t b/t/2-input.t
new file mode 100755 (executable)
index 0000000..afc0b1a
--- /dev/null
@@ -0,0 +1,66 @@
+#!/usr/bin/perl -w
+
+use Test::More tests => 49;
+use Test::Exception;
+use Cwd qw/abs_path/;
+use blib;
+use strict;
+
+BEGIN {
+use_ok( 'WebPAC::Input::ISIS' );
+}
+
+ok(my $abs_path = abs_path($0), "abs_path");
+$abs_path =~ s#/[^/]*$#/#;
+
+my $module = 'WebPAC::Input::ISIS';
+
+throws_ok { my $input = new WebPAC::Input( ) } qr/module/, "need module";
+ok(my $input = new WebPAC::Input( module => $module, no_log => 0 ), "new");
+ok(my $input_lm = new WebPAC::Input( module => $module, low_mem => 1, no_log => 1 ), "new");
+
+throws_ok { $input->open( ) } qr/path/, "need path";
+
+throws_ok { $input->open( path => '/dev/null', ) } qr/can't find database/ , "open";
+
+ok($input->open( path => "$abs_path/winisis/BIBL" ), "open");
+ok($input_lm->open( path => "$abs_path/winisis/BIBL", low_mem => 1 ), "open");
+
+cmp_ok($input->pos, '==', -1, "mfn");
+
+ok(my $size = $input->size, "size");
+
+my @db1;
+
+foreach my $mfn ( 1 ... $size ) {
+       ok(my $rec = $input->fetch, "fetch");
+       cmp_ok($input->pos, '==', $mfn, "rec $mfn");
+       push @db1, $rec;
+}
+
+my @db2;
+
+foreach my $mfn ( 1 ... $size ) {
+       ok($input_lm->seek($mfn), "seek");
+       ok(my $rec = $input_lm->fetch, "fetch");
+       cmp_ok($input_lm->pos, '==', $mfn, "rec $mfn");
+       push @db2, $rec;
+}
+
+is_deeply(\@db1, \@db2, "seek working");
+
+sub test_start_limit($$$) {
+       my ($s,$l,$e) = @_;
+
+       diag "offset $s, limit: $l, expected: $e";
+
+       ok($s = $input->open( path => "$abs_path/winisis/BIBL", offset => $s, limit => $l, debug => 1 ), "open");
+       cmp_ok($s, '==', $size, "db size from open = $size");
+       cmp_ok($input->size, '==', $e, "input->size = $e");
+}
+
+test_start_limit(1, 3, 3);
+test_start_limit($size, 3, 0);
+test_start_limit(3, $size, $size - 2);
+test_start_limit(1, $size + 2, $size);
+