/[pub]/suikawiki/script/lib/SuikaWiki/DB/Hash.pm
Suika

Contents of /suikawiki/script/lib/SuikaWiki/DB/Hash.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (hide annotations) (download)
Tue Nov 25 12:41:16 2003 UTC (22 years, 9 months ago) by wakaba
Branch: MAIN
Changes since 1.3: +11 -11 lines
Throw exception instead of old error raising interface

1 wakaba 1.1
2     =head1 NAME
3    
4     SuikaWiki::DB::Hash --- SuikaWiki WikiDatabase: WikiDatabase interface wrapper for hash
5    
6     =head1 DESCRIPTION
7    
8     This module wrappes perl's hash with WikiDatabase common interface of
9     SuikaWiki. It is useful for tied hash.
10    
11     This module is part of SuikaWiki.
12    
13     =cut
14    
15     package SuikaWiki::DB::Hash;
16 wakaba 1.2 use strict;
17 wakaba 1.4 our $VERSION=do{my @r=(q$Revision: 1.3 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
18 wakaba 1.1 require SuikaWiki::DB::Util;
19    
20     sub new ($%) {
21     my ($class, %o) = @_;
22     my $self = bless {}, $class;
23     if ($o{-lock}) {
24 wakaba 1.3 $self->{lock} = SuikaWiki::DB::Util->new_lock ($o{-lock});
25 wakaba 1.1 $self->{lock}->lock;
26     }
27 wakaba 1.2 $self->{db_hash} = &{$o{constructor} || sub {return {}}} ($self, \%o);
28 wakaba 1.1 unless ($self->{db_hash}) {
29 wakaba 1.4 throw SuikaWiki::DB::Util::Error type => 'DB_OPEN';
30 wakaba 1.1 }
31     $self->{has_exist} = defined $o{has_exist} ? $o{has_exist} : 1;
32     $self;
33     }
34    
35     sub get ($$$) {
36     my ($self, $prop, $key) = @_;
37     if (scalar @$key > 1) {
38 wakaba 1.4 throw SuikaWiki::DB::Util::Error type => 'KEY_INVALID_NAME', -key => $key;
39 wakaba 1.1 } else {
40     $self->{db_hash}->{$key->[0]};
41     }
42     }
43    
44     sub set ($$$$) {
45     my ($self, $prop, $key => $value) = @_;
46     if (scalar @$key > 1) {
47 wakaba 1.4 throw SuikaWiki::DB::Util::Error type => 'KEY_INVALID_NAME', -key => $key;
48 wakaba 1.1 } else {
49     $self->{db_hash}->{$key->[0]} = $value;
50     }
51     }
52    
53     sub exist ($$$) {
54     my ($self, $prop, $key) = @_;
55     if (scalar @$key > 1) {
56 wakaba 1.4 throw SuikaWiki::DB::Util::Error type => 'KEY_INVALID_NAME', -key => $key;
57 wakaba 1.1 } else {
58     if ($self->{has_exist}) {
59     return exist $self->{db_hash}->{$key->[0]};
60     } else {
61     return defined $self->{db_hash}->{$key->[0]} ? 1 : 0;
62     }
63     }
64     }
65    
66     sub delete ($$$) {
67     my ($self, $prop, $key) = @_;
68     if (scalar @$key > 1) {
69 wakaba 1.4 throw SuikaWiki::DB::Util::Error type => 'KEY_INVALID_NAME', -key => $key;
70 wakaba 1.1 } else {
71     delete $self->{db_hash}->{$key->[0]};
72     }
73     }
74    
75     sub keys ($$;%) {
76     my ($self, $prop, %opt) = @_;
77     if (scalar @{$opt{ns}} > 1) {
78 wakaba 1.4 throw SuikaWiki::DB::Util::Error type => 'KEY_INVALID_NS_NAME',
79     -key => $opt{ns};
80 wakaba 1.1 } else {
81     return map {[$_]} keys %{$self->{db_hash}};
82     }
83     }
84    
85     sub close ($) {
86     my $self = shift;
87 wakaba 1.4 ($self->{destructor} or sub { 1 })->($self)
88     or throw SuikaWiki::DB::Util::Error type => 'DB_CLOSE';
89 wakaba 1.1 $self->{db_hash} = undef;
90 wakaba 1.3 $self->{lock}->unlock if $self->{lock};
91     $self->{lock} = undef;
92 wakaba 1.1 }
93    
94     sub DESTROY ($) {
95     my $self = shift;
96     $self->close if $self->{db_hash};
97     }
98    
99     =head1 METHODS
100    
101     This module provides common interface of SuikaWiki WikiDatabase
102     modules. See C<SuikaWiki::DB>.
103    
104 wakaba 1.2 =head1 SEE ALSO
105    
106     C<SuikaWiki::DB>.
107    
108 wakaba 1.1 =head1 AUTHOR
109    
110     Wakaba <[email protected]>.
111    
112     =head1 LICENSE
113    
114 wakaba 1.2 Copyright AUTHOR 2003.
115 wakaba 1.1
116     This program is free software; you can redistribute it and/or
117     modify it under the same terms as Perl itself.
118    
119     =cut
120    
121 wakaba 1.4 1; # $Date: 2003/10/05 11:54:03 $

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24