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

Contents of /suikawiki/script/lib/SuikaWiki/DB/FileSystem/YukiWikiDBNS.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (hide annotations) (download)
Mon Dec 1 07:44:51 2003 UTC (22 years, 9 months ago) by wakaba
Branch: MAIN
Changes since 1.4: +5 -4 lines
__check_name: Check undefined value

1 wakaba 1.1
2     =head1 NAME
3    
4     SuikaWiki::DB::FileSystem::YukiWikiDBNS --- SuikaWiki WikiDatabase: WikiDatabase interface wrapper for Yuki::YukiWikiDBNS
5    
6     =head1 DESCRIPTION
7    
8     This module wrappes Yuki::YukiWikiDBNS (directory structuring extended
9     version of Yuki::YukiWikiDB2) for WikiDatabase common interface of
10     SuikaWiki.
11    
12     This module is part of SuikaWiki.
13    
14     =cut
15    
16     package SuikaWiki::DB::FileSystem::YukiWikiDBNS;
17 wakaba 1.2 use strict;
18 wakaba 1.5 our $VERSION = do{my @r=(q$Revision: 1.4 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
19 wakaba 1.1 require SuikaWiki::DB::Util;
20     require Yuki::YukiWikiDBNS;
21     my $Self = '.';
22     my $Parent = '..';
23     my $Sep = '//';
24     my $DB_Module = q(Yuki::YukiWikiDBNS);
25    
26     sub new ($%) {
27     my ($class, %o) = @_;
28     my $self = bless {}, $class;
29     if ($o{-lock}) {
30 wakaba 1.3 $self->{lock} = SuikaWiki::DB::Util->new_lock ($o{-lock});
31     $self->{lock}->lock
32 wakaba 1.4 or throw SuikaWiki::DB::Util::Error
33     type => 'LOCK_START', -method => 'new';
34 wakaba 1.1 }
35     my %db;
36     $self->{db_instance} = tie (%db, $DB_Module => $o{directory},
37 wakaba 1.3 -lock => 0, ## Yuki::YukiWikiDBNS's lock is buggy
38 wakaba 1.1 -backup => $o{use_history},
39     -logfile => $o{logfile},
40     -extension => $o{suffix},
41     )
42 wakaba 1.4 or throw SuikaWiki::DB::Util::Error
43     type => 'DB_OPEN',
44     -method => q(new),
45     -file => $o{directory};
46 wakaba 1.1 $self->{db_hash} = \%db;
47     $self;
48     }
49    
50     sub get ($$$) {
51     my ($self, $prop, $key) = @_;
52 wakaba 1.2 $self->{db_hash}->{ $self->__name2name ($key) };
53 wakaba 1.1 }
54    
55     sub set ($$$$) {
56     my ($self, $prop, $key => $value) = @_;
57 wakaba 1.3 if ($self->{lock}->writable) {
58     $self->{db_hash}->{ $self->__name2name ($key) } = $value;
59     } else {
60 wakaba 1.4 throw SuikaWiki::DB::Util::Error
61     type => 'KEY_SAVE_LOCKED',
62     -key => $key,
63     -prop => $prop;
64 wakaba 1.3 }
65 wakaba 1.1 }
66    
67     sub exist ($$$) {
68     my ($self, $prop, $key) = @_;
69 wakaba 1.5 CORE::exists $self->{db_hash}->{ $self->__name2name ($key) };
70 wakaba 1.1 }
71    
72     sub delete ($$$) {
73     my ($self, $prop, $key) = @_;
74 wakaba 1.3 if ($self->{lock}->writable) {
75 wakaba 1.5 CORE::delete $self->{db_hash}->{ $self->__name2name ($key) };
76 wakaba 1.3 } else {
77 wakaba 1.4 throw SuikaWiki::DB::Util::Error
78     type => 'KEY_SAVE_LOCKED',
79     -key => $key,
80     -prop => $prop;
81 wakaba 1.3 }
82 wakaba 1.1 }
83    
84     sub keys ($$;%) {
85     my ($self, $prop, %opt) = @_;
86 wakaba 1.3 map {[split /\Q$Sep\E/, $_]}
87     $self->{db_instance}->list_items ({ns => $self->__name2ns ($opt{-ns}||[]),
88     type => ($opt{-type} eq 'ns'?'ns':'key'),
89     recursive => $opt{-recursive}});
90 wakaba 1.1 }
91    
92     sub __name2name ($$) {
93     my ($self, $key) = @_;
94 wakaba 1.4 throw SuikaWiki::DB::Util::Error
95     type => 'KEY_INVALID_NAME', -key => $key
96 wakaba 1.3 unless $self->__check_name ($key);
97 wakaba 1.1 join '//', @$key;
98     }
99    
100     sub __name2ns ($$) {
101     my ($self, $key) = @_;
102 wakaba 1.3 return '' if scalar @$key == 0 || (scalar @$key == 1 && $key->[0] eq '');
103 wakaba 1.4 throw SuikaWiki::DB::Util::Error
104     type => 'KEY_INVALID_NS_NAME', -key => $key
105 wakaba 1.3 unless $self->__check_name ($key);
106 wakaba 1.1 $key = $self->__name2name ($key) . $Sep;
107 wakaba 1.3 $key eq $Sep ? undef : $key;
108 wakaba 1.1 }
109    
110     sub __check_name ($$) {
111     my ($self, $key) = @_;
112 wakaba 1.5 return 0 unless ref $key;
113 wakaba 1.1 for (@$key) {
114     return 0 unless $_; # '' or 0 or undef
115     return 0 if index ($_, $Sep) > -1;
116     return 0 if $_ eq $Self || $_ eq $Parent;
117     }
118     return 1;
119     }
120    
121     sub close ($) {
122     my $self = shift;
123     untie %{$self->{db_hash}};
124     $self->{db_instance} = undef;
125     $self->{db_hash} = undef;
126 wakaba 1.3 $self->{lock}->unlock if $self->{lock};
127     $self->{lock} = undef;
128 wakaba 1.4 throw SuikaWiki::DB::Util::Error
129     type => 'DB_CLOSED',
130     -method => 'close';
131 wakaba 1.1 }
132    
133     sub DESTROY ($) {
134     my $self = shift;
135     $self->close if $self->{db_instance};
136 wakaba 1.4 throw SuikaWiki::DB::Util::Error
137     type => 'DB_DESTROY',
138     -method => 'DESTROY';
139 wakaba 1.1 }
140    
141     =head1 METHODS
142    
143     This module provides common interface of SuikaWiki WikiDatabase
144     modules. See C<SuikaWiki::DB>.
145    
146     =head2 CONSTRUCTOR'S OPTIONS
147    
148     @@ TBD
149    
150     =head1 AUTHOR
151    
152     Wakaba <[email protected]>.
153    
154     =head1 LICENSE
155    
156     Copyright 2003 Wakaba <[email protected]>
157    
158     This program is free software; you can redistribute it and/or
159     modify it under the same terms as Perl itself.
160    
161     =cut
162    
163 wakaba 1.5 1; # $Date: 2003/11/25 12:41:16 $

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24