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):
""" A node in the image tree. """
def __init__(self, image):
def __init__(self, image, child_map):
self.image = image
self.parent = None
self.children = []
self.tags = []
def add_child(self, child):
self.children.append(child)
child.parent = self
self._child_map = child_map
@property
def children(self):
return self._child_map.get(str(self.image.id), [])
def add_tag(self, 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. """
def __init__(self, all_images, all_tags, base_filter=None):
self._tag_map = {}
self._image_map = {}
self._child_map = {}
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:
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.
for image_node in self._image_map.values():
image = image_node.image
# Add the node to the child map for its parent image (if any).
parent_image_id = image.ancestors.split('/')[-2] if image.ancestors else None
if not parent_image_id:
continue
if parent_image_id:
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))
if parent_node is not None:
parent_node.add_child(image_node)
self._child_map[parent_image_id].append(image_node)
# Build the tag map.
for tag in all_tags:
@ -52,7 +52,6 @@ class ImageTree(object):
if not image_node:
continue
self._tag_map = image_node
image_node.add_tag(tag.name)