line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#$Id: MLDBM.pm 114 2007-07-03 20:49:10Z zag $ |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package HTML::WebDAO::Store::MLDBM; |
4
|
2
|
|
|
2
|
|
1582
|
use File::Path; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
153
|
|
5
|
2
|
|
|
2
|
|
11
|
use Fcntl ":flock"; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
271
|
|
6
|
2
|
|
|
2
|
|
1706
|
use IO::File; |
|
2
|
|
|
|
|
20153
|
|
|
2
|
|
|
|
|
263
|
|
7
|
2
|
|
|
2
|
|
1661
|
use MLDBM qw (DB_File Data::Dumper); |
|
2
|
|
|
|
|
6588
|
|
|
2
|
|
|
|
|
11
|
|
8
|
2
|
|
|
2
|
|
530
|
use HTML::WebDAO::Store::Abstract; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
82
|
|
9
|
2
|
|
|
2
|
|
11
|
use Data::Dumper; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
91
|
|
10
|
2
|
|
|
2
|
|
10
|
use strict 'vars'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
49
|
|
11
|
2
|
|
|
2
|
|
12
|
use base 'HTML::WebDAO::Store::Abstract'; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
1525
|
|
12
|
|
|
|
|
|
|
__PACKAGE__->attributes qw/ _dir _cache _is_loaded/; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub init { |
15
|
|
|
|
|
|
|
my $self = shift; |
16
|
|
|
|
|
|
|
my %pars = @_; |
17
|
|
|
|
|
|
|
die "need param path to dir" unless exists $pars{path}; |
18
|
|
|
|
|
|
|
my $dir = $pars{path}; |
19
|
|
|
|
|
|
|
$dir .= "/" unless $dir =~ m%/$%; |
20
|
|
|
|
|
|
|
unless ( -d $dir ) { |
21
|
|
|
|
|
|
|
eval { |
22
|
|
|
|
|
|
|
mkpath( $dir, 0 ); |
23
|
|
|
|
|
|
|
}; |
24
|
|
|
|
|
|
|
if ($@) { |
25
|
|
|
|
|
|
|
_log1 $self "error mkdir".$@ |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
$self->_dir($dir); |
30
|
|
|
|
|
|
|
my %hash; |
31
|
|
|
|
|
|
|
$self->_cache( \%hash ); |
32
|
|
|
|
|
|
|
return 1; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub load { |
36
|
|
|
|
|
|
|
my $self = shift; |
37
|
|
|
|
|
|
|
my $id = shift || return {}; |
38
|
|
|
|
|
|
|
my %hash; |
39
|
|
|
|
|
|
|
my $db_file = $self->_dir() . "sess_$id.db"; |
40
|
|
|
|
|
|
|
my $db = tie %hash, "MLDBM", $db_file, O_CREAT | O_RDWR, 0644 or die "$!"; |
41
|
|
|
|
|
|
|
my $fd = $db->fd(); |
42
|
|
|
|
|
|
|
undef $db; |
43
|
|
|
|
|
|
|
local *DBM; |
44
|
|
|
|
|
|
|
open DBM, "+<&=$fd" or die "$!"; |
45
|
|
|
|
|
|
|
flock DBM, LOCK_SH; |
46
|
|
|
|
|
|
|
my $tmp_hash = $hash{$id}; |
47
|
|
|
|
|
|
|
untie %hash; |
48
|
|
|
|
|
|
|
flock DBM, LOCK_UN; |
49
|
|
|
|
|
|
|
close DBM; |
50
|
|
|
|
|
|
|
return $tmp_hash; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub store { |
54
|
|
|
|
|
|
|
my $self = shift; |
55
|
|
|
|
|
|
|
my $id = shift || return {}; |
56
|
|
|
|
|
|
|
my $ref_tree = shift; |
57
|
|
|
|
|
|
|
return unless $ref_tree && ref($ref_tree); |
58
|
|
|
|
|
|
|
my %hash; |
59
|
|
|
|
|
|
|
my $db_file = $self->_dir() . "sess_$id.db"; |
60
|
|
|
|
|
|
|
my $db = tie %hash, "MLDBM", $db_file, O_CREAT | O_RDWR, 0644 or die "$!"; |
61
|
|
|
|
|
|
|
my $fd = $db->fd(); |
62
|
|
|
|
|
|
|
undef $db; |
63
|
|
|
|
|
|
|
local *DBM; |
64
|
|
|
|
|
|
|
open DBM, "+<&=$fd" or die "$!"; |
65
|
|
|
|
|
|
|
flock DBM, LOCK_EX; |
66
|
|
|
|
|
|
|
$hash{$id} = $ref_tree; |
67
|
|
|
|
|
|
|
untie %hash; |
68
|
|
|
|
|
|
|
flock DBM, LOCK_UN; |
69
|
|
|
|
|
|
|
close DBM; |
70
|
|
|
|
|
|
|
return $id; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub _store_attributes { |
75
|
|
|
|
|
|
|
my $self = shift; |
76
|
|
|
|
|
|
|
my $id = shift || return; |
77
|
|
|
|
|
|
|
my $ref = shift || return; |
78
|
|
|
|
|
|
|
my $cache = $self->_cache(); |
79
|
|
|
|
|
|
|
while ( my ( $key, $val ) = each %$ref ) { |
80
|
|
|
|
|
|
|
$cache->{$key} = $val; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub _load_attributes { |
85
|
|
|
|
|
|
|
my $self = shift; |
86
|
|
|
|
|
|
|
my $id = shift || return; |
87
|
|
|
|
|
|
|
unless ( $self->_is_loaded ) { |
88
|
|
|
|
|
|
|
my $from_storage = $self->load($id); |
89
|
|
|
|
|
|
|
my $cache = $self->_cache; |
90
|
|
|
|
|
|
|
while ( my ( $key, $val ) = each %$from_storage ) { |
91
|
|
|
|
|
|
|
next if exists $cache->{$key}; |
92
|
|
|
|
|
|
|
$cache->{$key} = $val; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
$self->_is_loaded(1); |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
my $loaded = $self->_cache; |
97
|
|
|
|
|
|
|
my %res; |
98
|
|
|
|
|
|
|
foreach my $key (@_) { |
99
|
|
|
|
|
|
|
$res{$key} = $loaded->{$key} if exists $loaded->{$key}; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
return \%res; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub flush { |
105
|
|
|
|
|
|
|
my $self = shift; |
106
|
|
|
|
|
|
|
$self->store( @_, $self->_cache ); |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
1; |