Dump.pm

filter: Statementstmt 26.6% 16 / 60 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 22.2% 2 / 9 Podpod 100% 1 / 1 totaltotal 18.2% 19 / 104 CC 17 SCAR 35.8
CC
17
Cov
18%
_get_value
72.0
_get_type_and_filter
30.0
_get_values_by_keys
30.0
CRAP 35.8
↑ Summary Dump/Restore.pm »
1  
2 package Redis::Dump;
3  
4 17 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 with 'MooseX::Getopt';
6  
7 17 ▶use Redis 1.904;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # ABSTRACT: It's a simple way to dump and backup data from redis-server
10 17 our $VERSION = '0.016'; # VERSION
11  
12 has _conn => (
13 is => 'ro',
14 isa => 'Redis',
15 init_arg => undef,
16 lazy => 1,
17 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
18 0 my $tconf = shift;
19 0 my $tconn = Redis->new( server => $tconf->server );
20 0 $tconn->select( $tconf->database);
21 0 $tconn
22 }
23 17 );
24  
25 0 ▶sub _get_keys {
Subroutine _get_keys: not called CC=2 SCAR=17.9
26 0 my $self = shift;
27 0 my $filter = $self->filter;
28 0 ▶ return $self->_conn->keys("*$filter*") if $filter;
Branch: 1 branch
BranchTrueFalseTotal
if $filter 0 0 0
29 0 return $self->_conn->keys("*");
30 }
31  
32 0 ▶sub _get_type_and_filter {
Subroutine _get_type_and_filter: not called CC=5 SCAR=34.0
33 0 my ( $self, $key ) = @_;
34 0 ▶ return if $self->has_filter and not $key =~ $self->filter;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_filter and not $key =~ $self->filter 0 0 0
Condition0%0/3
$self->has_filter and not $key =~ $self->filter
!ll&&!rl&&r
000
MC/DC0%0/2
$self->has_filter and not $key =~ $self->filter
$self->has_filter not $key =~ $self->filter
Truth table0%0/3
A and not(B)
A= $self->has_filterB= not $key =~ $self->filter
ABresult
0X0
100
111
35 0 my $type = $self->_conn->type($key);
36 0 ▶ return if @{ $self->type } and not grep {/^$type/} @{ $self->type };
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->type;} and not grep({/^$type/;} @{$self->type;}) 0 0 0
Condition0%0/3
@{$self->type;} and not grep({/^$type/;} @{$self->type;})
!ll&&!rl&&r
000
MC/DC0%0/2
@{$self->type;} and not grep({/^$type/;} @{$self->type;})
@{$self->type;} not grep({/^$type/;} @{$self->type;})
Truth table0%0/3
A and not(B)
A= @{$self->type;}B= not grep({/^$type/;} @{$self->type;})
ABresult
0X0
100
111
37 0 return $type;
38 }
39  
40 0 ▶sub _get_value {
Subroutine _get_value: not called CC=8 SCAR=42.8
41 0 my ( $self, $key, $type ) = @_;
42 0 ▶ return $self->_conn->get($key) if $type eq 'string';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "string" 0 0 0
43 0 ▶ return $self->_conn->lrange( $key, 0, -1 ) if $type eq 'list';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "list" 0 0 0
44 0 ▶ return $self->_conn->smembers($key) if $type eq 'set';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "set" 0 0 0
45  
46 0 ▶ if ( $type eq 'zset' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "zset") 0 0 0
47 0 my %hash;
48 0 my @zsets = $self->_conn->zrange( $key, 0, -1, 'withscores' );
49 0 ▶ for ( my $loop = 0; $loop < scalar(@zsets) / 2; $loop++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loop < scalar(@zsets) / 2) 0 0 0
50 0 my $value = $zsets[ $loop * 2 ];
51 0 my $score = $zsets[ ( $loop * 2 ) + 1 ];
52 0 $hash{$score} = $value;
53 }
54 0 return [ {%hash} ];
55 }
56  
57 0 ▶ if ( $type eq 'hash' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "hash") 0 0 0
58 0 my %hash;
59 0 foreach my $item ( $self->_conn->hkeys($key) ) {
60 0 $hash{$item} = $self->_conn->hget( $key, $item );
61 }
62 0 return {%hash};
63 }
64 }
65  
66 0 ▶sub _get_values_by_keys {
Subroutine _get_values_by_keys: not called CC=5 SCAR=34.0
67 0 my $self = shift;
68 0 my %keys;
69  
70 0 foreach my $key ( $self->_get_keys ) {
71 0 ▶ my $type = $self->_get_type_and_filter($key) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $type = $self->_get_type_and_filter($key) or next 0 0 0
72 0 my $show_name = $key;
73 0 ▶ $show_name .= " ($type)" if $self->showtype;
Branch: 1 branch
BranchTrueFalseTotal
if $self->showtype 0 0 0
74 0 ▶ $keys{$show_name} = $self->hidevalues ? '' : $self->_get_value( $key, $type );
Branch: 1 branch
BranchTrueFalseTotal
$self->hidevalues ? : 0 0 0
75 }
76 0 return %keys;
77 }
78  
79  
80  
81 0 ▶sub run {
Subroutine run: not called CC=1 SCAR=6.9
Pod documented
82 0 my $self = shift;
83 0 return $self->_get_values_by_keys;
84 }
85  
86  
87 17 has server => (
88 is => 'ro',
89 isa => 'Str',
90 default => '127.0.0.1:6379',
91 documentation => 'Host:Port of redis server (ex. 127.0.0.1:6379)'
92 );
93  
94  
95 17 has database => (
96 is => 'ro',
97 isa => 'Int',
98 default => 0,
99 documentation => 'Database used in a multi-database setup'
100 );
101  
102  
103 17 has filter => (
104 is => 'ro',
105 isa => 'Str',
106 default => '',
107 predicate => 'has_filter',
108 documentation => 'String to filter keys stored in redis server'
109 );
110  
111  
112 has type => (
113 is => 'ro',
114 isa => 'ArrayRef[Str]',
115 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
116 17 predicate => 'has_type',
117 documentation => 'Show just this type of key',
118 );
119  
120  
121 17 has showtype => (
122 is => 'ro',
123 isa => 'Bool',
124 default => 0,
125 documentation => 'If you want to show type with key name.'
126 );
127  
128  
129 17 has hidevalues => (
130 is => 'ro',
131 isa => 'Bool',
132 default => 0,
133 documentation => 'Hide values of keys'
134 );
135  
136 17 1;
137  
138  
139  
140 =pod
141  
142 =head1 NAME
143  
144 Redis::Dump - It's a simple way to dump and backup data from redis-server
145  
146 =head1 VERSION
147  
148 version 0.016
149  
150 =head1 SYNOPSIS
151  
152 use Redis::Dump;
153 use Data::Dumper;
154  
155 my $dump = Redis::Dump->new({ server => '127.0.0.6379', filter => 'foo' });
156  
157 print Dumper( \$dump->run );
158  
159 =head1 DESCRIPTION
160  
161 It's a simple way to dump data from redis-server in JSON format or any format
162 you want.
163  
164 =head1 COMMAND LINE API
165  
166 This class uses L<MooseX::Getopt> to provide a command line api. The command line options map to the class attributes.
167  
168 =head1 METHODS
169  
170 =head2 new_with_options
171  
172 Provided by L<MooseX::Getopt>. Parses attributes init args from @ARGV.
173  
174 =head2 run
175  
176 Perfomas the actual dump.
177  
178 =head1 ATTRIBUTES
179  
180 =head2 server
181  
182 Host:Port of redis server, example: 127.0.0.1:6379.
183  
184 =head2 database
185  
186 If you want to select another database than default which is 0.
187  
188 =head2 filter
189  
190 String to filter keys stored in redis server.
191  
192 =head2 type
193  
194 If you want to get just some types of keys.
195  
196 It can be: lists, sets, hashs, strings, zsets
197  
198 =head2 showtype
199  
200 If you want to show type with key name.
201  
202 =head2 hidevalues
203  
204 Hide value of keys.
205  
206 =head1 DEVELOPMENT
207  
208 Redis::Dump is a open source project for everyone to participate. The code repository
209 is located on github. Feel free to send a bug report, a pull request, or a
210 beer.
211  
212 L<http://www.github.com/maluco/Redis-Dump>
213  
214 =head1 SEE ALSO
215  
216 L<Redis::Dump::Restore>, L<App::Redis::Dump>, L<App::Redis::Dump::Restore>
217  
218 =head1 CREDITS
219  
220 =over 4
221  
222 =item * Rémi Paulmier
223  
224 =back
225  
226 =head1 SUPPORT
227  
228 You can find documentation for this module with the perldoc command.
229  
230 perldoc Redis::Dump
231  
232 You can also look for information at:
233  
234 =over 4
235  
236 =item * RT: CPAN's request tracker
237  
238 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Redis-Dump>
239  
240 =item * AnnoCPAN: Annotated CPAN documentation
241  
242 L<http://annocpan.org/dist/Redis-Dump>
243  
244 =item * CPAN Ratings
245  
246 L<http://cpanratings.perl.org/d/Redis-Dump>
247  
248 =item * Search CPAN
249  
250 L<http://search.cpan.org/dist/Redis-Dump>
251  
252 =back
253  
254 =head1 AUTHOR
255  
256 Thiago Rondon <thiago@nsms.com.br>
257  
258 =head1 COPYRIGHT AND LICENSE
259  
260 This software is copyright (c) 2011 by Thiago Rondon.
261  
262 This is free software; you can redistribute it and/or modify it under
263 the same terms as the Perl 5 programming language system itself.
264  
265 =cut
266  
267  
268 __END__
↑ Summary Dump/Restore.pm »