4 |
use strict; |
use strict; |
5 |
use warnings; |
use warnings; |
6 |
|
|
7 |
use Data::Dump qw/dump/; |
use CWMP::Store::HASH; |
8 |
|
use base qw/CWMP::Store::HASH/; |
9 |
|
|
10 |
use YAML::Syck; |
use YAML::Syck; |
|
use Hash::Merge qw/merge/; |
|
|
use Carp qw/confess/; |
|
11 |
|
|
12 |
=head1 NAME |
=head1 NAME |
13 |
|
|
14 |
CWMP::Store::YAML - use YAML as storage |
CWMP::Store::YAML - use YAML as storage |
15 |
|
|
|
=head1 METHODS |
|
|
|
|
|
=head2 open |
|
|
|
|
|
$store->open({ |
|
|
path => 'var/', |
|
|
debug => 1, |
|
|
clean => 1, |
|
|
}); |
|
|
|
|
|
=cut |
|
|
|
|
|
my $path; |
|
|
|
|
|
my $debug = 0; |
|
|
|
|
|
sub open { |
|
|
my $self = shift; |
|
|
|
|
|
my $args = shift; |
|
|
|
|
|
$debug = $args->{debug}; |
|
|
$path = $args->{path} || confess "no path?"; |
|
|
|
|
|
warn "open ",dump( $args ) if $debug; |
|
|
|
|
|
$path = "$path/yaml"; |
|
|
|
|
|
if ( ! -e $path ) { |
|
|
mkdir $path || die "can't create $path: $!"; |
|
|
warn "created $path directory\n" if $debug; |
|
|
} elsif ( $args->{clean} ) { |
|
|
warn "removed old $path\n" if $debug; |
|
|
foreach my $uid ( $self->all_uids ) { |
|
|
my $file = "$path/$uid.yml"; |
|
|
unlink $file || die "can't remove $file: $!"; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
=head2 update_uid_state |
|
|
|
|
|
$store->update_uid_state( $uid, $state ); |
|
|
|
|
16 |
=cut |
=cut |
17 |
|
|
18 |
sub update_uid_state { |
my $full_path; |
|
my ( $self, $uid, $state ) = @_; |
|
|
|
|
|
my $file = "$path/$uid.yml"; |
|
|
|
|
|
my $old_state = $self->get_state( $uid ); |
|
|
|
|
|
my $combined = merge( $state, $old_state ); |
|
|
|
|
|
# warn "## ",dump( $old_state, $state, $combined ); |
|
|
|
|
|
DumpFile( $file, $combined ) || die "can't write $file: $!"; |
|
19 |
|
|
20 |
|
sub full_path { |
21 |
|
my ( $self, $path ) = @_; |
22 |
|
$full_path = "$path/yaml"; |
23 |
|
warn "## full_path: $full_path" if $debug; |
24 |
|
return $full_path; |
25 |
} |
} |
26 |
|
|
27 |
=head2 get_state |
sub file { |
|
|
|
|
$store->get_state( $uid ); |
|
|
|
|
|
=cut |
|
|
|
|
|
sub get_state { |
|
28 |
my ( $self, $uid ) = @_; |
my ( $self, $uid ) = @_; |
29 |
|
my $file = "$full_path/$uid" . $self->extension; |
30 |
my $file = "$path/$uid.yml"; |
warn "## file -> $file" if $debug; |
31 |
|
return $file; |
|
if ( -e $file ) { |
|
|
return LoadFile( $file ); |
|
|
} |
|
|
|
|
|
return; |
|
32 |
} |
} |
33 |
|
|
34 |
=head2 all_uids |
sub save_hash { |
35 |
|
my ( $self, $file, $hash ) = @_; |
36 |
my @uids = $store->all_uids; |
DumpFile( $file, $hash ); |
37 |
|
} |
|
=cut |
|
|
|
|
|
sub all_uids { |
|
|
my $self = shift; |
|
|
|
|
|
opendir(my $d, $path) || die "can't opendir $path: $!"; |
|
|
my @uids = grep { /\.yml$/ && -f "$path/$_" } readdir($d); |
|
|
closedir $d; |
|
38 |
|
|
39 |
return map { my $l = $_; $l =~ s/\.yml$//; $l } @uids; |
sub load_hash { |
40 |
|
my ( $self, $file ) = @_; |
41 |
|
LoadFile( $file ); |
42 |
} |
} |
43 |
|
|
44 |
|
sub extension { '.yml' }; |
45 |
|
|
46 |
1; |
1; |