MongoDB/MongoClient.pm

filter: Statementstmt 70.5% 280 / 397 Branchbran 43.9% 58 / 132 Conditioncond 34.8% 15 / 43 MC/DCmcdc 30.7% 8 / 26 Subroutinesub 75.5% 74 / 98 Podpod 80.0% 12 / 15 totaltotal 62.8% 447 / 711 CC 86 SCAR 25.7
CC
86
Cov
62%
list_databases
72.0
watch
72.0
__ssl_from_uri
39.9
CRAP 13.1
« MongoDB/Op/_Count.pm ↑ Summary MongoDB/Role/_WriteResult.pm »
1 # Copyright 2012 - present MongoDB, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14  
15 59 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 59 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 package MongoDB::MongoClient;
18  
19 # ABSTRACT: A connection to a MongoDB server or multi-server deployment
20  
21 59 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 59 our $VERSION = 'v2.2.2';
23  
24 59 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 59 ▶use MongoDB::ClientSession;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 59 ▶use MongoDB::Cursor;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 59 ▶use MongoDB::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 59 ▶use MongoDB::Op::_Command;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 59 ▶use MongoDB::Op::_FSyncUnlock;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 59 ▶use MongoDB::ReadConcern;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 59 ▶use MongoDB::ReadPreference;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 59 ▶use MongoDB::WriteConcern;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 59 ▶use MongoDB::_Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 59 ▶use MongoDB::_Credential;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 59 ▶use MongoDB::_Dispatcher;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 59 ▶use MongoDB::_SessionPool;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 59 ▶use MongoDB::_Topology;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 59 ▶use MongoDB::_URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 59 ▶use BSON 1.012000;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 59 ▶use Digest::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 59 ▶use UUID::URandom;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 59 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 59 ▶use Time::HiRes qw/usleep/;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 59 ▶use Carp 'carp', 'croak', 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
45 59 ▶use Safe::Isa 1.000007;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 59 ▶use Scalar::Util qw/reftype weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 59 ▶use boolean;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 59 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 59 ▶use MongoDB::_Types qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
50 ArrayOfHashRef
51 AuthMechanism
52 Boolish
53 BSONCodec
54 CompressionType
55 Document
56 HeartbeatFreq
57 MaxStalenessNum
58 NonNegNum
59 ReadPrefMode
60 ReadPreference
61 ZlibCompressionLevel
62 59 );
63 59 ▶use Types::Standard qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
64 CodeRef
65 HashRef
66 ArrayRef
67 InstanceOf
68 Undef
69 Int
70 Num
71 Str
72 Maybe
73 59 );
74  
75 59 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
76  
77 #--------------------------------------------------------------------------#
78 # public attributes
79 #
80 # Of these, only host, port and bson_codec are set without regard for
81 # connection string options. The rest are built lazily in BUILD so that
82 # option precedence can be resolved.
83 #--------------------------------------------------------------------------#
84  
85 #pod =attr host
86 #pod
87 #pod The C<host> attribute specifies either a single server to connect to (as
88 #pod C<hostname> or C<hostname:port>), or else a L<connection string URI|/CONNECTION
89 #pod STRING URI> with a seed list of one or more servers plus connection options.
90 #pod
91 #pod B<NOTE>: Options specified in the connection string take precedence over options
92 #pod provided as constructor arguments.
93 #pod
94 #pod Defaults to the connection string URI C<mongodb://localhost:27017>.
95 #pod
96 #pod For IPv6 support, you must have a recent version of L<IO::Socket::IP>
97 #pod installed. This module ships with the Perl core since v5.20.0 and is
98 #pod available on CPAN for older Perls.
99 #pod
100 #pod =cut
101  
102 59 has host => (
103 is => 'ro',
104 isa => Str,
105 default => 'mongodb://localhost:27017',
106 );
107  
108 #pod =attr app_name
109 #pod
110 #pod This attribute specifies an application name that should be associated with
111 #pod this client. The application name will be communicated to the server as
112 #pod part of the initial connection handshake, and will appear in
113 #pod connection-level and operation-level diagnostics on the server generated on
114 #pod behalf of this client. This may be set in a connection string with the
115 #pod C<appName> option.
116 #pod
117 #pod The default is the empty string, which indicates a lack of an application
118 #pod name.
119 #pod
120 #pod The application name must not exceed 128 bytes.
121 #pod
122 #pod =cut
123  
124 59 has app_name => (
125 is => 'lazy',
126 isa => Str,
127 builder => '_build_app_name',
128 );
129  
130 189 ▶sub _build_app_name {
Subroutine _build_app_name: called CC=2 SCAR=6.9
131 189 my ($self) = @_;
132 189 my $app_name = $self->__uri_or_else(
133 u => 'appname',
134 e => 'app_name',
135 d => '',
136 );
137 189 ▶ unless ( length($app_name) <= 128 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $app_name <= 128) 1 188 189
138 1 MongoDB::UsageError->throw("app name must be at most 128 bytes");
139 }
140 188 return $app_name;
141 }
142  
143 #pod =attr auth_mechanism
144 #pod
145 #pod This attribute determines how the client authenticates with the server.
146 #pod Valid values are:
147 #pod
148 #pod =for :list
149 #pod * NONE
150 #pod * DEFAULT
151 #pod * MONGODB-CR
152 #pod * MONGODB-X509
153 #pod * GSSAPI
154 #pod * PLAIN
155 #pod * SCRAM-SHA-1
156 #pod
157 #pod If not specified, then if no username or C<authSource> URI option is provided,
158 #pod it defaults to NONE. Otherwise, it is set to DEFAULT, which chooses
159 #pod SCRAM-SHA-1 if available or MONGODB-CR otherwise.
160 #pod
161 #pod This may be set in a connection string with the C<authMechanism> option.
162 #pod
163 #pod =cut
164  
165 59 has auth_mechanism => (
166 is => 'lazy',
167 isa => AuthMechanism,
168 builder => '_build_auth_mechanism',
169 );
170  
171 188 ▶sub _build_auth_mechanism {
Subroutine _build_auth_mechanism: called CC=4 SCAR=13.9
172 188 my ($self) = @_;
173  
174 188 ▶ my $source = $self->_uri->options->{authsource} // "";
Condition100%2/2
$self->_uri->options->{"authsource"} // ""
l!l
11177
MC/DC100%1/1
$self->_uri->options->{"authsource"} // ""
$self->_uri->options->{"authsource"}
Truth table100%2/2
A
A= $self->_uri->options->{"authsource"}
Aresult
11
00
175 188 ▶ my $default = length( $self->username ) || length($source) ? 'DEFAULT' : 'NONE';
Branch: 1 branch
BranchTrueFalseTotal
length $self->username || length $source ? : 31 157 188
Condition100%3/3
length $self->username or length $source
l!l&&r!l&&!r
301157
MC/DC100%2/2
length $self->username or length $source
length $self->username length $source
Truth table100%3/3
A or B
A= length $self->usernameB= length $source
ABresult
1X1
011
000
176  
177 188 return $self->__uri_or_else(
178 u => 'authmechanism',
179 e => 'auth_mechanism',
180 d => $default,
181 );
182 }
183  
184 #pod =attr auth_mechanism_properties
185 #pod
186 #pod This is an optional hash reference of authentication mechanism specific properties.
187 #pod See L</AUTHENTICATION> for details.
188 #pod
189 #pod This may be set in a connection string with the C<authMechanismProperties>
190 #pod option. If given, the value must be key/value pairs joined with a ":".
191 #pod Multiple pairs must be separated by a comma. If ": or "," appear in a key or
192 #pod value, they must be URL encoded.
193 #pod
194 #pod =cut
195  
196 59 has auth_mechanism_properties => (
197 is => 'lazy',
198 isa => HashRef,
199 builder => '_build_auth_mechanism_properties',
200 );
201  
202 188 ▶sub _build_auth_mechanism_properties {
Subroutine _build_auth_mechanism_properties: called CC=1 SCAR=0.0
203 188 my ($self) = @_;
204 188 return $self->__uri_or_else(
205 u => 'authmechanismproperties',
206 e => 'auth_mechanism_properties',
207 d => {},
208 );
209 }
210  
211 #pod =attr bson_codec
212 #pod
213 #pod An object that provides the C<encode_one> and C<decode_one> methods, such as
214 #pod from L<BSON>. It may be initialized with a hash reference that will
215 #pod be coerced into a new L<BSON> object.
216 #pod
217 #pod If not provided, a L<BSON> object with default values will be generated.
218 #pod
219 #pod =cut
220  
221 59 has bson_codec => (
222 is => 'lazy',
223 isa => BSONCodec,
224 coerce => BSONCodec->coercion,
225 writer => '_set_bson_codec',
226 builder => '_build_bson_codec',
227 );
228  
229 181 ▶sub _build_bson_codec {
Subroutine _build_bson_codec: called CC=1 SCAR=0.0
230 181 my ($self) = @_;
231 181 return BSON->new();
232 }
233  
234 #pod =attr compressors
235 #pod
236 #pod An array reference of compression type names. Currently, C<zlib>, C<zstd> and
237 #pod C<snappy> are supported.
238 #pod
239 #pod =cut
240  
241 59 has compressors => (
242 is => 'lazy',
243 isa => ArrayRef[CompressionType],
244 builder => '_build_compressors',
245 );
246  
247 115 ▶sub _build_compressors {
Subroutine _build_compressors: called CC=1 SCAR=0.0
248 115 my ($self) = @_;
249 115 return $self->__uri_or_else(
250 u => 'compressors',
251 e => 'compressors',
252 d => [],
253 );
254 }
255  
256 #pod =attr zlib_compression_level
257 #pod
258 #pod An integer from C<-1> to C<9> specifying the compression level to use
259 #pod when L</compression> is set to C<zlib>.
260 #pod
261 #pod B<Note>: When the special value C<-1> is given, the default compression
262 #pod level will be used.
263 #pod
264 #pod =cut
265  
266 59 has zlib_compression_level => (
267 is => 'lazy',
268 isa => ZlibCompressionLevel,
269 builder => '_build_zlib_compression_level',
270 );
271  
272 174 ▶sub _build_zlib_compression_level {
Subroutine _build_zlib_compression_level: called CC=1 SCAR=0.0
273 174 my ($self) = @_;
274 174 return $self->__uri_or_else(
275 u => 'zlibcompressionlevel',
276 e => 'zlib_compression_level',
277 d => -1,
278 );
279 }
280  
281 #pod =attr connect_timeout_ms
282 #pod
283 #pod This attribute specifies the amount of time in milliseconds to wait for a
284 #pod new connection to a server.
285 #pod
286 #pod The default is 10,000 ms.
287 #pod
288 #pod If set to a negative value, connection operations will block indefinitely
289 #pod until the server replies or until the operating system TCP/IP stack gives
290 #pod up (e.g. if the name can't resolve or there is no process listening on the
291 #pod target host/port).
292 #pod
293 #pod A zero value polls the socket during connection and is thus likely to fail
294 #pod except when talking to a local process (and perhaps even then).
295 #pod
296 #pod This may be set in a connection string with the C<connectTimeoutMS> option.
297 #pod
298 #pod =cut
299  
300 59 has connect_timeout_ms => (
301 is => 'lazy',
302 isa => Num,
303 builder => '_build_connect_timeout_ms',
304 );
305  
306 188 ▶sub _build_connect_timeout_ms {
Subroutine _build_connect_timeout_ms: called CC=1 SCAR=0.0
307 188 my ($self) = @_;
308 188 return $self->__uri_or_else(
309 u => 'connecttimeoutms',
310 e => 'connect_timeout_ms',
311 d => 10000,
312 );
313 }
314  
315 #pod =attr db_name
316 #pod
317 #pod Optional. If an L</auth_mechanism> requires a database for authentication,
318 #pod this attribute will be used. Otherwise, it will be ignored. Defaults to
319 #pod "admin".
320 #pod
321 #pod This may be provided in the L<connection string URI|/CONNECTION STRING URI> as
322 #pod a path between the authority and option parameter sections. For example, to
323 #pod authenticate against the "admin" database (showing a configuration option only
324 #pod for illustration):
325 #pod
326 #pod mongodb://localhost/admin?readPreference=primary
327 #pod
328 #pod =cut
329  
330 59 has db_name => (
331 is => 'lazy',
332 isa => Str,
333 builder => '_build_db_name',
334 );
335  
336 188 ▶sub _build_db_name {
Subroutine _build_db_name: called CC=1 SCAR=0.0
337 188 my ($self) = @_;
338 188 return __string( $self->_uri->db_name, $self->_deferred->{db_name} );
339 }
340  
341 #pod =attr heartbeat_frequency_ms
342 #pod
343 #pod The time in milliseconds (non-negative) between scans of all servers to
344 #pod check if they are up and update their latency. Defaults to 60,000 ms.
345 #pod
346 #pod This may be set in a connection string with the C<heartbeatFrequencyMS> option.
347 #pod
348 #pod =cut
349  
350 59 has heartbeat_frequency_ms => (
351 is => 'lazy',
352 isa => HeartbeatFreq,
353 builder => '_build_heartbeat_frequency_ms',
354 );
355  
356 188 ▶sub _build_heartbeat_frequency_ms {
Subroutine _build_heartbeat_frequency_ms: called CC=1 SCAR=0.0
357 188 my ($self) = @_;
358 188 return $self->__uri_or_else(
359 u => 'heartbeatfrequencyms',
360 e => 'heartbeat_frequency_ms',
361 d => 60000,
362 );
363 }
364  
365 #pod =attr j
366 #pod
367 #pod If true, the client will block until write operations have been committed to the
368 #pod server's journal. Prior to MongoDB 2.6, this option was ignored if the server was
369 #pod running without journaling. Starting with MongoDB 2.6, write operations will fail
370 #pod if this option is used when the server is running without journaling.
371 #pod
372 #pod This may be set in a connection string with the C<journal> option as the
373 #pod strings 'true' or 'false'.
374 #pod
375 #pod =cut
376  
377 59 has j => (
378 is => 'lazy',
379 isa => Boolish,
380 builder => '_build_j',
381 );
382  
383 187 ▶sub _build_j {
Subroutine _build_j: called CC=1 SCAR=0.0
384 187 my ($self) = @_;
385 187 return $self->__uri_or_else(
386 u => 'journal',
387 e => 'j',
388 d => undef,
389 );
390 }
391  
392 #pod =attr local_threshold_ms
393 #pod
394 #pod The width of the 'latency window': when choosing between multiple suitable
395 #pod servers for an operation, the acceptable delta in milliseconds
396 #pod (non-negative) between shortest and longest average round-trip times.
397 #pod Servers within the latency window are selected randomly.
398 #pod
399 #pod Set this to "0" to always select the server with the shortest average round
400 #pod trip time. Set this to a very high value to always randomly choose any known
401 #pod server.
402 #pod
403 #pod Defaults to 15 ms.
404 #pod
405 #pod See L</SERVER SELECTION> for more details.
406 #pod
407 #pod This may be set in a connection string with the C<localThresholdMS> option.
408 #pod
409 #pod =cut
410  
411 59 has local_threshold_ms => (
412 is => 'lazy',
413 isa => NonNegNum,
414 builder => '_build_local_threshold_ms',
415 );
416  
417 187 ▶sub _build_local_threshold_ms {
Subroutine _build_local_threshold_ms: called CC=1 SCAR=0.0
418 187 my ($self) = @_;
419 187 return $self->__uri_or_else(
420 u => 'localthresholdms',
421 e => 'local_threshold_ms',
422 d => 15,
423 );
424 }
425  
426 #pod =attr max_staleness_seconds
427 #pod
428 #pod The C<max_staleness_seconds> parameter represents the maximum replication lag in
429 #pod seconds (wall clock time) that a secondary can suffer and still be
430 #pod eligible for reads. The default is -1, which disables staleness checks.
431 #pod Otherwise, it must be a positive integer.
432 #pod
433 #pod B<Note>: this will only be used for server versions 3.4 or greater, as that
434 #pod was when support for staleness tracking was added.
435 #pod
436 #pod If the read preference mode is 'primary', then C<max_staleness_seconds> must not
437 #pod be supplied.
438 #pod
439 #pod The C<max_staleness_seconds> must be at least the C<heartbeat_frequency_ms>
440 #pod plus 10 seconds (which is how often the server makes idle writes to the
441 #pod oplog).
442 #pod
443 #pod This may be set in a connection string with the C<maxStalenessSeconds> option.
444 #pod
445 #pod =cut
446  
447 59 has max_staleness_seconds => (
448 is => 'lazy',
449 isa => MaxStalenessNum,
450 builder => '_build_max_staleness_seconds',
451 );
452  
453 187 ▶sub _build_max_staleness_seconds {
Subroutine _build_max_staleness_seconds: called CC=1 SCAR=0.0
454 187 my ($self) = @_;
455 187 return $self->__uri_or_else(
456 u => 'maxstalenessseconds',
457 e => 'max_staleness_seconds',
458 d => -1,
459 );
460 }
461  
462 #pod =attr max_time_ms
463 #pod
464 #pod Specifies the maximum amount of time in (non-negative) milliseconds that the
465 #pod server should use for working on a database command. Defaults to 0, which disables
466 #pod this feature. Make sure this value is shorter than C<socket_timeout_ms>.
467 #pod
468 #pod B<Note>: this will only be used for server versions 2.6 or greater, as that
469 #pod was when the C<$maxTimeMS> meta-operator was introduced.
470 #pod
471 #pod You are B<strongly> encouraged to set this variable if you know your
472 #pod environment has MongoDB 2.6 or later, as getting a definitive error response
473 #pod from the server is vastly preferred over a getting a network socket timeout.
474 #pod
475 #pod This may be set in a connection string with the C<maxTimeMS> option.
476 #pod
477 #pod =cut
478  
479 59 has max_time_ms => (
480 is => 'lazy',
481 isa => NonNegNum,
482 builder => '_build_max_time_ms',
483 );
484  
485 187 ▶sub _build_max_time_ms {
Subroutine _build_max_time_ms: called CC=1 SCAR=0.0
486 187 my ($self) = @_;
487 187 return $self->__uri_or_else(
488 u => 'maxtimems',
489 e => 'max_time_ms',
490 d => 0,
491 );
492 }
493  
494 #pod =attr monitoring_callback
495 #pod
496 #pod Specifies a code reference used to receive monitoring events. See
497 #pod L<MongoDB::Monitoring> for more details.
498 #pod
499 #pod =cut
500  
501 59 has monitoring_callback => (
502 is => 'ro',
503 isa => Maybe [CodeRef],
504 );
505  
506 #pod =attr password
507 #pod
508 #pod If an L</auth_mechanism> requires a password, this attribute will be
509 #pod used. Otherwise, it will be ignored.
510 #pod
511 #pod This may be provided in the L<connection string URI|/CONNECTION STRING URI> as
512 #pod a C<username:password> pair in the leading portion of the authority section
513 #pod before a C<@> character. For example, to authenticate as user "mulder" with
514 #pod password "trustno1":
515 #pod
516 #pod mongodb://mulder:trustno1@localhost
517 #pod
518 #pod If the username or password have a ":" or "@" in it, they must be URL encoded.
519 #pod An empty password still requires a ":" character.
520 #pod
521 #pod =cut
522  
523 59 has password => (
524 is => 'lazy',
525 isa => Str,
526 builder => '_build_password',
527 );
528  
529 187 ▶sub _build_password {
Subroutine _build_password: called CC=3 SCAR=11.0
530 187 my ($self) = @_;
531 return
532 defined( $self->_uri->password ) ? $self->_uri->password
533 : defined( $self->_deferred->{password} ) ? $self->_deferred->{password}
534 187 ▶ : '';
Branch: 2 branches
BranchTrueFalseTotal
defined $self->_uri->password ? : 19 168 187
defined $self->_deferred->{'password'} ? : 3 165 168
535 }
536  
537 #pod =attr port
538 #pod
539 #pod If a network port is not specified as part of the C<host> attribute, this
540 #pod attribute provides the port to use. It defaults to 27107.
541 #pod
542 #pod =cut
543  
544 59 has port => (
545 is => 'ro',
546 isa => Int,
547 default => 27017,
548 );
549  
550 #pod =attr read_concern_level
551 #pod
552 #pod The read concern level determines the consistency level required
553 #pod of data being read.
554 #pod
555 #pod The default level is C<undef>, which means the server will use its configured
556 #pod default.
557 #pod
558 #pod If the level is set to "local", reads will return the latest data a server has
559 #pod locally.
560 #pod
561 #pod Additional levels are storage engine specific. See L<Read
562 #pod Concern|http://docs.mongodb.org/manual/search/?query=readConcern> in the MongoDB
563 #pod documentation for more details.
564 #pod
565 #pod This may be set in a connection string with the the C<readConcernLevel> option.
566 #pod
567 #pod =cut
568  
569 59 has read_concern_level => (
570 is => 'lazy',
571 isa => Maybe [Str],
572 builder => '_build_read_concern_level',
573 );
574  
575 186 ▶sub _build_read_concern_level {
Subroutine _build_read_concern_level: called CC=1 SCAR=0.0
576 186 my ($self) = @_;
577 186 return $self->__uri_or_else(
578 u => 'readconcernlevel',
579 e => 'read_concern_level',
580 d => undef,
581 );
582 }
583  
584 #pod =attr read_pref_mode
585 #pod
586 #pod The read preference mode determines which server types are candidates
587 #pod for a read operation. Valid values are:
588 #pod
589 #pod =for :list
590 #pod * primary
591 #pod * primaryPreferred
592 #pod * secondary
593 #pod * secondaryPreferred
594 #pod * nearest
595 #pod
596 #pod For core documentation on read preference see
597 #pod L<http://docs.mongodb.org/manual/core/read-preference/>.
598 #pod
599 #pod This may be set in a connection string with the C<readPreference> option.
600 #pod
601 #pod =cut
602  
603 59 has read_pref_mode => (
604 is => 'lazy',
605 isa => ReadPrefMode,
606 coerce => ReadPrefMode->coercion,
607 builder => '_build_read_pref_mode',
608 );
609  
610 187 ▶sub _build_read_pref_mode {
Subroutine _build_read_pref_mode: called CC=1 SCAR=0.0
611 187 my ($self) = @_;
612 187 return $self->__uri_or_else(
613 u => 'readpreference',
614 e => 'read_pref_mode',
615 d => 'primary',
616 );
617 }
618  
619 #pod =attr read_pref_tag_sets
620 #pod
621 #pod The C<read_pref_tag_sets> parameter is an ordered list of tag sets used to
622 #pod restrict the eligibility of servers, such as for data center awareness. It
623 #pod must be an array reference of hash references.
624 #pod
625 #pod The application of C<read_pref_tag_sets> varies depending on the
626 #pod C<read_pref_mode> parameter. If the C<read_pref_mode> is 'primary', then
627 #pod C<read_pref_tag_sets> must not be supplied.
628 #pod
629 #pod For core documentation on read preference see
630 #pod L<http://docs.mongodb.org/manual/core/read-preference/>.
631 #pod
632 #pod This may be set in a connection string with the C<readPreferenceTags> option.
633 #pod If given, the value must be key/value pairs joined with a ":". Multiple pairs
634 #pod must be separated by a comma. If ": or "," appear in a key or value, they must
635 #pod be URL encoded. The C<readPreferenceTags> option may appear more than once, in
636 #pod which case each document will be added to the tag set list.
637 #pod
638 #pod =cut
639  
640 59 has read_pref_tag_sets => (
641 is => 'lazy',
642 isa => ArrayOfHashRef,
643 coerce => ArrayOfHashRef->coercion,
644 builder => '_build_read_pref_tag_sets',
645 );
646  
647 187 ▶sub _build_read_pref_tag_sets {
Subroutine _build_read_pref_tag_sets: called CC=1 SCAR=0.0
648 187 my ($self) = @_;
649 187 return $self->__uri_or_else(
650 u => 'readpreferencetags',
651 e => 'read_pref_tag_sets',
652 d => [ {} ],
653 );
654 }
655  
656 #pod =attr replica_set_name
657 #pod
658 #pod Specifies the replica set name to connect to. If this string is non-empty,
659 #pod then the topology is treated as a replica set and all server replica set
660 #pod names must match this or they will be removed from the topology.
661 #pod
662 #pod This may be set in a connection string with the C<replicaSet> option.
663 #pod
664 #pod =cut
665  
666 59 has replica_set_name => (
667 is => 'lazy',
668 isa => Str,
669 builder => '_build_replica_set_name',
670 );
671  
672 187 ▶sub _build_replica_set_name {
Subroutine _build_replica_set_name: called CC=1 SCAR=0.0
673 187 my ($self) = @_;
674 187 return $self->__uri_or_else(
675 u => 'replicaset',
676 e => 'replica_set_name',
677 d => '',
678 );
679 }
680  
681 #pod =attr retry_reads
682 #pod
683 #pod =cut
684  
685 59 has retry_reads => (
686 is => 'lazy',
687 isa => Boolish,
688 builder => '_build_retry_reads',
689 );
690  
691 187 ▶sub _build_retry_reads {
Subroutine _build_retry_reads: called CC=1 SCAR=0.0
692 187 my ( $self ) = @_;
693 187 return $self->__uri_or_else(
694 u => 'retryreads',
695 e => 'retry_reads',
696 d => 1,
697 );
698 }
699  
700 #pod =attr retry_writes
701 #pod
702 #pod Whether the client should use retryable writes for supported commands. The
703 #pod default value is true, which means that commands which support retryable writes
704 #pod will be retried on certain errors, such as C<not master> and C<node is
705 #pod recovering> errors.
706 #pod
707 #pod This may be set in a connection string with the C<retryWrites> option.
708 #pod
709 #pod Note that this is only supported on MongoDB > 3.6 in Replica Set or Shard
710 #pod Clusters, and will be ignored on other deployments.
711 #pod
712 #pod Unacknowledged write operations also do not support retryable writes, even when
713 #pod retry_writes has been enabled.
714 #pod
715 #pod The supported single statement write operations are currently as follows:
716 #pod
717 #pod =for :list
718 #pod * C<insert_one>
719 #pod * C<update_one>
720 #pod * C<replace_one>
721 #pod * C<delete_one>
722 #pod * C<find_one_and_delete>
723 #pod * C<find_one_and_replace>
724 #pod * C<find_one_and_update>
725 #pod
726 #pod The supported multi statement write operations are as follows:
727 #pod
728 #pod =for :list
729 #pod * C<insert_many>
730 #pod * C<bulk_write>
731 #pod
732 #pod The multi statement operations may be ether ordered or unordered. Note that for
733 #pod C<bulk_write> operations, the request may not include update_many or
734 #pod delete_many operations.
735 #pod
736 #pod =cut
737  
738 59 has retry_writes => (
739 is => 'lazy',
740 isa => Boolish,
741 builder => '_build_retry_writes',
742 );
743  
744 187 ▶sub _build_retry_writes {
Subroutine _build_retry_writes: called CC=1 SCAR=0.0
745 187 my ( $self ) = @_;
746 187 return $self->__uri_or_else(
747 u => 'retrywrites',
748 e => 'retry_writes',
749 d => 1,
750 );
751 }
752  
753 #pod =attr server_selection_timeout_ms
754 #pod
755 #pod This attribute specifies the amount of time in milliseconds to wait for a
756 #pod suitable server to be available for a read or write operation. If no
757 #pod server is available within this time period, an exception will be thrown.
758 #pod
759 #pod The default is 30,000 ms.
760 #pod
761 #pod See L</SERVER SELECTION> for more details.
762 #pod
763 #pod This may be set in a connection string with the C<serverSelectionTimeoutMS>
764 #pod option.
765 #pod
766 #pod =cut
767  
768 59 has server_selection_timeout_ms => (
769 is => 'lazy',
770 isa => Num,
771 builder => '_build_server_selection_timeout_ms',
772 );
773  
774 187 ▶sub _build_server_selection_timeout_ms {
Subroutine _build_server_selection_timeout_ms: called CC=1 SCAR=0.0
775 187 my ($self) = @_;
776 187 return $self->__uri_or_else(
777 u => 'serverselectiontimeoutms',
778 e => 'server_selection_timeout_ms',
779 d => 30000,
780 );
781 }
782  
783 #pod =attr server_selection_try_once
784 #pod
785 #pod This attribute controls whether the client will make only a single attempt
786 #pod to find a suitable server for a read or write operation. The default is true.
787 #pod
788 #pod When true, the client will B<not> use the C<server_selection_timeout_ms>.
789 #pod Instead, if the topology information is stale and needs to be checked or
790 #pod if no suitable server is available, the client will make a single
791 #pod scan of all known servers to try to find a suitable one.
792 #pod
793 #pod When false, the client will continually scan known servers until a suitable
794 #pod server is found or the C<serverSelectionTimeoutMS> is reached.
795 #pod
796 #pod See L</SERVER SELECTION> for more details.
797 #pod
798 #pod This may be set in a connection string with the C<serverSelectionTryOnce>
799 #pod option.
800 #pod
801 #pod =cut
802  
803 59 has server_selection_try_once => (
804 is => 'lazy',
805 isa => Boolish,
806 builder => '_build_server_selection_try_once',
807 );
808  
809 187 ▶sub _build_server_selection_try_once {
Subroutine _build_server_selection_try_once: called CC=1 SCAR=0.0
810 187 my ($self) = @_;
811 187 return $self->__uri_or_else(
812 u => 'serverselectiontryonce',
813 e => 'server_selection_try_once',
814 d => 1,
815 );
816 }
817  
818 #pod =attr server_selector
819 #pod
820 #pod Optional. This takes a function that augments the server selection rules.
821 #pod The function takes as a parameter a list of server descriptions representing
822 #pod the suitable servers for the read or write operation, and returns a list of
823 #pod server descriptions that should still be considered suitable. Most users
824 #pod should rely on the default server selection algorithm and should not need
825 #pod to set this attribute.
826 #pod
827 #pod =cut
828  
829 59 has server_selector => (
830 is => 'ro',
831 isa => Maybe[CodeRef],
832 );
833  
834 #pod =attr socket_check_interval_ms
835 #pod
836 #pod If a socket to a server has not been used in this many milliseconds, an
837 #pod C<ismaster> command will be issued to check the status of the server before
838 #pod issuing any reads or writes. Must be non-negative.
839 #pod
840 #pod The default is 5,000 ms.
841 #pod
842 #pod This may be set in a connection string with the C<socketCheckIntervalMS>
843 #pod option.
844 #pod
845 #pod =cut
846  
847 59 has socket_check_interval_ms => (
848 is => 'lazy',
849 isa => NonNegNum,
850 builder => '_build_socket_check_interval_ms',
851 );
852  
853 187 ▶sub _build_socket_check_interval_ms {
Subroutine _build_socket_check_interval_ms: called CC=1 SCAR=0.0
854 187 my ($self) = @_;
855 187 return $self->__uri_or_else(
856 u => 'socketcheckintervalms',
857 e => 'socket_check_interval_ms',
858 d => 5000,
859 );
860 }
861  
862 #pod =attr socket_timeout_ms
863 #pod
864 #pod This attribute specifies the amount of time in milliseconds to wait for a
865 #pod reply from the server before issuing a network exception.
866 #pod
867 #pod The default is 30,000 ms.
868 #pod
869 #pod If set to a negative value, socket operations will block indefinitely
870 #pod until the server replies or until the operating system TCP/IP stack
871 #pod gives up.
872 #pod
873 #pod The driver automatically sets the TCP keepalive option when initializing the
874 #pod socket. For keepalive related issues, check the MongoDB documentation for
875 #pod L<Does TCP keepalive time affect MongoDB Deployments?|https://docs.mongodb.com/v3.2/faq/diagnostics/#does-tcp-keepalive-time-affect-mongodb-deployments>.
876 #pod
877 #pod A zero value polls the socket for available data and is thus likely to fail
878 #pod except when talking to a local process (and perhaps even then).
879 #pod
880 #pod This may be set in a connection string with the C<socketTimeoutMS> option.
881 #pod
882 #pod =cut
883  
884 59 has socket_timeout_ms => (
885 is => 'lazy',
886 isa => Num,
887 builder => '_build_socket_timeout_ms',
888 );
889  
890 187 ▶sub _build_socket_timeout_ms {
Subroutine _build_socket_timeout_ms: called CC=1 SCAR=0.0
891 187 my ($self) = @_;
892 187 return $self->__uri_or_else(
893 u => 'sockettimeoutms',
894 e => 'socket_timeout_ms',
895 d => 30000,
896 );
897 }
898  
899 #pod =attr ssl
900 #pod
901 #pod ssl => 1
902 #pod ssl => \%ssl_options
903 #pod
904 #pod This tells the driver that you are connecting to an SSL mongodb instance.
905 #pod
906 #pod You must have L<IO::Socket::SSL> 1.42+ and L<Net::SSLeay> 1.49+ installed for
907 #pod SSL support.
908 #pod
909 #pod The C<ssl> attribute takes either a boolean value or a hash reference of
910 #pod options to pass to IO::Socket::SSL. For example, to set a CA file to validate
911 #pod the server certificate and set a client certificate for the server to validate,
912 #pod you could set the attribute like this:
913 #pod
914 #pod ssl => {
915 #pod SSL_ca_file => "/path/to/ca.pem",
916 #pod SSL_cert_file => "/path/to/client.pem",
917 #pod }
918 #pod
919 #pod If C<SSL_ca_file> is not provided, server certificates are verified against a
920 #pod default list of CAs, either L<Mozilla::CA> or an operating-system-specific
921 #pod default CA file. To disable verification, you can use
922 #pod C<< SSL_verify_mode => 0x00 >>.
923 #pod
924 #pod B<You are strongly encouraged to use your own CA file for increased security>.
925 #pod
926 #pod Server hostnames are also validated against the CN name in the server
927 #pod certificate using C<< SSL_verifycn_scheme => 'http' >>. You can use the
928 #pod scheme 'none' to disable this check.
929 #pod
930 #pod B<Disabling certificate or hostname verification is a security risk and is not
931 #pod recommended>.
932 #pod
933 #pod This may be set to the string 'true' or 'false' in a connection string with the
934 #pod C<ssl> option, which will enable ssl with default configuration. (See
935 #pod L<connection string URI|/CONNECTION STRING URI> for additional TLS
936 #pod configuration options.)
937 #pod
938 #pod =cut
939  
940 59 has ssl => (
941 is => 'lazy',
942 isa => Boolish|HashRef,
943 builder => '_build_ssl',
944 );
945  
946 187 ▶sub _build_ssl {
Subroutine _build_ssl: called CC=10 SCAR=26.9
947 187 my ($self) = @_;
948  
949 # options will be undef if not provided
950 187 my $uri_ssl = $self->__ssl_from_uri();
951 187 ▶ my $opt_ssl = exists $self->_deferred->{ssl} ? $self->_deferred->{ssl} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->_deferred->{'ssl'} ? : 62 125 187
952  
953 # no SSL options exist
954 187 ▶ if ( !defined $uri_ssl && !defined $opt_ssl ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $uri_ssl or defined $opt_ssl) 184 3 187
Condition100%3/3
defined $uri_ssl or defined $opt_ssl
l!l&&r!l&&!r
12184
MC/DC100%2/2
defined $uri_ssl or defined $opt_ssl
defined $uri_ssl defined $opt_ssl
Truth table100%3/3
A or B
A= defined $uri_sslB= defined $opt_ssl
ABresult
1X1
011
000
955 184 return 0;
956 }
957  
958 # validate deferred ssl arg type
959 3 ▶ if ( ref $opt_ssl && ref $opt_ssl ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opt_ssl and ref $opt_ssl ne "HASH") 0 3 3
Condition66%2/3
ref $opt_ssl and ref $opt_ssl ne "HASH"
!ll&&!rl&&r
210
MC/DC0%0/2
ref $opt_ssl and ref $opt_ssl ne "HASH"
ref $opt_ssl ref $opt_ssl ne "HASH"
Truth table66%2/3
A and B
A= ref $opt_sslB= ref $opt_ssl ne "HASH"
ABresult
0X0
100
111
960 0 MongoDB::UsageError->throw("ssl attribute must be scalar or hashref")
961 }
962  
963 # no URI SSL defined means use opts SSL
964 3 ▶ if ( !defined $uri_ssl ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $uri_ssl) 2 1 3
965 2 return $opt_ssl;
966 }
967  
968 # if URI SSL is false, that takes precedence
969 1 ▶ if ( ! $uri_ssl ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($uri_ssl) 1 0 1
970 1 return $uri_ssl;
971 }
972  
973 # if opt SSL isn't a hashref, it's irrelevant
974 0 ▶ if ( ref $opt_ssl ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opt_ssl ne "HASH") 0 0 0
975 0 return $uri_ssl;
976 }
977  
978 # if uri SSL isn't a hashref, we prefer opt SSL hashref
979 0 ▶ if ( ref $uri_ssl ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $uri_ssl ne "HASH") 0 0 0
980 0 return $opt_ssl;
981 }
982  
983 # both are hashes, so merge them with URI taking precedence
984 0 return { %$opt_ssl, %$uri_ssl };
985 }
986  
987 59 my @tls_options = qw(
988 tlsallowinvalidcertificates
989 tlsallowinvalidhostnames
990 tlscafile
991 tlscertificatekeyfile
992 tlscertificatekeyfilepassword
993 tlsinsecure
994 );
995  
996 187 ▶sub __ssl_from_uri {
Subroutine __ssl_from_uri: called CC=9 SCAR=36.9
997 187 my ($self) = @_;
998 187 my $uri_options = $self->_uri->options;
999 187 my $saw_tls_boolean = exists $uri_options->{tls};
1000 187 my $saw_tls_options = grep { length } map { exists $uri_options->{$_} } @tls_options;
1001  
1002 187 ▶ if (!$saw_tls_options) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($saw_tls_options) 187 0 187
1003 187 ▶ return $saw_tls_boolean ? $uri_options->{tls} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$saw_tls_boolean ? : 1 186 187
1004 }
1005  
1006 0 my $ssl = {};
1007 0 ▶ if (exists($uri_options->{tlscafile})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $uri_options->{"tlscafile"}) 0 0 0
1008 0 $ssl->{SSL_ca_file} = $uri_options->{tlscafile};
1009 }
1010 0 ▶ if (exists($uri_options->{tlscertificatekeyfile})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $uri_options->{"tlscertificatekeyfile"}) 0 0 0
1011 0 $ssl->{SSL_cert_file} = $uri_options->{tlscertificatekeyfile};
1012 }
1013 0 ▶ if (exists($uri_options->{tlscertificatekeyfilepassword})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $uri_options->{"tlscertificatekeyfilepassword"}) 0 0 0
1014 0 ▶ $ssl->{SSL_passwd_cb} = sub { $uri_options->{tlscertificatekeyfilepassword} };
Subroutine __ANON__: not called CC=1 SCAR=6.9
1015 }
1016 0 ▶ if (exists($uri_options->{tlsallowinvalidhostnames})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $uri_options->{"tlsallowinvalidhostnames"}) 0 0 0
1017 0 $ssl->{SSL_verifycn_scheme} = 'none';
1018 }
1019 0 ▶ if (exists($uri_options->{tlsallowinvalidcertificates})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $uri_options->{"tlsallowinvalidcertificates"}) 0 0 0
1020 0 $ssl->{SSL_verify_mode} = 0x00;
1021 }
1022 0 ▶ if (exists($uri_options->{tlsinsecure})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $uri_options->{"tlsinsecure"}) 0 0 0
1023 0 $ssl->{SSL_verify_mode} = 0x00;
1024 0 $ssl->{SSL_verifycn_scheme} = 'none';
1025 }
1026 0 return $ssl;
1027 }
1028  
1029 #pod =attr username
1030 #pod
1031 #pod Optional username for this client connection. If this field is set, the client
1032 #pod will attempt to authenticate when connecting to servers. Depending on the
1033 #pod L</auth_mechanism>, the L</password> field or other attributes will need to be
1034 #pod set for authentication to succeed.
1035 #pod
1036 #pod This may be provided in the L<connection string URI|/CONNECTION STRING URI> as
1037 #pod a C<username:password> pair in the leading portion of the authority section
1038 #pod before a C<@> character. For example, to authenticate as user "mulder" with
1039 #pod password "trustno1":
1040 #pod
1041 #pod mongodb://mulder:trustno1@localhost
1042 #pod
1043 #pod If the username or password have a ":" or "@" in it, they must be URL encoded.
1044 #pod An empty password still requires a ":" character.
1045 #pod
1046 #pod =cut
1047  
1048 59 has username => (
1049 is => 'lazy',
1050 isa => Str,
1051 builder => '_build_username',
1052 );
1053  
1054 188 ▶sub _build_username {
Subroutine _build_username: called CC=3 SCAR=11.0
1055 188 my ($self) = @_;
1056  
1057 return
1058 defined( $self->_uri->username ) ? $self->_uri->username
1059 : defined( $self->_deferred->{username} ) ? $self->_deferred->{username}
1060 188 ▶ : '';
Branch: 2 branches
BranchTrueFalseTotal
defined $self->_uri->username ? : 28 160 188
defined $self->_deferred->{'username'} ? : 3 157 160
1061 }
1062  
1063 #pod =attr w
1064 #pod
1065 #pod The client I<write concern>.
1066 #pod
1067 #pod =over 4
1068 #pod
1069 #pod =item * C<0> Unacknowledged. MongoClient will B<NOT> wait for an acknowledgment that
1070 #pod the server has received and processed the request. Older documentation may refer
1071 #pod to this as "fire-and-forget" mode. This option is not recommended.
1072 #pod
1073 #pod =item * C<1> Acknowledged. MongoClient will wait until the
1074 #pod primary MongoDB acknowledges the write.
1075 #pod
1076 #pod =item * C<2> Replica acknowledged. MongoClient will wait until at least two
1077 #pod replicas (primary and one secondary) acknowledge the write. You can set a higher
1078 #pod number for more replicas.
1079 #pod
1080 #pod =item * C<all> All replicas acknowledged.
1081 #pod
1082 #pod =item * C<majority> A majority of replicas acknowledged.
1083 #pod
1084 #pod =back
1085 #pod
1086 #pod If not set, the server default is used, which is typically "1".
1087 #pod
1088 #pod In MongoDB v2.0+, you can "tag" replica members. With "tagging" you can
1089 #pod specify a custom write concern For more information see L<Data Center
1090 #pod Awareness|http://docs.mongodb.org/manual/data-center-awareness/>
1091 #pod
1092 #pod This may be set in a connection string with the C<w> option.
1093 #pod
1094 #pod =cut
1095  
1096 59 has w => (
1097 is => 'lazy',
1098 isa => Int|Str|Undef,
1099 builder => '_build_w',
1100 );
1101  
1102 187 ▶sub _build_w {
Subroutine _build_w: called CC=1 SCAR=0.0
1103 187 my ($self) = @_;
1104 187 return $self->__uri_or_else(
1105 u => 'w',
1106 e => 'w',
1107 d => undef,
1108 );
1109 }
1110  
1111 #pod =attr wtimeout
1112 #pod
1113 #pod The number of milliseconds an operation should wait for C<w> secondaries to
1114 #pod replicate it.
1115 #pod
1116 #pod Defaults to 1000 (1 second). If you set this to undef, it could block indefinitely
1117 #pod (or until socket timeout is reached).
1118 #pod
1119 #pod See C<w> above for more information.
1120 #pod
1121 #pod This may be set in a connection string with the C<wTimeoutMS> option.
1122 #pod
1123 #pod =cut
1124  
1125 59 has wtimeout => (
1126 is => 'lazy',
1127 isa => Maybe[Int],
1128 builder => '_build_wtimeout',
1129 );
1130  
1131 186 ▶sub _build_wtimeout {
Subroutine _build_wtimeout: called CC=1 SCAR=0.0
1132 186 my ($self) = @_;
1133 186 return $self->__uri_or_else(
1134 u => 'wtimeoutms',
1135 e => 'wtimeout',
1136 d => 1000,
1137 );
1138 }
1139  
1140 #--------------------------------------------------------------------------#
1141 # computed attributes - these are private and can't be set in the
1142 # constructor, but have a public accessor
1143 #--------------------------------------------------------------------------#
1144  
1145 #pod =method read_preference
1146 #pod
1147 #pod Returns a L<MongoDB::ReadPreference> object constructed from
1148 #pod L</read_pref_mode> and L</read_pref_tag_sets>
1149 #pod
1150 #pod B<The use of C<read_preference> as a mutator has been removed.> Read
1151 #pod preference is read-only. If you need a different read preference for
1152 #pod a database or collection, you can specify that in C<get_database> or
1153 #pod C<get_collection>.
1154 #pod
1155 #pod =cut
1156  
1157 59 has _read_preference => (
1158 is => 'lazy',
1159 isa => ReadPreference,
1160 reader => 'read_preference',
1161 init_arg => undef,
1162 builder => '_build__read_preference',
1163 );
1164  
1165 186 ▶sub _build__read_preference {
Subroutine _build__read_preference: called CC=4 SCAR=15.8
1166 186 my ($self) = @_;
1167 186 ▶ return MongoDB::ReadPreference->new(
Branch: 3 branches
BranchTrueFalseTotal
defined $self->read_pref_mode ? : 186 0 186
defined $self->read_pref_tag_sets ? : 186 0 186
defined $self->max_staleness_seconds ? : 186 0 186
1168 ( defined $self->read_pref_mode ? ( mode => $self->read_pref_mode ) : () ),
1169 ( defined $self->read_pref_tag_sets ? ( tag_sets => $self->read_pref_tag_sets ) : () ),
1170 ( defined $self->max_staleness_seconds ? ( max_staleness_seconds => $self->max_staleness_seconds ) : () ),
1171 );
1172 }
1173  
1174 #pod =method write_concern
1175 #pod
1176 #pod Returns a L<MongoDB::WriteConcern> object constructed from L</w>, L</write_concern>
1177 #pod and L</j>.
1178 #pod
1179 #pod =cut
1180  
1181 59 has _write_concern => (
1182 is => 'lazy',
1183 isa => InstanceOf['MongoDB::WriteConcern'],
1184 reader => 'write_concern',
1185 init_arg => undef,
1186 builder => '_build__write_concern',
1187 );
1188  
1189 184 ▶sub _build__write_concern {
Subroutine _build__write_concern: called CC=1 SCAR=0.0
1190 184 my ($self) = @_;
1191  
1192 184 return MongoDB::WriteConcern->new( $self->_write_concern_options );
1193 }
1194  
1195 # Seperated out for use in transaction option defaults
1196 184 ▶sub _write_concern_options {
Subroutine _write_concern_options: called CC=3 SCAR=11.0
1197 184 my ($self) = @_;
1198  
1199 return (
1200 184 ▶ wtimeout => $self->wtimeout,
Branch: 2 branches
BranchTrueFalseTotal
defined $self->w ? : 10 174 184
defined $self->j ? : 7 177 184
1201 # Must check for defined as w can be 0, and defaults to undef
1202 ( defined $self->w ? ( w => $self->w ) : () ),
1203 ( defined $self->j ? ( j => $self->j ) : () ),
1204 );
1205 }
1206  
1207  
1208 #pod =method read_concern
1209 #pod
1210 #pod Returns a L<MongoDB::ReadConcern> object constructed from
1211 #pod L</read_concern_level>.
1212 #pod
1213 #pod =cut
1214  
1215 59 has _read_concern => (
1216 is => 'lazy',
1217 isa => InstanceOf['MongoDB::ReadConcern'],
1218 reader => 'read_concern',
1219 init_arg => undef,
1220 builder => '_build__read_concern',
1221 );
1222  
1223 5 ▶sub _build__read_concern {
Subroutine _build__read_concern: called CC=2 SCAR=6.9
1224 5 my ($self) = @_;
1225  
1226 5 ▶ return MongoDB::ReadConcern->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->read_concern_level ? : 4 1 5
1227 ( $self->read_concern_level ?
1228 ( level => $self->read_concern_level ) : () ),
1229 );
1230 }
1231  
1232 #--------------------------------------------------------------------------#
1233 # private attributes
1234 #--------------------------------------------------------------------------#
1235  
1236 # used for a more accurate 'is this client the same one' for sessions, instead
1237 # of memory location which just feels... yucky
1238 has _id => (
1239 is => 'ro',
1240 init_arg => undef,
1241 189 ▶ default => sub { UUID::URandom::create_uuid_string() },
Subroutine __ANON__: called CC=1 SCAR=0.0
1242 59 );
1243  
1244 # collects constructor options and defer them so precedence can be resolved
1245 # against the _uri options; unlike other private args, this needs a valid
1246 # init argument
1247 has _deferred => (
1248 is => 'ro',
1249 isa => HashRef,
1250 init_arg => '_deferred',
1251 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1252 59 );
1253  
1254 #pod =method topology_type
1255 #pod
1256 #pod Returns an enumerated topology type. If the L</replica_set_name> is set,
1257 #pod the value will be either 'ReplicaSetWithPrimary' or 'ReplicaSetNoPrimary'
1258 #pod (if the primary is down or not yet discovered). Without
1259 #pod L</replica_set_name>, if there is more than one server in the list of
1260 #pod hosts, the type will be 'Sharded'.
1261 #pod
1262 #pod With only a single host and no replica set name, the topology type will
1263 #pod start as 'Direct' until the server is contacted the first time, after which
1264 #pod the type will be 'Sharded' for a mongos or 'Single' for standalone server
1265 #pod or direct connection to a replica set member.
1266 #pod
1267 #pod =cut
1268  
1269 59 has _topology => (
1270 is => 'lazy',
1271 isa => InstanceOf ['MongoDB::_Topology'],
1272 init_arg => undef,
1273 builder => '_build__topology',
1274 handles => {
1275 topology_type => 'type',
1276 _cluster_time => 'cluster_time',
1277 _update_cluster_time => 'update_cluster_time',
1278 },
1279 clearer => '_clear__topology',
1280 );
1281  
1282 183 ▶sub _build__topology {
Subroutine _build__topology: called CC=6 SCAR=18.5
1283 183 my ($self) = @_;
1284  
1285 my $type =
1286 length( $self->replica_set_name ) ? 'ReplicaSetNoPrimary'
1287 183 ▶ : @{ $self->_uri->hostids } > 1 ? 'Sharded'
Branch: 2 branches
BranchTrueFalseTotal
length $self->replica_set_name ? : 2 181 183
@{$self->_uri->hostids;} > 1 ? : 0 181 181
1288 : 'Direct';
1289  
1290 183 ▶ MongoDB::_Topology->new(
Branch: 3 branches
BranchTrueFalseTotal
$self->connect_timeout_ms >= 0 ? : 174 0 174
$self->socket_timeout_ms >= 0 ? : 174 0 174
ref $self->ssl eq 'HASH' ? : 1 173 174
1291 uri => $self->_uri,
1292 type => $type,
1293 app_name => $self->app_name,
1294 replica_set_name => $self->replica_set_name,
1295 server_selection_timeout_sec => $self->server_selection_timeout_ms / 1000,
1296 server_selection_try_once => $self->server_selection_try_once,
1297 local_threshold_sec => $self->local_threshold_ms / 1000,
1298 heartbeat_frequency_sec => $self->heartbeat_frequency_ms / 1000,
1299 min_server_version => MIN_SERVER_VERSION,
1300 max_wire_version => MAX_WIRE_VERSION,
1301 min_wire_version => MIN_WIRE_VERSION,
1302 credential => $self->_credential,
1303 link_options => {
1304 connect_timeout => $self->connect_timeout_ms >= 0 ? $self->connect_timeout_ms / 1000
1305 : undef,
1306 socket_timeout => $self->socket_timeout_ms >= 0 ? $self->socket_timeout_ms / 1000
1307 : undef,
1308 with_ssl => !!$self->ssl,
1309 ( ref( $self->ssl ) eq 'HASH' ? ( SSL_options => $self->ssl ) : () ),
1310 },
1311 monitoring_callback => $self->monitoring_callback,
1312 compressors => $self->compressors,
1313 zlib_compression_level => $self->zlib_compression_level,
1314 socket_check_interval_sec => $self->socket_check_interval_ms / 1000,
1315 server_selector => $self->server_selector,
1316 );
1317 }
1318  
1319 59 has _credential => (
1320 is => 'lazy',
1321 isa => InstanceOf ['MongoDB::_Credential'],
1322 init_arg => undef,
1323 builder => '_build__credential',
1324 );
1325  
1326 183 ▶sub _build__credential {
Subroutine _build__credential: called CC=5 SCAR=16.1
1327 183 my ($self) = @_;
1328 183 my $mechanism = $self->auth_mechanism;
1329 183 my $uri_options = $self->_uri->options;
1330 183 my $source = $uri_options->{authsource};
1331 183 ▶ my $cred = MongoDB::_Credential->new(
Branch: 4 branches
BranchTrueFalseTotal
$self->username ? : 30 153 183
$self->password ? : 21 162 183
$source ? : 11 172 183
$self->db_name ? : 16 167 183
1332 monitoring_callback => $self->monitoring_callback,
1333 mechanism => $mechanism,
1334 mechanism_properties => $self->auth_mechanism_properties,
1335 ( $self->username ? ( username => $self->username ) : () ),
1336 ( $self->password ? ( password => $self->password ) : () ),
1337 ( $source ? ( source => $source ) : () ),
1338 ( $self->db_name ? ( db_name => $self->db_name ) : () ),
1339 );
1340 174 return $cred;
1341 }
1342  
1343 59 has _uri => (
1344 is => 'lazy',
1345 isa => InstanceOf ['MongoDB::_URI'],
1346 init_arg => undef,
1347 builder => '_build__uri',
1348 );
1349  
1350 189 ▶sub _build__uri {
Subroutine _build__uri: called CC=3 SCAR=11.0
1351 189 my ($self) = @_;
1352 189 ▶ if ( $self->host =~ m{^[\w\+]+://} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->host =~ m[^[\w\+]+://]) { } 132 57 189
1353 132 return MongoDB::_URI->new( uri => $self->host );
1354 }
1355 else {
1356 my $uri = $self->host =~ /:\d+$/
1357 ? $self->host
1358 57 ▶ : sprintf("%s:%s", map { $self->$_ } qw/host port/ );
Branch: 1 branch
BranchTrueFalseTotal
$self->host =~ /:\d+$/ ? : 1 56 57
1359 57 return MongoDB::_URI->new( uri => ("mongodb://$uri") );
1360 }
1361 }
1362  
1363 59 has _dispatcher => (
1364 is => 'lazy',
1365 isa => InstanceOf ['MongoDB::_Dispatcher'],
1366 init_arg => undef,
1367 builder => '_build__dispatcher',
1368 handles => [
1369 qw(
1370 send_direct_op
1371 send_primary_op
1372 send_retryable_read_op
1373 send_read_op
1374 send_retryable_write_op
1375 send_write_op
1376 )
1377 ],
1378 );
1379  
1380 0 ▶sub _build__dispatcher {
Subroutine _build__dispatcher: not called CC=1 SCAR=6.9
1381 0 my $self = shift;
1382 0 return MongoDB::_Dispatcher->new(
1383 topology => $self->_topology,
1384 retry_writes => $self->retry_writes,
1385 retry_reads => $self->retry_reads,
1386 );
1387 }
1388  
1389 59 has _server_session_pool => (
1390 is => 'lazy',
1391 isa => InstanceOf['MongoDB::_SessionPool'],
1392 init_arg => undef,
1393 builder => '_build__server_session_pool',
1394 );
1395  
1396 0 ▶sub _build__server_session_pool {
Subroutine _build__server_session_pool: not called CC=1 SCAR=6.9
1397 0 my $self = shift;
1398 0 return MongoDB::_SessionPool->new(
1399 dispatcher => $self->_dispatcher,
1400 topology => $self->_topology,
1401 );
1402 }
1403  
1404 #--------------------------------------------------------------------------#
1405 # Constructor customization
1406 #--------------------------------------------------------------------------#
1407  
1408 # these attributes are lazy, built from either _uri->options or from
1409 # _config_options captured in BUILDARGS
1410 59 my @deferred_options = qw(
1411 app_name
1412 auth_mechanism
1413 auth_mechanism_properties
1414 connect_timeout_ms
1415 db_name
1416 heartbeat_frequency_ms
1417 j
1418 local_threshold_ms
1419 max_staleness_seconds
1420 max_time_ms
1421 read_pref_mode
1422 read_pref_tag_sets
1423 replica_set_name
1424 retry_writes
1425 retry_reads
1426 server_selection_timeout_ms
1427 server_selection_try_once
1428 socket_check_interval_ms
1429 socket_timeout_ms
1430 ssl
1431 username
1432 password
1433 w
1434 wtimeout
1435 read_concern_level
1436 );
1437  
1438 189 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1439 189 my $orig = shift;
1440 189 my $class = shift;
1441 189 my $hr = $class->$orig(@_);
1442 189 my $deferred = {};
1443 189 for my $k ( @deferred_options ) {
1444 $deferred->{$k} = delete $hr->{$k}
1445 4725 ▶ if exists $hr->{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $hr->{$k} 441 4284 4725
1446 }
1447 189 $hr->{_deferred} = $deferred;
1448 189 return $hr;
1449 59 };
1450  
1451 189 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=12.4
Pod undocumented
1452 189 my ($self, $opts) = @_;
1453  
1454 189 my $uri = $self->_uri;
1455  
1456 189 my @addresses = @{ $uri->hostids };
1457  
1458 # resolve and validate all deferred attributes
1459 189 $self->$_ for @deferred_options;
1460  
1461 # resolve and validate read pref and write concern
1462 186 $self->read_preference;
1463 184 $self->write_concern;
1464  
1465 # Add error handler to codec if user didn't provide their own
1466 183 ▶ unless ( $self->bson_codec->error_callback ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->bson_codec->error_callback) 183 0 183
1467 $self->_set_bson_codec(
1468 $self->bson_codec->clone(
1469 0 ▶ error_callback => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1470 0 my ($msg, $ref, $op) = @_;
1471 0 ▶ if ( $op =~ /^encode/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op =~ /^encode/) { } 0 0 0
1472 0 MongoDB::DocumentError->throw(
1473 message => $msg,
1474 document => $ref
1475 );
1476 }
1477 else {
1478 0 MongoDB::DecodingError->throw($msg);
1479 }
1480 },
1481 )
1482 183 );
1483 }
1484  
1485 # Instantiate topology
1486 183 $self->_topology;
1487  
1488 174 return;
1489 }
1490  
1491 #--------------------------------------------------------------------------#
1492 # helper functions
1493 #--------------------------------------------------------------------------#
1494  
1495 4220 ▶sub __uri_or_else {
Subroutine __uri_or_else: called CC=3 SCAR=11.0
1496 4220 my ( $self, %spec ) = @_;
1497 4220 my $uri_options = $self->_uri->options;
1498 4220 my $deferred = $self->_deferred;
1499 4220 my ( $u, $e, $default ) = @spec{qw/u e d/};
1500 return
1501 exists $uri_options->{$u} ? $uri_options->{$u}
1502 4220 ▶ : exists $deferred->{$e} ? $deferred->{$e}
Branch: 2 branches
BranchTrueFalseTotal
exists $uri_options->{$u} ? : 80 4140 4220
exists $deferred->{$e} ? : 346 3794 4140
1503 : $default;
1504 }
1505  
1506 188 ▶sub __string {
Subroutine __string: called CC=3 SCAR=11.0
1507 188 local $_;
1508 188 ▶ my ($first) = grep { defined && length } @_;
Condition100%3/3
defined $_ && length $_
!ll&&!rl&&r
18617317
MC/DC100%2/2
defined $_ && length $_
defined $_ length $_
Truth table100%3/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
1509 188 ▶ return $first || '';
Condition100%2/2
$first || ''
l!l
16172
MC/DC100%1/1
$first || ''
$first
Truth table100%2/2
A
A= $first
Aresult
11
00
1510 }
1511  
1512 #--------------------------------------------------------------------------#
1513 # public methods - network communication
1514 #--------------------------------------------------------------------------#
1515  
1516 #pod =method connect
1517 #pod
1518 #pod $client->connect;
1519 #pod
1520 #pod Calling this method is unnecessary, as connections are established
1521 #pod automatically as needed. It is kept for backwards compatibility. Calling it
1522 #pod will check all servers in the deployment which ensures a connection to any
1523 #pod that are available.
1524 #pod
1525 #pod See L</reconnect> for a method that is useful when using forks or threads.
1526 #pod
1527 #pod =cut
1528  
1529 0 ▶sub connect {
Subroutine connect: not called CC=1 SCAR=6.9
Pod documented
1530 0 my ($self) = @_;
1531 0 $self->_topology->scan_all_servers;
1532 0 return 1;
1533 }
1534  
1535 #pod =method disconnect
1536 #pod
1537 #pod $client->disconnect;
1538 #pod
1539 #pod Drops all connections to servers.
1540 #pod
1541 #pod =cut
1542  
1543 0 ▶sub disconnect {
Subroutine disconnect: not called CC=1 SCAR=6.9
Pod documented
1544 0 my ($self) = @_;
1545 0 $self->_topology->close_all_links;
1546 0 return 1;
1547 }
1548  
1549 #pod =method reconnect
1550 #pod
1551 #pod $client->reconnect;
1552 #pod
1553 #pod This method closes all connections to the server, as if L</disconnect> were
1554 #pod called, and then immediately reconnects. It also clears the session
1555 #pod cache. Use this after forking or spawning off a new thread.
1556 #pod
1557 #pod =cut
1558  
1559 0 ▶sub reconnect {
Subroutine reconnect: not called CC=1 SCAR=6.9
Pod documented
1560 0 my ($self) = @_;
1561 0 $self->_topology->close_all_links;
1562 0 $self->_server_session_pool->reset_pool;
1563 0 $self->_topology->scan_all_servers(1);
1564 0 return 1;
1565 }
1566  
1567 #pod =method topology_status
1568 #pod
1569 #pod $client->topology_status;
1570 #pod $client->topology_status( refresh => 1 );
1571 #pod
1572 #pod Returns a hash reference with server topology information like this:
1573 #pod
1574 #pod {
1575 #pod 'topology_type' => 'ReplicaSetWithPrimary'
1576 #pod 'replica_set_name' => 'foo',
1577 #pod 'last_scan_time' => '1433766895.183241',
1578 #pod 'servers' => [
1579 #pod {
1580 #pod 'address' => 'localhost:50003',
1581 #pod 'ewma_rtt_ms' => '0.223462326',
1582 #pod 'type' => 'RSSecondary'
1583 #pod },
1584 #pod {
1585 #pod 'address' => 'localhost:50437',
1586 #pod 'ewma_rtt_ms' => '0.268435456',
1587 #pod 'type' => 'RSArbiter'
1588 #pod },
1589 #pod {
1590 #pod 'address' => 'localhost:50829',
1591 #pod 'ewma_rtt_ms' => '0.737782272',
1592 #pod 'type' => 'RSPrimary'
1593 #pod }
1594 #pod },
1595 #pod }
1596 #pod
1597 #pod If the 'refresh' argument is true, then the topology will be scanned
1598 #pod to update server data before returning the hash reference.
1599 #pod
1600 #pod =cut
1601  
1602 0 ▶sub topology_status {
Subroutine topology_status: not called CC=2 SCAR=17.9
Pod documented
1603 0 my ($self, %opts) = @_;
1604 0 ▶ $self->_topology->scan_all_servers(1) if $opts{refresh};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"refresh"} 0 0 0
1605 0 return $self->_topology->status_struct;
1606 }
1607  
1608 #pod =method start_session
1609 #pod
1610 #pod $client->start_session;
1611 #pod $client->start_session( $options );
1612 #pod
1613 #pod Returns a new L<MongoDB::ClientSession> with the supplied options.
1614 #pod
1615 #pod will throw a C<MongoDB::ConfigurationError> if sessions are not supported by
1616 #pod the connected MongoDB deployment.
1617 #pod
1618 #pod the options hash is an optional hash which can have the following keys:
1619 #pod
1620 #pod =for :list
1621 #pod * C<causalConsistency> - Enable Causally Consistent reads for this session.
1622 #pod Defaults to true.
1623 #pod
1624 #pod for more information see L<MongoDB::ClientSession/options>.
1625 #pod
1626 #pod =cut
1627  
1628 0 ▶sub start_session {
Subroutine start_session: not called CC=2 SCAR=17.9
Pod documented
1629 0 my ( $self, $opts ) = @_;
1630  
1631 0 ▶ unless ( $self->_topology->_supports_sessions ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_topology->_supports_sessions) 0 0 0
1632 0 MongoDB::ConfigurationError->throw( "Sessions are not supported by this MongoDB deployment" );
1633 }
1634  
1635 0 return $self->_start_client_session( 1, $opts );
1636 }
1637  
1638 0 ▶sub _maybe_get_implicit_session {
Subroutine _maybe_get_implicit_session: not called CC=2 SCAR=17.9
1639 0 my ($self) = @_;
1640  
1641 # Dont return an error as implicit sessions need to be backwards compatible
1642 0 ▶ return undef unless $self->_topology->_supports_sessions; ## no critic
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_topology->_supports_sessions 0 0 0
1643  
1644 0 return $self->_start_client_session(0);
1645 }
1646  
1647 0 ▶sub _start_client_session {
Subroutine _start_client_session: not called CC=2 SCAR=17.9
1648 0 my ( $self, $is_explicit, $opts ) = @_;
1649  
1650 0 ▶ $opts ||= {};
Condition0%0/2
$opts ||= {}
l!l
00
MC/DC0%0/1
$opts ||= {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
1651  
1652 0 my $session = $self->_server_session_pool->get_server_session;
1653 0 return MongoDB::ClientSession->new(
1654 client => $self,
1655 options => $opts,
1656 _is_explicit => $is_explicit,
1657 server_session => $session,
1658 );
1659 }
1660  
1661 #--------------------------------------------------------------------------#
1662 # semi-private methods; these are public but undocumented and their
1663 # semantics might change in future releases
1664 #--------------------------------------------------------------------------#
1665  
1666 # Undocumented in old MongoDB::MongoClient; semantics don't translate, but
1667 # best approximation is checking if we can send a command to a server
1668 0 ▶sub connected {
Subroutine connected: not called CC=1 SCAR=6.9
Pod undocumented
1669 0 my ($self) = @_;
1670 0 return eval { $self->send_admin_command([ismaster => 1]); 1 };
1671 }
1672  
1673 0 ▶sub send_admin_command {
Subroutine send_admin_command: not called CC=4 SCAR=30.0
Pod undocumented
1674 0 my ( $self, $command, $read_pref ) = @_;
1675  
1676 0 ▶ $read_pref = MongoDB::ReadPreference->new(
Branch: 2 branches
BranchTrueFalseTotal
if $read_pref and ref $read_pref ne "MongoDB::ReadPreference" 0 0 0
ref $read_pref ? : 0 0 0
Condition0%0/3
$read_pref and ref $read_pref ne "MongoDB::ReadPreference"
!ll&&!rl&&r
000
MC/DC0%0/2
$read_pref and ref $read_pref ne "MongoDB::ReadPreference"
$read_pref ref $read_pref ne "MongoDB::ReadPreference"
Truth table0%0/3
A and B
A= $read_prefB= ref $read_pref ne "MongoDB::ReadPreference"
ABresult
0X0
100
111
1677 ref($read_pref) ? $read_pref : ( mode => $read_pref ) )
1678 if $read_pref && ref($read_pref) ne 'MongoDB::ReadPreference';
1679  
1680 0 my $op = MongoDB::Op::_Command->_new(
1681 db_name => 'admin',
1682 query => $command,
1683 query_flags => {},
1684 bson_codec => $self->bson_codec,
1685 read_preference => $read_pref,
1686 session => $self->_maybe_get_implicit_session,
1687 monitoring_callback => $self->monitoring_callback,
1688 );
1689  
1690 0 return $self->send_retryable_read_op( $op );
1691 }
1692  
1693 # Ostensibly the same as above, but allows for specific addressing - uses 'send_direct_op'.
1694 0 ▶sub _send_direct_admin_command {
Subroutine _send_direct_admin_command: not called CC=4 SCAR=30.0
1695 0 my ( $self, $address, $command, $read_pref ) = @_;
1696  
1697 0 ▶ $read_pref = MongoDB::ReadPreference->new(
Branch: 2 branches
BranchTrueFalseTotal
if $read_pref and ref $read_pref ne "MongoDB::ReadPreference" 0 0 0
ref $read_pref ? : 0 0 0
Condition0%0/3
$read_pref and ref $read_pref ne "MongoDB::ReadPreference"
!ll&&!rl&&r
000
MC/DC0%0/2
$read_pref and ref $read_pref ne "MongoDB::ReadPreference"
$read_pref ref $read_pref ne "MongoDB::ReadPreference"
Truth table0%0/3
A and B
A= $read_prefB= ref $read_pref ne "MongoDB::ReadPreference"
ABresult
0X0
100
111
1698 ref($read_pref) ? $read_pref : ( mode => $read_pref ) )
1699 if $read_pref && ref($read_pref) ne 'MongoDB::ReadPreference';
1700  
1701 0 my $op = MongoDB::Op::_Command->_new(
1702 db_name => 'admin',
1703 query => $command,
1704 query_flags => {},
1705 bson_codec => $self->bson_codec,
1706 read_preference => $read_pref,
1707 session => $self->_maybe_get_implicit_session,
1708 monitoring_callback => $self->monitoring_callback,
1709 );
1710  
1711 0 return $self->send_direct_op( $op, $address );
1712 }
1713  
1714 #--------------------------------------------------------------------------#
1715 # database helper methods
1716 #--------------------------------------------------------------------------#
1717  
1718 #pod =method list_databases
1719 #pod
1720 #pod # get all information on all databases
1721 #pod my @dbs = $client->list_databases;
1722 #pod
1723 #pod # get only the foo databases
1724 #pod my @foo_dbs = $client->list_databases({ filter => { name => qr/^foo/ } });
1725 #pod
1726 #pod Lists all databases with information on each database. Supports filtering by
1727 #pod any of the output fields under the C<filter> argument, such as:
1728 #pod
1729 #pod =for :list
1730 #pod * C<name>
1731 #pod * C<sizeOnDisk>
1732 #pod * C<empty>
1733 #pod * C<shards>
1734 #pod
1735 #pod =cut
1736  
1737 0 ▶sub list_databases {
Subroutine list_databases: not called CC=8 SCAR=42.8
Pod documented
1738 0 my ( $self, $args ) = @_;
1739 0 my @databases;
1740 eval {
1741 0 ▶ my $output = $self->send_admin_command([ listDatabases => 1, ( $args ? %$args : () ) ])->output;
Branch: 1 branch
BranchTrueFalseTotal
$args ? : 0 0 0
1742 0 ▶ if (ref($output) eq 'HASH' && exists $output->{databases}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $output eq "HASH" and exists $output->{"databases"}) 0 0 0
Condition0%0/3
ref $output eq "HASH" and exists $output->{"databases"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $output eq "HASH" and exists $output->{"databases"}
ref $output eq "HASH" exists $output->{"databases"}
Truth table0%0/3
A and B
A= ref $output eq "HASH"B= exists $output->{"databases"}
ABresult
0X0
100
111
1743 0 @databases = @{ $output->{databases} };
1744 }
1745 0 return 1;
1746 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $output = $self->send_admin_command(["listDatabases", 1, $args ? %$args : ()])->output; if (ref $output eq "HASH" and exists $output->{"databases"}) { @databases = @{$output->{"databases"};}; } ; return 1 } } or do { my $error = $@ || "Unknown error"; if ($error->$MongoDB::MongoClient::_isa("MongoDB::DatabaseError")) { return if $error->result->output->{"code"} == 15927; } ; die $error } 0 0 0
1747 0 ▶ my $error = $@ || "Unknown error";
Condition0%0/2
$@ || 'Unknown error'
l!l
00
MC/DC0%0/1
$@ || 'Unknown error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
1748 0 ▶ if ( $error->$_isa("MongoDB::DatabaseError" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error->$MongoDB::MongoClient::_isa("MongoDB::DatabaseError")) 0 0 0
1749 0 ▶ return if $error->result->output->{code} == CANT_OPEN_DB_IN_READ_LOCK();
Branch: 1 branch
BranchTrueFalseTotal
if $error->result->output->{"code"} == 15927 0 0 0
1750 }
1751 0 die $error;
1752 };
1753 0 return @databases;
1754 }
1755  
1756 #pod =method database_names
1757 #pod
1758 #pod my @dbs = $client->database_names;
1759 #pod
1760 #pod # get only the foo database names
1761 #pod my @foo_dbs = $client->database_names({ filter => { name => qr/^foo/ } });
1762 #pod
1763 #pod List of all database names on the MongoDB server. Supports filters in the same
1764 #pod way as L</"list_databases">.
1765 #pod
1766 #pod =cut
1767  
1768 0 ▶sub database_names {
Subroutine database_names: not called CC=2 SCAR=17.9
Pod documented
1769 0 my ( $self, $args ) = @_;
1770  
1771 0 ▶ $args ||= {};
Condition0%0/2
$args ||= {}
l!l
00
MC/DC0%0/1
$args ||= {}
$args
Truth table0%0/2
A
A= $args
Aresult
11
00
1772 0 $args->{nameOnly} = 1;
1773 0 my @output = $self->list_databases($args);
1774  
1775 0 my @databases = map { $_->{name} } @output;
1776  
1777 0 return @databases;
1778 }
1779  
1780 #pod =method get_database, db
1781 #pod
1782 #pod my $database = $client->get_database('foo');
1783 #pod my $database = $client->get_database('foo', $options);
1784 #pod my $database = $client->db('foo', $options);
1785 #pod
1786 #pod Returns a L<MongoDB::Database> instance for the database with the given
1787 #pod C<$name>.
1788 #pod
1789 #pod It takes an optional hash reference of options that are passed to the
1790 #pod L<MongoDB::Database> constructor.
1791 #pod
1792 #pod The C<db> method is an alias for C<get_database>.
1793 #pod
1794 #pod =cut
1795  
1796 0 ▶sub get_database {
Subroutine get_database: not called CC=2 SCAR=17.9
Pod documented
1797 0 my ( $self, $database_name, $options ) = @_;
1798 0 ▶ return MongoDB::Database->new(
Branch: 1 branch
BranchTrueFalseTotal
$options ? : 0 0 0
1799 read_preference => $self->read_preference,
1800 write_concern => $self->write_concern,
1801 read_concern => $self->read_concern,
1802 bson_codec => $self->bson_codec,
1803 max_time_ms => $self->max_time_ms,
1804 ( $options ? %$options : () ),
1805 # not allowed to be overridden by options
1806 _client => $self,
1807 name => $database_name,
1808 );
1809 }
1810  
1811 59 ▶{ no warnings 'once'; *db = \&get_database }
Subroutine BEGIN: called CC=1 SCAR=0.0
1812  
1813 #pod =method get_namespace, ns
1814 #pod
1815 #pod my $collection = $client->get_namespace('test.foo');
1816 #pod my $collection = $client->get_namespace('test.foo', $options);
1817 #pod my $collection = $client->ns('test.foo', $options);
1818 #pod
1819 #pod Returns a L<MongoDB::Collection> instance for the given namespace.
1820 #pod The namespace has both the database name and the collection name
1821 #pod separated with a dot character.
1822 #pod
1823 #pod This is a quick way to get a collection object if you don't need
1824 #pod the database object separately.
1825 #pod
1826 #pod It takes an optional hash reference of options that are passed to the
1827 #pod L<MongoDB::Collection> constructor. The intermediate L<MongoDB::Database>
1828 #pod object will be created with default options.
1829 #pod
1830 #pod The C<ns> method is an alias for C<get_namespace>.
1831 #pod
1832 #pod =cut
1833  
1834 0 ▶sub get_namespace {
Subroutine get_namespace: not called CC=5 SCAR=34.0
Pod documented
1835 0 my ( $self, $ns, $options ) = @_;
1836 0 ▶ MongoDB::UsageError->throw("namespace requires a string argument")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ns and length $ns 0 0 0
Condition0%0/3
defined $ns and length $ns
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ns and length $ns
defined $ns length $ns
Truth table0%0/3
A and B
A= defined $nsB= length $ns
ABresult
0X0
100
111
1837 unless defined($ns) && length($ns);
1838 0 my ( $db, $coll ) = split /\./, $ns, 2;
1839 0 ▶ MongoDB::UsageError->throw("$ns is not a valid namespace")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $db and defined $coll 0 0 0
Condition0%0/3
defined $db and defined $coll
!ll&&!rl&&r
000
MC/DC0%0/2
defined $db and defined $coll
defined $db defined $coll
Truth table0%0/3
A and B
A= defined $dbB= defined $coll
ABresult
0X0
100
111
1840 unless defined($db) && defined($coll);
1841 0 return $self->db($db)->coll( $coll, $options );
1842 }
1843  
1844 59 ▶{ no warnings 'once'; *ns = \&get_namespace }
Subroutine BEGIN: called CC=1 SCAR=0.0
1845  
1846 #pod =method fsync(\%args)
1847 #pod
1848 #pod $client->fsync();
1849 #pod
1850 #pod A function that will forces the server to flush all pending writes to the storage layer.
1851 #pod
1852 #pod The fsync operation is synchronous by default, to run fsync asynchronously, use the following form:
1853 #pod
1854 #pod $client->fsync({async => 1});
1855 #pod
1856 #pod The primary use of fsync is to lock the database during backup operations. This will flush all data to the data storage layer and block all write operations until you unlock the database. Note: you can still read while the database is locked.
1857 #pod
1858 #pod $conn->fsync({lock => 1});
1859 #pod
1860 #pod =cut
1861  
1862 0 ▶sub fsync {
Subroutine fsync: not called CC=2 SCAR=17.9
Pod documented
1863 0 my ($self, $args) = @_;
1864  
1865 0 ▶ $args ||= {};
Condition0%0/2
$args ||= {}
l!l
00
MC/DC0%0/1
$args ||= {}
$args
Truth table0%0/2
A
A= $args
Aresult
11
00
1866  
1867 # Pass this in as array-ref to ensure that 'fsync => 1' is the first argument.
1868 0 return $self->get_database('admin')->run_command([fsync => 1, %$args]);
1869 }
1870  
1871 #pod =method fsync_unlock
1872 #pod
1873 #pod $conn->fsync_unlock();
1874 #pod
1875 #pod Unlocks a database server to allow writes and reverses the operation of a $conn->fsync({lock => 1}); operation.
1876 #pod
1877 #pod =cut
1878  
1879 0 ▶sub fsync_unlock {
Subroutine fsync_unlock: not called CC=1 SCAR=6.9
Pod documented
1880 0 my ($self) = @_;
1881  
1882 0 my $op = MongoDB::Op::_FSyncUnlock->_new(
1883 db_name => 'admin',
1884 client => $self,
1885 bson_codec => $self->bson_codec,
1886 monitoring_callback => $self->monitoring_callback,
1887 );
1888  
1889 0 return $self->send_primary_op($op);
1890 }
1891  
1892 0 ▶sub _get_session_from_hashref {
Subroutine _get_session_from_hashref: not called CC=4 SCAR=30.0
1893 0 my ( $self, $hashref ) = @_;
1894  
1895 0 my $session = delete $hashref->{session};
1896  
1897 0 ▶ if ( defined $session ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $session) { } 0 0 0
1898 0 ▶ MongoDB::UsageError->throw( "Cannot use session from another client" )
Branch: 1 branch
BranchTrueFalseTotal
if $session->client->_id ne $self->_id 0 0 0
1899 if ( $session->client->_id ne $self->_id );
1900 0 ▶ MongoDB::UsageError->throw( "Cannot use session which has ended" )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $session->session_id 0 0 0
1901 if ! defined $session->session_id;
1902 } else {
1903 0 $session = $self->_maybe_get_implicit_session;
1904 }
1905  
1906 0 return $session;
1907 }
1908  
1909 #pod =method watch
1910 #pod
1911 #pod Watches for changes on the cluster.
1912 #pod
1913 #pod Perform an aggregation with an implicit initial C<$changeStream> stage
1914 #pod and returns a L<MongoDB::ChangeStream> result which can be used to
1915 #pod iterate over the changes in the cluster. This functionality is
1916 #pod available since MongoDB 4.0.
1917 #pod
1918 #pod my $stream = $client->watch();
1919 #pod my $stream = $client->watch( \@pipeline );
1920 #pod my $stream = $client->watch( \@pipeline, \%options );
1921 #pod
1922 #pod while (1) {
1923 #pod
1924 #pod # This inner loop will only run until no more changes are
1925 #pod # available.
1926 #pod while (my $change = $stream->next) {
1927 #pod # process $change
1928 #pod }
1929 #pod }
1930 #pod
1931 #pod The returned stream will not block forever waiting for changes. If you
1932 #pod want to respond to changes over a longer time use C<maxAwaitTimeMS> and
1933 #pod regularly call C<next> in a loop.
1934 #pod
1935 #pod See L<MongoDB::Collection/watch> for details on usage and available
1936 #pod options.
1937 #pod
1938 #pod =cut
1939  
1940 0 ▶sub watch {
Subroutine watch: not called CC=8 SCAR=42.8
Pod documented
1941 0 my ( $self, $pipeline, $options ) = @_;
1942  
1943 0 ▶ $pipeline ||= [];
Condition0%0/2
$pipeline ||= []
l!l
00
MC/DC0%0/1
$pipeline ||= []
$pipeline
Truth table0%0/2
A
A= $pipeline
Aresult
11
00
1944 0 ▶ $options ||= {};
Condition0%0/2
$options ||= {}
l!l
00
MC/DC0%0/1
$options ||= {}
$options
Truth table0%0/2
A
A= $options
Aresult
11
00
1945  
1946 0 my $session = $self->_get_session_from_hashref( $options );
1947  
1948 return MongoDB::ChangeStream->new(
1949 exists($options->{startAtOperationTime})
1950 ? (start_at_operation_time => delete $options->{startAtOperationTime})
1951 : (),
1952 exists($options->{fullDocument})
1953 ? (full_document => delete $options->{fullDocument})
1954 : (full_document => 'default'),
1955 exists($options->{resumeAfter})
1956 ? (resume_after => delete $options->{resumeAfter})
1957 : (),
1958 exists($options->{startAfter})
1959 ? (start_after => delete $options->{startAfter})
1960 : (),
1961 exists($options->{maxAwaitTimeMS})
1962 ? (max_await_time_ms => delete $options->{maxAwaitTimeMS})
1963 0 ▶ : (),
Branch: 5 branches
BranchTrueFalseTotal
exists $options->{'startAtOperationTime'} ? : 0 0 0
exists $options->{'fullDocument'} ? : 0 0 0
exists $options->{'resumeAfter'} ? : 0 0 0
exists $options->{'startAfter'} ? : 0 0 0
exists $options->{'maxAwaitTimeMS'} ? : 0 0 0
1964 client => $self,
1965 all_changes_for_cluster => 1,
1966 pipeline => $pipeline,
1967 session => $session,
1968 options => $options,
1969 op_args => {
1970 read_concern => $self->read_concern,
1971 db_name => 'admin',,
1972 coll_name => 1,
1973 full_name => 'admin.1',
1974 bson_codec => $self->bson_codec,
1975 write_concern => $self->write_concern,
1976 read_concern => $self->read_concern,
1977 read_preference => $self->read_preference,
1978 monitoring_callback => $self->monitoring_callback,
1979 },
1980 );
1981 }
1982  
1983 0 ▶sub _primary_server_version {
Subroutine _primary_server_version: not called CC=1 SCAR=6.9
1984 0 my $self = shift;
1985 0 my $build = $self->send_admin_command( [ buildInfo => 1 ] )->output;
1986 0 my ($version_str) = $build->{version} =~ m{^([0-9.]+)};
1987 0 return version->parse("v$version_str");
1988 }
1989  
1990 59 1;
1991  
1992 __END__
« MongoDB/Op/_Count.pm ↑ Summary MongoDB/Role/_WriteResult.pm »