X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=lib%2FCWMP%2FStore%2FYAML.pm;h=0fa19504456e7b620526b7da6b8606e210509b45;hb=b67a262c16c1bf1ed95d99893efc946743842574;hp=588060fd320144df16c6158ae0c96f8f36c523b8;hpb=5932049f4e38bb24ce33fedc7574ef1988099618;p=perl-cwmp.git diff --git a/lib/CWMP/Store/YAML.pm b/lib/CWMP/Store/YAML.pm index 588060f..0fa1950 100644 --- a/lib/CWMP/Store/YAML.pm +++ b/lib/CWMP/Store/YAML.pm @@ -6,6 +6,8 @@ use warnings; use Data::Dump qw/dump/; use YAML qw/LoadFile DumpFile/; +use Hash::Merge qw/merge/; +use Carp qw/confess/; =head1 NAME @@ -15,22 +17,42 @@ CWMP::Store::YAML - use YAML as storage =head2 open + $store->open({ + path => 'var/', + debug => 1, + clean => 1, + }); + =cut -my $dir = 'yaml'; +my $path; -my $debug = 1; +my $debug = 0; sub open { my $self = shift; - warn "open ",dump( @_ ); + my $args = shift; + + $debug = $args->{debug}; + $path = $args->{path} || confess "no path?"; + + warn "open ",dump( $args ) if $debug; + + $path = "$path/yaml"; - if ( ! -e $dir ) { - mkdir $dir || die "can't create $dir: $!"; - warn "created $dir directory\n"; + 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 @@ -42,9 +64,15 @@ sub open { sub update_uid_state { my ( $self, $uid, $state ) = @_; - my $file = "$dir/$uid.yml"; + 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, $state ) || die "can't write $file: $!"; + DumpFile( $file, $combined ) || die "can't write $file: $!"; } @@ -57,7 +85,7 @@ sub update_uid_state { sub get_state { my ( $self, $uid ) = @_; - my $file = "$dir/$uid.yml"; + my $file = "$path/$uid.yml"; if ( -e $file ) { return LoadFile( $file ); @@ -75,11 +103,11 @@ sub get_state { sub all_uids { my $self = shift; - opendir(my $d, $dir) || die "can't opendir $dir: $!"; - my @uids = grep { /\.yml$/ && -f "$dir/$_" } readdir($d); + opendir(my $d, $path) || die "can't opendir $path: $!"; + my @uids = grep { /\.yml$/ && -f "$path/$_" } readdir($d); closedir $d; - return map { s/\.yml$// } @uids; + return map { my $l = $_; $l =~ s/\.yml$//; $l } @uids; } 1;