| 1 | # Copyright 2015 - 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 | 60 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 60 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | package MongoDB::Role::_UpdatePreEncoder; | ||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||
| 19 | # MongoDB interface for pre-encoding and validating update/replace docs | ||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 60 | ▶ | use version; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 60 | our $VERSION = 'v2.2.2'; | |||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 60 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 60 | ▶ | use BSON::Raw; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 60 | ▶ | use BSON::Types 'bson_array'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 60 | ▶ | use MongoDB::Error; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 60 | ▶ | use MongoDB::_Constants; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 60 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 60 | requires qw/bson_codec/; | |||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 1 | ▶ | sub _pre_encode_update { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_pre_encode_update: called CC=17 SCAR=39.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 1 | my ( $self, $max_bson_object_size, $doc, $is_replace ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | my $type = ref($doc); | |||||||||||||||||||||||||||||||||||||||||||||
| 39 | 1 | my $bson_doc; | |||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 1 | ▶ | if ( $type eq 'BSON::Raw' ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | $bson_doc = $doc->bson; | |||||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 44 | ▶ | elsif ($type eq 'BSON::Array' && !$is_replace) { | |||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$type eq "BSON::Array" and not $is_replace
MC/DC0%0/2
$type eq "BSON::Array" and not $is_replace
$type eq "BSON::Array"
not $is_replace
Truth table33%1/3
A and not(B)
A= $type eq "BSON::Array"B= not $is_replace
| |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | return $doc; | |||||||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 47 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 1 | ▶ | if ($type eq 'ARRAY' && scalar(@$doc) && ref($doc->[0]) && !$is_replace) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition37%3/8
$type eq "ARRAY" and scalar @$doc and ref $doc->[0] and not $is_replace
$type eq "ARRAY" and scalar @$doc and ref $doc->[0]
$type eq "ARRAY" and scalar @$doc
MC/DC0%0/3
$type eq "ARRAY" and scalar @$doc and ref $doc->[0] and not $is_replace
$type eq "ARRAY"
ref $doc->[0]
not $is_replace
Truth table25%1/4
A and B and not(C)
A= $type eq "ARRAY"B= ref $doc->[0]C= not $is_replace
| |||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | return bson_array(@$doc); | |||||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 51 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 1 | ▶ | $bson_doc = $self->bson_codec->encode_one( | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | $doc, | ||||||||||||||||||||||||||||||||||||||||||||||
| 54 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 55 | invalid_chars => $is_replace ? '.' : '', | ||||||||||||||||||||||||||||||||||||||||||||||
| 56 | max_length => $is_replace ? $max_bson_object_size : undef, | ||||||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | # must check if first character of first key is valid for replace/update; | ||||||||||||||||||||||||||||||||||||||||||||||
| 63 | # do this from BSON to get key *after* op_char replacment; | ||||||||||||||||||||||||||||||||||||||||||||||
| 64 | # only need to validate if length is enough for a document with a key | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 1 | my ( $len, undef, $first_char ) = unpack( P_INT32 . "CZ", $bson_doc ); | |||||||||||||||||||||||||||||||||||||||||||||
| 67 | 1 | ▶ | if ( $len >= MIN_KEYED_DOC_LENGTH ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 1 | my $err; | |||||||||||||||||||||||||||||||||||||||||||||
| 69 | 1 | ▶ | if ($is_replace) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | $err = "replacement document must not contain update operators" | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 71 | if $first_char eq '$'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 73 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 1 | ▶ | $err = "update document must only contain update operators" | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 75 | if $first_char ne '$'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 1 | ▶ | MongoDB::DocumentError->throw( | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 79 | message => $err, | ||||||||||||||||||||||||||||||||||||||||||||||
| 80 | document => $doc, | ||||||||||||||||||||||||||||||||||||||||||||||
| 81 | ) if $err; | ||||||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 83 | elsif ( ! $is_replace ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | MongoDB::DocumentError->throw( | |||||||||||||||||||||||||||||||||||||||||||||
| 85 | message => "Update document was empty!", | ||||||||||||||||||||||||||||||||||||||||||||||
| 86 | document => $doc, | ||||||||||||||||||||||||||||||||||||||||||||||
| 87 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 1 | ▶ | return $doc if $type eq 'BSON::Raw'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 91 | # manually bless for speed | ||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 1 | return bless { bson => $bson_doc, metadata => {} }, "BSON::Raw"; | |||||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 60 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||
| 97 | # vim: set ts=4 sts=4 sw=4 et tw=75: | ||||||||||||||||||||||||||||||||||||||||||||||