/[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.2 - (hide annotations) (download)
Wed Aug 6 02:54:40 2003 UTC (21 years, 9 months ago) by wakaba
Branch: MAIN
Branch point for: branch-suikawiki-1
Changes since 1.1: +10 -4 lines
Minor but important bug fixes

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     our $VERSION=do{my @r=(q$Revision: 1.10 $=~/\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     $self->{lock} = SuikaWiki::DB::Util->new_lock ($self->{-lock});
25     $self->{lock}->lock;
26     }
27     $self->{error} = SuikaWiki::DB::Util->error_handler;
28 wakaba 1.2 $self->{db_hash} = &{$o{constructor} || sub {return {}}} ($self, \%o);
29 wakaba 1.1 unless ($self->{db_hash}) {
30     $self->{error}->raise (type => 'DB_OPEN');
31     }
32     $self->{has_exist} = defined $o{has_exist} ? $o{has_exist} : 1;
33     $self;
34     }
35    
36     sub get ($$$) {
37     my ($self, $prop, $key) = @_;
38     if (scalar @$key > 1) {
39     $self->{error}->raise (type => 'KEY_INVALID_NAME', key => $key);
40     } else {
41     $self->{db_hash}->{$key->[0]};
42     }
43     }
44    
45     sub set ($$$$) {
46     my ($self, $prop, $key => $value) = @_;
47     if (scalar @$key > 1) {
48     $self->{error}->raise (type => 'KEY_INVALID_NAME', key => $key);
49     } else {
50     $self->{db_hash}->{$key->[0]} = $value;
51     }
52     }
53    
54     sub exist ($$$) {
55     my ($self, $prop, $key) = @_;
56     if (scalar @$key > 1) {
57     $self->{error}->raise (type => 'KEY_INVALID_NAME', key => $key);
58     } else {
59     if ($self->{has_exist}) {
60     return exist $self->{db_hash}->{$key->[0]};
61     } else {
62     return defined $self->{db_hash}->{$key->[0]} ? 1 : 0;
63     }
64     }
65     }
66    
67     sub delete ($$$) {
68     my ($self, $prop, $key) = @_;
69     if (scalar @$key > 1) {
70     $self->{error}->raise (type => 'KEY_INVALID_NAME', key => $key);
71     } else {
72     delete $self->{db_hash}->{$key->[0]};
73     }
74     }
75    
76     sub keys ($$;%) {
77     my ($self, $prop, %opt) = @_;
78     if (scalar @{$opt{ns}} > 1) {
79 wakaba 1.2 $self->{error}->raise (type => 'KEY_INVALID_NAME', 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     &{$self->{destructor} || sub { 1 }} ($self)
88     or $self->{error}->raise (type => 'DB_CLOSE');
89     $self->{db_hash} = undef;
90     }
91    
92     sub DESTROY ($) {
93     my $self = shift;
94     $self->close if $self->{db_hash};
95     }
96    
97     =head1 METHODS
98    
99     This module provides common interface of SuikaWiki WikiDatabase
100     modules. See C<SuikaWiki::DB>.
101    
102 wakaba 1.2 =head1 SEE ALSO
103    
104     C<SuikaWiki::DB>.
105    
106 wakaba 1.1 =head1 AUTHOR
107    
108     Wakaba <w@suika.fam.cx>.
109    
110     =head1 LICENSE
111    
112 wakaba 1.2 Copyright AUTHOR 2003.
113 wakaba 1.1
114     This program is free software; you can redistribute it and/or
115     modify it under the same terms as Perl itself.
116    
117     =cut
118    
119 wakaba 1.2 1; # $Date: 2003/08/06 01:26:14 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24  
Google Analytics is used in this page; Cookies are used. 忍者AdMax is used in this page; Cookies are used. Privacy policy.