| 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::_InsertPreEncoder; | ||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | # MongoDB interface for pre-encoding and validating docs to insert | ||||||||||||||||||||||||||||||||
| 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::OID; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | 60 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | 60 | requires qw/bson_codec/; | |||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | # takes MongoDB::_Link and ref of type Document; returns | ||||||||||||||||||||||||||||||||
| 34 | # blessed BSON encode doc and the original/generated _id | ||||||||||||||||||||||||||||||||
| 35 | 2 | ▶ | sub _pre_encode_insert { | ||||||||||||||||||||||||||||||
|
Subroutine
_pre_encode_insert: called CC=11 SCAR=32.1
| |||||||||||||||||||||||||||||||||
| 36 | 2 | my ( $self, $max_bson_size, $doc, $invalid_chars ) = @_; | |||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | 2 | my $type = ref($doc); | |||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | my $id = ( | ||||||||||||||||||||||||||||||||
| 41 | $type eq 'HASH' ? $doc->{_id} | ||||||||||||||||||||||||||||||||
| 42 | : $type eq 'ARRAY' || $type eq 'BSON::Doc' ? do { | ||||||||||||||||||||||||||||||||
| 43 | 0 | my $i; | |||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | for ( $i = 0; $i < @$doc; $i++ ) { last if $doc->[$i] eq '_id' } | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | $i < $#$doc ? $doc->[ $i + 1 ] : undef; | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||
| 47 | : $type eq 'Tie::IxHash' ? $doc->FETCH('_id') | ||||||||||||||||||||||||||||||||
| 48 | : $type eq 'BSON::Raw' ? do { | ||||||||||||||||||||||||||||||||
| 49 | 2 | my $decoded_doc = $self->bson_codec->decode_one( | |||||||||||||||||||||||||||||||
| 50 | $doc->bson, | ||||||||||||||||||||||||||||||||
| 51 | { ordered => 1 } | ||||||||||||||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||||||||||||||
| 53 | 2 | $decoded_doc->{_id}; | |||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||
| 55 | : $doc->{_id} # hashlike? | ||||||||||||||||||||||||||||||||
| 56 | 2 | ▶ | ); | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
$type eq "ARRAY" or $type eq "BSON::Doc"
MC/DC0%0/2
$type eq "ARRAY" or $type eq "BSON::Doc"
$type eq "ARRAY"
$type eq "BSON::Doc"
Truth table33%1/3
A or B
A= $type eq "ARRAY"B= $type eq "BSON::Doc"
| |||||||||||||||||||||||||||||||||
| 57 | 2 | ▶ | if ( ! defined $id ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 58 | 1 | my $creator = $self->bson_codec->can("create_oid"); | |||||||||||||||||||||||||||||||
| 59 | 1 | ▶ | $id = $creator ? $creator->() : BSON::OID->new(); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||
| 61 | 2 | my $bson_doc = $self->bson_codec->encode_one( | |||||||||||||||||||||||||||||||
| 62 | $doc, | ||||||||||||||||||||||||||||||||
| 63 | { | ||||||||||||||||||||||||||||||||
| 64 | invalid_chars => $invalid_chars, | ||||||||||||||||||||||||||||||||
| 65 | max_length => $max_bson_size, | ||||||||||||||||||||||||||||||||
| 66 | first_key => '_id', | ||||||||||||||||||||||||||||||||
| 67 | first_value => $id, | ||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||
| 69 | ); | ||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | # manually bless for speed | ||||||||||||||||||||||||||||||||
| 72 | 2 | return bless { bson => $bson_doc, metadata => { _id => $id } }, | |||||||||||||||||||||||||||||||
| 73 | "BSON::Raw"; | ||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | 60 | 1; | |||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||
| 78 | # vim: set ts=4 sts=4 sw=4 et tw=75: | ||||||||||||||||||||||||||||||||