our
$VERSION
=
'v0.999.999.6'
;
WriteConcern
)
;
requires
qw/db_name _parse_cmd _parse_gle/
;
has
write_concern
=> (
is
=>
'ro'
,
required
=> 1,
isa
=> WriteConcern,
);
sub
_send_legacy_op_with_gle {
my
(
$self
,
$link
,
$op_bson
,
$op_doc
,
$result_class
) =
@_
;
if
(
$self
->write_concern->is_acknowledged ) {
my
@write_concern
= %{
$self
->write_concern->as_struct };
my
$gle
=
$self
->bson_codec->encode_one( [
getlasterror
=> 1,
@write_concern
] );
my
(
$gle_bson
,
$request_id
) =
MongoDB::_Protocol::write_query(
$self
->db_name .
'.$cmd'
,
$gle
,
undef
, 0, -1 );
my
$res
=
$self
->_query_and_receive(
$link
,
$op_bson
.
$gle_bson
,
$request_id
,
undef
)
->{docs}[0];
if
( !
$res
->{ok} && (
$res
->{errmsg} ||
$res
->{
'$err'
} ) ) {
return
MongoDB::CommandResult->_new(
output
=>
$res
,
address
=>
$link
->address,
);
}
my
$got_error
=
(
exists
(
$res
->{jnote} ) &&
$res
->{jnote} =~ NO_JOURNAL_RE ) ?
$res
->{jnote}
: (
exists
(
$res
->{wnote} ) &&
$res
->{wnote} =~ NO_REPLICATION_RE ) ?
$res
->{wnote}
:
undef
;
if
(
$got_error
) {
MongoDB::DatabaseError->throw(
message
=>
$got_error
,
result
=> MongoDB::CommandResult->_new(
output
=>
$res
,
address
=>
$link
->address,
),
);
}
my
(
$write_concern_error
,
$write_error
);
my
$errmsg
=
$res
->{err};
my
$wtimeout
=
$res
->{wtimeout};
if
(
$wtimeout
) {
$write_concern_error
= {
errmsg
=>
$errmsg
,
errInfo
=> {
wtimeout
=>
$wtimeout
},
code
=>
$res
->{code} || WRITE_CONCERN_ERROR,
};
}
elsif
(
$errmsg
) {
$write_error
= {
errmsg
=>
$errmsg
,
code
=>
$res
->{code} || UNKNOWN_ERROR,
index
=> 0,
op
=>
$op_doc
,
};
}
return
$result_class
->_new(
acknowledged
=> 1,
write_errors
=> (
$write_error
? [
$write_error
] : [] ),
write_concern_errors
=> (
$write_concern_error
? [
$write_concern_error
] : [] ),
$self
->_parse_gle(
$res
,
$op_doc
),
);
}
else
{
$link
->
write
(
$op_bson
);
return
$result_class
->_new(
$self
->_parse_gle( {},
$op_doc
),
acknowledged
=> 0,
write_errors
=> [],
write_concern_errors
=> [],
);
}
}
sub
_send_write_command {
my
(
$self
,
$link
,
$cmd
,
$op_doc
,
$result_class
) =
@_
;
my
$res
=
$self
->_send_command(
$link
,
$cmd
);
if
(
$self
->write_concern->is_acknowledged ) {
if
( !
$res
->{ok} && (
$res
->{errmsg} ||
$res
->{
'$err'
} ) ) {
return
MongoDB::CommandResult->_new(
output
=>
$res
,
address
=>
$link
->address,
);
}
if
(
exists
(
$res
->{writeErrors}) && @{
$res
->{writeErrors}} ) {
$res
->{writeErrors}[0]{op} =
$op_doc
;
}
return
$result_class
->_new(
write_errors
=> (
$res
->{writeErrors} ?
$res
->{writeErrors} : [] ),
write_concern_errors
=>
(
$res
->{writeConcernError} ? [
$res
->{writeConcernError} ] : [] ),
$self
->_parse_cmd(
$res
),
);
}
else
{
return
MongoDB::UnacknowledgedResult->_new(
write_errors
=> [],
write_concern_errors
=> [],
);
}
}
1;