our
$VERSION
=
'v0.999.999.1'
;
has
db_name
=> (
is
=>
'ro'
,
isa
=> Str,
required
=> 1,
);
has
coll_name
=> (
is
=>
'ro'
,
isa
=> Str,
required
=> 1,
);
has
indexes
=> (
is
=>
'ro'
,
isa
=> ArrayRef[HashRef],
required
=> 1,
);
has
write_concern
=> (
is
=>
'ro'
,
isa
=> WriteConcern,
required
=> 1,
);
sub
execute {
my
(
$self
,
$link
) =
@_
;
my
$res
=
$link
->accepts_wire_version(2)
?
$self
->_command_create_indexes(
$link
)
:
$self
->_legacy_index_insert(
$link
);
$res
->assert;
return
$res
;
}
sub
_command_create_indexes {
my
(
$self
,
$link
,
$op_doc
) =
@_
;
my
$cmd
= Tie::IxHash->new(
createIndexes
=>
$self
->coll_name,
indexes
=>
$self
->indexes,
);
my
$res
=
$self
->_send_command(
$link
,
$cmd
);
return
MongoDB::CommandResult->new(
output
=>
$self
->write_concern->is_acknowledged ?
$res
: {
ok
=> 1 },
address
=>
$link
->address,
);
}
sub
_legacy_index_insert {
my
(
$self
,
$link
,
$op_doc
) =
@_
;
my
$ns
=
join
(
"."
,
$self
->db_name,
$self
->coll_name );
my
$indexes
= [
map
{
{
%$_
,
ns
=>
$ns
}
} @{
$self
->indexes }
];
my
$op
= MongoDB::Op::_BatchInsert->new(
db_name
=>
$self
->db_name,
coll_name
=>
"system.indexes"
,
documents
=>
$indexes
,
write_concern
=>
$self
->write_concern,
bson_codec
=>
$self
->bson_codec,
check_keys
=> 0,
);
return
$op
->execute(
$link
);
}
1;