4 |
use strict; |
use strict; |
5 |
use warnings; |
use warnings; |
6 |
|
|
7 |
use Data::Dump qw/dump/; |
use CWMP::Store::HASH; |
8 |
use YAML qw/LoadFile DumpFile/; |
use base qw/CWMP::Store::HASH/; |
9 |
|
|
10 |
|
use YAML::Syck; |
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 |
|
|
|
|
16 |
=cut |
=cut |
17 |
|
|
18 |
my $dir = 'yaml'; |
my $full_path; |
|
|
|
|
my $debug = 1; |
|
|
|
|
|
sub open { |
|
|
my $self = shift; |
|
|
|
|
|
warn "open ",dump( @_ ); |
|
|
|
|
|
if ( ! -e $dir ) { |
|
|
mkdir $dir || die "can't create $dir: $!"; |
|
|
warn "created $dir directory\n"; |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
=head2 update_uid_state |
|
|
|
|
|
$store->update_uid_state( $uid, $state ); |
|
|
|
|
|
=cut |
|
|
|
|
|
sub update_uid_state { |
|
|
my ( $self, $uid, $state ) = @_; |
|
|
|
|
|
my $file = "$dir/$uid.yml"; |
|
|
|
|
|
DumpFile( $file, $state ) || 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"; |
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 = "$dir/$uid.yml"; |
warn "## file -> $file"; |
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, $dir) || die "can't opendir $dir: $!"; |
|
|
my @uids = grep { /\.yml$/ && -f "$dir/$_" } readdir($d); |
|
|
closedir $d; |
|
38 |
|
|
39 |
return map { s/\.yml$// } @uids; |
sub load_hash { |
40 |
|
my ( $self, $file ) = @_; |
41 |
|
LoadFile( $file ); |
42 |
} |
} |
43 |
|
|
44 |
|
sub extension { '.yml' }; |
45 |
|
|
46 |
1; |
1; |