File Coverage

lib/App/MtAws/FileVersions.pm
Criterion Covered Total %
statement 45 45 100.0
branch 16 16 100.0
condition 3 3 100.0
subroutine 12 12 100.0
pod 0 5 0.0
total 76 81 93.8


line stmt bran cond sub pod time code
1             # mt-aws-glacier - Amazon Glacier sync client
2             # Copyright (C) 2012-2014 Victor Efimov
3             # http://mt-aws.com (also http://vs-dev.com) vs@vs-dev.com
4             # License: GPLv3
5             #
6             # This file is part of "mt-aws-glacier"
7             #
8             # mt-aws-glacier is free software: you can redistribute it and/or modify
9             # it under the terms of the GNU General Public License as published by
10             # the Free Software Foundation, either version 3 of the License, or
11             # (at your option) any later version.
12             #
13             # mt-aws-glacier is distributed in the hope that it will be useful,
14             # but WITHOUT ANY WARRANTY; without even the implied warranty of
15             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16             # GNU General Public License for more details.
17             #
18             # You should have received a copy of the GNU General Public License
19             # along with this program. If not, see <http://www.gnu.org/licenses/>.
20              
21             package App::MtAws::FileVersions;
22              
23             our $VERSION = '1.114_2';
24              
25 25     25   46232 use strict;
  25         1253  
  25         676  
26 25     25   101 use warnings;
  25         36  
  25         570  
27 25     25   103 use utf8;
  25         39  
  25         251  
28              
29             sub new
30             {
31 51     51 0 9068 my ($class) = @_;
32 51         77 my $self = [];
33 51         359 bless $self, $class;
34 51         98 return $self;
35             }
36              
37             sub add
38             {
39 245     245 0 1308 my ($self, $o) = @_;
40 245         298 my $after = $self->_find($o);
41 245 100       304 if (defined($after)) {
42 184         331 splice @$self, $after + 1, 0, $o;
43             } else {
44 61         149 unshift @$self, $o;
45             }
46             }
47              
48             sub _find
49             {
50 3830     3830   109098 my ($self, $o) = @_;
51 3830         3338 my ($start, $end) = (0, $#$self);
52 3830         5600 while ($end >= $start) {
53 10950         10021 my $mid = _mid($start, $end);
54 10950         11123 my $r = _cmp($o, $self->[$mid]);
55 10950 100       10853 if ($r >= 0) {
56 7337 100 100     12791 if ($mid == $end || _cmp($o, $self->[$mid+1]) < 0) {
57 3540         5402 return $mid;
58             }
59 3797         5797 $start = $mid + 1;
60             } else { # $r < 0
61 3613         4944 $end = $mid - 1;
62             }
63             }
64 290         437 return undef;
65             }
66              
67             sub _mid
68             {
69 25     25   19331 use integer;
  25         201  
  25         132  
70 10974     10974   11153 $_[0] + (($_[1] - $_[0])/2);
71             }
72              
73             sub all
74             {
75 25     25 0 10032 my ($self) = @_;
76 25         113 @$self;
77             }
78              
79             sub latest
80             {
81 9     9 0 15 my ($self) = @_;
82 9 100       54 $#$self == -1 ? undef : $self->[-1];
83             }
84              
85             # TODO: NOT USED (YET!)
86             sub delete_by_archive_id
87             {
88 37     37 0 20602 my ($self, $archive_id) = @_;
89 37         95 for (my $i = 0; $i <= $#$self; ++$i) { # O(n) search !
90 154 100       304 if ($self->[$i]{archive_id} eq $archive_id) {
91 19         29 splice @$self, $i, 1;
92 19         77 return 1;
93             }
94             }
95 18         49 return 0;
96             }
97              
98             # alternative 1:
99              
100             # if mtime defined for both a,b - compare mtime. otherwise compare time
101             # if mtime equal, compare time too
102              
103             # when $a->{mtime} <=> $b->{mtime} returns 0 (equal), we magicaly switch to 'time' comparsion
104             # when $a->{mtime} <=> $b->{mtime} returns 1 or -1, we use that
105             # ( defined($a->{mtime}) && defined($b->{mtime}) && ($a->{mtime} <=> $b->{mtime}) ) ||
106             # ( $a->{'time'} <=> $b->{'time'} );
107              
108             # alternative 2:
109             # possible alternative formula:
110             #(defined($a->{mtime}) ? $a->{mtime} : $a->{time}) <=> (defined($b->{mtime}) ? $b->{mtime} : $b->{time})
111              
112             sub _cmp
113             {
114 50414     50414   328492 my ($a, $b) = @_;
115             # use mtime (but if missed, use time) for both values
116             (
117             (defined($a->{mtime}) ? $a->{mtime} : $a->{time}) # mtime1 or time1
118             <=>
119             (defined($b->{mtime}) ? $b->{mtime} : $b->{time}) # mtime2 or time2
120             ) or # if copared values are equal (i.e. mtime1=mtime2 or in some cases mtime1=time2)
121             (
122 50414 100       176698 ( $a->{'time'} <=> $b->{'time'} ) # compare time
    100          
    100          
123             )
124             }
125              
126             1;