| 1 | # Copyright 2016 - 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::Role::_OpReplyParser; | ||||||||||
| 18 | |||||||||||
| 19 | # MongoDB interface for sending OP_QUERY|OP_GETMORE and parsing OP_REPLY | ||||||||||
| 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::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | |||||||||||
| 26 | 59 | ▶ | use MongoDB::Error; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 59 | ▶ | use MongoDB::_Protocol; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 59 | ▶ | use MongoDB::_Constants; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | |||||||||||
| 30 | 59 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | |||||||||||
| 32 | 59 | with $_ for qw( | |||||||||
| 33 | MongoDB::Role::_CommandMonitoring | ||||||||||
| 34 | ); | ||||||||||
| 35 | |||||||||||
| 36 | 59 | requires '_as_command'; | |||||||||
| 37 | |||||||||||
| 38 | # Sends a BSON query/get-more string, then read, parse and validate the reply. | ||||||||||
| 39 | # Throws various errors if the results indicate a problem. Returns | ||||||||||
| 40 | # a "result" structure generated by MongoDB::_Protocol, but with | ||||||||||
| 41 | # the 'docs' field replaced with inflated documents. | ||||||||||
| 42 | |||||||||||
| 43 | # as this is the hot loop, we do a number of odd things in the name of | ||||||||||
| 44 | # optimization, such as chaining lots of operations with ',' to keep them | ||||||||||
| 45 | # in a single statement | ||||||||||
| 46 | |||||||||||
| 47 | 0 | ▶ | sub _query_and_receive { | ||||||||
|
Subroutine
_query_and_receive: not called CC=11 SCAR=48.8
| |||||||||||
| 48 | 0 | my ($self, $link, $op_bson, $request_id) = @_; | |||||||||
| 49 | |||||||||||
| 50 | 0 | my ($result, $doc_bson, $bson_codec, $docs, $len, $i); | |||||||||
| 51 | |||||||||||
| 52 | 0 | ▶ | $self->publish_command_started( $link, $self->_as_command, $request_id ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | if $self->monitoring_callback; | ||||||||||
| 54 | |||||||||||
| 55 | 0 | eval { | |||||||||
| 56 | $link->write( $op_bson ), | ||||||||||
| 57 | ( $result = MongoDB::_Protocol::parse_reply( $link->read, $request_id ) ), | ||||||||||
| 58 | ( $doc_bson = $result->{docs} ), | ||||||||||
| 59 | ( $docs = $result->{docs} = [] ), | ||||||||||
| 60 | ( ( $bson_codec, $i ) = ( $self->bson_codec, 0 ) ), | ||||||||||
| 61 | 0 | ( $#$docs = $result->{number_returned} - 1 ); | |||||||||
| 62 | |||||||||||
| 63 | # XXX should address be added to result here? | ||||||||||
| 64 | |||||||||||
| 65 | MongoDB::CursorNotFoundError->throw("cursor not found") | ||||||||||
| 66 | 0 | ▶ | if $result->{flags}{cursor_not_found}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | |||||||||||
| 68 | # XXX eventually, BSON needs an API to do this efficiently for us without a | ||||||||||
| 69 | # loop here. Alternatively, BSON strings could be returned as objects that | ||||||||||
| 70 | # inflate lazily | ||||||||||
| 71 | |||||||||||
| 72 | 0 | ▶ | while ( length($doc_bson) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | 0 | $len = unpack( P_INT32, $doc_bson ); | |||||||||
| 74 | 0 | ▶ | MongoDB::ProtocolError->throw("document in response at index $i was truncated") | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | if $len > length($doc_bson); | ||||||||||
| 76 | 0 | $docs->[ $i++ ] = $bson_codec->decode_one( substr( $doc_bson, 0, $len, '' ) ); | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | MongoDB::ProtocolError->throw( | ||||||||||
| 80 | sprintf( | ||||||||||
| 81 | "unexpected number of documents: got %s, expected %s", | ||||||||||
| 82 | scalar @$docs, | ||||||||||
| 83 | $result->{number_returned} | ||||||||||
| 84 | ) | ||||||||||
| 85 | 0 | ▶ | ) if scalar @$docs != $result->{number_returned}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | }; | ||||||||||
| 87 | 0 | ▶ | if ( my $err = $@ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 0 | ▶ | $self->publish_command_exception($err) if $self->monitoring_callback; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 0 | die $err; | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | 0 | ▶ | if ($result->{flags}{query_failure}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | 0 | ▶ | $self->publish_legacy_query_error( $result->{docs}[0] ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | if $self->monitoring_callback; | ||||||||||
| 95 | # had query_failure, so pretend the query was a command and assert it here | ||||||||||
| 96 | MongoDB::CommandResult->_new( | ||||||||||
| 97 | 0 | output => $result->{docs}[0], | |||||||||
| 98 | address => $link->address | ||||||||||
| 99 | )->assert; | ||||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | 0 | ▶ | $self->publish_legacy_reply_succeeded($result) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | if $self->monitoring_callback; | ||||||||||
| 104 | |||||||||||
| 105 | 0 | return $result | |||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | 59 | 1; | |||||||||