--- trunk/lib/WebPAC/Input.pm 2006/08/26 12:00:31 624 +++ trunk/lib/WebPAC/Input.pm 2006/10/25 15:56:44 760 @@ -16,11 +16,11 @@ =head1 VERSION -Version 0.11 +Version 0.13 =cut -our $VERSION = '0.11'; +our $VERSION = '0.13'; =head1 SYNOPSIS @@ -98,58 +98,20 @@ $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; - $module .= '.pm'; - $log->debug("require low-level module $self->{module} from $module"); + my $module_path = $self->{module}; + $module_path =~ s#::#/#g; + $module_path .= '.pm'; + $log->debug("require low-level module $self->{module} from $module_path"); - require $module; - #eval $self->{module} .'->import'; + require $module_path; # check if required subclasses are implemented - foreach my $subclass (qw/open_db fetch_rec init/) { - my $n = $self->{module} . '::' . $subclass; - if (! defined &{ $n }) { - my $missing = "missing $subclass in $self->{module}"; - $self->{$subclass} = sub { $log->logwarn($missing) }; - } else { - $self->{$subclass} = \&{ $n }; - } - } - - if ($self->{init}) { - $log->debug("calling init"); - $self->{init}->($self, @_); + foreach my $subclass (qw/open_db fetch_rec init dump_rec/) { + # FIXME } $self->{'encoding'} ||= 'ISO-8859-2'; - # running with low_mem flag? well, use DBM::Deep then. - if ($self->{'low_mem'}) { - $log->info("running with low_mem which impacts performance (<32 Mb memory usage)"); - - my $db_file = "data.db"; - - if (-e $db_file) { - unlink $db_file or $log->logdie("can't remove '$db_file' from last run"); - $log->debug("removed '$db_file' from last run"); - } - - require DBM::Deep; - - my $db = new DBM::Deep $db_file; - - $log->logdie("DBM::Deep error: $!") unless ($db); - - if ($db->error()) { - $log->logdie("can't open '$db_file' under low_mem: ",$db->error()); - } else { - $log->debug("using file '$db_file' for DBM::Deep"); - } - - $self->{'db'} = $db; - } - $self ? return $self : return undef; } @@ -162,16 +124,16 @@ code_page => 'cp852', limit => 500, offset => 6000, - lookup => $lookup_obj, stats => 1, - lookup_ref => sub { - my ($k,$v) = @_; - # store lookup $k => $v + lookup_coderef => sub { + my $rec = shift; + # store lookups }, modify_records => { 900 => { '^a' => { ' : ' => '^b' } }, 901 => { '*' => { '^b' => ' ; ' } }, }, + modify_file => 'conf/modify/mapping.map', ); By default, C is assumed to be C. @@ -182,13 +144,16 @@ C create optional report about usage of fields and subfields -C is closure to call when adding C<< key => 'value' >> combinations to -lookup. +C is closure to called to save data into lookups C specify mapping from subfields to delimiters or from delimiters to subfields, as well as oprations on fields (if subfield is defined as C<*>. +C is alternative for C above which preserves order and offers +(hopefully) simplier sintax than YAML or perl (see L). This option +overrides C if both exists for same input. + Returns size of database, regardless of C and C parametars, see also C. @@ -204,6 +169,8 @@ $log->logconfess("lookup_coderef must be CODE, not ",ref($arg->{lookup_coderef})) if ($arg->{lookup_coderef} && ref($arg->{lookup_coderef}) ne 'CODE'); + $log->debug( $arg->{lookup_coderef} ? '' : 'not ', "using lookup_coderef"); + $log->logcroak("need path") if (! $arg->{'path'}); my $code_page = $arg->{'code_page'} || 'cp852'; @@ -235,10 +202,19 @@ } - my $rec_regex = $self->modify_record_regexps(%{ $arg->{modify_records} }); - $log->debug("rec_regex: ", Dumper($rec_regex)); + my $rec_regex; + if (my $p = $arg->{modify_file}) { + $log->debug("using modify_file $p"); + $rec_regex = $self->modify_file_regexps( $p ); + } elsif (my $h = $arg->{modify_records}) { + $log->debug("using modify_records ", Dumper( $h )); + $rec_regex = $self->modify_record_regexps(%{ $h }); + } + $log->debug("rec_regex: ", Dumper($rec_regex)) if ($rec_regex); + + my $class = $self->{module} || $log->logconfess("can't get low-level module name!"); - my ($db, $size) = $self->{open_db}->( $self, + my $ll_db = $class->new( path => $arg->{path}, # filter => sub { # my ($l,$f_nr) = @_; @@ -250,11 +226,13 @@ %{ $arg }, ); - unless (defined($db)) { + unless (defined($ll_db)) { $log->logwarn("can't open database $arg->{path}, skipping..."); return; } + my $size = $ll_db->size; + unless ($size) { $log->logwarn("no records in database $arg->{path}, skipping..."); return; @@ -281,20 +259,52 @@ $log->info("processing $self->{size}/$size records [$from_rec-$to_rec] convert $code_page -> $self->{encoding}", $self->{stats} ? ' [stats]' : ''); + # turn on low_mem for databases with more than 100000 records! + if (! $self->{low_mem} && $size > 100000) { + $log->warn("Using on-disk storage instead of memory for input data. This will affect performance."); + $self->{low_mem}++; + } + + # running with low_mem flag? well, use DBM::Deep then. + if ($self->{'low_mem'}) { + $log->info("running with low_mem which impacts performance (<32 Mb memory usage)"); + + my $db_file = "data.db"; + + if (-e $db_file) { + unlink $db_file or $log->logdie("can't remove '$db_file' from last run"); + $log->debug("removed '$db_file' from last run"); + } + + require DBM::Deep; + + my $db = new DBM::Deep $db_file; + + $log->logdie("DBM::Deep error: $!") unless ($db); + + if ($db->error()) { + $log->logdie("can't open '$db_file' under low_mem: ",$db->error()); + } else { + $log->debug("using file '$db_file' for DBM::Deep"); + } + + $self->{'db'} = $db; + } + # read database for (my $pos = $from_rec; $pos <= $to_rec; $pos++) { $log->debug("position: $pos\n"); - my $rec = $self->{fetch_rec}->($self, $db, $pos, sub { + my $rec = $ll_db->fetch_rec($pos, sub { my ($l,$f_nr) = @_; # return unless defined($l); # return $l unless ($rec_regex && $f_nr); -warn "## --> $f_nr ## $l\n"; + $log->debug("-=> $f_nr ## $l"); + # codepage conversion and recode_regex -# from_to($l, $code_page, $self->{'encoding'}); - from_to($l, $code_page, 'utf-8'); + from_to($l, $code_page, $self->{'encoding'}); $l =~ s/($recode_regex)/$recode_map->{$1}/g if ($recode_regex && $recode_map); # apply regexps @@ -302,13 +312,16 @@ $log->logconfess("regexps->{$f_nr} must be ARRAY") if (ref($rec_regex->{$f_nr}) ne 'ARRAY'); my $c = 0; foreach my $r (@{ $rec_regex->{$f_nr} }) { - #$log->debug("\$l = $l\neval \$l =~ $r"); + my $old_l = $l; eval '$l =~ ' . $r; + if ($old_l ne $l) { + $log->debug("REGEX on $f_nr eval \$l =~ $r\n## old l: [$old_l]\n## new l: [$l]"); + } $log->error("error applying regex: $r") if ($@); } } -warn "## <-- $f_nr ## $l\n"; + $log->debug("<=- $f_nr ## $l"); return $l; }); @@ -333,7 +346,7 @@ if ($self->{stats}) { # fetch clean record with regexpes applied for statistics - my $rec = $self->{fetch_rec}->($self, $db, $pos); + my $rec = $ll_db->fetch_rec($pos); foreach my $fld (keys %{ $rec }) { $self->{_stats}->{fld}->{ $fld }++; @@ -370,6 +383,9 @@ $self->{max_pos} = $to_rec; $log->debug("max_pos: $to_rec"); + # save for dump + $self->{ll_db} = $ll_db; + return $size; } @@ -531,9 +547,22 @@ return $out; } +=head2 dump + +Display humanly readable dump of record + +=cut + +sub dump { + my $self = shift; + + return $self->{ll_db}->dump_rec( $self->{pos} ); + +} + =head2 modify_record_regexps -Generate hash with regexpes to be applied using L. +Generate hash with regexpes to be applied using l. my $regexpes = $input->modify_record_regexps( 900 => { '^a' => { ' : ' => '^b' } }, @@ -542,37 +571,103 @@ =cut +sub _get_regex { + my ($sf,$from,$to) = @_; + if ($sf =~ /^\^/) { + return + 's/\Q'. $sf .'\E([^\^]*?)\Q'. $from .'\E([^\^]*?)/'. $sf .'$1'. $to .'$2/'; + } else { + return + 's/\Q'. $from .'\E/'. $to .'/g'; + } +} + sub modify_record_regexps { my $self = shift; my $modify_record = {@_}; my $regexpes; + my $log = $self->_get_logger(); + foreach my $f (keys %$modify_record) { -warn "--- f: $f\n"; + $log->debug("field: $f"); + foreach my $sf (keys %{ $modify_record->{$f} }) { -warn "---- sf: $sf\n"; + $log->debug("subfield: $sf"); + foreach my $from (keys %{ $modify_record->{$f}->{$sf} }) { my $to = $modify_record->{$f}->{$sf}->{$from}; #die "no field?" unless defined($to); -warn "----- transform: |$from| -> |$to|\n"; - - if ($sf =~ /^\^/) { - my $regex = - 's/\Q'. $sf .'\E([^\^]+)\Q'. $from .'\E([^\^]+)/'. $sf .'$1'. $to .'$2/g'; - push @{ $regexpes->{$f} }, $regex; -warn ">>>>> $regex [sf]\n"; - } else { - my $regex = - 's/\Q'. $from .'\E/'. $to .'/g'; - push @{ $regexpes->{$f} }, $regex; -warn ">>>>> $regex [global]\n"; - } + $log->debug("transform: |$from| -> |$to|"); + my $regex = _get_regex($sf,$from,$to); + push @{ $regexpes->{$f} }, $regex; + $log->debug("regex: $regex"); } } } + return $regexpes; +} + +=head2 modify_file_regexps + +Generate hash with regexpes to be applied using l from +pseudo hash/yaml format for regex mappings. + +It should be obvious: + + 200 + '^a' + ' : ' => '^e' + ' = ' => '^d' + +In field I<200> find C<'^a'> and then C<' : '>, and replace it with C<'^e'>. +In field I<200> find C<'^a'> and then C<' = '>, and replace it with C<'^d'>. + + my $regexpes = $input->modify_file_regexps( 'conf/modify/common.pl' ); + +On undef path it will just return. + +=cut + +sub modify_file_regexps { + my $self = shift; + + my $modify_path = shift || return; + + my $log = $self->_get_logger(); + + my $regexpes; + + CORE::open(my $fh, $modify_path) || $log->logdie("can't open modify file $modify_path: $!"); + + my ($f,$sf); + + while(<$fh>) { + chomp; + next if (/^#/ || /^\s*$/); + + if (/^\s*(\d+)\s*$/) { + $f = $1; + $log->debug("field: $f"); + next; + } elsif (/^\s*'([^']*)'\s*$/) { + $sf = $1; + $log->die("can't define subfiled before field in: $_") unless ($f); + $log->debug("subfield: $sf"); + } elsif (/^\s*'([^']*)'\s*=>\s*'([^']*)'\s*$/) { + my ($from,$to) = ($1, $2); + + $log->debug("transform: |$from| -> |$to|"); + + my $regex = _get_regex($sf,$from,$to); + push @{ $regexpes->{$f} }, $regex; + $log->debug("regex: $regex"); + } + } + return $regexpes; }