Change ImageTree to only use a single loop over the images when building. This should be slightly faster on large image sets

This commit is contained in:
Joseph Schorr 2015-03-04 16:53:22 -05:00
parent c47c9d6cb4
commit 4f04ad2acd

View file

@ -1,14 +1,15 @@
class ImageTreeNode(object): class ImageTreeNode(object):
""" A node in the image tree. """ """ A node in the image tree. """
def __init__(self, image): def __init__(self, image, child_map):
self.image = image self.image = image
self.parent = None self.parent = None
self.children = []
self.tags = [] self.tags = []
def add_child(self, child): self._child_map = child_map
self.children.append(child)
child.parent = self @property
def children(self):
return self._child_map.get(str(self.image.id), [])
def add_tag(self, tag): def add_tag(self, tag):
self.tags.append(tag) self.tags.append(tag)
@ -18,8 +19,8 @@ class ImageTree(object):
""" In-memory tree for easy traversal and lookup of images in a repository. """ """ In-memory tree for easy traversal and lookup of images in a repository. """
def __init__(self, all_images, all_tags, base_filter=None): def __init__(self, all_images, all_tags, base_filter=None):
self._tag_map = {}
self._image_map = {} self._image_map = {}
self._child_map = {}
self._build(all_images, all_tags, base_filter) self._build(all_images, all_tags, base_filter)
@ -33,18 +34,17 @@ class ImageTree(object):
if image.id != base_filter and not str(base_filter) in ancestors: if image.id != base_filter and not str(base_filter) in ancestors:
continue continue
self._image_map[image.id] = ImageTreeNode(image) # Create the node for the image.
image_node = ImageTreeNode(image, self._child_map)
self._image_map[image.id] = image_node
# Connect the nodes to their parents. # Add the node to the child map for its parent image (if any).
for image_node in self._image_map.values():
image = image_node.image
parent_image_id = image.ancestors.split('/')[-2] if image.ancestors else None parent_image_id = image.ancestors.split('/')[-2] if image.ancestors else None
if not parent_image_id: if parent_image_id:
continue if not parent_image_id in self._child_map:
self._child_map[parent_image_id] = []
parent_node = self._image_map.get(int(parent_image_id)) self._child_map[parent_image_id].append(image_node)
if parent_node is not None:
parent_node.add_child(image_node)
# Build the tag map. # Build the tag map.
for tag in all_tags: for tag in all_tags:
@ -52,7 +52,6 @@ class ImageTree(object):
if not image_node: if not image_node:
continue continue
self._tag_map = image_node
image_node.add_tag(tag.name) image_node.add_tag(tag.name)