=head1 NAME SuikaWiki::DB::Hash --- SuikaWiki WikiDatabase: WikiDatabase interface wrapper for hash =head1 DESCRIPTION This module wrappes perl's hash with WikiDatabase common interface of SuikaWiki. It is useful for tied hash. This module is part of SuikaWiki. =cut package SuikaWiki::DB::Hash; require SuikaWiki::DB::Util; sub new ($%) { my ($class, %o) = @_; my $self = bless {}, $class; if ($o{-lock}) { $self->{lock} = SuikaWiki::DB::Util->new_lock ($self->{-lock}); $self->{lock}->lock; } $self->{error} = SuikaWiki::DB::Util->error_handler; $self->{db_hash} = &{$o{constructor} || sub {return {}}} ($self, \%opt); unless ($self->{db_hash}) { $self->{error}->raise (type => 'DB_OPEN'); } $self->{has_exist} = defined $o{has_exist} ? $o{has_exist} : 1; $self; } sub get ($$$) { my ($self, $prop, $key) = @_; if (scalar @$key > 1) { $self->{error}->raise (type => 'KEY_INVALID_NAME', key => $key); } else { $self->{db_hash}->{$key->[0]}; } } sub set ($$$$) { my ($self, $prop, $key => $value) = @_; if (scalar @$key > 1) { $self->{error}->raise (type => 'KEY_INVALID_NAME', key => $key); } else { $self->{db_hash}->{$key->[0]} = $value; } } sub exist ($$$) { my ($self, $prop, $key) = @_; if (scalar @$key > 1) { $self->{error}->raise (type => 'KEY_INVALID_NAME', key => $key); } else { if ($self->{has_exist}) { return exist $self->{db_hash}->{$key->[0]}; } else { return defined $self->{db_hash}->{$key->[0]} ? 1 : 0; } } } sub delete ($$$) { my ($self, $prop, $key) = @_; if (scalar @$key > 1) { $self->{error}->raise (type => 'KEY_INVALID_NAME', key => $key); } else { delete $self->{db_hash}->{$key->[0]}; } } sub keys ($$;%) { my ($self, $prop, %opt) = @_; if (scalar @{$opt{ns}} > 1) { $self->{error}->raise (type => 'KEY_INVALID_NAME', key => $key); } else { return map {[$_]} keys %{$self->{db_hash}}; } } sub close ($) { my $self = shift; &{$self->{destructor} || sub { 1 }} ($self) or $self->{error}->raise (type => 'DB_CLOSE'); $self->{db_hash} = undef; } sub DESTROY ($) { my $self = shift; $self->close if $self->{db_hash}; } =head1 METHODS This module provides common interface of SuikaWiki WikiDatabase modules. See C. =head1 AUTHOR Wakaba . =head1 LICENSE Copyright 2003 Wakaba This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut 1; # $Date: 2003/08/06 01:26:14 $