Skip to content
This repository has been archived by the owner on Dec 22, 2021. It is now read-only.

Use ordered hash for point hints #46

Closed
wants to merge 2 commits into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/MongoDB/Cursor.pm
Expand Up @@ -405,7 +405,7 @@ sub hint {
confess "cannot set hint after querying"
if $self->started_iterating;
confess 'not a hash reference'
unless ref $index eq 'HASH';
unless ref $index eq 'HASH' || ref $index eq 'Tie::IxHash';

$self->_ensure_special;
$self->_query->{'$hint'} = $index;
Expand Down
30 changes: 28 additions & 2 deletions t/cursor.t
Expand Up @@ -19,7 +19,7 @@ if ($@) {
plan skip_all => $@;
}
else {
plan tests => 74;
plan tests => 77;
}

my $db = $conn->get_database('test_database');
Expand Down Expand Up @@ -166,7 +166,7 @@ $cursor->reset;
$exp = $cursor->limit(-20)->explain;
is(20, $exp->{'n'});

#hint
# hint
$cursor->reset;
my $hinted = $cursor->hint({'x' => 1});
is($hinted, $cursor);
Expand All @@ -183,6 +183,32 @@ eval {

ok($@ =~ m/bad hint/);

# ordered hint
$cursor->reset;
$hinted = $cursor->hint(Tie::IxHash->new('x' => 1));
is($hinted, $cursor);

$collection->drop;

$collection->insert({'num' => 1, 'foo' => 1});

$aok = 1;
eval {
$collection->query->hint(Tie::IxHash->new('num' => 1, 'foo' => 1))->explain;
$aok = 0;
};

ok($@ =~ m/bad hint/);

$collection->ensure_index(Tie::IxHash->new('num' => 1, 'foo' => 1));
$aok = 1;
eval {
$collection->find({num => 0, foo => 0})->hint(Tie::IxHash->new('num' => 1, 'foo' => 1))->explain;
$aok = 0;
};

ok($aok == 0);

# MongoDB::Cursor::slave_okay
$MongoDB::Cursor::slave_okay = 1;
$cursor = $collection->query->next;
Expand Down