File Coverage

blib/lib/Redis/AOF/Tail/File.pm
Criterion Covered Total %
statement 49 49 100.0
branch 10 12 83.3
condition 6 6 100.0
subroutine 6 6 100.0
pod 2 2 100.0
total 73 75 97.3


line stmt bran cond sub pod time code
1             package Redis::AOF::Tail::File;
2              
3 4     4   76581 use 5.008008;
  4         15  
  4         145  
4 4     4   18 use strict;
  4         6  
  4         194  
5 4     4   24 use warnings;
  4         11  
  4         147  
6 4     4   2167 use File::Tail::Lite;
  4         1830  
  4         1399  
7              
8             our $VERSION = '0.06';
9              
10             sub new {
11 5     5 1 1198 my $pkg = shift;
12 5         16 my $self = {@_};
13 5         10 bless $self, $pkg;
14              
15 5 50       131 return 0 unless -e $self->{aof_filename};
16              
17 5         11 $self->{ARRAY_REDIS_AOF} = ();
18              
19 5 100       14 if ( $self->{seekpos} ) {
20 4         26 $self->{FILE_TAIL_FH} = new File::Tail::Lite(
21             filename => $self->{aof_filename},
22             seekpos => $self->{seekpos}
23             );
24             }
25             else {
26 1         8 $self->{FILE_TAIL_FH} = $self->{FILE_TAIL_FH} = new File::Tail::Lite(
27             filename => $self->{aof_filename},
28             seekpos => 'start'
29             );
30             }
31 5         353 return $self;
32             }
33              
34             sub read_command {
35 9     9 1 1493 my $self = shift;
36 9 50       25 return 0 unless $self->{FILE_TAIL_FH};
37              
38 9         30 while ( my ( $pos, $line ) = $self->{FILE_TAIL_FH}->readline() ) {
39 70         1415 $line =~ s/\s//g;
40 70 100       118 next if length($line) == 0;
41 69         46 push @{ $self->{ARRAY_REDIS_AOF} }, $line;
  69         103  
42 69   100     58 while ( defined ${ $self->{ARRAY_REDIS_AOF} }[0]
  77         135  
  69         217  
43             and ${ $self->{ARRAY_REDIS_AOF} }[0] !~ /^\*\d/ )
44             {
45 8         7 shift @{ $self->{ARRAY_REDIS_AOF} };
  8         10  
46             }
47 61         145 my ($cmd_num) = ${ $self->{ARRAY_REDIS_AOF} }[0] =~ /^\*(\d{1,2})/
  69         108  
48 69 100       56 if ${ $self->{ARRAY_REDIS_AOF} }[0];
49              
50             next
51 61         261 if ( !$cmd_num
52 69 100 100     133 or scalar @{ $self->{ARRAY_REDIS_AOF} } < $cmd_num * 2 + 1 )
53             ; # Wait for the complete command
54              
55 9         18 shift @{ $self->{ARRAY_REDIS_AOF} };
  9         12  
56 9         11 my $cmd = "";
57 9         17 for ( 1 .. $cmd_num ) {
58 26         18 shift @{ $self->{ARRAY_REDIS_AOF} };
  26         25  
59 26         23 $cmd .= shift @{ $self->{ARRAY_REDIS_AOF} };
  26         25  
60 26         29 $cmd .= ' ';
61             }
62 9         16 $cmd = substr( $cmd, 0, -1 );
63 9         20 return ( $pos, $cmd );
64             }
65             }
66              
67             1;
68             __END__