From e583be391406bc3329b7befb727fba362dba7066 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Fri, 28 Apr 2017 20:10:54 -0400 Subject: [PATCH] Remove inner query for ancestors lookup on get_matching_tags --- data/model/image.py | 23 +++++++++-------------- data/model/tag.py | 9 +++++++-- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/data/model/image.py b/data/model/image.py index 5ec12682e..7ce160aa7 100644 --- a/data/model/image.py +++ b/data/model/image.py @@ -17,23 +17,18 @@ from util.canonicaljson import canonicalize logger = logging.getLogger(__name__) -def get_repository_image_and_deriving(docker_image_id, storage_uuid): - """ Returns all matching images with the given docker image ID and storage uuid, along with any - images which have the image ID as parents. +def get_image_with_storage(docker_image_id, storage_uuid): + """ Returns the image with the given docker image ID and storage uuid or None if none. """ try: - image_found = (Image - .select() - .join(ImageStorage) - .where(Image.docker_image_id == docker_image_id, - ImageStorage.uuid == storage_uuid) - .get()) + return (Image + .select() + .join(ImageStorage) + .where(Image.docker_image_id == docker_image_id, + ImageStorage.uuid == storage_uuid) + .get()) except Image.DoesNotExist: - return Image.select().where(Image.id < 0) # Empty query - - ancestors_pattern = '%s%s/%%' % (image_found.ancestors, image_found.id) - return Image.select().where((Image.ancestors ** ancestors_pattern) | - (Image.id == image_found.id)) + return None def get_parent_images_with_placements(namespace_name, repository_name, image_obj): diff --git a/data/model/tag.py b/data/model/tag.py index e4c7b90cd..c6112a092 100644 --- a/data/model/tag.py +++ b/data/model/tag.py @@ -56,14 +56,19 @@ def _tag_alive(query, now_ts=None): def get_matching_tags(docker_image_id, storage_uuid, *args): """ Returns a query pointing to all tags that contain the image with the given docker_image_id and storage_uuid. """ - image_query = image.get_repository_image_and_deriving(docker_image_id, storage_uuid) + image_row = image.get_image_with_storage(docker_image_id, storage_uuid) + if image_row is None: + return RepositoryTag.select().where(RepositoryTag.id < 0) # Empty query. + ancestors_str = '%s%s/%%' % (image_row.ancestors, image_row.id) return _tag_alive(RepositoryTag .select(*args) .distinct() .join(Image) .join(ImageStorage) - .where(Image.id << image_query, RepositoryTag.hidden == False)) + .where(RepositoryTag.hidden == False) + .where((Image.id == image_row.id) | + (Image.ancestors ** ancestors_str))) def get_tags_for_image(image_id, *args):