our
$VERSION
=
'v1.5.0'
;
ArrayRef
Bool
)
;
has
documents
=> (
is
=>
'ro'
,
required
=> 1,
isa
=> ArrayRef,
);
has
ordered
=> (
is
=>
'ro'
,
required
=> 1,
isa
=> Bool,
);
has
check_keys
=> (
is
=>
'ro'
,
required
=> 1,
isa
=> Bool,
);
has
_doc_ids
=> (
is
=>
'ro'
,
writer
=>
'_set_doc_ids'
,
init_arg
=>
undef
,
isa
=> ArrayRef,
);
with
$_
for
qw(
MongoDB::Role::_PrivateConstructor
MongoDB::Role::_CollectionOp
MongoDB::Role::_SingleBatchDocWrite
MongoDB::Role::_InsertPreEncoder
)
;
sub
execute {
my
(
$self
,
$link
) =
@_
;
my
$documents
=
$self
->documents;
my
$invalid_chars
=
$self
->check_keys ?
'.'
:
''
;
my
(
@insert_docs
,
@ids
);
my
$last_idx
=
$#$documents
;
for
(
my
$i
= 0;
$i
<=
$last_idx
;
$i
++ ) {
push
@insert_docs
,
$self
->_pre_encode_insert(
$link
,
$documents
->[
$i
],
$invalid_chars
);
push
@ids
,
$insert_docs
[-1]{metadata}{_id};
}
$self
->_set_doc_ids(\
@ids
);
return
!
$self
->write_concern->is_acknowledged
? (
$self
->_send_legacy_op_noreply(
$link
,
MongoDB::_Protocol::write_insert(
$self
->full_name,
join
(
""
,
map
{
$_
->{bson} }
@insert_docs
) ),
undef
,
"MongoDB::UnacknowledgedResult"
)
)
:
$link
->does_write_commands
? (
$self
->_send_write_command(
$link
,
Tie::IxHash->new(
insert
=>
$self
->coll_name,
documents
=> \
@insert_docs
,
@{
$self
->write_concern->as_args },
),
undef
,
"MongoDB::InsertManyResult"
,
)->assert
)
: (
$self
->_send_legacy_op_with_gle(
$link
,
MongoDB::_Protocol::write_insert(
$self
->full_name,
join
(
""
,
map
{
$_
->{bson} }
@insert_docs
) ),
undef
,
"MongoDB::InsertManyResult"
)->assert
);
}
sub
_parse_cmd {
my
(
$self
,
$res
) =
@_
;
return
unless
$res
->{ok};
my
$inserted
=
$self
->_doc_ids;
my
$ids
= [
map
+{
index
=>
$_
,
_id
=>
$inserted
->[
$_
] }, 0 .. $
return
(
inserted_count
=>
scalar
@$inserted
,
inserted
=>
$ids
);
}
BEGIN {
no
warnings
'once'
;
*_parse_gle
= \
&_parse_cmd
;
}
1;