/[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.9 - (show annotations) (download)
Thu Apr 1 04:45:06 2004 UTC (22 years, 5 months ago) by wakaba
Branch: MAIN
CVS Tags: suikawiki3-redirect, release-3-0-0, HEAD
Branch point for: paragraph-200404, helowiki, helowiki-2005
Changes since 1.8: +6 -2 lines
New option root_key implemented

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 use strict;
18 our $VERSION = do{my @r=(q$Revision: 1.8 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
19 require SuikaWiki::DB::Util;
20 require Yuki::YukiWikiDBNS;
21 push our @ISA, 'SuikaWiki::DB::Util::template';
22 my $Self = '.';
23 my $Parent = '..';
24 my $Sep = '//';
25 my $DB_Module = q(Yuki::YukiWikiDBNS);
26
27 sub new ($%) {
28 my $self = shift->SUPER::new (@_);
29 $self->{option} = {@_};
30 $self;
31 }
32
33 sub ___open_prop ($$) {
34 my ($self, $opt) = @_;
35 return "0 but true" if defined $self->{db_instance};
36 local $Error::Depth = $Error::Depth + 1;
37 my %db;
38 $self->{db_instance} = tie (%db, $DB_Module => $self->{option}->{directory},
39 -lock => 0, ## Yuki::YukiWikiDBNS's lock is buggy
40 -backup => $self->{option}->{use_history},
41 -logfile => $self->{option}->{logfile},
42 -extension => $self->{option}->{suffix},
43 )
44 or report SuikaWiki::DB::Util::Error
45 -type => 'DB_OPEN',
46 -object => $self, method => q(new),
47 file => $self->{option}->{directory};
48 $self->{db_hash} = \%db;
49 1;
50 }
51
52 sub get ($$$) {
53 my ($self, $prop, $key) = @_;
54 unless ($self->{opened}->{$prop}) {
55 local $Error::Depth = $Error::Depth + 1;
56 $self->open_prop (prop => $prop);
57 }
58 $self->{db_hash}->{ $self->__name2name ($key) };
59 }
60
61 sub set ($$$$) {
62 my ($self, $prop, $key => $value) = @_;
63 unless ($self->{opened}->{$prop}) {
64 local $Error::Depth = $Error::Depth + 1;
65 $self->open_prop (prop => $prop);
66 }
67 if (not $self->{lock} or $self->{lock}->writable) {
68 $self->{db_hash}->{ $self->__name2name ($key) } = $value;
69 } else {
70 report SuikaWiki::DB::Util::Error
71 -type => 'KEY_SAVE_LOCKED',
72 -object => $self, method => 'set',
73 key => $key,
74 prop => $prop;
75 }
76 }
77
78 sub exist ($$$) {
79 my ($self, $prop, $key) = @_;
80 unless ($self->{opened}->{$prop}) {
81 local $Error::Depth = $Error::Depth + 1;
82 $self->open_prop (prop => $prop);
83 }
84 CORE::exists $self->{db_hash}->{ $self->__name2name ($key) };
85 }
86
87 sub delete ($$$) {
88 my ($self, $prop, $key) = @_;
89 unless ($self->{opened}->{$prop}) {
90 local $Error::Depth = $Error::Depth + 1;
91 $self->open_prop (prop => $prop);
92 }
93 if (not $self->{lock} or $self->{lock}->writable) {
94 CORE::delete $self->{db_hash}->{ $self->__name2name ($key) };
95 } else {
96 report SuikaWiki::DB::Util::Error
97 -type => 'KEY_SAVE_LOCKED',
98 -object => $self, method => 'delete',
99 key => $key,
100 prop => $prop;
101 }
102 }
103
104 sub keys ($$;%) {
105 my ($self, $prop, %opt) = @_;
106 unless ($self->{opened}->{$prop}) {
107 local $Error::Depth = $Error::Depth + 1;
108 $self->open_prop (prop => $prop);
109 }
110 map {[split /\Q$Sep\E/o, $_]}
111 $self->{db_instance}->list_items ({ns => $self->__name2ns ($opt{-ns}||[]),
112 type => ($opt{-type} eq 'ns'?'ns':'key'),
113 recursive => $opt{-recursive}});
114 }
115
116 sub __name2name ($$) {
117 my ($self, $key) = @_;
118 if (ref $key and @$key == 0) {
119 $key = $self->{option}->{root_key};
120 }
121 local $Error::Depth = $Error::Depth + 1;
122 report SuikaWiki::DB::Util::Error
123 -type => 'KEY_INVALID_NAME', key => $key,
124 -object => $self, method => '__name2name',
125 unless $self->__check_name ($key);
126 join $Sep, @$key;
127 }
128
129 sub __name2ns ($$) {
130 my ($self, $key) = @_;
131 return '' if scalar @$key == 0 || (scalar @$key == 1 && $key->[0] eq '');
132 local $Error::Depth = $Error::Depth + 1;
133 report SuikaWiki::DB::Util::Error
134 -type => 'KEY_INVALID_NS_NAME', key => $key,
135 -object => $self, method => '__name2ns'
136 unless $self->__check_name ($key);
137 $key = $self->__name2name ($key) . $Sep;
138 $key eq $Sep ? undef : $key;
139 }
140
141 sub __check_name ($$) {
142 my ($self, $key) = @_;
143 return 0 unless ref $key;
144 return 0 if @$key == 0;
145 for (@$key) {
146 return 0 unless $_; # '' or 0 or undef
147 return 0 if index ($_, $Sep) > -1;
148 return 0 if $_ eq $Self || $_ eq $Parent;
149 }
150 return 1;
151 }
152
153 sub ___close_prop ($$) {
154 # no-op
155 }
156
157 sub close ($;%) {
158 my $self = shift;
159 untie %{$self->{db_hash}};
160 delete $self->{db_instance};
161 delete $self->{db_hash};
162 delete $self->{opened};
163 $self->{lock}->unlock if $self->{lock};
164 report SuikaWiki::DB::Util::Error
165 -type => 'DB_CLOSED',
166 -object => $self, method => 'close';
167 }
168
169 =head1 METHODS
170
171 This module provides common interface of SuikaWiki WikiDatabase
172 modules. See C<SuikaWiki::DB>.
173
174 =head2 CONSTRUCTOR'S OPTIONS
175
176 @@ TBD
177
178 =head1 EXAMPLE
179
180 ## Get sub (child) namespaces in $ns
181 @ns = $db->keys ($prop_name, -ns => $ns, -type => 'ns', -recursive => 0);
182
183 ## Get child keys in $ns
184 @key = $db->keys ($prop_name, -ns => $ns, -type => 'key', -recursive => 0);
185
186 ## Get descendant (child and grandchild and ...) keys in $ns
187 @key = $db->keys ($prop_name, -ns => $ns, -type => 'key', -recursive => 1);
188
189 ## This DOES NOT work as intended in current implementation...
190 @ns = $db->keys ($prop_name, -ns => $ns, -type => 'ns', -recursive => 1);
191
192 =head1 LICENSE
193
194 Copyright 2003-2004 Wakaba <[email protected]>. All rights reserved.
195
196 This program is free software; you can redistribute it and/or
197 modify it under the same terms as Perl itself.
198
199 =cut
200
201 1; # $Date: 2004/02/14 10:59:18 $

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24