0ffa04a71ecec09f8e62d84017bfc16fe569c201
[webpac2] / lib / WebPAC / Input.pm
1 package WebPAC::Input;
2
3 use warnings;
4 use strict;
5
6 use blib;
7
8 use WebPAC::Common;
9 use base qw/WebPAC::Common/;
10 use Data::Dumper;
11 use Encode qw/from_to/;
12
13 =head1 NAME
14
15 WebPAC::Input - read different file formats into WebPAC
16
17 =head1 VERSION
18
19 Version 0.13
20
21 =cut
22
23 our $VERSION = '0.13';
24
25 =head1 SYNOPSIS
26
27 This module implements input as database which have fixed and known
28 I<size> while indexing and single unique numeric identifier for database
29 position ranging from 1 to I<size>.
30
31 Simply, something that is indexed by unmber from 1 .. I<size>.
32
33 Examples of such databases are CDS/ISIS files, MARC files, lines in
34 text file, and so on.
35
36 Specific file formats are implemented using low-level interface modules,
37 located in C<WebPAC::Input::*> namespace which export C<open_db>,
38 C<fetch_rec> and optional C<init> functions.
39
40 Perhaps a little code snippet.
41
42         use WebPAC::Input;
43
44         my $db = WebPAC::Input->new(
45                 module => 'WebPAC::Input::ISIS',
46                 low_mem => 1,
47         );
48
49         $db->open( path => '/path/to/database' );
50         print "database size: ",$db->size,"\n";
51         while (my $rec = $db->fetch) {
52                 # do something with $rec
53         }
54
55
56
57 =head1 FUNCTIONS
58
59 =head2 new
60
61 Create new input database object.
62
63   my $db = new WebPAC::Input(
64         module => 'WebPAC::Input::MARC',
65         encoding => 'ISO-8859-2',
66         low_mem => 1,
67         recode => 'char pairs',
68         no_progress_bar => 1,
69   );
70
71 C<module> is low-level file format module. See L<WebPAC::Input::ISIS> and
72 L<WebPAC::Input::MARC>.
73
74 Optional parametar C<encoding> specify application code page (which will be
75 used internally). This should probably be your terminal encoding, and by
76 default, it C<ISO-8859-2>.
77
78 Default is not to use C<low_mem> options (see L<MEMORY USAGE> below).
79
80 C<recode> is optional string constisting of character or words pairs that
81 should be replaced in input stream.
82
83 C<no_progress_bar> disables progress bar output on C<STDOUT>
84
85 This function will also call low-level C<init> if it exists with same
86 parametars.
87
88 =cut
89
90 sub new {
91         my $class = shift;
92         my $self = {@_};
93         bless($self, $class);
94
95         my $log = $self->_get_logger;
96
97         $log->logconfess("code_page argument is not suppored any more. change it to encoding") if ($self->{lookup});
98         $log->logconfess("lookup argument is not suppored any more. rewrite call to lookup_ref") if ($self->{lookup});
99
100         $log->logconfess("specify low-level file format module") unless ($self->{module});
101         my $module = $self->{module};
102         $module =~ s#::#/#g;
103         $module .= '.pm';
104         $log->debug("require low-level module $self->{module} from $module");
105
106         require $module;
107         #eval $self->{module} .'->import';
108
109         # check if required subclasses are implemented
110         foreach my $subclass (qw/open_db fetch_rec init dump_rec/) {
111                 my $n = $self->{module} . '::' . $subclass;
112                 if (! defined &{ $n }) {
113                         my $missing = "missing $subclass in $self->{module}";
114                         $self->{$subclass} = sub { $log->logwarn($missing) };
115                 } else {
116                         $self->{$subclass} = \&{ $n };
117                 }
118         }
119
120         if ($self->{init}) {
121                 $log->debug("calling init");
122                 $self->{init}->($self, @_);
123         }
124
125         $self->{'encoding'} ||= 'ISO-8859-2';
126
127         # running with low_mem flag? well, use DBM::Deep then.
128         if ($self->{'low_mem'}) {
129                 $log->info("running with low_mem which impacts performance (<32 Mb memory usage)");
130
131                 my $db_file = "data.db";
132
133                 if (-e $db_file) {
134                         unlink $db_file or $log->logdie("can't remove '$db_file' from last run");
135                         $log->debug("removed '$db_file' from last run");
136                 }
137
138                 require DBM::Deep;
139
140                 my $db = new DBM::Deep $db_file;
141
142                 $log->logdie("DBM::Deep error: $!") unless ($db);
143
144                 if ($db->error()) {
145                         $log->logdie("can't open '$db_file' under low_mem: ",$db->error());
146                 } else {
147                         $log->debug("using file '$db_file' for DBM::Deep");
148                 }
149
150                 $self->{'db'} = $db;
151         }
152
153         $self ? return $self : return undef;
154 }
155
156 =head2 open
157
158 This function will read whole database in memory and produce lookups.
159
160  $input->open(
161         path => '/path/to/database/file',
162         code_page => 'cp852',
163         limit => 500,
164         offset => 6000,
165         stats => 1,
166         lookup_coderef => sub {
167                 my $rec = shift;
168                 # store lookups
169         },
170         modify_records => {
171                 900 => { '^a' => { ' : ' => '^b' } },
172                 901 => { '*' => { '^b' => ' ; ' } },
173         },
174         modify_file => 'conf/modify/mapping.map',
175  );
176
177 By default, C<code_page> is assumed to be C<cp852>.
178
179 C<offset> is optional parametar to position at some offset before reading from database.
180
181 C<limit> is optional parametar to read just C<limit> records from database
182
183 C<stats> create optional report about usage of fields and subfields
184
185 C<lookup_coderef> is closure to called to save data into lookups
186
187 C<modify_records> specify mapping from subfields to delimiters or from
188 delimiters to subfields, as well as oprations on fields (if subfield is
189 defined as C<*>.
190
191 C<modify_file> is alternative for C<modify_records> above which preserves order and offers
192 (hopefully) simplier sintax than YAML or perl (see L</modify_file_regex>). This option
193 overrides C<modify_records> if both exists for same input.
194
195 Returns size of database, regardless of C<offset> and C<limit>
196 parametars, see also C<size>.
197
198 =cut
199
200 sub open {
201         my $self = shift;
202         my $arg = {@_};
203
204         my $log = $self->_get_logger();
205
206         $log->logconfess("lookup argument is not suppored any more. rewrite call to lookup_coderef") if ($arg->{lookup});
207         $log->logconfess("lookup_coderef must be CODE, not ",ref($arg->{lookup_coderef}))
208                 if ($arg->{lookup_coderef} && ref($arg->{lookup_coderef}) ne 'CODE');
209
210         $log->debug( $arg->{lookup_coderef} ? '' : 'not ', "using lookup_coderef");
211
212         $log->logcroak("need path") if (! $arg->{'path'});
213         my $code_page = $arg->{'code_page'} || 'cp852';
214
215         # store data in object
216         $self->{'input_code_page'} = $code_page;
217         foreach my $v (qw/path offset limit/) {
218                 $self->{$v} = $arg->{$v} if ($arg->{$v});
219         }
220
221         my $filter_ref;
222         my $recode_regex;
223         my $recode_map;
224
225         if ($self->{recode}) {
226                 my @r = split(/\s/, $self->{recode});
227                 if ($#r % 2 != 1) {
228                         $log->logwarn("recode needs even number of elements (some number of valid pairs)");
229                 } else {
230                         while (@r) {
231                                 my $from = shift @r;
232                                 my $to = shift @r;
233                                 $recode_map->{$from} = $to;
234                         }
235
236                         $recode_regex = join '|' => keys %{ $recode_map };
237
238                         $log->debug("using recode regex: $recode_regex");
239                 }
240
241         }
242
243         my $rec_regex;
244         if (my $p = $arg->{modify_file}) {
245                 $log->debug("using modify_file $p");
246                 $rec_regex = $self->modify_file_regexps( $p );
247         } elsif (my $h = $arg->{modify_records}) {
248                 $log->debug("using modify_records ", Dumper( $h ));
249                 $rec_regex = $self->modify_record_regexps(%{ $h });
250         }
251         $log->debug("rec_regex: ", Dumper($rec_regex)) if ($rec_regex);
252
253         my ($db, $size) = $self->{open_db}->( $self, 
254                 path => $arg->{path},
255 #               filter => sub {
256 #                       my ($l,$f_nr) = @_;
257 #                       return unless defined($l);
258 #                       from_to($l, $code_page, $self->{'encoding'});
259 #                       $l =~ s/($recode_regex)/$recode_map->{$1}/g if ($recode_regex && $recode_map);
260 #                       return $l;
261 #               },
262                 %{ $arg },
263         );
264
265         unless (defined($db)) {
266                 $log->logwarn("can't open database $arg->{path}, skipping...");
267                 return;
268         }
269
270         unless ($size) {
271                 $log->logwarn("no records in database $arg->{path}, skipping...");
272                 return;
273         }
274
275         my $from_rec = 1;
276         my $to_rec = $size;
277
278         if (my $s = $self->{offset}) {
279                 $log->debug("skipping to MFN $s");
280                 $from_rec = $s;
281         } else {
282                 $self->{offset} = $from_rec;
283         }
284
285         if ($self->{limit}) {
286                 $log->debug("limiting to ",$self->{limit}," records");
287                 $to_rec = $from_rec + $self->{limit} - 1;
288                 $to_rec = $size if ($to_rec > $size);
289         }
290
291         # store size for later
292         $self->{size} = ($to_rec - $from_rec) ? ($to_rec - $from_rec + 1) : 0;
293
294         $log->info("processing $self->{size}/$size records [$from_rec-$to_rec] convert $code_page -> $self->{encoding}", $self->{stats} ? ' [stats]' : '');
295
296         # read database
297         for (my $pos = $from_rec; $pos <= $to_rec; $pos++) {
298
299                 $log->debug("position: $pos\n");
300
301                 my $rec = $self->{fetch_rec}->($self, $pos, sub {
302                                 my ($l,$f_nr) = @_;
303 #                               return unless defined($l);
304 #                               return $l unless ($rec_regex && $f_nr);
305
306                                 $log->debug("-=> $f_nr ## $l");
307
308                                 # codepage conversion and recode_regex
309                                 from_to($l, $code_page, $self->{'encoding'});
310                                 $l =~ s/($recode_regex)/$recode_map->{$1}/g if ($recode_regex && $recode_map);
311
312                                 # apply regexps
313                                 if ($rec_regex && defined($rec_regex->{$f_nr})) {
314                                         $log->logconfess("regexps->{$f_nr} must be ARRAY") if (ref($rec_regex->{$f_nr}) ne 'ARRAY');
315                                         my $c = 0;
316                                         foreach my $r (@{ $rec_regex->{$f_nr} }) {
317                                                 my $old_l = $l;
318                                                 eval '$l =~ ' . $r;
319                                                 if ($old_l ne $l) {
320                                                         $log->debug("REGEX on $f_nr eval \$l =~ $r\n## old l: [$old_l]\n## new l: [$l]");
321                                                 }
322                                                 $log->error("error applying regex: $r") if ($@);
323                                         }
324                                 }
325
326                                 $log->debug("<=- $f_nr ## $l");
327                                 return $l;
328                 });
329
330                 $log->debug(sub { Dumper($rec) });
331
332                 if (! $rec) {
333                         $log->warn("record $pos empty? skipping...");
334                         next;
335                 }
336
337                 # store
338                 if ($self->{low_mem}) {
339                         $self->{db}->put($pos, $rec);
340                 } else {
341                         $self->{data}->{$pos} = $rec;
342                 }
343
344                 # create lookup
345                 $arg->{'lookup_coderef'}->( $rec ) if ($rec && $arg->{'lookup_coderef'});
346
347                 # update counters for statistics
348                 if ($self->{stats}) {
349
350                         # fetch clean record with regexpes applied for statistics
351                         my $rec = $self->{fetch_rec}->($self, $pos);
352
353                         foreach my $fld (keys %{ $rec }) {
354                                 $self->{_stats}->{fld}->{ $fld }++;
355
356                                 $log->logdie("invalid record fild $fld, not ARRAY")
357                                         unless (ref($rec->{ $fld }) eq 'ARRAY');
358         
359                                 foreach my $row (@{ $rec->{$fld} }) {
360
361                                         if (ref($row) eq 'HASH') {
362
363                                                 foreach my $sf (keys %{ $row }) {
364                                                         next if ($sf eq 'subfields');
365                                                         $self->{_stats}->{sf}->{ $fld }->{ $sf }->{count}++;
366                                                         $self->{_stats}->{sf}->{ $fld }->{ $sf }->{repeatable}++
367                                                                         if (ref($row->{$sf}) eq 'ARRAY');
368                                                 }
369
370                                         } else {
371                                                 $self->{_stats}->{repeatable}->{ $fld }++;
372                                         }
373                                 }
374                         }
375                 }
376
377                 $self->progress_bar($pos,$to_rec) unless ($self->{no_progress_bar});
378
379         }
380
381         $self->{pos} = -1;
382         $self->{last_pcnt} = 0;
383
384         # store max mfn and return it.
385         $self->{max_pos} = $to_rec;
386         $log->debug("max_pos: $to_rec");
387
388         return $size;
389 }
390
391 =head2 fetch
392
393 Fetch next record from database. It will also displays progress bar.
394
395  my $rec = $isis->fetch;
396
397 Record from this function should probably go to C<data_structure> for
398 normalisation.
399
400 =cut
401
402 sub fetch {
403         my $self = shift;
404
405         my $log = $self->_get_logger();
406
407         $log->logconfess("it seems that you didn't load database!") unless ($self->{pos});
408
409         if ($self->{pos} == -1) {
410                 $self->{pos} = $self->{offset};
411         } else {
412                 $self->{pos}++;
413         }
414
415         my $mfn = $self->{pos};
416
417         if ($mfn > $self->{max_pos}) {
418                 $self->{pos} = $self->{max_pos};
419                 $log->debug("at EOF");
420                 return;
421         }
422
423         $self->progress_bar($mfn,$self->{max_pos}) unless ($self->{no_progress_bar});
424
425         my $rec;
426
427         if ($self->{low_mem}) {
428                 $rec = $self->{db}->get($mfn);
429         } else {
430                 $rec = $self->{data}->{$mfn};
431         }
432
433         $rec ||= 0E0;
434 }
435
436 =head2 pos
437
438 Returns current record number (MFN).
439
440  print $isis->pos;
441
442 First record in database has position 1.
443
444 =cut
445
446 sub pos {
447         my $self = shift;
448         return $self->{pos};
449 }
450
451
452 =head2 size
453
454 Returns number of records in database
455
456  print $isis->size;
457
458 Result from this function can be used to loop through all records
459
460  foreach my $mfn ( 1 ... $isis->size ) { ... }
461
462 because it takes into account C<offset> and C<limit>.
463
464 =cut
465
466 sub size {
467         my $self = shift;
468         return $self->{size};
469 }
470
471 =head2 seek
472
473 Seek to specified MFN in file.
474
475  $isis->seek(42);
476
477 First record in database has position 1.
478
479 =cut
480
481 sub seek {
482         my $self = shift;
483         my $pos = shift || return;
484
485         my $log = $self->_get_logger();
486
487         if ($pos < 1) {
488                 $log->warn("seek before first record");
489                 $pos = 1;
490         } elsif ($pos > $self->{max_pos}) {
491                 $log->warn("seek beyond last record");
492                 $pos = $self->{max_pos};
493         }
494
495         return $self->{pos} = (($pos - 1) || -1);
496 }
497
498 =head2 stats
499
500 Dump statistics about field and subfield usage
501
502   print $input->stats;
503
504 =cut
505
506 sub stats {
507         my $self = shift;
508
509         my $log = $self->_get_logger();
510
511         my $s = $self->{_stats};
512         if (! $s) {
513                 $log->warn("called stats, but there is no statistics collected");
514                 return;
515         }
516
517         my $max_fld = 0;
518
519         my $out = join("\n",
520                 map {
521                         my $f = $_ || die "no field";
522                         my $v = $s->{fld}->{$f} || die "no s->{fld}->{$f}";
523                         $max_fld = $v if ($v > $max_fld);
524
525                         my $o = sprintf("%4s %d ~", $f, $v);
526
527                         if (defined($s->{sf}->{$f})) {
528                                 map {
529                                         $o .= sprintf(" %s:%d%s", $_, 
530                                                 $s->{sf}->{$f}->{$_}->{count},
531                                                 $s->{sf}->{$f}->{$_}->{repeatable} ? '*' : '',
532                                         );
533                                 } sort keys %{ $s->{sf}->{$f} };
534                         }
535
536                         if (my $v_r = $s->{repeatable}->{$f}) {
537                                 $o .= " ($v_r)" if ($v_r != $v);
538                         }
539
540                         $o;
541                 } sort { $a cmp $b } keys %{ $s->{fld} }
542         );
543
544         $log->debug( sub { Dumper($s) } );
545
546         return $out;
547 }
548
549 =head2 dump
550
551 Display humanly readable dump of record
552
553 =cut
554
555 sub dump {
556         my $self = shift;
557
558         return $self->{dump_rec}->($self, $self->{pos});
559
560 }
561
562 =head2 modify_record_regexps
563
564 Generate hash with regexpes to be applied using l<filter>.
565
566   my $regexpes = $input->modify_record_regexps(
567                 900 => { '^a' => { ' : ' => '^b' } },
568                 901 => { '*' => { '^b' => ' ; ' } },
569   );
570
571 =cut
572
573 sub _get_regex {
574         my ($sf,$from,$to) = @_;
575         if ($sf =~ /^\^/) {
576                 return
577                         's/\Q'. $sf .'\E([^\^]*?)\Q'. $from .'\E([^\^]*?)/'. $sf .'$1'. $to .'$2/';
578         } else {
579                 return
580                         's/\Q'. $from .'\E/'. $to .'/g';
581         }
582 }
583
584 sub modify_record_regexps {
585         my $self = shift;
586         my $modify_record = {@_};
587
588         my $regexpes;
589
590         my $log = $self->_get_logger();
591
592         foreach my $f (keys %$modify_record) {
593                 $log->debug("field: $f");
594
595                 foreach my $sf (keys %{ $modify_record->{$f} }) {
596                         $log->debug("subfield: $sf");
597
598                         foreach my $from (keys %{ $modify_record->{$f}->{$sf} }) {
599                                 my $to = $modify_record->{$f}->{$sf}->{$from};
600                                 #die "no field?" unless defined($to);
601                                 $log->debug("transform: |$from| -> |$to|");
602
603                                 my $regex = _get_regex($sf,$from,$to);
604                                 push @{ $regexpes->{$f} }, $regex;
605                                 $log->debug("regex: $regex");
606                         }
607                 }
608         }
609
610         return $regexpes;
611 }
612
613 =head2 modify_file_regexps
614
615 Generate hash with regexpes to be applied using l<filter> from
616 pseudo hash/yaml format for regex mappings.
617
618 It should be obvious:
619
620         200
621           '^a'
622             ' : ' => '^e'
623             ' = ' => '^d'
624
625 In field I<200> find C<'^a'> and then C<' : '>, and replace it with C<'^e'>.
626 In field I<200> find C<'^a'> and then C<' = '>, and replace it with C<'^d'>.
627
628   my $regexpes = $input->modify_file_regexps( 'conf/modify/common.pl' );
629
630 On undef path it will just return.
631
632 =cut
633
634 sub modify_file_regexps {
635         my $self = shift;
636
637         my $modify_path = shift || return;
638
639         my $log = $self->_get_logger();
640
641         my $regexpes;
642
643         CORE::open(my $fh, $modify_path) || $log->logdie("can't open modify file $modify_path: $!");
644
645         my ($f,$sf);
646
647         while(<$fh>) {
648                 chomp;
649                 next if (/^#/ || /^\s*$/);
650
651                 if (/^\s*(\d+)\s*$/) {
652                         $f = $1;
653                         $log->debug("field: $f");
654                         next;
655                 } elsif (/^\s*'([^']*)'\s*$/) {
656                         $sf = $1;
657                         $log->die("can't define subfiled before field in: $_") unless ($f);
658                         $log->debug("subfield: $sf");
659                 } elsif (/^\s*'([^']*)'\s*=>\s*'([^']*)'\s*$/) {
660                         my ($from,$to) = ($1, $2);
661
662                         $log->debug("transform: |$from| -> |$to|");
663
664                         my $regex = _get_regex($sf,$from,$to);
665                         push @{ $regexpes->{$f} }, $regex;
666                         $log->debug("regex: $regex");
667                 }
668         }
669
670         return $regexpes;
671 }
672
673 =head1 MEMORY USAGE
674
675 C<low_mem> options is double-edged sword. If enabled, WebPAC
676 will run on memory constraint machines (which doesn't have enough
677 physical RAM to create memory structure for whole source database).
678
679 If your machine has 512Mb or more of RAM and database is around 10000 records,
680 memory shouldn't be an issue. If you don't have enough physical RAM, you
681 might consider using virtual memory (if your operating system is handling it
682 well, like on FreeBSD or Linux) instead of dropping to L<DBM::Deep> to handle
683 parsed structure of ISIS database (this is what C<low_mem> option does).
684
685 Hitting swap at end of reading source database is probably o.k. However,
686 hitting swap before 90% will dramatically decrease performance and you will
687 be better off with C<low_mem> and using rest of availble memory for
688 operating system disk cache (Linux is particuallary good about this).
689 However, every access to database record will require disk access, so
690 generation phase will be slower 10-100 times.
691
692 Parsed structures are essential - you just have option to trade RAM memory
693 (which is fast) for disk space (which is slow). Be sure to have planty of
694 disk space if you are using C<low_mem> and thus L<DBM::Deep>.
695
696 However, when WebPAC is running on desktop machines (or laptops :-), it's
697 highly undesireable for system to start swapping. Using C<low_mem> option can
698 reduce WecPAC memory usage to around 64Mb for same database with lookup
699 fields and sorted indexes which stay in RAM. Performance will suffer, but
700 memory usage will really be minimal. It might be also more confortable to
701 run WebPAC reniced on those machines.
702
703
704 =head1 AUTHOR
705
706 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
707
708 =head1 COPYRIGHT & LICENSE
709
710 Copyright 2005-2006 Dobrica Pavlinusic, All Rights Reserved.
711
712 This program is free software; you can redistribute it and/or modify it
713 under the same terms as Perl itself.
714
715 =cut
716
717 1; # End of WebPAC::Input