| 1 | |||||||||||
| 2 | package Redis::Dump::Restore; | ||||||||||
| 3 | |||||||||||
| 4 | 3 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use MooseX::Types::Path::Class; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 3 | ▶ | use JSON; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 3 | with 'MooseX::Getopt'; | |||||||||
| 8 | |||||||||||
| 9 | 3 | ▶ | use Redis 1.904; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | # ABSTRACT: It's a simple way to restore data to redis-server based on redis-dump. | ||||||||||
| 12 | 3 | our $VERSION = '0.016'; # VERSION | |||||||||
| 13 | |||||||||||
| 14 | has _conn => ( | ||||||||||
| 15 | is => 'ro', | ||||||||||
| 16 | isa => 'Redis', | ||||||||||
| 17 | init_arg => undef, | ||||||||||
| 18 | lazy => 1, | ||||||||||
| 19 | 0 | ▶ | default => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 20 | 0 | my $tconf = shift; | |||||||||
| 21 | 0 | my $tconn = Redis->new( server => $tconf->server ); | |||||||||
| 22 | 0 | $tconn->select( $tconf->database); | |||||||||
| 23 | 0 | $tconn | |||||||||
| 24 | } | ||||||||||
| 25 | 3 | ); | |||||||||
| 26 | |||||||||||
| 27 | 0 | ▶ | sub _set_string { | ||||||||
|
Subroutine
_set_string: not called CC=1 SCAR=6.9
| |||||||||||
| 28 | 0 | my ( $self, $name, $value ) = @_; | |||||||||
| 29 | 0 | $self->_conn->set( $name => $value ); | |||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | 0 | ▶ | sub _set_array { | ||||||||
|
Subroutine
_set_array: not called CC=5 SCAR=34.0
| |||||||||||
| 33 | 0 | my ( $self, $name, $value ) = @_; | |||||||||
| 34 | |||||||||||
| 35 | 0 | foreach my $item ( @{$value} ) { | |||||||||
| 36 | 0 | my $type = ref($item); | |||||||||
| 37 | 0 | ▶ | $self->_conn->rpush( $name, $item ) if !$type; # list | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | |||||||||||
| 39 | 0 | ▶ | if ( $type eq 'HASH' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | 0 | my %zsets = %{$item}; | |||||||||
| 41 | 0 | foreach my $range ( keys %zsets ) { | |||||||||
| 42 | 0 | $self->_conn->zadd( $name, $range, $zsets{$range} ); | |||||||||
| 43 | } | ||||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 0 | ▶ | sub _set_hash { | ||||||||
|
Subroutine
_set_hash: not called CC=5 SCAR=34.0
| |||||||||||
| 51 | 0 | my ( $self, $name, $value ) = @_; | |||||||||
| 52 | |||||||||||
| 53 | 0 | my %sets = %{$value}; | |||||||||
| 54 | 0 | foreach my $item ( keys %sets ) { | |||||||||
| 55 | 0 | my $type = ref($item); | |||||||||
| 56 | 0 | ▶ | $self->sadd( $name, $item, $sets{$item} ) if !$type; # smembers | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | |||||||||||
| 58 | 0 | ▶ | if ( $type eq 'HASH' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 0 | my %hashs = %{$item}; | |||||||||
| 60 | 0 | foreach my $key ( keys %hashs ) { | |||||||||
| 61 | 0 | $self->_conn->hset( $name, $key, $hashs{$key} ); | |||||||||
| 62 | } | ||||||||||
| 63 | } | ||||||||||
| 64 | } | ||||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | 0 | ▶ | sub _set_values_by_keys { | ||||||||
|
Subroutine
_set_values_by_keys: not called CC=6 SCAR=37.4
| |||||||||||
| 68 | 0 | my $self = shift; | |||||||||
| 69 | |||||||||||
| 70 | 0 | my %keys = %{ from_json( $self->file->slurp ) }; | |||||||||
| 71 | 0 | ▶ | $self->_conn->flushall if $self->flushall; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | |||||||||||
| 73 | 0 | foreach my $key ( keys %keys ) { | |||||||||
| 74 | 0 | my $type = ref( $keys{$key} ); | |||||||||
| 75 | 0 | ▶ | $self->_set_string( $key, $keys{$key} ) if !$type; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | 0 | ▶ | $self->_set_array( $key, $keys{$key} ) if $type eq 'ARRAY'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 0 | ▶ | $self->_set_hash( $key, $keys{$key} ) if $type eq 'HASH'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | } | ||||||||||
| 79 | 0 | return 1; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | |||||||||||
| 83 | 0 | ▶ | sub run { | ||||||||
|
Subroutine
run: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 84 | 0 | my $self = shift; | |||||||||
| 85 | |||||||||||
| 86 | 0 | ▶ | my $fh = $self->file->openr() or confess "Can't read file: $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | |||||||||||
| 88 | 0 | return $self->_set_values_by_keys; | |||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | |||||||||||
| 92 | 3 | has server => ( | |||||||||
| 93 | is => 'ro', | ||||||||||
| 94 | isa => 'Str', | ||||||||||
| 95 | default => '127.0.0.1:6379', | ||||||||||
| 96 | documentation => 'Host:Port of redis server (ex. 127.0.0.1:6379)' | ||||||||||
| 97 | ); | ||||||||||
| 98 | |||||||||||
| 99 | |||||||||||
| 100 | 3 | has database => ( | |||||||||
| 101 | is => 'ro', | ||||||||||
| 102 | isa => 'Int', | ||||||||||
| 103 | default => 0, | ||||||||||
| 104 | documentation => 'Database used in a multi-database setup' | ||||||||||
| 105 | ); | ||||||||||
| 106 | |||||||||||
| 107 | |||||||||||
| 108 | 3 | has flushall => ( | |||||||||
| 109 | is => 'ro', | ||||||||||
| 110 | isa => 'Bool', | ||||||||||
| 111 | default => 0, | ||||||||||
| 112 | documentation => 'Remove all keys from all databases' | ||||||||||
| 113 | ); | ||||||||||
| 114 | |||||||||||
| 115 | |||||||||||
| 116 | 3 | has file => ( | |||||||||
| 117 | is => 'ro', | ||||||||||
| 118 | isa => 'Path::Class::File', | ||||||||||
| 119 | coerce => 1, | ||||||||||
| 120 | documentation => 'File with restore' | ||||||||||
| 121 | ); | ||||||||||
| 122 | |||||||||||
| 123 | 3 | 1; | |||||||||
| 124 | |||||||||||
| 125 | |||||||||||
| 126 | |||||||||||
| 127 | =pod | ||||||||||
| 128 | |||||||||||
| 129 | =head1 NAME | ||||||||||
| 130 | |||||||||||
| 131 | Redis::Dump::Restore - It's a simple way to restore data to redis-server based on redis-dump. | ||||||||||
| 132 | |||||||||||
| 133 | =head1 VERSION | ||||||||||
| 134 | |||||||||||
| 135 | version 0.016 | ||||||||||
| 136 | |||||||||||
| 137 | =head1 SYNOPSIS | ||||||||||
| 138 | |||||||||||
| 139 | use Redis::Dump::Restore; | ||||||||||
| 140 | |||||||||||
| 141 | my $restore = Redis::Dump::Restore->new({ server => '127.0.0.6379', file => 'foo.txt'); | ||||||||||
| 142 | |||||||||||
| 143 | =head1 DESCRIPTION | ||||||||||
| 144 | |||||||||||
| 145 | It's a simple way to restore data to redis-server based on redis-dump (JSON). | ||||||||||
| 146 | |||||||||||
| 147 | =head1 COMMAND LINE API | ||||||||||
| 148 | |||||||||||
| 149 | This class uses L<MooseX::Getopt> to provide a command line api. The command line options map to the class attributes. | ||||||||||
| 150 | |||||||||||
| 151 | =head1 METHODS | ||||||||||
| 152 | |||||||||||
| 153 | =head2 new_with_options | ||||||||||
| 154 | |||||||||||
| 155 | Provided by L<MooseX::Getopt>. Parses attributes init args from @ARGV. | ||||||||||
| 156 | |||||||||||
| 157 | =head2 run | ||||||||||
| 158 | |||||||||||
| 159 | Perfomas the actual restore. | ||||||||||
| 160 | |||||||||||
| 161 | =head1 ATTRIBUTES | ||||||||||
| 162 | |||||||||||
| 163 | =head2 server | ||||||||||
| 164 | |||||||||||
| 165 | Host:Port of redis server, example: 127.0.0.1:6379. | ||||||||||
| 166 | |||||||||||
| 167 | =head2 database | ||||||||||
| 168 | |||||||||||
| 169 | If you want to select another database than default which is 0. | ||||||||||
| 170 | |||||||||||
| 171 | =head2 flushall | ||||||||||
| 172 | |||||||||||
| 173 | Remove all keys from all databases | ||||||||||
| 174 | |||||||||||
| 175 | =head2 file | ||||||||||
| 176 | |||||||||||
| 177 | File with dump | ||||||||||
| 178 | |||||||||||
| 179 | =head1 DEVELOPMENT | ||||||||||
| 180 | |||||||||||
| 181 | Redis::Dump is a open source project for everyone to participate. The code repository | ||||||||||
| 182 | is located on github. Feel free to send a bug report, a pull request, or a | ||||||||||
| 183 | beer. | ||||||||||
| 184 | |||||||||||
| 185 | L<http://www.github.com/maluco/Redis-Dump> | ||||||||||
| 186 | |||||||||||
| 187 | =head1 SEE ALSO | ||||||||||
| 188 | |||||||||||
| 189 | L<Redis::Dump>, L<App::Redis::Dump>, L<App::Redis::Dump::Restore> | ||||||||||
| 190 | |||||||||||
| 191 | =head1 SUPPORT | ||||||||||
| 192 | |||||||||||
| 193 | You can find documentation for this module with the perldoc command. | ||||||||||
| 194 | |||||||||||
| 195 | perldoc Redis::Dump | ||||||||||
| 196 | |||||||||||
| 197 | You can also look for information at: | ||||||||||
| 198 | |||||||||||
| 199 | =over 4 | ||||||||||
| 200 | |||||||||||
| 201 | =item * RT: CPAN's request tracker | ||||||||||
| 202 | |||||||||||
| 203 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Redis-Dump> | ||||||||||
| 204 | |||||||||||
| 205 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 206 | |||||||||||
| 207 | L<http://annocpan.org/dist/Redis-Dump> | ||||||||||
| 208 | |||||||||||
| 209 | =item * CPAN Ratings | ||||||||||
| 210 | |||||||||||
| 211 | L<http://cpanratings.perl.org/d/Redis-Dump> | ||||||||||
| 212 | |||||||||||
| 213 | =item * Search CPAN | ||||||||||
| 214 | |||||||||||
| 215 | L<http://search.cpan.org/dist/Redis-Dump> | ||||||||||
| 216 | |||||||||||
| 217 | =back | ||||||||||
| 218 | |||||||||||
| 219 | =head1 AUTHOR | ||||||||||
| 220 | |||||||||||
| 221 | Thiago Rondon <thiago@nsms.com.br> | ||||||||||
| 222 | |||||||||||
| 223 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 224 | |||||||||||
| 225 | This software is copyright (c) 2011 by Thiago Rondon. | ||||||||||
| 226 | |||||||||||
| 227 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 228 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 229 | |||||||||||
| 230 | =cut | ||||||||||
| 231 | |||||||||||
| 232 | |||||||||||
| 233 | __END__ | ||||||||||