Merge lp:~rvb/launchpad/revert-14355 into lp:launchpad

Proposed by Raphaël Badin
Status: Merged
Merged at revision: 14374
Proposed branch: lp:~rvb/launchpad/revert-14355
Merge into: lp:launchpad
Diff against target: 188 lines (+22/-77)
1 file modified
lib/lp/code/model/branchcollection.py (+22/-77)
To merge this branch: bzr merge lp:~rvb/launchpad/revert-14355
Reviewer Review Type Date Requested Status
Raphaël Badin (community) Approve
Review via email: mp+83183@code.launchpad.net

Commit message

Revert r14355.

Description of the change

This branch reverts r14355. The refactored query (which uses 2 CTEs, see OOPS-8c4f00a21be3b28698de236433c26fd3) as a worse performance than the original one. Reverted MP: https://code.launchpad.net/~rvb/launchpad/activereviews-bug-867941-hugequery/+merge/82650.

To post a comment you must log in.
Revision history for this message
Raphaël Badin (rvb) wrote :

This is a straight revert so I'm self reviewing.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'lib/lp/code/model/branchcollection.py'
--- lib/lp/code/model/branchcollection.py 2011-11-22 10:18:22 +0000
+++ lib/lp/code/model/branchcollection.py 2011-11-23 16:10:42 +0000
@@ -128,16 +128,6 @@
128 if exclude_from_search is None:128 if exclude_from_search is None:
129 exclude_from_search = []129 exclude_from_search = []
130 self._exclude_from_search = exclude_from_search130 self._exclude_from_search = exclude_from_search
131 self._with_dict = {}
132
133 @property
134 def store_with_with(self):
135 store = self.store
136 if len(self._with_dict) != 0:
137 with_expr = [With(name, expr)
138 for (name, expr) in self._with_dict.items()]
139 store = store.with_(with_expr)
140 return store
141131
142 def count(self):132 def count(self):
143 """See `IBranchCollection`."""133 """See `IBranchCollection`."""
@@ -275,8 +265,7 @@
275 self._tables.values() + self._asymmetric_tables.values())265 self._tables.values() + self._asymmetric_tables.values())
276 tables = [Branch] + list(all_tables)266 tables = [Branch] + list(all_tables)
277 expressions = self._getBranchExpressions()267 expressions = self._getBranchExpressions()
278 resultset = self.store_with_with.using(268 resultset = self.store.using(*tables).find(Branch, *expressions)
279 *tables).find(Branch, *expressions)
280 if not eager_load:269 if not eager_load:
281 return resultset270 return resultset
282271
@@ -297,15 +286,6 @@
297 target_branch=None, merged_revnos=None,286 target_branch=None, merged_revnos=None,
298 eager_load=False):287 eager_load=False):
299 """See `IBranchCollection`."""288 """See `IBranchCollection`."""
300 return self._getMergeProposals(
301 statuses=statuses, for_branches=for_branches,
302 target_branch=target_branch, merged_revnos=merged_revnos,
303 eager_load=eager_load)
304
305 def _getMergeProposals(self, statuses=None, for_branches=None,
306 target_branch=None, merged_revnos=None,
307 eager_load=False, return_ids=False,
308 include_with=True):
309 if for_branches is not None and not for_branches:289 if for_branches is not None and not for_branches:
310 # We have an empty branches list, so we can shortcut.290 # We have an empty branches list, so we can shortcut.
311 return EmptyResultSet()291 return EmptyResultSet()
@@ -317,19 +297,15 @@
317 target_branch is not None or297 target_branch is not None or
318 merged_revnos is not None):298 merged_revnos is not None):
319 return self._naiveGetMergeProposals(statuses, for_branches,299 return self._naiveGetMergeProposals(statuses, for_branches,
320 target_branch, merged_revnos, eager_load,300 target_branch, merged_revnos, eager_load)
321 return_ids=return_ids, include_with=include_with)
322 else:301 else:
323 # When examining merge proposals in a scope, this is a moderately302 # When examining merge proposals in a scope, this is a moderately
324 # effective set of constrained queries. It is not effective when303 # effective set of constrained queries. It is not effective when
325 # unscoped or when tight constraints on branches are present.304 # unscoped or when tight constraints on branches are present.
326 return self._scopedGetMergeProposals(305 return self._scopedGetMergeProposals(statuses)
327 statuses, return_ids=return_ids)
328306
329 def _naiveGetMergeProposals(self, statuses=None, for_branches=None,307 def _naiveGetMergeProposals(self, statuses=None, for_branches=None,
330 target_branch=None, merged_revnos=None,308 target_branch=None, merged_revnos=None, eager_load=False):
331 eager_load=False, return_ids=False,
332 include_with=True):
333309
334 def do_eager_load(rows):310 def do_eager_load(rows):
335 branch_ids = set()311 branch_ids = set()
@@ -386,20 +362,14 @@
386 if statuses is not None:362 if statuses is not None:
387 expressions.append(363 expressions.append(
388 BranchMergeProposal.queue_status.is_in(statuses))364 BranchMergeProposal.queue_status.is_in(statuses))
389 if include_with:365 resultset = self.store.using(*tables).find(
390 store = self.store_with_with366 BranchMergeProposal, *expressions)
391 else:
392 store = self.store
393 returned_obj = (
394 BranchMergeProposal.id if return_ids else BranchMergeProposal)
395 resultset = store.using(*tables).find(
396 returned_obj, *expressions)
397 if not eager_load:367 if not eager_load:
398 return resultset368 return resultset
399 else:369 else:
400 return DecoratedResultSet(resultset, pre_iter_hook=do_eager_load)370 return DecoratedResultSet(resultset, pre_iter_hook=do_eager_load)
401371
402 def _scopedGetMergeProposals(self, statuses, return_ids=False):372 def _scopedGetMergeProposals(self, statuses):
403 scope_tables = [Branch] + self._tables.values()373 scope_tables = [Branch] + self._tables.values()
404 scope_expressions = self._branch_filter_expressions374 scope_expressions = self._branch_filter_expressions
405 select = self.store.using(*scope_tables).find(375 select = self.store.using(*scope_tables).find(
@@ -422,23 +392,16 @@
422 if statuses is not None:392 if statuses is not None:
423 expressions.append(393 expressions.append(
424 BranchMergeProposal.queue_status.is_in(statuses))394 BranchMergeProposal.queue_status.is_in(statuses))
425 returned_obj = (
426 BranchMergeProposal.id if return_ids else BranchMergeProposal)
427 return self.store.with_(with_expr).using(*tables).find(395 return self.store.with_(with_expr).using(*tables).find(
428 returned_obj, *expressions)396 BranchMergeProposal, *expressions)
429397
430 def getMergeProposalsForPerson(self, person, status=None):398 def getMergeProposalsForPerson(self, person, status=None):
431 """See `IBranchCollection`."""399 """See `IBranchCollection`."""
432 # We want to limit the proposals to those where the source branch is400 # We want to limit the proposals to those where the source branch is
433 # limited by the defined collection.401 # limited by the defined collection.
434 owned = self.ownedBy(person)._getMergeProposals(402 owned = self.ownedBy(person).getMergeProposals(status)
435 status, include_with=False, return_ids=True)403 reviewing = self.getMergeProposalsForReviewer(person, status)
436 reviewing = self._getMergeProposalsForReviewer(404 resultset = owned.union(reviewing)
437 person, status, include_with=False, return_ids=True)
438 result = owned.union(reviewing)
439 resultset = self.store_with_with.using([BranchMergeProposal]).find(
440 BranchMergeProposal,
441 In(BranchMergeProposal.id, result._get_select()))
442405
443 def do_eager_load(rows):406 def do_eager_load(rows):
444 # Load the related diffs.407 # Load the related diffs.
@@ -468,10 +431,6 @@
468431
469 def getMergeProposalsForReviewer(self, reviewer, status=None):432 def getMergeProposalsForReviewer(self, reviewer, status=None):
470 """See `IBranchCollection`."""433 """See `IBranchCollection`."""
471 return self._getMergeProposalsForReviewer(reviewer, status=status)
472
473 def _getMergeProposalsForReviewer(self, reviewer, status=None,
474 include_with=True, return_ids=False):
475 tables = [434 tables = [
476 BranchMergeProposal,435 BranchMergeProposal,
477 Join(CodeReviewVoteReference,436 Join(CodeReviewVoteReference,
@@ -491,14 +450,8 @@
491 if status is not None:450 if status is not None:
492 expressions.append(451 expressions.append(
493 BranchMergeProposal.queue_status.is_in(status))452 BranchMergeProposal.queue_status.is_in(status))
494 if include_with:453 proposals = self.store.using(*tables).find(
495 store = self.store_with_with454 BranchMergeProposal, *expressions)
496 else:
497 store = self.store
498 returned_obj = (
499 BranchMergeProposal.id if return_ids else BranchMergeProposal)
500 proposals = store.using(*tables).find(
501 returned_obj, *expressions)
502 # Apply sorting here as we can't do it in the browser code. We need455 # Apply sorting here as we can't do it in the browser code. We need
503 # to think carefully about the best places to do this, but not here456 # to think carefully about the best places to do this, but not here
504 # nor now.457 # nor now.
@@ -900,29 +853,21 @@
900 Branch.transitively_private == True)))853 Branch.transitively_private == True)))
901 return private_branches854 return private_branches
902855
903 def _addVisibleBranchesCTE(self):
904 """Add the 'visible_branches' CTE to self._with_dict.
905
906 """
907 public_branches = Branch.transitively_private == False
908 if self._private_branch_ids is not None:
909 branches = Or(
910 public_branches,
911 Branch.id.is_in(self._private_branch_ids))
912 else:
913 branches = public_branches
914 branches_select = self.store.using(
915 [Branch]).find(Branch.id, branches)._get_select()
916 self._with_dict.update({'visible_branches': branches_select})
917
918 def _getBranchVisibilityExpression(self, branch_class=Branch):856 def _getBranchVisibilityExpression(self, branch_class=Branch):
919 """Return the where clauses for visibility.857 """Return the where clauses for visibility.
920858
921 :param branch_class: The Branch class to use - permits using859 :param branch_class: The Branch class to use - permits using
922 ClassAliases.860 ClassAliases.
923 """861 """
924 self._addVisibleBranchesCTE()862 public_branches = branch_class.transitively_private == False
925 return [branch_class.id.is_in(SQL("SELECT id FROM visible_branches"))]863 if self._private_branch_ids is None:
864 # Public only.
865 return [public_branches]
866 else:
867 public_or_private = Or(
868 public_branches,
869 branch_class.id.is_in(self._private_branch_ids))
870 return [public_or_private]
926871
927 def _getCandidateBranchesWith(self):872 def _getCandidateBranchesWith(self):
928 """Return WITH clauses defining candidate branches.873 """Return WITH clauses defining candidate branches.