From 76db56635e4277804982f04b3828a8cd88093963 Mon Sep 17 00:00:00 2001 From: "mkanat%bugzilla.org" <> Date: Thu, 27 Mar 2008 10:08:07 +0000 Subject: Bug 372795: Implement Bugzilla::Product::preload() to speed up query.cgi when there are many products Patch By Max Kanat-Alexander r=LpSolit, a=mkanat --- Bugzilla/Product.pm | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) (limited to 'Bugzilla/Product.pm') diff --git a/Bugzilla/Product.pm b/Bugzilla/Product.pm index 45c489973..edc621f67 100644 --- a/Bugzilla/Product.pm +++ b/Bugzilla/Product.pm @@ -51,6 +51,32 @@ use constant DB_COLUMNS => qw( products.defaultmilestone ); +############################### +#### Constructors ##### +############################### + +# This is considerably faster than calling new_from_list three times +# for each product in the list, particularly with hundreds or thousands +# of products. +sub preload { + my ($products) = @_; + my %prods = map { $_->id => $_ } @$products; + my @prod_ids = keys %prods; + return unless @prod_ids; + + my $dbh = Bugzilla->dbh; + foreach my $field (qw(component version milestone)) { + my $classname = "Bugzilla::" . ucfirst($field); + my $objects = $classname->match({ product_id => \@prod_ids }); + + # Now populate the products with this set of objects. + foreach my $obj (@$objects) { + my $product_id = $obj->product_id; + $prods{$product_id}->{"${field}s"} ||= []; + push(@{$prods{$product_id}->{"${field}s"}}, $obj); + } + } +} ############################### #### Methods #### @@ -300,7 +326,7 @@ below. =over -=item C +=item C Description: Returns an array of component objects belonging to the product. @@ -319,7 +345,7 @@ below. Returns: A hash with group id as key and hash containing a Bugzilla::Group object and the properties of group relative to the product. - + =item C =over @@ -356,7 +382,7 @@ groups, in this product. =back -=item C +=item C Description: Returns all valid versions for that product. @@ -364,7 +390,7 @@ groups, in this product. Returns: An array of Bugzilla::Version objects. -=item C +=item C Description: Returns all valid milestones for that product. @@ -415,6 +441,15 @@ groups, in this product. =over +=item C + +When passed an arrayref of C objects, preloads their +L, L, and L, which is much faster +than calling those accessors on every item in the array individually. + +This function is not exported, so must be called like +C. + =item C Description: Checks if the product name was passed in and if is a valid -- cgit v1.2.3-24-g4f1b