=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
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>.
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')) {
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
# 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});
}
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;
}
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;
sub pos {
my $self = shift;
- return $self->{'current_mfn'};
+ return $self->{pos};
}
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
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);
}
$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
);
);
}
- 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";
+++ /dev/null
-#!/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);
-
--- /dev/null
+#!/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);
+