--- trunk/lib/WebPAC/Input.pm 2006/05/14 19:45:26 496 +++ trunk/lib/WebPAC/Input.pm 2006/07/09 15:22:39 593 @@ -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.04 +Version 0.08 =cut -our $VERSION = '0.04'; +our $VERSION = '0.08'; =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'}) { @@ -160,6 +164,11 @@ limit => 500, 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>. @@ -168,6 +177,11 @@ C is optional parametar to read just C records from database +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. @@ -179,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'; @@ -189,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; @@ -222,6 +240,7 @@ my ($db, $size) = $self->{open_db}->( $self, path => $arg->{path}, filter => $filter_ref, + %{ $arg }, ); unless (defined($db)) { @@ -238,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; @@ -253,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}"); + $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++) { @@ -277,7 +296,33 @@ } # 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}) { + + foreach my $fld (keys %{ $rec }) { + $self->{_stats}->{fld}->{ $fld }++; + + $log->logdie("invalid record fild $fld, not ARRAY") + unless (ref($rec->{ $fld }) eq 'ARRAY'); + + foreach my $row (@{ $rec->{$fld} }) { + + if (ref($row) eq 'HASH') { + + foreach my $sf (keys %{ $row }) { + $self->{_stats}->{sf}->{ $fld }->{ $sf }->{count}++; + $self->{_stats}->{sf}->{ $fld }->{ $sf }->{repeatable}++ + if (ref($row->{$sf}) eq 'ARRAY'); + } + + } else { + $self->{_stats}->{repeatable}->{ $fld }++; + } + } + } + } $self->progress_bar($pos,$to_rec) unless ($self->{no_progress_bar}); @@ -400,6 +445,56 @@ return $self->{pos} = (($pos - 1) || -1); } +=head2 stats + +Dump statistics about field and subfield usage + + print $input->stats; + +=cut + +sub stats { + my $self = shift; + + 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", $_, + $s->{sf}->{$f}->{$_}->{count}, + $s->{sf}->{$f}->{$_}->{repeatable} ? '*' : '', + ); + } 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