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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (show annotations) (download)
Mon Nov 8 09:57:49 2004 UTC (21 years, 10 months ago) by wakaba
Branch: MAIN
CVS Tags: suikawiki3-redirect, HEAD
Branch point for: helowiki, helowiki-2005
Changes since 1.5: +22 -15 lines
Committed

1
2 =head1 NAME
3
4 SuikaWiki::DB::Logical --- SuikaWiki WikiDatabase: Logical database consists of multiple datasources
5
6 =head1 DESCRIPTION
7
8 This module provides a logical database implementation. It does not itself have
9 physical datasource at all. With database property name asspcoatopms, multiple
10 database instances that have common WikiDatabase interface can be used as
11 datasources.
12
13 This module is part of SuikaWiki.
14
15 =cut
16
17 package SuikaWiki::DB::Logical;
18 use strict;
19 our $VERSION=do{my @r=(q$Revision: 1.5 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
20 require SuikaWiki::DB::Util;
21 push our @ISA, 'SuikaWiki::DB::Util::template';
22
23 # new: inherited
24
25 sub get ($$$;%) {
26 my ($self, $prop, $key, %opt) = @_;
27 if ($self->{prop}->{$prop}) {
28 local $Error::Depth = $Error::Depth + 1;
29 unless ($self->{opened}->{$prop}) {
30 $self->open_prop (prop => $prop);
31 }
32 return $self->{prop}->{$prop}->{-db}->get
33 ($self->{prop}->{$prop}->{-prop},
34 $self->{prop}->{$prop}->{-key_mapper}->($self, $key), %opt);
35 } else {
36 return undef;
37 }
38 }
39
40 sub set ($$$$;%) {
41 my ($self, $prop, $key => $value, %opt) = @_;
42 if ($self->{prop}->{$prop}) {
43 local $Error::Depth = $Error::Depth + 1;
44 unless ($self->{opened}->{$prop}) {
45 $self->open_prop (prop => $prop);
46 }
47 $self->{prop}->{$prop}->{-db}->set
48 ($self->{prop}->{$prop}->{-prop},
49 $self->{prop}->{$prop}->{-key_mapper}->($self, $key) => $value, %opt);
50 } else {
51 report SuikaWiki::DB::Util::Error
52 -type => 'KEY_SAVE',
53 -object => $self, method => 'set',
54 prop => $prop, key => $key;
55 }
56 }
57
58 sub exist ($$$;%) {
59 my ($self, $prop, $key, %opt) = @_;
60 if ($self->{prop}->{$prop}) {
61 local $Error::Depth = $Error::Depth + 1;
62 unless ($self->{opened}->{$prop}) {
63 $self->open_prop (prop => $prop);
64 }
65 return $self->{prop}->{$prop}->{-db}->exist
66 ($self->{prop}->{$prop}->{-prop},
67 $self->{prop}->{$prop}->{-key_mapper}->($self, $key), %opt);
68 } else {
69 return 0;
70 }
71 }
72
73 sub delete ($$$;%) {
74 my ($self, $prop, $key, %opt) = @_;
75 if ($self->{prop}->{$prop}) {
76 local $Error::Depth = $Error::Depth + 1;
77 unless ($self->{opened}->{$prop}) {
78 $self->open_prop (prop => $prop);
79 }
80 $self->{prop}->{$prop}->{-db}->delete
81 ($self->{prop}->{$prop}->{-prop},
82 $self->{prop}->{$prop}->{-key_mapper}->($self, $key), %opt);
83 }
84 }
85
86 sub keys ($$;%) {
87 my ($self, $prop, %opt) = @_;
88 if ($self->{prop}->{$prop}) {
89 local $Error::Depth = $Error::Depth + 1;
90 unless ($self->{opened}->{$prop}) {
91 $self->open_prop (prop => $prop);
92 }
93 $opt{-ns} = $self->{prop}->{$prop}->{-key_mapper}->($self, $opt{-ns});
94 return
95 map {$self->{prop}->{$prop}->{-key_rev_mapper}->($self, $_)}
96 $self->{prop}->{$prop}->{-db}->keys
97 ($self->{prop}->{$prop}->{-prop}, %opt);
98 } else {
99 return ();
100 }
101 }
102
103 # close: Inherited
104
105 =head1 METHODS
106
107 This module provides common interface of SuikaWiki WikiDatabase
108 modules. See documentation of C<SuikaWiki::DB>.
109
110 In addition, this module implements additional methods.
111
112 =over 4
113
114 =item _set_prop_db ($prop_name, {options})
115
116 Addosiates actual database with property name of this logical (virtual)
117 database, or remove its association by specifying C<undef> instead of
118 C<{options}>.
119
120 Options:
121
122 =over 4
123
124 =item -db => $database_instance (REQUIRED)
125
126 Instance of database module, which implements common WikiDatabase interface.
127
128 =item -prop => $property_name (Default: same as $prop_name)
129
130 Property name used to access to $database_instance. With this option,
131 different property name from datasource's one can be used,
132 eg. maps $prop_name eq 'subdb_foo' to $property_name eq 'foo'.
133
134 =item -key_mapper => sub ($self, $key) (Default: sub {$key})
135
136 Mapper from this virtual database's keyname to sub database's one.
137
138 =item -key_rev_mapper => sub ($self, $key) (Default: sub {$key})
139
140 Mapper from sub database's keyname to this virtual database's keyname.
141
142 Note that -key_mapper (-key_rev_mapper ($key)) need not equal to $key,
143 although it seems an unusual case.
144
145 =back
146
147 =cut
148
149 sub _set_prop_db ($$$) {
150 my ($self, $prop, $db_and_opt) = @_;
151 $self->{prop}->{$prop} = $db_and_opt;
152 $db_and_opt->{-prop} = $prop unless defined $db_and_opt->{-prop};
153 $db_and_opt->{-key_mapper} ||= \&__default_key_mapper;
154 $db_and_opt->{-key_rev_mapper} ||= \&__default_key_rev_mapper;
155 $db_and_opt->{-db_close} ||= sub {
156 my %opt = @_;
157 local $Error::Depth = $Error::Depth + 1;
158 $opt{prop_info}->{-db}->close_prop (prop => $opt{prop_info}->{-prop});
159 };
160 }
161
162 sub __default_key_mapper ($$) {
163 #my ($self, $key) = @_;
164 #$key;
165 $_[1];
166 }
167 sub __default_key_rev_mapper ($$) {
168 #my ($self, $key) = @_;
169 #$key;
170 $_[1];
171 }
172
173 sub ___open_prop ($$) {
174 my ($self, $opt) = @_;
175 return "0 but true" if defined $self->{prop}->{$opt->{prop}}->{-db};
176 local $Error::Depth = $Error::Depth + 2;
177 report SuikaWiki::DB::Util::Error
178 -type => 'STOP_DB_PROP_CANT_OPEN__NOT_DEFINED',
179 -object => $self, method => 'open_prop',
180 prop => $opt->{prop}
181 and return 0
182 unless $self->{prop}->{$opt->{prop}}->{-db} or
183 $self->{prop}->{$opt->{prop}}->{-db_open};
184 $self->{prop}->{$opt->{prop}}->{-db}
185 = $self->{prop}->{$opt->{prop}}->{-db_open}->(metadb => $self);
186 1;
187 }
188
189 sub ___close_prop ($$) {
190 my ($self, $opt) = @_;
191 return "0 but true" unless $self->{opened}->{$opt->{prop}};
192 local $Error::Depth = $Error::Depth + 2;
193 $self->{prop}->{$_}->{-db_close}->(metadb => $self,
194 prop_info => $self->{prop}->{$_});
195 delete $self->{prop}->{$_}->{-db}
196 if $self->{prop}->{$_}->{-db_open};
197 1;
198 }
199
200 =back
201
202 =head1 LICENSE
203
204 Copyright 2003-2004 Wakaba <[email protected]>. All rights reserved.
205
206 This program is free software; you can redistribute it and/or
207 modify it under the same terms as Perl itself.
208
209 =cut
210
211 1; # $Date: 2003/12/06 02:19:09 $

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24