7 use base qw/WebPAC::Common/;
13 WebPAC::Input - read different file formats into WebPAC
21 our $VERSION = '0.04';
25 This module implements input as database which have fixed and known
26 I<size> while indexing and single unique numeric identifier for database
27 position ranging from 1 to I<size>.
29 Simply, something that is indexed by unmber from 1 .. I<size>.
31 Examples of such databases are CDS/ISIS files, MARC files, lines in
34 Specific file formats are implemented using low-level interface modules,
35 located in C<WebPAC::Input::*> namespace which export C<open_db>,
36 C<fetch_rec> and optional C<init> functions.
38 Perhaps a little code snippet.
42 my $db = WebPAC::Input->new(
43 module => 'WebPAC::Input::ISIS',
45 lookup => $lookup_obj,
49 $db->open('/path/to/database');
50 print "database size: ",$db->size,"\n";
51 while (my $rec = $db->fetch) {
52 # do something with $rec
61 Create new input database object.
63 my $db = new WebPAC::Input(
64 module => 'WebPAC::Input::MARC',
65 code_page => 'ISO-8859-2',
67 recode => 'char pairs',
70 C<module> is low-level file format module. See L<WebPAC::Input::Isis> and
71 L<WebPAC::Input::MARC>.
73 Optional parametar C<code_page> specify application code page (which will be
74 used internally). This should probably be your terminal encoding, and by
75 default, it C<ISO-8859-2>.
77 Default is not to use C<low_mem> options (see L<MEMORY USAGE> below).
79 This function will also call low-level C<init> if it exists with same
89 my $log = $self->_get_logger;
91 $log->logconfess("specify low-level file format module") unless ($self->{module});
92 my $module = $self->{module};
95 $log->debug("require low-level module $self->{module} from $module");
98 #eval $self->{module} .'->import';
100 # check if required subclasses are implemented
101 foreach my $subclass (qw/open_db fetch_rec init/) {
102 my $n = $self->{module} . '::' . $subclass;
103 if (! defined &{ $n }) {
104 my $missing = "missing $subclass in $self->{module}";
105 $self->{$subclass} = sub { $log->logwarn($missing) };
107 $self->{$subclass} = \&{ $n };
112 $log->debug("calling init");
113 $self->{init}->($self, @_);
116 $self->{'code_page'} ||= 'ISO-8859-2';
118 # running with low_mem flag? well, use DBM::Deep then.
119 if ($self->{'low_mem'}) {
120 $log->info("running with low_mem which impacts performance (<32 Mb memory usage)");
122 my $db_file = "data.db";
125 unlink $db_file or $log->logdie("can't remove '$db_file' from last run");
126 $log->debug("removed '$db_file' from last run");
131 my $db = new DBM::Deep $db_file;
133 $log->logdie("DBM::Deep error: $!") unless ($db);
136 $log->logdie("can't open '$db_file' under low_mem: ",$db->error());
138 $log->debug("using file '$db_file' for DBM::Deep");
144 $self ? return $self : return undef;
149 This function will read whole database in memory and produce lookups.
152 path => '/path/to/database/file',
156 lookup => $lookup_obj,
159 By default, C<code_page> is assumed to be C<852>.
161 C<offset> is optional parametar to position at some offset before reading from database.
163 C<limit> is optional parametar to read just C<limit> records from database
165 Returns size of database, regardless of C<offset> and C<limit>
166 parametars, see also C<size>.
174 my $log = $self->_get_logger();
176 $log->logcroak("need path") if (! $arg->{'path'});
177 my $code_page = $arg->{'code_page'} || '852';
179 # store data in object
180 $self->{'input_code_page'} = $code_page;
181 foreach my $v (qw/path offset limit/) {
182 $self->{$v} = $arg->{$v} if ($arg->{$v});
185 # create Text::Iconv object
186 $self->{iconv} = Text::Iconv->new($code_page,$self->{'code_page'});
190 if ($self->{recode}) {
191 my @r = split(/\s/, $self->{recode});
193 $log->logwarn("recode needs even number of elements (some number of valid pairs)");
199 $recode->{$from} = $to;
202 my $regex = join '|' => keys %{ $recode };
204 $log->debug("using recode regex: $regex");
208 $t =~ s/($regex)/$recode->{$1}/g;
216 my ($db, $size) = $self->{open_db}->( $self,
217 path => $arg->{path},
218 filter => $filter_ref,
222 $log->logwarn("can't open database $arg->{path}, skipping...");
227 $log->logwarn("no records in database $arg->{path}, skipping...");
234 if (my $s = $self->{offset}) {
235 $log->info("skipping to MFN $s");
238 $self->{offset} = $from_rec;
241 if ($self->{limit}) {
242 $log->debug("limiting to ",$self->{limit}," records");
243 $to_rec = $from_rec + $self->{limit} - 1;
244 $to_rec = $size if ($to_rec > $size);
247 # store size for later
248 $self->{size} = ($to_rec - $from_rec) ? ($to_rec - $from_rec + 1) : 0;
250 $log->info("processing $self->{size}/$size records [$from_rec-$to_rec] convert $code_page -> $self->{code_page}");
253 for (my $pos = $from_rec; $pos <= $to_rec; $pos++) {
255 $log->debug("position: $pos\n");
257 my $rec = $self->{fetch_rec}->($self, $db, $pos );
259 $log->debug(sub { Dumper($rec) });
262 $log->warn("record $pos empty? skipping...");
267 if ($self->{low_mem}) {
268 $self->{db}->put($pos, $rec);
270 $self->{data}->{$pos} = $rec;
274 $self->{'lookup'}->add( $rec ) if ($rec && $self->{'lookup'});
276 $self->progress_bar($pos,$to_rec);
281 $self->{last_pcnt} = 0;
283 # store max mfn and return it.
284 $self->{max_pos} = $to_rec;
285 $log->debug("max_pos: $to_rec");
292 Fetch next record from database. It will also displays progress bar.
294 my $rec = $isis->fetch;
296 Record from this function should probably go to C<data_structure> for
304 my $log = $self->_get_logger();
306 $log->logconfess("it seems that you didn't load database!") unless ($self->{pos});
308 if ($self->{pos} == -1) {
309 $self->{pos} = $self->{offset};
314 my $mfn = $self->{pos};
316 if ($mfn > $self->{max_pos}) {
317 $self->{pos} = $self->{max_pos};
318 $log->debug("at EOF");
322 $self->progress_bar($mfn,$self->{max_pos});
326 if ($self->{low_mem}) {
327 $rec = $self->{db}->get($mfn);
329 $rec = $self->{data}->{$mfn};
337 Returns current record number (MFN).
341 First record in database has position 1.
353 Returns number of records in database
357 Result from this function can be used to loop through all records
359 foreach my $mfn ( 1 ... $isis->size ) { ... }
361 because it takes into account C<offset> and C<limit>.
367 return $self->{size};
372 Seek to specified MFN in file.
376 First record in database has position 1.
382 my $pos = shift || return;
384 my $log = $self->_get_logger();
387 $log->warn("seek before first record");
389 } elsif ($pos > $self->{max_pos}) {
390 $log->warn("seek beyond last record");
391 $pos = $self->{max_pos};
394 return $self->{pos} = (($pos - 1) || -1);
400 C<low_mem> options is double-edged sword. If enabled, WebPAC
401 will run on memory constraint machines (which doesn't have enough
402 physical RAM to create memory structure for whole source database).
404 If your machine has 512Mb or more of RAM and database is around 10000 records,
405 memory shouldn't be an issue. If you don't have enough physical RAM, you
406 might consider using virtual memory (if your operating system is handling it
407 well, like on FreeBSD or Linux) instead of dropping to L<DBM::Deep> to handle
408 parsed structure of ISIS database (this is what C<low_mem> option does).
410 Hitting swap at end of reading source database is probably o.k. However,
411 hitting swap before 90% will dramatically decrease performance and you will
412 be better off with C<low_mem> and using rest of availble memory for
413 operating system disk cache (Linux is particuallary good about this).
414 However, every access to database record will require disk access, so
415 generation phase will be slower 10-100 times.
417 Parsed structures are essential - you just have option to trade RAM memory
418 (which is fast) for disk space (which is slow). Be sure to have planty of
419 disk space if you are using C<low_mem> and thus L<DBM::Deep>.
421 However, when WebPAC is running on desktop machines (or laptops :-), it's
422 highly undesireable for system to start swapping. Using C<low_mem> option can
423 reduce WecPAC memory usage to around 64Mb for same database with lookup
424 fields and sorted indexes which stay in RAM. Performance will suffer, but
425 memory usage will really be minimal. It might be also more confortable to
426 run WebPAC reniced on those machines.
431 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
433 =head1 COPYRIGHT & LICENSE
435 Copyright 2005 Dobrica Pavlinusic, All Rights Reserved.
437 This program is free software; you can redistribute it and/or modify it
438 under the same terms as Perl itself.
442 1; # End of WebPAC::Input