Skip to content
This repository was archived by the owner on Dec 22, 2021. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 65 additions & 4 deletions lib/MongoDB/IndexView.pm
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ direction/type.
See L</create_many> for important information about index specifications
and options.

The following additional options are recognized:

=for :list
* C<maxTimeMS> — maximum time in milliseconds before the operation will
time out.

=cut

my $create_one_args;
Expand All @@ -183,8 +189,15 @@ sub create_one {
MongoDB::UsageError->throw("Argument to create_one must be an ordered document")
unless is_OrderedDoc($keys);

my ($name) =
$self->create_many( { keys => $keys, ( $opts ? ( options => $opts ) : () ) } );
my $global_opts = {};
if (exists $opts->{maxTimeMS}) {
$global_opts->{maxTimeMS} = delete $opts->{maxTimeMS};
}

my ($name) = $self->create_many(
{ keys => $keys, ( $opts ? ( options => $opts ) : () ) },
$global_opts,
);
return $name;
}

Expand All @@ -195,10 +208,20 @@ sub create_one {
{ keys => [ z => 1 ], options => { unique => 1 } }
);

@names = $indexes->create_many(
{ keys => [ x => 1, y => 1 ] },
{ keys => [ z => 1 ], options => { unique => 1 } }
\%global_options,
);

This method takes a list of index models (given as hash references)
and returns a list of index names created. It will throw an exception
on error.

If the last value is a hash reference without a C<keys> entry, it will
be assumed to be a set of global options. See below for a list of
accepted global options.

Each index module is described by the following fields:

=for :list
Expand Down Expand Up @@ -242,13 +265,25 @@ Some of the more commonly used options include:
* C<name> — a name (string) for the index; one will be generated if this is
omitted.

Global options specified as the last value can contain the following
keys:

=for :list
* C<maxTimeMS> — maximum time in milliseconds before the operation will
time out.

=cut

my $create_many_args;

sub create_many {
my ( $self, @models ) = @_;

my $opts;
if (@models and ref $models[-1] eq 'HASH' and not exists $models[-1]{keys}) {
$opts = pop @models;
}

MongoDB::UsageError->throw("Argument to create_many must be a list of index models")
unless is_IndexModelList(\@models);

Expand All @@ -260,6 +295,10 @@ sub create_many {
bson_codec => $self->_bson_codec,
indexes => $indexes,
write_concern => $self->_write_concern,
(defined($opts->{maxTimeMS})
? (max_time_ms => $opts->{maxTimeMS})
: ()
),
);

# succeed or die; we don't care about response document
Expand All @@ -271,19 +310,26 @@ sub create_many {
=method drop_one

$output = $indexes->drop_one( $name );
$output = $indexes->drop_one( $name, \%options );

This method takes the name of an index and drops it. It returns the output
of the dropIndexes command (a hash reference) on success or throws a
exception if the command errors. However, if the index does not exist, the
command output will have the C<ok> field as a false value, but no exception
will e thrown.

Valid options are:

=for :list
* C<maxTimeMS> — maximum time in milliseconds before the operation will
time out.

=cut

my $drop_one_args;

sub drop_one {
my ( $self, $name ) = @_;
my ( $self, $name, $opts ) = @_;

MongoDB::UsageError->throw("Argument to drop_one must be a string")
unless is_Str($name);
Expand All @@ -299,6 +345,10 @@ sub drop_one {
bson_codec => $self->_bson_codec,
write_concern => $self->_write_concern,
index_name => $name,
(defined($opts->{maxTimeMS})
? (max_time_ms => $opts->{maxTimeMS})
: ()
),
);

$self->_client->send_write_op($op)->output;
Expand All @@ -307,17 +357,24 @@ sub drop_one {
=method drop_all

$output = $indexes->drop_all;
$output = $indexes->drop_all(\%options);

This method drops all indexes (except the one on the C<_id> field). It
returns the output of the dropIndexes command (a hash reference) on success
or throws a exception if the command fails.

Valid options are:

=for :list
* C<maxTimeMS> — maximum time in milliseconds before the operation will
time out.

=cut

my $drop_all_args;

sub drop_all {
my ($self) = @_;
my ($self, $opts) = @_;

my $op = MongoDB::Op::_DropIndexes->_new(
db_name => $self->_db_name,
Expand All @@ -326,6 +383,10 @@ sub drop_all {
bson_codec => $self->_bson_codec,
write_concern => $self->_write_concern,
index_name => '*',
(defined($opts->{maxTimeMS})
? (max_time_ms => $opts->{maxTimeMS})
: ()
),
);

$self->_client->send_write_op($op)->output;
Expand Down
12 changes: 11 additions & 1 deletion lib/MongoDB/Op/_CreateIndexes.pm
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use Types::Standard qw(
ArrayRef
Bool
HashRef
Num
);

use namespace::clean;
Expand All @@ -42,6 +43,11 @@ has indexes => (
isa => ArrayRef [HashRef],
);

has max_time_ms => (
is => 'ro',
isa => Num,
);

with $_ for qw(
MongoDB::Role::_PrivateConstructor
MongoDB::Role::_CollectionOp
Expand Down Expand Up @@ -76,7 +82,11 @@ sub _command_create_indexes {
query => [
createIndexes => $self->coll_name,
indexes => $self->indexes,
( $link->accepts_wire_version(5) ? ( @{ $self->write_concern->as_args } ) : () )
( $link->accepts_wire_version(5) ? ( @{ $self->write_concern->as_args } ) : () ),
(defined($self->max_time_ms)
? (maxTimeMS => $self->max_time_ms)
: ()
),
],
query_flags => {},
bson_codec => $self->bson_codec,
Expand Down
10 changes: 10 additions & 0 deletions lib/MongoDB/Op/_DropIndexes.pm
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use MongoDB::Op::_Command;
use Safe::Isa;
use Types::Standard qw(
Str
Num
);

use namespace::clean;
Expand All @@ -40,6 +41,11 @@ has index_name => (
isa => Str,
);

has max_time_ms => (
is => 'ro',
isa => Num,
);

with $_ for qw(
MongoDB::Role::_PrivateConstructor
MongoDB::Role::_CollectionOp
Expand All @@ -55,6 +61,10 @@ sub execute {
dropIndexes => $self->coll_name,
index => $self->index_name,
( $link->accepts_wire_version(5) ? ( @{ $self->write_concern->as_args } ) : () ),
(defined($self->max_time_ms)
? (maxTimeMS => $self->max_time_ms)
: ()
),
],
query_flags => {},
bson_codec => $self->bson_codec,
Expand Down
1 change: 1 addition & 0 deletions t/indexview.t
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ my $testdb = get_test_db($conn);
my $server_version = server_version($conn);
my $server_type = server_type($conn);
my $coll = $testdb->get_collection('test_collection');
my $admin = $conn->get_database("admin");

my $supports_collation = $server_version >= 3.3.9;
my $valid_collation = { locale => "en_US", strength => 2 };
Expand Down
Loading