MongoDB.pm

filter: Statementstmt 28.7% 29 / 101 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 29.1% 7 / 24 Podpod 0.0% 0 / 8 totaltotal 21.4% 36 / 168 SCAR 47.9
CC
15
Cov
22%
store
20.0
BUILD
12.0
fetch
12.0
CRAP 120.5
↑ Summary MongoDB/t/CHIDriverTests.pm »
1 package CHI::Driver::MongoDB;
2 # vim:syntax=perl:tabstop=4:number:noexpandtab:
3 1 $CHI::Driver::MongoDB::VERSION = '0.0001';
4 # ABSTRACT: MongoDB driver for CHI
5  
6 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use MongoDB;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use URI::Escape::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Time::Moment;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 extends 'CHI::Driver';
16  
17 1 has 'mongodb' => (
18 is => 'ro',
19 lazy => 1,
20 init_arg => undef,
21 builder => '_build_mongodb',
22 );
23  
24 has 'mongodb_options' => (
25 is => 'rw',
26 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
27 1 );
28  
29 has 'connection_uri' => (
30 is => 'ro',
31 lazy => 1,
32 0 ▶ default => sub { 'mongodb://127.0.0.1:27017' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 1 );
34  
35 has 'db_name' => (
36 is => 'ro',
37 lazy => 1,
38 0 ▶ default => sub {'_CHI_'},
Subroutine __ANON__: not called CC=1 SCAR=6.9
39 1 );
40  
41 1 has '_coll' => (
42 is => 'rw',
43 lazy => 1,
44 predicate => 1,
45 init_arg => undef,
46 builder => '_build_coll',
47 clearer => 1,
48 );
49  
50  
51 sub BUILD {
52 0 ▶ my ( $self, $params ) = @_;
Subroutine BUILD: not called CC=3 SCAR=24.8
Pod undocumented
53 0 foreach my $param (qw/ mongodb_options connection_uri db_name /) {
54 0 ▶ if ( exists $params->{$param} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{$param}) 0 0 0
55 0 delete $params->{$param};
56 }
57 }
58 0 my $codec = MongoDB::BSON->new( dt_type => 'Time::Moment' );
59  
60 my %options = (
61 bson_codec => $codec,
62 0 %{ $self->mongodb_options() },
63 0 %{ $self->non_common_constructor_params($params) },
64 );
65 0 $self->mongodb_options( \%options );
66 }
67  
68  
69 sub _build_mongodb {
70 0 ▶ my $self = shift;
Subroutine _build_mongodb: not called CC=1 SCAR=6.9
71  
72 0 my %opts = %{ $self->mongodb_options() };
73 0 my $uri = $self->connection_uri;
74  
75 0 return MongoDB->connect( $uri, \%opts );
76 }
77  
78  
79 sub _build_coll {
80 0 ▶ my $self = shift;
Subroutine _build_coll: not called CC=2 SCAR=17.9
81  
82 0 my $ns = sprintf( "%s.%s", $self->db_name, encodeURIComponent( $self->namespace ) );
83  
84 0 my $coll = $self->mongodb->get_namespace($ns);
85  
86 0 ▶ if ( $self->expires_on_backend ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->expires_on_backend) 0 0 0
87 0 $coll->indexes->create_one( [ expireAt => 1 ], { expireAfterSeconds => 0 } );
88 }
89  
90 0 return $coll;
91 }
92  
93  
94 sub fetch {
95 0 ▶ my ( $self, $key ) = @_;
Subroutine fetch: not called CC=3 SCAR=24.8
Pod undocumented
96  
97 0 $key = encodeURIComponent($key);
98 0 my $doc = $self->_coll->find_id( $key, { payload => 1 } );
99  
100 0 ▶ return "" . $doc->{'payload'} if defined($doc) and ref($doc) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if defined $doc and ref $doc eq "HASH" 0 0 0
Condition0%0/3
defined $doc and ref $doc eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $doc and ref $doc eq "HASH"
defined $doc ref $doc eq "HASH"
Truth table0%0/3
A and B
A= defined $docB= ref $doc eq "HASH"
ABresult
0X0
100
111
101 0 return undef;
102 }
103  
104  
105 sub fetch_multi_hashref {
106 0 ▶ my ( $self, $keys ) = @_;
Subroutine fetch_multi_hashref: not called CC=2 SCAR=17.9
Pod undocumented
107  
108 0 my @esc = map { encodeURIComponent($_) } @{$keys};
109 0 my $qresult = $self->_coll->find(
110 { _id => { '$in' => \@esc } },
111 {
112 batchSize => 100,
113 projection => {
114 _id => 1,
115 payload => 1
116 }
117 }
118 )->result;
119  
120 0 my %ret = ();
121 0 ▶ while ( my @batch = $qresult->batch ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@batch) = $qresult->batch) 0 0 0
122 0 map { my $k = decodeURIComponent( $_->{'_id'} ); $ret{$k} = "" . $_->{'payload'} } @batch;
123 }
124  
125 0 return \%ret;
126 }
127  
128  
129 sub store {
130 0 ▶ my ( $self, $key, $data, $expires_in ) = @_;
Subroutine store: not called CC=4 SCAR=30.0
Pod undocumented
131  
132 0 $key = encodeURIComponent($key);
133 0 my $doc = {
134 _id => $key,
135 payload => MongoDB::BSON::Binary->new( data => $data ),
136 };
137 0 ▶ if ( defined $expires_in ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expires_in) 0 0 0
138 0 $doc->{'expireAt'} = Time::Moment->from_epoch( time() + $expires_in );
139 }
140 0 my $result = $self->_coll->update_one( { _id => $key }, { '$set' => $doc }, { upsert => 1 } );
141  
142 0 ▶ warn "update_one() operation failed: unexpected result"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $result eq "MongoDB::UnacknowledgedResult" or ref $result eq "MongoDB::UpdateResult" 0 0 0
Condition0%0/3
ref $result eq "MongoDB::UnacknowledgedResult" or ref $result eq "MongoDB::UpdateResult"
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $result eq "MongoDB::UnacknowledgedResult" or ref $result eq "MongoDB::UpdateResult"
ref $result eq "MongoDB::UnacknowledgedResult" ref $result eq "MongoDB::UpdateResult"
Truth table0%0/3
A or B
A= ref $result eq "MongoDB::UnacknowledgedResult"B= ref $result eq "MongoDB::UpdateResult"
ABresult
1X1
011
000
143 unless ref($result) eq 'MongoDB::UnacknowledgedResult'
144 or ref($result) eq 'MongoDB::UpdateResult';
145 try {
146 0 ▶ $result->assert if $result->acknowledged;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $result->acknowledged 0 0 0
147 }
148 catch {
149 0 ▶ warn "update_one() operation failed: deletion not asserted";
Subroutine __ANON__: not called CC=1 SCAR=6.9
150 0 };
151  
152 0 return undef;
153 } ## end sub store
154  
155 # TODO
156 #sub store_multi {
157 # my ( $self, $key_data, $options ) = @_;
158 #}
159  
160  
161 sub remove {
162 0 ▶ my ( $self, $key ) = @_;
Subroutine remove: not called CC=3 SCAR=24.8
Pod undocumented
163  
164 0 $key = encodeURIComponent($key);
165 0 my $result = $self->_coll->delete_one( { _id => $key } );
166  
167 0 ▶ warn "delete_one() operation failed: unexpected result"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $result eq "MongoDB::UnacknowledgedResult" or ref $result eq "MongoDB::DeleteResult" 0 0 0
Condition0%0/3
ref $result eq "MongoDB::UnacknowledgedResult" or ref $result eq "MongoDB::DeleteResult"
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $result eq "MongoDB::UnacknowledgedResult" or ref $result eq "MongoDB::DeleteResult"
ref $result eq "MongoDB::UnacknowledgedResult" ref $result eq "MongoDB::DeleteResult"
Truth table0%0/3
A or B
A= ref $result eq "MongoDB::UnacknowledgedResult"B= ref $result eq "MongoDB::DeleteResult"
ABresult
1X1
011
000
168 unless ref($result) eq 'MongoDB::UnacknowledgedResult'
169 or ref($result) eq 'MongoDB::DeleteResult';
170 try {
171 0 ▶ $result->assert if $result->acknowledged;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $result->acknowledged 0 0 0
172 }
173 catch {
174 0 ▶ warn "delete_one() operation failed: deletion not asserted";
Subroutine __ANON__: not called CC=1 SCAR=6.9
175 0 };
176  
177 0 return undef;
178 }
179  
180  
181 sub clear {
182 0 ▶ my ($self) = @_;
Subroutine clear: not called CC=1 SCAR=6.9
Pod undocumented
183  
184 0 my $coll = $self->_coll;
185 0 $self->_clear_coll;
186 0 $coll->drop;
187  
188 0 return undef;
189 }
190  
191  
192 sub get_keys {
193 0 ▶ my ($self) = @_;
Subroutine get_keys: not called CC=2 SCAR=17.9
Pod undocumented
194  
195 0 my @allKeys = ();
196 0 my $qresult = $self->_coll->find(
197 {},
198 {
199 batchSize => 100,
200 projection => { _id => 1 }
201 }
202 )->result;
203 0 ▶ while ( my @batch = $qresult->batch ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@batch) = $qresult->batch) 0 0 0
204 0 push @allKeys, map { decodeURIComponent( $_->{'_id'} ) } @batch;
205 }
206 0 return @allKeys;
207 }
208  
209  
210 sub get_namespaces {
211 0 ▶ my $self = shift;
Subroutine get_namespaces: not called CC=1 SCAR=6.9
Pod undocumented
212 0 return $self->mongodb->get_database( $self->db_name )->collection_names;
213 }
214  
215 1 1;
216  
217 __END__
↑ Summary MongoDB/t/CHIDriverTests.pm »