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

Contents of /suikawiki/script/lib/SuikaWiki/DB/File/StructCache.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations) (download)
Mon Nov 8 10:00:34 2004 UTC (21 years, 8 months ago) by wakaba
Branch: MAIN
CVS Tags: suikawiki3-redirect, HEAD
Branch point for: helowiki-2005, helowiki
2004-11-08  Wakaba <w@suika.fam.cx>

        * ChangeLog: New.

2004-02-27  Wakaba <w@suika.fam.cx>

        * StructCache.pm: New. (NOTE: Experimental)

1
2 =head1 NAME
3
4 SuikaWiki::DB::File::StructCache -
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 use strict;
17 our $VERSION=do{my @r=(q$Revision: 1.5 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
18 require SuikaWiki::DB::Util;
19 push our @ISA, 'SuikaWiki::DB::Util::template';
20
21 sub new ($%) {
22
23 my ($class, %o) = @_;
24 my $self = bless {}, $class;
25 if ($o{-lock}) {
26 $self->{lock} = SuikaWiki::DB::Util->new_lock ($o{-lock});
27 $self->{lock}->lock;
28 }
29 $self->{db_hash} = &{$o{constructor} || sub {return {}}} ($self, \%o);
30 unless ($self->{db_hash}) {
31 report SuikaWiki::DB::Util::Error
32 -type => 'DB_OPEN',
33 -object => $self, method => 'new';
34 }
35 $self->{has_exist} = defined $o{has_exist} ? $o{has_exist} : 1;
36 $self;
37 }
38
39 sub get ($$$) {
40 my ($self, $prop, $key) = @_;
41 $self->___validate_name ($key);
42 $self->{db_hash}->{join $;, @$key};
43 }
44
45 sub set ($$$$) {
46 my ($self, $prop, $key => $value) = @_;
47 $self->___validate_name ($key);
48 $self->{db_hash}->{join $;, @$key} = $value;
49 }
50
51 sub exist ($$$) {
52 my ($self, $prop, $key) = @_;
53 $self->___validate_name ($key);
54 if ($self->{has_exist}) {
55 return CORE::exists $self->{db_hash}->{join $;, @$key};
56 } else {
57 return CORE::defined $self->{db_hash}->{join $;, @$key} ? 1 : 0;
58 }
59 }
60
61 sub delete ($$$) {
62 my ($self, $prop, $key) = @_;
63 $self->___validate_name ($key);
64 CORE::delete $self->{db_hash}->{join $;, @$key};
65 }
66
67 sub keys ($$;%) {
68 my ($self, $prop, %opt) = @_;
69 $self->___validate_name ($opt{ns}, error_type => 'KEY_INVALID_NS_NAME');
70 my $prefix = join $;, @{$opt{ns}};
71 my $prefix_length = length $prefix;
72 $prefix .= $; if $prefix_length;
73 return CORE::map {[$_]}
74 CORE::grep {substr ($_, 0, $prefix_length) eq $prefix}
75 CORE::keys %{$self->{db_hash}};
76 }
77
78 sub close ($) {
79 my $self = shift;
80 ($self->{destructor} or sub { 1 })->($self)
81 or report SuikaWiki::DB::Util::Error
82 -type => 'DB_CLOSE',
83 -object => $self, method => 'close';
84 $self->{db_hash} = undef;
85 $self->{lock}->unlock if $self->{lock};
86 $self->{lock} = undef;
87 }
88
89 sub DESTROY ($) {
90 my $self = shift;
91 $self->close if $self->{db_hash};
92 }
93
94 sub ___validate_name ($$;%) {
95 my ($self, $key, %opt) = @_;
96 if (ref $key) {
97 my $ok = 1;
98 for (@$key) {
99 if (index ($key, $;) > -1) {
100 $ok = 0;
101 last;
102 }
103 }
104 return if $ok;
105 }
106 local $Error::Depth = $Error::Depth + 1;
107 report SuikaWiki::DB::Util::Error
108 -type => $opt{error_type} || 'KEY_INVALID_NAME',
109 -object => $self, method => '___validate_name',
110 key => $key;
111 }
112
113 =head1 METHODS
114
115 This module provides common interface of SuikaWiki WikiDatabase
116 modules. See C<SuikaWiki::DB>.
117
118 =head1 SEE ALSO
119
120 C<SuikaWiki::DB>.
121
122 =head1 AUTHOR
123
124 Wakaba <w@suika.fam.cx>.
125
126 =head1 LICENSE
127
128 Copyright AUTHOR 2003.
129
130 This program is free software; you can redistribute it and/or
131 modify it under the same terms as Perl itself.
132
133 =cut
134
135 1; # $Date: 2003/12/06 02:19:09 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24