--- trunk/lib/WebPAC/Input.pm 2006/02/26 23:21:50 416 +++ trunk/lib/WebPAC/Input.pm 2006/05/21 19:29:26 523 @@ -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.05 =cut -our $VERSION = '0.04'; +our $VERSION = '0.05'; =head1 SYNOPSIS @@ -65,6 +67,7 @@ code_page => 'ISO-8859-2', low_mem => 1, recode => 'char pairs', + no_progress_bar => 1, ); C is low-level file format module. See L and @@ -76,6 +79,11 @@ Default is not to use C options (see L below). +C is optional string constisting of character or words pairs that +should be replaced in input stream. + +C disables progress bar output on C + This function will also call low-level C if it exists with same parametars. @@ -154,6 +162,7 @@ limit => 500, offset => 6000, lookup => $lookup_obj, + stats => 1, ); By default, C is assumed to be C<852>. @@ -162,6 +171,8 @@ C is optional parametar to read just C records from database +C create optional report about usage of fields and subfields + Returns size of database, regardless of C and C parametars, see also C. @@ -216,9 +227,10 @@ my ($db, $size) = $self->{open_db}->( $self, path => $arg->{path}, filter => $filter_ref, + %{ $arg }, ); - unless ($db) { + unless (defined($db)) { $log->logwarn("can't open database $arg->{path}, skipping..."); return; } @@ -232,7 +244,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; @@ -247,7 +259,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->{code_page}", $self->{stats} ? ' [stats]' : ''); # read database for (my $pos = $from_rec; $pos <= $to_rec; $pos++) { @@ -273,7 +285,26 @@ # create lookup $self->{'lookup'}->add( $rec ) if ($rec && $self->{'lookup'}); - $self->progress_bar($pos,$to_rec); + # update counters for statistics + if ($self->{stats}) { + map { + my $fld = $_; + $self->{_stats}->{fld}->{ $fld }++; + if (ref($rec->{ $fld }) eq 'ARRAY') { + map { + if (ref($_) eq 'HASH') { + map { + $self->{_stats}->{sf}->{ $fld }->{ $_ }++; + } keys %{ $_ }; + } else { + $self->{_stats}->{repeatable}->{ $fld }++; + } + } @{ $rec->{$fld} }; + } + } keys %{ $rec }; + } + + $self->progress_bar($pos,$to_rec) unless ($self->{no_progress_bar}); } @@ -319,7 +350,7 @@ return; } - $self->progress_bar($mfn,$self->{max_pos}); + $self->progress_bar($mfn,$self->{max_pos}) unless ($self->{no_progress_bar}); my $rec; @@ -394,6 +425,53 @@ 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->{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