Bug 28013: Performance improvements to CanBookBeRenewed

In the case of 'AllowRenewalIfOtherItemsAvailable' we check all existing reserves against
all existing items. This patchset reduces the number of DB/subroutine calls

To test:
1 - Apply patch
2 - prove -v t/db_dependent/Circulation.t

Signed-off-by: Amit Gupta <amitddng135@gmail.com>
Signed-off-by: Joonas Kylmälä <joonas.kylmala@helsinki.fi>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
This commit is contained in:
Nick Clemens 2021-03-22 13:03:11 +00:00 committed by Jonathan Druart
parent a620c1bb11
commit 0b8f4ec614
1 changed files with 17 additions and 33 deletions

View File

@ -2872,7 +2872,7 @@ sub CanBookBeRenewed {
}
}
my ( $resfound, $resrec, undef ) = C4::Reserves::CheckReserves($itemnumber);
my ( $resfound, $resrec, $possible_reserves ) = C4::Reserves::CheckReserves($itemnumber);
# If next hold is non priority, then check if any hold with priority (non_priority = 0) exists for the same biblionumber.
if ( $resfound && $resrec->{non_priority} ) {
@ -2888,9 +2888,7 @@ sub CanBookBeRenewed {
if ( $resfound
&& C4::Context->preference('AllowRenewalIfOtherItemsAvailable') )
{
my $schema = Koha::Database->new()->schema();
my $item_holds = $schema->resultset('Reserve')->search( { itemnumber => $itemnumber, found => undef } )->count();
my $item_holds = Koha::Holds->search( { itemnumber => $itemnumber, found => undef } )->count();
if ($item_holds) {
# There is an item level hold on this item, no other item can fill the hold
$resfound = 1;
@ -2898,29 +2896,18 @@ sub CanBookBeRenewed {
else {
# Get all other items that could possibly fill reserves
my @itemnumbers = $schema->resultset('Item')->search(
{
biblionumber => $resrec->{biblionumber},
onloan => undef,
notforloan => 0,
-not => { itemnumber => $itemnumber }
},
{ columns => 'itemnumber' }
)->get_column('itemnumber')->all();
my $items = Koha::Items->search({
biblionumber => $resrec->{biblionumber},
onloan => undef,
notforloan => 0,
-not => { itemnumber => $itemnumber }
});
# Get all other reserves that could have been filled by this item
my @borrowernumbers;
while (1) {
my ( $reserve_found, $reserve, undef ) =
C4::Reserves::CheckReserves( $itemnumber, undef, undef, \@borrowernumbers );
if ($reserve_found) {
push( @borrowernumbers, $reserve->{borrowernumber} );
}
else {
last;
}
}
my @borrowernumbers = map { $_->{borrowernumber} } @$possible_reserves;
my $patrons = Koha::Patrons->search({
borrowernumber => { -in => \@borrowernumbers }
});
# If the count of the union of the lists of reservable items for each borrower
# is equal or greater than the number of borrowers, we know that all reserves
@ -2928,15 +2915,12 @@ sub CanBookBeRenewed {
# by pushing all the elements onto an array and removing the duplicates.
my @reservable;
my %patrons;
ITEM: foreach my $itemnumber (@itemnumbers) {
my $item = Koha::Items->find( $itemnumber );
next if IsItemOnHoldAndFound( $itemnumber );
for my $borrowernumber (@borrowernumbers) {
my $patron = $patrons{$borrowernumber} //= Koha::Patrons->find( $borrowernumber );
ITEM: while ( my $item = $items->next ) {
next if IsItemOnHoldAndFound( $item->itemnumber );
while ( my $patron = $patrons->next ) {
next unless IsAvailableForItemLevelRequest($item, $patron);
next unless CanItemBeReserved($borrowernumber,$itemnumber);
push @reservable, $itemnumber;
next unless CanItemBeReserved($patron->borrowernumber,$item->itemnumber);
push @reservable, $item->itemnumber;
if (@reservable >= @borrowernumbers) {
$resfound = 0;
last ITEM;