package Yaa::LookupMap::Driver::BerkeleyDB;

# inherit everything from Yaa::LookupMap
@ISA = qw(Yaa::LookupMap);

# set debugging constant if YAA_DEBUG environment variable is set
BEGIN {
	use constant DEBUG => ((defined $ENV{YAA_DEBUG}) ? 1 : 0);
	require File::Basename if (DEBUG);
}

use strict;
use BerkeleyDB;
use vars qw ($VERSION);

=head1 NAME

Yaa::LookupMap::Driver::BerkeleyDB - bdb lookup map driver


This driver supports queries to BerkeleyDB vesion 2/3/4 database files.

WARNING: Do not use this class standalone!

=cut

=head1 SYNOPSIS

my $bdb_map = Yaa::LookupMap->factory(

	'driver' => 'BerkeleyDB',
	'type' => 'Hash',
	'file' => "/tmp/database";
	'-Flags' => 0;
	'-Cachesize' => 1000;
	'-Pagesize' => 1000;
);

my $result = $bdb_map->lookup($key);

# prints out query result

print $result->getMatchValueByKey('result') if (defined $result);

B<Result keys>: This driver returns result object with only one key named B<result>

=cut

######################################################
#                GLOBAL VARIABLES                    #
######################################################
my $VERSION = "0.1";

######################################################
#               OBJECT CONSTRUCTOR                   #
######################################################

=head1 OBJECT CONSTRUCTOR

Object constructor options:

B<'file'> - path to the database file (database must exist)

B<'type'> - database type {'Hash' | 'Btree' | 'Recno' | 'Queue' } (default: Hash)

B<'-Flags'> (default: DB_RDONLY)

B<'-Cachesize'> cache size in bytes

B<'-Pagesize'> page size in bytes

B<'-Env'> - initialized BerkeleyDB::Env object

B<'-Encrypt'>

For detailed option documentation see perldoc BerkeleyDB.

=cut
sub new {
	my $proto = shift;
	my $class = ref($proto) || $proto;
	
	# call parent constructor
	my $self = Yaa::LookupMap->new();

	##################################################
	#               PUBLIC VARS                      #
	##################################################
	
	##################################################
	#              PRIVATE VARS                      #
	##################################################
	
	# database handle
	$self->{_db} = undef;
	
	bless($self, $class);
	
	# clear parameters
	$self->clearParams();

	# set object properties	
	$self->setParams(@_);

	return $self;
}

####################################################
#               PUBLIC  METHODS                    #
####################################################

sub clearParams {
	my ($self) = @_;
	$self->{_db} = undef;
	$self->{type} = 'Hash';
	
	$self->{'-Filename'} = undef;
	$self->{'-Flags'} = DB_RDONLY;
	$self->{'-Cachesize'} = undef;
	$self->{'-Pagesize'} = undef;
	$self->{'-Env'} = undef;
	$self->{'-Encrypt'} = undef;

	return 1;
}

####################################################
#               PRIVATE METHODS                    #
####################################################

sub _lookup {
	my ($self, $key) = @_;
	# open database
	return undef unless ($self->_dbInit());
	
	my $value = undef;
	if ($self->{_db}->db_get($key, $value) != 0) {
		$self->{error} = "Error querying database: " . $BerkeleyDB::Error;
		return undef;
	}
	
	my $result = Yaa::LookupMap::Result->new;
	$result->addMatch({ result => $value });
	return $result;
}

# opens database
sub _dbInit {
	my ($self) = @_;
	
	if (! grep(/^$self->{type}$/, 'Hash', 'Btree', 'Recno', 'Queue')) {
		$self->{error} = "Unknown database format: " . $self->{type};
		return 0;
	}

	# try to open database
	$self->{_db} = ("BerkeleyDB::" . $self->{type})->new(
		'-Filename' => $self->{'file'},
		'-Flags' => $self->{'-Flags'},
		'-Cachesize' => $self->{'-Cachesize'},
		'-Pagesize' => $self->{'-Pagesize'},
		'-Env' => $self->{'-Env'},
		'-Encrypt' => $self->{'-Encrypt'},
	);
	
	if (! defined $self->{_db}) {
		$self->{error} = "Unable to open database: " . $! . ": " . $BerkeleyDB::Error;
		return 0;
	}
	
	return 1;
}

1;

=head1 SEE ALSO

For general class usage, please see perldoc Yaa::LookupMap.

=cut

=head1 AUTHOR

Branko F. Gracnar <bfg@frost.ath.cx>
http://frost.ath.cx/~bfg

=cut

=head1 LICENSE

This software is released under BSD-style license. If you like this software, send me a nice postcard from somewhere to the snail mail address published on my website.

=cut

