Bug 36277: Improve algorithmic complexity of batch_add

The 2 nested loops are terrible in term of algorithmic complexity.
Especially if we are fetching from there.

The goal of this patch is to fetch all the limits outside of the loop.

If you have 100 libraries, it will remove 100^2 - 1 fetches!

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
(cherry picked from commit 202628342a)
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
This commit is contained in:
Jonathan Druart 2024-03-14 14:39:56 +01:00 committed by Fridolin Somers
parent 4c56374272
commit 372adf555d
1 changed files with 20 additions and 13 deletions

View File

@ -141,32 +141,39 @@ sub batch_add {
@to_branches = @library_ids unless $params->{to_library_id};
}
my $dbic_params = Koha::Item::Transfer::Limits->new->attributes_from_api($params);
my %existing_limits =
map { sprintf( "%s:%s:%s:%s", $_->fromBranch, $_->toBranch, $_->itemtype, $_->ccode ) => 1 }
Koha::Item::Transfer::Limits->search($dbic_params)->as_list;
my @results;
foreach my $from ( @from_branches ) {
foreach my $to ( @to_branches ) {
my $limit_params = { %$params };
foreach my $from (@from_branches) {
foreach my $to (@to_branches) {
my $limit_params = {%$params};
$limit_params->{from_library_id} = $from;
$limit_params->{to_library_id} = $to;
$limit_params->{to_library_id} = $to;
next if $to eq $from;
my $transfer_limit = Koha::Item::Transfer::Limit->new_from_api( $limit_params );
my $exists = Koha::Item::Transfer::Limits->search( $transfer_limit->unblessed )->count;
unless ( $exists ) {
$transfer_limit->store;
push( @results, $transfer_limit->to_api());
}
my $key = sprintf(
"%s:%s:%s:%s", $limit_params->{from_branch_id} || q{},
$limit_params->{to_branch_id} || q{}, $limit_params->{item_type} || q{},
$limit_params->{collection_code} || q{}
);
next if exists $existing_limits{$key};
my $transfer_limit = Koha::Item::Transfer::Limit->new_from_api($limit_params);
$transfer_limit->store;
push( @results, $transfer_limit->to_api() );
}
}
my $transfer_limit = Koha::Item::Transfer::Limit->new_from_api( $params );
return $c->render(
status => 201,
openapi => \@results
);
}
catch {
} catch {
$c->unhandled_exception($_);
};
}