| 1 |
wakaba |
1.1 |
|
| 2 |
|
|
=head1 NAME |
| 3 |
|
|
|
| 4 |
wakaba |
1.3 |
SuikaWiki::DB::FileSystem::LeafFile - SuikaWiki Directory-Structured Simple Database |
| 5 |
wakaba |
1.1 |
|
| 6 |
|
|
=head1 SYNOPSIS |
| 7 |
|
|
|
| 8 |
wakaba |
1.3 |
require SuikaWiki::DB::FileSystem::LeafFile; |
| 9 |
|
|
my $db = SuikaWiki::DB::FileSystem::LeafFile->new |
| 10 |
|
|
(base_directory => $path_to_db, |
| 11 |
|
|
directory_suffix => '.ns', |
| 12 |
|
|
file_suffix => '.txt', |
| 13 |
|
|
-lock => { ..lock property.. }); |
| 14 |
wakaba |
1.1 |
|
| 15 |
|
|
=head1 DESCRIPTION |
| 16 |
|
|
|
| 17 |
wakaba |
1.3 |
C<SuikaWiki::DB::FileSystem::LeafFile> provides simple database |
| 18 |
|
|
which maps namespaces to filesystem directories and keys to files. |
| 19 |
|
|
Directories and files names are encoded by base 16 so that user |
| 20 |
|
|
does not need to make key names filesystem safe (but file names is |
| 21 |
|
|
to be unreadable by human). |
| 22 |
wakaba |
1.1 |
|
| 23 |
|
|
This module is part of SuikaWiki. |
| 24 |
|
|
|
| 25 |
|
|
=cut |
| 26 |
|
|
|
| 27 |
|
|
package SuikaWiki::DB::FileSystem::LeafFile; |
| 28 |
|
|
use strict; |
| 29 |
wakaba |
1.4 |
our $VERSION=do{my @r=(q$Revision: 1.3 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r}; |
| 30 |
wakaba |
1.1 |
require SuikaWiki::DB::FileSystem::Base; |
| 31 |
|
|
push our @ISA, 'SuikaWiki::DB::FileSystem::Base'; |
| 32 |
|
|
use IO::File; |
| 33 |
|
|
|
| 34 |
|
|
sub get ($$$;%) { |
| 35 |
|
|
my ($self, $prop, $key, %opt) = @_; |
| 36 |
|
|
report SuikaWiki::DB::Util::Error |
| 37 |
|
|
-type => 'KEY_INVALID_NAME', key => $key, |
| 38 |
|
|
-object => $self, method => 'get' |
| 39 |
|
|
unless $opt{no_key_check} or |
| 40 |
|
|
$self->__check_key (key => $key); |
| 41 |
|
|
|
| 42 |
|
|
unless ($self->{opened}->{$prop}) { |
| 43 |
|
|
local $Error::Depth = $Error::Depth + 1; |
| 44 |
|
|
$self->open_prop (prop => $prop); |
| 45 |
|
|
} |
| 46 |
|
|
|
| 47 |
|
|
my $path = $self->__key2filepath (key => $key); |
| 48 |
|
|
|
| 49 |
|
|
if (-e $path) { |
| 50 |
|
|
my $file = new IO::File $path, '<' |
| 51 |
|
|
or report SuikaWiki::DB::Util::Error |
| 52 |
|
|
-type => 'FILE_READ_FAILURE', |
| 53 |
|
|
-object => $self, method => 'get', |
| 54 |
|
|
file => $path, msg => $!; |
| 55 |
|
|
binmode $file; |
| 56 |
|
|
local $/ = undef; |
| 57 |
|
|
return scalar <$file>; |
| 58 |
|
|
} else { |
| 59 |
|
|
return undef; |
| 60 |
|
|
} |
| 61 |
|
|
} |
| 62 |
|
|
|
| 63 |
|
|
sub set ($$$$;%) { |
| 64 |
|
|
my ($self, $prop, $key, $val, %opt) = @_; |
| 65 |
wakaba |
1.2 |
report SuikaWiki::DB::Util::Error |
| 66 |
|
|
-type => 'KEY_INVALID_NAME', key => $key, |
| 67 |
|
|
-object => $self, method => 'set' |
| 68 |
|
|
unless $opt{no_key_check} or |
| 69 |
|
|
$self->__check_key (key => $key); |
| 70 |
wakaba |
1.1 |
|
| 71 |
|
|
unless ($self->{opened}->{$prop}) { |
| 72 |
|
|
local $Error::Depth = $Error::Depth + 1; |
| 73 |
|
|
$self->open_prop (prop => $prop); |
| 74 |
|
|
} |
| 75 |
|
|
|
| 76 |
|
|
if ($self->{lock} and not $self->{lock}->writable) { |
| 77 |
|
|
report SuikaWiki::DB::Util::Error |
| 78 |
|
|
-type => 'KEY_SAVE_LOCKED', |
| 79 |
|
|
-object => $self, method => 'set', |
| 80 |
|
|
key => $key, |
| 81 |
|
|
prop => $prop; |
| 82 |
|
|
return 0; |
| 83 |
|
|
} |
| 84 |
|
|
|
| 85 |
|
|
$self->__make_directory (directory => $self->__key2dirpath (fullkey => $key)); |
| 86 |
|
|
my $path = $self->__key2filepath (key => $key); |
| 87 |
|
|
my $file = new IO::File $path, '>' |
| 88 |
|
|
or report SuikaWiki::DB::Util::Error |
| 89 |
|
|
-type => 'FILE_WRITE_FAILURE', |
| 90 |
|
|
-object => $self, method => 'set', |
| 91 |
|
|
file => $path, msg => $!; |
| 92 |
|
|
binmode $file; |
| 93 |
|
|
print $file $val; |
| 94 |
|
|
close $file; |
| 95 |
|
|
1; |
| 96 |
|
|
} |
| 97 |
|
|
|
| 98 |
wakaba |
1.4 |
sub _content_id ($$$;%) { |
| 99 |
|
|
my ($self, $prop, $key, %opt) = @_; |
| 100 |
|
|
require Digest::SHA1; |
| 101 |
|
|
Digest::SHA1::sha1 ($self->get ($prop, $key, %opt)); |
| 102 |
|
|
} |
| 103 |
|
|
|
| 104 |
wakaba |
1.3 |
=head1 SEE ALSO |
| 105 |
|
|
|
| 106 |
|
|
C<SuikaWiki::DB::FileSystem::Base>. |
| 107 |
|
|
|
| 108 |
wakaba |
1.1 |
=head1 LICENSE |
| 109 |
|
|
|
| 110 |
|
|
Copyright 2004 Wakaba <w@suika.fam.cx>. All rights reserved. |
| 111 |
|
|
|
| 112 |
|
|
This program is free software; you can redistribute it and/or |
| 113 |
|
|
modify it under the same terms as Perl itself. |
| 114 |
|
|
|
| 115 |
|
|
=cut |
| 116 |
|
|
|
| 117 |
wakaba |
1.4 |
1; # $Date: 2004/06/03 06:38:48 $ |