Skip to content
Snippets Groups Projects
Commit f3795e77 authored by Andy Yates's avatar Andy Yates
Browse files

[ENSCORESW-720] and [ENSCORESW-721]. Allowing default HashRef in execute() and...

[ENSCORESW-720] and [ENSCORESW-721]. Allowing default HashRef in execute() and sleep for less than a second.

First issue raised by Lee from EG. He wanted to be able to work with hash refs
in the same way you can do with ArrayRefs. Done and dusted.

Second issue raised by me. Want to sleep for periods less than 1 second for the test
suite sanity.
parent a860ed33
No related branches found
No related tags found
No related merge requests found
......@@ -84,6 +84,7 @@ use Bio::EnsEMBL::Utils::Exception qw(throw);
use Bio::EnsEMBL::Utils::Iterator;
use English qw( -no_match_vars ); #Used for $PROCESS_ID
use Scalar::Util qw(weaken); #Used to not hold a strong ref to DBConnection
use Time::HiRes;
=pod
......@@ -264,8 +265,12 @@ sub execute {
#If no callback then we execute using a default one which returns a 2D array
if(!defined $callback) {
throw('Cannot use fetchrow_hashref() with default mappers. Turn off this option') if $use_hashrefs;
$callback = $self->_mappers()->{array_ref};
if($use_hashrefs) {
$callback = $self->_mappers()->{hash_ref};
}
else {
$callback = $self->_mappers()->{array_ref};
}
}
return $self->_execute( $sql, $callback, $has_return, $use_hashrefs, $params, $prepare_params, $iterator );
......@@ -523,7 +528,8 @@ sub execute_single_result {
Arg [RETRY] : integer the number of retries to attempt with this
transactional block. Defaults to 0.
Arg [PAUSE] : integer the time in seconds to pause in-between retries.
Defaults to 1.
Defaults to 1. Fractions are allowed as use delegate to
Time::HiRes' sleep function
Arg [CONDITION] : CodeRef allows you to inspect the exception raised
and should your callback return true then the
retry will be attempted. If not given then all
......@@ -658,7 +664,7 @@ sub transaction {
if($iteration != $retry) {
if($condition->($error)) {
warn("Encountered error on attempt ${iteration} of ${retry} and have issued a rollback. Will retry after sleeping for $pause second(s): $error");
sleep $pause;
Time::HiRes::sleep $pause;
}
else {
last; #break early if condition of error was not matched
......@@ -888,7 +894,11 @@ my $default_mappers = {
},
array_ref => sub {
my $row = shift @_;
return [@{$row}];
return [@{$row}]; #copy of array done because DBI's array is read only
},
hash_ref => sub {
my $row = shift @_;
return {%{$row}}; #applying same logic as above
}
};
......
......@@ -226,7 +226,7 @@ my $get_value = sub {
# First try retries until the very last attempt
{
my $counter = 0;
$helper->transaction( -RETRY => 3, -SLEEP => 1, -CALLBACK => sub {
$helper->transaction( -RETRY => 3, -PAUSE => 0.1, -CALLBACK => sub {
#Die for the first 3 times (so we will succeed on the final attempt)
$counter++;
if($counter != 4) {
......@@ -243,7 +243,7 @@ my $get_value = sub {
{
my $counter = 0;
throws_ok {
$helper->transaction( -RETRY => 2, -CALLBACK => sub {
$helper->transaction( -RETRY => 2, -PAUSE => 0.1, -CALLBACK => sub {
$counter++;
die 'Throwing an error 2';
})
......@@ -257,7 +257,7 @@ my $get_value = sub {
{
my $counter = 0;
throws_ok {
$helper->transaction( -RETRY => 1, -CALLBACK => sub {
$helper->transaction( -RETRY => 1, -PAUSE => 0.1, -CALLBACK => sub {
$helper->transaction( -RETRY => 10, -CALLBACK => sub {
$counter++;
die 'Throwing an error 3';
......@@ -273,7 +273,8 @@ my $get_value = sub {
my $counter = 0;
throws_ok {
$helper->transaction(
-RETRY => 4,
-RETRY => 4,
-PAUSE => 0.1,
-CALLBACK => sub {
$counter++;
die 'fake deadlock' if $counter <= 2;
......@@ -313,7 +314,7 @@ my $get_value = sub {
#Sixth says you cannot repeat the transaction if it worked
{
my $counter = 0;
$helper->transaction( -RETRY => 5, -CALLBACK => sub {
$helper->transaction( -RETRY => 5, -PAUSE => 0.1, -CALLBACK => sub {
$helper->execute_single_result('select 1');
$counter++;
});
......@@ -330,17 +331,27 @@ my $get_value = sub {
#Doing hashref checks
{
my $sql = 'select meta_key, meta_value from meta where meta_key =?';
my $callback = sub {
my ($row) = @_;
return { name => $row->{meta_value} };
};
my $array_of_hashes = $helper->execute(
-SQL => $sql,
-CALLBACK => $callback,
-USE_HASHREFS => 1,
-PARAMS => ['species.common_name']
);
is_deeply($array_of_hashes, [ { name => 'Human' } ], 'HashRefs in a callback works');
my $params = ['species.common_name'];
{
my $array_of_hashes = $helper->execute(
-SQL => $sql,
-CALLBACK => sub {
my ($row) = @_;
return { name => $row->{meta_value} };
},
-USE_HASHREFS => 1,
-PARAMS => $params
);
is_deeply($array_of_hashes, [ { name => 'Human' } ], 'HashRefs in a callback works');
}
{
my $array_of_hashes = $helper->execute(
-SQL => $sql,
-USE_HASHREFS => 1,
-PARAMS => $params
);
is_deeply($array_of_hashes, [ { meta_key => $params->[0], meta_value => 'Human' } ], 'HashRefs using a default callback works');
}
}
$dba->dbc()->do('alter table meta engine=MyISAM');
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment