1 |
package WebPAC::Input; |
2 |
|
3 |
use warnings; |
4 |
use strict; |
5 |
|
6 |
=head1 NAME |
7 |
|
8 |
WebPAC::Input - core module for input file format |
9 |
|
10 |
=head1 VERSION |
11 |
|
12 |
Version 0.01 |
13 |
|
14 |
=cut |
15 |
|
16 |
our $VERSION = '0.01'; |
17 |
|
18 |
=head1 SYNOPSIS |
19 |
|
20 |
This module will load particular loader module and execute it's functions. |
21 |
|
22 |
Perhaps a little code snippet. |
23 |
|
24 |
use WebPAC::Input; |
25 |
|
26 |
my $db = WebPAC::Input->new( |
27 |
format => 'NULL', |
28 |
config => $config, |
29 |
lookup => $lookup_obj, |
30 |
low_mem => 1, |
31 |
); |
32 |
|
33 |
$db->open('/path/to/database'); |
34 |
print "database size: ",$db->size,"\n"; |
35 |
while (my $row = $db->fetch) { |
36 |
... |
37 |
} |
38 |
$db->close; |
39 |
|
40 |
=head1 FUNCTIONS |
41 |
|
42 |
=head2 new |
43 |
|
44 |
Create new input database object. |
45 |
|
46 |
my $db = new WebPAC::Input( |
47 |
format => 'NULL' |
48 |
code_page => 'ISO-8859-2', |
49 |
low_mem => 1, |
50 |
); |
51 |
|
52 |
Optional parametar C<code_page> specify application code page (which will be |
53 |
used internally). This should probably be your terminal encoding, and by |
54 |
default, it C<ISO-8859-2>. |
55 |
|
56 |
Default is not to use C<low_mem> options (see L<MEMORY USAGE> below). |
57 |
|
58 |
=cut |
59 |
|
60 |
sub new { |
61 |
my $class = shift; |
62 |
my $self = {@_}; |
63 |
bless($self, $class); |
64 |
|
65 |
$self->{'code_page'} ||= 'ISO-8859-2'; |
66 |
|
67 |
my $log = $self->_get_logger; |
68 |
|
69 |
# running with low_mem flag? well, use DBM::Deep then. |
70 |
if ($self->{'low_mem'}) { |
71 |
$log->info("running with low_mem which impacts performance (<32 Mb memory usage)"); |
72 |
|
73 |
my $db_file = "data.db"; |
74 |
|
75 |
if (-e $db_file) { |
76 |
unlink $db_file or $log->logdie("can't remove '$db_file' from last run"); |
77 |
$log->debug("removed '$db_file' from last run"); |
78 |
} |
79 |
|
80 |
require DBM::Deep; |
81 |
|
82 |
my $db = new DBM::Deep $db_file; |
83 |
|
84 |
$log->logdie("DBM::Deep error: $!") unless ($db); |
85 |
|
86 |
if ($db->error()) { |
87 |
$log->logdie("can't open '$db_file' under low_mem: ",$db->error()); |
88 |
} else { |
89 |
$log->debug("using file '$db_file' for DBM::Deep"); |
90 |
} |
91 |
|
92 |
$self->{'db'} = $db; |
93 |
} |
94 |
|
95 |
$self ? return $self : return undef; |
96 |
} |
97 |
|
98 |
=head1 MEMORY USAGE |
99 |
|
100 |
C<low_mem> options is double-edged sword. If enabled, WebPAC |
101 |
will run on memory constraint machines (which doesn't have enough |
102 |
physical RAM to create memory structure for whole source database). |
103 |
|
104 |
If your machine has 512Mb or more of RAM and database is around 10000 records, |
105 |
memory shouldn't be an issue. If you don't have enough physical RAM, you |
106 |
might consider using virtual memory (if your operating system is handling it |
107 |
well, like on FreeBSD or Linux) instead of dropping to L<DBM::Deep> to handle |
108 |
parsed structure of ISIS database (this is what C<low_mem> option does). |
109 |
|
110 |
Hitting swap at end of reading source database is probably o.k. However, |
111 |
hitting swap before 90% will dramatically decrease performance and you will |
112 |
be better off with C<low_mem> and using rest of availble memory for |
113 |
operating system disk cache (Linux is particuallary good about this). |
114 |
However, every access to database record will require disk access, so |
115 |
generation phase will be slower 10-100 times. |
116 |
|
117 |
Parsed structures are essential - you just have option to trade RAM memory |
118 |
(which is fast) for disk space (which is slow). Be sure to have planty of |
119 |
disk space if you are using C<low_mem> and thus L<DBM::Deep>. |
120 |
|
121 |
However, when WebPAC is running on desktop machines (or laptops :-), it's |
122 |
highly undesireable for system to start swapping. Using C<low_mem> option can |
123 |
reduce WecPAC memory usage to around 64Mb for same database with lookup |
124 |
fields and sorted indexes which stay in RAM. Performance will suffer, but |
125 |
memory usage will really be minimal. It might be also more confortable to |
126 |
run WebPAC reniced on those machines. |
127 |
|
128 |
|
129 |
=head1 AUTHOR |
130 |
|
131 |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
132 |
|
133 |
=head1 COPYRIGHT & LICENSE |
134 |
|
135 |
Copyright 2005 Dobrica Pavlinusic, All Rights Reserved. |
136 |
|
137 |
This program is free software; you can redistribute it and/or modify it |
138 |
under the same terms as Perl itself. |
139 |
|
140 |
=cut |
141 |
|
142 |
1; # End of WebPAC::Input |