package CGI::Armature::Database; use strict; use Fcntl; use DB_File::Lock; use CGI::Armature::PathInfo; my %path_info = CGI::Armature::PathInfo::get_paths(); sub new { my $class = shift; my ($dbfile) = @_; die "need dbfile" unless $dbfile; my $self = { dbfile=>$dbfile, }; return bless($self, $class); } sub create { my $self = shift; my %db_file; my $locking = { mode=>'write', lockfile_name=>"$path_info{sessionpath}/exclusive.lock", }; tie( %db_file, 'DB_File::Lock', "$path_info{sessionpath}/$self->{dbfile}", O_RDWR|O_CREAT, 0600, $DB_HASH, $locking, ) or die "Couldn't tie DB_File $path_info{sessionpath}/$self->{dbfile} $!; aborting"; untie %db_file; } sub delete { my $self = shift; my $key = shift; my %db_file; my $locking = { mode=>'write', lockfile_name=>"$path_info{sessionpath}/exclusive.lock", }; tie( %db_file, 'DB_File::Lock', "$path_info{sessionpath}/$self->{dbfile}", O_RDWR, 0600, $DB_HASH, $locking, ) or die "Couldn't tie DB_File $path_info{sessionpath}/$self->{dbfile} $!; aborting"; delete $db_file{$key}; untie %db_file; } sub set_value { my $self = shift; die "not enough values for set_value" unless @_ == 2; my ($key, $value) = @_; my %db_file; my $locking = { mode=>'write', lockfile_name=>"$path_info{sessionpath}/exclusive.lock", }; tie( %db_file, 'DB_File::Lock', "$path_info{sessionpath}/$self->{dbfile}", O_RDWR, 0600, $DB_HASH, $locking, ) or die "Couldn't tie DB_File $path_info{sessionpath}/$self->{dbfile} $!; aborting"; $db_file{$key} = $value; untie %db_file; } sub get_value { my $self = shift; my $key = shift; my %db_file; my $locking = { mode=>'read', lockfile_name=>"$path_info{sessionpath}/exclusive.lock", }; tie( %db_file, 'DB_File::Lock', "$path_info{sessionpath}/$self->{dbfile}", O_RDONLY, 0600, $DB_HASH, $locking, ) or die "Couldn't tie DB_File $path_info{sessionpath}/$self->{dbfile} $!; aborting"; my $value = $db_file{$key}; untie %db_file; return $value; } sub get_keys { my $self = shift; my %db_file; my $locking = { mode=>'read', lockfile_name=>"$path_info{sessionpath}/exclusive.lock", }; tie( %db_file, 'DB_File::Lock', "$path_info{sessionpath}/$self->{dbfile}", O_RDONLY, 0600, $DB_HASH, $locking, ) or die "Couldn't tie DB_File $path_info{sessionpath}/$self->{dbfile} $!; aborting"; my @keys = keys %db_file; untie %db_file; return \@keys; } 1;