package WebPAC::Input;

use warnings;
use strict;

=head1 NAME

WebPAC::Input - core module for input file format

=head1 VERSION

Version 0.01

=cut

our $VERSION = '0.01';

=head1 SYNOPSIS

This module will load particular loader module and execute it's functions.

Perhaps a little code snippet.

    use WebPAC::Input;

    my $db = WebPAC::Input->new(
    	format => 'NULL',
	config => $config,
	lookup => $lookup_obj,
	low_mem => 1,
    );

    $db->open('/path/to/database');
    print "database size: ",$db->size,"\n";
    while (my $row = $db->fetch) {
	...
    }
    $db->close;

=head1 FUNCTIONS

=head2 new

Create new input database object.

  my $db = new WebPAC::Input(
  	format => 'NULL'
	code_page => 'ISO-8859-2',
	low_mem => 1,
  );

Optional parametar C<code_page> specify application code page (which will be
used internally). This should probably be your terminal encoding, and by
default, it C<ISO-8859-2>.

Default is not to use C<low_mem> options (see L<MEMORY USAGE> below).

=cut

sub new {
        my $class = shift;
        my $self = {@_};
	bless($self, $class);

	$self->{'code_page'} ||= 'ISO-8859-2';

	my $log = $self->_get_logger;

	# 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;
}

=head1 MEMORY USAGE

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 source database).

If your machine has 512Mb or more of RAM 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<DBM::Deep> to handle
parsed structure of ISIS database (this is what C<low_mem> option does).

Hitting swap at end of reading source database is probably o.k. However,
hitting swap before 90% will dramatically decrease performance and you will
be better off with C<low_mem> and using rest of availble memory for
operating system disk cache (Linux is particuallary good about this).
However, every access to database record will require disk access, so
generation phase will be slower 10-100 times.

Parsed structures are essential - you just have option to trade RAM memory
(which is fast) for disk space (which is slow). Be sure to have planty of
disk space if you are using C<low_mem> and thus L<DBM::Deep>.

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 around 64Mb 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.


=head1 AUTHOR

Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>

=head1 COPYRIGHT & LICENSE

Copyright 2005 Dobrica Pavlinusic, All Rights Reserved.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut

1; # End of WebPAC::Input
