--- trunk/lib/WebPAC/Input.pm 2006/05/15 09:59:05 506 +++ trunk/lib/WebPAC/Input.pm 2006/07/05 19:52:45 585 @@ -3,6 +3,8 @@ use warnings; use strict; +use blib; + use WebPAC::Common; use base qw/WebPAC::Common/; use Text::Iconv; @@ -14,11 +16,11 @@ =head1 VERSION -Version 0.05 +Version 0.07 =cut -our $VERSION = '0.05'; +our $VERSION = '0.07'; =head1 SYNOPSIS @@ -42,7 +44,6 @@ my $db = WebPAC::Input->new( module => 'WebPAC::Input::ISIS', config => $config, - lookup => $lookup_obj, low_mem => 1, ); @@ -62,7 +63,7 @@ my $db = new WebPAC::Input( module => 'WebPAC::Input::MARC', - code_page => 'ISO-8859-2', + encoding => 'ISO-8859-2', low_mem => 1, recode => 'char pairs', no_progress_bar => 1, @@ -71,7 +72,7 @@ C is low-level file format module. See L and L. -Optional parametar C specify application code page (which will be +Optional parametar C specify application code page (which will be used internally). This should probably be your terminal encoding, and by default, it C. @@ -94,6 +95,9 @@ my $log = $self->_get_logger; + $log->logconfess("code_page argument is not suppored any more. change it to encoding") if ($self->{lookup}); + $log->logconfess("lookup argument is not suppored any more. rewrite call to lookup_ref") if ($self->{lookup}); + $log->logconfess("specify low-level file format module") unless ($self->{module}); my $module = $self->{module}; $module =~ s#::#/#g; @@ -119,7 +123,7 @@ $self->{init}->($self, @_); } - $self->{'code_page'} ||= 'ISO-8859-2'; + $self->{'encoding'} ||= 'ISO-8859-2'; # running with low_mem flag? well, use DBM::Deep then. if ($self->{'low_mem'}) { @@ -161,6 +165,10 @@ offset => 6000, lookup => $lookup_obj, stats => 1, + lookup_ref => sub { + my ($k,$v) = @_; + # store lookup $k => $v + }, ); By default, C is assumed to be C<852>. @@ -171,6 +179,9 @@ C create optional report about usage of fields and subfields +C is closure to call when adding key => $value combinations to +lookup. + Returns size of database, regardless of C and C parametars, see also C. @@ -182,6 +193,10 @@ my $log = $self->_get_logger(); + $log->logconfess("lookup argument is not suppored any more. rewrite call to lookup_coderef") if ($arg->{lookup}); + $log->logconfess("lookup_coderef must be CODE, not ",ref($arg->{lookup_coderef})) + if ($arg->{lookup_coderef} && ref($arg->{lookup_coderef}) ne 'CODE'); + $log->logcroak("need path") if (! $arg->{'path'}); my $code_page = $arg->{'code_page'} || '852'; @@ -192,7 +207,7 @@ } # create Text::Iconv object - $self->{iconv} = Text::Iconv->new($code_page,$self->{'code_page'}); + $self->{iconv} = Text::Iconv->new($code_page,$self->{'encoding'}); ## FIXME remove! my $filter_ref; @@ -225,6 +240,7 @@ my ($db, $size) = $self->{open_db}->( $self, path => $arg->{path}, filter => $filter_ref, + %{ $arg }, ); unless (defined($db)) { @@ -241,7 +257,7 @@ my $to_rec = $size; if (my $s = $self->{offset}) { - $log->info("skipping to MFN $s"); + $log->debug("skipping to MFN $s"); $from_rec = $s; } else { $self->{offset} = $from_rec; @@ -256,7 +272,7 @@ # store size for later $self->{size} = ($to_rec - $from_rec) ? ($to_rec - $from_rec + 1) : 0; - $log->info("processing $self->{size}/$size records [$from_rec-$to_rec] convert $code_page -> $self->{code_page}", $self->{stats} ? ' [stats]' : ''); + $log->info("processing $self->{size}/$size records [$from_rec-$to_rec] convert $code_page -> $self->{encoding}", $self->{stats} ? ' [stats]' : ''); # read database for (my $pos = $from_rec; $pos <= $to_rec; $pos++) { @@ -280,7 +296,7 @@ } # create lookup - $self->{'lookup'}->add( $rec ) if ($rec && $self->{'lookup'}); + $arg->{'lookup_coderef'}->( $rec ) if ($rec && $arg->{'lookup_coderef'}); # update counters for statistics if ($self->{stats}) { @@ -426,13 +442,48 @@ Dump statistics about field and subfield usage - print Dumper( $input->stats ); + print $input->stats; =cut sub stats { my $self = shift; - return $self->{_stats}; + + my $log = $self->_get_logger(); + + my $s = $self->{_stats}; + if (! $s) { + $log->warn("called stats, but there is no statistics collected"); + return; + } + + my $max_fld = 0; + + my $out = join("\n", + map { + my $f = $_ || die "no field"; + my $v = $s->{fld}->{$f} || die "no s->{fld}->{$f}"; + $max_fld = $v if ($v > $max_fld); + + my $o = sprintf("%4s %d ~", $f, $v); + + if (defined($s->{sf}->{$f})) { + map { + $o .= sprintf(" %s:%d", $_, $s->{sf}->{$f}->{$_}); + } sort keys %{ $s->{sf}->{$f} }; + } + + if (my $v_r = $s->{repeatable}->{$f}) { + $o .= " ($v_r)" if ($v_r != $v); + } + + $o; + } sort { $a cmp $b } keys %{ $s->{fld} } + ); + + $log->debug( sub { Dumper($s) } ); + + return $out; } =head1 MEMORY USAGE