9 |
use XML::Simple; |
use XML::Simple; |
10 |
use Template; |
use Template; |
11 |
use Log::Log4perl qw(get_logger :levels); |
use Log::Log4perl qw(get_logger :levels); |
12 |
|
use Time::HiRes qw(time); |
13 |
|
|
14 |
use Data::Dumper; |
use Data::Dumper; |
15 |
|
|
40 |
|
|
41 |
Default C<code_page> is C<ISO-8859-2>. |
Default C<code_page> is C<ISO-8859-2>. |
42 |
|
|
43 |
|
Default is not to use C<low_mem> options (see L<MEMORY USAGE> below). |
44 |
|
|
45 |
This method will also read configuration files |
This method will also read configuration files |
46 |
C<global.conf> (used by indexer and Web font-end) |
C<global.conf> (used by indexer and Web font-end) |
47 |
and configuration file specified by C<config_file> |
and configuration file specified by C<config_file> |
48 |
which describes databases to be indexed. |
which describes databases to be indexed. |
49 |
|
|
|
C<low_mem> options is double-edged sword. If enabled, WebPAC |
|
|
will run on memory constraint machines (which doesn't have enough |
|
|
physical RAM to create memory structure for whole ISIS database). |
|
|
|
|
|
If your machine has 512Mb or more and database is around 10000 records, |
|
|
memory shouldn't be an issue. If you don't have enough physical RAM, you |
|
|
might consider using virtual memory (if your operating system is handling it |
|
|
well, like on FreeBSD or Linux) instead of dropping to L<DBD::Deep> to handle |
|
|
parsed structure of ISIS database. |
|
|
|
|
|
However, when WebPAC is running on desktop machines (or laptops :-), it's |
|
|
highly undesireable for system to start swapping. Using C<low_mem> option can |
|
|
reduce WecPAC memory usage to 16Mb for same database with lookup fields and |
|
|
sorted indexes which stay in RAM. Performance will suffer, but memory usage |
|
|
will really be minimal. It might be also more confortable to run WebPAC reniced |
|
|
on those machines. |
|
|
|
|
50 |
=cut |
=cut |
51 |
|
|
52 |
# mapping between data type and tag which specify |
# mapping between data type and tag which specify |
63 |
my $self = {@_}; |
my $self = {@_}; |
64 |
bless($self, $class); |
bless($self, $class); |
65 |
|
|
66 |
|
$self->{'start_t'} = time(); |
67 |
|
|
68 |
my $log_file = $self->{'log'} || "log.conf"; |
my $log_file = $self->{'log'} || "log.conf"; |
69 |
Log::Log4perl->init($log_file); |
Log::Log4perl->init($log_file); |
70 |
|
|
114 |
|
|
115 |
# running with low_mem flag? well, use DBM::Deep then. |
# running with low_mem flag? well, use DBM::Deep then. |
116 |
if ($self->{'low_mem'}) { |
if ($self->{'low_mem'}) { |
117 |
$log->info("running with low_mem which impacts performance (<64 Mb memory usage)"); |
$log->info("running with low_mem which impacts performance (<32 Mb memory usage)"); |
118 |
|
|
119 |
my $db_file = "data.db"; |
my $db_file = "data.db"; |
120 |
|
|
123 |
$log->debug("removed '$db_file' from last run"); |
$log->debug("removed '$db_file' from last run"); |
124 |
} |
} |
125 |
|
|
126 |
use DBM::Deep; |
require DBM::Deep; |
127 |
|
|
128 |
my $db = new DBM::Deep $db_file; |
my $db = new DBM::Deep $db_file; |
129 |
|
|
132 |
if ($db->error()) { |
if ($db->error()) { |
133 |
$log->logdie("can't open '$db_file' under low_mem: ",$db->error()); |
$log->logdie("can't open '$db_file' under low_mem: ",$db->error()); |
134 |
} else { |
} else { |
135 |
$log->debug("using file $db_file for DBM::Deep"); |
$log->debug("using file '$db_file' for DBM::Deep"); |
136 |
} |
} |
137 |
|
|
138 |
$self->{'db'} = $db; |
$self->{'db'} = $db; |
148 |
$webpac->open_isis( |
$webpac->open_isis( |
149 |
filename => '/data/ISIS/ISIS', |
filename => '/data/ISIS/ISIS', |
150 |
code_page => '852', |
code_page => '852', |
151 |
limit_mfn => '500', |
limit_mfn => 500, |
152 |
|
start_mfn => 6000, |
153 |
lookup => [ ... ], |
lookup => [ ... ], |
154 |
); |
); |
155 |
|
|
156 |
By default, ISIS code page is assumed to be C<852>. |
By default, ISIS code page is assumed to be C<852>. |
157 |
|
|
158 |
|
If optional parametar C<start_mfn> is set, this will be first MFN to read |
159 |
|
from database (so you can skip beginning of your database if you need to). |
160 |
|
|
161 |
If optional parametar C<limit_mfn> is set, it will read just 500 records |
If optional parametar C<limit_mfn> is set, it will read just 500 records |
162 |
from database in example above. |
from database in example above. |
163 |
|
|
185 |
$log->logcroak("need filename") if (! $arg->{'filename'}); |
$log->logcroak("need filename") if (! $arg->{'filename'}); |
186 |
my $code_page = $arg->{'code_page'} || '852'; |
my $code_page = $arg->{'code_page'} || '852'; |
187 |
|
|
188 |
|
$log->logdie("can't find database ",$arg->{'filename'}) unless (glob($arg->{'filename'}.'.*')); |
189 |
|
|
190 |
# store data in object |
# store data in object |
191 |
$self->{'isis_filename'} = $arg->{'filename'}; |
$self->{'isis_filename'} = $arg->{'filename'}; |
192 |
$self->{'isis_code_page'} = $code_page; |
$self->{'isis_code_page'} = $code_page; |
204 |
my $isis_db = OpenIsis::open($arg->{'filename'}); |
my $isis_db = OpenIsis::open($arg->{'filename'}); |
205 |
|
|
206 |
my $maxmfn = OpenIsis::maxRowid( $isis_db ) || 1; |
my $maxmfn = OpenIsis::maxRowid( $isis_db ) || 1; |
207 |
|
my $startmfn = 1; |
208 |
|
|
209 |
|
if (my $s = $self->{'start_mfn'}) { |
210 |
|
$log->info("skipping to MFN $s"); |
211 |
|
$startmfn = $s; |
212 |
|
} |
213 |
|
|
214 |
$maxmfn = $self->{limit_mfn} if ($self->{limit_mfn}); |
$maxmfn = $startmfn + $self->{limit_mfn} if ($self->{limit_mfn}); |
215 |
|
|
216 |
$log->info("processing $maxmfn records..."); |
$log->info("processing ",($maxmfn-$startmfn)." records..."); |
217 |
|
|
218 |
# read database |
# read database |
219 |
for (my $mfn = 1; $mfn <= $maxmfn; $mfn++) { |
for (my $mfn = $startmfn; $mfn <= $maxmfn; $mfn++) { |
220 |
|
|
221 |
|
|
222 |
$log->debug("mfn: $mfn\n"); |
$log->debug("mfn: $mfn\n"); |
264 |
|
|
265 |
} |
} |
266 |
|
|
267 |
$self->{'current_mfn'} = 1; |
$self->{'current_mfn'} = $startmfn; |
268 |
$self->{'last_pcnt'} = 0; |
$self->{'last_pcnt'} = 0; |
269 |
|
|
270 |
$log->debug("max mfn: $maxmfn"); |
$log->debug("max mfn: $maxmfn"); |
329 |
|
|
330 |
$self->{'last_pcnt'} ||= 1; |
$self->{'last_pcnt'} ||= 1; |
331 |
|
|
|
$self->{'last_pcnt'} = $curr if ($curr < $self->{'last_pcnt'}); |
|
|
|
|
332 |
my $p = int($curr * 100 / $max); |
my $p = int($curr * 100 / $max); |
333 |
|
|
334 |
|
# reset on re-run |
335 |
|
if ($p < $self->{'last_pcnt'}) { |
336 |
|
$self->{'last_pcnt'} = $p; |
337 |
|
$self->{'last_t'} = time(); |
338 |
|
$self->{'last_curr'} = 1; |
339 |
|
} |
340 |
|
|
341 |
if ($p != $self->{'last_pcnt'}) { |
if ($p != $self->{'last_pcnt'}) { |
342 |
printf STDERR ("%5d / %5d [%-51s] %-2d %% \r",$curr,$max,"=" x ($p/2).">", $p ); |
|
343 |
|
my $last_curr = $self->{'last_curr'} || $curr; |
344 |
|
my $t = time(); |
345 |
|
my $rate = ($curr - $last_curr) / (($t - $self->{'last_t'} || 1)); |
346 |
|
my $eta = ($max-$curr) / ($rate || 1); |
347 |
|
printf STDERR ("%5d [%-38s] %-5d %0.1f/s %s\r",$curr,"=" x ($p/3)."$p%>", $max, $rate, $self->fmt_time($eta)); |
348 |
$self->{'last_pcnt'} = $p; |
$self->{'last_pcnt'} = $p; |
349 |
|
$self->{'last_t'} = time(); |
350 |
|
$self->{'last_curr'} = $curr; |
351 |
} |
} |
352 |
print STDERR "\n" if ($p == 100); |
print STDERR "\n" if ($p == 100); |
353 |
} |
} |
354 |
|
|
355 |
|
=head2 fmt_time |
356 |
|
|
357 |
|
Format time (in seconds) for display. |
358 |
|
|
359 |
|
print $webpac->fmt_time(time()); |
360 |
|
|
361 |
|
This method is called by L<progress_bar> to display remaining time. |
362 |
|
|
363 |
|
=cut |
364 |
|
|
365 |
|
sub fmt_time { |
366 |
|
my $self = shift; |
367 |
|
|
368 |
|
my $t = shift || 0; |
369 |
|
my $out = ""; |
370 |
|
|
371 |
|
my ($ss,$mm,$hh) = gmtime($t); |
372 |
|
$out .= "${hh}h" if ($hh); |
373 |
|
$out .= sprintf("%02d:%02d", $mm,$ss); |
374 |
|
$out .= " " if ($hh == 0); |
375 |
|
return $out; |
376 |
|
} |
377 |
|
|
378 |
=head2 open_import_xml |
=head2 open_import_xml |
379 |
|
|
380 |
Read file from C<import_xml/> directory and parse it. |
Read file from C<import_xml/> directory and parse it. |
1057 |
also use method names, and not only classes (which are just few) |
also use method names, and not only classes (which are just few) |
1058 |
to filter logging. |
to filter logging. |
1059 |
|
|
1060 |
|
|
1061 |
|
=head1 MEMORY USAGE |
1062 |
|
|
1063 |
|
C<low_mem> options is double-edged sword. If enabled, WebPAC |
1064 |
|
will run on memory constraint machines (which doesn't have enough |
1065 |
|
physical RAM to create memory structure for whole source database). |
1066 |
|
|
1067 |
|
If your machine has 512Mb or more of RAM and database is around 10000 records, |
1068 |
|
memory shouldn't be an issue. If you don't have enough physical RAM, you |
1069 |
|
might consider using virtual memory (if your operating system is handling it |
1070 |
|
well, like on FreeBSD or Linux) instead of dropping to L<DBD::Deep> to handle |
1071 |
|
parsed structure of ISIS database (this is what C<low_mem> option does). |
1072 |
|
|
1073 |
|
Hitting swap at end of reading source database is probably o.k. However, |
1074 |
|
hitting swap before 90% will dramatically decrease performance and you will |
1075 |
|
be better off with C<low_mem> and using rest of availble memory for |
1076 |
|
operating system disk cache (Linux is particuallary good about this). |
1077 |
|
However, every access to database record will require disk access, so |
1078 |
|
generation phase will be slower 10-100 times. |
1079 |
|
|
1080 |
|
Parsed structures are essential - you just have option to trade RAM memory |
1081 |
|
(which is fast) for disk space (which is slow). Be sure to have planty of |
1082 |
|
disk space if you are using C<low_mem> and thus L<DBD::Deep>. |
1083 |
|
|
1084 |
|
However, when WebPAC is running on desktop machines (or laptops :-), it's |
1085 |
|
highly undesireable for system to start swapping. Using C<low_mem> option can |
1086 |
|
reduce WecPAC memory usage to around 64Mb for same database with lookup |
1087 |
|
fields and sorted indexes which stay in RAM. Performance will suffer, but |
1088 |
|
memory usage will really be minimal. It might be also more confortable to |
1089 |
|
run WebPAC reniced on those machines. |
1090 |
|
|
1091 |
=cut |
=cut |
1092 |
|
|
1093 |
1; |
1; |