Reducing in a tree to avoid recursion depth limits

This commit is contained in:
Jake Moshenko 2015-11-23 15:50:25 -05:00
parent cd4d94207b
commit 3a29dfc535

View file

@ -238,7 +238,31 @@ def lookup_repo_storages_by_content_checksum(repo, checksums):
.select(SQL('*'))
.from_(candidate_subq))
return reduce(lambda l, r: l.union_all(r), queries)
return _reduce_as_tree(queries)
def _reduce_as_tree(queries_to_reduce):
""" This method will split a list of queries into halves recursively until we reach individual
queries, at which point it will start unioning the queries, or the already unioned subqueries.
This works around a bug in peewee SQL generation where reducing linearly generates a chain
of queries that will exceed the recursion depth limit when it has around 80 queries.
"""
mid = len(queries_to_reduce)/2
left = queries_to_reduce[:mid]
right = queries_to_reduce[mid:]
to_reduce_right = right[0]
if len(right) > 1:
to_reduce_right = _reduce_as_tree(right)
if len(left) > 1:
to_reduce_left = _reduce_as_tree(left)
elif len(left) == 1:
to_reduce_left = left[0]
else:
return to_reduce_right
return to_reduce_left.union_all(to_reduce_right)
def get_storage_locations(uuid):