| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | #  Copyright 2014 - 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 | 58 |  |  | 58 |  | 420 | use strict; | 
|  | 58 |  |  |  |  | 147 |  | 
|  | 58 |  |  |  |  | 1779 |  | 
| 16 | 58 |  |  | 58 |  | 319 | use warnings; | 
|  | 58 |  |  |  |  | 137 |  | 
|  | 58 |  |  |  |  | 2039 |  | 
| 17 |  |  |  |  |  |  | package MongoDB::Op::_CreateIndexes; | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | # Encapsulate index creation operations; returns a MongoDB::CommandResult | 
| 20 |  |  |  |  |  |  | # or a MongoDB::InsertManyResult, depending on the server version | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 58 |  |  | 58 |  | 330 | use version; | 
|  | 58 |  |  |  |  | 147 |  | 
|  | 58 |  |  |  |  | 355 |  | 
| 23 |  |  |  |  |  |  | our $VERSION = 'v2.2.0'; | 
| 24 |  |  |  |  |  |  |  | 
| 25 | 58 |  |  | 58 |  | 4729 | use Moo; | 
|  | 58 |  |  |  |  | 169 |  | 
|  | 58 |  |  |  |  | 444 |  | 
| 26 |  |  |  |  |  |  |  | 
| 27 | 58 |  |  | 58 |  | 18776 | use MongoDB::Op::_Command; | 
|  | 58 |  |  |  |  | 167 |  | 
|  | 58 |  |  |  |  | 1625 |  | 
| 28 | 58 |  |  | 58 |  | 23784 | use MongoDB::Op::_BatchInsert; | 
|  | 58 |  |  |  |  | 205 |  | 
|  | 58 |  |  |  |  | 2515 |  | 
| 29 | 58 |  |  |  |  | 520 | use Types::Standard qw( | 
| 30 |  |  |  |  |  |  | ArrayRef | 
| 31 |  |  |  |  |  |  | HashRef | 
| 32 | 58 |  |  | 58 |  | 455 | ); | 
|  | 58 |  |  |  |  | 152 |  | 
| 33 | 58 |  |  |  |  | 406 | use MongoDB::_Types qw( | 
| 34 |  |  |  |  |  |  | Numish | 
| 35 | 58 |  |  | 58 |  | 54585 | ); | 
|  | 58 |  |  |  |  | 143 |  | 
| 36 |  |  |  |  |  |  |  | 
| 37 | 58 |  |  | 58 |  | 54768 | use namespace::clean; | 
|  | 58 |  |  |  |  | 171 |  | 
|  | 58 |  |  |  |  | 294 |  | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | has indexes => ( | 
| 40 |  |  |  |  |  |  | is       => 'ro', | 
| 41 |  |  |  |  |  |  | required => 1, | 
| 42 |  |  |  |  |  |  | isa      => ArrayRef [HashRef], | 
| 43 |  |  |  |  |  |  | ); | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | has max_time_ms => ( | 
| 46 |  |  |  |  |  |  | is => 'ro', | 
| 47 |  |  |  |  |  |  | isa => Numish, | 
| 48 |  |  |  |  |  |  | ); | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | with $_ for qw( | 
| 51 |  |  |  |  |  |  | MongoDB::Role::_PrivateConstructor | 
| 52 |  |  |  |  |  |  | MongoDB::Role::_CollectionOp | 
| 53 |  |  |  |  |  |  | MongoDB::Role::_WriteOp | 
| 54 |  |  |  |  |  |  | ); | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | sub has_collation { | 
| 57 | 0 |  |  | 0 | 0 |  | return grep { defined $_->{collation} } @{ $_[0]->indexes }; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | sub execute { | 
| 61 | 0 |  |  | 0 | 0 |  | my ( $self, $link ) = @_; | 
| 62 |  |  |  |  |  |  |  | 
| 63 | 0 | 0 | 0 |  |  |  | if ( $self->has_collation && !$link->supports_collation ) { | 
| 64 | 0 |  |  |  |  |  | MongoDB::UsageError->throw( | 
| 65 |  |  |  |  |  |  | "MongoDB host '" . $link->address . "' doesn't support collation" ); | 
| 66 |  |  |  |  |  |  | } | 
| 67 |  |  |  |  |  |  |  | 
| 68 | 0 | 0 |  |  |  |  | my $res = | 
| 69 |  |  |  |  |  |  | $link->supports_write_commands | 
| 70 |  |  |  |  |  |  | ? $self->_command_create_indexes($link) | 
| 71 |  |  |  |  |  |  | : $self->_legacy_index_insert($link); | 
| 72 |  |  |  |  |  |  |  | 
| 73 | 0 |  |  |  |  |  | return $res; | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | sub _command_create_indexes { | 
| 77 | 0 |  |  | 0 |  |  | my ( $self, $link, $op_doc ) = @_; | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | my $op = MongoDB::Op::_Command->_new( | 
| 80 |  |  |  |  |  |  | db_name => $self->db_name, | 
| 81 |  |  |  |  |  |  | query   => [ | 
| 82 |  |  |  |  |  |  | createIndexes => $self->coll_name, | 
| 83 |  |  |  |  |  |  | indexes       => $self->indexes, | 
| 84 | 0 | 0 |  |  |  |  | ( $link->supports_helper_write_concern ? ( @{ $self->write_concern->as_args } ) : () ), | 
|  | 0 | 0 |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | (defined($self->max_time_ms) | 
| 86 |  |  |  |  |  |  | ? (maxTimeMS => $self->max_time_ms) | 
| 87 |  |  |  |  |  |  | : () | 
| 88 |  |  |  |  |  |  | ), | 
| 89 |  |  |  |  |  |  | ], | 
| 90 |  |  |  |  |  |  | query_flags         => {}, | 
| 91 |  |  |  |  |  |  | bson_codec          => $self->bson_codec, | 
| 92 |  |  |  |  |  |  | monitoring_callback => $self->monitoring_callback, | 
| 93 |  |  |  |  |  |  | ); | 
| 94 |  |  |  |  |  |  |  | 
| 95 | 0 |  |  |  |  |  | my $res = $op->execute( $link ); | 
| 96 | 0 |  |  |  |  |  | $res->assert_no_write_concern_error; | 
| 97 |  |  |  |  |  |  |  | 
| 98 | 0 |  |  |  |  |  | return $res; | 
| 99 |  |  |  |  |  |  | } | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | sub _legacy_index_insert { | 
| 102 | 0 |  |  | 0 |  |  | my ( $self, $link, $op_doc ) = @_; | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | # construct docs for an insert many op | 
| 105 | 0 |  |  |  |  |  | my $ns = join( ".", $self->db_name, $self->coll_name ); | 
| 106 |  |  |  |  |  |  | my $indexes = [ | 
| 107 |  |  |  |  |  |  | map { | 
| 108 | 0 |  |  |  |  |  | { %$_, ns => $ns } | 
| 109 | 0 |  |  |  |  |  | } @{ $self->indexes } | 
|  | 0 |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | ]; | 
| 111 |  |  |  |  |  |  |  | 
| 112 | 0 |  |  |  |  |  | my $op = MongoDB::Op::_BatchInsert->_new( | 
| 113 |  |  |  |  |  |  | db_name             => $self->db_name, | 
| 114 |  |  |  |  |  |  | coll_name           => "system.indexes", | 
| 115 |  |  |  |  |  |  | full_name           => ( join ".", $self->db_name, "system.indexes" ), | 
| 116 |  |  |  |  |  |  | documents           => $indexes, | 
| 117 |  |  |  |  |  |  | write_concern       => $self->write_concern, | 
| 118 |  |  |  |  |  |  | bson_codec          => $self->bson_codec, | 
| 119 |  |  |  |  |  |  | check_keys          => 0, | 
| 120 |  |  |  |  |  |  | ordered             => 1, | 
| 121 |  |  |  |  |  |  | monitoring_callback => $self->monitoring_callback, | 
| 122 |  |  |  |  |  |  | ); | 
| 123 |  |  |  |  |  |  |  | 
| 124 | 0 |  |  |  |  |  | return $op->execute($link); | 
| 125 |  |  |  |  |  |  | } | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | 1; |