BusyBird/StatusStorage/Memory.pm

filter: Statementstmt 80.4% 169 / 210 Branchbran 82.6% 81 / 98 Conditioncond 92.3% 24 / 26 MC/DCmcdc 76.9% 10 / 13 Subroutinesub 78.1% 25 / 32 Podpod 100% 6 / 6 totaltotal 81.8% 315 / 385 CC 62 SCAR 25.6
CC
62
Cov
82%
put_statuses
25.1
get_statuses
16.0
delete_statuses
14.3
CRAP 13.0
↑ Summary BusyBird/StatusStorage/SQLite.pm »
1 package BusyBird::StatusStorage::Memory;
2 2 ▶use v5.8.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use parent ('BusyBird::StatusStorage');
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use BusyBird::Util qw(set_param sort_statuses);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use BusyBird::Log qw(bblog);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use BusyBird::StatusStorage::Common qw(contains ack_statuses get_unacked_counts);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use BusyBird::DateTime::Format;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use List::Util qw(min);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod documented
17 3 my ($class, %options) = @_;
18 3 my $self = bless {
19 timelines => {}, ## timelines should always be sorted.
20 }, $class;
21 3 $self->set_param(\%options, 'max_status_num', 2000);
22 3 ▶ if($self->{max_status_num} <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$self{"max_status_num"} <= 0) 0 3 3
23 0 croak "max_status_num option must be bigger than 0.";
24 }
25 3 return $self;
26 }
27  
28 0 ▶sub _log {
Subroutine _log: not called CC=1 SCAR=6.9
29 0 my ($self, $level, $msg) = @_;
30 0 bblog($level, $msg);
31 }
32  
33 1057 ▶sub _index {
Subroutine _index: called CC=4 SCAR=13.9
34 1057 my ($self, $timeline, $id) = @_;
35 1057 ▶ return -1 if not defined($self->{timelines}{$timeline});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$self{"timelines"}{$timeline} 45 1012 1057
36 1012 my $tl = $self->{timelines}{$timeline};
37 1012 my @ret = grep { $tl->[$_]{id} eq $id } 0..$#$tl;
38 1012 ▶ confess "multiple IDs in timeline $timeline." if int(@ret) >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if int @ret >= 2 0 1012 1012
39 1012 ▶ return int(@ret) == 0 ? -1 : $ret[0];
Branch: 1 branch
BranchTrueFalseTotal
int @ret == 0 ? : 533 479 1012
40 }
41  
42 4930 ▶sub _acked {
Subroutine _acked: called CC=1 SCAR=0.0
43 4930 my ($self, $status) = @_;
44 2 ▶ no autovivification;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 4930 return $status->{busybird}{acked_at};
46 }
47  
48 0 ▶sub save {
Subroutine save: not called CC=3 SCAR=24.8
Pod documented
49 0 my ($self, $filepath) = @_;
50 0 ▶ if(not defined($filepath)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $filepath) 0 0 0
51 0 croak '$filepath is not specified.';
52 }
53 0 my $file;
54 0 ▶ if(!open $file, ">", $filepath) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $file, ">", $filepath) 0 0 0
55 0 $self->_log("error", "Cannot open $filepath to write.");
56 0 return 0;
57 }
58 0 my $success;
59 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
60 0 print $file encode_json($self->{timelines});
61 0 $success = 1;
62 0 ▶ }catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
63 0 my $e = shift;
64 0 $self->_log("error", "Error while saving: $e");
65 0 $success = 0;
66 0 };
67 0 close $file;
68 0 return $success;
69 }
70  
71 0 ▶sub load {
Subroutine load: not called CC=3 SCAR=24.8
Pod documented
72 0 my ($self, $filepath) = @_;
73 0 ▶ if(not defined($filepath)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $filepath) 0 0 0
74 0 croak '$filepath is not specified.';
75 }
76 0 my $file;
77 0 ▶ if(!open $file, "<", $filepath) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $file, "<", $filepath) 0 0 0
78 0 $self->_log("notice", "Cannot open $filepath to read");
79 0 return 0;
80 }
81 0 my $success;
82 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
83 0 my $text = do { local $/; <$file> };
84 0 $self->{timelines} = decode_json($text);
85 0 $success = 1;
86 0 ▶ }catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
87 0 my $e = shift;
88 0 $self->_log("error", "Error while loading: $e");
89 0 $success = 0;
90 0 };
91 0 close $file;
92 0 return $success;
93 }
94  
95 1799 ▶sub _is_timestamp_format_ok {
Subroutine _is_timestamp_format_ok: called CC=2 SCAR=6.9
96 1799 my ($timestamp_str) = @_;
97 1799 ▶ return 1 if not defined $timestamp_str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timestamp_str 481 1318 1799
98
99 ## It is very inefficient to parse $timestamp_str to check its
100 ## format, because creating a DateTime object takes long time. We
101 ## do it because BB::SS::Memory is just a reference
102 ## implementation.
103 1318 return defined(BusyBird::DateTime::Format->parse_datetime($timestamp_str));
104 }
105  
106 121 ▶sub put_statuses {
Subroutine put_statuses: called CC=25 SCAR=32.2
Pod documented
107 121 my ($self, %args) = @_;
108 121 ▶ croak 'timeline arg is mandatory' if not defined $args{timeline};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"timeline"} 1 120 121
109 120 my $timeline = $args{timeline};
110 120 ▶ if(!defined($args{mode}) ||
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $args{"mode"} or $args{"mode"} ne "insert" and $args{"mode"} ne "update" and $args{"mode"} ne "upsert") 1 119 120
Condition77%7/9
not defined $args{"mode"} or $args{"mode"} ne "insert" and $args{"mode"} ne "update" and $args{"mode"} ne "upsert"
l!l&&r!l&&!r
10119
$args{"mode"} ne "insert" and $args{"mode"} ne "update" and $args{"mode"} ne "upsert"
!ll&&!rl&&r
11270
$args{"mode"} ne "insert" and $args{"mode"} ne "update"
!ll&&!rl&&r
69437
MC/DC25%1/4
not defined $args{"mode"} or $args{"mode"} ne "insert" and $args{"mode"} ne "update" and $args{"mode"} ne "upsert"
not defined $args{"mode"} $args{"mode"} ne "insert" $args{"mode"} ne "update" $args{"mode"} ne "upsert"
Truth table80%4/5
not(A) or B and C and D
A= not defined $args{"mode"}B= $args{"mode"} ne "insert"C= $args{"mode"} ne "update"D= $args{"mode"} ne "upsert"
ABCDresult
1XXX1
01111
00XX0
010X0
01100
111 ($args{mode} ne 'insert'
112 && $args{mode} ne 'update' && $args{mode} ne 'upsert')) {
113 1 croak 'mode arg must be insert/update/upsert';
114 }
115 119 my $mode = $args{mode};
116 119 my $statuses;
117 119 ▶ if(!defined($args{statuses})) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $args{'statuses'}) { } 1 118 119
elsif (ref $args{'statuses'} eq 'HASH') { } 28 90 118
elsif (ref $args{'statuses'} eq 'ARRAY') { } 90 0 90
118 1 croak 'statuses arg is mandatory';
119 }elsif(ref($args{statuses}) eq 'HASH') {
120 28 $statuses = [ $args{statuses} ];
121 }elsif(ref($args{statuses}) eq 'ARRAY') {
122 90 $statuses = $args{statuses};
123 }else {
124 0 croak 'statuses arg must be STATUS/ARRAYREF_OF_STATUSES';
125 }
126 118 foreach my $s (@$statuses) {
127 2 ▶ no autovivification;
Subroutine BEGIN: called CC=1 SCAR=0.0
128 919 ▶ croak "{id} field is mandatory in statuses" if not defined $s->{id};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$s{"id"} 15 904 919
129 904 ▶ croak "{busybird} field must be a hash-ref if present" if defined($s->{busybird}) && ref($s->{busybird}) ne "HASH";
Branch: 1 branch
BranchTrueFalseTotal
if defined $$s{"busybird"} and ref $$s{"busybird"} ne "HASH" 3 901 904
Condition100%3/3
defined $$s{"busybird"} and ref $$s{"busybird"} ne "HASH"
!ll&&!rl&&r
4354663
MC/DC100%2/2
defined $$s{"busybird"} and ref $$s{"busybird"} ne "HASH"
defined $$s{"busybird"} ref $$s{"busybird"} ne "HASH"
Truth table100%3/3
A and B
A= defined $$s{"busybird"}B= ref $$s{"busybird"} ne "HASH"
ABresult
0X0
100
111
130 901 ▶ croak "{created_at} field must be parsable by BusyBird::DateTime::Format" if !_is_timestamp_format_ok($s->{created_at});
Branch: 1 branch
BranchTrueFalseTotal
unless _is_timestamp_format_ok($$s{"created_at"}) 3 898 901
131 898 my $acked_at = $s->{busybird}{acked_at}; ## avoid autovivification
132 898 ▶ croak "{busybird}{acked_at} field must be parsable by BusyBird::DateTime::Format" if !_is_timestamp_format_ok($acked_at);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_timestamp_format_ok($acked_at) 3 895 898
133 }
134 94 my $put_count = 0;
135 94 foreach my $status_index (reverse 0 .. $#$statuses) {
136 880 my $s = $statuses->[$status_index];
137 880 my $tl_index = $self->_index($timeline, $s->{id});
138 880 my $existent = ($tl_index >= 0);
139 880 ▶ next if ($mode eq 'insert' && $existent) || ($mode eq 'update' && !$existent);
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "insert" and $existent or $mode eq "update" and not $existent 11 869 880
Condition100%9/9
$mode eq "insert" and $existent or $mode eq "update" and not $existent
l!l&&r!l&&!r
56869
$mode eq "insert" and $existent
!ll&&!rl&&r
3395365
$mode eq "update" and not $existent
!ll&&!rl&&r
5603096
MC/DC100%4/4
$mode eq "insert" and $existent or $mode eq "update" and not $existent
$mode eq "insert" $existent $mode eq "update" not $existent
Truth table71%5/7
A and B or C and not(D)
A= $mode eq "insert"B= $existentC= $mode eq "update"D= not $existent
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
140 869 my $is_insert = ($mode eq 'insert');
141 869 ▶ if($mode eq 'upsert') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "upsert") 24 845 869
142 24 $is_insert = (!$existent);
143 }
144 869 ▶ if($is_insert) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_insert) { } 548 321 869
145 548 unshift(@{$self->{timelines}{$timeline}}, dclone($s));
146 }else {
147 ## update
148 321 $self->{timelines}{$timeline}[$tl_index] = dclone($s);
149 }
150 869 $put_count++;
151 }
152 94 ▶ if($put_count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($put_count > 0) 90 4 94
153 90 $self->{timelines}{$timeline} = sort_statuses($self->{timelines}{$timeline});
154 90 ▶ if(int(@{$self->{timelines}{$timeline}}) > $self->{max_status_num}) {
Branch: 1 branch
BranchTrueFalseTotal
if (int @{$$self{"timelines"}{$timeline};} > $$self{"max_status_num"}) 3 87 90
155 3 splice(@{$self->{timelines}{$timeline}}, -(int(@{$self->{timelines}{$timeline}}) - $self->{max_status_num}));
156 }
157 }
158 94 ▶ if($args{callback}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"callback"}) 94 0 94
159 94 @_ = (undef, $put_count);
160 94 goto $args{callback};
161 }
162 }
163  
164 58 ▶sub delete_statuses {
Subroutine delete_statuses: called CC=14 SCAR=26.6
Pod documented
165 58 my ($self, %args) = @_;
166 58 ▶ croak 'timeline arg is mandatory' if not defined $args{timeline};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"timeline"} 1 57 58
167 57 ▶ croak 'ids arg is mandatory' if not exists $args{ids};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"ids"} 1 56 57
168 56 my $timeline = $args{timeline};
169 56 my $ids = $args{ids};
170 56 ▶ if(defined($ids)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ids) 7 49 56
171 7 ▶ if(!ref($ids)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $ids) { } 2 5 7
elsif (ref $ids eq 'ARRAY') { } 5 0 5
172 2 $ids = [$ids];
173 }elsif(ref($ids) eq 'ARRAY') {
174 5 ▶ croak "ids arg array must not contain undef" if grep { !defined($_) } @$ids;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_;} @$ids 1 4 5
175 }else {
176 0 croak "ids must be undef/ID/ARRAYREF_OF_IDS";
177 }
178 }
179 55 ▶ if(!$self->{timelines}{$timeline}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$self{"timelines"}{$timeline}) 14 41 55
180 14 ▶ if($args{callback}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"callback"}) 14 0 14
181 14 @_ = (undef, 0);
182 14 goto $args{callback};
183 }
184 0 return;
185 }
186 41 my $delete_num = 0;
187 41 ▶ if(defined($ids)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ids) { } 5 36 41
188 5 foreach my $id (@$ids) {
189 8 my $tl_index = $self->_index($timeline, $id);
190 8 ▶ last if $tl_index < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $tl_index < 0 1 7 8
191 7 splice(@{$self->{timelines}{$timeline}}, $tl_index, 1);
192 7 $delete_num++;
193 }
194 }else {
195 36 ▶ if(defined($self->{timelines}{$timeline})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$self{"timelines"}{$timeline}) 36 0 36
196 36 $delete_num = @{$self->{timelines}{$timeline}};
197 36 delete $self->{timelines}{$timeline};
198 }
199 }
200 41 ▶ if($args{callback}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"callback"}) 41 0 41
201 41 @_ = (undef, $delete_num);
202 41 goto $args{callback};
203 }
204 }
205  
206 538 ▶sub get_statuses {
Subroutine get_statuses: called CC=16 SCAR=27.7
Pod documented
207 538 my ($self, %args) = @_;
208 538 ▶ croak 'timeline arg is mandatory' if not defined $args{timeline};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"timeline"} 1 537 538
209 537 ▶ croak 'callback arg is mandatory' if not defined $args{callback};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"callback"} 1 536 537
210 536 my $timeline = $args{timeline};
211 536 ▶ if(!$self->{timelines}{$timeline}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$self{"timelines"}{$timeline}) 68 468 536
212 68 @_ = (undef, []);
213 68 goto $args{callback};
214 }
215 468 ▶ my $ack_state = $args{ack_state} || 'any';
Condition100%2/2
$args{'ack_state'} || 'any'
l!l
37593
MC/DC100%1/1
$args{'ack_state'} || 'any'
$args{'ack_state'}
Truth table100%2/2
A
A= $args{'ack_state'}
Aresult
11
00
216 468 my $max_id = $args{max_id};
217 468 ▶ my $count = defined($args{count}) ? $args{count} : 20;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'count'} ? : 468 0 468
218 2866 ▶ my $ack_test = $ack_state eq 'unacked' ? sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
219 2866 !$self->_acked(shift);
220 2064 ▶ } : $ack_state eq 'acked' ? sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
221 2064 $self->_acked(shift);
222 468 ▶ } : sub { 1 };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 2 branches
BranchTrueFalseTotal
$ack_state eq 'unacked' ? : 175 293 468
$ack_state eq 'acked' ? : 113 180 293
223 468 my $start_index;
224 468 ▶ if(defined($max_id)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max_id) 169 299 468
225 169 my $tl_index = $self->_index($timeline, $max_id);
226 169 ▶ if($tl_index < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tl_index < 0) 23 146 169
227 23 @_ = (undef, []);
228 23 goto $args{callback};
229 }
230 146 my $s = $self->{timelines}{$timeline}[$tl_index];
231 146 ▶ if(!$ack_test->($s)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&$ack_test($s)) 40 106 146
232 40 @_ = (undef, []);
233 40 goto $args{callback};
234 }
235 106 $start_index = $tl_index;
236 }
237 my @indice = grep {
238 7905 ▶ if(!$ack_test->($self->{timelines}{$timeline}[$_])) {
Branch: 2 branches
BranchTrueFalseTotal
if (not &$ack_test($$self{'timelines'}{$timeline}[$_])) { } 2283 5622 7905
elsif (defined $start_index and $_ < $start_index) { } 726 4896 5622
239 2283 0;
240 ▶ }elsif(defined($start_index) && $_ < $start_index) {
Condition100%3/3
defined $start_index and $_ < $start_index
!ll&&!rl&&r
3971925726
MC/DC100%2/2
defined $start_index and $_ < $start_index
defined $start_index $_ < $start_index
Truth table100%3/3
A and B
A= defined $start_indexB= $_ < $start_index
ABresult
0X0
100
111
241 726 0;
242 }else {
243 4896 1;
244 }
245 405 } 0 .. $#{$self->{timelines}{$timeline}};
246 405 ▶ $count = int(@indice) if $count eq 'all';
Branch: 1 branch
BranchTrueFalseTotal
if $count eq "all" 315 90 405
247 405 $count = min($count, int(@indice));
248 my $result_statuses = $count <= 0 ? [] : [ map {
249 405 ▶ dclone($self->{timelines}{$timeline}[$_])
Branch: 1 branch
BranchTrueFalseTotal
$count <= 0 ? : 33 372 405
250 } @indice[0 .. ($count-1)] ];
251  
252 405 @_ = (undef, $result_statuses);
253 405 goto $args{callback};
254 }
255  
256 2 1;
257  
258 __END__
↑ Summary BusyBird/StatusStorage/SQLite.pm »