Add a batch get_matching_tags_for_images method

This will be used in the security notification worker to retrieving the tags needed in a set of batch calls, rather than multiple calls per image
This commit is contained in:
Joseph Schorr 2017-05-02 15:38:25 -04:00
parent e583be3914
commit 74dd0ef8e8
4 changed files with 162 additions and 36 deletions

View file

@ -68,7 +68,8 @@ def _orphaned_storage_query(candidate_ids):
.from_(storage_subq))
# Build the set of storages that are missing. These storages are orphaned.
nonorphaned_storage_ids = {storage.id for storage in _reduce_as_tree(nonorphaned_queries)}
nonorphaned_storage_ids = {storage.id for storage
in _basequery.reduce_as_tree(nonorphaned_queries)}
return list(candidate_ids - nonorphaned_storage_ids)
@ -275,31 +276,7 @@ def lookup_repo_storages_by_content_checksum(repo, checksums):
.select(SQL('*'))
.from_(candidate_subq))
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)
return _basequery.reduce_as_tree(queries)
def set_image_storage_metadata(docker_image_id, namespace_name, repository_name, image_size,