BusyBird/StatusStorage/SQLite.pm

filter: Statementstmt 94.6% 462 / 488 Branchbran 83.9% 173 / 206 Conditioncond 80.4% 66 / 82 MC/DCmcdc 44.9% 22 / 49 Subroutinesub 88.0% 59 / 67 Podpod 100% 9 / 9 totaltotal 87.7% 791 / 901 CC 137 SCAR 19.0
CC
137
Cov
90%
put_statuses
16.7
delete_statuses
12.8
ack_statuses
11.5
CRAP 6.7
« BusyBird/StatusStorage/Memory.pm ↑ Summary BusyBird/Runner.pm »
1 package BusyBird::StatusStorage::SQLite;
2 12 ▶use v5.8.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use parent ("BusyBird::StatusStorage");
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use SQL::Maker 1.19;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use SQL::QueryMaker 0.03 qw(sql_and sql_eq sql_ne sql_or sql_lt sql_le sql_raw);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use BusyBird::DateTime::Format;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use BusyBird::Util qw(set_param);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 12 ▶use DateTime::Format::Strptime;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 12 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 12 ▶no autovivification;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 12 my @STATUSES_ORDER_BY = ('utc_acked_at DESC', 'utc_created_at DESC', 'status_id DESC');
20 12 my $DELETE_COUNT_ID = 0;
21  
22 12 my $UNDEF_TIMESTAMP = '9999-99-99T99:99:99';
23  
24 {
25 12 my $TIMESTAMP_FORMAT_STR = '%Y-%m-%dT%H:%M:%S';
26 12 my $TIMESTAMP_FORMAT = DateTime::Format::Strptime->new(
27 pattern => $TIMESTAMP_FORMAT_STR,
28 time_zone => 'UTC',
29 on_error => 'croak',
30 );
31  
32 4539 ▶ sub _format_datetime {
Subroutine _format_datetime: called CC=1 SCAR=0.0
33 4539 my ($dt) = @_;
34 4539 return $dt->strftime($TIMESTAMP_FORMAT_STR);
35 }
36  
37 15621 ▶ sub _parse_datetime {
Subroutine _parse_datetime: called CC=1 SCAR=0.0
38 15621 my ($dt_str) = @_;
39 15621 return $TIMESTAMP_FORMAT->parse_datetime($dt_str);
40 }
41 }
42  
43  
44 12 ▶sub new {
Subroutine new: called CC=4 SCAR=14.0
Pod documented
45 183 my ($class, %args) = @_;
46 183 my $self = bless {
47 maker => SQL::Maker->new(driver => 'SQLite', strict => 1),
48 in_memory_dbh => undef,
49 }, $class;
50 183 $self->set_param(\%args, "path", undef, 1);
51 183 $self->set_param(\%args, "max_status_num", 2000);
52 183 $self->set_param(\%args, "hard_max_status_num", int($self->{max_status_num} * 1.2));
53 183 $self->set_param(\%args, "vacuum_on_delete", int($self->{max_status_num} * 2.0));
54 183 ▶ croak "max_status_num must be a number" if !looks_like_number($self->{max_status_num});
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($$self{"max_status_num"}) 0 183 183
55 183 ▶ croak "hard_max_status_num must be a number" if !looks_like_number($self->{hard_max_status_num});
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($$self{"hard_max_status_num"}) 0 183 183
56 183 $self->{max_status_num} = int($self->{max_status_num});
57 183 $self->{hard_max_status_num} = int($self->{hard_max_status_num});
58 183 ▶ croak "hard_max_status_num must be >= max_status_num" if !($self->{hard_max_status_num} >= $self->{max_status_num});
Branch: 1 branch
BranchTrueFalseTotal
unless $$self{"hard_max_status_num"} >= $$self{"max_status_num"} 0 183 183
59 183 $self->_create_tables();
60 182 return $self;
61 }
62  
63 821 ▶sub _create_new_dbh {
Subroutine _create_new_dbh: called CC=1 SCAR=0.0
64 821 my ($self, @connect_params) = @_;
65 821 my $dbh = DBI->connect(@connect_params);
66 820 $dbh->do(q{PRAGMA foreign_keys = ON});
67 820 return $dbh;
68 }
69  
70 2649 ▶sub _get_my_dbh {
Subroutine _get_my_dbh: called CC=3 SCAR=11.0
71 2649 my ($self) = @_;
72 2649 my @connect_params = ("dbi:SQLite:dbname=$self->{path}", "", "", {
73 RaiseError => 1, PrintError => 0, AutoCommit => 1, sqlite_unicode => 1,
74 });
75 2649 ▶ if($self->{path} eq ':memory:') {
Branch: 1 branch
BranchTrueFalseTotal
if ($$self{"path"} eq ":memory:") 2000 649 2649
76 2000 ▶ $self->{in_memory_dbh} = $self->_create_new_dbh(@connect_params) if !$self->{in_memory_dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless $$self{"in_memory_dbh"} 172 1828 2000
77 2000 return $self->{in_memory_dbh};
78 }
79 649 return $self->_create_new_dbh(@connect_params);
80 }
81  
82 183 ▶sub _create_tables {
Subroutine _create_tables: called CC=1 SCAR=0.0
83 183 my ($self) = @_;
84 183 my $dbh = $self->_get_my_dbh();
85 182 $dbh->do(<<EOD);
86 CREATE TABLE IF NOT EXISTS timelines (
87 timeline_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
88 name TEXT UNIQUE NOT NULL
89 )
90 EOD
91 182 $dbh->do(<<EOD);
92 CREATE TABLE IF NOT EXISTS statuses (
93 timeline_id INTEGER NOT NULL
94 REFERENCES timelines(timeline_id) ON DELETE CASCADE ON UPDATE CASCADE,
95 status_id TEXT NOT NULL,
96 utc_acked_at TEXT NOT NULL,
97 utc_created_at TEXT NOT NULL,
98 timezone_acked_at TEXT NOT NULL,
99 timezone_created_at TEXT NOT NULL,
100 level INTEGER NOT NULL,
101 content TEXT NOT NULL,
102  
103 PRIMARY KEY (timeline_id, status_id)
104 )
105 EOD
106 182 $dbh->do(<<EOD);
107 CREATE TABLE IF NOT EXISTS delete_counts (
108 delete_count_id INTEGER PRIMARY KEY NOT NULL,
109 delete_count INTEGER NOT NULL
110 )
111 EOD
112 182 my ($sql, @bind) = $self->{maker}->insert('delete_counts', [
113 delete_count_id => $DELETE_COUNT_ID, delete_count => 0
114 ], {prefix => 'INSERT OR IGNORE INTO'});
115 182 $dbh->do($sql, undef, @bind);
116 }
117  
118 3283 ▶sub _record_hash_to_array {
Subroutine _record_hash_to_array: called CC=1 SCAR=0.0
119 3283 my ($record) = @_;
120 3283 return [ map { $_, $record->{$_} } sort { $a cmp $b } keys %$record ];
121 }
122  
123 426 ▶sub _put_update {
Subroutine _put_update: called CC=2 SCAR=6.9
124 426 my ($self, $dbh, $record, $prev_sth) = @_;
125 426 my $sth = $prev_sth;
126 my ($sql, @bind) = $self->{maker}->update('statuses', _record_hash_to_array($record), sql_and([
127 sql_eq('timeline_id' => $record->{timeline_id}), sql_eq(status_id => $record->{status_id})
128 426 ]));
129 426 ▶ if(!$sth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 111 315 426
130 ## Or, should we check $sql is not changed...?
131 111 $sth = $dbh->prepare($sql);
132 }
133 426 return ($sth->execute(@bind), $sth);
134 }
135  
136 2857 ▶sub _put_insert {
Subroutine _put_insert: called CC=2 SCAR=6.9
137 2857 my ($self, $dbh, $record, $prev_sth) = @_;
138 2857 my $sth = $prev_sth;
139 2857 my ($sql, @bind) = $self->{maker}->insert('statuses', _record_hash_to_array($record), {
140 prefix => 'INSERT OR IGNORE INTO'
141 });
142 2857 ▶ if(!$sth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 371 2486 2857
143 371 $sth = $dbh->prepare($sql);
144 }
145 2857 return ($sth->execute(@bind), $sth);
146 }
147  
148 68 ▶sub _put_upsert {
Subroutine _put_upsert: called CC=2 SCAR=6.9
149 68 my ($self, $dbh, $record) = @_;
150 68 my ($count) = $self->_put_update($dbh, $record);
151 68 ▶ if($count <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count <= 0) 34 34 68
152 34 ($count) = $self->_put_insert($dbh, $record);
153 }
154 68 return ($count, undef);
155 }
156  
157 430 ▶sub put_statuses {
Subroutine put_statuses: called CC=16 SCAR=28.2
Pod documented
158 430 my ($self, %args) = @_;
159 430 my $timeline = $args{timeline};
160 430 ▶ croak "timeline parameter is mandatory" if not defined $timeline;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeline 2 428 430
161 428 my $mode = $args{mode};
162 428 ▶ croak "mode parameter is mandatory" if not defined $mode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mode 2 426 428
163 426 ▶ if($mode ne 'insert' && $mode ne 'update' && $mode ne 'upsert') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode ne "insert" and $mode ne "update" and $mode ne "upsert") 0 426 426
Condition83%5/6
$mode ne "insert" and $mode ne "update" and $mode ne "upsert"
!ll&&!rl&&r
408180
$mode ne "insert" and $mode ne "update"
!ll&&!rl&&r
3555318
MC/DC0%0/3
$mode ne "insert" and $mode ne "update" and $mode ne "upsert"
$mode ne "insert" $mode ne "update" $mode ne "upsert"
Truth table75%3/4
A and B and C
A= $mode ne "insert"B= $mode ne "update"C= $mode ne "upsert"
ABCresult
0XX0
10X0
1100
1111
164 0 croak "mode must be either insert, update or upsert";
165 }
166 426 my $statuses = $args{statuses};
167 426 ▶ croak "statuses parameter is mandatory" if not defined $statuses;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $statuses 2 424 426
168 424 ▶ if(ref($statuses) ne 'HASH' && ref($statuses) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $statuses ne "HASH" and ref $statuses ne "ARRAY") 0 424 424
Condition66%2/3
ref $statuses ne "HASH" and ref $statuses ne "ARRAY"
!ll&&!rl&&r
823420
MC/DC0%0/2
ref $statuses ne "HASH" and ref $statuses ne "ARRAY"
ref $statuses ne "HASH" ref $statuses ne "ARRAY"
Truth table66%2/3
A and B
A= ref $statuses ne "HASH"B= ref $statuses ne "ARRAY"
ABresult
0X0
100
111
169 0 croak "statuses parameter must be either a status object or an array-ref of statuses";
170 }
171 424 ▶ if(ref($statuses) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $statuses eq "HASH") 82 342 424
172 82 $statuses = [$statuses];
173 }
174 424 foreach my $status (@$statuses) {
175 3309 ▶ croak "status object must be a hash-ref" if !defined($status) || !ref($status) || ref($status) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $status && ref $status or ref $status ne "HASH" 6 3303 3309
Condition66%4/6
not defined $status && ref $status or ref $status ne "HASH"
l!l&&r!l&&!r
603303
defined $status && ref $status
!ll&&!rl&&r
603303
MC/DC33%1/3
not defined $status && ref $status or ref $status ne "HASH"
defined $status ref $status ref $status ne "HASH"
Truth table50%2/4
not(A && B) or C
A= defined $statusB= ref $statusC= ref $status ne "HASH"
ABCresult
0XX1
10X1
1111
1100
176 3303 ▶ croak "status ID is missing" if not defined $status->{id};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$status{"id"} 24 3279 3303
177 }
178 394 ▶ my $callback = $args{callback} || sub {};
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
$args{'callback'} || sub { }
l!l&&r!l&&!r
39400
MC/DC0%0/2
$args{'callback'} || sub { }
$args{'callback'} sub { }
Truth table33%1/3
A || B
A= $args{'callback'}B= sub { }
ABresult
1X1
011
000
179 394 my $dbh;
180 394 ▶ my @results = try {
Subroutine __ANON__: called CC=9 SCAR=22.0
181 394 ▶ return (undef, 0) if @$statuses == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$statuses == 0 6 388 394
182 388 $dbh = $self->_get_my_dbh();
183 388 $dbh->begin_work();
184 388 ▶ my $timeline_id = $self->_get_timeline_id($dbh, $timeline) || $self->_create_timeline($dbh, $timeline);
Condition66%2/3
$self->_get_timeline_id($dbh, $timeline) || $self->_create_timeline($dbh, $timeline)
l!l&&r!l&&!r
1212670
MC/DC0%0/2
$self->_get_timeline_id($dbh, $timeline) || $self->_create_timeline($dbh, $timeline)
$self->_get_timeline_id($dbh, $timeline) $self->_create_timeline($dbh, $timeline)
Truth table66%2/3
A || B
A= $self->_get_timeline_id($dbh, $timeline)B= $self->_create_timeline($dbh, $timeline)
ABresult
1X1
011
000
185 388 ▶ if(!defined($timeline_id)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $timeline_id) 0 388 388
186 0 die "Internal error: could not create a timeline '$timeline' somehow.";
187 }
188 388 my $sth;
189 388 my $total_count = 0;
190 388 my $put_method = "_put_$mode";
191 388 foreach my $status (@$statuses) {
192 3267 my $record = _to_status_record($timeline_id, $status);
193 3249 my $count;
194 3249 ($count, $sth) = $self->$put_method($dbh, $record, $sth);
195 3249 ▶ if($count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count > 0) 3211 38 3249
196 3211 $total_count += $count;
197 }
198 }
199 370 my $exceeding_delete_count = 0;
200 370 ▶ if($mode ne "update" && $total_count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode ne "update" and $total_count > 0) 325 45 370
Condition100%3/3
$mode ne "update" and $total_count > 0
!ll&&!rl&&r
432325
MC/DC100%2/2
$mode ne "update" and $total_count > 0
$mode ne "update" $total_count > 0
Truth table100%3/3
A and B
A= $mode ne "update"B= $total_count > 0
ABresult
0X0
100
111
201 325 $exceeding_delete_count = $self->_delete_exceeding_statuses($dbh, $timeline_id);
202 }
203 370 $dbh->commit();
204 370 ▶ if($exceeding_delete_count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exceeding_delete_count > 0) 8 362 370
205 8 $self->_add_to_delete_count($dbh, $exceeding_delete_count);
206 }
207 370 return (undef, $total_count);
208 18 ▶ } catch {
Subroutine __ANON__: called CC=2 SCAR=7.0
209 18 my $e = shift;
210 18 ▶ if($dbh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh) 18 0 18
211 18 $dbh->rollback();
212 }
213 18 return ($e);
214 394 };
215 394 @_ = @results;
216 394 goto $callback;
217 }
218  
219 2721 ▶sub _get_timeline_id {
Subroutine _get_timeline_id: called CC=2 SCAR=6.9
220 2721 my ($self, $dbh, $timeline_name) = @_;
221 2721 my ($sql, @bind) = $self->{maker}->select('timelines', ['timeline_id'], sql_eq(name => $timeline_name));
222 2721 my $record = $dbh->selectrow_arrayref($sql, undef, @bind);
223 2721 ▶ if(!defined($record)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $record) 644 2077 2721
224 644 return undef;
225 }
226 2077 return $record->[0];
227 }
228  
229 267 ▶sub _create_timeline {
Subroutine _create_timeline: called CC=1 SCAR=0.0
230 267 my ($self, $dbh, $timeline_name) = @_;
231 267 my ($sql, @bind) = $self->{maker}->insert('timelines', [name => "$timeline_name"]);
232 267 $dbh->do($sql, undef, @bind);
233 267 return $self->_get_timeline_id($dbh, $timeline_name);
234 }
235  
236 3267 ▶sub _to_status_record {
Subroutine _to_status_record: called CC=4 SCAR=14.0
237 3267 my ($timeline_id, $status) = @_;
238 3267 ▶ croak "status ID must be set" if not defined $status->{id};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$status{"id"} 0 3267 3267
239 3267 ▶ croak "timeline_id must be defined" if not defined $timeline_id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeline_id 0 3267 3267
240 my $record = {
241 timeline_id => $timeline_id,
242 status_id => $status->{id},
243 3267 ▶ level => $status->{busybird}{level} || 0,
Condition100%2/2
$$status{'busybird'}{'level'} || 0
l!l
1393122
MC/DC100%1/1
$$status{'busybird'}{'level'} || 0
$$status{'busybird'}{'level'}
Truth table100%2/2
A
A= $$status{'busybird'}{'level'}
Aresult
11
00
244 };
245 3261 my $acked_at = $status->{busybird}{acked_at}; ## avoid autovivification
246 3261 ($record->{utc_acked_at}, $record->{timezone_acked_at}) = _extract_utc_timestamp_and_timezone($acked_at);
247 3255 ($record->{utc_created_at}, $record->{timezone_created_at}) = _extract_utc_timestamp_and_timezone($status->{created_at});
248 3249 $record->{content} = to_json($status);
249 3249 return $record;
250 }
251  
252 10319 ▶sub _from_status_record {
Subroutine _from_status_record: called CC=7 SCAR=19.5
253 10319 my ($record) = @_;
254 10319 my $status = from_json($record->{content});
255 10319 $status->{id} = $record->{status_id};
256 10319 ▶ if($record->{level} != 0 || defined($status->{busybird}{level})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$record{"level"} != 0 or defined $$status{"busybird"}{"level"}) 2760 7559 10319
Condition100%3/3
$$record{"level"} != 0 or defined $$status{"busybird"}{"level"}
l!l&&r!l&&!r
27424867559
MC/DC100%2/2
$$record{"level"} != 0 or defined $$status{"busybird"}{"level"}
$$record{"level"} != 0 defined $$status{"busybird"}{"level"}
Truth table100%3/3
A or B
A= $$record{"level"} != 0B= defined $$status{"busybird"}{"level"}
ABresult
1X1
011
000
257 2760 $status->{busybird}{level} = $record->{level};
258 }
259 10319 my $acked_at_str = _create_bb_timestamp_from_utc_timestamp_and_timezone($record->{utc_acked_at}, $record->{timezone_acked_at});
260 10319 ▶ if(defined($acked_at_str) || defined($status->{busybird}{acked_at})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $acked_at_str or defined $$status{"busybird"}{"acked_at"}) 5328 4991 10319
Condition66%2/3
defined $acked_at_str or defined $$status{"busybird"}{"acked_at"}
l!l&&r!l&&!r
532804991
MC/DC50%1/2
defined $acked_at_str or defined $$status{"busybird"}{"acked_at"}
defined $acked_at_str defined $$status{"busybird"}{"acked_at"}
Truth table66%2/3
A or B
A= defined $acked_at_strB= defined $$status{"busybird"}{"acked_at"}
ABresult
1X1
011
000
261 5328 $status->{busybird}{acked_at} = $acked_at_str;
262 }
263 10319 my $created_at_str = _create_bb_timestamp_from_utc_timestamp_and_timezone($record->{utc_created_at}, $record->{timezone_created_at});
264 10319 ▶ if(defined($created_at_str) || defined($status->{created_at})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $created_at_str or defined $$status{"created_at"}) 10293 26 10319
Condition66%2/3
defined $created_at_str or defined $$status{"created_at"}
l!l&&r!l&&!r
10293026
MC/DC50%1/2
defined $created_at_str or defined $$status{"created_at"}
defined $created_at_str defined $$status{"created_at"}
Truth table66%2/3
A or B
A= defined $created_at_strB= defined $$status{"created_at"}
ABresult
1X1
011
000
265 10293 $status->{created_at} = $created_at_str;
266 }
267 10319 return $status;
268 }
269  
270 6516 ▶sub _extract_utc_timestamp_and_timezone {
Subroutine _extract_utc_timestamp_and_timezone: called CC=4 SCAR=13.9
271 6516 my ($timestamp_str) = @_;
272 6516 ▶ if(!defined($timestamp_str) || $timestamp_str eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $timestamp_str or $timestamp_str eq "") 2106 4410 6516
Condition66%2/3
not defined $timestamp_str or $timestamp_str eq ""
l!l&&r!l&&!r
210604410
MC/DC50%1/2
not defined $timestamp_str or $timestamp_str eq ""
not defined $timestamp_str $timestamp_str eq ""
Truth table66%2/3
not(A) or B
A= not defined $timestamp_strB= $timestamp_str eq ""
ABresult
1X1
011
000
273 2106 return ($UNDEF_TIMESTAMP, 'UTC');
274 }
275 4410 my $datetime = BusyBird::DateTime::Format->parse_datetime($timestamp_str);
276 4410 ▶ croak "Invalid datetime format: $timestamp_str" if not defined $datetime;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $datetime 12 4398 4410
277 4398 my $timezone_name = $datetime->time_zone->name;
278 4398 $datetime->set_time_zone('UTC');
279 4398 my $utc_timestamp = _format_datetime($datetime);
280 4398 return ($utc_timestamp, $timezone_name);
281 }
282  
283 20638 ▶sub _create_bb_timestamp_from_utc_timestamp_and_timezone {
Subroutine _create_bb_timestamp_from_utc_timestamp_and_timezone: called CC=2 SCAR=6.9
284 20638 my ($utc_timestamp_str, $timezone) = @_;
285 20638 ▶ if($utc_timestamp_str eq $UNDEF_TIMESTAMP) {
Branch: 1 branch
BranchTrueFalseTotal
if ($utc_timestamp_str eq $UNDEF_TIMESTAMP) 5017 15621 20638
286 5017 return undef;
287 }
288 15621 my $dt = _parse_datetime($utc_timestamp_str);
289 15621 $dt->set_time_zone($timezone);
290 15621 return BusyBird::DateTime::Format->format_datetime($dt);
291 }
292  
293 1142 ▶sub get_statuses {
Subroutine get_statuses: called CC=11 SCAR=24.2
Pod documented
294 1142 my ($self, %args) = @_;
295 1142 my $timeline = $args{timeline};
296 1142 ▶ croak "timeline parameter is mandatory" if not defined $timeline;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeline 2 1140 1142
297 1140 my $callback = $args{callback};
298 1140 ▶ croak "callback parameter is mandatory" if not defined $callback;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $callback 2 1138 1140
299 1138 ▶ croak "callback parameter must be a CODEREF" if ref($callback) ne "CODE";
Branch: 1 branch
BranchTrueFalseTotal
if ref $callback ne "CODE" 0 1138 1138
300 1138 ▶ my $ack_state = defined($args{ack_state}) ? $args{ack_state} : "any";
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'ack_state'} ? : 864 274 1138
301 1138 ▶ if($ack_state ne "any" && $ack_state ne "unacked" && $ack_state ne "acked") {
Branch: 1 branch
BranchTrueFalseTotal
if ($ack_state ne "any" and $ack_state ne "unacked" and $ack_state ne "acked") 0 1138 1138
Condition83%5/6
$ack_state ne "any" and $ack_state ne "unacked" and $ack_state ne "acked"
!ll&&!rl&&r
8103280
$ack_state ne "any" and $ack_state ne "unacked"
!ll&&!rl&&r
490320328
MC/DC0%0/3
$ack_state ne "any" and $ack_state ne "unacked" and $ack_state ne "acked"
$ack_state ne "any" $ack_state ne "unacked" $ack_state ne "acked"
Truth table75%3/4
A and B and C
A= $ack_state ne "any"B= $ack_state ne "unacked"C= $ack_state ne "acked"
ABCresult
0XX0
10X0
1100
1111
302 0 croak "ack_state parameter must be either 'any' or 'acked' or 'unacked'";
303 }
304 1138 my $max_id = $args{max_id};
305 1138 ▶ my $count = defined($args{count}) ? $args{count} : 'all';
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'count'} ? : 1136 2 1138
306 1138 ▶ if($count ne 'all' && !looks_like_number($count)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count ne "all" and not &looks_like_number($count)) 0 1138 1138
Condition66%2/3
$count ne "all" and not &looks_like_number($count)
!ll&&!rl&&r
8682700
MC/DC0%0/2
$count ne "all" and not &looks_like_number($count)
$count ne "all" not &looks_like_number($count)
Truth table66%2/3
A and not(B)
A= $count ne "all"B= not &looks_like_number($count)
ABresult
0X0
100
111
307 0 croak "count parameter must be either 'all' or number";
308 }
309 1138 ▶ my @results = try {
Subroutine __ANON__: called CC=6 SCAR=17.9
310 1138 my $dbh = $self->_get_my_dbh();
311 1138 my $timeline_id = $self->_get_timeline_id($dbh, $timeline);
312 1138 ▶ if(!defined($timeline_id)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $timeline_id) 86 1052 1138
313 86 return (undef, []);
314 }
315 1052 my $cond = $self->_create_base_condition($timeline_id, $ack_state);
316 1052 ▶ if(defined($max_id)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max_id) 193 859 1052
317 193 my $max_id_cond = $self->_create_max_id_condition($dbh, $timeline_id, $max_id, $ack_state);
318 193 ▶ if(!defined($max_id_cond)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $max_id_cond) 78 115 193
319 78 return (undef, []);
320 }
321 115 $cond = sql_and([$cond, $max_id_cond]);
322 }
323 974 my %maker_opt = (order_by => \@STATUSES_ORDER_BY);
324 974 ▶ if($count ne 'all') {
Branch: 1 branch
BranchTrueFalseTotal
if ($count ne "all") 204 770 974
325 204 $maker_opt{limit} = "$count";
326 }
327 974 my ($sql, @bind) = $self->{maker}->select("statuses", ['*'], $cond, \%maker_opt);
328 974 my $sth = $dbh->prepare($sql);
329 974 $sth->execute(@bind);
330 974 my @statuses = ();
331 974 ▶ while(my $record = $sth->fetchrow_hashref('NAME_lc')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $record = $sth->fetchrow_hashref("NAME_lc")) 10319 974 11293
332 10319 push(@statuses, _from_status_record($record));
333 }
334 974 return (undef, \@statuses);
335 0 ▶ }catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
336 0 my $e = shift;
337 0 return ($e);
338 1138 };
339 1138 @_ = @results;
340 1138 goto $callback;
341 }
342  
343 1856 ▶sub _create_base_condition {
Subroutine _create_base_condition: called CC=4 SCAR=13.9
344 1856 my ($self, $timeline_id, $ack_state) = @_;
345 1856 ▶ $ack_state ||= 'any';
Condition50%1/2
$ack_state ||= "any"
l!l
18560
MC/DC0%0/1
$ack_state ||= "any"
$ack_state
Truth table50%1/2
A
A= $ack_state
Aresult
11
00
346 1856 my $cond = sql_eq(timeline_id => $timeline_id);
347 1856 ▶ if($ack_state eq 'acked') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ack_state eq 'acked') { } 369 1487 1856
elsif ($ack_state eq 'unacked') { } 970 517 1487
348 369 $cond = sql_and([$cond, sql_ne(utc_acked_at => $UNDEF_TIMESTAMP)]);
349 }elsif($ack_state eq 'unacked') {
350 970 $cond = sql_and([$cond, sql_eq(utc_acked_at => $UNDEF_TIMESTAMP)]);
351 }
352 1856 return $cond;
353 }
354  
355 244 ▶sub _get_timestamps_of {
Subroutine _get_timestamps_of: called CC=2 SCAR=6.9
356 244 my ($self, $dbh, $timeline_id, $status_id, $ack_state) = @_;
357 244 my $cond = $self->_create_base_condition($timeline_id, $ack_state);
358 244 $cond = sql_and([$cond, sql_eq(status_id => $status_id)]);
359 244 my ($sql, @bind) = $self->{maker}->select("statuses", ['utc_acked_at', 'utc_created_at'], $cond, {
360 limit => 1
361 });
362 244 my $record = $dbh->selectrow_arrayref($sql, undef, @bind);
363 244 ▶ if(!$record) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($record) 96 148 244
364 96 return ();
365 }
366 148 return ($record->[0], $record->[1]);
367 }
368  
369 244 ▶sub _create_max_id_condition {
Subroutine _create_max_id_condition: called CC=3 SCAR=11.0
370 244 my ($self, $dbh, $timeline_id, $max_id, $ack_state) = @_;
371 244 my ($max_acked_at, $max_created_at) = $self->_get_timestamps_of($dbh, $timeline_id, $max_id, $ack_state);
372 244 ▶ if(!defined($max_acked_at) || !defined($max_created_at)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $max_acked_at and defined $max_created_at) 96 148 244
Condition66%2/3
defined $max_acked_at and defined $max_created_at
!ll&&!rl&&r
960148
MC/DC50%1/2
defined $max_acked_at and defined $max_created_at
defined $max_acked_at defined $max_created_at
Truth table66%2/3
A and B
A= defined $max_acked_atB= defined $max_created_at
ABresult
0X0
100
111
373 96 return undef;
374 }
375 148 return $self->_create_max_time_condition($max_acked_at, $max_created_at, $max_id);
376 }
377  
378 156 ▶sub _create_max_time_condition {
Subroutine _create_max_time_condition: called CC=1 SCAR=0.0
379 156 my ($self, $max_acked_at, $max_created_at, $max_id) = @_;
380 156 my $cond = sql_or([
381 sql_lt(utc_acked_at => $max_acked_at),
382 sql_and([
383 sql_eq(utc_acked_at => $max_acked_at),
384 sql_or([
385 sql_lt(utc_created_at => $max_created_at),
386 sql_and([
387 sql_eq(utc_created_at => $max_created_at),
388 sql_le(status_id => $max_id),
389 ])
390 ])
391 ])
392 ]);
393 156 return $cond;
394 }
395  
396 145 ▶sub ack_statuses {
Subroutine ack_statuses: called CC=11 SCAR=24.4
Pod documented
397 145 my ($self, %args) = @_;
398 145 my $timeline = $args{timeline};
399 145 ▶ croak "timeline parameter is mandatory" if not defined $timeline;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeline 2 143 145
400 143 ▶ my $callback = defined($args{callback}) ? $args{callback} : sub {};
Subroutine __ANON__: not called CC=1 SCAR=0.4
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'callback'} ? : 143 0 143
401 143 ▶ croak "callback parameter must be a CODEREF" if ref($callback) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $callback ne "CODE" 0 143 143
402 143 my $ids = $args{ids};
403 143 ▶ if(defined($ids) && ref($ids) && ref($ids) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ids and ref $ids and ref $ids ne "ARRAY") 0 143 143
Condition83%5/6
defined $ids and ref $ids and ref $ids ne "ARRAY"
!ll&&!rl&&r
89540
defined $ids and ref $ids
!ll&&!rl&&r
701954
MC/DC0%0/3
defined $ids and ref $ids and ref $ids ne "ARRAY"
defined $ids ref $ids ref $ids ne "ARRAY"
Truth table75%3/4
A and B and C
A= defined $idsB= ref $idsC= ref $ids ne "ARRAY"
ABCresult
0XX0
10X0
1100
1111
404 0 croak "ids parameter must be either undef, a status ID or an array-ref of status IDs";
405 }
406 143 ▶ if(defined($ids) && !ref($ids)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ids and not ref $ids) 19 124 143
Condition100%3/3
defined $ids and not ref $ids
!ll&&!rl&&r
705419
MC/DC100%2/2
defined $ids and not ref $ids
defined $ids not ref $ids
Truth table100%3/3
A and not(B)
A= defined $idsB= not ref $ids
ABresult
0X0
100
111
407 19 $ids = [$ids];
408 }
409 143 ▶ if(defined($ids) && grep { !defined($_) } @$ids) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ids and grep {not defined $_;} @$ids) 2 141 143
Condition100%3/3
defined $ids and grep {not defined $_;} @$ids
!ll&&!rl&&r
70712
MC/DC100%2/2
defined $ids and grep {not defined $_;} @$ids
defined $ids grep {not defined $_;} @$ids
Truth table100%3/3
A and B
A= defined $idsB= grep {not defined $_;} @$ids
ABresult
0X0
100
111
410 2 croak "ids parameter array must not contain undef.";
411 }
412 141 my $max_id = $args{max_id};
413 141 my $dbh;
414 141 ▶ my @results = try {
Subroutine __ANON__: called CC=9 SCAR=22.0
415 141 my $ack_utc_timestamp = _format_datetime(DateTime->now(time_zone => 'UTC'));
416 141 $dbh = $self->_get_my_dbh();
417 141 my $timeline_id = $self->_get_timeline_id($dbh, $timeline);
418 141 ▶ return (undef, 0) if not defined $timeline_id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeline_id 0 141 141
419 141 $dbh->begin_work();
420 141 my $total_count = 0;
421 141 ▶ if(!defined($ids) && !defined($max_id)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ids || defined $max_id) { } 36 105 141
Condition100%3/3
defined $ids || defined $max_id
l!l&&r!l&&!r
713436
MC/DC100%2/2
defined $ids || defined $max_id
defined $ids defined $max_id
Truth table100%3/3
A || B
A= defined $idsB= defined $max_id
ABresult
1X1
011
000
422 36 $total_count = $self->_ack_all($dbh, $timeline_id, $ack_utc_timestamp);
423 }else {
424 105 ▶ if(defined($max_id)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max_id) 51 54 105
425 51 my $max_id_count = $self->_ack_max_id($dbh, $timeline_id, $ack_utc_timestamp, $max_id);
426 51 ▶ $total_count += $max_id_count if $max_id_count > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $max_id_count > 0 33 18 51
427 }
428 105 ▶ if(defined($ids)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ids) 71 34 105
429 71 my $ids_count = $self->_ack_ids($dbh, $timeline_id, $ack_utc_timestamp, $ids);
430 71 ▶ $total_count += $ids_count if $ids_count > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ids_count > 0 42 29 71
431 }
432 }
433 141 $dbh->commit();
434 141 ▶ $total_count = 0 if $total_count < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $total_count < 0 0 141 141
435 141 return (undef, $total_count);
436 0 ▶ }catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
437 0 my $e = shift;
438 0 ▶ if($dbh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh) 0 0 0
439 0 $dbh->rollback();
440 }
441 0 return ($e);
442 141 };
443 141 @_ = @results;
444 141 goto $callback;
445 }
446  
447 36 ▶sub _ack_all {
Subroutine _ack_all: called CC=1 SCAR=0.0
448 36 my ($self, $dbh, $timeline_id, $ack_utc_timestamp) = @_;
449 my ($sql, @bind) = $self->{maker}->update(
450 36 'statuses', [utc_acked_at => $ack_utc_timestamp],
451 sql_and([sql_eq(timeline_id => $timeline_id), sql_eq(utc_acked_at => $UNDEF_TIMESTAMP)]),
452 );
453 36 return $dbh->do($sql, undef, @bind);
454 }
455  
456 51 ▶sub _ack_max_id {
Subroutine _ack_max_id: called CC=2 SCAR=6.9
457 51 my ($self, $dbh, $timeline_id, $ack_utc_timestamp, $max_id) = @_;
458 51 my $max_id_cond = $self->_create_max_id_condition($dbh, $timeline_id, $max_id, 'unacked');
459 51 ▶ if(!defined($max_id_cond)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $max_id_cond) 18 33 51
460 18 return 0;
461 }
462 33 my $cond = $self->_create_base_condition($timeline_id, 'unacked');
463 my ($sql, @bind) = $self->{maker}->update(
464 33 'statuses', [utc_acked_at => $ack_utc_timestamp], sql_and([$cond, $max_id_cond])
465 );
466 33 return $dbh->do($sql, undef, @bind);
467 }
468  
469 71 ▶sub _ack_ids {
Subroutine _ack_ids: called CC=5 SCAR=16.1
470 71 my ($self, $dbh, $timeline_id, $ack_utc_timestamp, $ids) = @_;
471 71 ▶ if(@$ids == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$ids == 0) 14 57 71
472 14 return 0;
473 }
474 57 my $total_count = 0;
475 57 my $sth;
476 57 foreach my $id (@$ids) {
477 184 my $cond = $self->_create_base_condition($timeline_id, 'unacked');
478 184 $cond = sql_and([$cond, sql_eq(status_id => $id)]);
479 my ($sql, @bind) = $self->{maker}->update(
480 184 'statuses', [utc_acked_at => $ack_utc_timestamp], $cond
481 );
482 184 ▶ if(!$sth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 57 127 184
483 57 $sth = $dbh->prepare($sql);
484 }
485 184 my $count = $sth->execute(@bind);
486 184 ▶ if($count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count > 0) 94 90 184
487 94 $total_count += $count;
488 }
489 }
490 57 return $total_count;
491 }
492  
493 151 ▶sub delete_statuses {
Subroutine delete_statuses: called CC=12 SCAR=25.5
Pod documented
494 151 my ($self, %args) = @_;
495 151 my $timeline = $args{timeline};
496 151 ▶ croak 'timeline parameter is mandatory' if not defined $timeline;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeline 2 149 151
497 149 ▶ croak 'ids parameter is mandatory' if not exists $args{ids};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"ids"} 2 147 149
498 147 my $ids = $args{ids};
499 147 ▶ if(defined($ids) && ref($ids) && ref($ids) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ids and ref $ids and ref $ids ne "ARRAY") 0 147 147
Condition83%5/6
defined $ids and ref $ids and ref $ids ne "ARRAY"
!ll&&!rl&&r
129180
defined $ids and ref $ids
!ll&&!rl&&r
1141518
MC/DC0%0/3
defined $ids and ref $ids and ref $ids ne "ARRAY"
defined $ids ref $ids ref $ids ne "ARRAY"
Truth table75%3/4
A and B and C
A= defined $idsB= ref $idsC= ref $ids ne "ARRAY"
ABCresult
0XX0
10X0
1100
1111
500 0 croak 'ids parameter must be either undef, a status ID or array-ref of status IDs.';
501 }
502 147 ▶ if(defined($ids) && !ref($ids)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ids and not ref $ids) 15 132 147
Condition100%3/3
defined $ids and not ref $ids
!ll&&!rl&&r
1141815
MC/DC100%2/2
defined $ids and not ref $ids
defined $ids not ref $ids
Truth table100%3/3
A and not(B)
A= defined $idsB= not ref $ids
ABresult
0X0
100
111
503 15 $ids = [$ids];
504 }
505 147 ▶ if(defined($ids) && grep { !defined($_) } @$ids) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ids and grep {not defined $_;} @$ids) 2 145 147
Condition100%3/3
defined $ids and grep {not defined $_;} @$ids
!ll&&!rl&&r
114312
MC/DC100%2/2
defined $ids and grep {not defined $_;} @$ids
defined $ids grep {not defined $_;} @$ids
Truth table100%3/3
A and B
A= defined $idsB= grep {not defined $_;} @$ids
ABresult
0X0
100
111
506 2 croak "ids parameter array must not contain undef.";
507 }
508 145 ▶ my $callback = defined($args{callback}) ? $args{callback} : sub {};
Subroutine __ANON__: not called CC=1 SCAR=0.4
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'callback'} ? : 145 0 145
509 145 ▶ croak 'callback parameter must be a CODEREF' if ref($callback) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $callback ne "CODE" 0 145 145
510 145 my $dbh;
511 145 ▶ my @results = try {
Subroutine __ANON__: called CC=4 SCAR=13.9
512 145 my $dbh = $self->_get_my_dbh();
513 145 my $timeline_id = $self->_get_timeline_id($dbh, $timeline);
514 145 ▶ if(!defined($timeline_id)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $timeline_id) 27 118 145
515 27 return (undef, 0);
516 }
517 118 $dbh->begin_work();
518 118 my $total_count;
519 118 ▶ if(defined($ids)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ids) { } 31 87 118
520 31 $total_count = $self->_delete_ids($dbh, $timeline_id, $ids);
521 }else {
522 87 $total_count = $self->_delete_timeline($dbh, $timeline_id);
523 }
524 118 ▶ $total_count = 0 if $total_count < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $total_count < 0 0 118 118
525 118 $dbh->commit();
526 118 $self->_add_to_delete_count($dbh, $total_count);
527 118 return (undef, $total_count);
528 0 ▶ }catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
529 0 my $e = shift;
530 0 ▶ if($dbh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh) 0 0 0
531 0 $dbh->rollback();
532 }
533 0 return ($e);
534 145 };
535 145 @_ = @results;
536 145 goto $callback;
537 }
538  
539 87 ▶sub _delete_timeline {
Subroutine _delete_timeline: called CC=1 SCAR=0.0
540 87 my ($self, $dbh, $timeline_id) = @_;
541 87 my ($sql, @bind) = $self->{maker}->delete('statuses', sql_eq(timeline_id => $timeline_id));
542 87 my $status_count = $dbh->do($sql, undef, @bind);
543 87 ($sql, @bind) = $self->{maker}->delete('timelines', sql_eq(timeline_id => $timeline_id));
544 87 $dbh->do($sql, undef, @bind);
545 87 return $status_count;
546 }
547  
548 31 ▶sub _delete_ids {
Subroutine _delete_ids: called CC=5 SCAR=16.1
549 31 my ($self, $dbh, $timeline_id, $ids) = @_;
550 31 ▶ return 0 if @$ids == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$ids == 0 0 31 31
551 31 my $sth;
552 31 my $total_count = 0;
553 31 foreach my $id (@$ids) {
554 84 my ($sql, @bind) = $self->{maker}->delete('statuses', sql_and([
555 sql_eq(timeline_id => $timeline_id), sql_eq(status_id => $id)
556 ]));
557 84 ▶ if(!$sth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 31 53 84
558 31 $sth = $dbh->prepare($sql);
559 }
560 84 my $count = $sth->execute(@bind);
561 84 ▶ if($count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count > 0) 70 14 84
562 70 $total_count += $count;
563 }
564 }
565 31 return $total_count;
566 }
567  
568 325 ▶sub _delete_exceeding_statuses {
Subroutine _delete_exceeding_statuses: called CC=4 SCAR=14.1
569 325 my ($self, $dbh, $timeline_id) = @_;
570 ## get total count in the timeline
571 325 my ($sql, @bind) = $self->{maker}->select('statuses', [\'count(*)'], sql_eq(timeline_id => $timeline_id));
572 325 my $row = $dbh->selectrow_arrayref($sql, undef, @bind);
573 325 ▶ if(!defined($row)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $row) 0 325 325
574 0 die "count query for timeline $timeline_id returns undef. something is wrong.";
575 }
576 325 my $total_count = $row->[0];
577
578 325 ▶ if($total_count <= $self->{hard_max_status_num}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total_count <= $$self{"hard_max_status_num"}) 317 8 325
579 317 return 0;
580 }
581  
582 ## get the top of the exceeding statuses
583 ($sql, @bind) = $self->{maker}->select('statuses', [qw(utc_acked_at utc_created_at status_id)], sql_eq(timeline_id => $timeline_id), {
584 order_by => \@STATUSES_ORDER_BY,
585 offset => $self->{max_status_num},
586 8 limit => 1,
587 });
588 8 $row = $dbh->selectrow_arrayref($sql, undef, @bind);
589 8 ▶ if(!defined($row)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $row) 0 8 8
590 0 die "selecting the top of exceeding status returns undef. something is wrong.";
591 }
592 8 my $time_cond = $self->_create_max_time_condition(@$row);
593  
594 ## execute deletion
595 8 my $timeline_cond = sql_eq(timeline_id => $timeline_id);
596 8 ($sql, @bind) = $self->{maker}->delete('statuses', sql_and([$timeline_cond, $time_cond]));
597 8 return $dbh->do($sql, undef, @bind);
598 }
599  
600 607 ▶sub get_unacked_counts {
Subroutine get_unacked_counts: called CC=4 SCAR=13.9
Pod documented
601 607 my ($self, %args) = @_;
602 607 my $timeline = $args{timeline};
603 607 ▶ croak 'timeline parameter is mandatory' if not defined $timeline;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeline 2 605 607
604 605 my $callback = $args{callback};
605 605 ▶ croak 'callback parameter is mandatory' if not defined $callback;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $callback 2 603 605
606 603 ▶ croak 'callback parameter must be a CODEREF' if ref($callback) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $callback ne "CODE" 0 603 603
607 603 ▶ my @results = try {
Subroutine __ANON__: called CC=3 SCAR=11.0
608 603 my $dbh = $self->_get_my_dbh();
609 603 my $timeline_id = $self->_get_timeline_id($dbh, $timeline);
610 603 my %result_obj = (total => 0);
611 603 ▶ if(!defined($timeline_id)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $timeline_id) 260 343 603
612 260 return (undef, \%result_obj);
613 }
614 343 my $cond = $self->_create_base_condition($timeline_id, 'unacked');
615 343 my ($sql, @bind) = $self->{maker}->select('statuses', ['level', \'count(status_id)'], $cond, {
616 group_by => 'level'
617 });
618 343 my $sth = $dbh->prepare($sql);
619 343 $sth->execute(@bind);
620 343 ▶ while(my $record = $sth->fetchrow_arrayref()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $record = $sth->fetchrow_arrayref) 395 343 738
621 395 $result_obj{total} += $record->[1];
622 395 $result_obj{$record->[0]} = $record->[1];
623 }
624 343 return (undef, \%result_obj);
625 0 ▶ }catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
626 0 my $e = shift;
627 0 return ($e);
628 603 };
629 603 @_ = @results;
630 603 goto $callback;
631 }
632  
633 126 ▶sub _add_to_delete_count {
Subroutine _add_to_delete_count: called CC=5 SCAR=16.3
634 126 my ($self, $dbh, $add_count) = @_;
635 126 ▶ return if $self->{vacuum_on_delete} <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $$self{"vacuum_on_delete"} <= 0 0 126 126
636 126 ▶ return if $add_count <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $add_count <= 0 4 122 126
637 my ($sql, @bind) = $self->{maker}->update(
638 122 'delete_counts',
639 [delete_count => sql_raw('delete_count + ?', $add_count)],
640 sql_eq(delete_count_id => $DELETE_COUNT_ID));
641 122 $dbh->do($sql, undef, @bind);
642
643 122 ($sql, @bind) = $self->{maker}->select('delete_counts', ["delete_count"], sql_eq(delete_count_id => $DELETE_COUNT_ID));
644 122 my $row = $dbh->selectrow_arrayref($sql, undef, @bind);
645 122 ▶ if(!defined($row)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $row) 0 122 122
646 0 die "no delete_counts row with delete_count_id = $DELETE_COUNT_ID. something is wrong.";
647 }
648 122 my $current_delete_count = $row->[0];
649  
650 122 ▶ if($current_delete_count >= $self->{vacuum_on_delete}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_delete_count >= $$self{"vacuum_on_delete"}) 17 105 122
651 17 $self->_do_vacuum($dbh);
652 }
653 }
654  
655 19 ▶sub _do_vacuum {
Subroutine _do_vacuum: called CC=1 SCAR=0.0
656 19 my ($self, $dbh) = @_;
657 19 my ($sql, @bind) = $self->{maker}->update('delete_counts', [delete_count => 0], sql_eq(delete_count_id => $DELETE_COUNT_ID));
658 19 $dbh->do($sql, undef, @bind);
659 19 $dbh->do('VACUUM');
660 }
661  
662 2 ▶sub vacuum {
Subroutine vacuum: called CC=1 SCAR=0.0
Pod documented
663 2 my ($self) = @_;
664 2 $self->_do_vacuum($self->_get_my_dbh());
665 }
666  
667 47 ▶sub contains {
Subroutine contains: called CC=8 SCAR=20.8
Pod documented
668 47 my ($self, %args) = @_;
669 47 my $timeline = $args{timeline};
670 47 ▶ croak 'timeline parameter is mandatory' if not defined $timeline;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeline 2 45 47
671 45 my $callback = $args{callback};
672 45 ▶ croak 'callback parameter is mandatory' if not defined $callback;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $callback 2 43 45
673 43 my $query = $args{query};
674 43 ▶ croak 'query parameter is mandatory' if not defined $query;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $query 2 41 43
675 41 my $ref_query = ref($query);
676 41 ▶ if(!$ref_query || $ref_query eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (not $ref_query or $ref_query eq 'HASH') { } 16 25 41
elsif ($ref_query eq 'ARRAY') { } 25 0 25
Condition100%3/3
not $ref_query or $ref_query eq "HASH"
l!l&&r!l&&!r
61025
MC/DC100%2/2
not $ref_query or $ref_query eq "HASH"
not $ref_query $ref_query eq "HASH"
Truth table100%3/3
not(A) or B
A= not $ref_queryB= $ref_query eq "HASH"
ABresult
1X1
011
000
677 16 $query = [$query];
678 }elsif($ref_query eq 'ARRAY') {
679 ;
680 }else {
681 0 croak 'query parameter must be either a status, status ID or array-ref';
682 }
683 41 ▶ if(grep { !defined($_) } @$query) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {not defined $_;} @$query) 2 39 41
684 2 croak "query element must be defined";
685 }
686 39 ▶ my @method_result = try {
Subroutine __ANON__: called CC=8 SCAR=20.8
687 39 my $dbh = $self->_get_my_dbh();
688 39 my $timeline_id = $self->_get_timeline_id($dbh, $timeline);
689 39 my @ret_contained = ();
690 39 my @ret_not_contained = ();
691 39 ▶ if(!defined($timeline_id)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $timeline_id) 4 35 39
692 4 @ret_not_contained = @$query;
693 4 return (undef, \@ret_contained, \@ret_not_contained);
694 }
695 35 my $sth;
696 35 foreach my $query_elem (@$query) {
697 132 ▶ my $status_id = (ref($query_elem) eq 'HASH') ? $query_elem->{id} : $query_elem;
Branch: 1 branch
BranchTrueFalseTotal
ref $query_elem eq 'HASH' ? : 56 76 132
698 132 ▶ if(!defined($status_id)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $status_id) 12 120 132
699 ## ID-less statuses are always 'not contained'.
700 12 push @ret_not_contained, $query_elem;
701 12 next;
702 }
703 my ($sql, @bind) = $self->{maker}->select(
704 120 'statuses', ['timeline_id', 'status_id'],
705 sql_and([sql_eq(timeline_id => $timeline_id), sql_eq(status_id => $status_id)])
706 );
707 120 ▶ if(!$sth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 28 92 120
708 28 $sth = $dbh->prepare($sql);
709 }
710 120 $sth->execute(@bind);
711 120 my $result = $sth->fetchall_arrayref();
712 120 ▶ if(!defined($result)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $result) 0 120 120
713 0 confess "Statement handle is inactive. Something is wrong.";
714 }
715 120 ▶ if(@$result) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$result) { } 67 53 120
716 67 push @ret_contained, $query_elem;
717 }else {
718 53 push @ret_not_contained, $query_elem;
719 }
720 }
721 35 return (undef, \@ret_contained, \@ret_not_contained);
722 0 ▶ }catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
723 0 my $e = shift;
724 0 return ($e);
725 39 };
726 39 @_ = @method_result;
727 39 goto $callback;
728 }
729  
730 10 ▶sub get_timeline_names {
Subroutine get_timeline_names: called CC=1 SCAR=0.0
Pod documented
731 10 my ($self) = @_;
732 10 my $dbh = $self->_get_my_dbh();
733 my ($sql, @bind) = $self->{maker}->select(
734 10 'timelines', ['name']
735 );
736 10 my $result = $dbh->selectall_arrayref($sql, undef, @bind);
737 10 my @return = map { $_->[0] } @$result;
738 10 return @return;
739 }
740  
741 12 1;
742  
743 __END__
« BusyBird/StatusStorage/Memory.pm ↑ Summary BusyBird/Runner.pm »