blib/lib/Data/SortedSet/Shared/Strings.pm

filter: Statementstmt 100% 100 / 100 Branchbran 80.9% 34 / 42 Conditioncond 71.4% 10 / 14 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 100% 32 / 32 Podpod 0.0% 0 / 24 totaltotal 81.4% 180 / 221 CC 28 SCAR 10.9
CC
28
Cov
92%
add_many
7.1
wrap
5.1
new
5.1
CRAP 3.0
↑ Summary blib/lib/Data/SortedSet/Shared.pm »
1 package Data::SortedSet::Shared::Strings;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Data::Intern::Shared ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Data::SortedSet::Shared ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 our $VERSION = '0.08';
9  
10 # ---- construction ----
11  
12 11 ▶sub new {
Subroutine new: called CC=5 SCAR=16.2
Pod undocumented
13 11 my ($class, %opt) = @_;
14 11 my $max = $opt{max};
15 11 ▶ Carp::croak("new: 'max' is required") unless defined $max;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max 1 10 11
16 10 my $set = Data::SortedSet::Shared->new($opt{set}, $max, $opt{mode});
17 # pass mode only when given: Data::Intern::Shared 0.01 predates the mode argument
18 my $keys = Data::Intern::Shared->new($opt{keys}, $opt{max_keys} // $max, $opt{arena} // 0,
19 10 ▶ defined $opt{mode} ? $opt{mode} : ());
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'mode'} ? : 0 10 10
Condition80%4/5
$opt{"max_keys"} // $max
l!l&&r!l&&!r
370
$opt{"arena"} // 0
l!l
19
MC/DC0%0/2
$opt{"max_keys"} // $max
$opt{"max_keys"} $max
MC/DC100%1/1
$opt{"arena"} // 0
$opt{"arena"}
Truth table66%2/3
A // B
A= $opt{"max_keys"}B= $max
ABresult
1X1
011
000
Truth table100%2/2
A
A= $opt{"arena"}
Aresult
11
00
20 10 return bless { set => $set, keys => $keys }, $class;
21 }
22  
23 # wrap two already-constructed shared objects (e.g. memfd-backed)
24 3 ▶sub wrap {
Subroutine wrap: called CC=5 SCAR=16.2
Pod undocumented
25 3 my ($class, $set, $keys) = @_;
26 3 ▶ Carp::croak("wrap: expected a Data::SortedSet::Shared")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $set and $set->isa("Data::SortedSet::Shared") 1 2 3
Condition66%2/3
ref $set and $set->isa("Data::SortedSet::Shared")
!ll&&!rl&&r
102
MC/DC50%1/2
ref $set and $set->isa("Data::SortedSet::Shared")
ref $set $set->isa("Data::SortedSet::Shared")
Truth table66%2/3
A and B
A= ref $setB= $set->isa("Data::SortedSet::Shared")
ABresult
0X0
100
111
27 unless ref $set && $set->isa('Data::SortedSet::Shared');
28 2 ▶ Carp::croak("wrap: expected a Data::Intern::Shared")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $keys and $keys->isa("Data::Intern::Shared") 1 1 2
Condition66%2/3
ref $keys and $keys->isa("Data::Intern::Shared")
!ll&&!rl&&r
101
MC/DC50%1/2
ref $keys and $keys->isa("Data::Intern::Shared")
ref $keys $keys->isa("Data::Intern::Shared")
Truth table66%2/3
A and B
A= ref $keysB= $keys->isa("Data::Intern::Shared")
ABresult
0X0
100
111
29 unless ref $keys && $keys->isa('Data::Intern::Shared');
30 1 return bless { set => $set, keys => $keys }, $class;
31 }
32  
33 1 ▶sub set { $_[0]{set} } # the underlying Data::SortedSet::Shared
Subroutine set: called CC=1 SCAR=0.0
Pod undocumented
34 3 ▶sub key_table { $_[0]{keys} } # the underlying Data::Intern::Shared
Subroutine key_table: called CC=1 SCAR=0.0
Pod undocumented
35  
36 # ---- mutators (intern the key -> id) ----
37  
38 20233 ▶sub add {
Subroutine add: called CC=3 SCAR=11.3
Pod undocumented
39 20233 my ($self, $str, $score) = @_;
40 20233 ▶ Carp::croak("add: NaN score") if $score != $score; # reject before interning (else a ghost key leaks)
Branch: 1 branch
BranchTrueFalseTotal
if $score != $score 0 20233 20233
41 20233 my $id = $self->{keys}->intern($str);
42 20233 ▶ return undef unless defined $id; # key table full
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 20233 20233
43 20233 return $self->{set}->add($id, $score);
44 }
45  
46 5974 ▶sub incr {
Subroutine incr: called CC=3 SCAR=11.0
Pod undocumented
47 5974 my ($self, $str, $delta) = @_;
48 5974 ▶ Carp::croak("incr: NaN delta") if $delta != $delta; # reject before interning (else a ghost key leaks)
Branch: 1 branch
BranchTrueFalseTotal
if $delta != $delta 0 5974 5974
49 5974 my $id = $self->{keys}->intern($str);
50 5974 ▶ Carp::croak("incr: key table full") unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 1 5973 5974
51 5973 return $self->{set}->incr($id, $delta);
52 }
53  
54 7762 ▶sub remove {
Subroutine remove: called CC=2 SCAR=6.9
Pod undocumented
55 7762 my ($self, $str) = @_;
56 7762 my $id = $self->{keys}->id_of($str);
57 7762 ▶ return defined $id ? $self->{set}->remove($id) : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $id ? : 7325 437 7762
58 }
59  
60 3 ▶sub add_many {
Subroutine add_many: called CC=7 SCAR=19.7
Pod undocumented
61 3 my ($self, $rows) = @_;
62 3 ▶ Carp::croak("add_many: expected an arrayref") unless ref $rows eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rows eq "ARRAY" 0 3 3
63 3 my @id_rows;
64 3 for my $r (@$rows) {
65 10 ▶ next unless ref $r eq 'ARRAY' && @$r >= 2;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $r eq "ARRAY" and @$r >= 2 1 9 10
Condition66%2/3
ref $r eq "ARRAY" and @$r >= 2
!ll&&!rl&&r
109
MC/DC50%1/2
ref $r eq "ARRAY" and @$r >= 2
ref $r eq "ARRAY" @$r >= 2
Truth table66%2/3
A and B
A= ref $r eq "ARRAY"B= @$r >= 2
ABresult
0X0
100
111
66 9 ▶ next if $r->[1] != $r->[1]; # skip a NaN score before interning (no ghost key slot)
Branch: 1 branch
BranchTrueFalseTotal
if $r->[1] != $r->[1] 1 8 9
67 8 my $id = $self->{keys}->intern($r->[0]);
68 8 ▶ last unless defined $id; # key table full -> stop
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 8 8
69 8 push @id_rows, [ $id, $r->[1] ];
70 }
71 3 return $self->{set}->add_many(\@id_rows);
72 }
73  
74 3 ▶sub clear { $_[0]{set}->clear; $_[0]{keys}->clear; return }
Subroutine clear: called CC=1 SCAR=0.0
Pod undocumented
75  
76 # ---- lookup (id_of the key; undef short-circuits) ----
77  
78 1766 ▶sub score { my $id = $_[0]{keys}->id_of($_[1]); defined $id ? $_[0]{set}->score($id) : undef }
Subroutine score: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
defined $id ? : 1765 1 1766
79 883 ▶sub rank { my $id = $_[0]{keys}->id_of($_[1]); defined $id ? $_[0]{set}->rank($id) : undef }
Subroutine rank: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
defined $id ? : 882 1 883
80 1 ▶sub rev_rank { my $id = $_[0]{keys}->id_of($_[1]); defined $id ? $_[0]{set}->rev_rank($id) : undef }
Subroutine rev_rank: called CC=2 SCAR=7.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
defined $id ? : 1 0 1
81 5 ▶sub exists { my $id = $_[0]{keys}->id_of($_[1]); defined $id ? $_[0]{set}->exists($id) : 0 }
Subroutine exists: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
defined $id ? : 2 3 5
82 12 ▶sub count { $_[0]{set}->count }
Subroutine count: called CC=1 SCAR=0.0
Pod undocumented
83 13 ▶sub count_in_score { shift->{set}->count_in_score(@_) }
Subroutine count_in_score: called CC=1 SCAR=0.0
Pod undocumented
84  
85 # ---- rank / range (decode ids back to strings) ----
86  
87 22 ▶sub _decode {
Subroutine _decode: called CC=3 SCAR=11.0
88 22 my ($self, $ws, @list) = @_;
89 22 my $k = $self->{keys};
90 22 ▶ return map { $k->string($_) } @list unless $ws;
Branch: 1 branch
BranchTrueFalseTotal
unless $ws 20 2 22
91 2 my @out;
92 2 ▶ for (my $i = 0; $i < @list; $i += 2) { push @out, $k->string($list[$i]), $list[$i + 1] }
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @list) 5 2 7
93 2 return @out;
94 }
95  
96 3 ▶sub at_rank {
Subroutine at_rank: called CC=2 SCAR=6.9
Pod undocumented
97 3 my $id = $_[0]{set}->at_rank($_[1]);
98 3 ▶ return defined $id ? $_[0]{keys}->string($id) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $id ? : 2 1 3
99 }
100  
101 5 ▶sub range_by_rank {
Subroutine range_by_rank: called CC=1 SCAR=0.0
Pod undocumented
102 5 my ($self, $start, $stop, %opt) = @_;
103 5 $self->_decode($opt{withscores}, $self->{set}->range_by_rank($start, $stop, %opt));
104 }
105 2 ▶sub rev_range_by_rank {
Subroutine rev_range_by_rank: called CC=1 SCAR=0.0
Pod undocumented
106 2 my ($self, $start, $stop, %opt) = @_;
107 2 $self->_decode($opt{withscores}, $self->{set}->rev_range_by_rank($start, $stop, %opt));
108 }
109 14 ▶sub range_by_score {
Subroutine range_by_score: called CC=1 SCAR=0.0
Pod undocumented
110 14 my ($self, $min, $max, %opt) = @_;
111 14 $self->_decode($opt{withscores}, $self->{set}->range_by_score($min, $max, %opt));
112 }
113 1 ▶sub rev_range_by_score {
Subroutine rev_range_by_score: called CC=1 SCAR=0.0
Pod undocumented
114 1 my ($self, $max, $min, %opt) = @_;
115 1 $self->_decode($opt{withscores}, $self->{set}->rev_range_by_score($max, $min, %opt));
116 }
117  
118 # ---- pop / peek (id -> string) ----
119  
120 3 for my $m (qw(pop_min pop_max peek_min peek_max)) {
121 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
122 6059 ▶ *$m = sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
123 6059 my ($id, $score) = $_[0]{set}->$m;
124 6059 ▶ return defined $id ? ($_[0]{keys}->string($id), $score) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $id ? : 6059 0 6059
125 12 };
126 }
127  
128 # ---- iteration ----
129  
130 5 ▶sub each {
Subroutine each: called CC=1 SCAR=0.0
Pod undocumented
131 5 my ($self, $cb) = @_;
132 5 $self->{set}->_each_strings($self->{keys}, $cb);
133 3 return;
134 }
135  
136 # ---- lifecycle ----
137  
138 1 ▶sub sync { $_[0]{set}->sync; $_[0]{keys}->sync; return }
Subroutine sync: called CC=1 SCAR=0.0
Pod undocumented
139 1 ▶sub unlink { $_[0]{set}->unlink; $_[0]{keys}->unlink; return }
Subroutine unlink: called CC=1 SCAR=0.0
Pod undocumented
140 1 ▶sub stats { { set => $_[0]{set}->stats, keys => $_[0]{keys}->stats } }
Subroutine stats: called CC=1 SCAR=0.0
Pod undocumented
141  
142 3 1;
143 __END__
↑ Summary blib/lib/Data/SortedSet/Shared.pm »